blob: 39989a17ade94e1ea3c2e0e37ef90c2fdb7490c3 [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
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 Santo205c8c62018-07-20 11:42:31 +020026static struct device *flash_dev;
27
28struct 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 Puzdrowskib788c712018-04-12 12:42:49 +020036int 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 */
51int flash_area_id_from_image_slot(int slot)
52{
53 return slot + FLASH_AREA_IMAGE_0;
54}
Emanuele Di Santo205c8c62018-07-20 11:42:31 +020055
56int 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 Utzig42ad4462018-08-14 08:55:23 -030070}
71
Fabio Utzigcea90f92018-09-19 08:12:46 -030072#define ERASED_VAL 0xff
Fabio Utzig42ad4462018-08-14 08:55:23 -030073uint8_t flash_area_erased_val(const struct flash_area *fap)
74{
75 (void)fap;
Fabio Utzigcea90f92018-09-19 08:12:46 -030076 return ERASED_VAL;
77}
78
79int 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 Puzdrowski5f81b122018-10-09 12:18:49 +020086 rc = flash_area_read(fa, off, dst, len);
Fabio Utzigcea90f92018-09-19 08:12:46 -030087 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 Utzig42ad4462018-08-14 08:55:23 -030098}