2 Simple utility to make a single-image install kernel with initial ramdisk
3 for Sparc64 tftpbooting without need to set up nfs.
5 Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #include <sys/types.h>
32 /* Note: run this on an a.out kernel (use elftoaout for it), as PROM looks for a.out image onlly
33 usage: piggyback vmlinux System.map tail, where tail is gzipped fs of the initial ramdisk */
41 int main(int argc
,char **argv
)
43 char buffer
[1024], *q
, *r
;
44 unsigned int i
, j
, k
, start
, end
, offset
;
50 if (stat (argv
[3], &s
) < 0) die (argv
[3]);
51 map
= fopen (argv
[2], "r");
52 if (!map
) die(argv
[2]);
53 while (fgets (buffer
, 1024, map
)) {
54 if (!strcmp (buffer
+ 19, "_start\n"))
55 start
= strtoul (buffer
+ 8, NULL
, 16);
56 else if (!strcmp (buffer
+ 19, "_end\n"))
57 end
= strtoul (buffer
+ 8, NULL
, 16);
60 if ((image
= open(argv
[1],O_RDWR
)) < 0) die(argv
[1]);
61 if (read(image
,buffer
,512) != 512) die(argv
[1]);
62 if (!memcmp (buffer
, "\177ELF", 4)) {
63 unsigned int *p
= (unsigned int *)(buffer
+ *(unsigned int *)(buffer
+ 28));
65 i
= p
[1] + *(unsigned int *)(buffer
+ 24) - p
[2];
66 if (lseek(image
,i
,0) < 0) die("lseek");
67 if (read(image
,buffer
,512) != 512) die(argv
[1]);
69 } else if (*(unsigned int *)buffer
== 0x01030107) {
72 fprintf (stderr
, "Not ELF nor a.out. Don't blame me.\n");
76 if (j
== 32 && buffer
[40] == 'H' && buffer
[41] == 'd' && buffer
[42] == 'r' && buffer
[43] == 'S') {
79 i
+= ((*(unsigned short *)(buffer
+ j
+ 2))<<2) - 512;
80 if (lseek(image
,i
,0) < 0) die("lseek");
81 if (read(image
,buffer
,1024) != 1024) die(argv
[1]);
82 for (q
= buffer
, r
= q
+ 512; q
< r
; q
+= 4) {
83 if (*q
== 'H' && q
[1] == 'd' && q
[2] == 'r' && q
[3] == 'S')
87 fprintf (stderr
, "Couldn't find headers signature in the kernel.\n");
90 offset
= i
+ (q
- buffer
) + 10;
92 if (lseek(image
, offset
, 0) < 0) die ("lseek");
93 *(unsigned *)buffer
= 0;
94 *(unsigned *)(buffer
+ 4) = 0x01000000;
95 *(unsigned *)(buffer
+ 8) = ((end
+ 32 + 8191) & ~8191);
96 *(unsigned *)(buffer
+ 12) = s
.st_size
;
97 if (write(image
,buffer
+2,14) != 14) die (argv
[1]);
98 if (lseek(image
, 4, 0) < 0) die ("lseek");
99 *(unsigned *)buffer
= ((end
+ 32 + 8191) & ~8191) - (start
& ~0x3fffffUL
) + s
.st_size
;
100 *(unsigned *)(buffer
+ 4) = 0;
101 *(unsigned *)(buffer
+ 8) = 0;
102 if (write(image
,buffer
,12) != 12) die (argv
[1]);
103 if (lseek(image
, k
- start
+ ((end
+ 32 + 8191) & ~8191), 0) < 0) die ("lseek");
104 if ((tail
= open(argv
[3],O_RDONLY
)) < 0) die(argv
[3]);
105 while ((i
= read (tail
,buffer
,1024)) > 0)
106 if (write(image
,buffer
,i
) != i
) die (argv
[1]);
107 if (close(image
) < 0) die("close");
108 if (close(tail
) < 0) die("close");