File: /usr/src/linux/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     
12     #include "ecoff.h"
13     
14     #define MIPS_PAGE_SIZE	4096
15     #define MIPS_PAGE_MASK	(MIPS_PAGE_SIZE-1)
16     
17     #define swab16(x) \
18             ((unsigned short)( \
19                     (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
20                     (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
21     
22     #define swab32(x) \
23             ((unsigned int)( \
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) ))
28     
29     #define SWAB(a)	(swab ? swab32(a) : (a))
30     
31     void die (char *s)
32     {
33     	perror (s);
34     	exit (1);
35     }
36     
37     int main (int argc, char *argv[])
38     {
39     	int fd_vmlinux,fd_initrd,fd_outfile;
40     	FILHDR efile;
41     	AOUTHDR eaout;
42     	SCNHDR esecs[3];
43     	struct stat st;
44     	char buf[1024];
45     	unsigned long loadaddr;
46     	unsigned long initrd_header[2];
47     	int i;
48     	int swab = 0;
49     
50     	if (argc != 4) {
51     		printf ("Usage: %s <vmlinux> <initrd> <outfile>\n",argv[0]);
52     		exit (1);
53     	}
54     
55     	if ((fd_vmlinux = open (argv[1],O_RDWR)) < 0)
56     		 die ("open vmlinux");
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");
63     
64     	/*
65     	 * check whether the file is good for us
66     	 */
67     	/* TBD */
68     
69     	/*
70     	 * check, if we have to swab words
71     	 */
72     	if (ntohs(0xaa55) == 0xaa55) {
73     		if (efile.f_magic == swab16(MIPSELMAGIC))
74     			swab = 1;
75     	} else {
76     		if (efile.f_magic == swab16(MIPSEBMAGIC))
77     			swab = 1;
78     	}
79     
80     	if ((fd_initrd = open (argv[2], O_RDONLY)) < 0)
81     		die ("open initrd");
82     	if (fstat (fd_initrd, &st) < 0)
83     		die ("fstat initrd");
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);
92     
93     	if ((fd_outfile = open (argv[3], O_RDWR|O_CREAT|O_TRUNC,0666)) < 0)
94     		die ("open outfile");
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");
109     	close (fd_vmlinux);
110     	close (fd_initrd);
111     	return 0;
112     }
113