MAINTAINERS: Add Maximilian Brune to RISC-V
[coreboot.git] / util / kbc1126 / kbc1126_ec_insert.c
blobd8ee0ca46b25c2cd0baf9240390a49de18ebd5f4
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <stdio.h>
4 #include <stdlib.h>
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",
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);
23 static void FseekEnd(FILE *fp, long o)
25 if (fseek(fp, o, SEEK_END) != 0) {
26 puts("fseek() error!\n");
27 exit(1);
31 static long negoffset(long a, long romsz)
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;
44 int main(int argc, char *argv[])
46 FILE *fp, *fw1, *fw2;
47 long offset1, offset2;
49 if (argc != 4 && argc != 6)
50 usage(argv[0]);
52 fp = fopen(argv[1], "rb+");
53 if (fp == NULL) {
54 puts("Error opening firmware image!");
55 exit(1);
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);
64 if (fw1 == NULL || fw2 == NULL) {
65 puts("Error opening file!");
66 exit(1);
68 } else {
69 fw1 = NULL;
70 fw2 = NULL;
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");
77 exit(1);
80 long romsz;
81 FseekEnd(fp, -1);
82 romsz = ftell(fp) + 1;
83 printf("size of %s: 0x%lx\n", argv[1], romsz);
85 if (romsz & 0xff) {
86 puts("The ROM size must be multiple of 0x100");
87 exit(1);
90 offset1 = negoffset(offset1, romsz);
91 offset2 = negoffset(offset2, romsz);
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];
106 for (size_t i = 0; i < 8; i++)
107 printf("%02hhx ", offs[i]);
109 puts("");
110 FseekEnd(fp, -0x100);
111 printf("writing to 0x%lx\n", ftell(fp));
112 fwrite(offs, 1, 8, fp);
114 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);
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);
127 fclose(fw1);
128 fclose(fw2);
131 fclose(fp);
132 return 0;