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