|
| 1 | +#include <bootloader/slot_exam_mode.h> |
| 2 | +#include <assert.h> |
| 3 | +#include <ion.h> |
| 4 | +#include <ion/src/device/shared/drivers/flash.h> |
| 5 | + |
| 6 | +/* |
| 7 | + * Taken from https://github.com/UpsilonNumworks/Upsilon/pull/205 |
| 8 | + * |
| 9 | + * Thanks to the Upsilon team, specifically: |
| 10 | + * - devdl11 |
| 11 | + * - Yaya-Cout |
| 12 | + * - Lauryy06 |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Bootloader { |
| 16 | +namespace ExamMode { |
| 17 | + |
| 18 | +/* The exam mode is written in flash so that it is resilient to resets. |
| 19 | + * We erase the dedicated flash sector (all bits written to 1) and, upon |
| 20 | + * deactivating or activating standard, nosym or Dutch exam mode we write one, two or tree |
| 21 | + * bits to 0. To determine in which exam mode we are, we count the number of |
| 22 | + * leading 0 bits. If it is equal to: |
| 23 | + * - 0[3]: the exam mode is off; |
| 24 | + * - 1[3]: the standard exam mode is activated; |
| 25 | + * - 2[3]: the NoSym exam mode is activated; |
| 26 | + * - 3[3]: the Dutch exam mode is activated; |
| 27 | + * - 4[3]: the NoSymNoText exam mode is activated. */ |
| 28 | + |
| 29 | +/* significantExamModeAddress returns the first uint32_t * in the exam mode |
| 30 | + * flash sector that does not point to 0. If this flash sector has only 0s or |
| 31 | + * if it has only one 1, it is erased (to 1) and significantExamModeAddress |
| 32 | + * returns the start of the sector. */ |
| 33 | + |
| 34 | +constexpr static size_t numberOfBitsInByte = 8; |
| 35 | + |
| 36 | +// if i = 0b000011101, firstOneBitInByte(i) returns 5 |
| 37 | +size_t numberOfBitsAfterLeadingZeroes(int i) { |
| 38 | + int minShift = 0; |
| 39 | + int maxShift = numberOfBitsInByte; |
| 40 | + while (maxShift > minShift+1) { |
| 41 | + int shift = (minShift + maxShift)/2; |
| 42 | + int shifted = i >> shift; |
| 43 | + if (shifted == 0) { |
| 44 | + maxShift = shift; |
| 45 | + } else { |
| 46 | + minShift = shift; |
| 47 | + } |
| 48 | + } |
| 49 | + return maxShift; |
| 50 | +} |
| 51 | + |
| 52 | +uint8_t * SignificantSlotAExamModeAddress(bool newVersion) { |
| 53 | + uint32_t * persitence_start_32 = (uint32_t *)SlotsExamMode::getSlotAStartExamAddress(newVersion); |
| 54 | + uint32_t * persitence_end_32 = (uint32_t *)SlotsExamMode::getSlotAEndExamAddress(newVersion); |
| 55 | + if (!newVersion) { |
| 56 | + assert((persitence_end_32 - persitence_start_32) % 4 == 0); |
| 57 | + while (persitence_start_32 < persitence_end_32 && *persitence_start_32 == 0x0) { |
| 58 | + // Scan by groups of 32 bits to reach first non-zero bit |
| 59 | + persitence_start_32++; |
| 60 | + } |
| 61 | + uint8_t * persitence_start_8 = (uint8_t *)persitence_start_32; |
| 62 | + uint8_t * persitence_end_8 = (uint8_t *)persitence_end_32; |
| 63 | + while (persitence_start_8 < persitence_end_8 && *persitence_start_8 == 0x0) { |
| 64 | + // Scan by groups of 8 bits to reach first non-zero bit |
| 65 | + persitence_start_8++; |
| 66 | + } |
| 67 | + if (persitence_start_8 == persitence_end_8 |
| 68 | + // we can't toggle from 0[3] to 2[3] when there is only one 1 bit in the whole sector |
| 69 | + || (persitence_start_8 + 1 == persitence_end_8 && *persitence_start_8 == 1)) { |
| 70 | + assert(Ion::Device::Flash::SectorAtAddress(SlotsExamMode::getSlotAStartExamAddress(newVersion)) >= 0); |
| 71 | + Ion::Device::Flash::EraseSector(Ion::Device::Flash::SectorAtAddress(SlotsExamMode::getSlotAStartExamAddress(newVersion))); |
| 72 | + return (uint8_t *)SlotsExamMode::getSlotAStartExamAddress(newVersion); |
| 73 | + } |
| 74 | + |
| 75 | + return persitence_start_8; |
| 76 | + } else { |
| 77 | + persitence_end_32 = persitence_end_32 - 1; |
| 78 | + while (persitence_end_32 - (uint32_t)(10 / 8) >= persitence_end_32 && *persitence_end_32 == 0xFFFFFFFF) { |
| 79 | + persitence_end_32 -= 1; |
| 80 | + } |
| 81 | + uint8_t * start = reinterpret_cast<uint8_t *>(persitence_start_32); |
| 82 | + uint8_t * end = reinterpret_cast<uint8_t *>(persitence_end_32 + 1) - 1; |
| 83 | + while (end >= start + 2 && *end == 0xFF) { |
| 84 | + end -= 1; |
| 85 | + } |
| 86 | + return end - 1; |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +uint8_t * SignificantSlotBExamModeAddress(bool newVersion) { |
| 92 | + uint32_t * persitence_start_32 = (uint32_t *)SlotsExamMode::getSlotBStartExamAddress(newVersion); |
| 93 | + uint32_t * persitence_end_32 = (uint32_t *)SlotsExamMode::getSlotBEndExamAddress(newVersion); |
| 94 | + if (!newVersion) { |
| 95 | + assert((persitence_end_32 - persitence_start_32) % 4 == 0); |
| 96 | + while (persitence_start_32 < persitence_end_32 && *persitence_start_32 == 0x0) { |
| 97 | + // Scan by groups of 32 bits to reach first non-zero bit |
| 98 | + persitence_start_32++; |
| 99 | + } |
| 100 | + uint8_t * persitence_start_8 = (uint8_t *)persitence_start_32; |
| 101 | + uint8_t * persitence_end_8 = (uint8_t *)persitence_end_32; |
| 102 | + while (persitence_start_8 < persitence_end_8 && *persitence_start_8 == 0x0) { |
| 103 | + // Scan by groups of 8 bits to reach first non-zero bit |
| 104 | + persitence_start_8++; |
| 105 | + } |
| 106 | + if (persitence_start_8 == persitence_end_8 |
| 107 | + // we can't toggle from 0[3] to 2[3] when there is only one 1 bit in the whole sector |
| 108 | + || (persitence_start_8 + 1 == persitence_end_8 && *persitence_start_8 == 1)) { |
| 109 | + assert(Ion::Device::Flash::SectorAtAddress(SlotsExamMode::getSlotBStartExamAddress(newVersion)) >= 0); |
| 110 | + Ion::Device::Flash::EraseSector(Ion::Device::Flash::SectorAtAddress(SlotsExamMode::getSlotBStartExamAddress(newVersion))); |
| 111 | + return (uint8_t *)SlotsExamMode::getSlotBStartExamAddress(newVersion); |
| 112 | + } |
| 113 | + |
| 114 | + return persitence_start_8; |
| 115 | + } else { |
| 116 | + persitence_end_32 = persitence_end_32 - 1; |
| 117 | + while (persitence_end_32 - (uint32_t)(10 / 8) >= persitence_end_32 && *persitence_end_32 == 0xFFFFFFFF) { |
| 118 | + persitence_end_32 -= 1; |
| 119 | + } |
| 120 | + uint8_t * start = reinterpret_cast<uint8_t *>(persitence_start_32); |
| 121 | + uint8_t * end = reinterpret_cast<uint8_t *>(persitence_end_32 + 1) - 1; |
| 122 | + while (end >= start + 2 && *end == 0xFF) { |
| 123 | + end -= 1; |
| 124 | + } |
| 125 | + return end - 1; |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +uint8_t SlotsExamMode::FetchSlotAExamMode(bool newVersion) { |
| 131 | + uint8_t * readingAddress = SignificantSlotAExamModeAddress(newVersion); |
| 132 | + if (!newVersion) { |
| 133 | + // Count the number of 0[3] before reading address |
| 134 | + uint32_t nbOfZerosBefore = ((readingAddress - (uint8_t *)getSlotAStartExamAddress(newVersion)) * numberOfBitsInByte) % 4; |
| 135 | + // Count the number of 0[3] at reading address |
| 136 | + size_t numberOfLeading0 = (numberOfBitsInByte - numberOfBitsAfterLeadingZeroes(*readingAddress)) % 4; |
| 137 | + return (nbOfZerosBefore + numberOfLeading0) % 4; |
| 138 | + } else { |
| 139 | + return *((uint8_t *)readingAddress); |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +uint8_t SlotsExamMode::FetchSlotBExamMode(bool newVersion) { |
| 145 | + uint8_t * readingAddress = SignificantSlotBExamModeAddress(newVersion); |
| 146 | + if (!newVersion) { |
| 147 | + // Count the number of 0[3] before reading address |
| 148 | + uint32_t nbOfZerosBefore = ((readingAddress - (uint8_t *)getSlotBStartExamAddress(newVersion)) * numberOfBitsInByte) % 4; |
| 149 | + // Count the number of 0[3] at reading address |
| 150 | + size_t numberOfLeading0 = (numberOfBitsInByte - numberOfBitsAfterLeadingZeroes(*readingAddress)) % 4; |
| 151 | + return (nbOfZerosBefore + numberOfLeading0) % 4; |
| 152 | + } else { |
| 153 | + return *((uint8_t *)readingAddress); |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +uint32_t SlotsExamMode::getSlotAStartExamAddress(bool newVersion) { |
| 159 | + return newVersion ? SlotAExamModeBufferStartNewVersions : SlotAExamModeBufferStartOldVersions; |
| 160 | +} |
| 161 | + |
| 162 | +uint32_t SlotsExamMode::getSlotAEndExamAddress(bool newVersion) { |
| 163 | + return newVersion ? SlotAExamModeBufferEndNewVersions : SlotAExamModeBufferEndOldVersions; |
| 164 | +} |
| 165 | + |
| 166 | +uint32_t SlotsExamMode::getSlotBStartExamAddress(bool newVersion) { |
| 167 | + return newVersion ? SlotBExamModeBufferStartNewVersions : SlotBExamModeBufferStartOldVersions; |
| 168 | +} |
| 169 | + |
| 170 | +uint32_t SlotsExamMode::getSlotBEndExamAddress(bool newVersion) { |
| 171 | + return newVersion ? SlotBExamModeBufferEndNewVersions : SlotBExamModeBufferEndOldVersions; |
| 172 | +} |
| 173 | + |
| 174 | +} |
| 175 | +} |
0 commit comments