Skip to content

Commit 3ae8a43

Browse files
tomchycfriedt
authored andcommitted
storage: Allow to use subpartitions in flash_map
Allow to use both partition and subpartition names when suing the flash_map API. That way it is possible to introduce a hierarchy within DTS in a backward compatible way. Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent 5c010ed commit 3ae8a43

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

include/zephyr/storage/flash_map.h

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,17 @@ const char *flash_area_label(const struct flash_area *fa);
349349
uint8_t flash_area_erased_val(const struct flash_area *fa);
350350

351351
/**
352-
* Returns non-0 value if fixed-partition of given DTS node label exists.
352+
* Returns non-0 value if fixed-partition or fixed-subpartition of given
353+
* DTS node label exists.
353354
*
354355
* @param label DTS node label
355356
*
356357
* @return non-0 if fixed-partition node exists and is enabled;
357358
* 0 if node does not exist, is not enabled or is not fixed-partition.
358359
*/
359-
#define FIXED_PARTITION_EXISTS(label) DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label))
360+
#define FIXED_PARTITION_EXISTS(label) \
361+
UTIL_OR(DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label)), \
362+
DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)))
360363

361364
/**
362365
* Get flash area ID from fixed-partition DTS node label
@@ -368,14 +371,38 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
368371
#define FIXED_PARTITION_ID(label) DT_FIXED_PARTITION_ID(DT_NODELABEL(label))
369372

370373
/**
371-
* Get fixed-partition offset from DTS node label
374+
* Get fixed-partition or fixed-subpartition offset from DTS node label
372375
*
373376
* @param label DTS node label of a partition
374377
*
375378
* @return fixed-partition offset, as defined for the partition in DTS.
376379
*/
377380
#define FIXED_PARTITION_OFFSET(label) DT_REG_ADDR(DT_NODELABEL(label))
378381

382+
/**
383+
* Get fixed-partition or fixed-subpartition address from DTS node label
384+
*
385+
* @param label DTS node label of a partition or subpartition
386+
*
387+
* @return fixed-partition address, as defined for the partition in DTS.
388+
*/
389+
#define FIXED_PARTITION_ADDRESS(label) \
390+
(COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)), \
391+
(DT_FIXED_SUBPARTITION_ADDR(DT_NODELABEL(label))), \
392+
(DT_FIXED_PARTITION_ADDR(DT_NODELABEL(label)))))
393+
394+
/**
395+
* Get fixed-partition or fixed-subpartition address from DTS node
396+
*
397+
* @param node DTS node of a partition
398+
*
399+
* @return fixed-partition address, as defined for the partition in DTS.
400+
*/
401+
#define FIXED_PARTITION_NODE_ADDRESS(node) \
402+
(COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
403+
(DT_FIXED_SUBPARTITION_ADDR(node)), \
404+
(DT_FIXED_PARTITION_ADDR(node))))
405+
379406
/**
380407
* Get fixed-partition offset from DTS node
381408
*
@@ -421,8 +448,10 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
421448
* @return Pointer to a device.
422449
*/
423450
#define FIXED_PARTITION_DEVICE(label) \
424-
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))
425-
451+
DEVICE_DT_GET(COND_CODE_1( \
452+
DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)), \
453+
(DT_MTD_FROM_FIXED_SUBPARTITION(DT_NODELABEL(label))), \
454+
(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))))
426455
/**
427456
* Get device pointer for device the area/partition resides on
428457
*
@@ -431,7 +460,10 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
431460
* @return Pointer to a device.
432461
*/
433462
#define FIXED_PARTITION_NODE_DEVICE(node) \
434-
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(node))
463+
DEVICE_DT_GET(COND_CODE_1( \
464+
DT_FIXED_SUBPARTITION_EXISTS(node), \
465+
(DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
466+
(DT_MTD_FROM_FIXED_PARTITION(node))))
435467

436468
/**
437469
* Get pointer to flash_area object by partition label
@@ -467,6 +499,22 @@ DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
467499
#undef DECLARE_PARTITION
468500
#undef DECLARE_PARTITION_0
469501
#undef FOR_EACH_PARTITION_TABLE
502+
503+
#define FIXED_SUBPARTITION_1(node) FIXED_SUBPARTITION_0(DT_DEP_ORD(node))
504+
#define FIXED_SUBPARTITION_0(ord) \
505+
((const struct flash_area *)&DT_CAT(global_fixed_subpartition_ORD_, ord))
506+
507+
#define DECLARE_SUBPARTITION(node) DECLARE_SUBPARTITION_0(DT_DEP_ORD(node))
508+
#define DECLARE_SUBPARTITION_0(ord) \
509+
extern const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord);
510+
#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_SUBPARTITION)
511+
512+
/* Generate declarations */
513+
DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)
514+
515+
#undef DECLARE_SUBPARTITION
516+
#undef DECLARE_SUBPARTITION_0
517+
#undef FOR_EACH_SUBPARTITION_TABLE
470518
/** @endcond */
471519

472520
#ifdef __cplusplus

subsys/storage/flash_map/flash_map_default.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
const struct flash_area default_flash_map[] = {
4242
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOREACH_PARTITION)
43+
DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOREACH_PARTITION)
4344
};
4445

4546
const int flash_map_entries = ARRAY_SIZE(default_flash_map);
@@ -63,17 +64,17 @@ const struct flash_area *flash_map = default_flash_map;
6364
#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DEFINE_PARTITION)
6465
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
6566

66-
#define DEFINE_SUB_PARTITION(part) DEFINE_SUB_PARTITION_1(part, DT_DEP_ORD(part))
67-
#define DEFINE_SUB_PARTITION_1(part, ord) \
67+
#define DEFINE_SUBPARTITION(part) DEFINE_SUBPARTITION_1(part, DT_DEP_ORD(part))
68+
#define DEFINE_SUBPARTITION_1(part, ord) \
6869
COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_SUBPARTITION(part)), \
69-
(DEFINE_SUB_PARTITION_0(part, ord)), ())
70-
#define DEFINE_SUB_PARTITION_0(part, ord) \
70+
(DEFINE_SUBPARTITION_0(part, ord)), ())
71+
#define DEFINE_SUBPARTITION_0(part, ord) \
7172
const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord) = { \
7273
.fa_id = DT_FIXED_PARTITION_ID(part), \
7374
.fa_off = DT_REG_ADDR(part), \
7475
.fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_SUBPARTITION(part)), \
7576
.fa_size = DT_REG_SIZE(part), \
7677
};
7778

78-
#define FOR_EACH_SUB_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DEFINE_SUB_PARTITION)
79-
DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUB_PARTITION_TABLE)
79+
#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DEFINE_SUBPARTITION)
80+
DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)

0 commit comments

Comments
 (0)