2 * This file is part of the coreboot project.
4 * Copyright (C) 2017 Iru Cai <mytbk920423@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
19 static void usage(const char *s
)
21 printf("insert firmware blobs:\n\t"
22 "%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n\n",
24 printf("set addresses of firmware blobs only:\n\t"
25 "%s <rom file> <fw1 offset> <fw2 offset>\n\n",
28 "offset can be (example is put it to 0x7ffa00 when ROM is 8MB):\n"
29 "- file offset: 0x7ffa00\n"
30 "- distance to the end of file: -0x600\n"
31 "- the address when ROM is mapped to the end of memory: "
36 static void FseekEnd(FILE *fp
, long o
)
38 if (fseek(fp
, o
, SEEK_END
) != 0) {
39 puts("fseek() error!\n");
44 static long negoffset(long a
, long romsz
)
47 if (a
& 0x80000000) /* the address in memory, and sizeof(long)
49 return a
- 0x100000000;
50 else /* the file offset */
57 int main(int argc
, char *argv
[])
60 long offset1
, offset2
;
62 if (argc
!= 4 && argc
!= 6)
65 fp
= fopen(argv
[1], "rb+");
67 puts("Error opening firmware image!");
72 fw1
= fopen(argv
[2], "rb");
73 fw2
= fopen(argv
[3], "rb");
74 offset1
= strtoul(argv
[4], NULL
, 0);
75 offset2
= strtoul(argv
[5], NULL
, 0);
77 if (fw1
== NULL
|| fw2
== NULL
) {
78 puts("Error opening file!");
84 offset1
= strtoul(argv
[2], NULL
, 0);
85 offset2
= strtoul(argv
[3], NULL
, 0);
88 if ((offset1
& 0xff) || (offset2
& 0xff)) {
89 puts("The offsets must be aligned to 0x100");
95 romsz
= ftell(fp
) + 1;
96 printf("size of %s: 0x%lx\n", argv
[1], romsz
);
99 puts("The ROM size must be multiple of 0x100");
103 offset1
= negoffset(offset1
, romsz
);
104 offset2
= negoffset(offset2
, romsz
);
106 /* write two offsets to $s-0x100 */
109 os
= 0x1000000 + offset1
;
112 offs
[2] = 0xff - offs
[0];
113 offs
[3] = 0xff - offs
[1];
114 os
= 0x1000000 + offset2
;
117 offs
[6] = 0xff - offs
[4];
118 offs
[7] = 0xff - offs
[5];
119 for (size_t i
= 0; i
< 8; i
++)
120 printf("%02hhx ", offs
[i
]);
123 FseekEnd(fp
, -0x100);
124 printf("writing to 0x%lx\n", ftell(fp
));
125 fwrite(offs
, 1, 8, fp
);
128 /* write fw1 and fw2 */
130 FseekEnd(fp
, offset1
);
131 printf("writing to 0x%lx\n", ftell(fp
));
132 while (fread(&c
, 1, 1, fw1
) == 1)
133 fwrite(&c
, 1, 1, fp
);
135 FseekEnd(fp
, offset2
);
136 printf("writing to 0x%lx\n", ftell(fp
));
137 while (fread(&c
, 1, 1, fw2
) == 1)
138 fwrite(&c
, 1, 1, fp
);