blob: cfd95ec01f186f9d7528f27f4b2c969ccf344260 [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Simple utility to make a single-image install kernel with initial ramdisk
3 for Sparc tftpbooting without need to set up nfs.
4
5 Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000021
22#include <dirent.h>
23#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <string.h>
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000025#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000029#include <stdio.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sys/types.h>
32#include <sys/stat.h>
33
34/*
35 * Note: run this on an a.out kernel (use elftoaout for it),
36 * as PROM looks for a.out image only.
37 */
38
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000039/* read two bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070040static unsigned short ld2(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 return (p[0] << 8) | p[1];
43}
44
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000045/* read 4 bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070046static unsigned int ld4(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
49}
50
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000051/* save 4 bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070052static void st4(char *p, unsigned int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 p[0] = x >> 24;
55 p[1] = x >> 16;
56 p[2] = x >> 8;
57 p[3] = x;
58}
59
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000060static void die(const char *str)
61{
62 perror(str);
63 exit(1);
64}
65
Josh Triplettc843e312009-10-16 14:58:29 -070066static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 /* fs_img.gz is an image of initial ramdisk. */
69 fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
70 fprintf(stderr, "\tKernel image will be modified in place.\n");
71 exit(1);
72}
73
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000074static int start_line(const char *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000076 if (strcmp(line + 8, " T start\n") == 0)
77 return 1;
78 else if (strcmp(line + 16, " T start\n") == 0)
79 return 1;
80 return 0;
81}
82
83static int end_line(const char *line)
84{
85 if (strcmp(line + 8, " A _end\n") == 0)
86 return 1;
87 else if (strcmp (line + 16, " A _end\n") == 0)
88 return 1;
89 return 0;
90}
91
92/*
93 * Find address for start and end in System.map.
94 * The file looks like this:
95 * f0004000 T start
96 * f0379f79 A _end
97 * 1234567890123456
98 * ^coloumn 1
99 * There is support for 64 bit addresses too.
100 *
101 * Return 0 if either start or end is not found
102 */
103static int get_start_end(const char *filename, unsigned int *start, unsigned int *end)
104{
105 FILE *map;
106 char buffer[1024];
107
108 *start = 0;
109 *end = 0;
110 map = fopen(filename, "r");
111 if (!map)
112 die(filename);
113 while (fgets(buffer, 1024, map)) {
114 if (start_line(buffer))
115 *start = strtoul(buffer, NULL, 16);
116 else if (end_line(buffer))
117 *end = strtoul(buffer, NULL, 16);
118 }
119 fclose (map);
120
121 if (*start == 0 || *end == 0)
122 return 0;
123
124 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127int main(int argc,char **argv)
128{
129 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
Robert Reif22b096a2009-06-21 16:45:44 +0000130 char buffer[1024], *q, *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned int i, j, k, start, end, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct stat s;
133 int image, tail;
134
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000135 if (argc != 4)
136 usage();
137 if (stat (argv[3], &s) < 0)
138 die(argv[3]);
139
140 if (!get_start_end(argv[2], &start, &end)) {
141 fprintf (stderr, "Could not determine start and end from %s\n", argv[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 exit(1);
143 }
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000144 if ((image = open(argv[1], O_RDWR)) < 0)
145 die(argv[1]);
146 if (read(image, buffer, 512) != 512)
147 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (memcmp (buffer, "\177ELF", 4) == 0) {
149 q = buffer + ld4(buffer + 28);
150 i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000151 if (lseek(image, i, 0) < 0)
152 die("lseek");
153 if (read(image, buffer, 512) != 512)
154 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 j = 0;
156 } else if (memcmp(buffer, aout_magic, 4) == 0) {
157 i = j = 32;
158 } else {
159 fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
160 exit(1);
161 }
162 k = i;
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000163 i += (ld2(buffer + j + 2) << 2) - 512;
164 if (lseek(image, i, 0) < 0)
165 die("lseek");
166 if (read(image, buffer, 1024) != 1024)
167 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 for (q = buffer, r = q + 512; q < r; q += 4) {
169 if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
170 break;
171 }
172 if (q == r) {
173 fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
174 exit(1);
175 }
176 offset = i + (q - buffer) + 10;
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000177 if (lseek(image, offset, 0) < 0)
178 die("lseek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 st4(buffer, 0);
181 st4(buffer + 4, 0x01000000);
182 st4(buffer + 8, (end + 32 + 4095) & ~4095);
183 st4(buffer + 12, s.st_size);
184
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000185 if (write(image, buffer + 2, 14) != 14)
186 die(argv[1]);
187 if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0)
188 die("lseek");
189 if ((tail = open(argv[3],O_RDONLY)) < 0)
190 die(argv[3]);
191 while ((i = read (tail, buffer, 1024)) > 0)
192 if (write(image, buffer, i) != i)
193 die(argv[1]);
194 if (close(image) < 0)
195 die("close");
196 if (close(tail) < 0)
197 die("close");
198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}