Commit a8c601ca21e790f6a9d996bb0bf31f7496eb9509

Authored by Sam Ravnborg
Committed by David S. Miller
1 parent a88b5ba8bd

sparc,sparc64: unify boot/

Simple unification:
o renamed piggyback to *_32.c/*_64.c
o copied content of Makefile from sparc64 to sparc and guard it
o updated sparc/boot/.gitignore
o deleted remaining files in sparc64/boot

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 9 changed files with 283 additions and 291 deletions Side-by-side Diff

... ... @@ -89,17 +89,16 @@
89 89 export kallsyms.o := .tmp_kallsyms2.o
90 90 endif
91 91  
92   -boot-y := arch/sparc/boot
93   -boot-$(CONFIG_SPARC64) := arch/sparc64/boot
  92 +boot := arch/sparc/boot
94 93  
95 94 image zImage tftpboot.img vmlinux.aout: vmlinux
96   - $(Q)$(MAKE) $(build)=$(boot-y) $(boot-y)/$@
  95 + $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
97 96  
98 97 archclean:
99   - $(Q)$(MAKE) $(clean)=$(boot-y)
  98 + $(Q)$(MAKE) $(clean)=$(boot)
100 99  
101 100 # This is the image used for packaging
102   -KBUILD_IMAGE := $(boot-y)/zImage
  101 +KBUILD_IMAGE := $(boot)/zImage
103 102  
104 103 # Don't use tabs in echo arguments.
105 104 ifeq ($(ARCH),sparc)
arch/sparc/boot/.gitignore
... ... @@ -2,4 +2,7 @@
2 2 btfixupprep
3 3 image
4 4 zImage
  5 +tftpboot.img
  6 +vmlinux.aout
  7 +piggyback
arch/sparc/boot/Makefile
... ... @@ -6,14 +6,16 @@
6 6 ROOT_IMG := /usr/src/root.img
7 7 ELFTOAOUT := elftoaout
8 8  
9   -hostprogs-y := piggyback btfixupprep
10   -targets := tftpboot.img btfix.o btfix.S image zImage
  9 +hostprogs-y := piggyback_32 piggyback_64 btfixupprep
  10 +targets := tftpboot.img btfix.o btfix.S image zImage vmlinux.aout
11 11 clean-files := System.map
12 12  
13 13 quiet_cmd_elftoaout = ELFTOAOUT $@
14 14 cmd_elftoaout = $(ELFTOAOUT) $(obj)/image -o $@
  15 +
  16 +ifeq ($(CONFIG_SPARC32),y)
15 17 quiet_cmd_piggy = PIGGY $@
16   - cmd_piggy = $(obj)/piggyback $@ $(obj)/System.map $(ROOT_IMG)
  18 + cmd_piggy = $(obj)/piggyback_32 $@ $(obj)/System.map $(ROOT_IMG)
17 19 quiet_cmd_btfix = BTFIX $@
18 20 cmd_btfix = $(OBJDUMP) -x vmlinux | $(obj)/btfixupprep > $@
19 21 quiet_cmd_sysmap = SYSMAP $(obj)/System.map
... ... @@ -62,4 +64,28 @@
62 64  
63 65 $(obj)/btfix.S: $(obj)/btfixupprep vmlinux FORCE
64 66 $(call if_changed,btfix)
  67 +
  68 +endif
  69 +
  70 +ifeq ($(CONFIG_SPARC64),y)
  71 +quiet_cmd_piggy = PIGGY $@
  72 + cmd_piggy = $(obj)/piggyback_64 $@ System.map $(ROOT_IMG)
  73 +quiet_cmd_strip = STRIP $@
  74 + cmd_strip = $(STRIP) -R .comment -R .note -K sun4u_init -K _end -K _start vmlinux -o $@
  75 +
  76 +
  77 +# Actual linking
  78 +$(obj)/image: vmlinux FORCE
  79 + $(call if_changed,strip)
  80 + @echo ' kernel: $@ is ready'
  81 +
  82 +$(obj)/tftpboot.img: vmlinux $(obj)/piggyback_64 System.map $(ROOT_IMG) FORCE
  83 + $(call if_changed,elftoaout)
  84 + $(call if_changed,piggy)
  85 + @echo ' kernel: $@ is ready'
  86 +
  87 +$(obj)/vmlinux.aout: vmlinux FORCE
  88 + $(call if_changed,elftoaout)
  89 + @echo ' kernel: $@ is ready'
  90 +endif
arch/sparc/boot/piggyback.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 <stdio.h>
23   -#include <string.h>
24   -#include <ctype.h>
25   -#include <errno.h>
26   -#include <fcntl.h>
27   -#include <dirent.h>
28   -#include <unistd.h>
29   -#include <stdlib.h>
30   -#include <sys/types.h>
31   -#include <sys/stat.h>
32   -
33   -/*
34   - * Note: run this on an a.out kernel (use elftoaout for it),
35   - * as PROM looks for a.out image only.
36   - */
37   -
38   -unsigned short ld2(char *p)
39   -{
40   - return (p[0] << 8) | p[1];
41   -}
42   -
43   -unsigned int ld4(char *p)
44   -{
45   - return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
46   -}
47   -
48   -void st4(char *p, unsigned int x)
49   -{
50   - p[0] = x >> 24;
51   - p[1] = x >> 16;
52   - p[2] = x >> 8;
53   - p[3] = x;
54   -}
55   -
56   -void usage(void)
57   -{
58   - /* fs_img.gz is an image of initial ramdisk. */
59   - fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
60   - fprintf(stderr, "\tKernel image will be modified in place.\n");
61   - exit(1);
62   -}
63   -
64   -void die(char *str)
65   -{
66   - perror (str);
67   - exit(1);
68   -}
69   -
70   -int main(int argc,char **argv)
71   -{
72   - static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
73   - unsigned char buffer[1024], *q, *r;
74   - unsigned int i, j, k, start, end, offset;
75   - FILE *map;
76   - struct stat s;
77   - int image, tail;
78   -
79   - if (argc != 4) usage();
80   - start = end = 0;
81   - if (stat (argv[3], &s) < 0) die (argv[3]);
82   - map = fopen (argv[2], "r");
83   - if (!map) die(argv[2]);
84   - while (fgets (buffer, 1024, map)) {
85   - if (!strcmp (buffer + 8, " T start\n") || !strcmp (buffer + 16, " T start\n"))
86   - start = strtoul (buffer, NULL, 16);
87   - else if (!strcmp (buffer + 8, " A end\n") || !strcmp (buffer + 16, " A end\n"))
88   - end = strtoul (buffer, NULL, 16);
89   - }
90   - fclose (map);
91   - if (!start || !end) {
92   - fprintf (stderr, "Could not determine start and end from System.map\n");
93   - exit(1);
94   - }
95   - if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
96   - if (read(image,buffer,512) != 512) die(argv[1]);
97   - if (memcmp (buffer, "\177ELF", 4) == 0) {
98   - q = buffer + ld4(buffer + 28);
99   - i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
100   - if (lseek(image,i,0) < 0) die("lseek");
101   - if (read(image,buffer,512) != 512) die(argv[1]);
102   - j = 0;
103   - } else if (memcmp(buffer, aout_magic, 4) == 0) {
104   - i = j = 32;
105   - } else {
106   - fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
107   - exit(1);
108   - }
109   - k = i;
110   - i += (ld2(buffer + j + 2)<<2) - 512;
111   - if (lseek(image,i,0) < 0) die("lseek");
112   - if (read(image,buffer,1024) != 1024) die(argv[1]);
113   - for (q = buffer, r = q + 512; q < r; q += 4) {
114   - if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
115   - break;
116   - }
117   - if (q == r) {
118   - fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
119   - exit(1);
120   - }
121   - offset = i + (q - buffer) + 10;
122   - if (lseek(image, offset, 0) < 0) die ("lseek");
123   -
124   - st4(buffer, 0);
125   - st4(buffer + 4, 0x01000000);
126   - st4(buffer + 8, (end + 32 + 4095) & ~4095);
127   - st4(buffer + 12, s.st_size);
128   -
129   - if (write(image,buffer+2,14) != 14) die (argv[1]);
130   - if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0) die ("lseek");
131   - if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
132   - while ((i = read (tail,buffer,1024)) > 0)
133   - if (write(image,buffer,i) != i) die (argv[1]);
134   - if (close(image) < 0) die("close");
135   - if (close(tail) < 0) die("close");
136   - return 0;
137   -}
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 <stdio.h>
  23 +#include <string.h>
  24 +#include <ctype.h>
  25 +#include <errno.h>
  26 +#include <fcntl.h>
  27 +#include <dirent.h>
  28 +#include <unistd.h>
  29 +#include <stdlib.h>
  30 +#include <sys/types.h>
  31 +#include <sys/stat.h>
  32 +
  33 +/*
  34 + * Note: run this on an a.out kernel (use elftoaout for it),
  35 + * as PROM looks for a.out image only.
  36 + */
  37 +
  38 +unsigned short ld2(char *p)
  39 +{
  40 + return (p[0] << 8) | p[1];
  41 +}
  42 +
  43 +unsigned int ld4(char *p)
  44 +{
  45 + return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  46 +}
  47 +
  48 +void st4(char *p, unsigned int x)
  49 +{
  50 + p[0] = x >> 24;
  51 + p[1] = x >> 16;
  52 + p[2] = x >> 8;
  53 + p[3] = x;
  54 +}
  55 +
  56 +void usage(void)
  57 +{
  58 + /* fs_img.gz is an image of initial ramdisk. */
  59 + fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
  60 + fprintf(stderr, "\tKernel image will be modified in place.\n");
  61 + exit(1);
  62 +}
  63 +
  64 +void die(char *str)
  65 +{
  66 + perror (str);
  67 + exit(1);
  68 +}
  69 +
  70 +int main(int argc,char **argv)
  71 +{
  72 + static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
  73 + unsigned char buffer[1024], *q, *r;
  74 + unsigned int i, j, k, start, end, offset;
  75 + FILE *map;
  76 + struct stat s;
  77 + int image, tail;
  78 +
  79 + if (argc != 4) usage();
  80 + start = end = 0;
  81 + if (stat (argv[3], &s) < 0) die (argv[3]);
  82 + map = fopen (argv[2], "r");
  83 + if (!map) die(argv[2]);
  84 + while (fgets (buffer, 1024, map)) {
  85 + if (!strcmp (buffer + 8, " T start\n") || !strcmp (buffer + 16, " T start\n"))
  86 + start = strtoul (buffer, NULL, 16);
  87 + else if (!strcmp (buffer + 8, " A end\n") || !strcmp (buffer + 16, " A end\n"))
  88 + end = strtoul (buffer, NULL, 16);
  89 + }
  90 + fclose (map);
  91 + if (!start || !end) {
  92 + fprintf (stderr, "Could not determine start and end from System.map\n");
  93 + exit(1);
  94 + }
  95 + if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
  96 + if (read(image,buffer,512) != 512) die(argv[1]);
  97 + if (memcmp (buffer, "\177ELF", 4) == 0) {
  98 + q = buffer + ld4(buffer + 28);
  99 + i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
  100 + if (lseek(image,i,0) < 0) die("lseek");
  101 + if (read(image,buffer,512) != 512) die(argv[1]);
  102 + j = 0;
  103 + } else if (memcmp(buffer, aout_magic, 4) == 0) {
  104 + i = j = 32;
  105 + } else {
  106 + fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
  107 + exit(1);
  108 + }
  109 + k = i;
  110 + i += (ld2(buffer + j + 2)<<2) - 512;
  111 + if (lseek(image,i,0) < 0) die("lseek");
  112 + if (read(image,buffer,1024) != 1024) die(argv[1]);
  113 + for (q = buffer, r = q + 512; q < r; q += 4) {
  114 + if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
  115 + break;
  116 + }
  117 + if (q == r) {
  118 + fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
  119 + exit(1);
  120 + }
  121 + offset = i + (q - buffer) + 10;
  122 + if (lseek(image, offset, 0) < 0) die ("lseek");
  123 +
  124 + st4(buffer, 0);
  125 + st4(buffer + 4, 0x01000000);
  126 + st4(buffer + 8, (end + 32 + 4095) & ~4095);
  127 + st4(buffer + 12, s.st_size);
  128 +
  129 + if (write(image,buffer+2,14) != 14) die (argv[1]);
  130 + if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0) die ("lseek");
  131 + if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
  132 + while ((i = read (tail,buffer,1024)) > 0)
  133 + if (write(image,buffer,i) != i) die (argv[1]);
  134 + if (close(image) < 0) die("close");
  135 + if (close(tail) < 0) die("close");
  136 + return 0;
  137 +}
