2003-06-20 18:46:48 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* this is the beginning of a tool which will eventually
|
|
|
|
* be able to build rom images with both fallback and
|
|
|
|
* normal. For now it just builds a single image
|
|
|
|
* into a rom iamge
|
|
|
|
*/
|
|
|
|
/* one switch we already need: -zero allowing you to tell what
|
|
|
|
* to do with numbers that are "zero": make them 0xff or whatever
|
|
|
|
* for flash
|
|
|
|
* For now we assume "zero" is 0xff
|
|
|
|
*/
|
|
|
|
|
2004-05-24 21:48:13 +02:00
|
|
|
void usage()
|
|
|
|
{
|
2003-06-20 18:46:48 +02:00
|
|
|
fprintf(stderr, "Usage: buildrom <input> <output> <payload> ");
|
2008-01-18 16:08:58 +01:00
|
|
|
fprintf(stderr, " <coreboot-size> <total-size>\n");
|
2003-06-20 18:46:48 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-05-24 21:48:13 +02:00
|
|
|
void fatal(char *s)
|
|
|
|
{
|
2003-06-20 18:46:48 +02:00
|
|
|
perror(s);
|
|
|
|
exit(2);
|
|
|
|
}
|
2004-05-24 21:48:13 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2005-12-29 15:55:09 +01:00
|
|
|
int infd, payloadfd, outfd, size, readlen, writelen, i;
|
2003-06-20 18:46:48 +02:00
|
|
|
int romsize;
|
|
|
|
unsigned char *cp;
|
|
|
|
struct stat inbuf, payloadbuf;
|
|
|
|
char zero = 0xff;
|
|
|
|
|
|
|
|
if (argc != 6)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
infd = open(argv[1], O_RDONLY);
|
|
|
|
if (infd < 0)
|
|
|
|
fatal(argv[1]);
|
2004-05-24 21:48:13 +02:00
|
|
|
outfd = open(argv[2], O_RDWR | O_CREAT, 0666);
|
2003-06-20 18:46:48 +02:00
|
|
|
if (outfd < 0)
|
|
|
|
fatal(argv[2]);
|
|
|
|
payloadfd = open(argv[3], O_RDONLY);
|
|
|
|
if (payloadfd < 0)
|
|
|
|
fatal(argv[3]);
|
2004-05-24 21:48:13 +02:00
|
|
|
|
2003-06-20 18:46:48 +02:00
|
|
|
size = strtol(argv[4], 0, 0);
|
|
|
|
romsize = strtol(argv[5], 0, 0);
|
|
|
|
|
|
|
|
if (fstat(infd, &inbuf) < 0)
|
|
|
|
fatal("stat of infile");
|
2004-05-24 21:48:13 +02:00
|
|
|
if (inbuf.st_size > size) {
|
2008-01-18 16:08:58 +01:00
|
|
|
fprintf(stderr, "coreboot image is %d bytes; only %d allowed\n",
|
2005-12-29 15:55:09 +01:00
|
|
|
(int)inbuf.st_size, size);
|
2008-01-18 16:08:58 +01:00
|
|
|
fatal("Coreboot input file larger than allowed size!\n");
|
2003-07-23 03:45:47 +02:00
|
|
|
}
|
2003-06-20 18:46:48 +02:00
|
|
|
|
|
|
|
if (fstat(payloadfd, &payloadbuf) < 0)
|
|
|
|
fatal("stat of infile");
|
2006-01-19 19:11:21 +01:00
|
|
|
if (payloadbuf.st_size > (romsize - size)){
|
2008-01-18 16:08:58 +01:00
|
|
|
fprintf(stderr, "ERROR: payload (%d) + coreboot (%d) - Size is %d bytes larger than ROM size (%d).\n",
|
2008-08-01 13:20:33 +02:00
|
|
|
(int)payloadbuf.st_size, size,
|
|
|
|
(int)(payloadbuf.st_size+size-romsize),
|
2007-04-12 01:36:02 +02:00
|
|
|
romsize);
|
2006-01-19 19:11:21 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-20 18:46:48 +02:00
|
|
|
|
2008-01-18 16:08:58 +01:00
|
|
|
printf("Payload: %d coreboot: %d ROM size: %d Left space: %d\n",
|
2008-08-01 13:20:33 +02:00
|
|
|
(int)payloadbuf.st_size, size, romsize,
|
|
|
|
(int)(romsize-payloadbuf.st_size-size));
|
2007-04-12 01:36:02 +02:00
|
|
|
|
2003-06-20 18:46:48 +02:00
|
|
|
cp = malloc(romsize);
|
2004-05-24 21:48:13 +02:00
|
|
|
if (!cp)
|
2003-06-20 18:46:48 +02:00
|
|
|
fatal("malloc buffer");
|
2004-11-11 07:53:24 +01:00
|
|
|
for (i = 0; i < romsize; i++) {
|
2003-06-20 18:46:48 +02:00
|
|
|
cp[i] = zero;
|
2004-11-11 07:53:24 +01:00
|
|
|
}
|
2004-05-24 21:48:13 +02:00
|
|
|
|
2003-06-20 18:46:48 +02:00
|
|
|
/* read the input file in at the END of the array */
|
|
|
|
readlen = read(infd, &cp[romsize - inbuf.st_size], inbuf.st_size);
|
|
|
|
if (readlen < inbuf.st_size) {
|
2005-12-29 15:55:09 +01:00
|
|
|
fprintf(stderr, "Wanted %d, got %d\n", (int)inbuf.st_size, readlen);
|
2003-06-20 18:46:48 +02:00
|
|
|
fatal("Read input file");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the payload file in at the START of the array */
|
|
|
|
readlen = read(payloadfd, cp, payloadbuf.st_size);
|
|
|
|
if (readlen < payloadbuf.st_size) {
|
2004-05-24 21:48:13 +02:00
|
|
|
fprintf(stderr, "Wanted %d, got %d\n",
|
2005-12-29 15:55:09 +01:00
|
|
|
(int)payloadbuf.st_size, readlen);
|
2003-06-20 18:46:48 +02:00
|
|
|
fatal("Read payload file");
|
|
|
|
}
|
|
|
|
writelen = write(outfd, cp, romsize);
|
|
|
|
if (writelen < size) {
|
|
|
|
fprintf(stderr, "Wanted %d, got %d\n", size, writelen);
|
|
|
|
fatal("Write output file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|