+static int firmware_info(const char *input)
+{
+ struct flash_partition_entry pointers[MAX_PARTITIONS] = { };
+ struct flash_partition_entry *e;
+ FILE *fp;
+ int i;
+
+ fp = fopen(input, "r");
+
+ if (read_partition_table(fp, 0x1014, pointers, MAX_PARTITIONS, 0)) {
+ error(1, 0, "Error can not read the partition table (fwup-ptn)");
+ }
+
+ printf("Firmware image partitions:\n");
+ printf("%-8s %-8s %s\n", "base", "size", "name");
+ for (i = 0; i < MAX_PARTITIONS; i++) {
+ e = &pointers[i];
+
+ if (!e->name && !e->base && !e->size)
+ continue;
+
+ printf("%08x %08x %s\n", e->base, e->size, e->name ? e->name : "");
+ }
+
+ e = find_partition(pointers, MAX_PARTITIONS, "soft-version", NULL);
+ if (e) {
+ size_t data_len = e->size - sizeof(struct meta_header);
+ char *buf = malloc(data_len);
+ struct soft_version *s;
+ bool isstr;
+ int i;
+
+ if (!buf)
+ error(1, errno, "Failed to alloc buffer");
+
+ if (fseek(fp, 0x1014 + e->base + sizeof(struct meta_header), SEEK_SET))
+ error(1, errno, "Can not seek in the firmware");
+
+ if (fread(buf, data_len, 1, fp) != 1)
+ error(1, errno, "Can not read fwup-ptn data from the firmware");
+
+ /* Check for string ignoring padding character */
+ isstr = true;
+ for (i = 0; i < data_len - 1; i++) {
+ if (!isascii(buf[i])) {
+ isstr = false;
+ break;
+ }
+ }
+
+ printf("\n[Software version]\n");
+ if (isstr) {
+ fwrite(buf, data_len, 1, stdout);
+ putchar('\n');
+ } else if (data_len >= offsetof(struct soft_version, rev)) {
+ s = (struct soft_version *)buf;
+
+ printf("Version: %d.%d.%d\n", s->version_major, s->version_minor, s->version_patch);
+ printf("Date: %02x%02x-%02x-%02x\n", s->year_hi, s->year_lo, s->month, s->day);
+ } else {
+ printf("Failed to parse data\n");
+ }
+
+ free(buf);
+ }
+
+ e = find_partition(pointers, MAX_PARTITIONS, "support-list", NULL);
+ if (e) {
+ char buf[128];
+ size_t length;
+ size_t bytes;
+
+ if (fseek(fp, 0x1014 + e->base + sizeof(struct meta_header), SEEK_SET))
+ error(1, errno, "Can not seek in the firmware");
+
+ printf("\n[Support list]\n");
+ for (length = e->size - sizeof(struct meta_header); length; length -= bytes) {
+ bytes = fread(buf, 1, length > sizeof(buf) ? sizeof(buf) : length, fp);
+ if (bytes <= 0)
+ error(1, errno, "Can not read fwup-ptn data from the firmware");
+
+ puts(buf);
+ }
+ }
+
+ e = find_partition(pointers, MAX_PARTITIONS, "partition-table", NULL);
+ if (e) {
+ struct flash_partition_entry parts[MAX_PARTITIONS] = { };
+
+ if (read_partition_table(fp, 0x1014 + e->base + 4, parts, MAX_PARTITIONS, 1)) {
+ error(1, 0, "Error can not read the partition table (partition)");
+ }
+
+ printf("\n[Partition table]\n");
+ printf("%-8s %-8s %s\n", "base", "size", "name");
+ for (i = 0; i < MAX_PARTITIONS; i++) {
+ e = &parts[i];
+
+ if (!e->name && !e->base && !e->size)
+ continue;
+
+ printf("%08x %08x %s\n", e->base, e->size, e->name ? e->name : "");
+ }
+ }
+
+ fclose(fp);
+
+ return 0;
+}
+