arch/sparc/boot/piggyback_64.c
  1 +/*
  2 + Simple utility to make a single-image install kernel with initial ramdisk
  3 + for Sparc64 tftpbooting without need to set up nfs.
  4 +
  5 + Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6 +
  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.
  11 +
  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.
  16 +
  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. */
  20 +
  21 +#include <stdio.h>
  22 +#include <string.h>
  23 +#include <ctype.h>
  24 +#include <errno.h>
  25 +#include <fcntl.h>
  26 +#include <dirent.h>
  27 +#include <unistd.h>
  28 +#include <stdlib.h>
  29 +#include <sys/types.h>
  30 +#include <sys/stat.h>
  31 +
  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 */
  34 +
  35 +void die(char *str)
  36 +{
  37 + perror (str);
  38 + exit(1);
  39 +}
  40 +
  41 +int main(int argc,char **argv)
  42 +{
  43 + char buffer [1024], *q, *r;
  44 + unsigned int i, j, k, start, end, offset;
  45 + FILE *map;
  46 + struct stat s;
  47 + int image, tail;
  48 +
  49 + if (stat (argv[3], &s) < 0) die (argv[3]);
  50 + map = fopen (argv[2], "r");
  51 + if (!map) die(argv[2]);
  52 + while (fgets (buffer, 1024, map)) {
  53 + if (!strcmp (buffer + 19, "_start\n"))
  54 + start = strtoul (buffer + 8, NULL, 16);
  55 + else if (!strcmp (buffer + 19, "_end\n"))
  56 + end = strtoul (buffer + 8, NULL, 16);
  57 + }
  58 + fclose (map);
  59 + if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
  60 + if (read(image,buffer,512) != 512) die(argv[1]);
  61 + if (!memcmp (buffer, "\177ELF", 4)) {
  62 + unsigned int *p = (unsigned int *)(buffer + *(unsigned int *)(buffer + 28));
  63 +
  64 + i = p[1] + *(unsigned int *)(buffer + 24) - p[2];
  65 + if (lseek(image,i,0) < 0) die("lseek");
  66 + if (read(image,buffer,512) != 512) die(argv[1]);
  67 + j = 0;
  68 + } else if (*(unsigned int *)buffer == 0x01030107) {
  69 + i = j = 32;
  70 + } else {
  71 + fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
  72 + exit(1);
  73 + }
  74 + k = i;
  75 + if (j == 32 && buffer[40] == 'H' && buffer[41] == 'd' && buffer[42] == 'r' && buffer[43] == 'S') {
  76 + offset = 40 + 10;
  77 + } else {
  78 + i += ((*(unsigned short *)(buffer + j + 2))<<2) - 512;
  79 + if (lseek(image,i,0) < 0) die("lseek");
  80 + if (read(image,buffer,1024) != 1024) die(argv[1]);
  81 + for (q = buffer, r = q + 512; q < r; q += 4) {
  82 + if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
  83 + break;
  84 + }
  85 + if (q == r) {
  86 + fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
  87 + exit(1);
  88 + }
  89 + offset = i + (q - buffer) + 10;
  90 + }
  91 + if (lseek(image, offset, 0) < 0) die ("lseek");
  92 + *(unsigned *)buffer = 0;
  93 + *(unsigned *)(buffer + 4) = 0x01000000;
  94 + *(unsigned *)(buffer + 8) = ((end + 32 + 8191) & ~8191);
  95 + *(unsigned *)(buffer + 12) = s.st_size;
  96 + if (write(image,buffer+2,14) != 14) die (argv[1]);
  97 + if (lseek(image, 4, 0) < 0) die ("lseek");
  98 + *(unsigned *)buffer = ((end + 32 + 8191) & ~8191) - (start & ~0x3fffffUL) + s.st_size;
  99 + *(unsigned *)(buffer + 4) = 0;
  100 + *(unsigned *)(buffer + 8) = 0;
  101 + if (write(image,buffer,12) != 12) die (argv[1]);
  102 + if (lseek(image, k - start + ((end + 32 + 8191) & ~8191), 0) < 0) die ("lseek");
  103 + if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
  104 + while ((i = read (tail,buffer,1024)) > 0)
  105 + if (write(image,buffer,i) != i) die (argv[1]);
  106 + if (close(image) < 0) die("close");
  107 + if (close(tail) < 0) die("close");
  108 + return 0;
  109 +}
