1 /*
2  * Copyright (c) 2017-2021 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  * Copyright (c) 2017 Linaro Ltd
5  * Copyright (c) 2020 Gerson Fernando Budke <nandojve@gmail.com>
6  * Copyright (c) 2023 Sensorfy B.V.
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include <errno.h>
12 
13 #include <zephyr/types.h>
14 #include <stddef.h>
15 #include <sys/types.h>
16 #include <zephyr/device.h>
17 #include <zephyr/storage/flash_map.h>
18 #include "flash_map_priv.h"
19 #include <zephyr/drivers/flash.h>
20 #include <zephyr/init.h>
21 
flash_area_foreach(flash_area_cb_t user_cb,void * user_data)22 void flash_area_foreach(flash_area_cb_t user_cb, void *user_data)
23 {
24 	for (int i = 0; i < flash_map_entries; i++) {
25 		user_cb(&flash_map[i], user_data);
26 	}
27 }
28 
flash_area_open(uint8_t id,const struct flash_area ** fap)29 int flash_area_open(uint8_t id, const struct flash_area **fap)
30 {
31 	const struct flash_area *area;
32 
33 	if (flash_map == NULL) {
34 		return -EACCES;
35 	}
36 
37 	area = get_flash_area_from_id(id);
38 	if (area == NULL) {
39 		return -ENOENT;
40 	}
41 
42 	if (!area->fa_dev || !device_is_ready(area->fa_dev)) {
43 		return -ENODEV;
44 	}
45 
46 	*fap = area;
47 
48 	return 0;
49 }
50 
flash_area_close(const struct flash_area * fa)51 void flash_area_close(const struct flash_area *fa)
52 {
53 	/* nothing to do for now */
54 }
55 
flash_area_read(const struct flash_area * fa,off_t off,void * dst,size_t len)56 int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
57 		    size_t len)
58 {
59 	if (!is_in_flash_area_bounds(fa, off, len)) {
60 		return -EINVAL;
61 	}
62 
63 	return flash_read(fa->fa_dev, fa->fa_off + off, dst, len);
64 }
65 
flash_area_write(const struct flash_area * fa,off_t off,const void * src,size_t len)66 int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
67 		     size_t len)
68 {
69 	if (!is_in_flash_area_bounds(fa, off, len)) {
70 		return -EINVAL;
71 	}
72 
73 	return flash_write(fa->fa_dev, fa->fa_off + off, (void *)src, len);
74 }
75 
flash_area_erase(const struct flash_area * fa,off_t off,size_t len)76 int flash_area_erase(const struct flash_area *fa, off_t off, size_t len)
77 {
78 	if (!is_in_flash_area_bounds(fa, off, len)) {
79 		return -EINVAL;
80 	}
81 
82 	return flash_erase(fa->fa_dev, fa->fa_off + off, len);
83 }
84 
flash_area_flatten(const struct flash_area * fa,off_t off,size_t len)85 int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len)
86 {
87 	if (!is_in_flash_area_bounds(fa, off, len)) {
88 		return -EINVAL;
89 	}
90 
91 	return flash_flatten(fa->fa_dev, fa->fa_off + off, len);
92 }
93 
flash_area_align(const struct flash_area * fa)94 uint32_t flash_area_align(const struct flash_area *fa)
95 {
96 	return flash_get_write_block_size(fa->fa_dev);
97 }
98 
flash_area_has_driver(const struct flash_area * fa)99 int flash_area_has_driver(const struct flash_area *fa)
100 {
101 	if (!device_is_ready(fa->fa_dev)) {
102 		return -ENODEV;
103 	}
104 
105 	return 1;
106 }
107 
flash_area_get_device(const struct flash_area * fa)108 const struct device *flash_area_get_device(const struct flash_area *fa)
109 {
110 	return fa->fa_dev;
111 }
112 
113 #if CONFIG_FLASH_MAP_LABELS
flash_area_label(const struct flash_area * fa)114 const char *flash_area_label(const struct flash_area *fa)
115 {
116 	return fa->fa_label;
117 }
118 #endif
119 
flash_area_erased_val(const struct flash_area * fa)120 uint8_t flash_area_erased_val(const struct flash_area *fa)
121 {
122 	const struct flash_parameters *param;
123 
124 	param = flash_get_parameters(fa->fa_dev);
125 
126 	return param->erase_value;
127 }
128