From 1f001509b0fd5c6f8c5388e085b7b2bbd03f05fa Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Fri, 30 Jun 2023 17:42:18 +0200 Subject: [PATCH 1/5] Fix undefined behavior Fixes undefined behavior where SDCardBlockDevice::wait_for_completition() runs off the end without returning a value. --- libraries/BlockDevices/SDCardBlockDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BlockDevices/SDCardBlockDevice.cpp b/libraries/BlockDevices/SDCardBlockDevice.cpp index 1cd897ed6..00c7df4fd 100644 --- a/libraries/BlockDevices/SDCardBlockDevice.cpp +++ b/libraries/BlockDevices/SDCardBlockDevice.cpp @@ -478,7 +478,7 @@ fsp_err_t SDCardBlockDevice::wait_for_completition() { rv = FSP_ERR_TIMEOUT; } - + return rv; } From 578cd8403ee310acc9f182377e794212023c1832 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:10:52 +0200 Subject: [PATCH 2/5] Extend block device data types The size of large block devices overflows when these variables are 32 bits long. For example, a 32 GB SD Card is reported as smaller than it really is. --- libraries/BlockDevices/BlockDevice.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/BlockDevices/BlockDevice.h b/libraries/BlockDevices/BlockDevice.h index 89b317e15..98e554742 100644 --- a/libraries/BlockDevices/BlockDevice.h +++ b/libraries/BlockDevices/BlockDevice.h @@ -30,8 +30,8 @@ #define BD_ERROR_DEVICE_ERROR (-4001) #define BD_ERROR_WRITE_PROTECTED (4002) -typedef uint32_t bd_addr_t; -typedef uint32_t bd_size_t; +typedef uint64_t bd_addr_t; +typedef uint64_t bd_size_t; typedef pin_size_t pin_t; /* -------------------------------------------------------------------------- */ @@ -107,4 +107,4 @@ class BlockDevice { virtual const char *get_type() const = 0; }; -#endif \ No newline at end of file +#endif From a272ff6540bbe1e60e98f751f00017b4f6f1e1c1 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:13:20 +0200 Subject: [PATCH 3/5] Update declaration to match definition exactly --- libraries/BlockDevices/CodeFlashBlockDevice.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/BlockDevices/CodeFlashBlockDevice.h b/libraries/BlockDevices/CodeFlashBlockDevice.h index 24454b8b9..1b26e3f77 100644 --- a/libraries/BlockDevices/CodeFlashBlockDevice.h +++ b/libraries/BlockDevices/CodeFlashBlockDevice.h @@ -54,8 +54,8 @@ using FLASH_set_callback_f = fsp_err_t (*)(flash_ctrl_t * const p_api_ct class CodeFlashBlockDevice : public BlockDevice { private: CodeFlashBlockDevice(); - int erase_block(uint32_t add); - int check_blank(uint32_t add); + int erase_block(bd_addr_t add); + int check_blank(bd_addr_t add); uint32_t get_block_starting_address(bd_addr_t add); uint32_t get_physical_address(bd_addr_t add); virtual int write(const void *buffer, bd_addr_t addr, bd_size_t size) override; From 8d40a8ac5829731b0574df76d2d64e04f0441f30 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:14:18 +0200 Subject: [PATCH 4/5] Update declaration to match definition exactly --- libraries/BlockDevices/DataFlashBlockDevice.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/BlockDevices/DataFlashBlockDevice.h b/libraries/BlockDevices/DataFlashBlockDevice.h index a206ee689..4e036760d 100644 --- a/libraries/BlockDevices/DataFlashBlockDevice.h +++ b/libraries/BlockDevices/DataFlashBlockDevice.h @@ -73,8 +73,8 @@ using FLASH_set_callback_f = fsp_err_t (*)(flash_ctrl_t * const p_api_ct class DataFlashBlockDevice : public BlockDevice { private: DataFlashBlockDevice(); - int erase_block(uint32_t add); - int check_blank(uint32_t add); + int erase_block(bd_addr_t add); + int check_blank(bd_addr_t add); uint32_t get_block_starting_address(bd_addr_t add); uint32_t get_physical_address(bd_addr_t add); bool is_address_correct(bd_addr_t add); @@ -136,4 +136,4 @@ class DataFlashBlockDevice : public BlockDevice { }; -#endif \ No newline at end of file +#endif From eda817303900654e377332d47c53d82e926c48b5 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:19:47 +0200 Subject: [PATCH 5/5] Fix multiplication overflow The sd_card_info.sector_count and sd_card_info.sector_size_bytes variables are both 32 bit, so the multiplication overflows for large block devices. --- libraries/BlockDevices/SDCardBlockDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BlockDevices/SDCardBlockDevice.cpp b/libraries/BlockDevices/SDCardBlockDevice.cpp index 00c7df4fd..4b5c825b3 100644 --- a/libraries/BlockDevices/SDCardBlockDevice.cpp +++ b/libraries/BlockDevices/SDCardBlockDevice.cpp @@ -313,7 +313,7 @@ int SDCardBlockDevice::open() { read_block_size = sd_card_info.sector_size_bytes; erase_block_size = sd_card_info.sector_size_bytes; write_block_size = sd_card_info.sector_size_bytes; - total_size = sd_card_info.sector_count * sd_card_info.sector_size_bytes; + total_size = (bd_size_t) sd_card_info.sector_count * (bd_size_t) sd_card_info.sector_size_bytes; } }