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