Skip to content

Commit

Permalink
Implement FlashRegistry for Windows hosts (#1182)
Browse files Browse the repository at this point in the history
* Implement FlashRegistry for Windows hosts

* Fix typo

* Another typo. Coffee time.

* Test started passing

* Apply suggestions from code review

Co-authored-by: Florian Loitsch <[email protected]>

Co-authored-by: Florian Loitsch <[email protected]>
  • Loading branch information
kasperl and floitsch authored Nov 4, 2022
1 parent 3d2139b commit f915097
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
53 changes: 46 additions & 7 deletions src/flash_registry_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(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<char*>(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
Expand Down
1 change: 0 additions & 1 deletion tests/fail.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f915097

Please sign in to comment.