]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/sparc/boot/piggyback_32.c
2 Simple utility to make a single-image install kernel with initial ramdisk
3 for Sparc tftpbooting without need to set up nfs.
5 Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
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.
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.
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. */
31 #include <sys/types.h>
35 * Note: run this on an a.out kernel (use elftoaout for it),
36 * as PROM looks for a.out image only.
39 /* read two bytes as big endian */
40 static unsigned short ld2(char *p
)
42 return (p
[0] << 8) | p
[1];
45 /* read 4 bytes as big endian */
46 static unsigned int ld4(char *p
)
48 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
51 /* save 4 bytes as big endian */
52 static void st4(char *p
, unsigned int x
)
60 static void die(const char *str
)
66 static void usage(void)
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");
74 static int start_line(const char *line
)
76 if (strcmp(line
+ 8, " T _start\n") == 0)
78 else if (strcmp(line
+ 16, " T _start\n") == 0)
83 static int end_line(const char *line
)
85 if (strcmp(line
+ 8, " A _end\n") == 0)
87 else if (strcmp (line
+ 16, " A _end\n") == 0)
93 * Find address for start and end in System.map.
94 * The file looks like this:
99 * There is support for 64 bit addresses too.
101 * Return 0 if either start or end is not found
103 static int get_start_end(const char *filename
, unsigned int *start
, unsigned int *end
)
110 map
= fopen(filename
, "r");
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);
121 if (*start
== 0 || *end
== 0)
127 int main(int argc
,char **argv
)
129 static char aout_magic
[] = { 0x01, 0x03, 0x01, 0x07 };
130 char buffer
[1024], *q
, *r
;
131 unsigned int i
, j
, k
, start
, end
, offset
;
137 if (stat (argv
[3], &s
) < 0)
140 if (!get_start_end(argv
[2], &start
, &end
)) {
141 fprintf (stderr
, "Could not determine start and end from %s\n", argv
[2]);
144 if ((image
= open(argv
[1], O_RDWR
)) < 0)
146 if (read(image
, buffer
, 512) != 512)
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)
153 if (read(image
, buffer
, 512) != 512)
156 } else if (memcmp(buffer
, aout_magic
, 4) == 0) {
159 fprintf (stderr
, "Not ELF nor a.out. Don't blame me.\n");
163 i
+= (ld2(buffer
+ j
+ 2) << 2) - 512;
164 if (lseek(image
, i
, 0) < 0)
166 if (read(image
, buffer
, 1024) != 1024)
168 for (q
= buffer
, r
= q
+ 512; q
< r
; q
+= 4) {
169 if (*q
== 'H' && q
[1] == 'd' && q
[2] == 'r' && q
[3] == 'S')
173 fprintf (stderr
, "Couldn't find headers signature in the kernel.\n");
176 offset
= i
+ (q
- buffer
) + 10;
177 if (lseek(image
, offset
, 0) < 0)
181 st4(buffer
+ 4, 0x01000000);
182 st4(buffer
+ 8, (end
+ 32 + 4095) & ~4095);
183 st4(buffer
+ 12, s
.st_size
);
185 if (write(image
, buffer
+ 2, 14) != 14)
187 if (lseek(image
, k
- start
+ ((end
+ 32 + 4095) & ~4095), 0) < 0)
189 if ((tail
= open(argv
[3],O_RDONLY
)) < 0)
191 while ((i
= read (tail
, buffer
, 1024)) > 0)
192 if (write(image
, buffer
, i
) != i
)
194 if (close(image
) < 0)