2 * addinitrd - program to add a initrd image to an ecoff kernel
4 * (C) 1999 Thomas Bogendoerfer
14 #define MIPS_PAGE_SIZE 4096
15 #define MIPS_PAGE_MASK (MIPS_PAGE_SIZE-1)
19 (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
20 (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
24 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
25 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
26 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
27 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
29 #define SWAB(a) (swab ? swab32(a) : (a))
37 int main (int argc
, char *argv
[])
39 int fd_vmlinux
,fd_initrd
,fd_outfile
;
45 unsigned long loadaddr
;
46 unsigned long initrd_header
[2];
51 printf ("Usage: %s <vmlinux> <initrd> <outfile>\n",argv
[0]);
55 if ((fd_vmlinux
= open (argv
[1],O_RDWR
)) < 0)
57 if (read (fd_vmlinux
, &efile
, sizeof efile
) != sizeof efile
)
58 die ("read file header");
59 if (read (fd_vmlinux
, &eaout
, sizeof eaout
) != sizeof eaout
)
60 die ("read aout header");
61 if (read (fd_vmlinux
, esecs
, sizeof esecs
) != sizeof esecs
)
62 die ("read section headers");
65 * check whether the file is good for us
70 * check, if we have to swab words
72 if (ntohs(0xaa55) == 0xaa55) {
73 if (efile
.f_magic
== swab16(MIPSELMAGIC
))
76 if (efile
.f_magic
== swab16(MIPSEBMAGIC
))
80 if ((fd_initrd
= open (argv
[2], O_RDONLY
)) < 0)
82 if (fstat (fd_initrd
, &st
) < 0)
84 loadaddr
= ((SWAB(esecs
[2].s_vaddr
) + SWAB(esecs
[2].s_size
)
85 + MIPS_PAGE_SIZE
-1) & ~MIPS_PAGE_MASK
) - 8;
86 if (loadaddr
< (SWAB(esecs
[2].s_vaddr
) + SWAB(esecs
[2].s_size
)))
87 loadaddr
+= MIPS_PAGE_SIZE
;
88 initrd_header
[0] = SWAB(0x494E5244);
89 initrd_header
[1] = SWAB(st
.st_size
);
90 eaout
.dsize
= esecs
[1].s_size
= initrd_header
[1] = SWAB(st
.st_size
+8);
91 eaout
.data_start
= esecs
[1].s_vaddr
= esecs
[1].s_paddr
= SWAB(loadaddr
);
93 if ((fd_outfile
= open (argv
[3], O_RDWR
|O_CREAT
|O_TRUNC
,0666)) < 0)
95 if (write (fd_outfile
, &efile
, sizeof efile
) != sizeof efile
)
96 die ("write file header");
97 if (write (fd_outfile
, &eaout
, sizeof eaout
) != sizeof eaout
)
98 die ("write aout header");
99 if (write (fd_outfile
, esecs
, sizeof esecs
) != sizeof esecs
)
100 die ("write section headers");
101 while ((i
= read (fd_vmlinux
, buf
, sizeof buf
)) > 0)
102 if (write (fd_outfile
, buf
, i
) != i
)
103 die ("write vmlinux");
104 if (write (fd_outfile
, initrd_header
, sizeof initrd_header
) != sizeof initrd_header
)
105 die ("write initrd header");
106 while ((i
= read (fd_initrd
, buf
, sizeof buf
)) > 0)
107 if (write (fd_outfile
, buf
, i
) != i
)
108 die ("write initrd");