northbridge: Remove unneeded include <pc80/mc146818rtc.h>
[coreboot.git] / util / kbc1126 / kbc1126_ec_insert.c
blob64d1295c967c834b74576ffa1ada03e1149be029
1 /*
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.
16 #include <stdio.h>
17 #include <stdlib.h>
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",
23 s);
24 printf("set addresses of firmware blobs only:\n\t"
25 "%s <rom file> <fw1 offset> <fw2 offset>\n\n",
26 s);
27 printf(
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: "
32 "0xfffffa00\n");
33 exit(1);
36 static void FseekEnd(FILE *fp, long o)
38 if (fseek(fp, o, SEEK_END) != 0) {
39 puts("fseek() error!\n");
40 exit(1);
44 static long negoffset(long a, long romsz)
46 if (a > 0) {
47 if (a & 0x80000000) /* the address in memory, and sizeof(long)
48 is 8 */
49 return a - 0x100000000;
50 else /* the file offset */
51 return a - romsz;
52 } else {
53 return a;
57 int main(int argc, char *argv[])
59 FILE *fp, *fw1, *fw2;
60 long offset1, offset2;
62 if (argc != 4 && argc != 6)
63 usage(argv[0]);
65 fp = fopen(argv[1], "rb+");
66 if (fp == NULL) {
67 puts("Error opening firmware image!");
68 exit(1);
71 if (argc == 6) {
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!");
79 exit(1);
81 } else {
82 fw1 = NULL;
83 fw2 = NULL;
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");
90 exit(1);
93 long romsz;
94 FseekEnd(fp, -1);
95 romsz = ftell(fp) + 1;
96 printf("size of %s: 0x%lx\n", argv[1], romsz);
98 if (romsz & 0xff) {
99 puts("The ROM size must be multiple of 0x100");
100 exit(1);
103 offset1 = negoffset(offset1, romsz);
104 offset2 = negoffset(offset2, romsz);
106 /* write two offsets to $s-0x100 */
107 char offs[8];
108 long os;
109 os = 0x1000000 + offset1;
110 offs[0] = os >> 16;
111 offs[1] = os >> 8;
112 offs[2] = 0xff - offs[0];
113 offs[3] = 0xff - offs[1];
114 os = 0x1000000 + offset2;
115 offs[4] = os >> 16;
116 offs[5] = os >> 8;
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]);
122 puts("");
123 FseekEnd(fp, -0x100);
124 printf("writing to 0x%lx\n", ftell(fp));
125 fwrite(offs, 1, 8, fp);
127 if (argc == 6) {
128 /* write fw1 and fw2 */
129 char c;
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);
140 fclose(fw1);
141 fclose(fw2);
144 fclose(fp);
145 return 0;