|
| 1 | +#include <BlockDevice.h> |
| 2 | + |
| 3 | +struct __attribute__((packed)) mbrEntry { |
| 4 | + uint8_t status; |
| 5 | + uint8_t chsStart[3]; |
| 6 | + uint8_t type; |
| 7 | + uint8_t chsStop[3]; |
| 8 | + uint32_t lbaOffset; |
| 9 | + uint32_t lbaSize; |
| 10 | +}; |
| 11 | + |
| 12 | +struct __attribute__((packed)) mbrTable { |
| 13 | + mbrEntry entries[4]; |
| 14 | + uint8_t signature[2]; |
| 15 | +}; |
| 16 | + |
| 17 | +using namespace mbed; |
| 18 | + |
| 19 | +unsigned long allocatedSpace {}; |
| 20 | + |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + Serial.begin(115200); |
| 24 | + for (const auto timeout = millis() + 2500; !Serial && millis() < timeout; delay(250)) |
| 25 | + ; |
| 26 | + |
| 27 | + auto bd = BlockDevice::get_default_instance(); |
| 28 | + auto ret = bd->init(); |
| 29 | + if (ret) { |
| 30 | + Serial.println("ERROR! Unable to read the Block Device."); |
| 31 | + while (true) |
| 32 | + ; |
| 33 | + } |
| 34 | + |
| 35 | + // Allocate smallest buffer necessary to write MBR |
| 36 | + auto buffer_size = std::max<uint32_t>(bd->get_program_size(), sizeof(mbrTable)); |
| 37 | + |
| 38 | + // Prevent alignment issues |
| 39 | + if (buffer_size % bd->get_program_size() != 0) { |
| 40 | + buffer_size += bd->get_program_size() - (buffer_size % bd->get_program_size()); |
| 41 | + } |
| 42 | + |
| 43 | + auto buffer = new uint8_t[buffer_size]; |
| 44 | + |
| 45 | + // Check for existing MBR |
| 46 | + ret = bd->read(buffer, 512 - buffer_size, buffer_size); |
| 47 | + if (ret) { |
| 48 | + Serial.println("ERROR! Unable to read the Master Boot Record"); |
| 49 | + |
| 50 | + delete[] buffer; |
| 51 | + while (true) |
| 52 | + ; |
| 53 | + } |
| 54 | + |
| 55 | + auto table_start_offset = buffer_size - sizeof(mbrTable); |
| 56 | + auto table = reinterpret_cast<mbrTable*>(&buffer[table_start_offset]); |
| 57 | + |
| 58 | + Serial.println(); |
| 59 | + Serial.print("Looking for Partitions on the Flash Memory... "); |
| 60 | + |
| 61 | + if (table->signature[0] != 0x55 || table->signature[1] != 0xAA) { |
| 62 | + Serial.println("MBR Not Found"); |
| 63 | + Serial.println("Flash Memory doesn't have partitions."); |
| 64 | + } else { |
| 65 | + |
| 66 | + Serial.println("MBR Found"); |
| 67 | + Serial.print("Boot Signature: 0x"); |
| 68 | + Serial.print(table->signature[0], HEX); |
| 69 | + Serial.println(table->signature[1], HEX); |
| 70 | + |
| 71 | + Serial.println(); |
| 72 | + Serial.println("Printing Partitions Table and Info..."); |
| 73 | + |
| 74 | + auto part { 1u }; |
| 75 | + for (auto const& entry : table->entries) { |
| 76 | + Serial.println("================================"); |
| 77 | + Serial.print("Partition: "); |
| 78 | + Serial.println(part++); |
| 79 | + |
| 80 | + Serial.print("Bootable: "); |
| 81 | + Serial.println(entry.status == 0 ? "No" : "Yes"); |
| 82 | + |
| 83 | + Serial.print("Type: 0x"); |
| 84 | + if (entry.type < 0x10) |
| 85 | + Serial.print(0); |
| 86 | + Serial.println(entry.type, HEX); |
| 87 | + |
| 88 | + if (entry.type == 0x00) |
| 89 | + continue; |
| 90 | + |
| 91 | + Serial.print("Size [KBytes]: "); |
| 92 | + Serial.println((entry.lbaSize * 4096) >> 10); |
| 93 | + |
| 94 | + allocatedSpace += entry.lbaSize * 4096; |
| 95 | + |
| 96 | + Serial.print("Start [C/H/S]: "); |
| 97 | + Serial.print(entry.chsStart[0]); |
| 98 | + Serial.print("/"); |
| 99 | + Serial.print(entry.chsStart[1]); |
| 100 | + Serial.print("/"); |
| 101 | + Serial.println(entry.chsStart[2]); |
| 102 | + |
| 103 | + Serial.print("Stop [C/H/S]: "); |
| 104 | + Serial.print(entry.chsStop[0]); |
| 105 | + Serial.print("/"); |
| 106 | + Serial.print(entry.chsStop[1]); |
| 107 | + Serial.print("/"); |
| 108 | + Serial.println(entry.chsStop[2]); |
| 109 | + |
| 110 | + Serial.println(); |
| 111 | + } |
| 112 | + |
| 113 | + Serial.println(); |
| 114 | + Serial.println("No more partitions are present."); |
| 115 | + } |
| 116 | + |
| 117 | + Serial.println(); |
| 118 | + Serial.print("Total Space [KBytes]: "); |
| 119 | + Serial.println(bd->size() >> 10); |
| 120 | + Serial.print("Allocated Space [KBytes]: "); |
| 121 | + Serial.println(allocatedSpace >> 10); |
| 122 | + Serial.print("Unallocated Space [KBytes]: "); |
| 123 | + Serial.println((bd->size() - allocatedSpace) >> 10); |
| 124 | +} |
| 125 | + |
| 126 | +void loop() |
| 127 | +{ |
| 128 | + delay(10000); |
| 129 | +} |
0 commit comments