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 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
19 #include "hcs_macro.h"
20 #include "hdf_config_macro.h"
21 #else
22 #include "device_resource_if.h"
23 #endif
24
25 /* Logic partition on flash devices */
26 hal_logic_partition_t g_halPartitions[] = {
27 [HAL_PARTITION_BOOTLOADER] = {
28 .partition_owner = HAL_FLASH_EMBEDDED,
29 .partition_description = "boot1",
30 .partition_start_addr = 0x0,
31 .partition_length = 0x10000, //64KB
32 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
33 },
34 [HAL_PARTITION_BOOT2A] = {
35 .partition_owner = HAL_FLASH_EMBEDDED,
36 .partition_description = "boot2A",
37 .partition_start_addr = 0x10000,
38 .partition_length = 0x10000, //64KB
39 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
40 },
41 [HAL_PARTITION_BOOT2B] = {
42 .partition_owner = HAL_FLASH_EMBEDDED,
43 .partition_description = "boot2B",
44 .partition_start_addr = 0x20000,
45 .partition_length = 0x10000, //64KB
46 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
47 },
48 [HAL_PARTITION_TRUSTZONEA] = {
49 .partition_owner = HAL_FLASH_EMBEDDED,
50 .partition_description = "trustzoneA",
51 .partition_start_addr = 0x30000,
52 .partition_length = 0x26000, //152KB
53 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
54 },
55 [HAL_PARTITION_TRUSTZONEB] = {
56 .partition_owner = HAL_FLASH_EMBEDDED,
57 .partition_description = "trustzoneB",
58 .partition_start_addr = 0x56000,
59 .partition_length = 0x26000, //152KB
60 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
61 },
62 [HAL_PARTITION_TZ_INFO] = {
63 .partition_owner = HAL_FLASH_EMBEDDED,
64 .partition_description = "trustzoneInfo",
65 .partition_start_addr = 0x7C000,
66 .partition_length = 0x4000, //16KB
67 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
68 },
69 [HAL_PARTITION_CM33_MAIN] = {
70 .partition_owner = HAL_FLASH_EMBEDDED,
71 .partition_description = "cm33_main",
72 .partition_start_addr = 0x80000,
73 .partition_length = 0x7E0000, //8064KB
74 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
75 },
76 [HAL_PARTITION_SYSTEM_MINI] = {
77 .partition_owner = HAL_FLASH_EMBEDDED,
78 .partition_description = "system_mini",
79 .partition_start_addr = 0x860000,
80 .partition_length = 0x300000, //3072KB
81 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
82 },
83 [HAL_PARTITION_RESOURCE] = {
84 .partition_owner = HAL_FLASH_EMBEDDED,
85 .partition_description = "littlefs",
86 .partition_start_addr = 0xB60000,
87 .partition_length = 0x400000, //4096KB
88 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
89 },
90 [HAL_PARTITION_LOG] = {
91 .partition_owner = HAL_FLASH_EMBEDDED,
92 .partition_description = "log",
93 .partition_start_addr = 0xF60000,
94 .partition_length = 0x30000, //192KB
95 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
96 },
97 [HAL_PARTITION_DATA] = {
98 .partition_owner = HAL_FLASH_EMBEDDED,
99 .partition_description = "data",
100 .partition_start_addr = 0xF90000,
101 .partition_length = 0x40000, //256KB
102 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
103 },
104 [HAL_PARTITION_MISC] = {
105 .partition_owner = HAL_FLASH_EMBEDDED,
106 .partition_description = "misc",
107 .partition_start_addr = 0xFD0000,
108 .partition_length = 0xC000, //48KB
109 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
110 },
111 [HAL_PARTITION_USERDATA] = {
112 .partition_owner = HAL_FLASH_EMBEDDED,
113 .partition_description = "userdata",
114 .partition_start_addr = 0xFDC000,
115 .partition_length = 0x4000, //16KB
116 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
117 },
118 [HAL_PARTITION_ENV] = {
119 .partition_owner = HAL_FLASH_EMBEDDED,
120 .partition_description = "factory",
121 .partition_start_addr = 0xFE0000,
122 .partition_length = 0x10000, //64KB
123 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
124 },
125 [HAL_PARTITION_ENV_REDUND] = {
126 .partition_owner = HAL_FLASH_EMBEDDED,
127 .partition_description = "factory_backup",
128 .partition_start_addr = 0xFF0000,
129 .partition_length = 0x10000, //64KB
130 .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN,
131 },
132 };
133 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
134 #define DISPLAY_MISC_STORAGE_FLASH_CONFIG HCS_NODE(HCS_NODE(HCS_NODE(HCS_ROOT, misc), storage_config), flash_config)
FlashGetResource()135 static uint32_t FlashGetResource()
136 {
137 int32_t num = HCS_ARRAYS_SIZE(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, partitions));
138 if (num < 0 || num > HAL_PARTITION_MAX) {
139 HDF_LOGE("%s: invalid partitions num %d", __func__, num);
140 return HDF_FAILURE;
141 }
142 uint32_t partition;
143 uint32_t partitions[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, partitions));
144 uint32_t partition_owner[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, owner));
145 char * description[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, description));
146 uint32_t start_addr[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, start_addr));
147 uint32_t length[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, length));
148 uint32_t options[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_STORAGE_FLASH_CONFIG, options));
149 for (int32_t i = 0; i < num; i++) {
150 partition = partitions[i];
151 if (partition >= HAL_PARTITION_MAX) {
152 HDF_LOGE("%s: invalid partition %u", __func__, partition);
153 return HDF_FAILURE;
154 }
155 g_halPartitions[partition].partition_owner = partition_owner[i];
156 g_halPartitions[partition].partition_description = description[i];
157 g_halPartitions[partition].partition_start_addr = start_addr[i];
158 g_halPartitions[partition].partition_length = length[i];
159 g_halPartitions[partition].partition_options = options[i];
160 HDF_LOGD("%s: partition[%u] owner=%u, description=%s, start_addr=0x%x, length=0x%x, options=%u", __func__, partition,
161 g_halPartitions[partition].partition_owner, g_halPartitions[partition].partition_description,
162 g_halPartitions[partition].partition_start_addr, g_halPartitions[partition].partition_length,
163 g_halPartitions[partition].partition_options);
164 }
165 return HDF_SUCCESS;
166 }
167 #else
FlashGetResource(const struct DeviceResourceNode * resourceNode)168 static uint32_t FlashGetResource(const struct DeviceResourceNode *resourceNode)
169 {
170 struct DeviceResourceIface *resource = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
171 if (resource == NULL) {
172 HDF_LOGE("Invalid DeviceResourceIface");
173 return HDF_FAILURE;
174 }
175 int32_t num = resource->GetElemNum(resourceNode, "partitions");
176 if (num < 0 || num > HAL_PARTITION_MAX) {
177 HDF_LOGE("%s: invalid partitions num %d", __func__, num);
178 return HDF_FAILURE;
179 }
180 for (int32_t i = 0; i < num; i++) {
181 uint32_t partition = 0;
182 if (resource->GetUint32ArrayElem(resourceNode, "partitions", i, &partition, 0) != HDF_SUCCESS) {
183 HDF_LOGE("%s: failed to get partitions", __func__);
184 return HDF_FAILURE;
185 }
186 if (partition >= HAL_PARTITION_MAX) {
187 HDF_LOGE("%s: invalid partition %u", __func__, partition);
188 return HDF_FAILURE;
189 }
190 if (resource->GetUint32ArrayElem(resourceNode, "owner", i, (uint32_t *)&g_halPartitions[partition].partition_owner, 0) != HDF_SUCCESS) {
191 HDF_LOGE("%s: failed to get owner", __func__);
192 return HDF_FAILURE;
193 }
194 if (resource->GetStringArrayElem(resourceNode, "description", i, &g_halPartitions[partition].partition_description, NULL) != HDF_SUCCESS) {
195 HDF_LOGE("%s: failed to get mount_points", __func__);
196 return HDF_FAILURE;
197 }
198 if (resource->GetUint32ArrayElem(resourceNode, "start_addr", i, &g_halPartitions[partition].partition_start_addr, 0) != HDF_SUCCESS) {
199 HDF_LOGE("%s: failed to get block_size", __func__);
200 return HDF_FAILURE;
201 }
202 if (resource->GetUint32ArrayElem(resourceNode, "length", i, &g_halPartitions[partition].partition_length, 0) != HDF_SUCCESS) {
203 HDF_LOGE("%s: failed to get block_count", __func__);
204 return HDF_FAILURE;
205 }
206 if (resource->GetUint32ArrayElem(resourceNode, "options", i, &g_halPartitions[partition].partition_options, 0) != HDF_SUCCESS) {
207 HDF_LOGE("%s: failed to get block_count", __func__);
208 return HDF_FAILURE;
209 }
210 HDF_LOGD("%s: partition[%u] owner=%u, description=%s, start_addr=0x%x, length=0x%x, options=%u", __func__, partition,
211 g_halPartitions[partition].partition_owner, g_halPartitions[partition].partition_description,
212 g_halPartitions[partition].partition_start_addr, g_halPartitions[partition].partition_length,
213 g_halPartitions[partition].partition_options);
214 }
215 return HDF_SUCCESS;
216 }
217 #endif
FlashDriverInit(struct HdfDeviceObject * object)218 static int32_t FlashDriverInit(struct HdfDeviceObject *object)
219 {
220 if (object == NULL) {
221 return HDF_FAILURE;
222 }
223 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
224 if (FlashGetResource() != HDF_SUCCESS) {
225 HDF_LOGE("%s: FlashGetResource failed", __func__);
226 return HDF_FAILURE;
227 }
228 #else
229 if (object->property) {
230 if (FlashGetResource(object->property) != HDF_SUCCESS) {
231 HDF_LOGE("%s: FlashGetResource failed", __func__);
232 return HDF_FAILURE;
233 }
234 }
235 #endif
236
237 return HDF_SUCCESS;
238 }
239
FlashDriverBind(struct HdfDeviceObject * device)240 static int32_t FlashDriverBind(struct HdfDeviceObject *device)
241 {
242 (void)device;
243 return HDF_SUCCESS;
244 }
245
FlashDriverRelease(struct HdfDeviceObject * device)246 static void FlashDriverRelease(struct HdfDeviceObject *device)
247 {
248 (void)device;
249 }
250
251 static struct HdfDriverEntry g_FlashDriverEntry = {
252 .moduleVersion = 1,
253 .moduleName = "HDF_STORAGE_FLASH",
254 .Bind = FlashDriverBind,
255 .Init = FlashDriverInit,
256 .Release = FlashDriverRelease,
257 };
258
259 HDF_INIT(g_FlashDriverEntry);