From 26e98f6f0614e38e96ac283cc4fd7150716068e8 Mon Sep 17 00:00:00 2001 From: Aarya Chaumal Date: Mon, 4 Jul 2022 19:18:21 +0530 Subject: [PATCH] flashrom.c:Add function to align region to sector boundaries Add a function to align start and end address of the region (in struct walk_info) to some erase sector boundaries and modify the region start and end addresses to match nearest erase sector boundaries. This function will be used in the new algorithm for erase function selection. Change-Id: I215ea4986aa23360fc65ff761f4e49c6069160ac Signed-off-by: Aarya Chaumal --- flashrom.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/flashrom.c b/flashrom.c index d3f802bf6..ab82d1ff5 100644 --- a/flashrom.c +++ b/flashrom.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1010,6 +1011,31 @@ static int walk_eraseblocks(struct flashctx *const flashctx, return 0; } +void align_region(const struct erase_layout *layout, struct flashctx *const flashctx, chipoff_t *region_start, chipoff_t *region_end); + +void align_region(const struct erase_layout *layout, struct flashctx *const flashctx, chipoff_t *region_start, chipoff_t *region_end) +{ + chipoff_t start_diff = UINT_MAX, end_diff = UINT_MAX; + int erasefn_count = count_usable_erasers(flashctx); + for (int i = 0; i < erasefn_count; i++) { + for (size_t j = 0; j < layout[i].block_count; j++) { + if (layout[i].layout_list[j].start_addr <= *region_start) + start_diff = (*region_start - layout[i].layout_list[j].start_addr) > start_diff ? start_diff : (*region_start - layout[i].layout_list[j].start_addr); + if (layout[i].layout_list[j].end_addr >= *region_end) + end_diff = (layout[i].layout_list[j].end_addr - *region_end) > end_diff ? end_diff : (layout[i].layout_list[j].end_addr - *region_end); + } + } + + if (start_diff) { + msg_cinfo("Region start not sector aligned!! Extending start boundaries...\n"); + *region_start = *region_start - start_diff; + } + if (end_diff) { + msg_cinfo("Region end not sector aligned!! Extending end boundaries...\n"); + *region_end = *region_end + end_diff; + } +} + static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info, const per_blockfn_t per_blockfn) {