arch/sparc64/boot/.gitignore
1   -image
2   -tftpboot.img
3   -vmlinux.aout
4   -piggyback
arch/sparc64/boot/Makefile
1   -# Makefile for the Sparc64 boot stuff.
2   -#
3   -# Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
4   -# Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5   -
6   -ROOT_IMG := /usr/src/root.img
7   -ELFTOAOUT := elftoaout
8   -
9   -hostprogs-y := piggyback
10   -targets := image tftpboot.img vmlinux.aout
11   -
12   -quiet_cmd_elftoaout = ELF2AOUT $@
13   - cmd_elftoaout = $(ELFTOAOUT) vmlinux -o $@
14   -quiet_cmd_piggy = PIGGY $@
15   - cmd_piggy = $(obj)/piggyback $@ System.map $(ROOT_IMG)
16   -quiet_cmd_strip = STRIP $@
17   - cmd_strip = $(STRIP) -R .comment -R .note -K sun4u_init -K _end -K _start vmlinux -o $@
18   -
19   -
20   -# Actual linking
21   -$(obj)/image: vmlinux FORCE
22   - $(call if_changed,strip)
23   - @echo ' kernel: $@ is ready'
24   -
25   -$(obj)/tftpboot.img: vmlinux $(obj)/piggyback System.map $(ROOT_IMG) FORCE
26   - $(call if_changed,elftoaout)
27   - $(call if_changed,piggy)
28   - @echo ' kernel: $@ is ready'
29   -
30   -$(obj)/vmlinux.aout: vmlinux FORCE
31   - $(call if_changed,elftoaout)
32   - @echo ' kernel: $@ is ready'
arch/sparc64/boot/piggyback.c
1   -/*
2   - Simple utility to make a single-image install kernel with initial ramdisk
3   - for Sparc64 tftpbooting without need to set up nfs.
4   -
5   - Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6   -
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.
11   -
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.
16   -
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. */
20   -
21   -#include <stdio.h>
22   -#include <string.h>
23   -#include <ctype.h>
24   -#include <errno.h>
25   -#include <fcntl.h>
26   -#include <dirent.h>
27   -#include <unistd.h>
28   -#include <stdlib.h>
29   -#include <sys/types.h>
30   -#include <sys/stat.h>
31   -
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 */
34   -
35   -void die(char *str)
36   -{
37   - perror (str);
38   - exit(1);
39   -}
40   -
41   -int main(int argc,char **argv)
42   -{
43   - char buffer [1024], *q, *r;
44   - unsigned int i, j, k, start, end, offset;
45   - FILE *map;
46   - struct stat s;
47   - int image, tail;
48   -
49   - if (stat (argv[3], &s) < 0) die (argv[3]);
50   - map = fopen (argv[2], "r");
51   - if (!map) die(argv[2]);
52   - while (fgets (buffer, 1024, map)) {
53   - if (!strcmp (buffer + 19, "_start\n"))
54   - start = strtoul (buffer + 8, NULL, 16);
55   - else if (!strcmp (buffer + 19, "_end\n"))
56   - end = strtoul (buffer + 8, NULL, 16);
57   - }
58   - fclose (map);
59   - if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
60   - if (read(image,buffer,512) != 512) die(argv[1]);
61   - if (!memcmp (buffer, "\177ELF", 4)) {
62   - unsigned int *p = (unsigned int *)(buffer + *(unsigned int *)(buffer + 28));
63   -
64   - i = p[1] + *(unsigned int *)(buffer + 24) - p[2];
65   - if (lseek(image,i,0) < 0) die("lseek");
66   - if (read(image,buffer,512) != 512) die(argv[1]);
67   - j = 0;
68   - } else if (*(unsigned int *)buffer == 0x01030107) {
69   - i = j = 32;
70   - } else {
71   - fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
72   - exit(1);
73   - }
74   - k = i;
75   - if (j == 32 && buffer[40] == 'H' && buffer[41] == 'd' && buffer[42] == 'r' && buffer[43] == 'S') {
76   - offset = 40 + 10;
77   - } else {
78   - i += ((*(unsigned short *)(buffer + j + 2))<<2) - 512;
79   - if (lseek(image,i,0) < 0) die("lseek");
80   - if (read(image,buffer,1024) != 1024) die(argv[1]);
81   - for (q = buffer, r = q + 512; q < r; q += 4) {
82   - if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
83   - break;
84   - }
85   - if (q == r) {
86   - fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
87   - exit(1);
88   - }
89   - offset = i + (q - buffer) + 10;
90   - }
91   - if (lseek(image, offset, 0) < 0) die ("lseek");
92   - *(unsigned *)buffer = 0;
93   - *(unsigned *)(buffer + 4) = 0x01000000;
94   - *(unsigned *)(buffer + 8) = ((end + 32 + 8191) & ~8191);
95   - *(unsigned *)(buffer + 12) = s.st_size;
96   - if (write(image,buffer+2,14) != 14) die (argv[1]);
97   - if (lseek(image, 4, 0) < 0) die ("lseek");
98   - *(unsigned *)buffer = ((end + 32 + 8191) & ~8191) - (start & ~0x3fffffUL) + s.st_size;
99   - *(unsigned *)(buffer + 4) = 0;
100   - *(unsigned *)(buffer + 8) = 0;
101   - if (write(image,buffer,12) != 12) die (argv[1]);
102   - if (lseek(image, k - start + ((end + 32 + 8191) & ~8191), 0) < 0) die ("lseek");
103   - if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
104   - while ((i = read (tail,buffer,1024)) > 0)
105   - if (write(image,buffer,i) != i) die (argv[1]);
106   - if (close(image) < 0) die("close");
107   - if (close(tail) < 0) die("close");
108   - return 0;
109   -}