@@ -77,7 +77,9 @@ static int disk_flash_access_init(struct disk_info *disk)
77
77
LOG_INF ("offset %lx, sector size %zu, page size %zu, volume size %zu" ,
78
78
(long )ctx -> offset , ctx -> sector_size , ctx -> page_size , ctx -> size );
79
79
80
- if (ctx -> page_size > ctx -> cache_size ) {
80
+ if (ctx -> cache_size == 0 ) {
81
+ LOG_INF ("%s is read-only" , disk -> name );
82
+ } else if (ctx -> page_size > ctx -> cache_size ) {
81
83
LOG_ERR ("Cache too small (%zu needs %zu)" ,
82
84
ctx -> cache_size , ctx -> page_size );
83
85
rc = - ENOMEM ;
@@ -272,6 +274,10 @@ static int disk_flash_access_write(struct disk_info *disk, const uint8_t *buff,
272
274
273
275
ctx = CONTAINER_OF (disk , struct flashdisk_data , info );
274
276
277
+ if (ctx -> cache_size == 0 ) {
278
+ return - ENOTSUP ;
279
+ }
280
+
275
281
if (!sectors_in_range (ctx , start_sector , sector_count )) {
276
282
return - EINVAL ;
277
283
}
@@ -383,9 +389,11 @@ static const struct disk_operations flash_disk_ops = {
383
389
#define DT_DRV_COMPAT zephyr_flash_disk
384
390
385
391
#define PARTITION_PHANDLE (n ) DT_PHANDLE_BY_IDX(DT_DRV_INST(n), partition, 0)
392
+ /* Force cache size to 0 if partition is read-only */
393
+ #define CACHE_SIZE (n ) (DT_INST_PROP(n, cache_size) * !DT_PROP(PARTITION_PHANDLE(n), read_only))
386
394
387
395
#define DEFINE_FLASHDISKS_CACHE (n ) \
388
- static uint8_t __aligned(4) flashdisk##n##_cache[DT_INST_PROP(n, cache_size )];
396
+ static uint8_t __aligned(4) flashdisk##n##_cache[CACHE_SIZE(n )];
389
397
DT_INST_FOREACH_STATUS_OKAY (DEFINE_FLASHDISKS_CACHE )
390
398
391
399
#define DEFINE_FLASHDISKS_DEVICE (n ) \
@@ -397,7 +405,7 @@ DT_INST_FOREACH_STATUS_OKAY(DEFINE_FLASHDISKS_CACHE)
397
405
.area_id = DT_FIXED_PARTITION_ID(PARTITION_PHANDLE(n)), \
398
406
.offset = DT_REG_ADDR(PARTITION_PHANDLE(n)), \
399
407
.cache = flashdisk##n##_cache, \
400
- .cache_size = DT_INST_PROP(n, cache_size ), \
408
+ .cache_size = sizeof(flashdisk##n##_cache ), \
401
409
.size = DT_REG_SIZE(PARTITION_PHANDLE(n)), \
402
410
.sector_size = DT_INST_PROP(n, sector_size), \
403
411
},
@@ -406,6 +414,14 @@ static struct flashdisk_data flash_disks[] = {
406
414
DT_INST_FOREACH_STATUS_OKAY (DEFINE_FLASHDISKS_DEVICE )
407
415
};
408
416
417
+ #define VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY (n ) \
418
+ COND_CODE_1(DT_PROP(PARTITION_PHANDLE(n), read_only), \
419
+ (/* cache-size is not used for read-only disks */ ), \
420
+ (BUILD_ASSERT (DT_INST_PROP (n , cache_size ) != 0 , \
421
+ "Devicetree node " DT_NODE_PATH (DT_DRV_INST (n )) \
422
+ " must have non-zero cache-size" );))
423
+ DT_INST_FOREACH_STATUS_OKAY (VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY )
424
+
409
425
#define VERIFY_CACHE_SIZE_IS_MULTIPLY_OF_SECTOR_SIZE (n ) \
410
426
BUILD_ASSERT(DT_INST_PROP(n, cache_size) % DT_INST_PROP(n, sector_size) == 0, \
411
427
"Devicetree node " DT_NODE_PATH(DT_DRV_INST(n)) \
0 commit comments