xref: /rk3399_rockchip-uboot/cmd/bootrkp.c (revision 8dd9db5d1cd5826638c3cdb5f681300ff2f29f3b)
1 /*
2  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <malloc.h>
9 #include <android_bootloader.h>
10 #include <attestation_key.h>
11 #include <boot_rkimg.h>
12 #include <keymaster.h>
13 
14 #define OEM_UNLOCK_ARG_SIZE 30
15 
16 static int do_boot_rockchip(cmd_tbl_t *cmdtp, int flag, int argc,
17 		      char * const argv[])
18 {
19 	disk_partition_t part_info;
20 	struct blk_desc *dev_desc;
21 	int mode = 0;
22 	char *boot_partname = PART_BOOT;
23 	int ret = 0;
24 	int i = 0;
25 
26 	dev_desc = rockchip_get_bootdev();
27 	if (!dev_desc) {
28 		printf("%s: dev_desc is NULL!\n", __func__);
29 		return CMD_RET_FAILURE;
30 	}
31 
32 #ifdef CONFIG_ANDROID_KEYMASTER_CA
33 	disk_partition_t misc_part_info;
34 
35 	/* load attestation key from misc partition. */
36 	ret = part_get_info_by_name(dev_desc, "misc",
37 				    &misc_part_info);
38 	if (ret < 0)
39 		printf("%s Could not find misc partition\n", __func__);
40 	else
41 		load_attestation_key(dev_desc, &misc_part_info);
42 #endif
43 
44 #ifdef CONFIG_FASTBOOT_OEM_UNLOCK
45 	/* read oem unlock status and attach to bootargs */
46 	uint8_t unlock = 0;
47 	TEEC_Result result;
48 	char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0};
49 	result = trusty_read_oem_unlock(&unlock);
50 	if (result) {
51 		printf("read oem unlock status with error : 0x%x\n", result);
52 	} else {
53 		snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock);
54 		env_update("bootargs", oem_unlock);
55 	}
56 #endif
57 
58 	mode = rockchip_get_boot_mode();
59 	if (mode == BOOT_MODE_RECOVERY) {
60 		boot_partname = PART_RECOVERY;
61 		printf("%s boot from Recovery partition!\n", __func__);
62 	}
63 
64 	for (i = 0; i < argc; i++) {
65 		if (!strcmp(argv[i], "boot-recovery")) {
66 			boot_partname = PART_RECOVERY;
67 			printf("%s argv%d:%s boot from Recovery partition!\n",
68 				__func__, i, argv[i]);
69 		}
70 	}
71 
72 	ret = part_get_info_by_name(dev_desc, boot_partname, &part_info);
73 
74 	if(boot_rockchip_image(dev_desc, &part_info))
75 		ret = CMD_RET_FAILURE;
76 
77 	return ret;
78 }
79 
80 U_BOOT_CMD(
81 	bootrkp,  CONFIG_SYS_MAXARGS,     1,      do_boot_rockchip,
82 	"Boot Linux Image from rockchip image type",
83 	"kernel.img: zImage/Image\n"
84 	"boot.img: ramdisk\n"
85 	"resource.img: dtb, u-boot logo, kernel logo"
86 );
87 
88 static int do_rkimg_test(cmd_tbl_t *cmdtp, int flag, int argc,
89 		      char * const argv[])
90 {
91 	struct blk_desc *dev_desc;
92 	u32* buffer;
93 	int ret = 0;
94 
95 	dev_desc = blk_get_dev(argv[1], simple_strtoul(argv[2], NULL, 16));
96 
97 	buffer = memalign(ARCH_DMA_MINALIGN, 1024);
98 	/* Read one block from begining of IDB data */
99 	ret = blk_dread(dev_desc, 64, 2, buffer);
100 	if (ret != 2) {
101 		printf("%s fail to read data from IDB\n", __func__);
102 		free(buffer);
103 		return CMD_RET_FAILURE;
104 	}
105 
106 	if (buffer[0] == 0xFCDC8C3B){
107 		printf("%s found IDB in SDcard\n", __func__);
108 		ret = CMD_RET_SUCCESS;
109 		if (0 == buffer[128 + 104 / 4]) /* TAG in IDB */
110 			env_update("bootargs", "sdfwupdate");
111 	}
112 
113 	free(buffer);
114 
115 	return ret;
116 }
117 
118 U_BOOT_CMD(
119 	rkimgtest, 3, 0,    do_rkimg_test,
120 	"Test if storage media have rockchip image",
121 	""
122 );
123