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
83 size_t fs_size;
84};
85
86#if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY)
93struct flash_area_check {
94 const uint8_t *match;
95 size_t clen;
96 size_t off;
97 uint8_t *rbuf;
98 size_t rblen;
99};
100
110int flash_area_check_int_sha256(const struct flash_area *fa,
111 const struct flash_area_check *fac);
112#endif
113
127int flash_area_open(uint8_t id, const struct flash_area **fa);
128
137static inline void flash_area_close(__unused const struct flash_area *fa)
138{
139 /* nothing to do for now */
140}
141
159{
160 return (fa != NULL && device_is_ready(fa->fa_dev));
161}
162
177int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
178 size_t len);
179
194int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
195 size_t len);
196
215int flash_area_copy(const struct flash_area *src_fa, off_t src_off,
216 const struct flash_area *dst_fa, off_t dst_off,
217 off_t len, uint8_t *buf, size_t buf_size);
218
232int flash_area_erase(const struct flash_area *fa, off_t off, size_t len);
233
255int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len);
256
268
281int flash_area_get_sectors(int fa_id, uint32_t *count,
282 struct flash_sector *sectors);
283
296int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
297
305typedef void (*flash_area_cb_t)(const struct flash_area *fa,
306 void *user_data);
307
314void flash_area_foreach(flash_area_cb_t user_cb, void *user_data);
315
324int flash_area_has_driver(const struct flash_area *fa);
325
333static inline const struct device *flash_area_get_device(const struct flash_area *fa)
334{
335 return fa->fa_dev;
336}
337
345static inline const char *flash_area_label(const struct flash_area *fa)
346{
347#if defined(CONFIG_FLASH_MAP_LABELS)
348 return fa->fa_label;
349#else /* CONFIG_FLASH_MAP_LABELS */
350 ARG_UNUSED(fa);
351 return NULL;
352#endif /* CONFIG_FLASH_MAP_LABELS */
353}
354
365
374#define PARTITION_EXISTS(label) DT_PARTITION_EXISTS(DT_NODELABEL(label))
375
380#define FIXED_PARTITION_EXISTS(label) PARTITION_EXISTS(label)
381
389#define PARTITION_ID(label) \
390 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
391 (DT_MAPPED_PARTITION_ID(DT_NODELABEL(label))), \
392 (DT_FIXED_PARTITION_ID(DT_NODELABEL(label))))
393
395#define FIXED_PARTITION_ID(label) PARTITION_ID(label) __DEPRECATED_MACRO
396
409#define PARTITION_OFFSET(label) PARTITION_NODE_OFFSET(DT_NODELABEL(label))
410
412#define FIXED_PARTITION_OFFSET(label) PARTITION_OFFSET(label) __DEPRECATED_MACRO
413
421#define PARTITION_ADDRESS(label) PARTITION_NODE_ADDRESS(DT_NODELABEL(label))
422
424#define FIXED_PARTITION_ADDRESS(label) PARTITION_ADDRESS(label) __DEPRECATED_MACRO
425
433#define PARTITION_NODE_ADDRESS(node) \
434 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
435 (DT_MAPPED_PARTITION_ADDR(node)), \
436 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
437 (DT_FIXED_SUBPARTITION_ADDR(node)), \
438 (DT_FIXED_PARTITION_ADDR(node)))))
439
441#define FIXED_PARTITION_NODE_ADDRESS(node) PARTITION_NODE_ADDRESS(node) __DEPRECATED_MACRO
442
455#define PARTITION_NODE_OFFSET(node) \
456 COND_CASE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
457 (DT_MAPPED_PARTITION_OFFSET(node)), \
458 DT_FIXED_SUBPARTITION_EXISTS(node), \
459 (DT_PROP_BY_IDX(DT_PARENT(node), reg, 0) + DT_PROP_BY_IDX(node, reg, 0)), \
460 ((DT_PROP_BY_IDX(node, reg, 0))))
461
463#define FIXED_PARTITION_NODE_OFFSET(label) PARTITION_NODE_OFFSET(label) __DEPRECATED_MACRO
464
472#define PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
473
475#define FIXED_PARTITION_SIZE(label) PARTITION_SIZE(label) __DEPRECATED_MACRO
476
484#define PARTITION_NODE_SIZE(node) DT_REG_SIZE(node)
485
487#define FIXED_PARTITION_NODE_SIZE(node) PARTITION_NODE_SIZE(node) __DEPRECATED_MACRO
488
496#define FLASH_AREA_DEVICE(label) \
497 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
498 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION( \
499 DT_NODE_BY_MAPPED_PARTITION_LABEL(label)))), \
500 (DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION( \
501 DT_NODE_BY_FIXED_PARTITION_LABEL(label)))))
502
510#define PARTITION_DEVICE(label) PARTITION_NODE_DEVICE(DT_NODELABEL(label))
511
513#define FIXED_PARTITION_DEVICE(label) PARTITION_DEVICE(label) __DEPRECATED_MACRO
514
522#define PARTITION_NODE_DEVICE(node) \
523 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
524 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION(node))), \
525 (DEVICE_DT_GET(COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
526 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
527 (DT_MTD_FROM_FIXED_PARTITION(node))))))
528
530#define FIXED_PARTITION_NODE_DEVICE(node) PARTITION_NODE_DEVICE(node) __DEPRECATED_MACRO
531
539#define PARTITION_MTD(label) PARTITION_NODE_MTD(DT_NODELABEL(label))
540
542#define FIXED_PARTITION_MTD(label) PARTITION_MTD(label) __DEPRECATED_MACRO
543
551#define PARTITION_NODE_MTD(node) \
552 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
553 (DT_MTD_FROM_MAPPED_PARTITION(node)), \
554 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
555 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
556 (DT_MTD_FROM_FIXED_PARTITION(node)))))
557
559#define FIXED_PARTITION_NODE_MTD(node) PARTITION_NODE_MTD(node) __DEPRECATED_MACRO
560
568#define PARTITION(label) PARTITION_BY_NODE(DT_NODELABEL(label))
569
571#define FIXED_PARTITION(label) PARTITION(label) __DEPRECATED_MACRO
572
580#define PARTITION_BY_NODE(node) \
581 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
582 (MAPPED_PARTITION_1(node)), \
583 (FIXED_PARTITION_1(node)))
584
586#define FIXED_PARTITION_BY_NODE(node) PARTITION_BY_NODE(node) __DEPRECATED_MACRO
587
589#define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
590#define FIXED_PARTITION_0(ord) \
591 ((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))
592
593#define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
594#define DECLARE_PARTITION_0(ord) \
595 extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
596#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
597
598/* Generate declarations */
599DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
600
601#undef DECLARE_PARTITION
602#undef DECLARE_PARTITION_0
603#undef FOR_EACH_PARTITION_TABLE
604
605#define MAPPED_PARTITION_1(node) MAPPED_PARTITION_0(DT_DEP_ORD(node))
606#define MAPPED_PARTITION_0(ord) \
607 ((const struct flash_area *)&DT_CAT(global_zephyr_mapped_partition_ORD_, ord))
608
609#define DECLARE_MAPPED_PARTITION(node) DECLARE_MAPPED_PARTITION_0(DT_DEP_ORD(node))
610#define DECLARE_MAPPED_PARTITION_0(ord) \
611 extern const struct flash_area DT_CAT(global_zephyr_mapped_partition_ORD_, ord);
612
613DT_FOREACH_STATUS_OKAY(zephyr_mapped_partition, DECLARE_MAPPED_PARTITION)
614
615#undef DECLARE_MAPPED_PARTITION
616#undef DECLARE_MAPPED_PARTITION_0
617
618#define FIXED_SUBPARTITION_1(node) FIXED_SUBPARTITION_0(DT_DEP_ORD(node))
619#define FIXED_SUBPARTITION_0(ord) \
620 ((const struct flash_area *)&DT_CAT(global_fixed_subpartition_ORD_, ord))
621
622#define DECLARE_SUBPARTITION(node) DECLARE_SUBPARTITION_0(DT_DEP_ORD(node))
623#define DECLARE_SUBPARTITION_0(ord) \
624 extern const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord);
625#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_SUBPARTITION)
626
627/* Generate declarations */
628DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)
629
630#undef DECLARE_SUBPARTITION
631#undef DECLARE_SUBPARTITION_0
632#undef FOR_EACH_SUBPARTITION_TABLE
634
635#ifdef __cplusplus
636}
637#endif
638
642
643#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:17
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:3675
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:158
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:137
static const struct device * flash_area_get_device(const struct flash_area *fa)
Get driver for given flash area.
Definition flash_map.h:333
static const char * flash_area_label(const struct flash_area *fa)
Get the label property from the device tree.
Definition flash_map.h:345
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:305
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:161
__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:513
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:79
off_t fs_off
Sector offset from the beginning of the flash device.
Definition flash_map.h:81
size_t fs_size
Sector size in bytes.
Definition flash_map.h:83