0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <dirent.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015 #include <unistd.h>
0016 #include <ctype.h>
0017 #include <errno.h>
0018 #include <fcntl.h>
0019 #include <stdio.h>
0020
0021 #include <sys/types.h>
0022 #include <sys/stat.h>
0023
0024
0025
0026
0027
0028
0029 #define AOUT_TEXT_OFFSET 32
0030
0031 static int is64bit = 0;
0032
0033
0034 static int align(int n)
0035 {
0036 if (is64bit)
0037 return (n + 0x1fff) & ~0x1fff;
0038 else
0039 return (n + 0xfff) & ~0xfff;
0040 }
0041
0042
0043 static unsigned short ld2(char *p)
0044 {
0045 return (p[0] << 8) | p[1];
0046 }
0047
0048
0049 static void st4(char *p, unsigned int x)
0050 {
0051 p[0] = x >> 24;
0052 p[1] = x >> 16;
0053 p[2] = x >> 8;
0054 p[3] = x;
0055 }
0056
0057 static void die(const char *str)
0058 {
0059 perror(str);
0060 exit(1);
0061 }
0062
0063 static void usage(void)
0064 {
0065
0066 fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
0067 fprintf(stderr, "\tKernel image will be modified in place.\n");
0068 exit(1);
0069 }
0070
0071 static int start_line(const char *line)
0072 {
0073 if (strcmp(line + 10, " _start\n") == 0)
0074 return 1;
0075 else if (strcmp(line + 18, " _start\n") == 0)
0076 return 1;
0077 return 0;
0078 }
0079
0080 static int end_line(const char *line)
0081 {
0082 if (strcmp(line + 10, " _end\n") == 0)
0083 return 1;
0084 else if (strcmp (line + 18, " _end\n") == 0)
0085 return 1;
0086 return 0;
0087 }
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 static int get_start_end(const char *filename, unsigned int *start,
0101 unsigned int *end)
0102 {
0103 FILE *map;
0104 char buffer[1024];
0105
0106 *start = 0;
0107 *end = 0;
0108 map = fopen(filename, "r");
0109 if (!map)
0110 die(filename);
0111 while (fgets(buffer, 1024, map)) {
0112 if (start_line(buffer))
0113 *start = strtoul(buffer, NULL, 16);
0114 else if (end_line(buffer))
0115 *end = strtoul(buffer, NULL, 16);
0116 }
0117 fclose (map);
0118
0119 if (*start == 0 || *end == 0)
0120 return 0;
0121
0122 return 1;
0123 }
0124
0125 #define LOOKBACK (128 * 4)
0126 #define BUFSIZE 1024
0127
0128
0129
0130
0131
0132
0133
0134
0135 static off_t get_hdrs_offset(int kernelfd, const char *filename)
0136 {
0137 char buffer[BUFSIZE];
0138 off_t offset;
0139 int i;
0140
0141 if (lseek(kernelfd, 0, SEEK_SET) < 0)
0142 die("lseek");
0143 if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
0144 die(filename);
0145
0146 if (buffer[40] == 'H' && buffer[41] == 'd' &&
0147 buffer[42] == 'r' && buffer[43] == 'S') {
0148 return 40;
0149 } else {
0150
0151
0152 offset = ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2;
0153
0154 offset -= LOOKBACK;
0155
0156 offset += AOUT_TEXT_OFFSET;
0157 if (offset < 0) {
0158 errno = -EINVAL;
0159 die("Calculated a negative offset, probably elftoaout generated an invalid image. Did you use a recent elftoaout ?");
0160 }
0161 if (lseek(kernelfd, offset, SEEK_SET) < 0)
0162 die("lseek");
0163 if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
0164 die(filename);
0165
0166 for (i = 0; i < LOOKBACK; i += 4) {
0167 if (buffer[i + 0] == 'H' && buffer[i + 1] == 'd' &&
0168 buffer[i + 2] == 'r' && buffer[i + 3] == 'S') {
0169 return offset + i;
0170 }
0171 }
0172 }
0173 fprintf (stderr, "Couldn't find headers signature in %s\n", filename);
0174 exit(1);
0175 }
0176
0177 int main(int argc,char **argv)
0178 {
0179 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
0180 char buffer[1024];
0181 unsigned int i, start, end;
0182 off_t offset;
0183 struct stat s;
0184 int image, tail;
0185
0186 if (argc != 5)
0187 usage();
0188 if (strcmp(argv[1], "64") == 0)
0189 is64bit = 1;
0190 if (stat (argv[4], &s) < 0)
0191 die(argv[4]);
0192
0193 if (!get_start_end(argv[3], &start, &end)) {
0194 fprintf(stderr, "Could not determine start and end from %s\n",
0195 argv[3]);
0196 exit(1);
0197 }
0198 if ((image = open(argv[2], O_RDWR)) < 0)
0199 die(argv[2]);
0200 if (read(image, buffer, 512) != 512)
0201 die(argv[2]);
0202 if (memcmp(buffer, aout_magic, 4) != 0) {
0203 fprintf (stderr, "Not a.out. Don't blame me.\n");
0204 exit(1);
0205 }
0206
0207
0208
0209
0210
0211
0212
0213
0214 offset = get_hdrs_offset(image, argv[2]);
0215
0216 offset += 10;
0217
0218 if (lseek(image, offset, 0) < 0)
0219 die("lseek");
0220
0221
0222
0223
0224
0225
0226
0227
0228 st4(buffer, 0);
0229 st4(buffer + 4, 0x01000000);
0230 st4(buffer + 8, align(end + 32));
0231 st4(buffer + 12, s.st_size);
0232
0233 if (write(image, buffer + 2, 14) != 14)
0234 die(argv[2]);
0235
0236
0237 if (is64bit)
0238 {
0239 if (lseek(image, 4, 0) < 0)
0240 die("lseek");
0241
0242 st4(buffer, align(end + 32 + 8191) - (start & ~0x3fffffUL) +
0243 s.st_size);
0244
0245 st4(buffer + 4, 0);
0246
0247 st4(buffer + 8, 0);
0248 if (write(image, buffer, 12) != 12)
0249 die(argv[2]);
0250 }
0251
0252
0253 if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0)
0254 die("lseek");
0255 if ((tail = open(argv[4], O_RDONLY)) < 0)
0256 die(argv[4]);
0257 while ((i = read(tail, buffer, 1024)) > 0)
0258 if (write(image, buffer, i) != i)
0259 die(argv[2]);
0260 if (close(image) < 0)
0261 die("close");
0262 if (close(tail) < 0)
0263 die("close");
0264 return 0;
0265 }