Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Nordic Semiconductor ASA |
| 3 | * Copyright (c) 2015 Runtime Inc |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | */ |
| 7 | |
Gerard Marull-Paretas | 34dd9e7 | 2022-05-09 12:13:12 +0200 | [diff] [blame] | 8 | #include <zephyr/kernel.h> |
Gerard Marull-Paretas | 3cd2cec | 2022-05-09 12:10:05 +0200 | [diff] [blame] | 9 | #include <zephyr/devicetree.h> |
| 10 | #include <zephyr/drivers/flash.h> |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 11 | |
| 12 | #include "target.h" |
| 13 | |
| 14 | #include <flash_map_backend/flash_map_backend.h> |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 15 | #include <sysflash/sysflash.h> |
| 16 | |
| 17 | #include "bootutil/bootutil_log.h" |
| 18 | |
Carlos Falgueras GarcĂa | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 19 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 20 | |
Gerard Marull-Paretas | aa041a2 | 2022-03-25 12:22:29 +0100 | [diff] [blame] | 21 | #if (!defined(CONFIG_XTENSA) && DT_HAS_CHOSEN(zephyr_flash_controller)) |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 22 | #define FLASH_DEVICE_ID SOC_FLASH_0_ID |
Rajavardhan Gundi | 73bb71b | 2019-01-28 15:07:04 +0530 | [diff] [blame] | 23 | #define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS |
Kumar Gala | 8eadf8c | 2022-07-27 09:15:46 -0500 | [diff] [blame^] | 24 | #define FLASH_DEVICE_NODE DT_CHOSEN(zephyr_flash_controller) |
| 25 | #elif (defined(CONFIG_XTENSA) && DT_NODE_EXISTS(DT_INST(0, jedec_spi_nor)) |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 26 | #define FLASH_DEVICE_ID SPI_FLASH_0_ID |
Rajavardhan Gundi | 73bb71b | 2019-01-28 15:07:04 +0530 | [diff] [blame] | 27 | #define FLASH_DEVICE_BASE 0 |
Kumar Gala | 8eadf8c | 2022-07-27 09:15:46 -0500 | [diff] [blame^] | 28 | #define FLASH_DEVICE_NODE DT_INST(0, jedec_spi_nor) |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 29 | #else |
| 30 | #error "FLASH_DEVICE_ID could not be determined" |
| 31 | #endif |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 32 | |
Kumar Gala | 8eadf8c | 2022-07-27 09:15:46 -0500 | [diff] [blame^] | 33 | static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE); |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 34 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 35 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 36 | { |
| 37 | if (fd_id != FLASH_DEVICE_ID) { |
| 38 | BOOT_LOG_ERR("invalid flash ID %d; expected %d", |
| 39 | fd_id, FLASH_DEVICE_ID); |
| 40 | return -EINVAL; |
| 41 | } |
| 42 | *ret = FLASH_DEVICE_BASE; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* |
Andrzej Puzdrowski | 419a475 | 2019-01-23 16:31:19 +0100 | [diff] [blame] | 47 | * This depends on the mappings defined in sysflash.h. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 48 | * MCUBoot uses continuous numbering for the primary slot, the secondary slot, |
| 49 | * and the scratch while zephyr might number it differently. |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 50 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 51 | int flash_area_id_from_multi_image_slot(int image_index, int slot) |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 52 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 53 | switch (slot) { |
| 54 | case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); |
Andrzej Puzdrowski | fdff3e1 | 2020-09-15 08:23:25 +0200 | [diff] [blame] | 55 | #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 56 | case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); |
Fabio Utzig | c58842e | 2019-11-28 10:30:01 -0300 | [diff] [blame] | 57 | #endif |
Andrzej Puzdrowski | c49d7c9 | 2021-05-13 12:52:34 +0200 | [diff] [blame] | 58 | #if defined(CONFIG_BOOT_SWAP_USING_SCRATCH) |
| 59 | case 2: return FLASH_AREA_IMAGE_SCRATCH; |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 60 | #endif |
Andrzej Puzdrowski | 419a475 | 2019-01-23 16:31:19 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return -EINVAL; /* flash_area_open will fail on that */ |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 64 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 65 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 66 | int flash_area_id_from_image_slot(int slot) |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 67 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 68 | return flash_area_id_from_multi_image_slot(0, slot); |
| 69 | } |
| 70 | |
| 71 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
| 72 | { |
| 73 | if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 74 | return 0; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 75 | } |
Andrzej Puzdrowski | fdff3e1 | 2020-09-15 08:23:25 +0200 | [diff] [blame] | 76 | #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 77 | if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 78 | return 1; |
| 79 | } |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 80 | #endif |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 81 | |
| 82 | BOOT_LOG_ERR("invalid flash area ID"); |
| 83 | return -1; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 86 | int flash_area_id_to_image_slot(int area_id) |
| 87 | { |
| 88 | return flash_area_id_to_multi_image_slot(0, area_id); |
| 89 | } |
| 90 | |
Dominik Ermel | 6c8932e | 2021-07-09 10:28:40 +0000 | [diff] [blame] | 91 | #if defined(CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) |
| 92 | int flash_area_id_from_direct_image(int image_id) |
| 93 | { |
| 94 | switch (image_id) { |
| 95 | case 0: |
| 96 | case 1: |
| 97 | return FLASH_AREA_ID(image_0); |
| 98 | #if DT_HAS_FIXED_PARTITION_LABEL(image_1) |
| 99 | case 2: |
| 100 | return FLASH_AREA_ID(image_1); |
| 101 | #endif |
| 102 | #if DT_HAS_FIXED_PARTITION_LABEL(image_2) |
| 103 | case 3: |
| 104 | return FLASH_AREA_ID(image_2); |
| 105 | #endif |
| 106 | #if DT_HAS_FIXED_PARTITION_LABEL(image_3) |
| 107 | case 4: |
| 108 | return FLASH_AREA_ID(image_3); |
| 109 | #endif |
| 110 | } |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | #endif |
| 114 | |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 115 | int flash_area_sector_from_off(off_t off, struct flash_sector *sector) |
| 116 | { |
| 117 | int rc; |
| 118 | struct flash_pages_info page; |
| 119 | |
| 120 | rc = flash_get_page_info_by_offs(flash_dev, off, &page); |
| 121 | if (rc) { |
| 122 | return rc; |
| 123 | } |
| 124 | |
| 125 | sector->fs_off = page.start_offset; |
| 126 | sector->fs_size = page.size; |
| 127 | |
| 128 | return rc; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 129 | } |
| 130 | |
Dominik Ermel | dc1b9f0 | 2021-04-23 05:28:25 +0000 | [diff] [blame] | 131 | uint8_t flash_area_get_device_id(const struct flash_area *fa) |
| 132 | { |
| 133 | (void)fa; |
| 134 | return FLASH_DEVICE_ID; |
| 135 | } |
| 136 | |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 137 | #define ERASED_VAL 0xff |
Andrzej Puzdrowski | c0dbdd4 | 2020-10-27 13:17:59 +0100 | [diff] [blame] | 138 | __weak uint8_t flash_area_erased_val(const struct flash_area *fap) |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 139 | { |
| 140 | (void)fap; |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 141 | return ERASED_VAL; |
| 142 | } |