blob: b7fa610d4f2d3684ca499f61e2fe633f81d47ed3 [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
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053018#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020019#define FLASH_DEVICE_ID SOC_FLASH_0_ID
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053020#elif (defined(CONFIG_XTENSA) && defined(DT_SPI_NOR_DRV_NAME))
21#define FLASH_DEVICE_ID SPI_FLASH_0_ID
22#else
23#error "FLASH_DEVICE_ID could not be determined"
24#endif
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020025
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053026#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
Emanuele Di Santo205c8c62018-07-20 11:42:31 +020027static struct device *flash_dev;
28
29struct device *flash_device_get_binding(char *dev_name)
30{
31 if (!flash_dev) {
32 flash_dev = device_get_binding(dev_name);
33 }
34 return flash_dev;
35}
36
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020037int flash_device_base(uint8_t fd_id, uintptr_t *ret)
38{
39 if (fd_id != FLASH_DEVICE_ID) {
40 BOOT_LOG_ERR("invalid flash ID %d; expected %d",
41 fd_id, FLASH_DEVICE_ID);
42 return -EINVAL;
43 }
44 *ret = FLASH_DEVICE_BASE;
45 return 0;
46}
47
48/*
49 * This depends on the mappings defined in sysflash.h, and assumes
50 * that slot 0, slot 1, and the scratch areas are contiguous.
51 */
52int flash_area_id_from_image_slot(int slot)
53{
54 return slot + FLASH_AREA_IMAGE_0;
55}
Emanuele Di Santo205c8c62018-07-20 11:42:31 +020056
57int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
58{
59 int rc;
60 struct flash_pages_info page;
61
62 rc = flash_get_page_info_by_offs(flash_dev, off, &page);
63 if (rc) {
64 return rc;
65 }
66
67 sector->fs_off = page.start_offset;
68 sector->fs_size = page.size;
69
70 return rc;
Fabio Utzig42ad4462018-08-14 08:55:23 -030071}
72
Fabio Utzigcea90f92018-09-19 08:12:46 -030073#define ERASED_VAL 0xff
Fabio Utzig42ad4462018-08-14 08:55:23 -030074uint8_t flash_area_erased_val(const struct flash_area *fap)
75{
76 (void)fap;
Fabio Utzigcea90f92018-09-19 08:12:46 -030077 return ERASED_VAL;
78}
79
80int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
81 void *dst, uint32_t len)
82{
83 uint8_t i;
84 uint8_t *u8dst;
85 int rc;
86
Andrzej Puzdrowski5f81b122018-10-09 12:18:49 +020087 rc = flash_area_read(fa, off, dst, len);
Fabio Utzigcea90f92018-09-19 08:12:46 -030088 if (rc) {
89 return -1;
90 }
91
92 for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
93 if (u8dst[i] != ERASED_VAL) {
94 return 0;
95 }
96 }
97
98 return 1;
Fabio Utzig42ad4462018-08-14 08:55:23 -030099}