David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 1 | /* |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include "target.h" |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 10 | #include "flash_map/flash_map.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 11 | #include "flash_map_backend/flash_map_backend.h" |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 12 | #include "bootutil_priv.h" |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 13 | #include "bootutil/bootutil_log.h" |
| 14 | #include "Driver_Flash.h" |
| 15 | |
Mark Horvath | 8576e38 | 2021-03-12 10:24:55 +0100 | [diff] [blame^] | 16 | extern const struct flash_area flash_map[]; |
| 17 | extern const int flash_map_entry_num; |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 18 | |
| 19 | /* |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 20 | * Check the target address in the flash_area_xxx operation. |
| 21 | */ |
| 22 | static bool is_range_valid(const struct flash_area *area, |
| 23 | uint32_t off, |
| 24 | uint32_t len) |
| 25 | { |
| 26 | uint32_t size; |
| 27 | |
| 28 | if (!area) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | if (!boot_u32_safe_add(&size, off, len)) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (area->fa_size < size) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | /* |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 44 | * `open` a flash area. The `area` in this case is not the individual |
| 45 | * sectors, but describes the particular flash area in question. |
| 46 | */ |
| 47 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 48 | { |
| 49 | int i; |
| 50 | |
| 51 | BOOT_LOG_DBG("area %d", id); |
| 52 | |
| 53 | for (i = 0; i < flash_map_entry_num; i++) { |
| 54 | if (id == flash_map[i].fa_id) { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | if (i == flash_map_entry_num) { |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | *area = &flash_map[i]; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | void flash_area_close(const struct flash_area *area) |
| 67 | { |
| 68 | /* Nothing to do. */ |
| 69 | } |
| 70 | |
| 71 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 72 | uint32_t len) |
| 73 | { |
| 74 | BOOT_LOG_DBG("read area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 75 | |
| 76 | if (!is_range_valid(area, off, len)) { |
| 77 | return -1; |
| 78 | } |
| 79 | |
Michel Jaouen | 26f6c02 | 2020-12-03 10:37:22 +0100 | [diff] [blame] | 80 | return DRV_FLASH_AREA(area)->ReadData(area->fa_off + off, dst, len); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int flash_area_write(const struct flash_area *area, uint32_t off, |
| 84 | const void *src, uint32_t len) |
| 85 | { |
| 86 | BOOT_LOG_DBG("write area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 87 | |
| 88 | if (!is_range_valid(area, off, len)) { |
| 89 | return -1; |
| 90 | } |
| 91 | |
Michel Jaouen | 26f6c02 | 2020-12-03 10:37:22 +0100 | [diff] [blame] | 92 | return DRV_FLASH_AREA(area)->ProgramData(area->fa_off + off, src, len); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 96 | { |
| 97 | ARM_FLASH_INFO *flash_info; |
| 98 | uint32_t deleted_len = 0; |
| 99 | int32_t rc = 0; |
| 100 | |
| 101 | BOOT_LOG_DBG("erase area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
Sherry Zhang | 6e0f324 | 2021-03-08 12:18:31 +0800 | [diff] [blame] | 102 | |
| 103 | if (!is_range_valid(area, off, len)) { |
| 104 | return -1; |
| 105 | } |
| 106 | |
Michel Jaouen | 26f6c02 | 2020-12-03 10:37:22 +0100 | [diff] [blame] | 107 | flash_info = DRV_FLASH_AREA(area)->GetInfo(); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 108 | |
| 109 | if (flash_info->sector_info == NULL) { |
| 110 | /* Uniform sector layout */ |
| 111 | while (deleted_len < len) { |
Michel Jaouen | 26f6c02 | 2020-12-03 10:37:22 +0100 | [diff] [blame] | 112 | rc = DRV_FLASH_AREA(area)->EraseSector(area->fa_off + off); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 113 | if (rc != 0) { |
| 114 | break; |
| 115 | } |
| 116 | deleted_len += flash_info->sector_size; |
| 117 | off += flash_info->sector_size; |
| 118 | } |
| 119 | } else { |
| 120 | /* Inhomogeneous sector layout, explicitly defined |
| 121 | * Currently not supported. |
| 122 | */ |
| 123 | } |
| 124 | |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | uint32_t flash_area_align(const struct flash_area *area) |
| 129 | { |
| 130 | ARM_FLASH_INFO *flash_info; |
| 131 | |
Michel Jaouen | 26f6c02 | 2020-12-03 10:37:22 +0100 | [diff] [blame] | 132 | flash_info = DRV_FLASH_AREA(area)->GetInfo(); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 133 | return flash_info->program_unit; |
| 134 | } |