Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003    Simple utility to make a single-image install kernel with initial ramdisk
0004    for Sparc tftpbooting without need to set up nfs.
0005 
0006    Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
0007    Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
0008    Copyright (C) 2011 Sam Ravnborg <sam@ravnborg.org>
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  * Note: run this on an a.out kernel (use elftoaout for it),
0026  * as PROM looks for a.out image only.
0027  */
0028 
0029 #define AOUT_TEXT_OFFSET   32
0030 
0031 static int is64bit = 0;
0032 
0033 /* align to power-of-two size */
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 /* read two bytes as big endian */
0043 static unsigned short ld2(char *p)
0044 {
0045     return (p[0] << 8) | p[1];
0046 }
0047 
0048 /* save 4 bytes as big endian */
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     /* fs_img.gz is an image of initial ramdisk. */
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  * Find address for start and end in System.map.
0091  * The file looks like this:
0092  * f0004000 ... _start
0093  * f0379f79 ... _end
0094  * 1234567890123456
0095  * ^coloumn 1
0096  * There is support for 64 bit addresses too.
0097  *
0098  * Return 0 if either start or end is not found
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  * Find the HdrS entry from head_32/head_64.
0129  * We check if it is at the beginning of the file (sparc64 case)
0130  * and if not we search for it.
0131  * When we search do so in steps of 4 as HdrS is on a 4-byte aligned
0132  * address (it is on same alignment as sparc instructions)
0133  * Return the offset to the HdrS entry (as off_t)
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         /*  Find the gokernel label */
0151         /* Decode offset from branch instruction */
0152         offset = ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2;
0153         /* Go back 512 bytes so we do not miss HdrS */
0154         offset -= LOOKBACK;
0155         /* skip a.out header */
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      * We need to fill in values for
0208      * sparc_ramdisk_image + sparc_ramdisk_size
0209      * To locate these symbols search for the "HdrS" text which appear
0210      * in the image a little before the gokernel symbol.
0211      * See definition of these in init_32.S
0212      */
0213 
0214     offset = get_hdrs_offset(image, argv[2]);
0215     /* skip HdrS + LINUX_VERSION_CODE + HdrS version */
0216     offset += 10;
0217 
0218     if (lseek(image, offset, 0) < 0)
0219         die("lseek");
0220 
0221     /*
0222      * root_flags = 0
0223      * root_dev = 1 (RAMDISK_MAJOR)
0224      * ram_flags = 0
0225      * sparc_ramdisk_image = "PAGE aligned address after _end")
0226      * sparc_ramdisk_size = size of image
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     /* For sparc64 update a_text and clear a_data + a_bss */
0237     if (is64bit)
0238     {
0239         if (lseek(image, 4, 0) < 0)
0240             die("lseek");
0241         /* a_text */
0242         st4(buffer, align(end + 32 + 8191) - (start & ~0x3fffffUL) +
0243                     s.st_size);
0244         /* a_data */
0245         st4(buffer + 4, 0);
0246         /* a_bss */
0247         st4(buffer + 8, 0);
0248         if (write(image, buffer, 12) != 12)
0249             die(argv[2]);
0250     }
0251 
0252     /* seek page aligned boundary in the image file and add boot image */
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 }