blob: 5e856f2e9e0f04cf6be76b4688bd5baac6ad23ac [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/* read 4 bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070048static unsigned int ld4(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
51}
52
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000053/* save 4 bytes as big endian */
Josh Triplettc843e312009-10-16 14:58:29 -070054static void st4(char *p, unsigned int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 p[0] = x >> 24;
57 p[1] = x >> 16;
58 p[2] = x >> 8;
59 p[3] = x;
60}
61
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000062static void die(const char *str)
63{
64 perror(str);
65 exit(1);
66}
67
Josh Triplettc843e312009-10-16 14:58:29 -070068static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 /* fs_img.gz is an image of initial ramdisk. */
71 fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
72 fprintf(stderr, "\tKernel image will be modified in place.\n");
73 exit(1);
74}
75
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000076static int start_line(const char *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Sam Ravnborgb2a39b02011-01-04 11:39:12 +000078 if (strcmp(line + 8, " T _start\n") == 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000079 return 1;
Sam Ravnborgb2a39b02011-01-04 11:39:12 +000080 else if (strcmp(line + 16, " T _start\n") == 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000081 return 1;
82 return 0;
83}
84
85static int end_line(const char *line)
86{
87 if (strcmp(line + 8, " A _end\n") == 0)
88 return 1;
89 else if (strcmp (line + 16, " A _end\n") == 0)
90 return 1;
91 return 0;
92}
93
94/*
95 * Find address for start and end in System.map.
96 * The file looks like this:
Sam Ravnborgb2a39b02011-01-04 11:39:12 +000097 * f0004000 T _start
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +000098 * f0379f79 A _end
99 * 1234567890123456
100 * ^coloumn 1
101 * There is support for 64 bit addresses too.
102 *
103 * Return 0 if either start or end is not found
104 */
105static int get_start_end(const char *filename, unsigned int *start, unsigned int *end)
106{
107 FILE *map;
108 char buffer[1024];
109
110 *start = 0;
111 *end = 0;
112 map = fopen(filename, "r");
113 if (!map)
114 die(filename);
115 while (fgets(buffer, 1024, map)) {
116 if (start_line(buffer))
117 *start = strtoul(buffer, NULL, 16);
118 else if (end_line(buffer))
119 *end = strtoul(buffer, NULL, 16);
120 }
121 fclose (map);
122
123 if (*start == 0 || *end == 0)
124 return 0;
125
126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129int main(int argc,char **argv)
130{
131 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
Robert Reif22b096a2009-06-21 16:45:44 +0000132 char buffer[1024], *q, *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned int i, j, k, start, end, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct stat s;
135 int image, tail;
136
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000137 if (argc != 4)
138 usage();
139 if (stat (argv[3], &s) < 0)
140 die(argv[3]);
141
142 if (!get_start_end(argv[2], &start, &end)) {
143 fprintf (stderr, "Could not determine start and end from %s\n", argv[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 exit(1);
145 }
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000146 if ((image = open(argv[1], O_RDWR)) < 0)
147 die(argv[1]);
148 if (read(image, buffer, 512) != 512)
149 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (memcmp (buffer, "\177ELF", 4) == 0) {
151 q = buffer + ld4(buffer + 28);
152 i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000153 if (lseek(image, i, 0) < 0)
154 die("lseek");
155 if (read(image, buffer, 512) != 512)
156 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 j = 0;
158 } else if (memcmp(buffer, aout_magic, 4) == 0) {
Sam Ravnborg9c239052011-01-04 11:39:13 +0000159 i = j = AOUT_TEXT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 } else {
161 fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
162 exit(1);
163 }
164 k = i;
Sam Ravnborg9c239052011-01-04 11:39:13 +0000165 /*
166 * We need to fill in values for sparc_ramdisk_image + sparc_ramdisk_size
167 * To locate these symbols search for the "HdrS" text which appear
168 * in the image a little before the gokernel symbol.
169 * See definition of these in init_32.S
170 */
171
172 /* Find the gokernel label */
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000173 i += (ld2(buffer + j + 2) << 2) - 512;
174 if (lseek(image, i, 0) < 0)
175 die("lseek");
176 if (read(image, buffer, 1024) != 1024)
177 die(argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 for (q = buffer, r = q + 512; q < r; q += 4) {
179 if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
180 break;
181 }
182 if (q == r) {
183 fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
184 exit(1);
185 }
186 offset = i + (q - buffer) + 10;
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000187 if (lseek(image, offset, 0) < 0)
188 die("lseek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Sam Ravnborg9c239052011-01-04 11:39:13 +0000190 /*
191 * root_flags = 0
192 * root_dev = 1 (RAMDISK_MAJOR)
193 * ram_flags = 0
194 * sparc_ramdisk_image = "PAGE aligned address after _end")
195 * sparc_ramdisk_size = size of image
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 st4(buffer, 0);
198 st4(buffer + 4, 0x01000000);
199 st4(buffer + 8, (end + 32 + 4095) & ~4095);
200 st4(buffer + 12, s.st_size);
201
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000202 if (write(image, buffer + 2, 14) != 14)
203 die(argv[1]);
Sam Ravnborg9c239052011-01-04 11:39:13 +0000204
205 /* seek page aligned boundary in the image file and add boot image */
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000206 if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0)
207 die("lseek");
208 if ((tail = open(argv[3],O_RDONLY)) < 0)
209 die(argv[3]);
Sam Ravnborg9c239052011-01-04 11:39:13 +0000210 while ((i = read(tail, buffer, 1024)) > 0)
Sam Ravnborg2fe74fa2011-01-04 11:39:10 +0000211 if (write(image, buffer, i) != i)
212 die(argv[1]);
213 if (close(image) < 0)
214 die("close");
215 if (close(tail) < 0)
216 die("close");
217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}