Skip to content

Commit 7f0716a

Browse files
committed
Fix sfdp_find_addr_region algorithm
sfdp_find_addr_region() was causing issues with SPI flashes with sector table parsed from SFDP (in particular SST26VF016B). In particular, it was returning -1 when address 0 is passed (probably also if the address in the first region). I do not know why the search algorithm is written to search from the higher to lower regions, but it was obvious that it would fail for the first region. Also it was harder to read due to the index manipulation.
1 parent 0db72d0 commit 7f0716a

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

drivers/include/drivers/internal/SFDP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ int sfdp_detect_erase_types_inst_and_size(uint8_t *bptbl_ptr, sfdp_hdr_info &sfd
121121
*
122122
* @return Region number
123123
*/
124-
int sfdp_find_addr_region(bd_size_t offset, const sfdp_hdr_info &sfdp_info);
124+
int sfdp_find_addr_region(bd_addr_t offset, const sfdp_hdr_info &sfdp_info);
125125

126126
/** Finds the largest Erase Type of the Region to which the offset belongs to
127127
*

drivers/source/SFDP.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,20 @@ int sfdp_detect_erase_types_inst_and_size(uint8_t *bptbl_ptr, sfdp_hdr_info &sfd
377377
return 0;
378378
}
379379

380-
int sfdp_find_addr_region(bd_size_t offset, const sfdp_hdr_info &sfdp_info)
380+
int sfdp_find_addr_region(bd_addr_t offset, const sfdp_hdr_info &sfdp_info)
381381
{
382-
if ((offset > sfdp_info.bptbl.device_size_bytes) || (sfdp_info.smptbl.region_cnt == 0)) {
382+
if ((offset >= sfdp_info.bptbl.device_size_bytes) || (sfdp_info.smptbl.region_cnt == 0)) {
383383
return -1;
384384
}
385385

386386
if (sfdp_info.smptbl.region_cnt == 1) {
387387
return 0;
388388
}
389389

390-
for (int idx = sfdp_info.smptbl.region_cnt - 2; idx >= 0; idx--) {
390+
for (int idx = 0; idx < sfdp_info.smptbl.region_cnt; idx++) {
391391

392-
if (offset > sfdp_info.smptbl.region_high_boundary[idx]) {
393-
return (idx + 1);
392+
if (offset <= sfdp_info.smptbl.region_high_boundary[idx]) {
393+
return idx;
394394
}
395395
}
396396
return -1;

0 commit comments

Comments
 (0)