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 | |
| 18 | /* |
| 19 | * For now, we only support one flash device. |
| 20 | * |
| 21 | * Pick the SoC Flash driver ID. |
| 22 | */ |
| 23 | #define FLASH_DEVICE_ID SOC_FLASH_0_ID |
| 24 | #define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS |
| 25 | |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 26 | static struct device *flash_dev; |
| 27 | |
| 28 | struct device *flash_device_get_binding(char *dev_name) |
| 29 | { |
| 30 | if (!flash_dev) { |
| 31 | flash_dev = device_get_binding(dev_name); |
| 32 | } |
| 33 | return flash_dev; |
| 34 | } |
| 35 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 36 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 37 | { |
| 38 | if (fd_id != FLASH_DEVICE_ID) { |
| 39 | BOOT_LOG_ERR("invalid flash ID %d; expected %d", |
| 40 | fd_id, FLASH_DEVICE_ID); |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | *ret = FLASH_DEVICE_BASE; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * This depends on the mappings defined in sysflash.h, and assumes |
| 49 | * that slot 0, slot 1, and the scratch areas are contiguous. |
| 50 | */ |
| 51 | int flash_area_id_from_image_slot(int slot) |
| 52 | { |
| 53 | return slot + FLASH_AREA_IMAGE_0; |
| 54 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 55 | |
| 56 | int flash_area_sector_from_off(off_t off, struct flash_sector *sector) |
| 57 | { |
| 58 | int rc; |
| 59 | struct flash_pages_info page; |
| 60 | |
| 61 | rc = flash_get_page_info_by_offs(flash_dev, off, &page); |
| 62 | if (rc) { |
| 63 | return rc; |
| 64 | } |
| 65 | |
| 66 | sector->fs_off = page.start_offset; |
| 67 | sector->fs_size = page.size; |
| 68 | |
| 69 | return rc; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 70 | } |
| 71 | |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 72 | #define ERASED_VAL 0xff |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 73 | uint8_t flash_area_erased_val(const struct flash_area *fap) |
| 74 | { |
| 75 | (void)fap; |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 76 | return ERASED_VAL; |
| 77 | } |
| 78 | |
| 79 | int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off, |
| 80 | void *dst, uint32_t len) |
| 81 | { |
| 82 | uint8_t i; |
| 83 | uint8_t *u8dst; |
| 84 | int rc; |
| 85 | |
Andrzej Puzdrowski | 5f81b12 | 2018-10-09 12:18:49 +0200 | [diff] [blame^] | 86 | rc = flash_area_read(fa, off, dst, len); |
Fabio Utzig | cea90f9 | 2018-09-19 08:12:46 -0300 | [diff] [blame] | 87 | if (rc) { |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) { |
| 92 | if (u8dst[i] != ERASED_VAL) { |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 1; |
Fabio Utzig | 42ad446 | 2018-08-14 08:55:23 -0300 | [diff] [blame] | 98 | } |