]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/sparc/boot/piggyback_32.c
sparc: use _start for the start entry (like 64 bit does)
[mirror_ubuntu-bionic-kernel.git] / arch / sparc / boot / piggyback_32.c
1 /*
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.
12
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. */
21
22 #include <dirent.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30
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
39 /* read two bytes as big endian */
40 static unsigned short ld2(char *p)
41 {
42 return (p[0] << 8) | p[1];
43 }
44
45 /* read 4 bytes as big endian */
46 static unsigned int ld4(char *p)
47 {
48 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
49 }
50
51 /* save 4 bytes as big endian */
52 static void st4(char *p, unsigned int x)
53 {
54 p[0] = x >> 24;
55 p[1] = x >> 16;
56 p[2] = x >> 8;
57 p[3] = x;
58 }
59
60 static void die(const char *str)
61 {
62 perror(str);
63 exit(1);
64 }
65
66 static void usage(void)
67 {
68 /* fs_img.gz is an image of initial ramdisk. */
69 fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
70 fprintf(stderr, "\tKernel image will be modified in place.\n");
71 exit(1);
72 }
73
74 static int start_line(const char *line)
75 {
76 if (strcmp(line + 8, " T _start\n") == 0)
77 return 1;
78 else if (strcmp(line + 16, " T _start\n") == 0)
79 return 1;
80 return 0;
81 }
82
83 static int end_line(const char *line)
84 {
85 if (strcmp(line + 8, " A _end\n") == 0)
86 return 1;
87 else if (strcmp (line + 16, " A _end\n") == 0)
88 return 1;
89 return 0;
90 }
91
92 /*
93 * Find address for start and end in System.map.
94 * The file looks like this:
95 * f0004000 T _start
96 * f0379f79 A _end
97 * 1234567890123456
98 * ^coloumn 1
99 * There is support for 64 bit addresses too.
100 *
101 * Return 0 if either start or end is not found
102 */
103 static int get_start_end(const char *filename, unsigned int *start, unsigned int *end)
104 {
105 FILE *map;
106 char buffer[1024];
107
108 *start = 0;
109 *end = 0;
110 map = fopen(filename, "r");
111 if (!map)
112 die(filename);
113 while (fgets(buffer, 1024, map)) {
114 if (start_line(buffer))
115 *start = strtoul(buffer, NULL, 16);
116 else if (end_line(buffer))
117 *end = strtoul(buffer, NULL, 16);
118 }
119 fclose (map);
120
121 if (*start == 0 || *end == 0)
122 return 0;
123
124 return 1;
125 }
126
127 int main(int argc,char **argv)
128 {
129 static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
130 char buffer[1024], *q, *r;
131 unsigned int i, j, k, start, end, offset;
132 struct stat s;
133 int image, tail;
134
135 if (argc != 4)
136 usage();
137 if (stat (argv[3], &s) < 0)
138 die(argv[3]);
139
140 if (!get_start_end(argv[2], &start, &end)) {
141 fprintf (stderr, "Could not determine start and end from %s\n", argv[2]);
142 exit(1);
143 }
144 if ((image = open(argv[1], O_RDWR)) < 0)
145 die(argv[1]);
146 if (read(image, buffer, 512) != 512)
147 die(argv[1]);
148 if (memcmp (buffer, "\177ELF", 4) == 0) {
149 q = buffer + ld4(buffer + 28);
150 i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
151 if (lseek(image, i, 0) < 0)
152 die("lseek");
153 if (read(image, buffer, 512) != 512)
154 die(argv[1]);
155 j = 0;
156 } else if (memcmp(buffer, aout_magic, 4) == 0) {
157 i = j = 32;
158 } else {
159 fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
160 exit(1);
161 }
162 k = i;
163 i += (ld2(buffer + j + 2) << 2) - 512;
164 if (lseek(image, i, 0) < 0)
165 die("lseek");
166 if (read(image, buffer, 1024) != 1024)
167 die(argv[1]);
168 for (q = buffer, r = q + 512; q < r; q += 4) {
169 if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
170 break;
171 }
172 if (q == r) {
173 fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
174 exit(1);
175 }
176 offset = i + (q - buffer) + 10;
177 if (lseek(image, offset, 0) < 0)
178 die("lseek");
179
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
185 if (write(image, buffer + 2, 14) != 14)
186 die(argv[1]);
187 if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0)
188 die("lseek");
189 if ((tail = open(argv[3],O_RDONLY)) < 0)
190 die(argv[3]);
191 while ((i = read (tail, buffer, 1024)) > 0)
192 if (write(image, buffer, i) != i)
193 die(argv[1]);
194 if (close(image) < 0)
195 die("close");
196 if (close(tail) < 0)
197 die("close");
198 return 0;
199 }