Skip to content

Commit ab51cd5

Browse files
committed
[nrf fromlist] 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. Upstream PR #: 96808 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent fbedd73 commit ab51cd5

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

include/zephyr/storage/flash_map.h

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,17 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
353353
#else
354354

355355
/**
356-
* Returns non-0 value if fixed-partition of given DTS node label exists.
356+
* Returns non-0 value if fixed-partition as well as fixed-subpartition of given
357+
* DTS node label exists.
357358
*
358359
* @param label DTS node label
359360
*
360361
* @return non-0 if fixed-partition node exists and is enabled;
361362
* 0 if node does not exist, is not enabled or is not fixed-partition.
362363
*/
363-
#define FIXED_PARTITION_EXISTS(label) DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label))
364+
#define FIXED_PARTITION_EXISTS(label) \
365+
UTIL_OR(DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label)), \
366+
DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)))
364367

365368
/**
366369
* Get flash area ID from fixed-partition DTS node label
@@ -372,14 +375,40 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
372375
#define FIXED_PARTITION_ID(label) DT_FIXED_PARTITION_ID(DT_NODELABEL(label))
373376

374377
/**
375-
* Get fixed-partition offset from DTS node label
378+
* Get fixed-partition as well as fixed-subpartition offset from DTS node label
376379
*
377380
* @param label DTS node label of a partition
378381
*
379382
* @return fixed-partition offset, as defined for the partition in DTS.
380383
*/
381384
#define FIXED_PARTITION_OFFSET(label) DT_REG_ADDR(DT_NODELABEL(label))
382385

386+
/**
387+
* Get fixed-partition as well as fixed-subpartition address from DTS node label
388+
*
389+
* @param label DTS node label of a partition or subpartition
390+
*
391+
* @return fixed-partition address, as defined for the partition in DTS.
392+
*/
393+
#define FIXED_PARTITION_ADDRESS(label) \
394+
(DT_REG_ADDR(DT_NODELABEL(label)) + \
395+
COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)), \
396+
(DT_REG_ADDR(DT_GPARENT(DT_PARENT(DT_NODELABEL(label))))), \
397+
(DT_REG_ADDR(DT_GPARENT(DT_NODELABEL(label))))))
398+
399+
/**
400+
* Get fixed-partition as well as fixed-subpartition address from DTS node
401+
*
402+
* @param node DTS node of a partition
403+
*
404+
* @return fixed-partition address, as defined for the partition in DTS.
405+
*/
406+
#define FIXED_PARTITION_NODE_ADDRESS(node) \
407+
(DT_REG_ADDR(node) + \
408+
COND_CODE_1(DT_FIXED_SUBPARTITION_EXISTS(node), \
409+
(DT_REG_ADDR(DT_GPARENT(DT_PARENT(node)))), \
410+
(DT_REG_ADDR(DT_GPARENT(node)))))
411+
383412
/**
384413
* Get fixed-partition offset from DTS node
385414
*
@@ -425,8 +454,10 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
425454
* @return Pointer to a device.
426455
*/
427456
#define FIXED_PARTITION_DEVICE(label) \
428-
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))
429-
457+
DEVICE_DT_GET(COND_CODE_1( \
458+
DT_FIXED_SUBPARTITION_EXISTS(DT_NODELABEL(label)), \
459+
(DT_MTD_FROM_FIXED_SUBPARTITION(DT_NODELABEL(label))), \
460+
(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))))
430461
/**
431462
* Get device pointer for device the area/partition resides on
432463
*
@@ -435,7 +466,10 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
435466
* @return Pointer to a device.
436467
*/
437468
#define FIXED_PARTITION_NODE_DEVICE(node) \
438-
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(node))
469+
DEVICE_DT_GET(COND_CODE_1( \
470+
DT_FIXED_SUBPARTITION_EXISTS(node), \
471+
(DT_MTD_FROM_FIXED_SUBPARTITION(node)), \
472+
(DT_MTD_FROM_FIXED_PARTITION(node))))
439473

440474
/**
441475
* Get pointer to flash_area object by partition label
@@ -471,6 +505,22 @@ DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
471505
#undef DECLARE_PARTITION
472506
#undef DECLARE_PARTITION_0
473507
#undef FOR_EACH_PARTITION_TABLE
508+
509+
#define FIXED_SUBPARTITION_1(node) FIXED_SUBPARTITION_0(DT_DEP_ORD(node))
510+
#define FIXED_SUBPARTITION_0(ord) \
511+
((const struct flash_area *)&DT_CAT(global_fixed_subpartition_ORD_, ord))
512+
513+
#define DECLARE_SUBPARTITION(node) DECLARE_SUBPARTITION_0(DT_DEP_ORD(node))
514+
#define DECLARE_SUBPARTITION_0(ord) \
515+
extern const struct flash_area DT_CAT(global_fixed_subpartition_ORD_, ord);
516+
#define FOR_EACH_SUBPARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_SUBPARTITION)
517+
518+
/* Generate declarations */
519+
DT_FOREACH_STATUS_OKAY(fixed_subpartitions, FOR_EACH_SUBPARTITION_TABLE)
520+
521+
#undef DECLARE_SUBPARTITION
522+
#undef DECLARE_SUBPARTITION_0
523+
#undef FOR_EACH_SUBPARTITION_TABLE
474524
/** @endcond */
475525

476526
#endif /* USE_PARTITION_MANAGER */

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)