32 #include <sys/types.h>
40 #define AOUT_TEXT_OFFSET 32
42 static int is64bit = 0;
48 return (n + 0x1fff) & ~0x1fff;
50 return (n + 0xfff) & ~0xfff;
54 static unsigned short ld2(
char *
p)
56 return (p[0] << 8) | p[1];
60 static void st4(
char *p,
unsigned int x)
68 static void die(
const char *
str)
74 static void usage(
void)
77 fprintf(stderr,
"Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
78 fprintf(stderr,
"\tKernel image will be modified in place.\n");
82 static int start_line(
const char *
line)
84 if (
strcmp(line + 10,
" _start\n") == 0)
86 else if (
strcmp(line + 18,
" _start\n") == 0)
91 static int end_line(
const char *line)
93 if (
strcmp(line + 10,
" _end\n") == 0)
95 else if (
strcmp (line + 18,
" _end\n") == 0)
111 static int get_start_end(
const char *
filename,
unsigned int *
start,
119 map = fopen(filename,
"r");
122 while (fgets(buffer, 1024, map)) {
123 if (start_line(buffer))
124 *start = strtoul(buffer,
NULL, 16);
125 else if (end_line(buffer))
126 *end = strtoul(buffer,
NULL, 16);
130 if (*start == 0 || *end == 0)
136 #define LOOKBACK (128 * 4)
146 static off_t get_hdrs_offset(
int kernelfd,
const char *filename)
152 if (lseek(kernelfd, 0,
SEEK_SET) < 0)
157 if (buffer[40] ==
'H' && buffer[41] ==
'd' &&
158 buffer[42] ==
'r' && buffer[43] ==
'S') {
168 if (lseek(kernelfd, offset,
SEEK_SET) < 0)
174 if (buffer[i + 0] ==
'H' && buffer[i + 1] ==
'd' &&
175 buffer[i + 2] ==
'r' && buffer[i + 3] ==
'S') {
180 fprintf (stderr,
"Couldn't find headers signature in %s\n", filename);
186 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
195 if (
strcmp(argv[1],
"64") == 0)
197 if (
stat (argv[4], &s) < 0)
200 if (!get_start_end(argv[3], &start, &end)) {
201 fprintf(stderr,
"Could not determine start and end from %s\n",
207 if (
read(image, buffer, 512) != 512)
209 if (
memcmp(buffer, aout_magic, 4) != 0) {
210 fprintf (stderr,
"Not a.out. Don't blame me.\n");
221 offset = get_hdrs_offset(image, argv[2]);
225 if (lseek(image, offset, 0) < 0)
236 st4(buffer + 4, 0x01000000);
237 st4(buffer + 8,
align(end + 32));
240 if (
write(image, buffer + 2, 14) != 14)
246 if (lseek(image, 4, 0) < 0)
249 st4(buffer,
align(end + 32 + 8191) - (start & ~0x3fffffUL) +
255 if (
write(image, buffer, 12) != 12)
264 while ((i =
read(tail, buffer, 1024)) > 0)
265 if (
write(image, buffer, i) != i)
267 if (close(image) < 0)