piggyback.c (9fec6060d9e48ed7db0dac0e16d0f0f0e615b7f6) piggyback.c (56818a6f9105ac016ecf5907f7cb63bcdab639fe)
1/*
2 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.
1/*
2 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.
12
12
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.
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. */
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.
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. */
21
22#include <stdio.h>
21
22#include <dirent.h>
23#include <stdlib.h>
23#include <string.h>
24#include <string.h>
25#include <unistd.h>
24#include <ctype.h>
25#include <errno.h>
26#include <fcntl.h>
26#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
27#include <dirent.h>
28#include <unistd.h>
29#include <stdlib.h>
29#include <stdio.h>
30
30#include <sys/types.h>
31#include <sys/stat.h>
32
33/*
34 * Note: run this on an a.out kernel (use elftoaout for it),
35 * as PROM looks for a.out image only.
36 */
37
31#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
38unsigned short ld2(char *p)
39#define AOUT_TEXT_OFFSET 32
40
41static int is64bit = 0;
42
43/* align to power-of-two size */
44static int align(int n)
39{
45{
40 return (p[0] << 8) | p[1];
46 if (is64bit)
47 return (n + 0x1fff) & ~0x1fff;
48 else
49 return (n + 0xfff) & ~0xfff;
41}
42
50}
51
43unsigned int ld4(char *p)
52/* read two bytes as big endian */
53static unsigned short ld2(char *p)
44{
54{
45 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
55 return (p[0] << 8) | p[1];
46}
47
56}
57
48void st4(char *p, unsigned int x)
58/* save 4 bytes as big endian */
59static void st4(char *p, unsigned int x)
49{
50 p[0] = x >> 24;
51 p[1] = x >> 16;
52 p[2] = x >> 8;
53 p[3] = x;
54}
55
60{
61 p[0] = x >> 24;
62 p[1] = x >> 16;
63 p[2] = x >> 8;
64 p[3] = x;
65}
66
56void usage(void)
67static void die(const char *str)
57{
68{
69 perror(str);
70 exit(1);
71}
72
73static void usage(void)
74{
58 /* fs_img.gz is an image of initial ramdisk. */
75 /* fs_img.gz is an image of initial ramdisk. */
59 fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
76 fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
60 fprintf(stderr, "\tKernel image will be modified in place.\n");
61 exit(1);
62}
63
77 fprintf(stderr, "\tKernel image will be modified in place.\n");
78 exit(1);
79}
80
64void die(char *str)
81static int start_line(const char *line)
65{
82{
66 perror (str);
83 if (strcmp(line + 8, " T _start\n") == 0)
84 return 1;
85 else if (strcmp(line + 16, " T _start\n") == 0)
86 return 1;
87 return 0;
88}
89
90static int end_line(const char *line)
91{
92 if (strcmp(line + 8, " A _end\n") == 0)
93 return 1;
94 else if (strcmp (line + 16, " A _end\n") == 0)
95 return 1;
96 return 0;
97}
98
99/*
100 * Find address for start and end in System.map.
101 * The file looks like this:
102 * f0004000 T _start
103 * f0379f79 A _end
104 * 1234567890123456
105 * ^coloumn 1
106 * There is support for 64 bit addresses too.
107 *
108 * Return 0 if either start or end is not found
109 */
110static int get_start_end(const char *filename, unsigned int *start,
111 unsigned int *end)
112{
113 FILE *map;
114 char buffer[1024];
115
116 *start = 0;
117 *end = 0;
118 map = fopen(filename, "r");
119 if (!map)
120 die(filename);
121 while (fgets(buffer, 1024, map)) {
122 if (start_line(buffer))
123 *start = strtoul(buffer, NULL, 16);
124 else if (end_line(buffer))
125 *end = strtoul(buffer, NULL, 16);
126 }
127 fclose (map);
128
129 if (*start == 0 || *end == 0)
130 return 0;
131
132 return 1;
133}
134
135#define LOOKBACK (128 * 4)
136#define BUFSIZE 1024
137/*
138 * Find the HdrS entry from head_32/head_64.
139 * We check if it is at the beginning of the file (sparc64 case)
140 * and if not we search for it.
141 * When we search do so in steps of 4 as HdrS is on a 4-byte aligned
142 * address (it is on same alignment as sparc instructions)
143 * Return the offset to the HdrS entry (as off_t)
144 */
145static off_t get_hdrs_offset(int kernelfd, const char *filename)
146{
147 char buffer[BUFSIZE];
148 off_t offset;
149 int i;
150
151 if (lseek(kernelfd, 0, SEEK_SET) < 0)
152 die("lseek");
153 if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
154 die(filename);
155
156 if (buffer[40] == 'H' && buffer[41] == 'd' &&
157 buffer[42] == 'r' && buffer[43] == 'S') {
158 return 40;
159 } else {
160 /* Find the gokernel label */
161 /* Decode offset from branch instruction */
162 offset = ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2;
163 /* Go back 512 bytes so we do not miss HdrS */
164 offset -= LOOKBACK;
165 /* skip a.out header */
166 offset += AOUT_TEXT_OFFSET;
167 if (lseek(kernelfd, offset, SEEK_SET) < 0)
168 die("lseek");
169 if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
170 die(filename);
171
172 for (i = 0; i < LOOKBACK; i += 4) {
173 if (buffer[i + 0] == 'H' && buffer[i + 1] == 'd' &&
174 buffer[i + 2] == 'r' && buffer[i + 3] == 'S') {
175 return offset + i;
176 }
177 }
178 }
179 fprintf (stderr, "Couldn't find headers signature in %s\n", filename);
67 exit(1);
68}
69
70int main(int argc,char **argv)
71{
72 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
180 exit(1);
181}
182
183int main(int argc,char **argv)
184{
185 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
73 unsigned char buffer[1024], *q, *r;
74 unsigned int i, j, k, start, end, offset;
75 FILE *map;
186 char buffer[1024];
187 unsigned int i, start, end;
188 off_t offset;
76 struct stat s;
77 int image, tail;
78
189 struct stat s;
190 int image, tail;
191
79 if (argc != 4) usage();
80 start = end = 0;
81 if (stat (argv[3], &s) < 0) die (argv[3]);
82 map = fopen (argv[2], "r");
83 if (!map) die(argv[2]);
84 while (fgets (buffer, 1024, map)) {
85 if (!strcmp (buffer + 8, " T start\n") || !strcmp (buffer + 16, " T start\n"))
86 start = strtoul (buffer, NULL, 16);
87 else if (!strcmp (buffer + 8, " A end\n") || !strcmp (buffer + 16, " A end\n"))
88 end = strtoul (buffer, NULL, 16);
89 }
90 fclose (map);
91 if (!start || !end) {
92 fprintf (stderr, "Could not determine start and end from System.map\n");
192 if (argc != 5)
193 usage();
194 if (strcmp(argv[1], "64") == 0)
195 is64bit = 1;
196 if (stat (argv[4], &s) < 0)
197 die(argv[4]);
198
199 if (!get_start_end(argv[3], &start, &end)) {
200 fprintf(stderr, "Could not determine start and end from %s\n",
201 argv[3]);
93 exit(1);
94 }
202 exit(1);
203 }
95 if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
96 if (read(image,buffer,512) != 512) die(argv[1]);
97 if (memcmp (buffer, "\177ELF", 4) == 0) {
98 q = buffer + ld4(buffer + 28);
99 i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
100 if (lseek(image,i,0) < 0) die("lseek");
101 if (read(image,buffer,512) != 512) die(argv[1]);
102 j = 0;
103 } else if (memcmp(buffer, aout_magic, 4) == 0) {
104 i = j = 32;
105 } else {
106 fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
204 if ((image = open(argv[2], O_RDWR)) < 0)
205 die(argv[2]);
206 if (read(image, buffer, 512) != 512)
207 die(argv[2]);
208 if (memcmp(buffer, aout_magic, 4) != 0) {
209 fprintf (stderr, "Not a.out. Don't blame me.\n");
107 exit(1);
108 }
210 exit(1);
211 }
109 k = i;
110 i += (ld2(buffer + j + 2)<<2) - 512;
111 if (lseek(image,i,0) < 0) die("lseek");
112 if (read(image,buffer,1024) != 1024) die(argv[1]);
113 for (q = buffer, r = q + 512; q < r; q += 4) {
114 if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
115 break;
116 }
117 if (q == r) {
118 fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
119 exit(1);
120 }
121 offset = i + (q - buffer) + 10;
122 if (lseek(image, offset, 0) < 0) die ("lseek");
212 /*
213 * We need to fill in values for
214 * sparc_ramdisk_image + sparc_ramdisk_size
215 * To locate these symbols search for the "HdrS" text which appear
216 * in the image a little before the gokernel symbol.
217 * See definition of these in init_32.S
218 */
123
219
220 offset = get_hdrs_offset(image, argv[2]);
221 /* skip HdrS + LINUX_VERSION_CODE + HdrS version */
222 offset += 10;
223
224 if (lseek(image, offset, 0) < 0)
225 die("lseek");
226
227 /*
228 * root_flags = 0
229 * root_dev = 1 (RAMDISK_MAJOR)
230 * ram_flags = 0
231 * sparc_ramdisk_image = "PAGE aligned address after _end")
232 * sparc_ramdisk_size = size of image
233 */
124 st4(buffer, 0);
125 st4(buffer + 4, 0x01000000);
234 st4(buffer, 0);
235 st4(buffer + 4, 0x01000000);
126 st4(buffer + 8, (end + 32 + 4095) & ~4095);
236 st4(buffer + 8, align(end + 32));
127 st4(buffer + 12, s.st_size);
128
237 st4(buffer + 12, s.st_size);
238
129 if (write(image,buffer+2,14) != 14) die (argv[1]);
130 if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0) die ("lseek");
131 if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
132 while ((i = read (tail,buffer,1024)) > 0)
133 if (write(image,buffer,i) != i) die (argv[1]);
134 if (close(image) < 0) die("close");
135 if (close(tail) < 0) die("close");
136 return 0;
239 if (write(image, buffer + 2, 14) != 14)
240 die(argv[2]);
241
242 /* For sparc64 update a_text and clear a_data + a_bss */
243 if (is64bit)
244 {
245 if (lseek(image, 4, 0) < 0)
246 die("lseek");
247 /* a_text */
248 st4(buffer, align(end + 32 + 8191) - (start & ~0x3fffffUL) +
249 s.st_size);
250 /* a_data */
251 st4(buffer + 4, 0);
252 /* a_bss */
253 st4(buffer + 8, 0);
254 if (write(image, buffer, 12) != 12)
255 die(argv[2]);
256 }
257
258 /* seek page aligned boundary in the image file and add boot image */
259 if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0)
260 die("lseek");
261 if ((tail = open(argv[4], O_RDONLY)) < 0)
262 die(argv[4]);
263 while ((i = read(tail, buffer, 1024)) > 0)
264 if (write(image, buffer, i) != i)
265 die(argv[2]);
266 if (close(image) < 0)
267 die("close");
268 if (close(tail) < 0)
269 die("close");
270 return 0;
137}
271}