summaryrefslogtreecommitdiff
path: root/util/kbc1126/kbc1126_ec_insert.c
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2017-03-26 12:05:32 +0800
committerPatrick Georgi <pgeorgi@google.com>2017-05-11 16:48:40 +0200
commit5fd00ce71a1bfd5cd36f9d42ab657a8e7d6c54f2 (patch)
tree0b57a49a7095eb93d15a8aecf450bce2f7649c5a /util/kbc1126/kbc1126_ec_insert.c
parent5f9fe7232a24e4abf88b6b642f4df462142cfd85 (diff)
util: Add tools for dumping and inserting KBC1126 firmware images.
Change-Id: Ic521b177b9602ff042312cccaaa89371db7c5855 Signed-off-by: Iru Cai <mytbk920423@gmail.com> Reviewed-on: https://review.coreboot.org/19071 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Diffstat (limited to 'util/kbc1126/kbc1126_ec_insert.c')
-rw-r--r--util/kbc1126/kbc1126_ec_insert.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/util/kbc1126/kbc1126_ec_insert.c b/util/kbc1126/kbc1126_ec_insert.c
new file mode 100644
index 0000000000..6c19c0e97f
--- /dev/null
+++ b/util/kbc1126/kbc1126_ec_insert.c
@@ -0,0 +1,108 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2017 Iru Cai <mytbk920423@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void usage(const char *s)
+{
+ printf("%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n", s);
+ exit(1);
+}
+
+static void FseekEnd(FILE *fp, long o)
+{
+ if (fseek(fp, o, SEEK_END) != 0) {
+ puts("fseek() error!\n");
+ exit(1);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc < 6)
+ usage(argv[0]);
+
+ FILE *fp = fopen(argv[1], "rb+");
+ FILE *fw1 = fopen(argv[2], "rb");
+ FILE *fw2 = fopen(argv[3], "rb");
+ long offset1 = strtol(argv[4], NULL, 0);
+ long offset2 = strtol(argv[5], NULL, 0);
+
+ if (fp == NULL || fw1 == NULL || fw2 == NULL) {
+ puts("Error opening file!");
+ exit(1);
+ }
+
+ if ((offset1 & 0xff) || (offset2 & 0xff)) {
+ puts("The offsets must be aligned to 0x100");
+ exit(1);
+ }
+
+ long romsz;
+ FseekEnd(fp, -1);
+ romsz = ftell(fp) + 1;
+ printf("size of %s: 0x%lx\n", argv[1], romsz);
+
+ if (romsz & 0xff) {
+ puts("The ROM size must be multiple of 0x100");
+ exit(1);
+ }
+
+ if (offset1 > 0)
+ offset1 = offset1 - romsz;
+
+ if (offset2 > 0)
+ offset2 = offset2 - romsz;
+
+ /* write two offsets to $s-0x100 */
+ char offs[8];
+ long os;
+ os = 0x1000000 + offset1;
+ offs[0] = os >> 16;
+ offs[1] = os >> 8;
+ offs[2] = 0xff - offs[0];
+ offs[3] = 0xff - offs[1];
+ os = 0x1000000 + offset2;
+ offs[4] = os >> 16;
+ offs[5] = os >> 8;
+ offs[6] = 0xff - offs[4];
+ offs[7] = 0xff - offs[5];
+ for (size_t i = 0; i < 8; i++) {
+ printf("%02hhx ", offs[i]);
+ }
+ puts("");
+ FseekEnd(fp, -0x100);
+ printf("writing to 0x%lx\n", ftell(fp));
+ fwrite(offs, 1, 8, fp);
+
+ /* write fw1 and fw2 */
+ char c;
+ FseekEnd(fp, offset1);
+ printf("writing to 0x%lx\n", ftell(fp));
+ while (fread(&c, 1, 1, fw1) == 1) {
+ fwrite(&c, 1, 1, fp);
+ }
+ FseekEnd(fp, offset2);
+ printf("writing to 0x%lx\n", ftell(fp));
+ while (fread(&c, 1, 1, fw2) == 1) {
+ fwrite(&c, 1, 1, fp);
+ }
+
+ fclose(fp);
+ fclose(fw1);
+ fclose(fw2);
+ return 0;
+}