diff mbox

[06/13] tools: added function to dump header

Message ID 1465979400-2964-7-git-send-email-adrian@lisas.de
State Accepted
Headers show

Commit Message

Adrian Reber June 15, 2016, 8:29 a.m. UTC
-d, --dump prints the information from the header, including
the total number of files in flash image as well as the CRC.

(cherry picked from commit 4d6f40c47d6b84cced86024a09ad68dfade45812)

Cherry picked from https://lisas.de/~adrian/slof/slof.git/

Signed-off-by: Adrian Reber <adrian@lisas.de>
---
 tools/sloffs.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/tools/sloffs.c b/tools/sloffs.c
index 34a10c5..eca06d8 100644
--- a/tools/sloffs.c
+++ b/tools/sloffs.c
@@ -29,9 +29,13 @@ 
 #ifdef _BIG_ENDIAN
 #define cpu_to_be64(x)  (x)
 #define be64_to_cpu(x)  (x)
+#define be16_to_cpu(x)  (x)
+#define be32_to_cpu(x)  (x)
 #else
 #define cpu_to_be64(x)  bswap_64(x)
 #define be64_to_cpu(x)  bswap_64(x)
+#define be16_to_cpu(x)  bswap_16(x)
+#define be32_to_cpu(x)  bswap_32(x)
 #endif
 
 
@@ -61,6 +65,95 @@  next_file(struct sloffs *sloffs)
 				 be64_to_cpu(sloffs->next));
 }
 
+static struct sloffs *
+find_file(const void *data, const char *name)
+{
+	struct sloffs *sloffs = (struct sloffs *)data;
+
+	for (;;) {
+		if (!strcmp((char *)&sloffs->name, name))
+			return sloffs;
+
+		if (be64_to_cpu(sloffs->next) == 0)
+			break;
+		sloffs = next_file(sloffs);
+	}
+	return NULL;
+}
+
+static void
+sloffs_dump(const void *data)
+{
+	struct stH *header;
+	struct sloffs *sloffs;
+	int i;
+	uint64_t crc;
+
+	/* find the "header" file with all the information about
+	 * the flash image */
+	sloffs = find_file(data, "header");
+	if (!sloffs) {
+		printf("sloffs file \"header\" not found. aborting...\n");
+		return;
+	}
+
+	header = (struct stH *)((unsigned char *)sloffs +
+				be64_to_cpu(sloffs->data));
+
+	if (memcmp(FLASHFS_MAGIC, header->magic, strlen(FLASHFS_MAGIC))) {
+		printf("sloffs magic not found. "
+		       "probably not a valid SLOF flash image. aborting...\n");
+		return;
+	}
+	printf("  Magic       : %s\n", header->magic);
+	printf("  Platform    : %s\n", header->platform_name);
+	printf("  Version     : %s\n", header->version);
+	printf("  Build Date  : ");
+	/* there is a bug in the date position;
+	 * it should be at header->date, but it is at (header->date + 2) */
+	if (be64_to_cpu(*(uint64_t *)header->date)) {
+		printf("%04x", be16_to_cpu(*(uint16_t *)(header->date + 2)));
+		printf("-%02x", *(uint8_t *)(header->date + 4));
+		printf("-%02x", *(uint8_t *)(header->date + 5));
+		printf(" %02x:", *(uint8_t *)(header->date + 6));
+		printf("%02x", *(uint8_t *)(header->date + 7));
+	} else {
+		printf("N/A");
+	}
+	printf("\n");
+	printf("  Modify Date : ");
+	if (be64_to_cpu(*(uint64_t *)header->mdate)) {
+		printf("%04x", be16_to_cpu(*(uint16_t *)(header->mdate + 2)));
+		printf("-%02x", *(uint8_t *)(header->mdate + 4));
+		printf("-%02x", *(uint8_t *)(header->mdate + 5));
+		printf(" %02x:", *(uint8_t *)(header->mdate + 6));
+		printf("%02x", *(uint8_t *)(header->mdate + 7));
+	} else {
+		printf("N/A");
+	}
+	printf("\n");
+	printf("  Image Length: %lld", be64_to_cpu(header->flashlen));
+	printf(" (0x%llx) bytes\n", be64_to_cpu(header->flashlen));
+	printf("  Revision    : %s\n", header->platform_revision);
+	crc = be64_to_cpu(header->ui64CRC);
+	printf("  Header CRC  : 0x%016llx\n", crc);
+	crc = be64_to_cpu(header->flashlen);
+	crc = *(uint64_t *)(unsigned char *)(data + crc - 8);
+	crc = be64_to_cpu(crc);
+	printf("  Image CRC   : 0x%016llx\n", crc);
+
+	/* count number of files */
+	sloffs = (struct sloffs *)data;
+	i = 0;
+	for (;;) {
+		i++;
+		if (be64_to_cpu(sloffs->next) == 0)
+			break;
+		sloffs = next_file(sloffs);
+	}
+	printf("  Files       : %d\n", i);
+}
+
 static void
 sloffs_list(const void *data)
 {
@@ -143,7 +236,7 @@  main(int argc, char *argv[])
 	};
 	const char *soption = "dhlv";
 	int c;
-	char mode;
+	char mode = 0;
 
 	for (;;) {
 		c = getopt_long(argc, argv, soption, loption, NULL);
@@ -156,6 +249,9 @@  main(int argc, char *argv[])
 		case 'v':
 			printf("sloffs (version %d)\n", VERSION);
 			exit(0);
+		case 'd':
+			mode = 'd';
+			break;
 		case 'h':
 		default:
 			usage();
@@ -179,6 +275,9 @@  main(int argc, char *argv[])
 	case 'l':
 		sloffs_list(file);
 		break;
+	case 'd':
+		sloffs_dump(file);
+		break;
 	}
 
 	munmap(file, stat.st_size);