initial version
[otheros_bootloader.git] / mkimage.c
blob5714bf97cdb6c2f3862fa13bafa7ee915b385a2b
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <arpa/inet.h>
7 static void usage(const char *progname);
9 static unsigned char header_magic[] =
11 0x0F, 0xAC, 0xE0, 0xFF, 0xDE, 0xAD, 0xBA, 0xBE
15 * main
17 int main(int argc, char **argv)
19 FILE *in, *out;
20 uint32_t size;
21 unsigned char buf[4096];
22 int nread, n;
24 if (argc != 3)
25 usage(argv[0]);
27 in = fopen(argv[1], "r");
28 if (in == NULL)
30 fprintf(stderr, "couldn't open file '%s'\n", argv[1]);
31 exit(1);
34 out = fopen(argv[2], "w");
35 if (out == NULL)
37 fprintf(stderr, "couldn't open file '%s'\n", argv[2]);
38 exit(1);
42 n = fwrite(header_magic, 1, sizeof(header_magic), out);
43 if (n != sizeof(header_magic))
45 fprintf(stderr, "fwrite failed\n");
46 exit(1);
49 size = 0;
51 n = fwrite(&size, 1, sizeof(size), out);
52 if (n != sizeof(size))
54 fprintf(stderr, "fwrite failed\n");
55 exit(1);
58 n = fseek(in, 0, SEEK_END);
59 if (n < 0)
61 fprintf(stderr, "fseek failed\n");
62 exit(1);
65 size = ftell(in);
67 n = fseek(in, 0, SEEK_SET);
68 if (n < 0)
70 fprintf(stderr, "fseek failed\n");
71 exit(1);
74 size = htonl(size);
76 n = fwrite(&size, 1, sizeof(size), out);
77 if (n != sizeof(size))
79 fprintf(stderr, "fwrite failed\n");
80 exit(1);
83 n = fseek(out, 0x400, SEEK_SET);
84 if (n < 0)
86 fprintf(stderr, "fseek failed\n");
87 exit(1);
90 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0)
92 n = fwrite(buf, 1, nread, out);
93 if (n != nread)
95 fprintf(stderr, "fwrite failed\n");
96 exit(1);
100 fclose(in);
101 fclose(out);
103 exit(0);
107 * usage
109 static void usage(const char *progname)
111 fprintf(stderr, "usage: %s <input file> <output file>\n", progname);
112 exit(1);