Zephyr API Documentation 4.0.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
14#ifndef ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
15#define ZEPHYR_INCLUDE_STORAGE_FLASH_MAP_H_
16
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>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
54struct flash_area {
61 size_t fa_size;
63 const struct device *fa_dev;
64#if CONFIG_FLASH_MAP_LABELS
66 const char *fa_label;
67#endif
68};
69
80 size_t fs_size;
81};
82
83#if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY)
90struct flash_area_check {
91 const uint8_t *match;
92 size_t clen;
93 size_t off;
94 uint8_t *rbuf;
95 size_t rblen;
96};
97
107int flash_area_check_int_sha256(const struct flash_area *fa,
108 const struct flash_area_check *fac);
109#endif
110
124int flash_area_open(uint8_t id, const struct flash_area **fa);
125
134void flash_area_close(const struct flash_area *fa);
135
153{
154 return (fa != NULL && device_is_ready(fa->fa_dev));
155}
156
171int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
172 size_t len);
173
188int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
189 size_t len);
190
204int flash_area_erase(const struct flash_area *fa, off_t off, size_t len);
205
227int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len);
228
240
253int flash_area_get_sectors(int fa_id, uint32_t *count,
254 struct flash_sector *sectors);
255
268int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
269
277typedef void (*flash_area_cb_t)(const struct flash_area *fa,
278 void *user_data);
279
286void flash_area_foreach(flash_area_cb_t user_cb, void *user_data);
287
296int flash_area_has_driver(const struct flash_area *fa);
297
305const struct device *flash_area_get_device(const struct flash_area *fa);
306
307#if CONFIG_FLASH_MAP_LABELS
315const char *flash_area_label(const struct flash_area *fa);
316#endif
317
328
337#define FIXED_PARTITION_EXISTS(label) DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label))
338
346#define FIXED_PARTITION_ID(label) DT_FIXED_PARTITION_ID(DT_NODELABEL(label))
347
355#define FIXED_PARTITION_OFFSET(label) DT_REG_ADDR(DT_NODELABEL(label))
356
364#define FIXED_PARTITION_NODE_OFFSET(node) DT_REG_ADDR(node)
365
373#define FIXED_PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
374
382#define FIXED_PARTITION_NODE_SIZE(node) DT_REG_SIZE(node)
383
391#define FLASH_AREA_DEVICE(label) \
392 DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODE_BY_FIXED_PARTITION_LABEL(label)))
393
401#define FIXED_PARTITION_DEVICE(label) \
402 DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))
403
411#define FIXED_PARTITION_NODE_DEVICE(node) \
412 DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(node))
413
421#define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label))
422
430#define FIXED_PARTITION_BY_NODE(node) FIXED_PARTITION_1(node)
431
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 */
443DT_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
450#ifdef __cplusplus
451}
452#endif
453
458#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
Devicetree main header.
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:3426
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:152
int flash_area_read(const struct flash_area *fa, off_t off, void *dst, size_t len)
Read flash area data.
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:277
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 ALWAYS_INLINE
Definition common.h:129
__INTPTR_TYPE__ off_t
Definition types.h:36
__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:411
Flash partition.
Definition flash_map.h:54
off_t fa_off
Start offset from the beginning of the flash device.
Definition flash_map.h:59
size_t fa_size
Total size.
Definition flash_map.h:61
const struct device * fa_dev
Backing flash device.
Definition flash_map.h:63
uint8_t fa_id
ID number.
Definition flash_map.h:56
uint16_t pad16
Definition flash_map.h:57
Structure for transfer flash sector boundaries.
Definition flash_map.h:76
off_t fs_off
Sector offset from the beginning of the flash device.
Definition flash_map.h:78
size_t fs_size
Sector size in bytes.
Definition flash_map.h:80