|
7 | 7 | #include <drivers/flash.h> |
8 | 8 |
|
9 | 9 | static int flash_get_page_info(const struct device *dev, off_t offs, |
10 | | - bool use_addr, struct flash_pages_info *info) |
| 10 | + uint32_t index, struct flash_pages_info *info) |
11 | 11 | { |
12 | 12 | const struct flash_driver_api *api = dev->api; |
13 | 13 | const struct flash_pages_layout *layout; |
14 | | - size_t page_count = 0; |
15 | | - off_t group_offs = 0; |
16 | | - uint32_t num_in_group; |
17 | | - off_t end = 0; |
18 | 14 | size_t layout_size; |
| 15 | + uint32_t index_jmp; |
| 16 | + |
| 17 | + info->start_offset = 0; |
| 18 | + info->index = 0U; |
19 | 19 |
|
20 | 20 | api->page_layout(dev, &layout, &layout_size); |
21 | 21 |
|
22 | 22 | while (layout_size--) { |
23 | | - if (use_addr) { |
24 | | - end += layout->pages_count * layout->pages_size; |
| 23 | + info->size = layout->pages_size; |
| 24 | + if (offs == 0) { |
| 25 | + index_jmp = index - info->index; |
25 | 26 | } else { |
26 | | - end += layout->pages_count; |
| 27 | + index_jmp = (offs - info->start_offset) / info->size; |
27 | 28 | } |
28 | 29 |
|
29 | | - if (offs < end) { |
30 | | - info->size = layout->pages_size; |
31 | | - |
32 | | - if (use_addr) { |
33 | | - num_in_group = (offs - group_offs) / |
34 | | - layout->pages_size; |
35 | | - } else { |
36 | | - num_in_group = offs - page_count; |
37 | | - } |
38 | | - |
39 | | - info->start_offset = group_offs + |
40 | | - num_in_group * layout->pages_size; |
41 | | - info->index = page_count + num_in_group; |
42 | | - |
| 30 | + index_jmp = MIN(index_jmp, layout->pages_count); |
| 31 | + info->start_offset += (index_jmp * info->size); |
| 32 | + info->index += index_jmp; |
| 33 | + if (index_jmp < layout->pages_count) { |
43 | 34 | return 0; |
44 | 35 | } |
45 | 36 |
|
46 | | - group_offs += layout->pages_count * layout->pages_size; |
47 | | - page_count += layout->pages_count; |
48 | | - |
49 | 37 | layout++; |
50 | 38 | } |
51 | 39 |
|
52 | | - return -EINVAL; /* page of the index doesn't exist */ |
| 40 | + return -EINVAL; /* page at offs or idx doesn't exist */ |
53 | 41 | } |
54 | 42 |
|
55 | 43 | int z_impl_flash_get_page_info_by_offs(const struct device *dev, off_t offs, |
56 | 44 | struct flash_pages_info *info) |
57 | 45 | { |
58 | | - return flash_get_page_info(dev, offs, true, info); |
| 46 | + return flash_get_page_info(dev, offs, 0U, info); |
59 | 47 | } |
60 | 48 |
|
61 | 49 | int z_impl_flash_get_page_info_by_idx(const struct device *dev, |
62 | 50 | uint32_t page_index, |
63 | 51 | struct flash_pages_info *info) |
64 | 52 | { |
65 | | - return flash_get_page_info(dev, page_index, false, info); |
| 53 | + return flash_get_page_info(dev, 0, page_index, info); |
66 | 54 | } |
67 | 55 |
|
68 | 56 | size_t z_impl_flash_get_page_count(const struct device *dev) |
|
0 commit comments