1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  * Copyright (c) 2020-2021 Arm Limited.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 /*
10  * Original code taken from mcuboot project at:
11  * https://github.com/mcu-tools/mcuboot
12  * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
13  */
14 
15 #ifndef __FLASH_MAP_BACKEND_H__
16 #define __FLASH_MAP_BACKEND_H__
17 
18 #include "flash_map/flash_map.h"
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26  * Provides abstraction of flash regions for type of use.
27  *
28  * System will contain a map which contains flash areas. Every
29  * region will contain flash identifier, offset within flash and length.
30  */
31 
32 /*
33  * Retrieve a memory-mapped flash device's base address.
34  *
35  * On success, the address will be stored in the value pointed to by
36  * ret.
37  *
38  * Returns 0 on success, or an error code on failure.
39  */
40 int flash_device_base(uint8_t fd_id, uintptr_t *ret);
41 
42 int flash_area_id_from_image_slot(int slot);
43 int flash_area_id_from_multi_image_slot(int image_index, int slot);
44 
45 /**
46  * Converts the specified flash area ID to an image slot index.
47  *
48  * Returns image slot index (0 or 1), or -1 if ID doesn't correspond to an image
49  * slot.
50  */
51 int flash_area_id_to_image_slot(int area_id);
52 
53 /**
54  * Converts the specified flash area ID and image index (in multi-image setup)
55  * to an image slot index.
56  *
57  * Returns image slot index (0 or 1), or -1 if ID doesn't correspond to an image
58  * slot.
59  */
60 int flash_area_id_to_multi_image_slot(int image_index, int area_id);
61 
62 /*
63  * Returns the value expected to be read when accessing any erased
64  * flash byte.
65  */
66 uint8_t flash_area_erased_val(const struct flash_area *fap);
67 
68 /*
69  * Reads len bytes from off, and checks if the read data is erased.
70  *
71  * Returns 1 if erased, 0 if non-erased, and -1 on failure.
72  */
73 int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
74         void *dst, uint32_t len);
75 
flash_area_get_id(const struct flash_area * fa)76 static inline uint8_t flash_area_get_id(const struct flash_area *fa)
77 {
78     return fa->fa_id;
79 }
80 
flash_area_get_device_id(const struct flash_area * fa)81 static inline uint8_t flash_area_get_device_id(const struct flash_area *fa)
82 {
83     return fa->fa_device_id;
84 }
85 
flash_area_get_off(const struct flash_area * fa)86 static inline uint32_t flash_area_get_off(const struct flash_area *fa)
87 {
88     return fa->fa_off;
89 }
90 
flash_area_get_size(const struct flash_area * fa)91 static inline uint32_t flash_area_get_size(const struct flash_area *fa)
92 {
93     return fa->fa_size;
94 }
95 
flash_sector_get_off(const struct flash_sector * fs)96 static inline uint32_t flash_sector_get_off(const struct flash_sector *fs)
97 {
98     return fs->fs_off;
99 }
100 
flash_sector_get_size(const struct flash_sector * fs)101 static inline uint32_t flash_sector_get_size(const struct flash_sector *fs)
102 {
103     return fs->fs_size;
104 }
105 
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif /* __FLASH_MAP_BACKEND_H__ */
112