Line data Source code
1 1 : /*
2 : * Copyright (c) 2017-2024 Nordic Semiconductor ASA
3 : * Copyright (c) 2015 Runtime Inc
4 : * Copyright (c) 2023 Sensorfy B.V.
5 : *
6 : * SPDX-License-Identifier: Apache-2.0
7 : */
8 :
9 : /**
10 : * @file
11 : * @brief Public API for flash map
12 : */
13 :
14 : #ifndef ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
15 : #define ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
16 :
17 : /**
18 : * @brief Abstraction over flash partitions/areas and their drivers
19 : *
20 : * @defgroup flash_area_api flash area Interface
21 : * @since 1.11
22 : * @version 1.0.0
23 : * @ingroup storage_apis
24 : * @{
25 : */
26 :
27 : /*
28 : * This API makes it possible to operate on flash areas easily and
29 : * effectively.
30 : *
31 : * The system contains global data about flash areas. Every area
32 : * contains an ID number, offset, and length.
33 : */
34 :
35 : /**
36 : *
37 : */
38 : #include <zephyr/types.h>
39 : #include <stddef.h>
40 : #include <sys/types.h>
41 : #include <zephyr/device.h>
42 : #include <zephyr/devicetree.h>
43 :
44 : #ifdef __cplusplus
45 : extern "C" {
46 : #endif
47 :
48 : /**
49 : * @brief Flash partition
50 : *
51 : * This structure represents a fixed-size partition on a flash device.
52 : * Each partition contains one or more flash sectors.
53 : */
54 1 : struct flash_area {
55 : /** ID number */
56 1 : uint8_t fa_id;
57 0 : uint16_t pad16;
58 : /** Start offset from the beginning of the flash device */
59 1 : off_t fa_off;
60 : /** Total size */
61 1 : size_t fa_size;
62 : /** Backing flash device */
63 1 : const struct device *fa_dev;
64 : #if CONFIG_FLASH_MAP_LABELS
65 : /** Partition label if defined in DTS. Otherwise nullptr; */
66 : const char *fa_label;
67 : #endif
68 : };
69 :
70 : /**
71 : * @brief Structure for transfer flash sector boundaries
72 : *
73 : * This template is used for presentation of flash memory structure. It
74 : * consumes much less RAM than @ref flash_area
75 : */
76 1 : struct flash_sector {
77 : /** Sector offset from the beginning of the flash device */
78 1 : off_t fs_off;
79 : /** Sector size in bytes */
80 1 : size_t fs_size;
81 : };
82 :
83 : #if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY)
84 : /**
85 : * @brief Structure for verify flash region integrity
86 : *
87 : * This is used to pass data to be used to check flash integrity using SHA-256
88 : * algorithm.
89 : */
90 : struct flash_area_check {
91 : const uint8_t *match; /** 256 bits match vector */
92 : size_t clen; /** Content len to be compared */
93 : size_t off; /** Start Offset */
94 : uint8_t *rbuf; /** Temporary read buffer */
95 : size_t rblen; /** Size of read buffer */
96 : };
97 :
98 : /**
99 : * Verify flash memory length bytes integrity from a flash area. The start
100 : * point is indicated by an offset value.
101 : *
102 : * @param[in] fa Flash area
103 : * @param[in] fic Flash area check integrity data
104 : *
105 : * @return 0 on success, negative errno code on fail
106 : */
107 : int flash_area_check_int_sha256(const struct flash_area *fa,
108 : const struct flash_area_check *fac);
109 : #endif
110 :
111 : /**
112 : * @brief Retrieve partitions flash area from the flash_map.
113 : *
114 : * Function Retrieves flash_area from flash_map for given partition.
115 : *
116 : * @param[in] id ID of the flash partition.
117 : * @param[out] fa Pointer which has to reference flash_area. If
118 : * @p ID is unknown, it will be NULL on output.
119 : *
120 : * @return 0 on success, -EACCES if the flash_map is not available ,
121 : * -ENOENT if @p ID is unknown, -ENODEV if there is no driver attached
122 : * to the area.
123 : */
124 1 : int flash_area_open(uint8_t id, const struct flash_area **fa);
125 :
126 : /**
127 : * @brief Close flash_area
128 : *
129 : * Reserved for future usage and external projects compatibility reason.
130 : * Currently is NOP.
131 : *
132 : * @param[in] fa Flash area to be closed.
133 : */
134 1 : void flash_area_close(const struct flash_area *fa);
135 :
136 : /**
137 : * @brief Verify that a device assigned to flash area is ready for use.
138 : *
139 : * Indicates whether the provided flash area has a device known to be
140 : * in a state where it can be used with Flash Map API.
141 : *
142 : * This can be used with struct flash_area pointers captured from FIXED_PARTITION().
143 : * At minimum this means that the device has been successfully initialized.
144 : *
145 : * @param fa pointer to flash_area object to check.
146 : *
147 : * @retval true If the device is ready for use.
148 : * @retval false If the device is not ready for use or if a NULL pointer is
149 : * passed as flash area pointer or device pointer within flash area object
150 : * is NULL.
151 : */
152 1 : static ALWAYS_INLINE bool flash_area_device_is_ready(const struct flash_area *fa)
153 : {
154 : return (fa != NULL && device_is_ready(fa->fa_dev));
155 : }
156 :
157 : /**
158 : * @brief Read flash area data
159 : *
160 : * Read data from flash area. Area readout boundaries are asserted before read
161 : * request. API has the same limitation regard read-block alignment and size
162 : * as wrapped flash driver.
163 : *
164 : * @param[in] fa Flash area
165 : * @param[in] off Offset relative from beginning of flash area to read
166 : * @param[out] dst Buffer to store read data
167 : * @param[in] len Number of bytes to read
168 : *
169 : * @return 0 on success, negative errno code on fail.
170 : */
171 1 : int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
172 : size_t len);
173 :
174 : /**
175 : * @brief Write data to flash area
176 : *
177 : * Write data to flash area. Area write boundaries are asserted before write
178 : * request. API has the same limitation regard write-block alignment and size
179 : * as wrapped flash driver.
180 : *
181 : * @param[in] fa Flash area
182 : * @param[in] off Offset relative from beginning of flash area to write
183 : * @param[in] src Buffer with data to be written
184 : * @param[in] len Number of bytes to write
185 : *
186 : * @return 0 on success, negative errno code on fail.
187 : */
188 1 : int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
189 : size_t len);
190 :
191 : /**
192 : * @brief Erase flash area
193 : *
194 : * Erase given flash area range. Area boundaries are asserted before erase
195 : * request. API has the same limitation regard erase-block alignment and size
196 : * as wrapped flash driver.
197 : *
198 : * @param[in] fa Flash area
199 : * @param[in] off Offset relative from beginning of flash area.
200 : * @param[in] len Number of bytes to be erase
201 : *
202 : * @return 0 on success, negative errno code on fail.
203 : */
204 1 : int flash_area_erase(const struct flash_area *fa, off_t off, size_t len);
205 :
206 : /**
207 : * @brief Erase flash area or fill with erase-value
208 : *
209 : * On program-erase devices this function behaves exactly like flash_area_erase.
210 : * On RAM non-volatile device it will call erase, if driver provides such
211 : * callback, or will fill given range with erase-value defined by driver.
212 : * This function should be only used by code that has not been written
213 : * to directly support devices that do not require erase and rely on
214 : * device being erased prior to some operations.
215 : * Note that emulated erase, on devices that do not require, is done
216 : * via write, which affects endurance of device.
217 : *
218 : * @see flash_area_erase()
219 : * @see flash_flatten()
220 : *
221 : * @param[in] fa Flash area
222 : * @param[in] off Offset relative from beginning of flash area.
223 : * @param[in] len Number of bytes to be erase
224 : *
225 : * @return 0 on success, negative errno code on fail.
226 : */
227 1 : int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len);
228 :
229 : /**
230 : * @brief Get write block size of the flash area
231 : *
232 : * Currently write block size might be treated as read block size, although
233 : * most of drivers supports unaligned readout.
234 : *
235 : * @param[in] fa Flash area
236 : *
237 : * @return Alignment restriction for flash writes in [B].
238 : */
239 1 : uint32_t flash_area_align(const struct flash_area *fa);
240 :
241 : /**
242 : * Retrieve info about sectors within the area.
243 : *
244 : * @param[in] fa_id Given flash area ID
245 : * @param[out] sectors buffer for sectors data
246 : * @param[in,out] count On input Capacity of @p sectors, on output number of
247 : * sectors Retrieved.
248 : *
249 : * @return 0 on success, negative errno code on fail. Especially returns
250 : * -ENOMEM if There are too many flash pages on the flash_area to fit in the
251 : * array.
252 : */
253 1 : int flash_area_get_sectors(int fa_id, uint32_t *count,
254 : struct flash_sector *sectors);
255 :
256 : /**
257 : * Retrieve info about sectors within the area.
258 : *
259 : * @param[in] fa pointer to flash area object.
260 : * @param[out] sectors buffer for sectors data
261 : * @param[in,out] count On input Capacity of @p sectors, on output number of
262 : * sectors retrieved.
263 : *
264 : * @return 0 on success, negative errno code on fail. Especially returns
265 : * -ENOMEM if There are too many flash pages on the flash_area to fit in the
266 : * array.
267 : */
268 1 : int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
269 :
270 : /**
271 : * Flash map iteration callback
272 : *
273 : * @param fa flash area
274 : * @param user_data User supplied data
275 : *
276 : */
277 1 : typedef void (*flash_area_cb_t)(const struct flash_area *fa,
278 : void *user_data);
279 :
280 : /**
281 : * Iterate over flash map
282 : *
283 : * @param user_cb User callback
284 : * @param user_data User supplied data
285 : */
286 1 : void flash_area_foreach(flash_area_cb_t user_cb, void *user_data);
287 :
288 : /**
289 : * Check whether given flash area has supporting flash driver
290 : * in the system.
291 : *
292 : * @param[in] fa Flash area.
293 : *
294 : * @return 1 On success. -ENODEV if no driver match.
295 : */
296 1 : int flash_area_has_driver(const struct flash_area *fa);
297 :
298 : /**
299 : * Get driver for given flash area.
300 : *
301 : * @param[in] fa Flash area.
302 : *
303 : * @return device driver.
304 : */
305 1 : const struct device *flash_area_get_device(const struct flash_area *fa);
306 :
307 : #if CONFIG_FLASH_MAP_LABELS
308 : /**
309 : * Get the label property from the device tree
310 : *
311 : * @param[in] fa Flash area.
312 : *
313 : * @return The label property if it is defined, otherwise NULL
314 : */
315 : const char *flash_area_label(const struct flash_area *fa);
316 : #endif
317 :
318 : /**
319 : * Get the value expected to be read when accessing any erased
320 : * flash byte.
321 : * This API is compatible with the MCUBoot's porting layer.
322 : *
323 : * @param fa Flash area.
324 : *
325 : * @return Byte value of erase memory.
326 : */
327 1 : uint8_t flash_area_erased_val(const struct flash_area *fa);
328 :
329 : /**
330 : * Returns non-0 value if fixed-partition of given DTS node label exists.
331 : *
332 : * @param label DTS node label
333 : *
334 : * @return non-0 if fixed-partition node exists and is enabled;
335 : * 0 if node does not exist, is not enabled or is not fixed-partition.
336 : */
337 1 : #define FIXED_PARTITION_EXISTS(label) DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label))
338 :
339 : /**
340 : * Get flash area ID from fixed-partition DTS node label
341 : *
342 : * @param label DTS node label of a partition
343 : *
344 : * @return flash area ID
345 : */
346 1 : #define FIXED_PARTITION_ID(label) DT_FIXED_PARTITION_ID(DT_NODELABEL(label))
347 :
348 : /**
349 : * Get fixed-partition offset from DTS node label
350 : *
351 : * @param label DTS node label of a partition
352 : *
353 : * @return fixed-partition offset, as defined for the partition in DTS.
354 : */
355 1 : #define FIXED_PARTITION_OFFSET(label) DT_REG_ADDR(DT_NODELABEL(label))
356 :
357 : /**
358 : * Get fixed-partition offset from DTS node
359 : *
360 : * @param node DTS node of a partition
361 : *
362 : * @return fixed-partition offset, as defined for the partition in DTS.
363 : */
364 1 : #define FIXED_PARTITION_NODE_OFFSET(node) DT_REG_ADDR(node)
365 :
366 : /**
367 : * Get fixed-partition size for DTS node label
368 : *
369 : * @param label DTS node label
370 : *
371 : * @return fixed-partition offset, as defined for the partition in DTS.
372 : */
373 1 : #define FIXED_PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
374 :
375 : /**
376 : * Get fixed-partition size for DTS node
377 : *
378 : * @param node DTS node of a partition
379 : *
380 : * @return fixed-partition size, as defined for the partition in DTS.
381 : */
382 1 : #define FIXED_PARTITION_NODE_SIZE(node) DT_REG_SIZE(node)
383 :
384 : /**
385 : * Get device pointer for device the area/partition resides on
386 : *
387 : * @param label DTS node label of a partition
388 : *
389 : * @return const struct device type pointer
390 : */
391 1 : #define FLASH_AREA_DEVICE(label) \
392 : DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODE_BY_FIXED_PARTITION_LABEL(label)))
393 :
394 : /**
395 : * Get device pointer for device the area/partition resides on
396 : *
397 : * @param label DTS node label of a partition
398 : *
399 : * @return Pointer to a device.
400 : */
401 1 : #define FIXED_PARTITION_DEVICE(label) \
402 : DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))
403 :
404 : /**
405 : * Get device pointer for device the area/partition resides on
406 : *
407 : * @param node DTS node of a partition
408 : *
409 : * @return Pointer to a device.
410 : */
411 1 : #define FIXED_PARTITION_NODE_DEVICE(node) \
412 : DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(node))
413 :
414 : /**
415 : * Get pointer to flash_area object by partition label
416 : *
417 : * @param label DTS node label of a partition
418 : *
419 : * @return Pointer to flash_area type object representing partition
420 : */
421 1 : #define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label))
422 :
423 : /**
424 : * Get pointer to flash_area object by partition node in DTS
425 : *
426 : * @param node DTS node of a partition
427 : *
428 : * @return Pointer to flash_area type object representing partition
429 : */
430 1 : #define FIXED_PARTITION_BY_NODE(node) FIXED_PARTITION_1(node)
431 :
432 : /** @cond INTERNAL_HIDDEN */
433 : #define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
434 : #define FIXED_PARTITION_0(ord) \
435 : ((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))
436 :
437 : #define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
438 : #define DECLARE_PARTITION_0(ord) \
439 : extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
440 : #define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
441 :
442 : /* Generate declarations */
443 : DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
444 :
445 : #undef DECLARE_PARTITION
446 : #undef DECLARE_PARTITION_0
447 : #undef FOR_EACH_PARTITION_TABLE
448 : /** @endcond */
449 :
450 : #ifdef __cplusplus
451 : }
452 : #endif
453 :
454 : /**
455 : * @}
456 : */
457 :
458 : #endif /* ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_ */
|