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