2 * addinitrd - program to add a initrd image to an ecoff kernel
4 * (C) 1999 Thomas Bogendoerfer
12 #include <netinet/in.h>
16 #define MIPS_PAGE_SIZE 4096
17 #define MIPS_PAGE_MASK (MIPS_PAGE_SIZE-1)
21 (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
22 (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
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) ))
31 #define SWAB(a) (swab ? swab32(a) : (a))
39 int main (int argc, char *argv[])
41 int fd_vmlinux,fd_initrd,fd_outfile;
47 unsigned long loadaddr;
48 unsigned long initrd_header[2];
53 printf ("Usage: %s <vmlinux> <initrd> <outfile>\n",argv[0]);
57 if ((fd_vmlinux = open (argv[1],O_RDWR)) < 0)
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");
66 * check whether the file is good for us
71 * check, if we have to swab words
73 if (ntohs(0xaa55) == 0xaa55) {
74 if (efile.f_magic == swab16(MIPSELMAGIC))
77 if (efile.f_magic == swab16(MIPSEBMAGIC))
81 if ((fd_initrd = open (argv[2], O_RDONLY)) < 0)
83 if (fstat (fd_initrd, &st) < 0)
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);
94 if ((fd_outfile = open (argv[3], O_RDWR|O_CREAT|O_TRUNC,0666)) < 0)
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");
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);
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");
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");