1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "flash.h"
16 #include "hdf_log.h"
17 #include "hdf_device_desc.h"
18 #include "los_fs.h"
19
20 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
21 #include "hcs_macro.h"
22 #include "hdf_config_macro.h"
23 #else
24 #include "device_resource_if.h"
25 #endif
26
27 /* Logic partition on flash devices */
28 hal_logic_partition_t g_halPartitions[] = {
29 [HAL_PARTITION_BOOTLOADER] = {
30 .partition_owner = HAL_FLASH_EMBEDDED,
31 .partition_description = "boot1",
32 .partition_start_addr = 0x0,
33 .partition_length = 0x10000, //64KB
34 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
35 },
36 [HAL_PARTITION_BOOT2A] = {
37 .partition_owner = HAL_FLASH_EMBEDDED,
38 .partition_description = "boot2A",
39 .partition_start_addr = 0x10000,
40 .partition_length = 0x10000, //64KB
41 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
42 },
43 [HAL_PARTITION_BOOT2B] = {
44 .partition_owner = HAL_FLASH_EMBEDDED,
45 .partition_description = "boot2B",
46 .partition_start_addr = 0x20000,
47 .partition_length = 0x10000, //64KB
48 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
49 },
50 [HAL_PARTITION_TRUSTZONEA] = {
51 .partition_owner = HAL_FLASH_EMBEDDED,
52 .partition_description = "trustzoneA",
53 .partition_start_addr = 0x30000,
54 .partition_length = 0x26000, //152KB
55 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
56 },
57 [HAL_PARTITION_TRUSTZONEB] = {
58 .partition_owner = HAL_FLASH_EMBEDDED,
59 .partition_description = "trustzoneB",
60 .partition_start_addr = 0x56000,
61 .partition_length = 0x26000, //152KB
62 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
63 },
64 [HAL_PARTITION_TZ_INFO] = {
65 .partition_owner = HAL_FLASH_EMBEDDED,
66 .partition_description = "trustzoneInfo",
67 .partition_start_addr = 0x7C000,
68 .partition_length = 0x4000, //16KB
69 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
70 },
71 [HAL_PARTITION_CM33_MAIN] = {
72 .partition_owner = HAL_FLASH_EMBEDDED,
73 .partition_description = "cm33_main",
74 .partition_start_addr = 0x80000,
75 .partition_length = 0x7E0000, //8064KB
76 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
77 },
78 [HAL_PARTITION_SYSTEM_MINI] = {
79 .partition_owner = HAL_FLASH_EMBEDDED,
80 .partition_description = "system_mini",
81 .partition_start_addr = 0x860000,
82 .partition_length = 0x300000, //3072KB
83 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
84 },
85 [HAL_PARTITION_RESOURCE] = {
86 .partition_owner = HAL_FLASH_EMBEDDED,
87 .partition_description = "littlefs",
88 .partition_start_addr = 0xB60000,
89 .partition_length = 0x400000, //4096KB
90 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
91 },
92 [HAL_PARTITION_LOG] = {
93 .partition_owner = HAL_FLASH_EMBEDDED,
94 .partition_description = "log",
95 .partition_start_addr = 0xF60000,
96 .partition_length = 0x30000, //192KB
97 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
98 },
99 [HAL_PARTITION_DATA] = {
100 .partition_owner = HAL_FLASH_EMBEDDED,
101 .partition_description = "data",
102 .partition_start_addr = 0xF90000,
103 .partition_length = 0x40000, //256KB
104 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
105 },
106 [HAL_PARTITION_MISC] = {
107 .partition_owner = HAL_FLASH_EMBEDDED,
108 .partition_description = "misc",
109 .partition_start_addr = 0xFD0000,
110 .partition_length = 0xC000, //48KB
111 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
112 },
113 [HAL_PARTITION_USERDATA] = {
114 .partition_owner = HAL_FLASH_EMBEDDED,
115 .partition_description = "userdata",
116 .partition_start_addr = 0xFDC000,
117 .partition_length = 0x4000, //16KB
118 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
119 },
120 [HAL_PARTITION_ENV] = {
121 .partition_owner = HAL_FLASH_EMBEDDED,
122 .partition_description = "factory",
123 .partition_start_addr = 0xFE0000,
124 .partition_length = 0x10000, //64KB
125 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
126 },
127 [HAL_PARTITION_ENV_REDUND] = {
128 .partition_owner = HAL_FLASH_EMBEDDED,
129 .partition_description = "factory_backup",
130 .partition_start_addr = 0xFF0000,
131 .partition_length = 0x10000, //64KB
132 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
133 },
134 };
135 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
136 #define DISPLAY_MISC_STORAGE_FLASH_CONFIG HCS_NODE(HCS_NODE(HCS_NODE(HCS_ROOT, misc), storage_config), flash_config)
FlashGetResource()137 static uint32_t FlashGetResource()
138 {
139 int32_t num = HCS_ARRAYS_SIZE(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, partitions));
140 if (num < 0 || num > HAL_PARTITION_MAX) {
141 HDF_LOGE("%s: invalid partitions num %d", __func__, num);
142 return HDF_FAILURE;
143 }
144 uint32_t partition;
145 uint32_t partitions[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, partitions));
146 uint32_t partition_owner[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, owner));
147 char * description[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, description));
148 uint32_t start_addr[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, start_addr));
149 uint32_t length[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, length));
150 uint32_t options[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, options));
151 for (int32_t i = 0; i < num; i++) {
152 partition = partitions[i];
153 if (partition >= HAL_PARTITION_MAX) {
154 HDF_LOGE("%s: invalid partition %u", __func__, partition);
155 return HDF_FAILURE;
156 }
157 g_halPartitions[partition].partition_owner = partition_owner[i];
158 g_halPartitions[partition].partition_description = description[i];
159 g_halPartitions[partition].partition_start_addr = start_addr[i];
160 g_halPartitions[partition].partition_length = length[i];
161 g_halPartitions[partition].partition_options = options[i];
162 HDF_LOGD("%s: partition[%u] owner=%u, description=%s, start_addr=0x%x, length=0x%x, options=%u", __func__, partition,
163 g_halPartitions[partition].partition_owner, g_halPartitions[partition].partition_description,
164 g_halPartitions[partition].partition_start_addr, g_halPartitions[partition].partition_length,
165 g_halPartitions[partition].partition_options);
166 }
167 return HDF_SUCCESS;
168 }
169 #else
FlashGetResource(const struct DeviceResourceNode * resourceNode)170 static uint32_t FlashGetResource(const struct DeviceResourceNode *resourceNode)
171 {
172 struct DeviceResourceIface *resource = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
173 if (resource == NULL) {
174 HDF_LOGE("Invalid DeviceResourceIface");
175 return HDF_FAILURE;
176 }
177 int32_t num = resource->GetElemNum(resourceNode, "partitions");
178 if (num < 0 || num > HAL_PARTITION_MAX) {
179 HDF_LOGE("%s: invalid partitions num %d", __func__, num);
180 return HDF_FAILURE;
181 }
182 for (int32_t i = 0; i < num; i++) {
183 uint32_t partition = 0;
184 if (resource->GetUint32ArrayElem(resourceNode, "partitions", i, &partition, 0) != HDF_SUCCESS) {
185 HDF_LOGE("%s: failed to get partitions", __func__);
186 return HDF_FAILURE;
187 }
188 if (partition >= HAL_PARTITION_MAX) {
189 HDF_LOGE("%s: invalid partition %u", __func__, partition);
190 return HDF_FAILURE;
191 }
192 if (resource->GetUint32ArrayElem(resourceNode, "owner", i, (uint32_t *)&g_halPartitions[partition].partition_owner, 0) != HDF_SUCCESS) {
193 HDF_LOGE("%s: failed to get owner", __func__);
194 return HDF_FAILURE;
195 }
196 if (resource->GetStringArrayElem(resourceNode, "description", i, &g_halPartitions[partition].partition_description, NULL) != HDF_SUCCESS) {
197 HDF_LOGE("%s: failed to get mount_points", __func__);
198 return HDF_FAILURE;
199 }
200 if (resource->GetUint32ArrayElem(resourceNode, "start_addr", i, &g_halPartitions[partition].partition_start_addr, 0) != HDF_SUCCESS) {
201 HDF_LOGE("%s: failed to get block_size", __func__);
202 return HDF_FAILURE;
203 }
204 if (resource->GetUint32ArrayElem(resourceNode, "length", i, &g_halPartitions[partition].partition_length, 0) != HDF_SUCCESS) {
205 HDF_LOGE("%s: failed to get block_count", __func__);
206 return HDF_FAILURE;
207 }
208 if (resource->GetUint32ArrayElem(resourceNode, "options", i, &g_halPartitions[partition].partition_options, 0) != HDF_SUCCESS) {
209 HDF_LOGE("%s: failed to get block_count", __func__);
210 return HDF_FAILURE;
211 }
212 HDF_LOGD("%s: partition[%u] owner=%u, description=%s, start_addr=0x%x, length=0x%x, options=%u", __func__, partition,
213 g_halPartitions[partition].partition_owner, g_halPartitions[partition].partition_description,
214 g_halPartitions[partition].partition_start_addr, g_halPartitions[partition].partition_length,
215 g_halPartitions[partition].partition_options);
216 }
217 return HDF_SUCCESS;
218 }
219 #endif
FlashDriverInit(struct HdfDeviceObject * object)220 static int32_t FlashDriverInit(struct HdfDeviceObject *object)
221 {
222 if (object == NULL) {
223 return HDF_FAILURE;
224 }
225 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
226 if (FlashGetResource() != HDF_SUCCESS) {
227 HDF_LOGE("%s: FlashGetResource failed", __func__);
228 return HDF_FAILURE;
229 }
230 #else
231 if (object->property) {
232 if (FlashGetResource(object->property) != HDF_SUCCESS) {
233 HDF_LOGE("%s: FlashGetResource failed", __func__);
234 return HDF_FAILURE;
235 }
236 }
237 #endif
238
239 int lengthArray[HAL_PARTITION_MAX];
240 int addrArray[HAL_PARTITION_MAX];
241 for (int i = 0; i < HAL_PARTITION_MAX; i++) {
242 lengthArray[i] = g_halPartitions[i].partition_length;
243 addrArray[i] = g_halPartitions[i].partition_start_addr;
244 }
245
246 (VOID)LOS_DiskPartition(DEV_FLASH_NAME, "littlefs", lengthArray, addrArray, HAL_PARTITION_MAX);
247
248 return HDF_SUCCESS;
249 }
250
FlashDriverBind(struct HdfDeviceObject * device)251 static int32_t FlashDriverBind(struct HdfDeviceObject *device)
252 {
253 (void)device;
254 return HDF_SUCCESS;
255 }
256
FlashDriverRelease(struct HdfDeviceObject * device)257 static void FlashDriverRelease(struct HdfDeviceObject *device)
258 {
259 (void)device;
260 }
261
262 static struct HdfDriverEntry g_FlashDriverEntry = {
263 .moduleVersion = 1,
264 .moduleName = "HDF_STORAGE_FLASH",
265 .Bind = FlashDriverBind,
266 .Init = FlashDriverInit,
267 .Release = FlashDriverRelease,
268 };
269
270 HDF_INIT(g_FlashDriverEntry);