Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
flash_map.h
Go to the documentation of this file.
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
13
14#ifndef ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
15#define ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
16
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
38#include <zephyr/types.h>
39#include <stddef.h>
40#include <sys/types.h>
41#include <zephyr/device.h>
42#include <zephyr/devicetree.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
57struct flash_area {
64 size_t fa_size;
66 const struct device *fa_dev;
67#if defined(CONFIG_FLASH_MAP_LABELS)
69 const char *fa_label;
70#endif
71};
72
85 ptrdiff_t fs_off;
87 size_t fs_size;
88};
89
90#if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY)
97struct flash_area_check {
98 const uint8_t *match;
99 size_t clen;
100 size_t off;
101 uint8_t *rbuf;
102 size_t rblen;
103};
104
114int flash_area_check_int_sha256(const struct flash_area *fa,
115 const struct flash_area_check *fac);
116#endif
117
131int flash_area_open(uint8_t id, const struct flash_area **fa);
132
141static inline void flash_area_close(__unused const struct flash_area *fa)
142{
143 /* nothing to do for now */
144}
145
163{
164 return (fa != NULL && device_is_ready(fa->fa_dev));
165}
166
181int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
182 size_t len);
183
198int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
199 size_t len);
200
219int flash_area_copy(const struct flash_area *src_fa, off_t src_off,
220 const struct flash_area *dst_fa, off_t dst_off,
221 off_t len, uint8_t *buf, size_t buf_size);
222
236int flash_area_erase(const struct flash_area *fa, off_t off, size_t len);
237
259int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len);
260
272
285int flash_area_get_sectors(int fa_id, uint32_t *count,
286 struct flash_sector *sectors);
287
300int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
301
309typedef void (*flash_area_cb_t)(const struct flash_area *fa,
310 void *user_data);
311
318void flash_area_foreach(flash_area_cb_t user_cb, void *user_data);
319
328int flash_area_has_driver(const struct flash_area *fa);
329
337static inline const struct device *flash_area_get_device(const struct flash_area *fa)
338{
339 return fa->fa_dev;
340}
341
349static inline const char *flash_area_label(const struct flash_area *fa)
350{
351#if defined(CONFIG_FLASH_MAP_LABELS)
352 return fa->fa_label;
353#else /* CONFIG_FLASH_MAP_LABELS */
354 ARG_UNUSED(fa);
355 return NULL;
356#endif /* CONFIG_FLASH_MAP_LABELS */
357}
358
369
378#define PARTITION_EXISTS(label) DT_PARTITION_EXISTS(DT_NODELABEL(label))
379
384#define FIXED_PARTITION_EXISTS(label) PARTITION_EXISTS(label)
385
393#define PARTITION_ID(label) \
394 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
395 (DT_MAPPED_PARTITION_ID(DT_NODELABEL(label))), \
396 (DT_FIXED_PARTITION_ID(DT_NODELABEL(label))))
397
399#define FIXED_PARTITION_ID(label) PARTITION_ID(label) __DEPRECATED_MACRO
400
413#define PARTITION_OFFSET(label) PARTITION_NODE_OFFSET(DT_NODELABEL(label))
414
416#define FIXED_PARTITION_OFFSET(label) PARTITION_OFFSET(label) __DEPRECATED_MACRO
417
425#define PARTITION_ADDRESS(label) PARTITION_NODE_ADDRESS(DT_NODELABEL(label))
426
428#define FIXED_PARTITION_ADDRESS(label) PARTITION_ADDRESS(label) __DEPRECATED_MACRO
429
437#define PARTITION_NODE_ADDRESS(node) \
438 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
439 (DT_MAPPED_PARTITION_ADDR(node)), \
440 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
441 (DT_FIXED_SUBPARTITION_ADDR(node)), \
442 (DT_FIXED_PARTITION_ADDR(node)))))
443
445#define FIXED_PARTITION_NODE_ADDRESS(node) PARTITION_NODE_ADDRESS(node) __DEPRECATED_MACRO
446
459#define PARTITION_NODE_OFFSET(node) \
460 COND_CASE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
461 (DT_MAPPED_PARTITION_OFFSET(node)), \
462 DT_FIXED_SUBPARTITION_EXISTS(node), \
463 (DT_PROP_BY_IDX(DT_PARENT(node), reg, 0) + DT_PROP_BY_IDX(node, reg, 0)), \
464 ((DT_PROP_BY_IDX(node, reg, 0))))
465
467#define FIXED_PARTITION_NODE_OFFSET(label) PARTITION_NODE_OFFSET(label) __DEPRECATED_MACRO
468
476#define PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
477
479#define FIXED_PARTITION_SIZE(label) PARTITION_SIZE(label) __DEPRECATED_MACRO
480
488#define PARTITION_NODE_SIZE(node) DT_REG_SIZE(node)
489
491#define FIXED_PARTITION_NODE_SIZE(node) PARTITION_NODE_SIZE(node) __DEPRECATED_MACRO
492
500#define FLASH_AREA_DEVICE(label) \
501 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
502 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION( \
503 DT_NODE_BY_MAPPED_PARTITION_LABEL(label)))), \
504 (DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION( \
505 DT_NODE_BY_FIXED_PARTITION_LABEL(label)))))
506
514#define PARTITION_DEVICE(label) PARTITION_NODE_DEVICE(DT_NODELABEL(label))
515
517#define FIXED_PARTITION_DEVICE(label) PARTITION_DEVICE(label) __DEPRECATED_MACRO
518
526#define PARTITION_NODE_DEVICE(node) \
527 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
528 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION(node))), \
529 (DEVICE_DT_GET(COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
530 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
531 (DT_MTD_FROM_FIXED_PARTITION(node))))))
532
534#define FIXED_PARTITION_NODE_DEVICE(node) PARTITION_NODE_DEVICE(node) __DEPRECATED_MACRO
535
543#define PARTITION_MTD(label) PARTITION_NODE_MTD(DT_NODELABEL(label))
544
546#define FIXED_PARTITION_MTD(label) PARTITION_MTD(label) __DEPRECATED_MACRO
547
555#define PARTITION_NODE_MTD(node) \
556 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
557 (DT_MTD_FROM_MAPPED_PARTITION(node)), \
558 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
559 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
560 (DT_MTD_FROM_FIXED_PARTITION(node)))))
561
563#define FIXED_PARTITION_NODE_MTD(node) PARTITION_NODE_MTD(node) __DEPRECATED_MACRO
564
572#define PARTITION(label) PARTITION_BY_NODE(DT_NODELABEL(label))
573
575#define FIXED_PARTITION(label) PARTITION(label) __DEPRECATED_MACRO
576
584#define PARTITION_BY_NODE(node) \
585 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
586 (MAPPED_PARTITION_1(node)), \
587 (FIXED_PARTITION_1(node)))
588
590#define FIXED_PARTITION_BY_NODE(node) PARTITION_BY_NODE(node) __DEPRECATED_MACRO
591
593#define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
594#define FIXED_PARTITION_0(ord) \
595 ((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))
596
597#define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
598#define DECLARE_PARTITION_0(ord) \
599 extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
600#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
601
602/* Generate declarations */
603DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
604
605#undef DECLARE_PARTITION
606#undef DECLARE_PARTITION_0
607#undef FOR_EACH_PARTITION_TABLE
608
609#define MAPPED_PARTITION_1(node) MAPPED_PARTITION_0(DT_DEP_ORD(node))
610#define MAPPED_PARTITION_0(ord) \
611 ((const struct flash_area *)&DT_CAT(global_zephyr_mapped_partition_ORD_, ord))
612
613#define DECLARE_MAPPED_PARTITION(node) DECLARE_MAPPED_PARTITION_0(DT_DEP_ORD(node))
614#define DECLARE_MAPPED_PARTITION_0(ord) \
615 extern const struct flash_area DT_CAT(global_zephyr_mapped_partition_ORD_, ord);
616
617DT_FOREACH_STATUS_OKAY(zephyr_mapped_partition, DECLARE_MAPPED_PARTITION)
618
619#undef DECLARE_MAPPED_PARTITION
620#undef DECLARE_MAPPED_PARTITION_0
621
622#define FIXED_SUBPARTITION_1(node) FIXED_SUBPARTITION_0(DT_DEP_ORD(node))
623#define FIXED_SUBPARTITION_0(ord) \
624 ((const struct flash_area *)&DT_CAT(global_fixed_subpartition_ORD_, ord))
625
626#define DECLARE_SUBPARTITION(node) DECLARE_SUBPARTITION_0(DT_DEP_ORD(node))
627#define DECLARE_SUBPARTITION_0(ord) \
628 extern const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord);
629#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_SUBPARTITION)
630
631/* Generate declarations */
632DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)
633
634#undef DECLARE_SUBPARTITION
635#undef DECLARE_SUBPARTITION_0
636#undef FOR_EACH_SUBPARTITION_TABLE
638
639#ifdef __cplusplus
640}
641#endif
642
646
647#endif /* ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_ */
irp nz macro MOVR cc s mov cc s endm endr irp aw macro LDR aa off
Definition asm-macro-32-bit-gnu.h:20
APIs and macros for the Zephyr device model.
Flash Devicetree macro public API header file, for partitions.
Devicetree main header.
Flash Devicetree macro public API header file.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
#define DT_FOREACH_STATUS_OKAY(compat, fn)
Invokes fn for each status okay node of a compatible.
Definition devicetree.h:4059
int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors)
Retrieve info about sectors within the area.
uint32_t flash_area_align(const struct flash_area *fa)
Get write block size of the flash area.
uint8_t flash_area_erased_val(const struct flash_area *fa)
Get the value expected to be read when accessing any erased flash byte.
void flash_area_foreach(flash_area_cb_t user_cb, void *user_data)
Iterate over flash map.
int flash_area_has_driver(const struct flash_area *fa)
Check whether given flash area has supporting flash driver in the system.
int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors)
Retrieve info about sectors within the area.
int flash_area_open(uint8_t id, const struct flash_area **fa)
Retrieve partitions flash area from the flash_map.
static ALWAYS_INLINE bool flash_area_device_is_ready(const struct flash_area *fa)
Verify that a device assigned to flash area is ready for use.
Definition flash_map.h:162
int flash_area_read(const struct flash_area *fa, off_t off, void *dst, size_t len)
Read flash area data.
static void flash_area_close(__unused const struct flash_area *fa)
Close flash_area.
Definition flash_map.h:141
static const struct device * flash_area_get_device(const struct flash_area *fa)
Get driver for given flash area.
Definition flash_map.h:337
static const char * flash_area_label(const struct flash_area *fa)
Get the label property from the device tree.
Definition flash_map.h:349
int flash_area_copy(const struct flash_area *src_fa, off_t src_off, const struct flash_area *dst_fa, off_t dst_off, off_t len, uint8_t *buf, size_t buf_size)
Copy flash memory from one flash area to another.
int flash_area_write(const struct flash_area *fa, off_t off, const void *src, size_t len)
Write data to flash area.
void(* flash_area_cb_t)(const struct flash_area *fa, void *user_data)
Flash map iteration callback.
Definition flash_map.h:309
int flash_area_erase(const struct flash_area *fa, off_t off, size_t len)
Erase flash area.
int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len)
Erase flash area or fill with erase-value.
#define NULL
Definition iar_missing_defs.h:20
#define ALWAYS_INLINE
Definition common.h:186
__INTPTR_TYPE__ off_t
Definition types.h:36
Flash Devicetree macro public API header file, for memory-mapped partitions.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
Flash partition.
Definition flash_map.h:57
off_t fa_off
Start offset from the beginning of the flash device.
Definition flash_map.h:62
size_t fa_size
Total size.
Definition flash_map.h:64
const struct device * fa_dev
Backing flash device.
Definition flash_map.h:66
uint8_t fa_id
ID number.
Definition flash_map.h:59
uint16_t pad16
Definition flash_map.h:60
Structure for transfer flash sector boundaries.
Definition flash_map.h:83
ptrdiff_t fs_off
Sector offset from the beginning of the flash device.
Definition flash_map.h:85
size_t fs_size
Sector size in bytes.
Definition flash_map.h:87