1 /* SPDX-License-Identifier: GPL-2.0-only */
6 static void usage(const char *s
)
8 printf("insert firmware blobs:\n\t"
9 "%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n\n",
11 printf("set addresses of firmware blobs only:\n\t"
12 "%s <rom file> <fw1 offset> <fw2 offset>\n\n",
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: "
23 static void FseekEnd(FILE *fp
, long o
)
25 if (fseek(fp
, o
, SEEK_END
) != 0) {
26 puts("fseek() error!\n");
31 static long negoffset(long a
, long romsz
)
34 if (a
& 0x80000000) /* the address in memory, and sizeof(long)
36 return a
- 0x100000000;
37 else /* the file offset */
44 int main(int argc
, char *argv
[])
47 long offset1
, offset2
;
49 if (argc
!= 4 && argc
!= 6)
52 fp
= fopen(argv
[1], "rb+");
54 puts("Error opening firmware image!");
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);
64 if (fw1
== NULL
|| fw2
== NULL
) {
65 puts("Error opening file!");
71 offset1
= strtoul(argv
[2], NULL
, 0);
72 offset2
= strtoul(argv
[3], NULL
, 0);
75 if ((offset1
& 0xff) || (offset2
& 0xff)) {
76 puts("The offsets must be aligned to 0x100");
82 romsz
= ftell(fp
) + 1;
83 printf("size of %s: 0x%lx\n", argv
[1], romsz
);
86 puts("The ROM size must be multiple of 0x100");
90 offset1
= negoffset(offset1
, romsz
);
91 offset2
= negoffset(offset2
, romsz
);
93 /* write two offsets to $s-0x100 */
96 os
= 0x1000000 + offset1
;
99 offs
[2] = 0xff - offs
[0];
100 offs
[3] = 0xff - offs
[1];
101 os
= 0x1000000 + offset2
;
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
]);
110 FseekEnd(fp
, -0x100);
111 printf("writing to 0x%lx\n", ftell(fp
));
112 fwrite(offs
, 1, 8, fp
);
115 /* write fw1 and fw2 */
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
);
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
);