buildrom.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <fcntl.h>
00006 #include <unistd.h>
00007 
00008 
00009 /* this is the beginning of a tool which will eventually 
00010  * be able to build rom images with both fallback and 
00011  * normal. For now it just builds a single image
00012  * into a rom iamge
00013  */
00014 /* one switch we already need: -zero allowing you to tell what 
00015  * to do with numbers that are "zero": make them 0xff or whatever
00016  * for flash
00017  * For now we assume "zero" is 0xff
00018  */
00019 
00020 void usage()
00021 {
00022         fprintf(stderr, "Usage: buildrom <input> <output> <payload> ");
00023         fprintf(stderr, " <coreboot-size> <total-size>\n");
00024         exit(1);
00025 }
00026 
00027 void fatal(char *s)
00028 {
00029         perror(s);
00030         exit(2);
00031 }
00032 
00033 int main(int argc, char *argv[])
00034 {
00035         int infd, payloadfd, outfd, size, readlen, writelen, i;
00036         int romsize;
00037         unsigned char *cp;
00038         struct stat inbuf, payloadbuf;
00039         char zero = 0xff;
00040 
00041         if (argc != 6)
00042                 usage();
00043 
00044         infd = open(argv[1], O_RDONLY);
00045         if (infd < 0)
00046                 fatal(argv[1]);
00047         outfd = open(argv[2], O_RDWR | O_CREAT, 0666);
00048         if (outfd < 0)
00049                 fatal(argv[2]);
00050         payloadfd = open(argv[3], O_RDONLY);
00051         if (payloadfd < 0)
00052                 fatal(argv[3]);
00053 
00054         size = strtol(argv[4], 0, 0);
00055         romsize = strtol(argv[5], 0, 0);
00056 
00057         if (fstat(infd, &inbuf) < 0)
00058                 fatal("stat of infile");
00059         if (inbuf.st_size > size) {
00060                 fprintf(stderr, "coreboot image is %d bytes; only %d allowed\n",
00061                         (int)inbuf.st_size, size);
00062                 fatal("Coreboot input file larger than allowed size!\n");
00063         }
00064 
00065         if (fstat(payloadfd, &payloadbuf) < 0)
00066                 fatal("stat of infile");
00067         if (payloadbuf.st_size > (romsize - size)){
00068                 fprintf(stderr, "ERROR: payload (%d) + coreboot (%d) - Size is %d bytes larger than ROM size (%d).\n", 
00069                                 (int)payloadbuf.st_size, size, 
00070                                 (int)(payloadbuf.st_size+size-romsize),
00071                                 romsize);
00072                 exit(1);
00073         }
00074 
00075         printf("Payload: %d coreboot: %d ROM size: %d Left space: %d\n",
00076                         (int)payloadbuf.st_size, size, romsize,
00077                         (int)(romsize-payloadbuf.st_size-size));
00078 
00079         cp = malloc(romsize);
00080         if (!cp)
00081                 fatal("malloc buffer");
00082         for (i = 0; i < romsize; i++) {
00083                 cp[i] = zero;
00084         }
00085 
00086         /* read the input file in at the END of the array */
00087         readlen = read(infd, &cp[romsize - inbuf.st_size], inbuf.st_size);
00088         if (readlen < inbuf.st_size) {
00089                 fprintf(stderr, "Wanted %d, got %d\n", (int)inbuf.st_size, readlen);
00090                 fatal("Read input file");
00091         }
00092 
00093         /* read the payload file in at the START of the array */
00094         readlen = read(payloadfd, cp, payloadbuf.st_size);
00095         if (readlen < payloadbuf.st_size) {
00096                 fprintf(stderr, "Wanted %d, got %d\n",
00097                         (int)payloadbuf.st_size, readlen);
00098                 fatal("Read payload file");
00099         }
00100         writelen = write(outfd, cp, romsize);
00101         if (writelen < size) {
00102                 fprintf(stderr, "Wanted %d, got %d\n", size, writelen);
00103                 fatal("Write output file");
00104         }
00105 
00106         return 0;
00107 }

Generated on Mon Dec 29 10:54:12 2008 for coreboot by  doxygen 1.5.5