bool storage_read_block(uint8_t *dest, uint32_t block) { //printf("RD %u\n", block); if (block == 0) { // fake the MBR so we can decide on our own partition table for (int i = 0; i < 446; i++) { dest[i] = 0; } build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, FLASH_PART1_NUM_BLOCKS); build_partition(dest + 462, 0, 0, 0, 0); build_partition(dest + 478, 0, 0, 0, 0); build_partition(dest + 494, 0, 0, 0, 0); dest[510] = 0x55; dest[511] = 0xaa; return true; } else { // non-MBR block, get data from flash memory, possibly via cache uint32_t flash_addr = convert_block_to_flash_addr(block); if (flash_addr == -1) { // bad block number return false; } uint8_t *src = flash_cache_get_addr_for_read(flash_addr); memcpy(dest, src, FLASH_BLOCK_SIZE); return true; } }
bool storage_read_block(uint8_t *dest, uint32_t block) { //printf("RD %u\n", block); if (block == 0) { // fake the MBR so we can decide on our own partition table for (int i = 0; i < 446; i++) { dest[i] = 0; } build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, FLASH_PART1_NUM_BLOCKS); build_partition(dest + 462, 0, 0, 0, 0); build_partition(dest + 478, 0, 0, 0, 0); build_partition(dest + 494, 0, 0, 0, 0); dest[510] = 0x55; dest[511] = 0xaa; return true; } else { #if USE_INTERNAL // non-MBR block, get data from flash memory, possibly via cache uint32_t flash_addr = convert_block_to_flash_addr(block); if (flash_addr == -1) { // bad block number return false; } uint8_t *src = flash_cache_get_addr_for_read(flash_addr); memcpy(dest, src, FLASH_BLOCK_SIZE); return true; #else // non-MBR block, get data from SPI flash if (block < FLASH_PART1_START_BLOCK || block >= FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { // bad block number return false; } // we must disable USB irqs to prevent MSC contention with SPI flash uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS); mp_spiflash_read((mp_spiflash_t*)&spiflash, (block - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE, FLASH_BLOCK_SIZE, dest); restore_irq_pri(basepri); return true; #endif } }
unsigned long fetch_partition_offset(int part, FILE* diskImage, bool verbose) { unsigned long offset; partition partitionTable[4]; /* Special value for uninitialized. */ if (part == -1) { return 0; } /* Check bounds. */ if (part < 0 || part > 3) { fprintf(stderr, "There are only 4 primary partitions on a disk (0-indexed)\n"); exit(1); } build_partition(partitionTable, diskImage, offset, verbose); if (partitionTable[part].type != MINIX_FILESYSTEM_TYPE) { fprintf(stderr, "Bad partition type. (%#x)\n", partitionTable[part].type); fprintf(stderr, "This doesn't appear to be a Minix partition.\n"); exit(1); } offset = partitionTable[part].lFirst * SECTOR_SIZE; if (verbose) { fprintf(stderr, "Calculated offset for the partition is %lu bytes.\n", offset); } return offset; }