VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc / boot / utils / mktree.c
1 /*
2  * Makes a tree bootable image for IBM Evaluation boards.
3  * Basically, just take a zImage, skip the ELF header, and stuff
4  * a 32 byte header on the front.
5  *
6  * We use htonl, which is a network macro, to make sure we're doing
7  * The Right Thing on an LE machine.  It's non-obvious, but it should
8  * work on anything BSD'ish.
9  */
10
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <netinet/in.h>
18 #include <stdint.h>
19
20 /* This gets tacked on the front of the image.  There are also a few
21  * bytes allocated after the _start label used by the boot rom (see
22  * head.S for details).
23  */
24 typedef struct boot_block {
25         uint32_t bb_magic;              /* 0x0052504F */
26         uint32_t bb_dest;               /* Target address of the image */
27         uint32_t bb_num_512blocks;      /* Size, rounded-up, in 512 byte blks */
28         uint32_t bb_debug_flag; /* Run debugger or image after load */
29         uint32_t bb_entry_point;        /* The image address to start */
30         uint32_t bb_checksum;   /* 32 bit checksum including header */
31         uint32_t reserved[2];
32 } boot_block_t;
33
34 #define IMGBLK  512
35 char    tmpbuf[IMGBLK];
36
37 int main(int argc, char *argv[])
38 {
39         int     in_fd, out_fd;
40         int     nblks, i;
41         uint    cksum, *cp;
42         struct  stat    st;
43         boot_block_t    bt;
44
45         if (argc < 3) {
46                 fprintf(stderr, "usage: %s <zImage-file> <boot-image> [entry-point]\n",argv[0]);
47                 exit(1);
48         }
49
50         if (stat(argv[1], &st) < 0) {
51                 perror("stat");
52                 exit(2);
53         }
54
55         nblks = (st.st_size + IMGBLK) / IMGBLK;
56
57         bt.bb_magic = htonl(0x0052504F);
58
59         /* If we have the optional entry point parameter, use it */
60         if (argc == 4)
61                 bt.bb_dest = bt.bb_entry_point = htonl(strtoul(argv[3], NULL, 0));
62         else
63                 bt.bb_dest = bt.bb_entry_point = htonl(0x500000);
64
65         /* We know these from the linker command.
66          * ...and then move it up into memory a little more so the
67          * relocation can happen.
68          */
69         bt.bb_num_512blocks = htonl(nblks);
70         bt.bb_debug_flag = 0;
71
72         bt.bb_checksum = 0;
73
74         /* To be neat and tidy :-).
75         */
76         bt.reserved[0] = 0;
77         bt.reserved[1] = 0;
78
79         if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
80                 perror("zImage open");
81                 exit(3);
82         }
83
84         if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
85                 perror("bootfile open");
86                 exit(3);
87         }
88
89         cksum = 0;
90         cp = (void *)&bt;
91         for (i=0; i<sizeof(bt)/sizeof(uint); i++)
92                 cksum += *cp++;
93         
94         /* Assume zImage is an ELF file, and skip the 64K header.
95         */
96         if (read(in_fd, tmpbuf, IMGBLK) != IMGBLK) {
97                 fprintf(stderr, "%s is too small to be an ELF image\n",
98                                 argv[1]);
99                 exit(4);
100         }
101
102         if ((*(uint *)tmpbuf) != htonl(0x7f454c46)) {
103                 fprintf(stderr, "%s is not an ELF image\n", argv[1]);
104                 exit(4);
105         }
106
107         if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
108                 fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
109                 exit(4);
110         }
111
112         nblks -= (64 * 1024) / IMGBLK;
113
114         /* And away we go......
115         */
116         if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
117                 perror("boot-image write");
118                 exit(5);
119         }
120
121         while (nblks-- > 0) {
122                 if (read(in_fd, tmpbuf, IMGBLK) < 0) {
123                         perror("zImage read");
124                         exit(5);
125                 }
126                 cp = (uint *)tmpbuf;
127                 for (i=0; i<sizeof(tmpbuf)/sizeof(uint); i++)
128                         cksum += *cp++;
129                 if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
130                         perror("boot-image write");
131                         exit(5);
132                 }
133         }
134
135         /* rewrite the header with the computed checksum.
136         */
137         bt.bb_checksum = htonl(cksum);
138         if (lseek(out_fd, 0, SEEK_SET) < 0) {
139                 perror("rewrite seek");
140                 exit(1);
141         }
142         if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
143                 perror("boot-image rewrite");
144                 exit(1);
145         }
146
147         exit(0);
148 }