Zephyr API Documentation 4.4.0-rc1
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 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
137void flash_area_close(const struct flash_area *fa);
138
156{
157 return (fa != NULL && device_is_ready(fa->fa_dev));
158}
159
174int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
175 size_t len);
176
191int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
192 size_t len);
193
212int flash_area_copy(const struct flash_area *src_fa, off_t src_off,
213 const struct flash_area *dst_fa, off_t dst_off,
214 off_t len, uint8_t *buf, size_t buf_size);
215
229int flash_area_erase(const struct flash_area *fa, off_t off, size_t len);
230
252int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len);
253
265
278int flash_area_get_sectors(int fa_id, uint32_t *count,
279 struct flash_sector *sectors);
280
293int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
294
302typedef void (*flash_area_cb_t)(const struct flash_area *fa,
303 void *user_data);
304
311void flash_area_foreach(flash_area_cb_t user_cb, void *user_data);
312
321int flash_area_has_driver(const struct flash_area *fa);
322
330const struct device *flash_area_get_device(const struct flash_area *fa);
331
332#if CONFIG_FLASH_MAP_LABELS
340const char *flash_area_label(const struct flash_area *fa);
341#endif
342
353
362#define PARTITION_EXISTS(label) DT_PARTITION_EXISTS(DT_NODELABEL(label))
363
368#define FIXED_PARTITION_EXISTS(label) PARTITION_EXISTS(label)
369
377#define PARTITION_ID(label) \
378 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
379 (DT_MAPPED_PARTITION_ID(DT_NODELABEL(label))), \
380 (DT_FIXED_PARTITION_ID(DT_NODELABEL(label))))
381
383#define FIXED_PARTITION_ID(label) PARTITION_ID(label) __DEPRECATED_MACRO
384
397#define PARTITION_OFFSET(label) PARTITION_NODE_OFFSET(DT_NODELABEL(label))
398
400#define FIXED_PARTITION_OFFSET(label) PARTITION_OFFSET(label) __DEPRECATED_MACRO
401
409#define PARTITION_ADDRESS(label) PARTITION_NODE_ADDRESS(DT_NODELABEL(label))
410
412#define FIXED_PARTITION_ADDRESS(label) PARTITION_ADDRESS(label) __DEPRECATED_MACRO
413
421#define PARTITION_NODE_ADDRESS(node) \
422 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
423 (DT_MAPPED_PARTITION_ADDR(node)), \
424 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
425 (DT_FIXED_SUBPARTITION_ADDR(node)), \
426 (DT_FIXED_PARTITION_ADDR(node)))))
427
429#define FIXED_PARTITION_NODE_ADDRESS(node) PARTITION_NODE_ADDRESS(node) __DEPRECATED_MACRO
430
443#define PARTITION_NODE_OFFSET(node) \
444 COND_CASE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
445 (DT_MAPPED_PARTITION_OFFSET(node)), \
446 DT_FIXED_SUBPARTITION_EXISTS(node), \
447 (DT_PROP_BY_IDX(DT_PARENT(node), reg, 0) + DT_PROP_BY_IDX(node, reg, 0)), \
448 ((DT_PROP_BY_IDX(node, reg, 0))))
449
451#define FIXED_PARTITION_NODE_OFFSET(label) PARTITION_NODE_OFFSET(label) __DEPRECATED_MACRO
452
460#define PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
461
463#define FIXED_PARTITION_SIZE(label) PARTITION_SIZE(label) __DEPRECATED_MACRO
464
472#define PARTITION_NODE_SIZE(node) DT_REG_SIZE(node)
473
475#define FIXED_PARTITION_NODE_SIZE(node) PARTITION_NODE_SIZE(node) __DEPRECATED_MACRO
476
484#define FLASH_AREA_DEVICE(label) \
485 COND_CODE_1(DT_NODE_HAS_COMPAT(DT_NODELABEL(label), zephyr_mapped_partition), \
486 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION( \
487 DT_NODE_BY_MAPPED_PARTITION_LABEL(label)))), \
488 (DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION( \
489 DT_NODE_BY_FIXED_PARTITION_LABEL(label)))))
490
498#define PARTITION_DEVICE(label) PARTITION_NODE_DEVICE(DT_NODELABEL(label))
499
501#define FIXED_PARTITION_DEVICE(label) PARTITION_DEVICE(label) __DEPRECATED_MACRO
502
510#define PARTITION_NODE_DEVICE(node) \
511 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
512 (DEVICE_DT_GET(DT_MTD_FROM_MAPPED_PARTITION(node))), \
513 (DEVICE_DT_GET(COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
514 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
515 (DT_MTD_FROM_FIXED_PARTITION(node))))))
516
518#define FIXED_PARTITION_NODE_DEVICE(node) PARTITION_NODE_DEVICE(node) __DEPRECATED_MACRO
519
527#define PARTITION_MTD(label) PARTITION_NODE_MTD(DT_NODELABEL(label))
528
530#define FIXED_PARTITION_MTD(label) PARTITION_MTD(label) __DEPRECATED_MACRO
531
539#define PARTITION_NODE_MTD(node) \
540 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
541 (DT_MTD_FROM_MAPPED_PARTITION(node)), \
542 (COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
543 (DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
544 (DT_MTD_FROM_FIXED_PARTITION(node)))))
545
547#define FIXED_PARTITION_NODE_MTD(node) PARTITION_NODE_MTD(node) __DEPRECATED_MACRO
548
556#define PARTITION(label) PARTITION_BY_NODE(DT_NODELABEL(label))
557
559#define FIXED_PARTITION(label) PARTITION(label) __DEPRECATED_MACRO
560
568#define PARTITION_BY_NODE(node) \
569 COND_CODE_1(DT_NODE_HAS_COMPAT(node, zephyr_mapped_partition), \
570 (MAPPED_PARTITION_1(node)), \
571 (FIXED_PARTITION_1(node)))
572
574#define FIXED_PARTITION_BY_NODE(node) PARTITION_BY_NODE(node) __DEPRECATED_MACRO
575
577#define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
578#define FIXED_PARTITION_0(ord) \
579 ((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))
580
581#define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
582#define DECLARE_PARTITION_0(ord) \
583 extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
584#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
585
586/* Generate declarations */
587DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
588
589#undef DECLARE_PARTITION
590#undef DECLARE_PARTITION_0
591#undef FOR_EACH_PARTITION_TABLE
592
593#define MAPPED_PARTITION_1(node) MAPPED_PARTITION_0(DT_DEP_ORD(node))
594#define MAPPED_PARTITION_0(ord) \
595 ((const struct flash_area *)&DT_CAT(global_zephyr_mapped_partition_ORD_, ord))
596
597#define DECLARE_MAPPED_PARTITION(node) DECLARE_MAPPED_PARTITION_0(DT_DEP_ORD(node))
598#define DECLARE_MAPPED_PARTITION_0(ord) \
599 extern const struct flash_area DT_CAT(global_zephyr_mapped_partition_ORD_, ord);
600
601DT_FOREACH_STATUS_OKAY(zephyr_mapped_partition, DECLARE_MAPPED_PARTITION)
602
603#undef DECLARE_MAPPED_PARTITION
604#undef DECLARE_MAPPED_PARTITION_0
605
606#define FIXED_SUBPARTITION_1(node) FIXED_SUBPARTITION_0(DT_DEP_ORD(node))
607#define FIXED_SUBPARTITION_0(ord) \
608 ((const struct flash_area *)&DT_CAT(global_fixed_subpartition_ORD_, ord))
609
610#define DECLARE_SUBPARTITION(node) DECLARE_SUBPARTITION_0(DT_DEP_ORD(node))
611#define DECLARE_SUBPARTITION_0(ord) \
612 extern const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord);
613#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_SUBPARTITION)
614
615/* Generate declarations */
616DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)
617
618#undef DECLARE_SUBPARTITION
619#undef DECLARE_SUBPARTITION_0
620#undef FOR_EACH_SUBPARTITION_TABLE
622
623#ifdef __cplusplus
624}
625#endif
626
630
631#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:3651
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:155
int flash_area_read(const struct flash_area *fa, off_t off, void *dst, size_t len)
Read flash area data.
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:302
int flash_area_erase(const struct flash_area *fa, off_t off, size_t len)
Erase flash area.
const struct device * flash_area_get_device(const struct flash_area *fa)
Get driver for given 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.
void flash_area_close(const struct flash_area *fa)
Close flash_area.
#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