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 | |
| 8 | #include <zephyr.h> |
Peter Bigot | 54c1e3f | 2020-01-25 05:50:12 -0600 | [diff] [blame] | 9 | #include <drivers/flash.h> |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 10 | |
| 11 | #include "target.h" |
| 12 | |
| 13 | #include <flash_map_backend/flash_map_backend.h> |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 14 | #include <sysflash/sysflash.h> |
| 15 | |
| 16 | #include "bootutil/bootutil_log.h" |
| 17 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 18 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 19 | |
Kumar Gala | 32b61f3 | 2020-04-08 12:02:03 -0500 | [diff] [blame] | 20 | #if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 21 | #define FLASH_DEVICE_ID SOC_FLASH_0_ID |
Rajavardhan Gundi | 73bb71b | 2019-01-28 15:07:04 +0530 | [diff] [blame] | 22 | #define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS |
Rajavardhan Gundi | 24321c3 | 2019-02-08 12:48:34 +0530 | [diff] [blame] | 23 | #elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL)) |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 24 | #define FLASH_DEVICE_ID SPI_FLASH_0_ID |
Rajavardhan Gundi | 73bb71b | 2019-01-28 15:07:04 +0530 | [diff] [blame] | 25 | #define FLASH_DEVICE_BASE 0 |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 26 | #else |
| 27 | #error "FLASH_DEVICE_ID could not be determined" |
| 28 | #endif |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 29 | |
Fabio Utzig | 0b6b51f | 2020-09-02 11:39:43 -0300 | [diff] [blame] | 30 | static const struct device *flash_dev; |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 31 | |
Fabio Utzig | 0b6b51f | 2020-09-02 11:39:43 -0300 | [diff] [blame] | 32 | const struct device *flash_device_get_binding(char *dev_name) |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 33 | { |
| 34 | if (!flash_dev) { |
| 35 | flash_dev = device_get_binding(dev_name); |
| 36 | } |
| 37 | return flash_dev; |
| 38 | } |
| 39 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 40 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 41 | { |
| 42 | if (fd_id != FLASH_DEVICE_ID) { |
| 43 | BOOT_LOG_ERR("invalid flash ID %d; expected %d", |
| 44 | fd_id, FLASH_DEVICE_ID); |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | *ret = FLASH_DEVICE_BASE; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* |
Andrzej Puzdrowski | 419a475 | 2019-01-23 16:31:19 +0100 | [diff] [blame] | 52 | * This depends on the mappings defined in sysflash.h. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 53 | * MCUBoot uses continuous numbering for the primary slot, the secondary slot, |
| 54 | * and the scratch while zephyr might number it differently. |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 55 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 56 | 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] | 57 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 58 | switch (slot) { |
| 59 | case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); |
Andrzej Puzdrowski | fdff3e1 | 2020-09-15 08:23:25 +0200 | [diff] [blame] | 60 | #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 61 | case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); |
Fabio Utzig | c58842e | 2019-11-28 10:30:01 -0300 | [diff] [blame] | 62 | #endif |
Andrzej Puzdrowski | c49d7c9 | 2021-05-13 12:52:34 +0200 | [diff] [blame] | 63 | #if defined(CONFIG_BOOT_SWAP_USING_SCRATCH) |
| 64 | case 2: return FLASH_AREA_IMAGE_SCRATCH; |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 65 | #endif |
Andrzej Puzdrowski | 419a475 | 2019-01-23 16:31:19 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return -EINVAL; /* flash_area_open will fail on that */ |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 69 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 70 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 71 | int flash_area_id_from_image_slot(int slot) |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 72 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 73 | return flash_area_id_from_multi_image_slot(0, slot); |
| 74 | } |
| 75 | |
| 76 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
| 77 | { |
| 78 | if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 79 | return 0; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 80 | } |
Andrzej Puzdrowski | fdff3e1 | 2020-09-15 08:23:25 +0200 | [diff] [blame] | 81 | #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 82 | if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 83 | return 1; |
| 84 | } |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 85 | #endif |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 86 | |
| 87 | BOOT_LOG_ERR("invalid flash area ID"); |
| 88 | return -1; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 91 | int flash_area_id_to_image_slot(int area_id) |
| 92 | { |
| 93 | return flash_area_id_to_multi_image_slot(0, area_id); |
| 94 | } |
| 95 | |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 96 | int flash_area_sector_from_off(off_t off, struct flash_sector *sector) |
| 97 | { |
| 98 | int rc; |
| 99 | struct flash_pages_info page; |
| 100 | |
| 101 | rc = flash_get_page_info_by_offs(flash_dev, off, &page); |
| 102 | if (rc) { |
| 103 | return rc; |
| 104 | } |
| 105 | |
| 106 | sector->fs_off = page.start_offset; |
| 107 | sector->fs_size = page.size; |
| 108 | |
| 109 | return rc; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 110 | } |
| 111 | |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 112 | #define ERASED_VAL 0xff |
Andrzej Puzdrowski | c0dbdd4 | 2020-10-27 13:17:59 +0100 | [diff] [blame] | 113 | __weak uint8_t flash_area_erased_val(const struct flash_area *fap) |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 114 | { |
| 115 | (void)fap; |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 116 | return ERASED_VAL; |
| 117 | } |