blob: 725902ab8fae724132253367c41040f3d165062b [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <amlogic/storage.h>
7#include <div64.h>
8#include <linux/math64.h>
9#include <amlogic/cpu_id.h>
10#include <amlogic/store_wrapper.h>
11#include <asm/amlogic/arch/register.h>
12#include <asm/amlogic/arch/bl31_apis.h>
13#include <amlogic/aml_efuse.h>
14#include <asm/amlogic/arch/cpu_config.h>
15#include <asm/amlogic/arch/romboot.h>
16#include <asm/amlogic/arch/secure_apb.h>
17#include <amlogic/blxx2bl33_param.h>
18#include <amlogic/aml_mtd.h>
19#include <mmc.h>
20
21#include <display_options.h>
22
23#ifdef CONFIG_SPI_FLASH_MTD
24extern int spi_nor_pre(void);
25extern int spi_nor_probe(u32 init_flag);
26#endif
27
Bo Lv72d0e902023-01-02 14:27:34 +000028#ifdef CONFIG_MTD_SPI_NAND
29extern int spi_nand_pre(void);
30extern int spi_nand_probe(u32 init_flag);
31#endif
32
Bo Lv72d0e902023-01-02 14:27:34 +000033#ifdef CONFIG_MESON_NFC
34extern int nand_pre(void);
35extern int nand_probe(uint32_t init_flag);
36#endif
37
38#ifdef CONFIG_MMC_MESON_GX
39extern int emmc_pre(void);
40extern int emmc_probe(u32 init_flag);
41#endif
42
43#ifdef CONFIG_MMC_MESON_GX
44extern int sdcard_pre(void);
45extern int sdcard_probe(u32 init_flag);
46#endif
47/* for info protect, fixme later */
48int info_disprotect = 0;
49
50static struct storage_t *current;
51static struct device_node_t device_list[] = {
52#ifdef CONFIG_MESON_NFC
53 {BOOT_NAND_MTD, "mtd", nand_pre, nand_probe},
54#endif
Bo Lv72d0e902023-01-02 14:27:34 +000055#ifdef CONFIG_MTD_SPI_NAND
56 {BOOT_SNAND, "spi-nand", spi_nand_pre, spi_nand_probe},
57#endif
58#ifdef CONFIG_SPI_FLASH_MTD
59 {BOOT_SNOR, "spi-nor", spi_nor_pre, spi_nor_probe},
60#endif
61#if 0
62 {BOOT_SD, "sd", sdcard_pre, sdcard_probe},
63#endif
64
65#ifdef CONFIG_MMC_MESON_GX
66 {BOOT_EMMC, "emmc", emmc_pre, emmc_probe},
67#endif
68
69};
70
71int store_register(struct storage_t *store_dev)
72{
73 if (!store_dev)
74 return 1;
75 if (!current) {
76 INIT_LIST_HEAD(&store_dev->list);
77 current = store_dev;
78 return 0;
79 }
80 /**
81 * the head node will not be a valid node
82 * usually when we use the list, but in storage
83 * interface module, we init the device node as
84 * a head instead a global list_head pointer,
85 * it should be traver scaled.
86 */
87 if (store_dev == current)
88 return 0;
89 struct storage_t *dev;
90
91 if (store_dev->type == current->type)
92 return 1;
93 list_for_each_entry(dev, &current->list, list) {
94 if (dev == store_dev)
95 return 0;
96 else if (dev->type == store_dev->type)
97 return 1;
98 }
99 list_add_tail(&store_dev->list, &current->list);
100 current = store_dev;
101 return 0;
102}
103
104void store_unregister(struct storage_t *store_dev)
105{
106 if (store_dev == current) {
107 if (list_empty_careful(&store_dev->list)) {
108 current = NULL;
109 } else {
110 current = list_entry((current->list).next,
111 struct storage_t, list);
112 list_del_init(&store_dev->list);
113 }
114 } else {
115 list_del_init(&store_dev->list);
116 }
117}
118
119int sheader_need(void)
120{
121 return BOOTLOADER_MODE_ADVANCE_INIT;
122}
123
124unsigned char *ubootdata = NULL;
125void sheader_load(void *addr)
126{
127 ubootdata = addr;
128}
129
130/*
131 * storage header which size is 512B
132 * is bind into the tail of bl2.bin.
133 * @addr: uboot address.
134 */
135static p_payload_info_t parse_uboot_sheader(void *addr)
136{
137 p_payload_info_t pInfo = (p_payload_info_t)(addr + BL2_SIZE);
138
139 if (AML_MAGIC_HDR_L == pInfo->hdr.nMagicL &&
140 AML_MAGIC_HDR_R == pInfo->hdr.nMagicR) {
141 printf("aml log : bootloader blxx mode!\n");
142 return pInfo;
143 }
144 return NULL;
145}
146
147boot_area_entry_t general_boot_part_entry[MAX_BOOT_AREA_ENTRIES] = {
148 {BOOT_BL2, BOOT_AREA_BB1ST, 0, 0},
149 {BOOT_BL2E, BOOT_AREA_BL2E, 0, 0},
150 {BOOT_BL2X, BOOT_AREA_BL2X, 0, 0},
151 {BOOT_DDRFIP, BOOT_AREA_DDRFIP, 0, 0},
152 {BOOT_DEVFIP, BOOT_AREA_DEVFIP, 0, 0},
153};
154
155struct boot_layout general_boot_layout = {.boot_entry = general_boot_part_entry};
156struct storage_startup_parameter g_ssp;
157struct storage_bl *g_storage = NULL;
158
159static void storage_boot_layout_debug_info(
160 struct boot_layout *boot_layout)
161{
162 boot_area_entry_t *boot_entry = boot_layout->boot_entry;
163 int i;
164
165 printf("boot area list: \n");
Bichao Zheng8951e442023-07-13 13:21:29 +0800166 for (i = 0; i <= BOOT_AREA_DEVFIP; i++) {
Bo Lv72d0e902023-01-02 14:27:34 +0000167 printf("%10s ", boot_entry[i].name);
168 printf("%10llx ", boot_entry[i].offset);
169 printf("%10llx\n", boot_entry[i].size);
170 }
171}
172
173/* use STORAGE_ROUND_UP, y must be power of 2 */
174#define STORAGE_ROUND_UP_IF_UNALIGN(x, y) ((x) = (((x) + (y) - 1) & (~(y - 1))))
Bo Lv72d0e902023-01-02 14:27:34 +0000175#define ALIGN_SIZE (4096)
zhikui.cui31f7eab2024-05-17 09:11:12 +0000176static int storage_boot_layout_rebuild(struct boot_layout *boot_layout)
Bo Lv72d0e902023-01-02 14:27:34 +0000177{
178 struct storage_startup_parameter *ssp = &g_ssp;
179 boot_area_entry_t *boot_entry = boot_layout->boot_entry;
zhikui.cui4333f3f2024-08-12 07:19:21 +0000180 u64 align_size, cal_copy = 1;
Bichao Zheng8951e442023-07-13 13:21:29 +0800181 cpu_id_t cpu_id = get_cpu_id();
zhikui.cui31f7eab2024-05-17 09:11:12 +0000182 u8 i = BOOT_AREA_BL2E, bl2_copy;
Bo Lv72d0e902023-01-02 14:27:34 +0000183
184 align_size = ALIGN_SIZE;
zhikui.cui31f7eab2024-05-17 09:11:12 +0000185 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BB1ST].size, align_size);
186 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BL2E].size, align_size);
187 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BL2X].size, align_size);
188
189 if ((ssp->boot_device == BOOT_SNAND) ||
Bo Lv72d0e902023-01-02 14:27:34 +0000190 (ssp->boot_device == BOOT_NAND_MTD)) {
zhikui.cui31f7eab2024-05-17 09:11:12 +0000191 bl2_copy = mtd_store_boot_copy_num(BOOT_BL2);
192 if (!bl2_copy)
193 return -1;
194 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BB1ST].size,
195 ((BOOT_TOTAL_PAGES / bl2_copy) * ssp->sip.nsp.page_size));
Bichao Zheng5e306f72024-06-16 10:35:35 +0800196 boot_entry[BOOT_AREA_BL2E].offset = MTD_RSV_START_BLOCK * ssp->sip.nsp.block_size
zhikui.cui31f7eab2024-05-17 09:11:12 +0000197 + ssp->sip.nsp.layout_reserve_size;
198
199 if (store_boot_layout_is_discrete_all()) {
200 align_size = ssp->sip.nsp.block_size;
201 cal_copy = ssp->boot_backups;
202 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BL2E].size,
203 align_size);
204 }
205 i = BOOT_AREA_BL2X;
Bichao Zheng8951e442023-07-13 13:21:29 +0800206 } else if (ssp->boot_device == BOOT_EMMC) {
zhikui.cui31f7eab2024-05-17 09:11:12 +0000207 ssp->boot_entry[BOOT_AREA_BB1ST].offset =
208 boot_entry[BOOT_AREA_BB1ST].offset += BL2_CORE_BASE_OFFSET_EMMC;
Bichao Zheng8951e442023-07-13 13:21:29 +0800209 } else if (ssp->boot_device == BOOT_SNOR &&
Bichao Zhengd9e46b22023-07-27 21:26:18 +0800210 ((cpu_id.family_id == MESON_CPU_MAJOR_ID_A4) ||
Feng Chen32c0fb22024-01-12 20:50:41 +0800211 (cpu_id.family_id == MESON_CPU_MAJOR_ID_S1A) ||
zhikui.cui01923c82024-04-20 05:58:16 +0000212 (cpu_id.family_id == MESON_CPU_MAJOR_ID_S7) ||
213 (cpu_id.family_id == MESON_CPU_MAJOR_ID_S7D))) {
zhikui.cui31f7eab2024-05-17 09:11:12 +0000214 boot_entry[BOOT_AREA_BB1ST].size += 0x200;
215 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[BOOT_AREA_BB1ST].size,
216 align_size);
Bo Lv72d0e902023-01-02 14:27:34 +0000217 }
Bichao Zheng8951e442023-07-13 13:21:29 +0800218
zhikui.cui31f7eab2024-05-17 09:11:12 +0000219 ssp->boot_entry[BOOT_AREA_BB1ST].size = boot_entry[BOOT_AREA_BB1ST].size;
220 ssp->boot_entry[BOOT_AREA_BL2E].size = boot_entry[BOOT_AREA_BL2E].size;
Bichao Zheng5e306f72024-06-16 10:35:35 +0800221 ssp->boot_entry[BOOT_AREA_BL2E].offset = boot_entry[BOOT_AREA_BL2E].offset;
Bo Lv72d0e902023-01-02 14:27:34 +0000222
zhikui.cui31f7eab2024-05-17 09:11:12 +0000223 for (;i <= BOOT_AREA_DEVFIP; i++) {
Bo Lv72d0e902023-01-02 14:27:34 +0000224 STORAGE_ROUND_UP_IF_UNALIGN(boot_entry[i].size, align_size);
zhikui.cui31f7eab2024-05-17 09:11:12 +0000225 boot_entry[i].offset = boot_entry[i - 1].offset +
226 boot_entry[i - 1].size * cal_copy;
Bo Lv72d0e902023-01-02 14:27:34 +0000227 ssp->boot_entry[i].size = boot_entry[i].size;
228 ssp->boot_entry[i].offset = boot_entry[i].offset;
229 }
230
231 return 0;
232}
233
234/* use STORAGE_ROUND_UP, y must be power of 2 */
235#define STORAGE_ROUND_UP_IF_UNALIGN(x, y) ((x) = (((x) + (y) - 1) & (~(y - 1))))
Bo Lv72d0e902023-01-02 14:27:34 +0000236#define ALIGN_SIZE (4096)
jinbiao9f9847e2023-08-09 09:27:07 +0000237static int storage_boot_layout_general_setting(struct boot_layout *boot_layout, int need_build)
Bo Lv72d0e902023-01-02 14:27:34 +0000238{
239 struct storage_startup_parameter *ssp = &g_ssp;
240 boot_area_entry_t *boot_entry = boot_layout->boot_entry;
241 struct storage_boot_entry *sbentry = ssp->boot_entry;
jinbiao9f9847e2023-08-09 09:27:07 +0000242 p_payload_info_t p_info = parse_uboot_sheader(ubootdata);
243 p_payload_info_hdr_t hdr;
244 p_payload_info_item_t p_item;
245 int off_payload = 0, sz_payload = 0;
Bo Lv72d0e902023-01-02 14:27:34 +0000246 unsigned int bl2e_size = 0, bl2x_size = 0;
247 char name[8] = {0};
jinbiao9f9847e2023-08-09 09:27:07 +0000248 int n_index = 0;
Bo Lv72d0e902023-01-02 14:27:34 +0000249
Bo Lv72d0e902023-01-02 14:27:34 +0000250 if (need_build == BOOT_ID_USB) {
jinbiao9f9847e2023-08-09 09:27:07 +0000251 if (!p_info)
252 return -1;
253
254 hdr = &p_info->hdr;
255 p_item = p_info->arrItems;
256
257 for (n_index = 1, p_item += 1; n_index < hdr->byItemNum; ++n_index, ++p_item) {
258 memcpy(name, &p_item->nMagic, sizeof(unsigned int));
259 off_payload = p_item->nOffset;
260 if (n_index == BOOT_AREA_BL2E)
261 bl2e_size = p_item->nPayLoadSize;
262 if (n_index == BOOT_AREA_BL2X)
263 bl2x_size = p_item->nPayLoadSize;
264 sz_payload = p_item->nPayLoadSize;
zhikui.cui31f7eab2024-05-17 09:11:12 +0000265 pr_info("Item[%d]%4s offset 0x%08x sz 0x%x\n",
266 n_index, name, off_payload, sz_payload);
Bo Lv72d0e902023-01-02 14:27:34 +0000267 }
zhikui.cui31f7eab2024-05-17 09:11:12 +0000268
269 boot_entry[BOOT_AREA_BB1ST].size =
270 ssp->boot_entry[BOOT_AREA_BB1ST].size;
271 boot_entry[BOOT_AREA_BL2E].size = bl2e_size;
272 boot_entry[BOOT_AREA_BL2X].size = bl2x_size;
273 boot_entry[BOOT_AREA_DDRFIP].size =
274 ssp->boot_entry[BOOT_AREA_DDRFIP].size;
275 boot_entry[BOOT_AREA_DEVFIP].size =
276 ssp->boot_entry[BOOT_AREA_DEVFIP].size;
277 storage_boot_layout_rebuild(boot_layout);
Bo Lv72d0e902023-01-02 14:27:34 +0000278 } else {
279 /* may be sdcard boot and also have to rebuild layout */
280 if (need_build == BOOT_ID_SDCARD) {
281 bl2e_size = sbentry[BOOT_AREA_BL2E].size;
282 bl2x_size = sbentry[BOOT_AREA_BL2X].size;
zhikui.cui31f7eab2024-05-17 09:11:12 +0000283 printf("bl2e_size=%x bl2x_size=%x current->type=%d\n",
284 bl2e_size, bl2x_size, current->type);
285 boot_entry[BOOT_AREA_BB1ST].size =
286 ssp->boot_entry[BOOT_AREA_BB1ST].size;
287 boot_entry[BOOT_AREA_BL2E].size = bl2e_size;
288 boot_entry[BOOT_AREA_BL2X].size = bl2x_size;
289 boot_entry[BOOT_AREA_DDRFIP].size =
290 ssp->boot_entry[BOOT_AREA_DDRFIP].size;
291 boot_entry[BOOT_AREA_DEVFIP].size =
292 ssp->boot_entry[BOOT_AREA_DEVFIP].size;
293 storage_boot_layout_rebuild(boot_layout);
Bo Lv72d0e902023-01-02 14:27:34 +0000294 return 0;
295 }
296 /* normal boot */
Bichao Zheng5ced7b52023-07-25 15:20:20 +0800297 for (n_index = 0; n_index <= BOOT_AREA_DEVFIP; n_index++, sbentry++) {
jinbiao9f9847e2023-08-09 09:27:07 +0000298 boot_entry[n_index].size = sbentry->size;
299 boot_entry[n_index].offset = sbentry->offset;
Bo Lv72d0e902023-01-02 14:27:34 +0000300 }
301 }
302
303 return 0;
304}
305
306uint8_t emmc_boot_seqs_tbl[8][2] = {
307 {0, 3}, {0, 2}, {0, 3}, {0, 1},
308 {1, 2}, {1, 1}, {2, 1}, {0, 0}
309 };
310
311static int _get_emmc_boot_seqs(void)
312{
313 uint8_t ebcfg = 0;
314 if (IS_FEAT_DIS_EMMC_USER())
315 ebcfg |= (1<<2);
316 if (IS_FEAT_DIS_EMMC_BOOT_0())
317 ebcfg |= (1<<1);
318 if (IS_FEAT_DIS_EMMC_BOOT_1())
319 ebcfg |= (1<<0);
320
321 return ebcfg;
322}
323
324static int storage_get_emmc_boot_seqs(void)
325{
326 return emmc_boot_seqs_tbl[_get_emmc_boot_seqs()][1];;
327}
328
329static int storage_get_emmc_boot_start(void)
330{
331 return emmc_boot_seqs_tbl[_get_emmc_boot_seqs()][0];;
332}
333
Bo Lv72d0e902023-01-02 14:27:34 +0000334#define NSP_PAGE0_DISABLE 1
335extern unsigned char *ubootdata;
336static int storage_get_and_parse_ssp(int *need_build) // boot_device:
337{
338 struct storage_startup_parameter *ssp = &g_ssp;
339 union storage_independent_parameter *sip;
340 static struct param_e *storage_param_e;
341 int usb_boot = *need_build;
342
343 memset(ssp, 0, sizeof(struct storage_startup_parameter));
344 if (!usb_boot) {
345 storage_param_e = param_of(STORAGE_PARAM_TYPE);
346 if (!storage_param_e)
347 return -1;
348 memcpy(ssp, storage_param_e->data,
349 sizeof(struct storage_startup_parameter));
350 /* may be sdcard boot and also have to rebuild layout */
351 if (ssp->boot_device == BOOT_ID_SDCARD ||
352 ssp->boot_device == BOOT_ID_USB) {
353 /* need change the storage base here */
354 *need_build = ssp->boot_device;
355 }
356 }
357
358 if (*need_build) {
359 sip = &ssp->sip;
360 ssp->boot_device = current->type;
361 switch (ssp->boot_device) {
362 case BOOT_EMMC:
363 ssp->boot_backups = storage_get_emmc_boot_seqs();
zhikui.cui31f7eab2024-05-17 09:11:12 +0000364 ssp->boot_layout = BOOT_DISCRETE_DEFAULT;
Bo Lv72d0e902023-01-02 14:27:34 +0000365 break;
366 case BOOT_SNOR:
zhikui.cui31f7eab2024-05-17 09:11:12 +0000367 ssp->boot_layout = BOOT_DISCRETE_ALL;
Bo Lv72d0e902023-01-02 14:27:34 +0000368 if (IS_FEAT_EN_4BL2_SNOR())
369 ssp->boot_backups = 4;
370 else if (IS_FEAT_DIS_NBL2_SNOR())
371 ssp->boot_backups = 1;
372 else
Bichao Zheng8951e442023-07-13 13:21:29 +0800373 ssp->boot_backups = 1; /* Default 2 backup, consistent with rom */
Bo Lv72d0e902023-01-02 14:27:34 +0000374 break;
375 case BOOT_SNAND:
Bo Lv72d0e902023-01-02 14:27:34 +0000376 case BOOT_NAND_MTD:
zhikui.cui4333f3f2024-08-12 07:19:21 +0000377 ssp->boot_backups = CONFIG_NAND_TPL_COPY_NUM;
zhikui.cui31f7eab2024-05-17 09:11:12 +0000378 #ifdef BOARD_BOOT_LAYOUT_DISCRETE_BL2
379 ssp->boot_layout = BOOT_DISCRETE_BL2;
380 #else
381 ssp->boot_layout = BOOT_DISCRETE_ALL;
382 #endif
Bo Lv72d0e902023-01-02 14:27:34 +0000383 sip->nsp.page_size = current->info.write_unit;
384 sip->nsp.block_size = current->info.erase_unit;
385 sip->nsp.pages_per_block =
386 current->info.erase_unit / current->info.write_unit;
387 sip->nsp.layout_reserve_size =
Bichao Zheng5ced7b52023-07-25 15:20:20 +0800388 MTD_RSV_BLOCK_CNT * sip->nsp.block_size;
Bo Lv72d0e902023-01-02 14:27:34 +0000389 sip->nsp.page0_disable = NSP_PAGE0_DISABLE;
390 break;
391 default:
392 /* do nothing. */
393 break;
394 }
395
396 }
397
398 /* sanity check */
Bo Lv72d0e902023-01-02 14:27:34 +0000399 printf("boot_device:%d\n", ssp->boot_device);
400 printf("boot_seq:%d\n", ssp->boot_seq);
401 printf("boot_backups:%d\n", ssp->boot_backups);
402 printf("rebuild_id :%d\n", *need_build);
403
404 return 0;
405}
406
407int storage_post_init(void)
408{
409 int ret = -1;
410 int need_build = 0;
411
412 ret = storage_get_and_parse_ssp(&need_build);
413 if (ret < 0)
414 return -1;
jinbiao9f9847e2023-08-09 09:27:07 +0000415
416 ret = storage_boot_layout_general_setting(&general_boot_layout, need_build);
417 if (ret < 0)
418 return ret;
419
Bo Lv72d0e902023-01-02 14:27:34 +0000420 storage_boot_layout_debug_info(&general_boot_layout);
421
422 return ret;
423}
424
425int store_init(u32 init_flag)
426{
427 int i, ret = 0;
428 u8 record = 0;
429
430 /*1. pre scan*/
431 for (i = 0; i < ARRAY_SIZE(device_list); i++) {
432 if (!device_list[i].pre()) {
433 record |= BIT(i);
434 }
435 }
436
Bichao Zheng8951e442023-07-13 13:21:29 +0800437 pr_info("record = 0x%x\n", record);
438
Bo Lv72d0e902023-01-02 14:27:34 +0000439 if (!record) {
440 pr_info("No Valid storage device\n");
441 return record;
442 }
443
jinbiao9f9847e2023-08-09 09:27:07 +0000444 if (BOOTLOADER_MODE_ADVANCE_INIT) {
445 ret = storage_post_init();
Ruixuan.li53ff14f2023-10-24 16:34:01 +0800446 if (ret)
447 pr_info("storage_post_init failed\n");
jinbiao9f9847e2023-08-09 09:27:07 +0000448 }
Bo Lv72d0e902023-01-02 14:27:34 +0000449
450 /*2. Enter the probe of the valid device*/
451 for (i = 0; i < ARRAY_SIZE(device_list); i++) {
452 if (record & BIT(i)) {
453 ret = device_list[i].probe(init_flag);
454 if (ret)
455 pr_info("the 0x%x storage device probe failed\n",
456 device_list[i].index);
457 }
458 }
459
460 return record;
461}
462
463static struct storage_t *store_get_current(void)
464{
465 return current;
466}
467
468int store_set_device(enum boot_type_e type)
469{
470 struct list_head *entry;
471 struct storage_t *dev, *store_dev = store_get_current();
472
473 if (!store_dev) {
474 pr_info("%s %d no current device\n", __func__, __LINE__);
475 return 1;
476 }
477 if (store_dev->type == type)
478 return 0;
479 list_for_each(entry, &store_dev->list) {
480 dev = list_entry(entry, struct storage_t, list);
481 if (dev->type == type) {
482 current = dev;
483 return 0;
484 }
485 }
486 pr_info("%s %d please confirm the %d device is valid\n",
487 __func__, __LINE__, type);
488 return 1;
489}
490
491enum boot_type_e store_get_type(void)
492{
493 struct storage_t *store = store_get_current();
494
495 if (!store) {
496 pr_info("%s %d please init storage device first\n",
497 __func__, __LINE__);
498 return BOOT_NONE;
499 }
500
501 return store->type;
502}
503
504int store_get_device_info(struct storage_info_t *info)
505{
506 struct storage_t *store = store_get_current();
507
508 if (!store) {
509 pr_info("%s %d please init storage device first\n",
510 __func__, __LINE__);
511 return 1;
512 }
513
514 memcpy((char *)info, (char *)&store->info,
515 sizeof(struct storage_info_t));
516 return 0;
517}
518
519int store_get_device_bootloader_mode(void)
520{
521 struct storage_t *store = store_get_current();
522
523 if (!store) {
524 pr_info("%s %d please init storage device first\n",
525 __func__, __LINE__);
526 return -1;
527 }
528 return store->info.mode;
529}
530
531int store_read(const char *name, loff_t off, size_t size, void *buf)
532{
533 struct storage_t *store = store_get_current();
534
535 if (!store) {
536 pr_info("%s %d please init storage device first\n",
537 __func__, __LINE__);
538 return 1;
539 }
540 return store->read(name, off, size, buf);
541}
542
543int store_write(const char *name, loff_t off, size_t size, void *buf)
544{
545 struct storage_t *store = store_get_current();
546
547 if (!store) {
548 pr_info("%s %d please init storage device first\n",
549 __func__, __LINE__);
550 return 1;
551 }
552 return store->write(name, off, size, buf);
553}
554
555int store_erase(const char *name, loff_t off, size_t size, int scrub)
556{
557 struct storage_t *store = store_get_current();
558
559 if (!store) {
560 pr_info("%s %d please init storage device first\n",
561 __func__, __LINE__);
562 return 1;
563 }
564 return store->erase(name, off, size, scrub);
565}
566
567u64 store_part_size(const char *name)
568{
569 struct storage_t *store = store_get_current();
570
571 if (!store) {
572 pr_info("%s %d please init storage device first\n",
573 __func__, __LINE__);
574 return 1;
575 }
576 return store->get_part_size(name);
577}
578
579u8 store_boot_copy_num(const char *name)
580{
581 struct storage_t *store = store_get_current();
582
583 if (!store) {
584 pr_info("%s %d please init storage device first\n",
585 __func__, __LINE__);
586 return 1;
587 }
588 return store->get_copies(name);
589}
590
591
592#ifndef SYSCTRL_SEC_STATUS_REG2
593static u32 fake_reg = 0;
594#define SYSCTRL_SEC_STATUS_REG2 (&fake_reg)
595#endif
596u8 store_boot_copy_start(void)
597{
598 struct storage_t *store = store_get_current();
599
600 if (!store) {
601 pr_info("%s %d please init storage device first\n",
602 __func__, __LINE__);
603 return 0;
604 }
605 if (store->type != BOOT_EMMC)
606 return 0;
607 /* new arch since sc2 */
608 if (BOOTLOADER_MODE_ADVANCE_INIT)
609 return storage_get_emmc_boot_start();
610 return 0;
611}
612
613u8 store_bootup_bootidx(const char *name)
614{
615 u8 bl2_idx = 0, fip_idx = 0;
616 u32 val = 0;
617
618 /* new arch since sc2 */
619 if (BOOTLOADER_MODE_ADVANCE_INIT) {
620 bl2_idx = readl(SYSCTRL_SEC_STATUS_REG2) & 0xF;
621 //TODO: fixme after robust devfip is finished.
622 fip_idx = bl2_idx;
623 } else {
624 /* according to the:
625 commit 975b4acbcfa686601999d56843471d98e9c0a2cd
626 storage: robust boot: record bootlog in SEC_AO_SEC_GP_CFG2 [1/2]
627 PD#SWPL-4850
628 ...
629 record the bootup bl2/fip into SEC_AO_SEC_GP_CFG2
630 bit[27-25] bl2
631 bit[24-22] fip
632 */
633 val = readl(SEC_AO_SEC_GP_CFG2);
634 bl2_idx = (val >> 25) & 0x7;
635 fip_idx = (val >> 22) & 0x7;
636 }
637 if (!strncmp(name, "bl2", sizeof("bl2")) ||
638 !strncmp(name, "spl", sizeof("spl")))
639 return bl2_idx;
640 else
641 return fip_idx;
642}
643
644void store_restore_bootidx(void)
645{
646 /* new arch since sc2 */
647 if (BOOTLOADER_MODE_ADVANCE_INIT) {
648 extern void aml_set_bootsequence(uint32_t val);
649 aml_set_bootsequence(0x55);
650 }
651 return;
652}
653
654u64 store_boot_copy_size(const char *name)
655{
656 struct storage_t *store = store_get_current();
657
658 if (!store) {
659 pr_info("%s %d please init storage device first\n",
660 __func__, __LINE__);
661 return 1;
662 }
663 return store->get_copy_size(name);
664}
665
zhikui.cui31f7eab2024-05-17 09:11:12 +0000666static int _store_boot_read(const char *part_name, u8 cpy, size_t size, void *addr)
667{
668 struct storage_t *store = store_get_current();
669 int ret, read_len, tpl_offset = BL2_SIZE;
670
671 if (store_get_device_bootloader_mode() == COMPACT_BOOTLOADER)
672 return store->boot_read(part_name, cpy, size, (u_char *)addr);
673
674 read_len = size > tpl_offset ? tpl_offset : size;
675 ret = store->boot_read(BOOT_BL2, cpy, read_len, addr);
676 if (ret) {
677 pr_err("read bl2 fail\n");
678 goto out;
679 }
680
681 if (size <= BL2_SIZE)
682 goto out;
683
684 if (store_boot_layout_is_discrete_all()) {
685 p_payload_info_t pinfo = parse_uboot_sheader((u8 *)ubootdata);
686 p_payload_info_hdr_t hdr = &pinfo->hdr;
687 p_payload_info_item_t pitem = pinfo->arrItems;
688 tpl_offset = ALIGN(BL2_SIZE, ALIGN_SIZE);
689 int nindex, offset = 0;
690
691 for (nindex = 1, pitem += 1; nindex < hdr->byItemNum; ++nindex, ++pitem) {
692 pr_info("item[%d]%4s offset 0x%08x sz 0x%x\n",
693 nindex, (char *)&pitem->nMagic,
694 offset, pitem->nPayLoadSize);
695 if (!pitem->nPayLoadSize)
696 continue;
697
698 read_len = size > pitem->nPayLoadSize ? pitem->nPayLoadSize : size;
699 if (read_len > 0) {
700 ret = store->boot_read(general_boot_part_entry[nindex].name,
701 cpy,
702 read_len,
703 (u_char *)(addr + tpl_offset + offset));
704 if (ret) {
705 pr_info("Fail in flash payload %s\n",
706 general_boot_part_entry[nindex].name);
707 goto out;
708 }
709 }
710
711 offset += pitem->nPayLoadSize;
712 size -= read_len;
713 }
714
715 } else {
716 if (store_boot_layout_is_discrete_bl2())
717 tpl_offset = ALIGN(BL2_SIZE, ALIGN_SIZE);
718 ret = store->boot_read(BOOT_TPL,
719 cpy,
720 size - tpl_offset,
721 (u_char *)addr + tpl_offset);
722 if (ret) {
723 pr_err("read tpl fail\n");
724 goto out;
725 }
726 }
727
728out:
729 return ret;
730}
731
Bo Lv72d0e902023-01-02 14:27:34 +0000732int store_boot_read(const char *name, u8 copy, size_t size, void *buf)
733{
734 struct storage_t *store = store_get_current();
735
736 if (!store) {
737 pr_info("%s %d please init storage device first\n",
738 __func__, __LINE__);
739 return 1;
740 }
zhikui.cui31f7eab2024-05-17 09:11:12 +0000741
742 if (!strcmp(name, "bootloader") &&
743 (store->type == BOOT_SNAND ||
744 store->type == BOOT_NAND_MTD))
745 return _store_boot_read(name, copy, size, buf);
746
Bo Lv72d0e902023-01-02 14:27:34 +0000747 return store->boot_read(name, copy, size, buf);
748}
749
zhikui.cui31f7eab2024-05-17 09:11:12 +0000750static int bl2x_mode_check_header(p_payload_info_t pInfo)
751{
752 p_payload_info_hdr_t hdr = &pInfo->hdr;
753 const int nItemNum = hdr->byItemNum;
754 p_payload_info_item_t pItem = pInfo->arrItems;
755 u8 i = 0, bl2_copy;
756 int sz_payload = 0;
757 uint64_t align_size = 1;
758 struct storage_startup_parameter *ssp = &g_ssp;
759
760 printf("\naml log : info parse...\n");
761 printf("\tsztimes : %s\n",hdr->szTimeStamp);
762 printf("\tversion : %d\n",hdr->byVersion);
763 printf("\tItemNum : %d\n",nItemNum);
764 printf("\tSize : %d(0x%x)\n", hdr->nSize, hdr->nSize);
765 if (nItemNum > MAX_BOOT_AREA_ENTRIES || nItemNum < 3) {
766 pr_info("illegal nitem num %d\n", nItemNum);
767 return __LINE__;
768 }
769 if (ssp->boot_device == BOOT_NAND_MTD ||
770 ssp->boot_device == BOOT_SNAND) {
771 bl2_copy = mtd_store_boot_copy_num(BOOT_BL2);
772 if (!bl2_copy)
773 return __LINE__;
774 align_size = (uint64_t)(BOOT_TOTAL_PAGES / bl2_copy) * ssp->sip.nsp.page_size;
775 }
776
777 sz_payload = pItem->nPayLoadSize;
778 STORAGE_ROUND_UP_IF_UNALIGN(sz_payload, align_size);
779 if (sz_payload > ssp->boot_entry[0].size)
780 return __LINE__;
781 if (ssp->boot_device == BOOT_NAND_MTD ||
782 ssp->boot_device == BOOT_SNAND)
783 align_size = ssp->sip.nsp.block_size;
784
785 ++pItem;
786
787 for (i = 1; i < nItemNum; i++, ++pItem) {
788 sz_payload = pItem->nPayLoadSize;
789 STORAGE_ROUND_UP_IF_UNALIGN(sz_payload, align_size);
790 if (sz_payload > ssp->boot_entry[i].size)
791 return __LINE__;
792 }
793
794 return 0;
795}
796
797static int _store_boot_write(const char *part_name, u8 cpy, size_t size, void *addr)
798{
799 int ret = 0;
800 struct storage_t *store = store_get_current();
801
802 if (store_get_device_bootloader_mode() == COMPACT_BOOTLOADER)
803 return store->boot_write(part_name, cpy, size, (u_char *)addr);
804
805 if (store_get_device_bootloader_mode() == DISCRETE_BOOTLOADER) {
806 if (size > (BL2_SIZE + CONFIG_TPL_SIZE_PER_COPY)) {
807 pr_info("bootloader sz 0x%lx too large,max sz 0x%x\n",
808 size, BL2_SIZE + CONFIG_TPL_SIZE_PER_COPY);
809 return CMD_RET_FAILURE;
810 }
811 }
812
813 if ((cpy >= mtd_store_boot_copy_num(BOOT_TPL) ||
814 cpy >= mtd_store_boot_copy_num(BOOT_BL2)) &&
815 cpy != BOOT_OPS_ALL) {
816 pr_info("update copy %d invalid, must < min(%d, %d)\n",
817 cpy, mtd_store_boot_copy_num(BOOT_TPL),
818 mtd_store_boot_copy_num(BOOT_BL2));
819 return CMD_RET_FAILURE;
820 }
821
822 if (store_boot_layout_is_discrete_all()) {
823 p_payload_info_t pinfo = parse_uboot_sheader((u8 *)addr);
824 p_payload_info_hdr_t hdr = &pinfo->hdr;
825 p_payload_info_item_t pitem = pinfo->arrItems;
826 char name[8];
827 int nindex = 0, off_payload = 0, sz_payload = 0;
828
829 if (bl2x_mode_check_header(pinfo)) {
830 pr_info("!!!warning bl2xx size is bigger than bl2x layout size\n");
831 pr_info("please check bl2x,or erase flash and turn off\n");
832 pr_info("then turn on, and update uboot again\n");
833 return CMD_RET_FAILURE;
834 }
835
836 memset(name, 0, 8);
837 for (nindex = 1, pitem += 1; nindex < hdr->byItemNum; ++nindex, ++pitem) {
838 memcpy(name, &pitem->nMagic, sizeof(unsigned int));
839 off_payload = pitem->nOffset;
840 sz_payload = pitem->nPayLoadSize;
841 pr_info("item[%d]%4s offset 0x%08x sz 0x%x\n",
842 nindex, name, off_payload, sz_payload);
843 if (!sz_payload)
844 continue;
845 ret = store->boot_write(general_boot_part_entry[nindex].name,
846 cpy,
847 sz_payload,
848 (u_char *)(addr + off_payload));
849 if (ret) {
850 pr_info("Fail in flash payload %s\n", name);
851 return CMD_RET_FAILURE;
852 }
853 }
854 } else if (store_boot_layout_is_discrete_bl2()) {
855 ret = store->boot_write("tpl",
856 cpy,
857 size - ALIGN(BL2_SIZE, ALIGN_SIZE),
858 (u_char *)(addr + ALIGN(BL2_SIZE, ALIGN_SIZE)));
859 } else {
860 ret = store->boot_write("tpl",
861 cpy,
862 size - BL2_SIZE,
863 (u_char *)(addr + BL2_SIZE));
864 if (ret) {
865 pr_info("failed update tpl\n");
866 return CMD_RET_FAILURE;
867 }
868
869 }
870
871 ret = store->boot_write("bl2", cpy, BL2_SIZE, (u_char *)addr);
872 if (ret) {
873 pr_info("Fail in flash payload bl2\n");
874 return CMD_RET_FAILURE;
875 }
876
877 return ret;
878}
879
Bo Lv72d0e902023-01-02 14:27:34 +0000880int store_boot_write(const char *name, u8 copy, size_t size, void *buf)
881{
882 struct storage_t *store = store_get_current();
883
884 if (!store) {
885 pr_info("%s %d please init storage device first\n",
886 __func__, __LINE__);
887 return 1;
888 }
zhikui.cui31f7eab2024-05-17 09:11:12 +0000889
890 if (!strcmp(name, "bootloader") &&
891 (store->type == BOOT_SNAND ||
892 store->type == BOOT_NAND_MTD))
893 return _store_boot_write(name, copy, size, buf);
894
Bo Lv72d0e902023-01-02 14:27:34 +0000895 return store->boot_write(name, copy, size, buf);
896}
897
898int store_boot_erase(const char *name, u8 copy)
899{
900 struct storage_t *store = store_get_current();
901
902 if (!store) {
903 pr_info("%s %d please init storage device first\n",
904 __func__, __LINE__);
905 return 1;
906 }
907 return store->boot_erase(name, copy);
908}
909
910int store_gpt_read(void *buf)
911{
912 struct storage_t *store = store_get_current();
913
914 if (!store) {
915 pr_info("%s %d please init storage device first\n",
916 __func__, __LINE__);
917 return 1;
918 }
919 if (!store->gpt_read)
920 return 1;
921 return store->gpt_read(buf);
922}
923
924int store_gpt_write(void *buf)
925{
926 struct storage_t *store = store_get_current();
927
928 if (!store) {
929 pr_info("%s %d please init storage device first\n",
930 __func__, __LINE__);
931 return 1;
932 }
933 if (!store->gpt_write)
934 return 1;
935 return store->gpt_write(buf);
936}
937
938int store_gpt_erase(void)
939{
940 struct storage_t *store = store_get_current();
941
942 if (!store) {
943 pr_info("%s %d please init storage device first\n",
944 __func__, __LINE__);
945 return 1;
946 }
947 if (!store->gpt_erase)
948 return 1;
949 return store->gpt_erase();
950}
951
jinbiaod6719f12023-12-22 05:34:45 +0000952int store_boot_copy_enable(int index)
953{
954 struct storage_t *store = store_get_current();
955
956 if (!store) {
957 pr_info("%s %d please init storage device first\n",
958 __func__, __LINE__);
959 return -1;
960 }
961 if (!store->boot_copy_enable)
962 return -1;
963 return store->boot_copy_enable(index);
964}
965
Bo Lv72d0e902023-01-02 14:27:34 +0000966u32 store_rsv_size(const char *name)
967{
968 struct storage_t *store = store_get_current();
969
970 if (!store) {
971 pr_info("%s %d please init storage device first\n",
972 __func__, __LINE__);
973 return 1;
974 }
975 return store->get_rsv_size(name);
976}
977
978int store_rsv_read(const char *name, size_t size, void *buf)
979{
980 struct storage_t *store = store_get_current();
981
982 if (!store) {
983 pr_info("%s %d please init storage device first\n",
984 __func__, __LINE__);
985 return 1;
986 }
987 return store->read_rsv(name, size, buf);
988}
989
990int store_rsv_write(const char *name, size_t size, void *buf)
991{
992 struct storage_t *store = store_get_current();
993
994 if (!store) {
995 pr_info("%s %d please init storage device first\n",
996 __func__, __LINE__);
997 return 1;
998 }
999 return store->write_rsv(name, size, buf);
1000}
1001
1002int store_rsv_erase(const char *name)
1003{
1004 struct storage_t *store = store_get_current();
1005
1006 if (!store) {
1007 pr_info("%s %d please init storage device first\n",
1008 __func__, __LINE__);
1009 return 1;
1010 }
1011 return store->erase_rsv(name);
1012}
1013
1014int store_rsv_protect(const char *name, bool ops)
1015{
1016 struct storage_t *store = store_get_current();
1017
1018 if (!store) {
1019 pr_info("%s %d please init storage device first\n",
1020 __func__, __LINE__);
1021 return 1;
1022 }
1023 return store->protect_rsv(name, ops);
1024}
1025
1026static int do_store_init(cmd_tbl_t *cmdtp,
1027 int flag, int argc, char * const argv[])
1028{
1029 u32 init_flag = 1;
1030 u8 ret = 0;
1031
1032 if (unlikely(argc != 2 && argc != 3))
1033 return CMD_RET_USAGE;
1034
1035 if (argc == 3)
1036 init_flag = simple_strtoul(argv[2], NULL, 10);
1037
1038 /*Returns a nonzero value: device index*/
1039 if (store_init(init_flag))
1040 ret = 0;
1041 else ret = 1;
1042 return ret;
1043}
1044
1045void store_print_device(struct storage_t *store_dev)
1046{
1047 int i;
1048
1049 for (i = 0; i < ARRAY_SIZE(device_list); i++)
1050 if (store_dev->type & device_list[i].index)
1051 pr_info("device type: [%s]\n", device_list[i].type);
1052 pr_info("name %s\n", store_dev->info.name);
1053 pr_info("id :");
1054 for (i = 0; i < ARRAY_SIZE(store_dev->info.id); i++)
1055 pr_info(" 0x%x", store_dev->info.id[i]);
1056 pr_info("\n");
1057 pr_info("read unit %d\n", store_dev->info.read_unit);
1058 pr_info("write unit %d\n", store_dev->info.write_unit);
1059 pr_info("erase unit %d\n", store_dev->info.erase_unit);
1060 pr_info("total size %lld\n", store_dev->info.caps);
1061 if (store_dev->info.mode)
1062 pr_info("bootloader in discrete mode : %d\n",
1063 store_dev->info.mode);
1064 else
1065 pr_info("bootloader in compact mode : %d\n",
1066 store_dev->info.mode);
1067}
1068
1069static int do_store_device(cmd_tbl_t *cmdtp,
1070 int flag, int argc, char * const argv[])
1071{
1072 if (argc == 2) {
1073 struct storage_t *store_dev, *dev;
1074 struct list_head *entry;
1075
1076 store_dev = store_get_current();
1077 pr_info("current device:\n");
1078 pr_info("----------------------------------\n");
1079 store_print_device(store_dev);
1080 pr_info("----------------------------------\n");
1081 list_for_each(entry, &store_dev->list) {
1082 dev = list_entry(entry, struct storage_t, list);
1083 pr_info("valid device:\n");
1084 pr_info("----------------------------------\n");
1085 store_print_device(dev);
1086 pr_info("----------------------------------\n");
1087 }
1088 return 0;
1089 } else if (argc == 3) {
1090 char *name = NULL;
1091 int i = 0, ret = 0;
1092 name = argv[2];
1093 for (i = 0; i < ARRAY_SIZE(device_list); i++)
1094 if (!strcmp(name, device_list[i].type)) {
1095
1096 ret = store_set_device(device_list[i].index);
1097 if (!ret) {
1098 pr_info("now current device is: %s\n",
1099 name);
1100 return 0;
1101 }
1102 }
1103 pr_info("%s %d no such device: %s\n",
1104 __func__, __LINE__, name);
1105 return ret;
1106 }
1107 return CMD_RET_USAGE;
1108}
1109
1110static int do_store_partition(cmd_tbl_t *cmdtp,
1111 int flag, int argc, char * const argv[])
1112{
1113 struct storage_t *store_dev;
1114 int i = 0, partitions = 0;
1115 int ret = 0;
1116 char name[16];
1117
1118 if (argc > 2)
1119 return CMD_RET_USAGE;
1120 else {
1121 store_dev = store_get_current();
1122 if (store_dev->get_part_count)
1123 partitions = store_dev->get_part_count();
1124 pr_info("%d partitions of device %s:\n",
1125 partitions, store_dev->info.name);
1126
1127 if (store_dev->list_part_name)
1128 ret = store_dev->list_part_name(i, name);
1129
1130 return ret;
1131 }
1132}
1133
1134#ifdef CONFIG_AML_MTD
1135extern int is_mtd_store_boot_area(const char *part_name);
1136#endif
1137static int do_store_erase(cmd_tbl_t *cmdtp,
1138 int flag, int argc, char * const argv[])
1139{
1140 struct storage_t *store = store_get_current();
1141 unsigned long offset;
1142 size_t size = 0;
1143 char *name = NULL;
1144 char *s;
1145 int erase_flag = 0, ret;
1146 unsigned long time;
1147
1148 const char *scrub =
1149 "Warning: scrub_flag is 1!!!!"
1150 "scrub operation!!!\n"
1151 "will erase oob area\n"
1152 "There is no reliable way to recover them.\n"
1153 " "
1154 "are sure of what you are doing!\n"
1155 "\nReally erase this NAND flash? <y/N>\n";
1156
1157 if (!store) {
1158 pr_info("%s %d please init your storage device first!\n",
1159 __func__, __LINE__);
1160 return CMD_RET_FAILURE;
1161 }
1162
1163 if (!strncmp(argv[1], "scrub", 5)) {
1164 erase_flag |= STORE_SCRUB;
1165 puts(scrub);
1166 if (!confirm_yesno()) {
1167 printf("erase aborted\n");
1168 return 1;
1169 }
1170 }
1171
1172 /*store erase.chip*/
1173 s = strchr(argv[1], '.');
1174 if (s != NULL && strcmp(s, ".chip") == 0) {
1175 if (argc == 3 && !simple_strtoul(argv[argc - 1], NULL, 16))
1176 erase_flag |= STORE_ERASE_DATA;
1177 else if ((argc == 3) && (simple_strtoul(argv[argc - 1], NULL, 16) == 1))
1178 erase_flag |= STORE_ERASE_RSV;
1179 else if (argc == 3)
1180 return CMD_RET_USAGE;
1181
1182 offset = 0;
1183 } else {
1184 /*store erase normal, partition name can't NULL*/
1185 if (unlikely(argc != 5))
1186 return CMD_RET_USAGE;
1187
1188 size = (size_t)simple_strtoul(argv[argc - 1], NULL, 16);
1189 offset = simple_strtoul(argv[argc - 2], NULL, 16);
1190 name = argv[2];
1191#ifdef CONFIG_AML_MTD
1192 if (is_mtd_store_boot_area(name)) {
1193 pr_info("%s %d please enter normal partition name except tpl area!\n",
1194 __func__, __LINE__);
1195 return CMD_RET_FAILURE;
1196 }
1197#endif
1198 }
1199
1200 time = get_timer(0);
1201 ret = store->erase(name, offset, size, erase_flag);
1202 time = get_timer(time);
1203
1204 if (size != 0)
1205 printf("%lu bytes ", size);
1206
1207 printf("erased in %lu ms", time);
1208 if ((time > 0) && (size != 0)) {
1209 puts(" (");
1210 print_size(div_u64(size, time) * 1000, "/s");
1211 puts(")");
1212 }
1213 puts("\n");
1214
1215 return ret;
1216}
1217
1218static int do_store_read(cmd_tbl_t *cmdtp,
1219 int flag, int argc, char * const argv[])
1220{
1221 struct storage_t *store = store_get_current();
1222 unsigned long offset, addr, time;
1223 size_t size;
1224 char *name = NULL;
1225 int ret;
1226
1227 if (!store) {
1228 pr_info("%s %d please init your storage device first!\n",
1229 __func__, __LINE__);
1230 return CMD_RET_FAILURE;
1231 }
1232
1233 if (unlikely(argc != 5 && argc != 6))
1234 return CMD_RET_USAGE;
1235
1236 addr = simple_strtoul(argv[2], NULL, 16);
1237 size = (size_t)simple_strtoul(argv[argc - 1], NULL, 16);
1238 offset = simple_strtoul(argv[argc - 2], NULL, 16);
1239 if (argc == 6)
1240 name = argv[3];
1241#ifdef CONFIG_AML_MTD
1242 if (is_mtd_store_boot_area(name)) {
1243 pr_info("%s %d please enter normal partition name except tpl area!\n",
1244 __func__, __LINE__);
1245 return CMD_RET_FAILURE;
1246 }
1247#endif
1248 time = get_timer(0);
1249 ret = store->read(name, offset, size, (u_char *)addr);
1250 time = get_timer(time);
1251
1252 if (size != 0)
1253 pr_info("%lu bytes ", size);
1254
1255 pr_info("read in %lu ms", time);
1256 if ((time > 0) && (size != 0)) {
1257 puts(" (");
1258 print_size(div_u64(size, time) * 1000, "/s");
1259 puts(")");
1260 }
1261 puts("\n");
1262
1263 return ret;
1264}
1265
1266static int name2index(struct boot_layout *boot_layout, const char *img)
1267{
1268 boot_area_entry_t *boot_entry = NULL;
1269 int i;
1270
1271 boot_entry = boot_layout->boot_entry;
Bichao Zheng5ced7b52023-07-25 15:20:20 +08001272 for (i = 1; i <= BOOT_AREA_DEVFIP; i++) {
Bo Lv72d0e902023-01-02 14:27:34 +00001273 if (!strncmp(img, boot_entry[i].name, strlen(boot_entry[i].name)))
1274 return i;
1275 }
1276
1277 return -1;
1278}
1279
1280static int do_store_write_bl2img(cmd_tbl_t *cmdtp,
1281 int flag, int argc, char * const argv[])
1282{
1283 struct storage_t *store = store_get_current();
1284 unsigned long offset, addr;
1285 size_t size, size_src;
1286 char *name = NULL;
1287 int ret = -1, index;
1288 struct boot_layout *boot_layout = &general_boot_layout;
1289
1290 if (!store) {
1291 pr_info("%s %d please init your storage device first!\n",
1292 __func__, __LINE__);
1293 return CMD_RET_FAILURE;
1294 }
1295
1296 addr = simple_strtoul(argv[2], NULL, 16);
1297 name = argv[3];
1298 size = simple_strtoul(argv[4], NULL, 16);
1299
1300 index = name2index(&general_boot_layout, name);
1301 offset = boot_layout->boot_entry[index].offset;
1302 size_src = boot_layout->boot_entry[index].size;
1303 printf("[%s] offset:0x%lx, index:%d\n", name, offset, index);
1304
1305 if (size_src != size)
1306 printf("new img size:0x%lx != img src:0x%lx\n", size, size_src);
1307
1308 ret = store->boot_write(name, offset, size, (u_char *)addr);
1309
1310 return ret;
1311}
1312
1313int store_write_bl2img(void* addr, const char *name, size_t size)
1314{
1315 struct storage_t *store = store_get_current();
1316 unsigned long offset;
1317 size_t size_src;
1318 int ret = -1, index;
1319 struct boot_layout *boot_layout = &general_boot_layout;
1320
1321 if (!store) {
1322 pr_info("%s %d please init your storage device first!\n",
1323 __func__, __LINE__);
1324 return CMD_RET_FAILURE;
1325 }
1326
1327 index = name2index(&general_boot_layout, name);
1328 offset = boot_layout->boot_entry[index].offset;
1329 size_src = boot_layout->boot_entry[index].size;
1330 printf("[%s] offset:0x%lx, index:%d\n", name, offset, index);
1331
1332 if (size_src != size)
1333 printf("new img size:0x%zx != img src:0x%zx\n", size, size_src);
1334
1335 ret = store->boot_write(name, offset, size, (u_char *)addr);
1336 if (size != 0)
1337 printf("[%s][%d]%lx bytes\n", __func__, __LINE__, size);
1338
1339 return ret;
1340}
1341
1342static int do_store_write(cmd_tbl_t *cmdtp,
1343 int flag, int argc, char * const argv[])
1344{
1345 struct storage_t *store = store_get_current();
1346 unsigned long offset, addr, time;
1347 size_t size;
1348 char *name = NULL;
1349 int ret;
1350
1351 if (!store) {
1352 pr_info("%s %d please init your storage device first!\n",
1353 __func__, __LINE__);
1354 return CMD_RET_FAILURE;
1355 }
1356
1357 if (unlikely(argc != 5 && argc != 6))
1358 return CMD_RET_USAGE;
1359
1360 addr = simple_strtoul(argv[2], NULL, 16);
1361 offset = simple_strtoul(argv[argc - 2], NULL, 16);
1362 size = (size_t)simple_strtoul(argv[argc - 1], NULL, 16);
1363 if (argc == 6)
1364 name = argv[3];
1365#ifdef CONFIG_AML_MTD
1366 if (is_mtd_store_boot_area(name)) {
1367 pr_info("%s %d please enter normal partition name except tpl area!\n",
1368 __func__, __LINE__);
1369 return CMD_RET_FAILURE;
1370 }
1371#endif
1372 time = get_timer(0);
1373 ret = store->write(name, offset, size, (u_char *)addr);
1374 time = get_timer(time);
1375
1376 if (size != 0)
1377 printf("%lu bytes ", size);
1378
1379 printf("write in %lu ms", time);
1380 if ((time > 0) && (size != 0)) {
1381 puts(" (");
1382 print_size(div_u64(size, time) * 1000, "/s");
1383 puts(")");
1384 }
1385 puts("\n");
1386
1387 return ret;
1388}
1389
1390static int do_store_boot_read(cmd_tbl_t *cmdtp,
1391 int flag, int argc, char * const argv[])
1392{
1393 struct storage_t *store = store_get_current();
1394 unsigned long addr;
1395 size_t size;
1396 u8 cpy;
1397 char *name;
1398
1399 if (!store) {
1400 pr_info("%s %d please init your storage device first!\n",
1401 __func__, __LINE__);
1402 return CMD_RET_FAILURE;
1403 }
1404
1405 if (unlikely(argc != 6))
1406 return CMD_RET_USAGE;
1407
1408 name = argv[2];
1409 addr = (unsigned long)simple_strtoul(argv[3], NULL, 16);
1410 cpy = (u8)simple_strtoul(argv[4], NULL, 16);
1411 size = (size_t)simple_strtoul(argv[5], NULL, 16);
1412
1413 return store->boot_read(name, cpy, size, (u_char *)addr);
1414}
1415
Bo Lv72d0e902023-01-02 14:27:34 +00001416static int do_store_boot_write(cmd_tbl_t *cmdtp,
1417 int flag, int argc, char * const argv[])
1418{
1419 struct storage_t *store = store_get_current();
1420 unsigned long addr;
1421 size_t size;
1422 u8 cpy = BOOT_OPS_ALL;
1423 char *name;
1424
1425 if (!store) {
1426 pr_info("%s %d please init your storage device first!\n",
1427 __func__, __LINE__);
1428 return CMD_RET_FAILURE;
1429 }
1430
1431 if (unlikely(argc != 5 && argc != 6))
1432 return CMD_RET_USAGE;
1433
1434 name = argv[2];
1435 addr = (unsigned long)simple_strtoul(argv[3], NULL, 16);
1436 size = (size_t)simple_strtoul(argv[argc - 1], NULL, 16);
1437 if (argc == 6)
1438 cpy = (u8)simple_strtoul(argv[4], NULL, 16);
1439
1440 if (strcmp(name, "bootloader") == 0) {
1441 return _store_boot_write(name, cpy, size, (u_char *)addr);
1442 }
1443
1444 return store->boot_write(name, cpy, size, (u_char *)addr);
1445}
1446
1447static int do_store_boot_erase(cmd_tbl_t *cmdtp,
1448 int flag, int argc, char * const argv[])
1449{
1450 struct storage_t *store = store_get_current();
1451 u8 cpy = BOOT_OPS_ALL;
1452 char *name;
1453
1454 if (!store) {
1455 pr_info("%s %d please init your storage device first!\n",
1456 __func__, __LINE__);
1457 return CMD_RET_FAILURE;
1458 }
1459
1460 if (unlikely(argc != 3 && argc != 4))
1461 return CMD_RET_USAGE;
1462
1463 name = argv[2];
1464 if (argc == 4)
1465 cpy = (u8)simple_strtoul(argv[3], NULL, 16);
1466
1467 return store->boot_erase(name, cpy);
1468}
1469
1470static int do_store_gpt_read(cmd_tbl_t *cmdtp,
1471 int flag, int argc, char * const argv[])
1472{
1473 struct storage_t *store = store_get_current();
1474 unsigned long addr;
1475 int ret;
1476
1477 if (!store) {
1478 pr_info("%s %d please init your storage device first!\n",
1479 __func__, __LINE__);
1480 return CMD_RET_FAILURE;
1481 }
1482
1483 if (unlikely(argc != 3))
1484 return CMD_RET_USAGE;
1485
1486 addr = simple_strtoul(argv[2], NULL, 16);
1487
1488 if (store->gpt_read) {
1489 ret = store->gpt_read((u_char *)addr);
1490 return ret;
1491 }
1492
1493 printf("read gpt is not prepared\n");
1494 return CMD_RET_USAGE;
1495}
1496
1497static int do_store_gpt_write(cmd_tbl_t *cmdtp,
1498 int flag, int argc, char * const argv[])
1499{
1500 struct storage_t *store = store_get_current();
1501 unsigned long addr;
1502 int ret;
1503
1504 if (!store) {
1505 pr_info("%s %d please init your storage device first!\n",
1506 __func__, __LINE__);
1507 return CMD_RET_FAILURE;
1508 }
1509
1510 if (unlikely(argc != 3))
1511 return CMD_RET_USAGE;
1512
1513 addr = simple_strtoul(argv[2], NULL, 16);
1514
1515 if (store->gpt_write) {
1516 ret = store->gpt_write((u_char *)addr);
1517 return ret;
1518 }
1519
1520 printf("write gpt is not prepared\n");
1521 return CMD_RET_USAGE;
1522}
1523
1524static int do_store_gpt_erase(cmd_tbl_t *cmdtp,
1525 int flag, int argc, char * const argv[])
1526{
1527 struct storage_t *store = store_get_current();
1528 int ret;
1529
1530 if (!store) {
1531 pr_info("%s %d please init your storage device first!\n",
1532 __func__, __LINE__);
1533 return CMD_RET_FAILURE;
1534 }
1535
1536 if (unlikely(argc != 2))
1537 return CMD_RET_USAGE;
1538
1539 if (store->gpt_erase) {
1540 ret = store->gpt_erase();
1541 return ret;
1542 }
1543
1544 printf("erase gpt is not prepared\n");
1545 return CMD_RET_USAGE;
1546}
1547
jinbiaod6719f12023-12-22 05:34:45 +00001548/*
1549 * Check whether the current boot can be written
1550 * ret: 0 disable; 1: enable
1551 */
1552static int do_store_boot_copy_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1553{
1554 enum boot_type_e medium_type = store_get_type();
1555 struct storage_t *store = store_get_current();
jinbiao325839f2024-03-29 09:34:08 +00001556 int ret = CMD_RET_USAGE, index;
jinbiaod6719f12023-12-22 05:34:45 +00001557
1558 if (!store) {
1559 pr_info("%s %d please init your storage device first!\n",
1560 __func__, __LINE__);
jinbiao325839f2024-03-29 09:34:08 +00001561 return ret;
jinbiaod6719f12023-12-22 05:34:45 +00001562 }
1563
1564 if (unlikely(argc != 3))
jinbiao325839f2024-03-29 09:34:08 +00001565 return ret;
jinbiaod6719f12023-12-22 05:34:45 +00001566
1567 if (medium_type != BOOT_EMMC) {
1568 printf("%s %d not eMMC boot\n", __func__, __LINE__);
jinbiao325839f2024-03-29 09:34:08 +00001569 return ret;
jinbiaod6719f12023-12-22 05:34:45 +00001570 }
1571
1572 index = simple_strtoul(argv[2], NULL, 16);
1573 if (store->boot_copy_enable) {
1574 ret = store->boot_copy_enable(index);
jinbiao325839f2024-03-29 09:34:08 +00001575 printf("%s\n", ret ? "enable" : "disable");
1576 } else
1577 printf("boot copy enable is not prepared\n");
jinbiaod6719f12023-12-22 05:34:45 +00001578
jinbiao325839f2024-03-29 09:34:08 +00001579 return ret;
jinbiaod6719f12023-12-22 05:34:45 +00001580}
1581
Bo Lv72d0e902023-01-02 14:27:34 +00001582static int do_store_rsv_ops(cmd_tbl_t *cmdtp,
1583 int flag, int argc, char * const argv[])
1584{
1585 struct storage_t *store = store_get_current();
1586 char *name = NULL;
1587
1588 if (!store) {
1589 pr_info("%s %d please init your storage device first!\n",
1590 __func__, __LINE__);
1591 return CMD_RET_FAILURE;
1592 }
1593
1594 if (!strcmp(argv[2], "erase")) {
1595 if (argc == 3)
1596 ;
1597 else if (argc == 4)
1598 name = argv[3];
1599 else
1600 return CMD_RET_USAGE;
1601 return store->erase_rsv(name);
1602 } else if (!strcmp(argv[2], "read") ||
1603 !strcmp(argv[2], "write")) {
1604 u8 cmd = strcmp(argv[2], "read") ? 0 : 1;
1605 unsigned long addr = simple_strtoul(argv[4], NULL, 16);
1606 size_t size = (size_t)simple_strtoul(argv[5], NULL, 16);
1607
1608 name = argv[3];
1609 if (unlikely(argc != 6))
1610 return CMD_RET_USAGE;
1611 if (cmd)
1612 return store->read_rsv(name, size, (u_char *)addr);
1613 else
1614 return store->write_rsv(name, size, (u_char *)addr);
1615 } else if (!strcmp(argv[2], "protect")) {
1616 char *ops;
1617 flag = false;
1618
1619 if (unlikely(argc != 4 && argc != 5))
1620 return CMD_RET_USAGE;
1621
1622 name = (argc == 4) ? NULL : argv[3];
1623 ops = argv[argc - 1];
1624 if (!strcmp(ops, "on"))
1625 flag = true;
1626 else if (!strcmp(ops, "off"))
1627 flag = false;
1628 return store->protect_rsv(name, flag);
1629 }
1630 return CMD_RET_USAGE;
1631}
1632
1633static int do_store_param_ops(cmd_tbl_t *cmdtp,
1634 int flag, int argc, char * const argv[])
1635{
Bichao Zheng5ced7b52023-07-25 15:20:20 +08001636 struct storage_t *store = store_get_current();
Bo Lv72d0e902023-01-02 14:27:34 +00001637
Bichao Zheng050532c2024-01-09 10:24:31 +08001638 if (!store) {
1639 pr_info("%s %d please init your storage device first!\n",
1640 __func__, __LINE__);
1641 return CMD_RET_FAILURE;
1642 }
1643
Bichao Zheng5ced7b52023-07-25 15:20:20 +08001644 if (store->param_ops)
1645 return store->param_ops();
Bo Lv72d0e902023-01-02 14:27:34 +00001646
1647 return 0;
1648}
1649
1650static cmd_tbl_t cmd_store_sub[] = {
1651 U_BOOT_CMD_MKENT(init, 4, 0, do_store_init, "", ""),
1652 U_BOOT_CMD_MKENT(device, 4, 0, do_store_device, "", ""),
1653 U_BOOT_CMD_MKENT(partition, 3, 0, do_store_partition, "", ""),
1654 U_BOOT_CMD_MKENT(scrub, 5, 0, do_store_erase, "", ""),
1655 U_BOOT_CMD_MKENT(erase, 5, 0, do_store_erase, "", ""),
1656 U_BOOT_CMD_MKENT(read, 6, 0, do_store_read, "", ""),
1657 U_BOOT_CMD_MKENT(write, 7, 0, do_store_write, "", ""),
1658 U_BOOT_CMD_MKENT(write_gpt, 3, 0, do_store_gpt_write, "", ""),
1659 U_BOOT_CMD_MKENT(read_gpt, 3, 0, do_store_gpt_read, "", ""),
1660 U_BOOT_CMD_MKENT(erase_gpt, 2, 0, do_store_gpt_erase, "", ""),
1661 U_BOOT_CMD_MKENT(write_bl2img, 5, 0, do_store_write_bl2img, "", ""),
1662 U_BOOT_CMD_MKENT(boot_read, 6, 0, do_store_boot_read, "", ""),
1663 U_BOOT_CMD_MKENT(boot_write, 6, 0, do_store_boot_write, "", ""),
1664 U_BOOT_CMD_MKENT(boot_erase, 4, 0, do_store_boot_erase, "", ""),
1665 U_BOOT_CMD_MKENT(rsv, 6, 0, do_store_rsv_ops, "", ""),
1666 U_BOOT_CMD_MKENT(param, 2, 0, do_store_param_ops, "", ""),
jinbiaod6719f12023-12-22 05:34:45 +00001667 U_BOOT_CMD_MKENT(boot_copy_enable, 3, 0, do_store_boot_copy_enable, "", ""),
Bo Lv72d0e902023-01-02 14:27:34 +00001668};
1669
1670static int do_store(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1671{
1672 cmd_tbl_t *c;
1673
1674 if (argc < 2)
1675 return CMD_RET_USAGE;
1676
1677 c = find_cmd_tbl(argv[1], cmd_store_sub, ARRAY_SIZE(cmd_store_sub));
1678 if (c)
1679 return c->cmd(cmdtp, flag, argc, argv);
1680
1681 return CMD_RET_USAGE;
1682}
1683
1684U_BOOT_CMD(store, CONFIG_SYS_MAXARGS, 1, do_store,
1685 "STORE sub-system:",
1686 "store init [flag]\n"
1687 " init storage device\n"
1688 "store device [name]\n"
1689 " show or set storage device\n"
1690 " 'store device' command will list\n"
1691 " all valid storage device and print.\n"
1692 " 'store device [name]' will set the\n"
1693 " [name] device to the current device\n"
1694 "store partition\n"
1695 " show partitions of current device\n"
1696 "store read addr [partition name] off size\n"
1697 " read 'size' bytes from offset 'off'\n"
1698 " of device/partition 'partition name' to.\n"
1699 " address 'addr' of memory.\n"
1700 " if partition name not value. read start with\n"
1701 " offset in normal logic area,if tpl area exist\n"
1702 " read offset at end of tpl area\n"
1703 "store write addr [partition name] off size\n"
1704 " write 'size' bytes to offset 'off' of\n"
1705 " device/partition [partition name] from\n"
1706 " address 'addr' of memory.\n"
1707 " if partition name not value. write start with\n"
1708 " offset in normal logic area,if tpl area exist\n"
1709 " write offset at end of tpl area\n"
1710 "store write_gpt addr\n"
1711 " write gpt from address 'addr'\n"
1712 "store read_gpt addr\n"
1713 " read gpt to address 'addr'\n"
1714 "store erase_gpt\n"
1715 " erase primary and secondary gpt\n"
1716 "store erase partition name off size.\n"
1717 " erase 'size' bytes from offset 'off'\n"
1718 " of device/partition [partition name]\n"
1719 " partition name must't NULL\n"
1720 "store scrub partition name off size.\n"
1721 " erase 'size' bytes from offset 'off'\n"
1722 " of device/partition [partition name]\n"
1723 " includes oob area if the device has.\n"
1724 " partition name must't NULL\n"
1725 "store erase.chip [flag]\n"
1726 " erase all nand chip,except bad block\n"
1727 " flag 0 erase all nand chip,except bootloader&rsv\n"
1728 " flag 1 erase rsv\n"
1729 "store scrub.chip\n"
1730 " erase all nand chip,include bad block\n"
1731 "store boot_read name addr copy size\n"
1732 " read 'size' bytes from 'copy'th backup\n"
1733 " in name partition, 'copy' can't be null.\n"
1734 " name:\n"
1735 " in discrete mode: 'bl2'/'tpl'(fip)\n"
1736 " in compact mode: 'bootloader'\n"
1737 "store boot_write name addr [copy] size\n"
1738 " write 'size' bytes to 'copy'th backup\n"
1739 " in [name] partition from address\n"
1740 " 'addr' of memory. when the optional 'copy'\n"
1741 " is null, it will writes to all copies\n"
1742 " name:\n"
1743 " in discrete mode:\n"
1744 " 'bl2/bl2e/bl2x/ddrfip/tpl(fip), only update part\n"
1745 " 'bootloader', update whole uboot.bin, in this case\n"
1746 " @copy:if used, must < min(tplCpyNum, Bl2CpyNum), update only the specified copy\n"
1747 " if not used, update all the copies of bl2 bl2e bl2x ddrfip tpl!\n"
1748 " in compact mode: 'bootloader'\n"
1749 "store boot_erase name [copy]\n"
1750 " erase the name info from 'copy'th backup\n"
1751 " when the optional 'copy' not value, it\n"
1752 " will erase all copies.\n"
1753 " name:\n"
1754 " in discrete mode: \n"
1755 " 'bl2'/'tpl'(fip): erase bl2/tpl partition\n"
1756 " 'bootloader':erase bl2 + tpl partition\n"
1757 " in compact mode: 'bootloader'\n"
1758 "store rsv read name addr size\n"
1759 " read 'size' bytes 'name' rsv info\n"
1760 " to address 'addr' of memory\n"
1761 " 'name' could be key/dtb/env etc...\n"
1762 "store rsv write name addr size\n"
1763 " write 'size' bytes 'name' rsv info\n"
1764 " from address 'addr' of memory\n"
1765 "store rsv erase name\n"
1766 " erase 'name' rsv info\n"
1767 " name must't null\n"
1768 "store rsv protect name on/off\n"
1769 " turn on/off the rsv info protection\n"
1770 " name must't null\n"
1771 "store param\n"
jinbiaod6719f12023-12-22 05:34:45 +00001772 " transfer bl2e/x ddrfip devfip size to kernel in such case like sc2\n"
1773 "store boot_copy_enable [boot_index]\n"
1774 " check bootloader_x whether enable\n"
Bo Lv72d0e902023-01-02 14:27:34 +00001775);