| 1 |
|
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
static void usage(const char *s) |
| 7 |
{ |
| 8 |
printf("insert firmware blobs:\n\t" |
| 9 |
"%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n\n", |
| 10 |
s); |
| 11 |
printf("set addresses of firmware blobs only:\n\t" |
| 12 |
"%s <rom file> <fw1 offset> <fw2 offset>\n\n", |
| 13 |
s); |
| 14 |
printf( |
| 15 |
"offset can be (example is put it to 0x7ffa00 when ROM is 8MB):\n" |
| 16 |
"- file offset: 0x7ffa00\n" |
| 17 |
"- distance to the end of file: -0x600\n" |
| 18 |
"- the address when ROM is mapped to the end of memory: " |
| 19 |
"0xfffffa00\n"); |
| 20 |
exit(1); |
| 21 |
} |
| 22 |
|
| 23 |
static void FseekEnd(FILE *fp, long o) |
| 24 |
{ |
| 25 |
if (fseek(fp, o, SEEK_END) != 0) { |
| 26 |
puts("fseek() error!\n"); |
| 27 |
exit(1); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
static long negoffset(long a, long romsz) |
| 32 |
{ |
| 33 |
if (a > 0) { |
| 34 |
if (a & 0x80000000) |
| 35 |
|
| 36 |
return a - 0x100000000; |
| 37 |
else |
| 38 |
return a - romsz; |
| 39 |
} else { |
| 40 |
return a; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
int main(int argc, char *argv[]) |
| 45 |
{ |
| 46 |
FILE *fp, *fw1, *fw2; |
| 47 |
long offset1, offset2; |
| 48 |
|
| 49 |
if (argc != 4 && argc != 6) |
| 50 |
usage(argv[0]); |
| 51 |
|
| 52 |
fp = fopen(argv[1], "rb+"); |
| 53 |
if (fp == NULL) { |
| 54 |
puts("Error opening firmware image!"); |
| 55 |
exit(1); |
| 56 |
} |
| 57 |
|
| 58 |
if (argc == 6) { |
| 59 |
fw1 = fopen(argv[2], "rb"); |
| 60 |
fw2 = fopen(argv[3], "rb"); |
| 61 |
offset1 = strtoul(argv[4], NULL, 0); |
| 62 |
offset2 = strtoul(argv[5], NULL, 0); |
| 63 |
|
| 64 |
if (fw1 == NULL || fw2 == NULL) { |
| 65 |
puts("Error opening file!"); |
| 66 |
exit(1); |
| 67 |
} |
| 68 |
} else { |
| 69 |
fw1 = NULL; |
| 70 |
fw2 = NULL; |
| 71 |
offset1 = strtoul(argv[2], NULL, 0); |
| 72 |
offset2 = strtoul(argv[3], NULL, 0); |
| 73 |
} |
| 74 |
|
| 75 |
if ((offset1 & 0xff) || (offset2 & 0xff)) { |
| 76 |
puts("The offsets must be aligned to 0x100"); |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
|
| 80 |
long romsz; |
| 81 |
FseekEnd(fp, -1); |
| 82 |
romsz = ftell(fp) + 1; |
| 83 |
printf("size of %s: 0x%lx\n", argv[1], romsz); |
| 84 |
|
| 85 |
if (romsz & 0xff) { |
| 86 |
puts("The ROM size must be multiple of 0x100"); |
| 87 |
exit(1); |
| 88 |
} |
| 89 |
|
| 90 |
offset1 = negoffset(offset1, romsz); |
| 91 |
offset2 = negoffset(offset2, romsz); |
| 92 |
|
| 93 |
|
| 94 |
char offs[8]; |
| 95 |
long os; |
| 96 |
os = 0x1000000 + offset1; |
| 97 |
offs[0] = os >> 16; |
| 98 |
offs[1] = os >> 8; |
| 99 |
offs[2] = 0xff - offs[0]; |
| 100 |
offs[3] = 0xff - offs[1]; |
| 101 |
os = 0x1000000 + offset2; |
| 102 |
offs[4] = os >> 16; |
| 103 |
offs[5] = os >> 8; |
| 104 |
offs[6] = 0xff - offs[4]; |
| 105 |
offs[7] = 0xff - offs[5]; |
| 106 |
for (size_t i = 0; i < 8; i++) |
| 107 |
printf("%02hhx ", offs[i]); |
| 108 |
|
| 109 |
puts(""); |
| 110 |
FseekEnd(fp, -0x100); |
| 111 |
printf("writing to 0x%lx\n", ftell(fp)); |
| 112 |
fwrite(offs, 1, 8, fp); |
| 113 |
|
| 114 |
if (argc == 6) { |
| 115 |
|
| 116 |
char c; |
| 117 |
FseekEnd(fp, offset1); |
| 118 |
printf("writing to 0x%lx\n", ftell(fp)); |
| 119 |
while (fread(&c, 1, 1, fw1) == 1) |
| 120 |
fwrite(&c, 1, 1, fp); |
| 121 |
|
| 122 |
FseekEnd(fp, offset2); |
| 123 |
printf("writing to 0x%lx\n", ftell(fp)); |
| 124 |
while (fread(&c, 1, 1, fw2) == 1) |
| 125 |
fwrite(&c, 1, 1, fp); |
| 126 |
|
| 127 |
fclose(fw1); |
| 128 |
fclose(fw2); |
| 129 |
} |
| 130 |
|
| 131 |
fclose(fp); |
| 132 |
return 0; |
| 133 |
} |