From 0aa41b86ca7a551cf7f63308ed1ccad090c9a1fe Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 3 Aug 2023 11:55:29 -0700 Subject: [PATCH] Fixes for handling 64-bit assign mmap virtual addresses. --- hal/sim.c | 18 ++++++++---------- include/hal.h | 4 ++-- src/libwolfboot.c | 12 ++++++------ tools/scripts/qemu64/sign_linux.sh | 4 ++-- tools/test.mk | 8 ++++---- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/hal/sim.c b/hal/sim.c index 75e1334a6..fef7efbd1 100644 --- a/hal/sim.c +++ b/hal/sim.c @@ -77,6 +77,8 @@ static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address) if (mmaped_addr == MAP_FAILED) return -1; + printf("Simulator assigned %s to base %p\n", path, mmaped_addr); + *ret_address = mmaped_addr; close(fd); @@ -98,28 +100,24 @@ void hal_prepare_boot(void) /* no op */ } -int hal_flash_write(uint32_t address, const uint8_t *data, int len) +int hal_flash_write(uintptr_t address, const uint8_t *data, int len) { - uint8_t *ptr = 0; - /* implicit cast abide compiler warning */ - memcpy(ptr + address, data, len); + memcpy((void*)address, data, len); return 0; } -int hal_flash_erase(uint32_t address, int len) +int hal_flash_erase(uintptr_t address, int len) { - uint8_t *ptr = 0; - /* implicit cast abide compiler warning */ - fprintf(stderr,"hal_flash_erase addr %x len %d\n", address, len); + fprintf(stderr,"hal_flash_erase addr %p len %d\n", (void*)address, len); if (address == erasefail_address) { fprintf(stderr,"POWER FAILURE\n"); /* Corrupt page */ - memset(ptr + address, 0xEE, len); + memset((void*)address, 0xEE, len); exit(0); } - memset(ptr + address, 0xff, len); + memset((void*)address, 0xff, len); return 0; } diff --git a/include/hal.h b/include/hal.h index 54411f834..f371042e4 100644 --- a/include/hal.h +++ b/include/hal.h @@ -46,8 +46,8 @@ void hal_deinit(); #endif void hal_init(void); -int hal_flash_write(uint32_t address, const uint8_t *data, int len); -int hal_flash_erase(uint32_t address, int len); +int hal_flash_write(uintptr_t address, const uint8_t *data, int len); +int hal_flash_erase(uintptr_t address, int len); void hal_flash_unlock(void); void hal_flash_lock(void); void hal_prepare_boot(void); diff --git a/src/libwolfboot.c b/src/libwolfboot.c index c44567379..bdccecd0a 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -238,11 +238,11 @@ static int RAMFUNCTION trailer_write(uint8_t part, uint32_t addr, uint8_t val) { return ret; } -static int RAMFUNCTION partition_magic_write(uint8_t part, uint32_t addr) +static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr) { - uint32_t off = addr % NVM_CACHE_SIZE; - size_t base = (size_t)addr - off; - size_t addr_read, addr_write; + uintptr_t off = addr % NVM_CACHE_SIZE; + uintptr_t base = (uintptr_t)addr - off; + uintptr_t addr_read, addr_write; int ret; nvm_cached_sector = nvm_select_fresh_sector(part); addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE); @@ -384,10 +384,10 @@ static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val) static void RAMFUNCTION set_partition_magic(uint8_t part) { if (part == PART_BOOT) { - partition_magic_write(part, (uint32_t)(PART_BOOT_ENDFLAGS - sizeof(uint32_t))); + partition_magic_write(part, PART_BOOT_ENDFLAGS - sizeof(uint32_t)); } else if (part == PART_UPDATE) { - partition_magic_write(part, (uint32_t)(PART_UPDATE_ENDFLAGS - sizeof(uint32_t))); + partition_magic_write(part, PART_UPDATE_ENDFLAGS - sizeof(uint32_t)); } } #endif /* EXT_FLASH */ diff --git a/tools/scripts/qemu64/sign_linux.sh b/tools/scripts/qemu64/sign_linux.sh index d64d1a989..fa8db8980 100755 --- a/tools/scripts/qemu64/sign_linux.sh +++ b/tools/scripts/qemu64/sign_linux.sh @@ -4,8 +4,8 @@ tools/keytools/sign --ecc256 --sha256 bzImage wolfboot_signing_private_key.der 8 tools/keytools/sign --ecc256 --sha256 bzImage wolfboot_signing_private_key.der 2 cp base-part-image app.bin -dd if=bzImage_v8_signed.bin of=app.bin bs=1K seek=1024 conv=notrunc -dd if=bzImage_v2_signed.bin of=app.bin bs=1K seek=17408 conv=notrunc +dd if=bzImage_v8_signed.bin of=app.bin bs=1k seek=1024 conv=notrunc +dd if=bzImage_v2_signed.bin of=app.bin bs=1k seek=17408 conv=notrunc diff --git a/tools/test.mk b/tools/test.mk index d4be6fbbb..fc8a949a1 100644 --- a/tools/test.mk +++ b/tools/test.mk @@ -141,11 +141,11 @@ test-update: test-app/image.bin FORCE test-sim-external-flash-with-enc-update:SIGN_ENC_ARGS=--encrypt /tmp/enc_key.der --aes128 test-sim-external-flash-with-enc-update: wolfboot.bin test-app/image.elf FORCE $(Q)cp test-app/image.elf test-app/image.bak.elf - $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1K count=16 oflag=append conv=notrunc + $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1k count=16 oflag=append conv=notrunc @printf "0123456789abcdef0123456789abcdef0123456789abcdef" > /tmp/enc_key.der $(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) $(SIGN_ENC_ARGS) test-app/image.elf $(PRIVATE_KEY) 1 $(Q)cp test-app/image.bak.elf test-app/image.elf - $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1K count=16 oflag=append conv=notrunc + $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1k count=16 oflag=append conv=notrunc $(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) $(SIGN_ENC_ARGS) test-app/image.elf $(PRIVATE_KEY) $(TEST_UPDATE_VERSION) $(Q)$(SIGN_TOOL) $(SIGN_ARGS) $(DELTA_UPDATE_OPTIONS) $(SIGN_ENC_ARGS) \ test-app/image.elf $(PRIVATE_KEY) $(TEST_UPDATE_VERSION) @@ -167,10 +167,10 @@ test-sim-external-flash-with-enc-delta-update: test-sim-internal-flash-with-update: wolfboot.bin test-app/image.elf FORCE $(Q)cp test-app/image.elf test-app/image.bak.elf - $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1K count=16 oflag=append conv=notrunc + $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1k count=16 oflag=append conv=notrunc $(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) test-app/image.elf $(PRIVATE_KEY) 1 $(Q)cp test-app/image.bak.elf test-app/image.elf - $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1K count=16 oflag=append conv=notrunc + $(Q)dd if=/dev/urandom of=test-app/image.elf bs=1k count=16 oflag=append conv=notrunc $(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) test-app/image.elf $(PRIVATE_KEY) $(TEST_UPDATE_VERSION) $(Q)dd if=/dev/zero bs=$$(($(WOLFBOOT_SECTOR_SIZE))) count=1 2>/dev/null | tr "\000" "\377" > erased_sec.dd $(Q)$(SIGN_TOOL) $(SIGN_ARGS) $(DELTA_UPDATE_OPTIONS) \