mpspec.h: Tweak the write_smp_table macro so that it is safe if passed a complex...
[coreboot.git] / util / buildrom / buildrom.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7
8
9 /* this is the beginning of a tool which will eventually 
10  * be able to build rom images with both fallback and 
11  * normal. For now it just builds a single image
12  * into a rom iamge
13  */
14 /* one switch we already need: -zero allowing you to tell what 
15  * to do with numbers that are "zero": make them 0xff or whatever
16  * for flash
17  * For now we assume "zero" is 0xff
18  */
19
20 void usage()
21 {
22         fprintf(stderr, "Usage: buildrom <input> <output> <payload> ");
23         fprintf(stderr, " <linuxbios-size> <total-size>\n");
24         exit(1);
25 }
26
27 void fatal(char *s)
28 {
29         perror(s);
30         exit(2);
31 }
32
33 int main(int argc, char *argv[])
34 {
35         int infd, payloadfd, outfd, size, insize, readlen, writelen, i;
36         int romsize;
37         unsigned char *cp;
38         struct stat inbuf, payloadbuf;
39         char zero = 0xff;
40
41         if (argc != 6)
42                 usage();
43
44         infd = open(argv[1], O_RDONLY);
45         if (infd < 0)
46                 fatal(argv[1]);
47         outfd = open(argv[2], O_RDWR | O_CREAT, 0666);
48         if (outfd < 0)
49                 fatal(argv[2]);
50         payloadfd = open(argv[3], O_RDONLY);
51         if (payloadfd < 0)
52                 fatal(argv[3]);
53
54         size = strtol(argv[4], 0, 0);
55         romsize = strtol(argv[5], 0, 0);
56
57         if (fstat(infd, &inbuf) < 0)
58                 fatal("stat of infile");
59         if (inbuf.st_size > size) {
60                 fprintf(stderr, "linuxbios image is %d bytes; only %d allowed\n",
61                         inbuf.st_size, size);
62                 fatal("Linuxbios input file larger than allowed size!\n");
63         }
64
65         if (fstat(payloadfd, &payloadbuf) < 0)
66                 fatal("stat of infile");
67         if (payloadbuf.st_size > (romsize - size))
68                 fatal("payload + linuxbios size larger than ROM size!\n");
69
70         cp = malloc(romsize);
71         if (!cp)
72                 fatal("malloc buffer");
73         for (i = 0; i < romsize; i++) {
74                 cp[i] = zero;
75         }
76
77         /* read the input file in at the END of the array */
78         readlen = read(infd, &cp[romsize - inbuf.st_size], inbuf.st_size);
79         if (readlen < inbuf.st_size) {
80                 fprintf(stderr, "Wanted %d, got %d\n", inbuf.st_size, readlen);
81                 fatal("Read input file");
82         }
83
84         /* read the payload file in at the START of the array */
85         readlen = read(payloadfd, cp, payloadbuf.st_size);
86         if (readlen < payloadbuf.st_size) {
87                 fprintf(stderr, "Wanted %d, got %d\n",
88                         payloadbuf.st_size, readlen);
89                 fatal("Read payload file");
90         }
91         writelen = write(outfd, cp, romsize);
92         if (writelen < size) {
93                 fprintf(stderr, "Wanted %d, got %d\n", size, writelen);
94                 fatal("Write output file");
95         }
96
97         return 0;
98 }