blob: db2f94bb41584aaa630214f1d7e85eb192885897 [file] [log] [blame]
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001/*
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 Puzdrowskib788c712018-04-12 12:42:49 +020014#include <sysflash/sysflash.h>
15
16#include "bootutil/bootutil_log.h"
17
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010018MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
19
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053020#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020021#define FLASH_DEVICE_ID SOC_FLASH_0_ID
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053022#elif (defined(CONFIG_XTENSA) && defined(DT_SPI_NOR_DRV_NAME))
23#define FLASH_DEVICE_ID SPI_FLASH_0_ID
24#else
25#error "FLASH_DEVICE_ID could not be determined"
26#endif
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020027
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053028#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
Emanuele Di Santo205c8c62018-07-20 11:42:31 +020029static struct device *flash_dev;
30
31struct device *flash_device_get_binding(char *dev_name)
32{
33 if (!flash_dev) {
34 flash_dev = device_get_binding(dev_name);
35 }
36 return flash_dev;
37}
38
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020039int flash_device_base(uint8_t fd_id, uintptr_t *ret)
40{
41 if (fd_id != FLASH_DEVICE_ID) {
42 BOOT_LOG_ERR("invalid flash ID %d; expected %d",
43 fd_id, FLASH_DEVICE_ID);
44 return -EINVAL;
45 }
46 *ret = FLASH_DEVICE_BASE;
47 return 0;
48}
49
50/*
51 * This depends on the mappings defined in sysflash.h, and assumes
52 * that slot 0, slot 1, and the scratch areas are contiguous.
53 */
54int flash_area_id_from_image_slot(int slot)
55{
56 return slot + FLASH_AREA_IMAGE_0;
57}
Emanuele Di Santo205c8c62018-07-20 11:42:31 +020058
59int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
60{
61 int rc;
62 struct flash_pages_info page;
63
64 rc = flash_get_page_info_by_offs(flash_dev, off, &page);
65 if (rc) {
66 return rc;
67 }
68
69 sector->fs_off = page.start_offset;
70 sector->fs_size = page.size;
71
72 return rc;
Fabio Utzig42ad4462018-08-14 08:55:23 -030073}
74
Fabio Utzigcea90f92018-09-19 08:12:46 -030075#define ERASED_VAL 0xff
Fabio Utzig42ad4462018-08-14 08:55:23 -030076uint8_t flash_area_erased_val(const struct flash_area *fap)
77{
78 (void)fap;
Fabio Utzigcea90f92018-09-19 08:12:46 -030079 return ERASED_VAL;
80}
81
82int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
83 void *dst, uint32_t len)
84{
85 uint8_t i;
86 uint8_t *u8dst;
87 int rc;
88
Andrzej Puzdrowski5f81b122018-10-09 12:18:49 +020089 rc = flash_area_read(fa, off, dst, len);
Fabio Utzigcea90f92018-09-19 08:12:46 -030090 if (rc) {
91 return -1;
92 }
93
94 for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
95 if (u8dst[i] != ERASED_VAL) {
96 return 0;
97 }
98 }
99
100 return 1;
Fabio Utzig42ad4462018-08-14 08:55:23 -0300101}