summaryrefslogtreecommitdiff
path: root/util/buildrom/buildrom.c
blob: cdbb45d2d46e204498d1274eaf65e623ea342503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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
 */

void usage()
{
	fprintf(stderr, "Usage: buildrom <input> <output> <payload> ");
	fprintf(stderr, " <linuxbios-size> <total-size>\n");
	exit(1);
}

void fatal(char *s)
{
	perror(s);
	exit(2);
}

int main(int argc, char *argv[])
{
	int infd, payloadfd, outfd, size, insize, readlen, writelen, i;
	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]);
	outfd = open(argv[2], O_RDWR | O_CREAT, 0666);
	if (outfd < 0)
		fatal(argv[2]);
	payloadfd = open(argv[3], O_RDONLY);
	if (payloadfd < 0)
		fatal(argv[3]);

	size = strtol(argv[4], 0, 0);
	romsize = strtol(argv[5], 0, 0);

	if (fstat(infd, &inbuf) < 0)
		fatal("stat of infile");
	if (inbuf.st_size > size) {
		fprintf(stderr, "linuxbios image is %d bytes; only %d allowed\n",
			inbuf.st_size, size);
		fatal("Linuxbios input file larger than allowed size!\n");
	}

	if (fstat(payloadfd, &payloadbuf) < 0)
		fatal("stat of infile");
	if (payloadbuf.st_size > (romsize - size))
		fatal("payload + linuxbios size larger than ROM size!\n");

	cp = malloc(romsize);
	if (!cp)
		fatal("malloc buffer");
	for (i = 0; i < romsize; i++) {
		cp[i] = zero;
	}

	/* 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) {
		fprintf(stderr, "Wanted %d, got %d\n", inbuf.st_size, readlen);
		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) {
		fprintf(stderr, "Wanted %d, got %d\n",
			payloadbuf.st_size, readlen);
		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;
}