static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector, int part_num, unsigned int disksig) { lbaint_t lba_start = ext_part_sector + le32_to_uint32(p->start4); lbaint_t lba_size = le32_to_uint32(p->size4); printf("%3d\t%-10llu\t%-10llu\t%08x-%02x\t%02x%s%s\n", part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, (is_extended(p->sys_ind) ? " Extd" : ""), (is_bootable(p) ? " Boot" : "")); }
/* Print a partition that is relative to its Extended partition table */ static void print_partition_extended(block_dev_desc_t *dev_desc, lbaint_t ext_part_sector, lbaint_t relative, int part_num, unsigned int disksig) { char buffer[DOS_PART_DEFAULT_SECTOR]; dos_partition_t *pt; int i; if (dev_desc->block_read(dev_desc, ext_part_sector, 1, buffer) != 1) { printf ("** Can't read partition table on %d:" LBAFU " **\n", dev_desc->dev, ext_part_sector); return; } i=test_block_type(buffer); if (i != DOS_MBR) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return; } if (!ext_part_sector) disksig = le32_to_uint32(&buffer[DOS_PART_DISKSIG_OFFSET]); /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* fdisk does not show the extended partitions that are not in the MBR */ if ((pt->sys_ind != 0) && (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { print_one_part(pt, ext_part_sector, part_num, disksig); } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { lbaint_t lba_start = le32_to_uint32(pt->start4) + relative; print_partition_extended(dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num, disksig); } } return; }
/* Print a partition that is relative to its Extended partition table */ static int get_partition_info_extended (block_dev_desc_t *dev_desc, lbaint_t ext_part_sector, lbaint_t relative, int part_num, int which_part, disk_partition_t *info, unsigned int disksig) { unsigned char buffer[DOS_PART_DEFAULT_SECTOR]; dos_partition_t *pt; int i; int dos_type; if (dev_desc->block_read(dev_desc, ext_part_sector, 1, buffer) != 1) { printf("** Can't read partition table on %d:" LBAFU " **\n", dev_desc->dev, ext_part_sector); return -1; } if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return -1; } // disksig is "Disk identifier" shown in "sudo fdisk -l" if (!ext_part_sector) disksig = le32_to_uint32(&buffer[DOS_PART_DISKSIG_OFFSET]); /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* * fdisk does not show the extended partitions that are not in the MBR */ if (((pt->boot_ind & ~0x80) == 0) && (pt->sys_ind != 0) && (part_num == which_part) && (is_extended(pt->sys_ind) == 0)) { info->blksz = DOS_PART_DEFAULT_SECTOR; info->start = (lbaint_t)(ext_part_sector + le32_to_uint32(pt->start4)); info->size = (lbaint_t)le32_to_uint32(pt->size4); switch(dev_desc->if_type) { case IF_TYPE_IDE: case IF_TYPE_SATA: case IF_TYPE_ATAPI: sprintf ((char *)info->name, "hd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_SCSI: sprintf ((char *)info->name, "sd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_USB: sprintf ((char *)info->name, "usbd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_DOC: sprintf ((char *)info->name, "docd%c%d", 'a' + dev_desc->dev, part_num); break; default: sprintf ((char *)info->name, "xx%c%d", 'a' + dev_desc->dev, part_num); break; } /* sprintf(info->type, "%d, pt->sys_ind); */ strcpy((char *)info->type, "U-Boot"); info->bootable = is_bootable(pt); sprintf(info->uuid, "%08x-%02x", disksig, part_num); return 0; } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { lbaint_t lba_start = le32_to_uint32(pt->start4) + relative; return get_partition_info_extended (dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num, which_part, info, disksig); } } /* Check for DOS PBR if no partition is found */ dos_type = test_block_type(buffer); if (dos_type == DOS_PBR) { info->start = 0; info->size = dev_desc->lba; info->blksz = DOS_PART_DEFAULT_SECTOR; info->bootable = 0; strcpy((char *)info->type, "U-Boot"); info->uuid[0] = 0; return 0; } return -1; }
nxt_firmware_t *load_nxt_firmware (const char *fn) { /* NXOS binary footer is: * magic (4 bytes, little-endian) * in-ram size (") * in-rom size (") * write address (") * boot address (") * samba flag (1 byte) * Total: 21 bytes */ const long bin_footer_len = 21; nxt_firmware_t *fw = NULL; uint32_t tmp; uint8_t buf[32]; /* must be big enough for footer */ long eof; FILE *fh; if ((fh = fopen (fn, "rb")) == NULL) { fprintf (stderr, "Error; unable to open %s: %s (%d)\n", fn, strerror (errno), errno); return NULL; } /* Find end of file minus footer length */ fseek (fh, -bin_footer_len, SEEK_END); eof = ftell (fh); if (fread (buf, 1, bin_footer_len, fh) < bin_footer_len) { fprintf (stderr, "Error; unable to read firmware footer from %s: %s (%d)\n", fn, strerror (errno), errno); goto errout; } tmp = le32_to_uint32 (&(buf[0])); if (tmp != 0xDEADBEEF) { fprintf (stderr, "Error; firmware magic does not match for %s: %08x\n", fn, tmp); goto errout; } /* Sanity check */ if ((eof < 0L) || (eof > (1024L * 1024L))) { fprintf (stderr, "Error; firmware file seems too big: %s\n", fn); goto errout; } fw = (nxt_firmware_t *) malloc (sizeof (nxt_firmware_t) + eof); fw->in_ram = le32_to_uint32 (&(buf[4])); fw->in_rom = le32_to_uint32 (&(buf[8])); fw->write_addr = le32_to_uint32 (&(buf[12])); fw->boot_addr = le32_to_uint32 (&(buf[16])); fw->len = eof; fw->data = ((uint8_t *)fw) + (sizeof (nxt_firmware_t)); fseek (fh, 0L, SEEK_SET); if (fread (fw->data, fw->len, 1, fh) != 1) { fprintf (stderr, "Error; unable to read firmware data from %s: %s (%d)\n", fn, strerror (errno), errno); goto errout; } fclose (fh); return fw; errout: if (fw != NULL) free (fw); fclose (fh); return NULL; }