blob: 369634d8271e738133b3cfb1a6459cb88702894b [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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 Ravnborg2fe74fa2011-01-04 11:39:10 +000012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 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 Ravnborg2fe74fa2011-01-04 11:39:10 +000021
22#include <dirent.h>
23#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <string.h>
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000025#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000029#include <stdio.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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 Ravnborg9c239052011-01-04 11:39:13 +000039#define AOUT_TEXT_OFFSET 32
40
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000041/* read two bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070042static unsigned short ld2(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 return (p[0] << 8) | p[1];
45}
46
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000047/* save 4 bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070048static void st4(char *p, unsigned int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 p[0] = x >> 24;
51 p[1] = x >> 16;
52 p[2] = x >> 8;
53 p[3] = x;
54}
55
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000056static void die(const char *str)
57{
58 perror(str);
59 exit(1);
60}
61
Josh Triplettc843e312009-10-16 14:58:29 -070062static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
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 Ravnborg2fe74fa2011-01-04 11:39:10 +000070static int start_line(const char *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Sam Ravnborgb2a39b02011-01-04 11:39:12 +000072 if (strcmp(line + 8, " T _start\n") == 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000073 return 1;
Sam Ravnborgb2a39b02011-01-04 11:39:12 +000074 else if (strcmp(line + 16, " T _start\n") == 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000075 return 1;
76 return 0;
77}
78
79static 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 Ravnborgb2a39b02011-01-04 11:39:12 +000091 * f0004000 T _start
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000092 * 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 */
99static 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 Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123int main(int argc,char **argv)
124{
125 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
Robert Reif22b096a2009-06-21 16:45:44 +0000126 char buffer[1024], *q, *r;
Sam Ravnborg571e08f2011-01-04 11:39:14 +0000127 unsigned int i, start, end, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct stat s;
129 int image, tail;
130
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000131 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 Torvalds1da177e2005-04-16 15:20:36 -0700138 exit(1);
139 }
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000140 if ((image = open(argv[1], O_RDWR)) < 0)
141 die(argv[1]);
142 if (read(image, buffer, 512) != 512)
143 die(argv[1]);
Sam Ravnborg571e08f2011-01-04 11:39:14 +0000144 if (memcmp(buffer, aout_magic, 4) != 0) {
145 fprintf (stderr, "Not a.out. Don't blame me.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 exit(1);
147 }
Sam Ravnborg9c239052011-01-04 11:39:13 +0000148 /*
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 Ravnborg571e08f2011-01-04 11:39:14 +0000156 i = AOUT_TEXT_OFFSET + (ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2) - 512;
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000157 if (lseek(image, i, 0) < 0)
158 die("lseek");
159 if (read(image, buffer, 1024) != 1024)
160 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 Ravnborg2fe74fa2011-01-04 11:39:10 +0000170 if (lseek(image, offset, 0) < 0)
171 die("lseek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Sam Ravnborg9c239052011-01-04 11:39:13 +0000173 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700180 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 Ravnborg2fe74fa2011-01-04 11:39:10 +0000185 if (write(image, buffer + 2, 14) != 14)
186 die(argv[1]);
Sam Ravnborg9c239052011-01-04 11:39:13 +0000187
188 /* seek page aligned boundary in the image file and add boot image */
Sam Ravnborg571e08f2011-01-04 11:39:14 +0000189 if (lseek(image, AOUT_TEXT_OFFSET - start + ((end + 32 + 4095) & ~4095), 0) < 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000190 die("lseek");
191 if ((tail = open(argv[3],O_RDONLY)) < 0)
192 die(argv[3]);
Sam Ravnborg9c239052011-01-04 11:39:13 +0000193 while ((i = read(tail, buffer, 1024)) > 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000194 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 Torvalds1da177e2005-04-16 15:20:36 -0700201}