Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 21 | |
| 22 | #include <dirent.h> |
| 23 | #include <stdlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <string.h> |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sam Ravnborg | 9c23905 | 2011-01-04 11:39:13 +0000 | [diff] [blame] | 39 | #define AOUT_TEXT_OFFSET 32 |
| 40 | |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 41 | static int is64bit = 0; |
| 42 | |
| 43 | /* align to power-of-two size */ |
| 44 | static int align(int n) |
| 45 | { |
| 46 | if (is64bit) |
| 47 | return (n + 0x1fff) & ~0x1fff; |
| 48 | else |
| 49 | return (n + 0xfff) & ~0xfff; |
| 50 | } |
| 51 | |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 52 | /* read two bytes as big endian */ |
Josh Triplett | c843e31 | 2009-10-16 14:58:29 -0700 | [diff] [blame] | 53 | static unsigned short ld2(char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | return (p[0] << 8) | p[1]; |
| 56 | } |
| 57 | |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 58 | /* save 4 bytes as big endian */ |
Josh Triplett | c843e31 | 2009-10-16 14:58:29 -0700 | [diff] [blame] | 59 | static void st4(char *p, unsigned int x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | p[0] = x >> 24; |
| 62 | p[1] = x >> 16; |
| 63 | p[2] = x >> 8; |
| 64 | p[3] = x; |
| 65 | } |
| 66 | |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 67 | static void die(const char *str) |
| 68 | { |
| 69 | perror(str); |
| 70 | exit(1); |
| 71 | } |
| 72 | |
Josh Triplett | c843e31 | 2009-10-16 14:58:29 -0700 | [diff] [blame] | 73 | static void usage(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | /* fs_img.gz is an image of initial ramdisk. */ |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 76 | fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | fprintf(stderr, "\tKernel image will be modified in place.\n"); |
| 78 | exit(1); |
| 79 | } |
| 80 | |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 81 | static int start_line(const char *line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Sam Ravnborg | b2a39b0 | 2011-01-04 11:39:12 +0000 | [diff] [blame] | 83 | if (strcmp(line + 8, " T _start\n") == 0) |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 84 | return 1; |
Sam Ravnborg | b2a39b0 | 2011-01-04 11:39:12 +0000 | [diff] [blame] | 85 | else if (strcmp(line + 16, " T _start\n") == 0) |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 86 | return 1; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static 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: |
Sam Ravnborg | b2a39b0 | 2011-01-04 11:39:12 +0000 | [diff] [blame] | 102 | * f0004000 T _start |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 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 | */ |
| 110 | static int get_start_end(const char *filename, unsigned int *start, unsigned int *end) |
| 111 | { |
| 112 | FILE *map; |
| 113 | char buffer[1024]; |
| 114 | |
| 115 | *start = 0; |
| 116 | *end = 0; |
| 117 | map = fopen(filename, "r"); |
| 118 | if (!map) |
| 119 | die(filename); |
| 120 | while (fgets(buffer, 1024, map)) { |
| 121 | if (start_line(buffer)) |
| 122 | *start = strtoul(buffer, NULL, 16); |
| 123 | else if (end_line(buffer)) |
| 124 | *end = strtoul(buffer, NULL, 16); |
| 125 | } |
| 126 | fclose (map); |
| 127 | |
| 128 | if (*start == 0 || *end == 0) |
| 129 | return 0; |
| 130 | |
| 131 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int main(int argc,char **argv) |
| 135 | { |
| 136 | static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 }; |
Robert Reif | 22b096a | 2009-06-21 16:45:44 +0000 | [diff] [blame] | 137 | char buffer[1024], *q, *r; |
Sam Ravnborg | 571e08f | 2011-01-04 11:39:14 +0000 | [diff] [blame] | 138 | unsigned int i, start, end, offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | struct stat s; |
| 140 | int image, tail; |
| 141 | |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 142 | if (argc != 5) |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 143 | usage(); |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 144 | if (strcmp(argv[1], "64") == 0) |
| 145 | is64bit = 1; |
| 146 | if (stat (argv[4], &s) < 0) |
| 147 | die(argv[4]); |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 148 | |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 149 | if (!get_start_end(argv[3], &start, &end)) { |
| 150 | fprintf (stderr, "Could not determine start and end from %s\n", argv[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | exit(1); |
| 152 | } |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 153 | if ((image = open(argv[2], O_RDWR)) < 0) |
| 154 | die(argv[2]); |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 155 | if (read(image, buffer, 512) != 512) |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 156 | die(argv[2]); |
Sam Ravnborg | 571e08f | 2011-01-04 11:39:14 +0000 | [diff] [blame] | 157 | if (memcmp(buffer, aout_magic, 4) != 0) { |
| 158 | fprintf (stderr, "Not a.out. Don't blame me.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | exit(1); |
| 160 | } |
Sam Ravnborg | 9c23905 | 2011-01-04 11:39:13 +0000 | [diff] [blame] | 161 | /* |
| 162 | * We need to fill in values for sparc_ramdisk_image + sparc_ramdisk_size |
| 163 | * To locate these symbols search for the "HdrS" text which appear |
| 164 | * in the image a little before the gokernel symbol. |
| 165 | * See definition of these in init_32.S |
| 166 | */ |
| 167 | |
| 168 | /* Find the gokernel label */ |
Sam Ravnborg | 571e08f | 2011-01-04 11:39:14 +0000 | [diff] [blame] | 169 | i = AOUT_TEXT_OFFSET + (ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2) - 512; |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 170 | if (lseek(image, i, 0) < 0) |
| 171 | die("lseek"); |
| 172 | if (read(image, buffer, 1024) != 1024) |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 173 | die(argv[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | for (q = buffer, r = q + 512; q < r; q += 4) { |
| 175 | if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S') |
| 176 | break; |
| 177 | } |
| 178 | if (q == r) { |
| 179 | fprintf (stderr, "Couldn't find headers signature in the kernel.\n"); |
| 180 | exit(1); |
| 181 | } |
| 182 | offset = i + (q - buffer) + 10; |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 183 | if (lseek(image, offset, 0) < 0) |
| 184 | die("lseek"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Sam Ravnborg | 9c23905 | 2011-01-04 11:39:13 +0000 | [diff] [blame] | 186 | /* |
| 187 | * root_flags = 0 |
| 188 | * root_dev = 1 (RAMDISK_MAJOR) |
| 189 | * ram_flags = 0 |
| 190 | * sparc_ramdisk_image = "PAGE aligned address after _end") |
| 191 | * sparc_ramdisk_size = size of image |
| 192 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | st4(buffer, 0); |
| 194 | st4(buffer + 4, 0x01000000); |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 195 | st4(buffer + 8, align(end + 32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | st4(buffer + 12, s.st_size); |
| 197 | |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 198 | if (write(image, buffer + 2, 14) != 14) |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 199 | die(argv[2]); |
Sam Ravnborg | 9c23905 | 2011-01-04 11:39:13 +0000 | [diff] [blame] | 200 | |
| 201 | /* seek page aligned boundary in the image file and add boot image */ |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 202 | if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0) |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 203 | die("lseek"); |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 204 | if ((tail = open(argv[4], O_RDONLY)) < 0) |
| 205 | die(argv[4]); |
Sam Ravnborg | 9c23905 | 2011-01-04 11:39:13 +0000 | [diff] [blame] | 206 | while ((i = read(tail, buffer, 1024)) > 0) |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 207 | if (write(image, buffer, i) != i) |
Sam Ravnborg | a020bb1 | 2011-01-04 11:39:15 +0000 | [diff] [blame^] | 208 | die(argv[2]); |
Sam Ravnborg | 2fe74fa | 2011-01-04 11:39:10 +0000 | [diff] [blame] | 209 | if (close(image) < 0) |
| 210 | die("close"); |
| 211 | if (close(tail) < 0) |
| 212 | die("close"); |
| 213 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |