2 Simple utility to make a single-image install kernel with initial ramdisk
3 for Sparc tftpbooting without need to set up nfs.
5 Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
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.
13 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.
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. */
31 #include <sys/types.h>
35 * Note: run this on an a.out kernel (use elftoaout for it),
36 * as PROM looks for a.out image only.
39 #define AOUT_TEXT_OFFSET 32
41 static int is64bit = 0;
43 /* align to power-of-two size */
44 static int align(int n)
47 return (n + 0x1fff) & ~0x1fff;
49 return (n + 0xfff) & ~0xfff;
52 /* read two bytes as big endian */
53 static unsigned short ld2(char *p)
55 return (p[0] << 8) | p[1];
58 /* save 4 bytes as big endian */
59 static void st4(char *p, unsigned int x)
67 static void die(const char *str)
73 static void usage(void)
75 /* fs_img.gz is an image of initial ramdisk. */
76 fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
77 fprintf(stderr, "\tKernel image will be modified in place.\n");
81 static int start_line(const char *line)
83 if (strcmp(line + 8, " T _start\n") == 0)
85 else if (strcmp(line + 16, " T _start\n") == 0)
90 static int end_line(const char *line)
92 if (strcmp(line + 8, " A _end\n") == 0)
94 else if (strcmp (line + 16, " A _end\n") == 0)
100 * Find address for start and end in System.map.
101 * The file looks like this:
106 * There is support for 64 bit addresses too.
108 * Return 0 if either start or end is not found
110 static int get_start_end(const char *filename, unsigned int *start, unsigned int *end)
117 map = fopen(filename, "r");
120 while (fgets(buffer, 1024, map)) {
121 if (start_line(buffer))
122 *start = strtoul(buffer, NULL, 16);
123 else if (end_line(buffer))
124 *end = strtoul(buffer, NULL, 16);
128 if (*start == 0 || *end == 0)
134 int main(int argc,char **argv)
136 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
137 char buffer[1024], *q, *r;
138 unsigned int i, start, end, offset;
144 if (strcmp(argv[1], "64") == 0)
146 if (stat (argv[4], &s) < 0)
149 if (!get_start_end(argv[3], &start, &end)) {
150 fprintf (stderr, "Could not determine start and end from %s\n", argv[3]);
153 if ((image = open(argv[2], O_RDWR)) < 0)
155 if (read(image, buffer, 512) != 512)
157 if (memcmp(buffer, aout_magic, 4) != 0) {
158 fprintf (stderr, "Not a.out. Don't blame me.\n");
162 * We need to fill in values for sparc_ramdisk_image + sparc_ramdisk_size
163 * To locate these symbols search for the "HdrS" text which appear
164 * in the image a little before the gokernel symbol.
165 * See definition of these in init_32.S
168 /* Find the gokernel label */
169 i = AOUT_TEXT_OFFSET + (ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2) - 512;
170 if (lseek(image, i, 0) < 0)
172 if (read(image, buffer, 1024) != 1024)
174 for (q = buffer, r = q + 512; q < r; q += 4) {
175 if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
179 fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
182 offset = i + (q - buffer) + 10;
183 if (lseek(image, offset, 0) < 0)
188 * root_dev = 1 (RAMDISK_MAJOR)
190 * sparc_ramdisk_image = "PAGE aligned address after _end")
191 * sparc_ramdisk_size = size of image
194 st4(buffer + 4, 0x01000000);
195 st4(buffer + 8, align(end + 32));
196 st4(buffer + 12, s.st_size);
198 if (write(image, buffer + 2, 14) != 14)
201 /* seek page aligned boundary in the image file and add boot image */
202 if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0)
204 if ((tail = open(argv[4], O_RDONLY)) < 0)
206 while ((i = read(tail, buffer, 1024)) > 0)
207 if (write(image, buffer, i) != i)
209 if (close(image) < 0)