blob: 7133c99b66499f6fc05afe81061c5c6db8fa139a [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;
70}