[coreboot-gerrit] Change in coreboot[master]: util/kbc1126: refactor kbc1126_ec_insert

Iru Cai (Code Review) gerrit at coreboot.org
Thu Jul 6 11:36:21 CEST 2017


Iru Cai has uploaded this change for review. ( https://review.coreboot.org/20474


Change subject: util/kbc1126: refactor kbc1126_ec_insert
......................................................................

util/kbc1126: refactor kbc1126_ec_insert

Add support for setting addresses of blobs only, and using address in
memory when ROM is mapped, so that we can now use cbfstool to add
these blobs and use this tool to set the addresses only.

Change-Id: I053c55253eee00558071d15923e3eb17b1e8410c
---
M util/kbc1126/kbc1126_ec_insert.c
1 file changed, 62 insertions(+), 28 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/74/20474/1

diff --git a/util/kbc1126/kbc1126_ec_insert.c b/util/kbc1126/kbc1126_ec_insert.c
index 6c19c0e..6e85f39 100644
--- a/util/kbc1126/kbc1126_ec_insert.c
+++ b/util/kbc1126/kbc1126_ec_insert.c
@@ -18,7 +18,14 @@
 
 static void usage(const char *s)
 {
-	printf("%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n", s);
+	printf("insert firmware blobs:\n\t"
+			"%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n\n", s);
+	printf("set addresses of firmware blobs only:\n\t"
+			"%s <rom file> <fw1 offset> <fw2 offset>\n\n", s);
+	printf("offset can be (example is put it to 0x7ffa00 when ROM is 8MB):\n"
+			"- file offset: 0x7ffa00\n"
+			"- distance to the end of file: -0x600\n"
+			"- the address when ROM is mapped to the end of memory: 0xfffffa00\n");
 	exit(1);
 }
 
@@ -30,20 +37,47 @@
 	}
 }
 
+static long negoffset(long a, long romsz)
+{
+	if (a>0) {
+		if (a & 0x80000000) /* the address in memory, and sizeof(long) is 8 */
+			return a - 0x100000000;
+		else /* the file offset */
+			return a - romsz;
+	} else {
+		return a;
+	}
+}
+
 int main(int argc, char *argv[])
 {
-	if (argc < 6)
+	FILE *fp, *fw1, *fw2;
+	long offset1, offset2;
+
+	if (argc != 4 && 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!");
+	fp = fopen(argv[1], "rb+");
+	if (fp == NULL) {
+		puts("Error opening firmware image!");
 		exit(1);
+	}
+
+	if (argc == 6) {
+		fw1 = fopen(argv[2], "rb");
+		fw2 = fopen(argv[3], "rb");
+		offset1 = strtoul(argv[4], NULL, 0);
+		offset2 = strtoul(argv[5], NULL, 0);
+
+		if (fw1 == NULL || fw2 == NULL) {
+			puts("Error opening file!");
+			exit(1);
+		}
+	} else {
+		fw1 = NULL;
+		fw2 = NULL;
+		offset1 = strtoul(argv[2], NULL, 0);
+		offset2 = strtoul(argv[3], NULL, 0);
 	}
 
 	if ((offset1 & 0xff) || (offset2 & 0xff)) {
@@ -61,11 +95,8 @@
 		exit(1);
 	}
 
-	if (offset1 > 0)
-		offset1 = offset1 - romsz;
-
-	if (offset2 > 0)
-		offset2 = offset2 - romsz;
+	offset1 = negoffset(offset1, romsz);
+	offset2 = negoffset(offset2, romsz);
 
 	/* write two offsets to $s-0x100 */
 	char offs[8];
@@ -88,21 +119,24 @@
 	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);
+	if (argc == 6) {
+		/* 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(fw1);
+		fclose(fw2);
 	}
 
 	fclose(fp);
-	fclose(fw1);
-	fclose(fw2);
 	return 0;
 }

-- 
To view, visit https://review.coreboot.org/20474
To unsubscribe, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I053c55253eee00558071d15923e3eb17b1e8410c
Gerrit-Change-Number: 20474
Gerrit-PatchSet: 1
Gerrit-Owner: Iru Cai <mytbk920423 at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20170706/ca0414d2/attachment-0001.html>


More information about the coreboot-gerrit mailing list