ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / boot / addinitrd.c
1 /*
2  * addinitrd - program to add a initrd image to an ecoff kernel
3  *
4  * (C) 1999 Thomas Bogendoerfer
5  */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <netinet/in.h>
13
14 #include "ecoff.h"
15
16 #define MIPS_PAGE_SIZE  4096
17 #define MIPS_PAGE_MASK  (MIPS_PAGE_SIZE-1)
18
19 #define swab16(x) \
20         ((unsigned short)( \
21                 (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
22                 (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
23
24 #define swab32(x) \
25         ((unsigned int)( \
26                 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
27                 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) <<  8) | \
28                 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >>  8) | \
29                 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
30
31 #define SWAB(a) (swab ? swab32(a) : (a))
32
33 void die (char *s)
34 {
35         perror (s);
36         exit (1);
37 }
38
39 int main (int argc, char *argv[])
40 {
41         int fd_vmlinux,fd_initrd,fd_outfile;
42         FILHDR efile;
43         AOUTHDR eaout;
44         SCNHDR esecs[3];
45         struct stat st;
46         char buf[1024];
47         unsigned long loadaddr;
48         unsigned long initrd_header[2];
49         int i,cnt;
50         int swab = 0;
51
52         if (argc != 4) {
53                 printf ("Usage: %s <vmlinux> <initrd> <outfile>\n",argv[0]);
54                 exit (1);
55         }
56
57         if ((fd_vmlinux = open (argv[1],O_RDWR)) < 0)
58                  die ("open vmlinux");
59         if (read (fd_vmlinux, &efile, sizeof efile) != sizeof efile)
60                 die ("read file header");
61         if (read (fd_vmlinux, &eaout, sizeof eaout) != sizeof eaout)
62                 die ("read aout header");
63         if (read (fd_vmlinux, esecs, sizeof esecs) != sizeof esecs)
64                 die ("read section headers");
65         /*
66          * check whether the file is good for us
67          */
68         /* TBD */
69
70         /*
71          * check, if we have to swab words
72          */
73         if (ntohs(0xaa55) == 0xaa55) {
74                 if (efile.f_magic == swab16(MIPSELMAGIC))
75                         swab = 1;
76         } else {
77                 if (efile.f_magic == swab16(MIPSEBMAGIC))
78                         swab = 1;
79         }
80
81         if ((fd_initrd = open (argv[2], O_RDONLY)) < 0)
82                 die ("open initrd");
83         if (fstat (fd_initrd, &st) < 0)
84                 die ("fstat initrd");
85         loadaddr = ((SWAB(esecs[2].s_vaddr) + SWAB(esecs[2].s_size)
86                         + MIPS_PAGE_SIZE-1) & ~MIPS_PAGE_MASK) - 8;
87         if (loadaddr < (SWAB(esecs[2].s_vaddr) + SWAB(esecs[2].s_size)))
88                 loadaddr += MIPS_PAGE_SIZE;
89         initrd_header[0] = SWAB(0x494E5244);
90         initrd_header[1] = SWAB(st.st_size);
91         eaout.dsize = esecs[1].s_size = initrd_header[1] = SWAB(st.st_size+8);
92         eaout.data_start = esecs[1].s_vaddr = esecs[1].s_paddr = SWAB(loadaddr);
93
94         if ((fd_outfile = open (argv[3], O_RDWR|O_CREAT|O_TRUNC,0666)) < 0)
95                 die ("open outfile");
96         if (write (fd_outfile, &efile, sizeof efile) != sizeof efile)
97                 die ("write file header");
98         if (write (fd_outfile, &eaout, sizeof eaout) != sizeof eaout)
99                 die ("write aout header");
100         if (write (fd_outfile, esecs, sizeof esecs) != sizeof esecs)
101                 die ("write section headers");
102         /* skip padding */
103         if(lseek(fd_vmlinux, SWAB(esecs[0].s_scnptr), SEEK_SET) == (off_t)-1)
104                 die ("lseek vmlinux");
105         if(lseek(fd_outfile, SWAB(esecs[0].s_scnptr), SEEK_SET) == (off_t)-1)
106                 die ("lseek outfile");
107         /* copy text segment */
108         cnt = SWAB(eaout.tsize);
109         while (cnt) {
110                 if ((i = read (fd_vmlinux, buf, sizeof buf)) <= 0)
111                         die ("read vmlinux");
112                 if (write (fd_outfile, buf, i) != i)
113                         die ("write vmlinux");
114                 cnt -= i;
115         }
116         if (write (fd_outfile, initrd_header, sizeof initrd_header) != sizeof initrd_header)
117                 die ("write initrd header");
118         while ((i = read (fd_initrd, buf, sizeof buf)) > 0)
119                 if (write (fd_outfile, buf, i) != i)
120                         die ("write initrd");
121         close (fd_vmlinux);
122         close (fd_initrd);
123         return 0;
124 }