blob: d8ee0ca46b25c2cd0baf9240390a49de18ebd5f4 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Iru Cai5fd00ce2017-03-26 12:05:32 +08002
3#include <stdio.h>
4#include <stdlib.h>
5
6static void usage(const char *s)
7{
Iru Cai42254932017-07-06 00:04:47 +08008 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");
Iru Cai5fd00ce2017-03-26 12:05:32 +080020 exit(1);
21}
22
23static 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
Iru Cai42254932017-07-06 00:04:47 +080031static long negoffset(long a, long romsz)
32{
33 if (a > 0) {
34 if (a & 0x80000000) /* the address in memory, and sizeof(long)
35 is 8 */
36 return a - 0x100000000;
37 else /* the file offset */
38 return a - romsz;
39 } else {
40 return a;
41 }
42}
43
Iru Cai5fd00ce2017-03-26 12:05:32 +080044int main(int argc, char *argv[])
45{
Iru Cai42254932017-07-06 00:04:47 +080046 FILE *fp, *fw1, *fw2;
47 long offset1, offset2;
48
49 if (argc != 4 && argc != 6)
Iru Cai5fd00ce2017-03-26 12:05:32 +080050 usage(argv[0]);
51
Iru Cai42254932017-07-06 00:04:47 +080052 fp = fopen(argv[1], "rb+");
53 if (fp == NULL) {
54 puts("Error opening firmware image!");
Iru Cai5fd00ce2017-03-26 12:05:32 +080055 exit(1);
56 }
57
Iru Cai42254932017-07-06 00:04:47 +080058 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
Iru Cai5fd00ce2017-03-26 12:05:32 +080075 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
Iru Cai42254932017-07-06 00:04:47 +080090 offset1 = negoffset(offset1, romsz);
91 offset2 = negoffset(offset2, romsz);
Iru Cai5fd00ce2017-03-26 12:05:32 +080092
93 /* write two offsets to $s-0x100 */
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];
Iru Cai42254932017-07-06 00:04:47 +0800106 for (size_t i = 0; i < 8; i++)
Iru Cai5fd00ce2017-03-26 12:05:32 +0800107 printf("%02hhx ", offs[i]);
Iru Cai42254932017-07-06 00:04:47 +0800108
Iru Cai5fd00ce2017-03-26 12:05:32 +0800109 puts("");
110 FseekEnd(fp, -0x100);
111 printf("writing to 0x%lx\n", ftell(fp));
112 fwrite(offs, 1, 8, fp);
113
Iru Cai42254932017-07-06 00:04:47 +0800114 if (argc == 6) {
115 /* write fw1 and fw2 */
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);
Iru Cai5fd00ce2017-03-26 12:05:32 +0800129 }
130
131 fclose(fp);
Iru Cai5fd00ce2017-03-26 12:05:32 +0800132 return 0;
133}