1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Simple utility to make a single-image install kernel with initial ramdisk
4 for Sparc tftpbooting without need to set up nfs.
6 Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
8 Copyright (C) 2011 Sam Ravnborg <sam@ravnborg.org>
21 #include <sys/types.h>
25 * Note: run this on an a.out kernel (use elftoaout for it),
26 * as PROM looks for a.out image only.
29 #define AOUT_TEXT_OFFSET 32
31 static int is64bit
= 0;
33 /* align to power-of-two size */
34 static int align(int n
)
37 return (n
+ 0x1fff) & ~0x1fff;
39 return (n
+ 0xfff) & ~0xfff;
42 /* read two bytes as big endian */
43 static unsigned short ld2(char *p
)
45 return (p
[0] << 8) | p
[1];
48 /* save 4 bytes as big endian */
49 static void st4(char *p
, unsigned int x
)
57 static void die(const char *str
)
63 static void usage(void)
65 /* fs_img.gz is an image of initial ramdisk. */
66 fprintf(stderr
, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
67 fprintf(stderr
, "\tKernel image will be modified in place.\n");
71 static int start_line(const char *line
)
73 if (strcmp(line
+ 10, " _start\n") == 0)
75 else if (strcmp(line
+ 18, " _start\n") == 0)
80 static int end_line(const char *line
)
82 if (strcmp(line
+ 10, " _end\n") == 0)
84 else if (strcmp (line
+ 18, " _end\n") == 0)
90 * Find address for start and end in System.map.
91 * The file looks like this:
96 * There is support for 64 bit addresses too.
98 * Return 0 if either start or end is not found
100 static int get_start_end(const char *filename
, unsigned int *start
,
108 map
= fopen(filename
, "r");
111 while (fgets(buffer
, 1024, map
)) {
112 if (start_line(buffer
))
113 *start
= strtoul(buffer
, NULL
, 16);
114 else if (end_line(buffer
))
115 *end
= strtoul(buffer
, NULL
, 16);
119 if (*start
== 0 || *end
== 0)
125 #define LOOKBACK (128 * 4)
128 * Find the HdrS entry from head_32/head_64.
129 * We check if it is at the beginning of the file (sparc64 case)
130 * and if not we search for it.
131 * When we search do so in steps of 4 as HdrS is on a 4-byte aligned
132 * address (it is on same alignment as sparc instructions)
133 * Return the offset to the HdrS entry (as off_t)
135 static off_t
get_hdrs_offset(int kernelfd
, const char *filename
)
137 char buffer
[BUFSIZE
];
141 if (lseek(kernelfd
, 0, SEEK_SET
) < 0)
143 if (read(kernelfd
, buffer
, BUFSIZE
) != BUFSIZE
)
146 if (buffer
[40] == 'H' && buffer
[41] == 'd' &&
147 buffer
[42] == 'r' && buffer
[43] == 'S') {
150 /* Find the gokernel label */
151 /* Decode offset from branch instruction */
152 offset
= ld2(buffer
+ AOUT_TEXT_OFFSET
+ 2) << 2;
153 /* Go back 512 bytes so we do not miss HdrS */
155 /* skip a.out header */
156 offset
+= AOUT_TEXT_OFFSET
;
157 if (lseek(kernelfd
, offset
, SEEK_SET
) < 0)
159 if (read(kernelfd
, buffer
, BUFSIZE
) != BUFSIZE
)
162 for (i
= 0; i
< LOOKBACK
; i
+= 4) {
163 if (buffer
[i
+ 0] == 'H' && buffer
[i
+ 1] == 'd' &&
164 buffer
[i
+ 2] == 'r' && buffer
[i
+ 3] == 'S') {
169 fprintf (stderr
, "Couldn't find headers signature in %s\n", filename
);
173 int main(int argc
,char **argv
)
175 static char aout_magic
[] = { 0x01, 0x03, 0x01, 0x07 };
177 unsigned int i
, start
, end
;
184 if (strcmp(argv
[1], "64") == 0)
186 if (stat (argv
[4], &s
) < 0)
189 if (!get_start_end(argv
[3], &start
, &end
)) {
190 fprintf(stderr
, "Could not determine start and end from %s\n",
194 if ((image
= open(argv
[2], O_RDWR
)) < 0)
196 if (read(image
, buffer
, 512) != 512)
198 if (memcmp(buffer
, aout_magic
, 4) != 0) {
199 fprintf (stderr
, "Not a.out. Don't blame me.\n");
203 * We need to fill in values for
204 * sparc_ramdisk_image + sparc_ramdisk_size
205 * To locate these symbols search for the "HdrS" text which appear
206 * in the image a little before the gokernel symbol.
207 * See definition of these in init_32.S
210 offset
= get_hdrs_offset(image
, argv
[2]);
211 /* skip HdrS + LINUX_VERSION_CODE + HdrS version */
214 if (lseek(image
, offset
, 0) < 0)
219 * root_dev = 1 (RAMDISK_MAJOR)
221 * sparc_ramdisk_image = "PAGE aligned address after _end")
222 * sparc_ramdisk_size = size of image
225 st4(buffer
+ 4, 0x01000000);
226 st4(buffer
+ 8, align(end
+ 32));
227 st4(buffer
+ 12, s
.st_size
);
229 if (write(image
, buffer
+ 2, 14) != 14)
232 /* For sparc64 update a_text and clear a_data + a_bss */
235 if (lseek(image
, 4, 0) < 0)
238 st4(buffer
, align(end
+ 32 + 8191) - (start
& ~0x3fffffUL
) +
244 if (write(image
, buffer
, 12) != 12)
248 /* seek page aligned boundary in the image file and add boot image */
249 if (lseek(image
, AOUT_TEXT_OFFSET
- start
+ align(end
+ 32), 0) < 0)
251 if ((tail
= open(argv
[4], O_RDONLY
)) < 0)
253 while ((i
= read(tail
, buffer
, 1024)) > 0)
254 if (write(image
, buffer
, i
) != i
)
256 if (close(image
) < 0)