From f9150971f113dd6a6a8bf5023ba8ac030f2d498c Mon Sep 17 00:00:00 2001 From: Kasper Lund Date: Fri, 4 Nov 2022 14:37:45 +0100 Subject: [PATCH] Implement FlashRegistry for Windows hosts (#1182) * Implement FlashRegistry for Windows hosts * Fix typo * Another typo. Coffee time. * Test started passing * Apply suggestions from code review Co-authored-by: Florian Loitsch Co-authored-by: Florian Loitsch --- src/flash_registry_win.cc | 53 +++++++++++++++++++++++++++++++++------ tests/fail.cmake | 1 - 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/flash_registry_win.cc b/src/flash_registry_win.cc index 7906ee25a..f9c58dd99 100644 --- a/src/flash_registry_win.cc +++ b/src/flash_registry_win.cc @@ -22,40 +22,79 @@ namespace toit { +static const int ALLOCATION_SIZE = 2 * MB; +static const int ENCRYPTION_WRITE_SIZE = 16; + +// An aligned (FLASH_BASED_SIZE) view into the allocations_malloced. const char* FlashRegistry::allocations_memory_ = null; +static void* allocations_malloced = null; void FlashRegistry::set_up() { + ASSERT(allocations_malloced == null); + ASSERT(allocations_memory() == null); + + allocations_malloced = malloc(ALLOCATION_SIZE + FLASH_PAGE_SIZE); + // Align the memory to FLASH_PAGE_SIZE. + // Note that we allocated FLASH_PAGE_SIZE more than necessary, so we could do this. + allocations_memory_ = Utils::round_up(unvoid_cast(allocations_malloced), FLASH_PAGE_SIZE); } void FlashRegistry::tear_down() { + allocations_memory_ = null; + free(allocations_malloced); + allocations_malloced = null; } bool FlashRegistry::is_allocations_set_up() { - return true; + return allocations_memory_ != null; } void FlashRegistry::flush() { - UNIMPLEMENTED(); + // No flushing necessary. } int FlashRegistry::allocations_size() { - return 0; + return ALLOCATION_SIZE; } int FlashRegistry::erase_chunk(int offset, int size) { - UNIMPLEMENTED(); + ASSERT(Utils::is_aligned(offset, FLASH_PAGE_SIZE)); + size = Utils::round_up(size, FLASH_PAGE_SIZE); + memset(memory(offset, size), 0xff, size); + return size; +} + +bool is_erased(void* memory, int offset, int size) { + char* dest = reinterpret_cast(memory); + for (int i = 0; i < size; i++) { + uint8 value = dest[i]; + if (value != 0xff) { + return false; + } + } + return true; } bool FlashRegistry::write_chunk(const void* chunk, int offset, int size) { - UNIMPLEMENTED(); + void* dest = memory(offset, size); + ASSERT(Utils::is_aligned(offset, ENCRYPTION_WRITE_SIZE)); + ASSERT(Utils::is_aligned(size, ENCRYPTION_WRITE_SIZE)); + ASSERT(is_erased(dest, 0, size)); + memcpy(dest, chunk, size); + return true; } int FlashRegistry::offset(const void* cursor) { - UNIMPLEMENTED(); + ASSERT(allocations_memory() != null); + word offset = Utils::address_distance(allocations_memory(), cursor); + ASSERT(0 <= offset && offset < allocations_size()); + return offset; } bool FlashRegistry::erase_flash_registry() { - UNIMPLEMENTED(); + ASSERT(allocations_memory() != null); + FlashRegistry::erase_chunk(0, allocations_size()); + return true; } } // namespace toit diff --git a/tests/fail.cmake b/tests/fail.cmake index d1e638d00..072a88152 100644 --- a/tests/fail.cmake +++ b/tests/fail.cmake @@ -26,7 +26,6 @@ list(APPEND TOIT_SKIP_TESTS if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "MSYS") list(APPEND TOIT_FAILING_TESTS - tests/containers_test.toit tests/time_test.toit tests/zlib_test.toit tests/class_field_limit_test_compiler.toit