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> |
| 9 | #include <flash.h> |
| 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 | |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 20 | #if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME)) |
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 | |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 30 | static struct device *flash_dev; |
| 31 | |
| 32 | struct device *flash_device_get_binding(char *dev_name) |
| 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 | */ |
| 56 | int flash_area_id_from_image_slot(int slot) |
| 57 | { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame^] | 58 | #if (MCUBOOT_IMAGE_NUMBER == 1) |
| 59 | static |
| 60 | #endif |
| 61 | const int area_id_tab[] = {FLASH_AREA_IMAGE_PRIMARY, |
| 62 | FLASH_AREA_IMAGE_SECONDARY, |
| 63 | FLASH_AREA_IMAGE_SCRATCH}; |
Andrzej Puzdrowski | 419a475 | 2019-01-23 16:31:19 +0100 | [diff] [blame] | 64 | |
| 65 | if (slot >= 0 && slot < ARRAY_SIZE(area_id_tab)) { |
| 66 | return area_id_tab[slot]; |
| 67 | } |
| 68 | |
| 69 | return -EINVAL; /* flash_area_open will fail on that */ |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 70 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 71 | |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 72 | int flash_area_id_to_image_slot(int area_id) |
| 73 | { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame^] | 74 | if (area_id == FLASH_AREA_IMAGE_PRIMARY) { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 75 | return 0; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 76 | } |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame^] | 77 | if (area_id == FLASH_AREA_IMAGE_SECONDARY) { |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | BOOT_LOG_ERR("invalid flash area ID"); |
| 82 | return -1; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 85 | int flash_area_sector_from_off(off_t off, struct flash_sector *sector) |
| 86 | { |
| 87 | int rc; |
| 88 | struct flash_pages_info page; |
| 89 | |
| 90 | rc = flash_get_page_info_by_offs(flash_dev, off, &page); |
| 91 | if (rc) { |
| 92 | return rc; |
| 93 | } |
| 94 | |
| 95 | sector->fs_off = page.start_offset; |
| 96 | sector->fs_size = page.size; |
| 97 | |
| 98 | return rc; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 101 | #define ERASED_VAL 0xff |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 102 | uint8_t flash_area_erased_val(const struct flash_area *fap) |
| 103 | { |
| 104 | (void)fap; |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 105 | return ERASED_VAL; |
| 106 | } |
| 107 | |
| 108 | int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off, |
| 109 | void *dst, uint32_t len) |
| 110 | { |
| 111 | uint8_t i; |
| 112 | uint8_t *u8dst; |
| 113 | int rc; |
| 114 | |
Andrzej Puzdrowski | 5f81b12 | 2018-10-09 12:18:49 +0200 | [diff] [blame] | 115 | rc = flash_area_read(fa, off, dst, len); |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 116 | if (rc) { |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) { |
| 121 | if (u8dst[i] != ERASED_VAL) { |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return 1; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 127 | } |