Commit e05d44f550ca90b899e568eed770a68fba90afcb

Authored by Ji Luo
1 parent 4668e8a45e

MA-13832 [Trusty] Support random rpmb key set

Sometimes we need to set random rpmb key which is invisible
except for the device.
Generate the random key with hwcrypto interface and support
fastboot command "fastboot oem set-rpmb-random-key" to set it.

Test: build and boot on imx8q.

Change-Id: I44e1b6b091366d8ffceb1159fc65c17610ce5243
Signed-off-by: Ji Luo <ji.luo@nxp.com>

Showing 4 changed files with 39 additions and 10 deletions Inline Diff

drivers/usb/gadget/f_fastboot.c
1 /* 1 /*
2 * (C) Copyright 2008 - 2009 2 * (C) Copyright 2008 - 2009
3 * Windriver, <www.windriver.com> 3 * Windriver, <www.windriver.com>
4 * Tom Rix <Tom.Rix@windriver.com> 4 * Tom Rix <Tom.Rix@windriver.com>
5 * 5 *
6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> 6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 * 7 *
8 * Copyright 2014 Linaro, Ltd. 8 * Copyright 2014 Linaro, Ltd.
9 * Rob Herring <robh@kernel.org> 9 * Rob Herring <robh@kernel.org>
10 * 10 *
11 * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. 11 * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
12 * Copyright 2017 NXP 12 * Copyright 2017 NXP
13 * 13 *
14 * SPDX-License-Identifier: GPL-2.0+ 14 * SPDX-License-Identifier: GPL-2.0+
15 */ 15 */
16 #include <config.h> 16 #include <config.h>
17 #include <common.h> 17 #include <common.h>
18 #include <errno.h> 18 #include <errno.h>
19 #include <stdlib.h> 19 #include <stdlib.h>
20 #include <fastboot.h> 20 #include <fastboot.h>
21 #include <malloc.h> 21 #include <malloc.h>
22 #include <linux/usb/ch9.h> 22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h> 23 #include <linux/usb/gadget.h>
24 #include <linux/usb/composite.h> 24 #include <linux/usb/composite.h>
25 #include <linux/compiler.h> 25 #include <linux/compiler.h>
26 #include <version.h> 26 #include <version.h>
27 #include <g_dnl.h> 27 #include <g_dnl.h>
28 #include "../lib/avb/fsl/utils.h" 28 #include "../lib/avb/fsl/utils.h"
29 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 29 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
30 #include <fb_mmc.h> 30 #include <fb_mmc.h>
31 #endif 31 #endif
32 32
33 #ifdef CONFIG_IMX_TRUSTY_OS 33 #ifdef CONFIG_IMX_TRUSTY_OS
34 #define ATAP_UUID_SIZE 32 34 #define ATAP_UUID_SIZE 32
35 #define ATAP_UUID_STR_SIZE ((ATAP_UUID_SIZE*2) + 1) 35 #define ATAP_UUID_STR_SIZE ((ATAP_UUID_SIZE*2) + 1)
36 36
37 extern int armv7_init_nonsec(void); 37 extern int armv7_init_nonsec(void);
38 extern void trusty_os_init(void); 38 extern void trusty_os_init(void);
39 #include <trusty/libtipc.h> 39 #include <trusty/libtipc.h>
40 #endif 40 #endif
41 41
42 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV 42 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
43 #include <fb_nand.h> 43 #include <fb_nand.h>
44 #endif 44 #endif
45 45
46 #ifdef CONFIG_FSL_FASTBOOT 46 #ifdef CONFIG_FSL_FASTBOOT
47 #include <asm/mach-imx/sys_proto.h> 47 #include <asm/mach-imx/sys_proto.h>
48 #include <fsl_fastboot.h> 48 #include <fsl_fastboot.h>
49 #include <mmc.h> 49 #include <mmc.h>
50 #include <android_image.h> 50 #include <android_image.h>
51 #include <asm/bootm.h> 51 #include <asm/bootm.h>
52 #include <nand.h> 52 #include <nand.h>
53 #include <part.h> 53 #include <part.h>
54 #include <sparse_format.h> 54 #include <sparse_format.h>
55 #include <image-sparse.h> 55 #include <image-sparse.h>
56 #include <image.h> 56 #include <image.h>
57 #include <asm/mach-imx/boot_mode.h> 57 #include <asm/mach-imx/boot_mode.h>
58 #include <asm/arch/sys_proto.h> 58 #include <asm/arch/sys_proto.h>
59 #include <asm/setup.h> 59 #include <asm/setup.h>
60 #include <environment.h> 60 #include <environment.h>
61 #ifdef CONFIG_ANDROID_RECOVERY 61 #ifdef CONFIG_ANDROID_RECOVERY
62 #include <recovery.h> 62 #include <recovery.h>
63 #endif 63 #endif
64 #endif 64 #endif
65 65
66 #ifdef CONFIG_BCB_SUPPORT 66 #ifdef CONFIG_BCB_SUPPORT
67 #include "bcb.h" 67 #include "bcb.h"
68 #endif 68 #endif
69 69
70 #ifdef CONFIG_AVB_SUPPORT 70 #ifdef CONFIG_AVB_SUPPORT
71 #include <dt_table.h> 71 #include <dt_table.h>
72 #include <fsl_avb.h> 72 #include <fsl_avb.h>
73 #endif 73 #endif
74 74
75 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 75 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
76 #include <asm-generic/gpio.h> 76 #include <asm-generic/gpio.h>
77 #include <asm/mach-imx/gpio.h> 77 #include <asm/mach-imx/gpio.h>
78 #include "../lib/avb/fsl/fsl_avbkey.h" 78 #include "../lib/avb/fsl/fsl_avbkey.h"
79 #include "../arch/arm/include/asm/mach-imx/hab.h" 79 #include "../arch/arm/include/asm/mach-imx/hab.h"
80 #endif 80 #endif
81 81
82 #ifdef CONFIG_FASTBOOT_LOCK 82 #ifdef CONFIG_FASTBOOT_LOCK
83 #include "fastboot_lock_unlock.h" 83 #include "fastboot_lock_unlock.h"
84 #endif 84 #endif
85 85
86 #if defined(CONFIG_IMX_TRUSTY_OS) && defined(CONFIG_DUAL_BOOTLOADER) 86 #if defined(CONFIG_IMX_TRUSTY_OS) && defined(CONFIG_DUAL_BOOTLOADER)
87 #include "u-boot/sha256.h" 87 #include "u-boot/sha256.h"
88 #endif 88 #endif
89 89
90 #define FASTBOOT_VERSION "0.4" 90 #define FASTBOOT_VERSION "0.4"
91 91
92 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M) 92 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M)
93 #define FASTBOOT_COMMON_VAR_NUM 14 93 #define FASTBOOT_COMMON_VAR_NUM 14
94 #else 94 #else
95 #define FASTBOOT_COMMON_VAR_NUM 13 95 #define FASTBOOT_COMMON_VAR_NUM 13
96 #endif 96 #endif
97 97
98 #define FASTBOOT_VAR_YES "yes" 98 #define FASTBOOT_VAR_YES "yes"
99 #define FASTBOOT_VAR_NO "no" 99 #define FASTBOOT_VAR_NO "no"
100 #define FASTBOOT_INTERFACE_CLASS 0xff 100 #define FASTBOOT_INTERFACE_CLASS 0xff
101 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 101 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
102 #define FASTBOOT_INTERFACE_PROTOCOL 0x03 102 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
103 103
104 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) 104 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
105 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) 105 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
106 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) 106 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
107 107
108 #define EP_BUFFER_SIZE 4096 108 #define EP_BUFFER_SIZE 4096
109 109
110 #ifdef CONFIG_FSL_FASTBOOT 110 #ifdef CONFIG_FSL_FASTBOOT
111 111
112 #define ANDROID_GPT_OFFSET 0 112 #define ANDROID_GPT_OFFSET 0
113 #define ANDROID_GPT_SIZE 0x100000 113 #define ANDROID_GPT_SIZE 0x100000
114 #define ANDROID_GPT_END 0x4400 114 #define ANDROID_GPT_END 0x4400
115 115
116 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 116 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
117 struct fastboot_device_info fastboot_firmwareinfo; 117 struct fastboot_device_info fastboot_firmwareinfo;
118 #endif 118 #endif
119 119
120 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M) 120 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M)
121 #define DST_DECOMPRESS_LEN 1024*1024*32 121 #define DST_DECOMPRESS_LEN 1024*1024*32
122 #endif 122 #endif
123 123
124 #endif 124 #endif
125 125
126 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 126 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
127 #define FDT_PART_NAME "oem_bootloader" 127 #define FDT_PART_NAME "oem_bootloader"
128 #else 128 #else
129 #define FDT_PART_NAME "dtbo" 129 #define FDT_PART_NAME "dtbo"
130 #endif 130 #endif
131 131
132 #define MEK_8QM_EMMC 0 132 #define MEK_8QM_EMMC 0
133 133
134 /* 134 /*
135 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size 135 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
136 * (64 or 512 or 1024), else we break on certain controllers like DWC3 136 * (64 or 512 or 1024), else we break on certain controllers like DWC3
137 * that expect bulk OUT requests to be divisible by maxpacket size. 137 * that expect bulk OUT requests to be divisible by maxpacket size.
138 */ 138 */
139 139
140 /* Offset (in u32's) of start and end fields in the zImage header. */ 140 /* Offset (in u32's) of start and end fields in the zImage header. */
141 #define ZIMAGE_START_ADDR 10 141 #define ZIMAGE_START_ADDR 10
142 #define ZIMAGE_END_ADDR 11 142 #define ZIMAGE_END_ADDR 11
143 143
144 /* common variables of fastboot getvar command */ 144 /* common variables of fastboot getvar command */
145 char *fastboot_common_var[FASTBOOT_COMMON_VAR_NUM] = { 145 char *fastboot_common_var[FASTBOOT_COMMON_VAR_NUM] = {
146 "version", 146 "version",
147 "version-bootloader", 147 "version-bootloader",
148 "version-baseband", 148 "version-baseband",
149 "product", 149 "product",
150 "secure", 150 "secure",
151 "max-download-size", 151 "max-download-size",
152 "erase-block-size", 152 "erase-block-size",
153 "logical-block-size", 153 "logical-block-size",
154 "unlocked", 154 "unlocked",
155 "off-mode-charge", 155 "off-mode-charge",
156 "battery-voltage", 156 "battery-voltage",
157 "variant", 157 "variant",
158 "battery-soc-ok", 158 "battery-soc-ok",
159 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M) 159 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M)
160 "baseboard_id" 160 "baseboard_id"
161 #endif 161 #endif
162 }; 162 };
163 163
164 /* at-vboot-state variable list */ 164 /* at-vboot-state variable list */
165 #ifdef CONFIG_AVB_ATX 165 #ifdef CONFIG_AVB_ATX
166 #define AT_VBOOT_STATE_VAR_NUM 6 166 #define AT_VBOOT_STATE_VAR_NUM 6
167 extern struct imx_sec_config_fuse_t const imx_sec_config_fuse; 167 extern struct imx_sec_config_fuse_t const imx_sec_config_fuse;
168 extern int fuse_read(u32 bank, u32 word, u32 *val); 168 extern int fuse_read(u32 bank, u32 word, u32 *val);
169 169
170 char *fastboot_at_vboot_state_var[AT_VBOOT_STATE_VAR_NUM] = { 170 char *fastboot_at_vboot_state_var[AT_VBOOT_STATE_VAR_NUM] = {
171 "bootloader-locked", 171 "bootloader-locked",
172 "bootloader-min-versions", 172 "bootloader-min-versions",
173 "avb-perm-attr-set", 173 "avb-perm-attr-set",
174 "avb-locked", 174 "avb-locked",
175 "avb-unlock-disabled", 175 "avb-unlock-disabled",
176 "avb-min-versions" 176 "avb-min-versions"
177 }; 177 };
178 #endif 178 #endif
179 179
180 /* Boot metric variables */ 180 /* Boot metric variables */
181 boot_metric metrics = { 181 boot_metric metrics = {
182 .bll_1 = 0, 182 .bll_1 = 0,
183 .ble_1 = 0, 183 .ble_1 = 0,
184 .kl = 0, 184 .kl = 0,
185 .kd = 0, 185 .kd = 0,
186 .avb = 0, 186 .avb = 0,
187 .odt = 0, 187 .odt = 0,
188 .sw = 0 188 .sw = 0
189 }; 189 };
190 190
191 typedef struct usb_req usb_req; 191 typedef struct usb_req usb_req;
192 struct usb_req { 192 struct usb_req {
193 struct usb_request *in_req; 193 struct usb_request *in_req;
194 usb_req *next; 194 usb_req *next;
195 }; 195 };
196 196
197 struct f_fastboot { 197 struct f_fastboot {
198 struct usb_function usb_function; 198 struct usb_function usb_function;
199 199
200 /* IN/OUT EP's and corresponding requests */ 200 /* IN/OUT EP's and corresponding requests */
201 struct usb_ep *in_ep, *out_ep; 201 struct usb_ep *in_ep, *out_ep;
202 struct usb_request *in_req, *out_req; 202 struct usb_request *in_req, *out_req;
203 usb_req *front, *rear; 203 usb_req *front, *rear;
204 }; 204 };
205 205
206 static char fb_ext_prop_name[] = "DeviceInterfaceGUID"; 206 static char fb_ext_prop_name[] = "DeviceInterfaceGUID";
207 static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}"; 207 static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}";
208 208
209 static struct usb_os_desc_ext_prop fb_ext_prop = { 209 static struct usb_os_desc_ext_prop fb_ext_prop = {
210 .type = 1, /* NUL-terminated Unicode String (REG_SZ) */ 210 .type = 1, /* NUL-terminated Unicode String (REG_SZ) */
211 .name = fb_ext_prop_name, 211 .name = fb_ext_prop_name,
212 .data = fb_ext_prop_data, 212 .data = fb_ext_prop_data,
213 }; 213 };
214 214
215 /* 16 bytes of "Compatible ID" and "Subcompatible ID" */ 215 /* 16 bytes of "Compatible ID" and "Subcompatible ID" */
216 static char fb_cid[16] = {'W', 'I', 'N', 'U', 'S', 'B'}; 216 static char fb_cid[16] = {'W', 'I', 'N', 'U', 'S', 'B'};
217 static struct usb_os_desc fb_os_desc = { 217 static struct usb_os_desc fb_os_desc = {
218 .ext_compat_id = fb_cid, 218 .ext_compat_id = fb_cid,
219 }; 219 };
220 220
221 static struct usb_os_desc_table fb_os_desc_table = { 221 static struct usb_os_desc_table fb_os_desc_table = {
222 .os_desc = &fb_os_desc, 222 .os_desc = &fb_os_desc,
223 }; 223 };
224 224
225 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) 225 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
226 { 226 {
227 return container_of(f, struct f_fastboot, usb_function); 227 return container_of(f, struct f_fastboot, usb_function);
228 } 228 }
229 229
230 static struct f_fastboot *fastboot_func; 230 static struct f_fastboot *fastboot_func;
231 static unsigned int download_size; 231 static unsigned int download_size;
232 static unsigned int download_bytes; 232 static unsigned int download_bytes;
233 233
234 static struct usb_endpoint_descriptor fs_ep_in = { 234 static struct usb_endpoint_descriptor fs_ep_in = {
235 .bLength = USB_DT_ENDPOINT_SIZE, 235 .bLength = USB_DT_ENDPOINT_SIZE,
236 .bDescriptorType = USB_DT_ENDPOINT, 236 .bDescriptorType = USB_DT_ENDPOINT,
237 .bEndpointAddress = USB_DIR_IN, 237 .bEndpointAddress = USB_DIR_IN,
238 .bmAttributes = USB_ENDPOINT_XFER_BULK, 238 .bmAttributes = USB_ENDPOINT_XFER_BULK,
239 .wMaxPacketSize = cpu_to_le16(64), 239 .wMaxPacketSize = cpu_to_le16(64),
240 }; 240 };
241 241
242 static struct usb_endpoint_descriptor fs_ep_out = { 242 static struct usb_endpoint_descriptor fs_ep_out = {
243 .bLength = USB_DT_ENDPOINT_SIZE, 243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT, 244 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = USB_DIR_OUT, 245 .bEndpointAddress = USB_DIR_OUT,
246 .bmAttributes = USB_ENDPOINT_XFER_BULK, 246 .bmAttributes = USB_ENDPOINT_XFER_BULK,
247 .wMaxPacketSize = cpu_to_le16(64), 247 .wMaxPacketSize = cpu_to_le16(64),
248 }; 248 };
249 249
250 static struct usb_endpoint_descriptor hs_ep_in = { 250 static struct usb_endpoint_descriptor hs_ep_in = {
251 .bLength = USB_DT_ENDPOINT_SIZE, 251 .bLength = USB_DT_ENDPOINT_SIZE,
252 .bDescriptorType = USB_DT_ENDPOINT, 252 .bDescriptorType = USB_DT_ENDPOINT,
253 .bEndpointAddress = USB_DIR_IN, 253 .bEndpointAddress = USB_DIR_IN,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK, 254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 .wMaxPacketSize = cpu_to_le16(512), 255 .wMaxPacketSize = cpu_to_le16(512),
256 }; 256 };
257 257
258 static struct usb_endpoint_descriptor hs_ep_out = { 258 static struct usb_endpoint_descriptor hs_ep_out = {
259 .bLength = USB_DT_ENDPOINT_SIZE, 259 .bLength = USB_DT_ENDPOINT_SIZE,
260 .bDescriptorType = USB_DT_ENDPOINT, 260 .bDescriptorType = USB_DT_ENDPOINT,
261 .bEndpointAddress = USB_DIR_OUT, 261 .bEndpointAddress = USB_DIR_OUT,
262 .bmAttributes = USB_ENDPOINT_XFER_BULK, 262 .bmAttributes = USB_ENDPOINT_XFER_BULK,
263 .wMaxPacketSize = cpu_to_le16(512), 263 .wMaxPacketSize = cpu_to_le16(512),
264 }; 264 };
265 265
266 static struct usb_interface_descriptor interface_desc = { 266 static struct usb_interface_descriptor interface_desc = {
267 .bLength = USB_DT_INTERFACE_SIZE, 267 .bLength = USB_DT_INTERFACE_SIZE,
268 .bDescriptorType = USB_DT_INTERFACE, 268 .bDescriptorType = USB_DT_INTERFACE,
269 .bInterfaceNumber = 0x00, 269 .bInterfaceNumber = 0x00,
270 .bAlternateSetting = 0x00, 270 .bAlternateSetting = 0x00,
271 .bNumEndpoints = 0x02, 271 .bNumEndpoints = 0x02,
272 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, 272 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
273 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, 273 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
274 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, 274 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
275 }; 275 };
276 276
277 static struct usb_descriptor_header *fb_fs_function[] = { 277 static struct usb_descriptor_header *fb_fs_function[] = {
278 (struct usb_descriptor_header *)&interface_desc, 278 (struct usb_descriptor_header *)&interface_desc,
279 (struct usb_descriptor_header *)&fs_ep_in, 279 (struct usb_descriptor_header *)&fs_ep_in,
280 (struct usb_descriptor_header *)&fs_ep_out, 280 (struct usb_descriptor_header *)&fs_ep_out,
281 }; 281 };
282 282
283 static struct usb_descriptor_header *fb_hs_function[] = { 283 static struct usb_descriptor_header *fb_hs_function[] = {
284 (struct usb_descriptor_header *)&interface_desc, 284 (struct usb_descriptor_header *)&interface_desc,
285 (struct usb_descriptor_header *)&hs_ep_in, 285 (struct usb_descriptor_header *)&hs_ep_in,
286 (struct usb_descriptor_header *)&hs_ep_out, 286 (struct usb_descriptor_header *)&hs_ep_out,
287 NULL, 287 NULL,
288 }; 288 };
289 289
290 /* Super speed */ 290 /* Super speed */
291 static struct usb_endpoint_descriptor ss_ep_in = { 291 static struct usb_endpoint_descriptor ss_ep_in = {
292 .bLength = USB_DT_ENDPOINT_SIZE, 292 .bLength = USB_DT_ENDPOINT_SIZE,
293 .bDescriptorType = USB_DT_ENDPOINT, 293 .bDescriptorType = USB_DT_ENDPOINT,
294 .bEndpointAddress = USB_DIR_IN, 294 .bEndpointAddress = USB_DIR_IN,
295 .bmAttributes = USB_ENDPOINT_XFER_BULK, 295 .bmAttributes = USB_ENDPOINT_XFER_BULK,
296 .wMaxPacketSize = cpu_to_le16(1024), 296 .wMaxPacketSize = cpu_to_le16(1024),
297 }; 297 };
298 298
299 static struct usb_endpoint_descriptor ss_ep_out = { 299 static struct usb_endpoint_descriptor ss_ep_out = {
300 .bLength = USB_DT_ENDPOINT_SIZE, 300 .bLength = USB_DT_ENDPOINT_SIZE,
301 .bDescriptorType = USB_DT_ENDPOINT, 301 .bDescriptorType = USB_DT_ENDPOINT,
302 .bEndpointAddress = USB_DIR_OUT, 302 .bEndpointAddress = USB_DIR_OUT,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK, 303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(1024), 304 .wMaxPacketSize = cpu_to_le16(1024),
305 }; 305 };
306 306
307 static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = { 307 static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = {
308 .bLength = sizeof(fb_ss_bulk_comp_desc), 308 .bLength = sizeof(fb_ss_bulk_comp_desc),
309 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 309 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
310 }; 310 };
311 311
312 static struct usb_descriptor_header *fb_ss_function[] = { 312 static struct usb_descriptor_header *fb_ss_function[] = {
313 (struct usb_descriptor_header *)&interface_desc, 313 (struct usb_descriptor_header *)&interface_desc,
314 (struct usb_descriptor_header *)&ss_ep_in, 314 (struct usb_descriptor_header *)&ss_ep_in,
315 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc, 315 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
316 (struct usb_descriptor_header *)&ss_ep_out, 316 (struct usb_descriptor_header *)&ss_ep_out,
317 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc, 317 (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
318 NULL, 318 NULL,
319 }; 319 };
320 320
321 static struct usb_endpoint_descriptor * 321 static struct usb_endpoint_descriptor *
322 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, 322 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
323 struct usb_endpoint_descriptor *hs, 323 struct usb_endpoint_descriptor *hs,
324 struct usb_endpoint_descriptor *ss) 324 struct usb_endpoint_descriptor *ss)
325 { 325 {
326 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER) 326 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER)
327 return ss; 327 return ss;
328 328
329 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 329 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
330 return hs; 330 return hs;
331 331
332 return fs; 332 return fs;
333 } 333 }
334 334
335 /* 335 /*
336 * static strings, in UTF-8 336 * static strings, in UTF-8
337 */ 337 */
338 static const char fastboot_name[] = "Android Fastboot"; 338 static const char fastboot_name[] = "Android Fastboot";
339 339
340 static struct usb_string fastboot_string_defs[] = { 340 static struct usb_string fastboot_string_defs[] = {
341 [0].s = fastboot_name, 341 [0].s = fastboot_name,
342 { } /* end of list */ 342 { } /* end of list */
343 }; 343 };
344 344
345 static struct usb_gadget_strings stringtab_fastboot = { 345 static struct usb_gadget_strings stringtab_fastboot = {
346 .language = 0x0409, /* en-us */ 346 .language = 0x0409, /* en-us */
347 .strings = fastboot_string_defs, 347 .strings = fastboot_string_defs,
348 }; 348 };
349 349
350 static struct usb_gadget_strings *fastboot_strings[] = { 350 static struct usb_gadget_strings *fastboot_strings[] = {
351 &stringtab_fastboot, 351 &stringtab_fastboot,
352 NULL, 352 NULL,
353 }; 353 };
354 354
355 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); 355 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
356 static int strcmp_l1(const char *s1, const char *s2); 356 static int strcmp_l1(const char *s1, const char *s2);
357 357
358 358
359 static char *fb_response_str; 359 static char *fb_response_str;
360 360
361 #ifdef CONFIG_FSL_FASTBOOT 361 #ifdef CONFIG_FSL_FASTBOOT
362 362
363 #ifndef TRUSTY_OS_MMC_BLKS 363 #ifndef TRUSTY_OS_MMC_BLKS
364 #define TRUSTY_OS_MMC_BLKS 0x7FF 364 #define TRUSTY_OS_MMC_BLKS 0x7FF
365 #endif 365 #endif
366 #ifndef TEE_HWPARTITION_ID 366 #ifndef TEE_HWPARTITION_ID
367 #define TEE_HWPARTITION_ID 2 367 #define TEE_HWPARTITION_ID 2
368 #endif 368 #endif
369 369
370 #define FASTBOOT_PARTITION_ALL "all" 370 #define FASTBOOT_PARTITION_ALL "all"
371 371
372 #define ANDROID_MBR_OFFSET 0 372 #define ANDROID_MBR_OFFSET 0
373 #define ANDROID_MBR_SIZE 0x200 373 #define ANDROID_MBR_SIZE 0x200
374 #define ANDROID_BOOTLOADER_SIZE 0x400000 374 #define ANDROID_BOOTLOADER_SIZE 0x400000
375 375
376 #define MMC_SATA_BLOCK_SIZE 512 376 #define MMC_SATA_BLOCK_SIZE 512
377 #define FASTBOOT_FBPARTS_ENV_MAX_LEN 1024 377 #define FASTBOOT_FBPARTS_ENV_MAX_LEN 1024
378 /* To support the Android-style naming of flash */ 378 /* To support the Android-style naming of flash */
379 #define MAX_PTN 32 379 #define MAX_PTN 32
380 struct fastboot_ptentry g_ptable[MAX_PTN]; 380 struct fastboot_ptentry g_ptable[MAX_PTN];
381 unsigned int g_pcount; 381 unsigned int g_pcount;
382 struct fastboot_device_info fastboot_devinfo = {0xff, 0xff}; 382 struct fastboot_device_info fastboot_devinfo = {0xff, 0xff};
383 383
384 384
385 enum { 385 enum {
386 PTN_GPT_INDEX = 0, 386 PTN_GPT_INDEX = 0,
387 PTN_TEE_INDEX, 387 PTN_TEE_INDEX,
388 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 388 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
389 PTN_M4_OS_INDEX, 389 PTN_M4_OS_INDEX,
390 #endif 390 #endif
391 PTN_ALL_INDEX, 391 PTN_ALL_INDEX,
392 PTN_BOOTLOADER_INDEX, 392 PTN_BOOTLOADER_INDEX,
393 }; 393 };
394 static unsigned int download_bytes_unpadded; 394 static unsigned int download_bytes_unpadded;
395 395
396 static struct cmd_fastboot_interface interface = { 396 static struct cmd_fastboot_interface interface = {
397 .rx_handler = NULL, 397 .rx_handler = NULL,
398 .reset_handler = NULL, 398 .reset_handler = NULL,
399 .product_name = NULL, 399 .product_name = NULL,
400 .serial_no = NULL, 400 .serial_no = NULL,
401 .nand_block_size = 0, 401 .nand_block_size = 0,
402 .transfer_buffer = (unsigned char *)0xffffffff, 402 .transfer_buffer = (unsigned char *)0xffffffff,
403 .transfer_buffer_size = 0, 403 .transfer_buffer_size = 0,
404 }; 404 };
405 405
406 int read_from_partition_multi(const char* partition, 406 int read_from_partition_multi(const char* partition,
407 int64_t offset, size_t num_bytes, void* buffer, size_t* out_num_read) 407 int64_t offset, size_t num_bytes, void* buffer, size_t* out_num_read)
408 { 408 {
409 struct fastboot_ptentry *pte; 409 struct fastboot_ptentry *pte;
410 unsigned char *bdata; 410 unsigned char *bdata;
411 unsigned char *out_buf = (unsigned char *)buffer; 411 unsigned char *out_buf = (unsigned char *)buffer;
412 unsigned char *dst, *dst64 = NULL; 412 unsigned char *dst, *dst64 = NULL;
413 unsigned long blksz; 413 unsigned long blksz;
414 unsigned long s, cnt; 414 unsigned long s, cnt;
415 size_t num_read = 0; 415 size_t num_read = 0;
416 lbaint_t part_start, part_end, bs, be, bm, blk_num; 416 lbaint_t part_start, part_end, bs, be, bm, blk_num;
417 margin_pos_t margin; 417 margin_pos_t margin;
418 struct blk_desc *fs_dev_desc = NULL; 418 struct blk_desc *fs_dev_desc = NULL;
419 int dev_no; 419 int dev_no;
420 int ret; 420 int ret;
421 421
422 assert(buffer != NULL && out_num_read != NULL); 422 assert(buffer != NULL && out_num_read != NULL);
423 423
424 dev_no = mmc_get_env_dev(); 424 dev_no = mmc_get_env_dev();
425 if ((fs_dev_desc = blk_get_dev("mmc", dev_no)) == NULL) { 425 if ((fs_dev_desc = blk_get_dev("mmc", dev_no)) == NULL) {
426 printf("mmc device not found\n"); 426 printf("mmc device not found\n");
427 return -1; 427 return -1;
428 } 428 }
429 429
430 pte = fastboot_flash_find_ptn(partition); 430 pte = fastboot_flash_find_ptn(partition);
431 if (!pte) { 431 if (!pte) {
432 printf("no %s partition\n", partition); 432 printf("no %s partition\n", partition);
433 fastboot_flash_dump_ptn(); 433 fastboot_flash_dump_ptn();
434 return -1; 434 return -1;
435 } 435 }
436 436
437 blksz = fs_dev_desc->blksz; 437 blksz = fs_dev_desc->blksz;
438 part_start = pte->start; 438 part_start = pte->start;
439 part_end = pte->start + pte->length - 1; 439 part_end = pte->start + pte->length - 1;
440 440
441 if (get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz, 441 if (get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz,
442 &margin, offset, num_bytes, true)) 442 &margin, offset, num_bytes, true))
443 return -1; 443 return -1;
444 444
445 bs = (lbaint_t)margin.blk_start; 445 bs = (lbaint_t)margin.blk_start;
446 be = (lbaint_t)margin.blk_end; 446 be = (lbaint_t)margin.blk_end;
447 s = margin.start; 447 s = margin.start;
448 bm = margin.multi; 448 bm = margin.multi;
449 449
450 /* alloc a blksz mem */ 450 /* alloc a blksz mem */
451 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz); 451 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz);
452 if (bdata == NULL) { 452 if (bdata == NULL) {
453 printf("Failed to allocate memory!\n"); 453 printf("Failed to allocate memory!\n");
454 return -1; 454 return -1;
455 } 455 }
456 456
457 /* support multi blk read */ 457 /* support multi blk read */
458 while (bs <= be) { 458 while (bs <= be) {
459 if (!s && bm > 1) { 459 if (!s && bm > 1) {
460 dst = out_buf; 460 dst = out_buf;
461 dst64 = PTR_ALIGN(out_buf, 64); /* for mmc blk read alignment */ 461 dst64 = PTR_ALIGN(out_buf, 64); /* for mmc blk read alignment */
462 if (dst64 != dst) { 462 if (dst64 != dst) {
463 dst = dst64; 463 dst = dst64;
464 bm--; 464 bm--;
465 } 465 }
466 blk_num = bm; 466 blk_num = bm;
467 cnt = bm * blksz; 467 cnt = bm * blksz;
468 bm = 0; /* no more multi blk */ 468 bm = 0; /* no more multi blk */
469 } else { 469 } else {
470 blk_num = 1; 470 blk_num = 1;
471 cnt = blksz - s; 471 cnt = blksz - s;
472 if (num_read + cnt > num_bytes) 472 if (num_read + cnt > num_bytes)
473 cnt = num_bytes - num_read; 473 cnt = num_bytes - num_read;
474 dst = bdata; 474 dst = bdata;
475 } 475 }
476 if (blk_dread(fs_dev_desc, bs, blk_num, dst) != blk_num) { 476 if (blk_dread(fs_dev_desc, bs, blk_num, dst) != blk_num) {
477 ret = -1; 477 ret = -1;
478 goto fail; 478 goto fail;
479 } 479 }
480 480
481 if (dst == bdata) 481 if (dst == bdata)
482 memcpy(out_buf, bdata + s, cnt); 482 memcpy(out_buf, bdata + s, cnt);
483 else if (dst == dst64) 483 else if (dst == dst64)
484 memcpy(out_buf, dst, cnt); /* internal copy */ 484 memcpy(out_buf, dst, cnt); /* internal copy */
485 485
486 s = 0; 486 s = 0;
487 bs += blk_num; 487 bs += blk_num;
488 num_read += cnt; 488 num_read += cnt;
489 out_buf += cnt; 489 out_buf += cnt;
490 } 490 }
491 *out_num_read = num_read; 491 *out_num_read = num_read;
492 ret = 0; 492 ret = 0;
493 493
494 fail: 494 fail:
495 free(bdata); 495 free(bdata);
496 return ret; 496 return ret;
497 } 497 }
498 498
499 static void save_env(struct fastboot_ptentry *ptn, 499 static void save_env(struct fastboot_ptentry *ptn,
500 char *var, char *val) 500 char *var, char *val)
501 { 501 {
502 env_set(var, val); 502 env_set(var, val);
503 env_save(); 503 env_save();
504 } 504 }
505 505
506 /* When save = 0, just parse. The input is unchanged 506 /* When save = 0, just parse. The input is unchanged
507 When save = 1, parse and do the save. The input is changed */ 507 When save = 1, parse and do the save. The input is changed */
508 static int parse_env(void *ptn, char *err_string, int save, int debug) 508 static int parse_env(void *ptn, char *err_string, int save, int debug)
509 { 509 {
510 int ret = 1; 510 int ret = 1;
511 unsigned int sets = 0; 511 unsigned int sets = 0;
512 unsigned int comment_start = 0; 512 unsigned int comment_start = 0;
513 char *var = NULL; 513 char *var = NULL;
514 char *var_end = NULL; 514 char *var_end = NULL;
515 char *val = NULL; 515 char *val = NULL;
516 char *val_end = NULL; 516 char *val_end = NULL;
517 unsigned int i; 517 unsigned int i;
518 518
519 char *buff = (char *)interface.transfer_buffer; 519 char *buff = (char *)interface.transfer_buffer;
520 unsigned int size = download_bytes_unpadded; 520 unsigned int size = download_bytes_unpadded;
521 521
522 /* The input does not have to be null terminated. 522 /* The input does not have to be null terminated.
523 This will cause a problem in the corner case 523 This will cause a problem in the corner case
524 where the last line does not have a new line. 524 where the last line does not have a new line.
525 Put a null after the end of the input. 525 Put a null after the end of the input.
526 526
527 WARNING : Input buffer is assumed to be bigger 527 WARNING : Input buffer is assumed to be bigger
528 than the size of the input */ 528 than the size of the input */
529 if (save) 529 if (save)
530 buff[size] = 0; 530 buff[size] = 0;
531 531
532 for (i = 0; i < size; i++) { 532 for (i = 0; i < size; i++) {
533 533
534 if (NULL == var) { 534 if (NULL == var) {
535 535
536 /* 536 /*
537 * Check for comments, comment ok only on 537 * Check for comments, comment ok only on
538 * mostly empty lines 538 * mostly empty lines
539 */ 539 */
540 if (buff[i] == '#') 540 if (buff[i] == '#')
541 comment_start = 1; 541 comment_start = 1;
542 542
543 if (comment_start) { 543 if (comment_start) {
544 if ((buff[i] == '\r') || 544 if ((buff[i] == '\r') ||
545 (buff[i] == '\n')) { 545 (buff[i] == '\n')) {
546 comment_start = 0; 546 comment_start = 0;
547 } 547 }
548 } else { 548 } else {
549 if (!((buff[i] == ' ') || 549 if (!((buff[i] == ' ') ||
550 (buff[i] == '\t') || 550 (buff[i] == '\t') ||
551 (buff[i] == '\r') || 551 (buff[i] == '\r') ||
552 (buff[i] == '\n'))) { 552 (buff[i] == '\n'))) {
553 /* 553 /*
554 * Normal whitespace before the 554 * Normal whitespace before the
555 * variable 555 * variable
556 */ 556 */
557 var = &buff[i]; 557 var = &buff[i];
558 } 558 }
559 } 559 }
560 560
561 } else if (((NULL == var_end) || (NULL == val)) && 561 } else if (((NULL == var_end) || (NULL == val)) &&
562 ((buff[i] == '\r') || (buff[i] == '\n'))) { 562 ((buff[i] == '\r') || (buff[i] == '\n'))) {
563 563
564 /* This is the case when a variable 564 /* This is the case when a variable
565 is unset. */ 565 is unset. */
566 566
567 if (save) { 567 if (save) {
568 /* Set the var end to null so the 568 /* Set the var end to null so the
569 normal string routines will work 569 normal string routines will work
570 570
571 WARNING : This changes the input */ 571 WARNING : This changes the input */
572 buff[i] = '\0'; 572 buff[i] = '\0';
573 573
574 save_env(ptn, var, val); 574 save_env(ptn, var, val);
575 575
576 if (debug) 576 if (debug)
577 printf("Unsetting %s\n", var); 577 printf("Unsetting %s\n", var);
578 } 578 }
579 579
580 /* Clear the variable so state is parse is back 580 /* Clear the variable so state is parse is back
581 to initial. */ 581 to initial. */
582 var = NULL; 582 var = NULL;
583 var_end = NULL; 583 var_end = NULL;
584 sets++; 584 sets++;
585 } else if (NULL == var_end) { 585 } else if (NULL == var_end) {
586 if ((buff[i] == ' ') || 586 if ((buff[i] == ' ') ||
587 (buff[i] == '\t')) 587 (buff[i] == '\t'))
588 var_end = &buff[i]; 588 var_end = &buff[i];
589 } else if (NULL == val) { 589 } else if (NULL == val) {
590 if (!((buff[i] == ' ') || 590 if (!((buff[i] == ' ') ||
591 (buff[i] == '\t'))) 591 (buff[i] == '\t')))
592 val = &buff[i]; 592 val = &buff[i];
593 } else if (NULL == val_end) { 593 } else if (NULL == val_end) {
594 if ((buff[i] == '\r') || 594 if ((buff[i] == '\r') ||
595 (buff[i] == '\n')) { 595 (buff[i] == '\n')) {
596 /* look for escaped cr or ln */ 596 /* look for escaped cr or ln */
597 if ('\\' == buff[i - 1]) { 597 if ('\\' == buff[i - 1]) {
598 /* check for dos */ 598 /* check for dos */
599 if ((buff[i] == '\r') && 599 if ((buff[i] == '\r') &&
600 (buff[i+1] == '\n')) 600 (buff[i+1] == '\n'))
601 buff[i + 1] = ' '; 601 buff[i + 1] = ' ';
602 buff[i - 1] = buff[i] = ' '; 602 buff[i - 1] = buff[i] = ' ';
603 } else { 603 } else {
604 val_end = &buff[i]; 604 val_end = &buff[i];
605 } 605 }
606 } 606 }
607 } else { 607 } else {
608 sprintf(err_string, "Internal Error"); 608 sprintf(err_string, "Internal Error");
609 609
610 if (debug) 610 if (debug)
611 printf("Internal error at %s %d\n", 611 printf("Internal error at %s %d\n",
612 __FILE__, __LINE__); 612 __FILE__, __LINE__);
613 return 1; 613 return 1;
614 } 614 }
615 /* Check if a var / val pair is ready */ 615 /* Check if a var / val pair is ready */
616 if (NULL != val_end) { 616 if (NULL != val_end) {
617 if (save) { 617 if (save) {
618 /* Set the end's with nulls so 618 /* Set the end's with nulls so
619 normal string routines will 619 normal string routines will
620 work. 620 work.
621 621
622 WARNING : This changes the input */ 622 WARNING : This changes the input */
623 *var_end = '\0'; 623 *var_end = '\0';
624 *val_end = '\0'; 624 *val_end = '\0';
625 625
626 save_env(ptn, var, val); 626 save_env(ptn, var, val);
627 627
628 if (debug) 628 if (debug)
629 printf("Setting %s %s\n", var, val); 629 printf("Setting %s %s\n", var, val);
630 } 630 }
631 631
632 /* Clear the variable so state is parse is back 632 /* Clear the variable so state is parse is back
633 to initial. */ 633 to initial. */
634 var = NULL; 634 var = NULL;
635 var_end = NULL; 635 var_end = NULL;
636 val = NULL; 636 val = NULL;
637 val_end = NULL; 637 val_end = NULL;
638 638
639 sets++; 639 sets++;
640 } 640 }
641 } 641 }
642 642
643 /* Corner case 643 /* Corner case
644 Check for the case that no newline at end of the input */ 644 Check for the case that no newline at end of the input */
645 if ((NULL != var) && 645 if ((NULL != var) &&
646 (NULL == val_end)) { 646 (NULL == val_end)) {
647 if (save) { 647 if (save) {
648 /* case of val / val pair */ 648 /* case of val / val pair */
649 if (var_end) 649 if (var_end)
650 *var_end = '\0'; 650 *var_end = '\0';
651 /* else case handled by setting 0 past 651 /* else case handled by setting 0 past
652 the end of buffer. 652 the end of buffer.
653 Similar for val_end being null */ 653 Similar for val_end being null */
654 save_env(ptn, var, val); 654 save_env(ptn, var, val);
655 655
656 if (debug) { 656 if (debug) {
657 if (var_end) 657 if (var_end)
658 printf("Trailing Setting %s %s\n", var, val); 658 printf("Trailing Setting %s %s\n", var, val);
659 else 659 else
660 printf("Trailing Unsetting %s\n", var); 660 printf("Trailing Unsetting %s\n", var);
661 } 661 }
662 } 662 }
663 sets++; 663 sets++;
664 } 664 }
665 /* Did we set anything ? */ 665 /* Did we set anything ? */
666 if (0 == sets) 666 if (0 == sets)
667 sprintf(err_string, "No variables set"); 667 sprintf(err_string, "No variables set");
668 else 668 else
669 ret = 0; 669 ret = 0;
670 670
671 return ret; 671 return ret;
672 } 672 }
673 673
674 static int saveenv_to_ptn(struct fastboot_ptentry *ptn, char *err_string) 674 static int saveenv_to_ptn(struct fastboot_ptentry *ptn, char *err_string)
675 { 675 {
676 int ret = 1; 676 int ret = 1;
677 int save = 0; 677 int save = 0;
678 int debug = 0; 678 int debug = 0;
679 679
680 /* err_string is only 32 bytes 680 /* err_string is only 32 bytes
681 Initialize with a generic error message. */ 681 Initialize with a generic error message. */
682 sprintf(err_string, "%s", "Unknown Error"); 682 sprintf(err_string, "%s", "Unknown Error");
683 683
684 /* Parse the input twice. 684 /* Parse the input twice.
685 Only save to the enviroment if the entire input if correct */ 685 Only save to the enviroment if the entire input if correct */
686 save = 0; 686 save = 0;
687 if (0 == parse_env(ptn, err_string, save, debug)) { 687 if (0 == parse_env(ptn, err_string, save, debug)) {
688 save = 1; 688 save = 1;
689 ret = parse_env(ptn, err_string, save, debug); 689 ret = parse_env(ptn, err_string, save, debug);
690 } 690 }
691 return ret; 691 return ret;
692 } 692 }
693 693
694 static int get_block_size(void); 694 static int get_block_size(void);
695 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 695 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
696 static void process_flash_sf(const char *cmdbuf) 696 static void process_flash_sf(const char *cmdbuf)
697 { 697 {
698 int blksz = 0; 698 int blksz = 0;
699 blksz = get_block_size(); 699 blksz = get_block_size();
700 700
701 if (download_bytes) { 701 if (download_bytes) {
702 struct fastboot_ptentry *ptn; 702 struct fastboot_ptentry *ptn;
703 ptn = fastboot_flash_find_ptn(cmdbuf); 703 ptn = fastboot_flash_find_ptn(cmdbuf);
704 if (ptn == 0) { 704 if (ptn == 0) {
705 fastboot_fail("partition does not exist"); 705 fastboot_fail("partition does not exist");
706 fastboot_flash_dump_ptn(); 706 fastboot_flash_dump_ptn();
707 } else if ((download_bytes > ptn->length * blksz)) { 707 } else if ((download_bytes > ptn->length * blksz)) {
708 fastboot_fail("image too large for partition"); 708 fastboot_fail("image too large for partition");
709 /* TODO : Improve check for yaffs write */ 709 /* TODO : Improve check for yaffs write */
710 } else { 710 } else {
711 int ret; 711 int ret;
712 char sf_command[128]; 712 char sf_command[128];
713 /* Normal case */ 713 /* Normal case */
714 /* Probe device */ 714 /* Probe device */
715 sprintf(sf_command, "sf probe"); 715 sprintf(sf_command, "sf probe");
716 ret = run_command(sf_command, 0); 716 ret = run_command(sf_command, 0);
717 if (ret){ 717 if (ret){
718 fastboot_fail("Probe sf failed"); 718 fastboot_fail("Probe sf failed");
719 return; 719 return;
720 } 720 }
721 /* Erase */ 721 /* Erase */
722 sprintf(sf_command, "sf erase 0x%x 0x%x", ptn->start * blksz, /*start*/ 722 sprintf(sf_command, "sf erase 0x%x 0x%x", ptn->start * blksz, /*start*/
723 ptn->length * blksz /*size*/); 723 ptn->length * blksz /*size*/);
724 ret = run_command(sf_command, 0); 724 ret = run_command(sf_command, 0);
725 if (ret) { 725 if (ret) {
726 fastboot_fail("Erasing sf failed"); 726 fastboot_fail("Erasing sf failed");
727 return; 727 return;
728 } 728 }
729 /* Write image */ 729 /* Write image */
730 sprintf(sf_command, "sf write 0x%x 0x%x 0x%x", 730 sprintf(sf_command, "sf write 0x%x 0x%x 0x%x",
731 (unsigned int)(ulong)interface.transfer_buffer, /* source */ 731 (unsigned int)(ulong)interface.transfer_buffer, /* source */
732 ptn->start * blksz, /* start */ 732 ptn->start * blksz, /* start */
733 download_bytes /*size*/); 733 download_bytes /*size*/);
734 printf("sf write '%s'\n", ptn->name); 734 printf("sf write '%s'\n", ptn->name);
735 ret = run_command(sf_command, 0); 735 ret = run_command(sf_command, 0);
736 if (ret){ 736 if (ret){
737 fastboot_fail("Writing sf failed"); 737 fastboot_fail("Writing sf failed");
738 return; 738 return;
739 } 739 }
740 printf("sf write finished '%s'\n", ptn->name); 740 printf("sf write finished '%s'\n", ptn->name);
741 fastboot_okay(""); 741 fastboot_okay("");
742 } 742 }
743 } else { 743 } else {
744 fastboot_fail("no image downloaded"); 744 fastboot_fail("no image downloaded");
745 } 745 }
746 } 746 }
747 747
748 #ifdef CONFIG_ARCH_IMX8M 748 #ifdef CONFIG_ARCH_IMX8M
749 /* Check if the mcu image is built for running from TCM */ 749 /* Check if the mcu image is built for running from TCM */
750 static bool is_tcm_image(unsigned char *image_addr) 750 static bool is_tcm_image(unsigned char *image_addr)
751 { 751 {
752 u32 stack; 752 u32 stack;
753 753
754 stack = *(u32 *)image_addr; 754 stack = *(u32 *)image_addr;
755 755
756 if ((stack != (u32)ANDROID_MCU_FIRMWARE_HEADER_STACK)) { 756 if ((stack != (u32)ANDROID_MCU_FIRMWARE_HEADER_STACK)) {
757 printf("Please flash mcu firmware images for running from TCM\n"); 757 printf("Please flash mcu firmware images for running from TCM\n");
758 return false; 758 return false;
759 } else 759 } else
760 return true; 760 return true;
761 } 761 }
762 762
763 static int do_bootmcu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 763 static int do_bootmcu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
764 { 764 {
765 int ret; 765 int ret;
766 size_t out_num_read; 766 size_t out_num_read;
767 void *m4_base_addr = (void *)M4_BOOTROM_BASE_ADDR; 767 void *m4_base_addr = (void *)M4_BOOTROM_BASE_ADDR;
768 char command[32]; 768 char command[32];
769 769
770 ret = read_from_partition_multi(FASTBOOT_MCU_FIRMWARE_PARTITION, 770 ret = read_from_partition_multi(FASTBOOT_MCU_FIRMWARE_PARTITION,
771 0, ANDROID_MCU_FIRMWARE_SIZE, (void *)m4_base_addr, &out_num_read); 771 0, ANDROID_MCU_FIRMWARE_SIZE, (void *)m4_base_addr, &out_num_read);
772 if ((ret != 0) || (out_num_read != ANDROID_MCU_FIRMWARE_SIZE)) { 772 if ((ret != 0) || (out_num_read != ANDROID_MCU_FIRMWARE_SIZE)) {
773 printf("Read M4 images failed!\n"); 773 printf("Read M4 images failed!\n");
774 return 1; 774 return 1;
775 } else { 775 } else {
776 printf("run command: 'bootaux 0x%x'\n",(unsigned int)(ulong)m4_base_addr); 776 printf("run command: 'bootaux 0x%x'\n",(unsigned int)(ulong)m4_base_addr);
777 777
778 sprintf(command, "bootaux 0x%x", (unsigned int)(ulong)m4_base_addr); 778 sprintf(command, "bootaux 0x%x", (unsigned int)(ulong)m4_base_addr);
779 ret = run_command(command, 0); 779 ret = run_command(command, 0);
780 if (ret) { 780 if (ret) {
781 printf("run 'bootaux' command failed!\n"); 781 printf("run 'bootaux' command failed!\n");
782 return 1; 782 return 1;
783 } 783 }
784 } 784 }
785 return 0; 785 return 0;
786 } 786 }
787 787
788 U_BOOT_CMD( 788 U_BOOT_CMD(
789 bootmcu, 1, 0, do_bootmcu, 789 bootmcu, 1, 0, do_bootmcu,
790 "boot mcu images\n", 790 "boot mcu images\n",
791 "boot mcu images from 'm4_os' partition, only support images run from TCM" 791 "boot mcu images from 'm4_os' partition, only support images run from TCM"
792 ); 792 );
793 #endif 793 #endif
794 #endif /* CONFIG_FLASH_MCUFIRMWARE_SUPPORT */ 794 #endif /* CONFIG_FLASH_MCUFIRMWARE_SUPPORT */
795 795
796 static ulong bootloader_mmc_offset(void) 796 static ulong bootloader_mmc_offset(void)
797 { 797 {
798 if (is_imx8m() || (is_imx8() && is_soc_rev(CHIP_REV_A))) 798 if (is_imx8m() || (is_imx8() && is_soc_rev(CHIP_REV_A)))
799 return 0x8400; 799 return 0x8400;
800 else if (is_imx8qm()) { 800 else if (is_imx8qm()) {
801 if (MEK_8QM_EMMC == fastboot_devinfo.dev_id) 801 if (MEK_8QM_EMMC == fastboot_devinfo.dev_id)
802 /* target device is eMMC boot0 partition, bootloader offset is 0x0 */ 802 /* target device is eMMC boot0 partition, bootloader offset is 0x0 */
803 return 0x0; 803 return 0x0;
804 else 804 else
805 /* target device is SD card, bootloader offset is 0x8000 */ 805 /* target device is SD card, bootloader offset is 0x8000 */
806 return 0x8000; 806 return 0x8000;
807 } 807 }
808 else if (is_imx8()) 808 else if (is_imx8())
809 return 0x8000; 809 return 0x8000;
810 else 810 else
811 return 0x400; 811 return 0x400;
812 } 812 }
813 813
814 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) || defined(CONFIG_FASTBOOT_STORAGE_SATA) 814 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) || defined(CONFIG_FASTBOOT_STORAGE_SATA)
815 static int is_raw_partition(struct fastboot_ptentry *ptn) 815 static int is_raw_partition(struct fastboot_ptentry *ptn)
816 { 816 {
817 #ifdef CONFIG_ANDROID_AB_SUPPORT 817 #ifdef CONFIG_ANDROID_AB_SUPPORT
818 if (ptn && (!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER, 818 if (ptn && (!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER,
819 strlen(FASTBOOT_PARTITION_BOOTLOADER)) || 819 strlen(FASTBOOT_PARTITION_BOOTLOADER)) ||
820 !strncmp(ptn->name, FASTBOOT_PARTITION_GPT, 820 !strncmp(ptn->name, FASTBOOT_PARTITION_GPT,
821 strlen(FASTBOOT_PARTITION_GPT)) || 821 strlen(FASTBOOT_PARTITION_GPT)) ||
822 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT_A, 822 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT_A,
823 strlen(FASTBOOT_PARTITION_BOOT_A)) || 823 strlen(FASTBOOT_PARTITION_BOOT_A)) ||
824 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT_B, 824 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT_B,
825 strlen(FASTBOOT_PARTITION_BOOT_B)) || 825 strlen(FASTBOOT_PARTITION_BOOT_B)) ||
826 #ifdef CONFIG_FASTBOOT_LOCK 826 #ifdef CONFIG_FASTBOOT_LOCK
827 !strncmp(ptn->name, FASTBOOT_PARTITION_FBMISC, 827 !strncmp(ptn->name, FASTBOOT_PARTITION_FBMISC,
828 strlen(FASTBOOT_PARTITION_FBMISC)) || 828 strlen(FASTBOOT_PARTITION_FBMISC)) ||
829 #endif 829 #endif
830 !strncmp(ptn->name, FASTBOOT_PARTITION_MISC, 830 !strncmp(ptn->name, FASTBOOT_PARTITION_MISC,
831 strlen(FASTBOOT_PARTITION_MISC)))) { 831 strlen(FASTBOOT_PARTITION_MISC)))) {
832 #else 832 #else
833 if (ptn && (!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER, 833 if (ptn && (!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER,
834 strlen(FASTBOOT_PARTITION_BOOTLOADER)) || 834 strlen(FASTBOOT_PARTITION_BOOTLOADER)) ||
835 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT, 835 !strncmp(ptn->name, FASTBOOT_PARTITION_BOOT,
836 strlen(FASTBOOT_PARTITION_BOOT)) || 836 strlen(FASTBOOT_PARTITION_BOOT)) ||
837 #ifdef CONFIG_FASTBOOT_LOCK 837 #ifdef CONFIG_FASTBOOT_LOCK
838 !strncmp(ptn->name, FASTBOOT_PARTITION_FBMISC, 838 !strncmp(ptn->name, FASTBOOT_PARTITION_FBMISC,
839 strlen(FASTBOOT_PARTITION_FBMISC)) || 839 strlen(FASTBOOT_PARTITION_FBMISC)) ||
840 #endif 840 #endif
841 !strncmp(ptn->name, FASTBOOT_PARTITION_MISC, 841 !strncmp(ptn->name, FASTBOOT_PARTITION_MISC,
842 strlen(FASTBOOT_PARTITION_MISC)))) { 842 strlen(FASTBOOT_PARTITION_MISC)))) {
843 #endif 843 #endif
844 printf("support sparse flash partition for %s\n", ptn->name); 844 printf("support sparse flash partition for %s\n", ptn->name);
845 return 1; 845 return 1;
846 } else 846 } else
847 return 0; 847 return 0;
848 } 848 }
849 849
850 static lbaint_t mmc_sparse_write(struct sparse_storage *info, 850 static lbaint_t mmc_sparse_write(struct sparse_storage *info,
851 lbaint_t blk, lbaint_t blkcnt, const void *buffer) 851 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
852 { 852 {
853 #define SPARSE_FILL_BUF_SIZE (2 * 1024 * 1024) 853 #define SPARSE_FILL_BUF_SIZE (2 * 1024 * 1024)
854 854
855 855
856 struct blk_desc *dev_desc = (struct blk_desc *)info->priv; 856 struct blk_desc *dev_desc = (struct blk_desc *)info->priv;
857 ulong ret = 0; 857 ulong ret = 0;
858 void *data; 858 void *data;
859 int fill_buf_num_blks, cnt; 859 int fill_buf_num_blks, cnt;
860 860
861 if ((unsigned long)buffer & (CONFIG_SYS_CACHELINE_SIZE - 1)) { 861 if ((unsigned long)buffer & (CONFIG_SYS_CACHELINE_SIZE - 1)) {
862 862
863 fill_buf_num_blks = SPARSE_FILL_BUF_SIZE / info->blksz; 863 fill_buf_num_blks = SPARSE_FILL_BUF_SIZE / info->blksz;
864 864
865 data = memalign(CONFIG_SYS_CACHELINE_SIZE, fill_buf_num_blks * info->blksz); 865 data = memalign(CONFIG_SYS_CACHELINE_SIZE, fill_buf_num_blks * info->blksz);
866 866
867 while (blkcnt) { 867 while (blkcnt) {
868 868
869 if (blkcnt > fill_buf_num_blks) 869 if (blkcnt > fill_buf_num_blks)
870 cnt = fill_buf_num_blks; 870 cnt = fill_buf_num_blks;
871 else 871 else
872 cnt = blkcnt; 872 cnt = blkcnt;
873 873
874 memcpy(data, buffer, cnt * info->blksz); 874 memcpy(data, buffer, cnt * info->blksz);
875 875
876 ret += blk_dwrite(dev_desc, blk, cnt, data); 876 ret += blk_dwrite(dev_desc, blk, cnt, data);
877 877
878 blk += cnt; 878 blk += cnt;
879 blkcnt -= cnt; 879 blkcnt -= cnt;
880 buffer = (void *)((unsigned long)buffer + cnt * info->blksz); 880 buffer = (void *)((unsigned long)buffer + cnt * info->blksz);
881 881
882 } 882 }
883 883
884 free(data); 884 free(data);
885 } else { 885 } else {
886 ret = blk_dwrite(dev_desc, blk, blkcnt, buffer); 886 ret = blk_dwrite(dev_desc, blk, blkcnt, buffer);
887 } 887 }
888 888
889 return ret; 889 return ret;
890 } 890 }
891 891
892 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info, 892 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
893 lbaint_t blk, lbaint_t blkcnt) 893 lbaint_t blk, lbaint_t blkcnt)
894 { 894 {
895 return blkcnt; 895 return blkcnt;
896 } 896 }
897 897
898 /*judge wether the gpt image and bootloader image are overlay*/ 898 /*judge wether the gpt image and bootloader image are overlay*/
899 bool bootloader_gpt_overlay(void) 899 bool bootloader_gpt_overlay(void)
900 { 900 {
901 return (g_ptable[PTN_GPT_INDEX].partition_id == g_ptable[PTN_BOOTLOADER_INDEX].partition_id && 901 return (g_ptable[PTN_GPT_INDEX].partition_id == g_ptable[PTN_BOOTLOADER_INDEX].partition_id &&
902 bootloader_mmc_offset() < ANDROID_GPT_END); 902 bootloader_mmc_offset() < ANDROID_GPT_END);
903 } 903 }
904 904
905 int write_backup_gpt(void) 905 int write_backup_gpt(void)
906 { 906 {
907 int mmc_no = 0; 907 int mmc_no = 0;
908 struct mmc *mmc; 908 struct mmc *mmc;
909 struct blk_desc *dev_desc; 909 struct blk_desc *dev_desc;
910 910
911 mmc_no = fastboot_devinfo.dev_id; 911 mmc_no = fastboot_devinfo.dev_id;
912 mmc = find_mmc_device(mmc_no); 912 mmc = find_mmc_device(mmc_no);
913 if (mmc == NULL) { 913 if (mmc == NULL) {
914 printf("invalid mmc device\n"); 914 printf("invalid mmc device\n");
915 return -1; 915 return -1;
916 } 916 }
917 dev_desc = blk_get_dev("mmc", mmc_no); 917 dev_desc = blk_get_dev("mmc", mmc_no);
918 if (dev_desc == NULL) { 918 if (dev_desc == NULL) {
919 printf("Can't get Block device MMC %d\n", 919 printf("Can't get Block device MMC %d\n",
920 mmc_no); 920 mmc_no);
921 return -ENODEV; 921 return -ENODEV;
922 } 922 }
923 923
924 /* write backup get partition */ 924 /* write backup get partition */
925 if (write_backup_gpt_partitions(dev_desc, interface.transfer_buffer)) { 925 if (write_backup_gpt_partitions(dev_desc, interface.transfer_buffer)) {
926 printf("writing GPT image fail\n"); 926 printf("writing GPT image fail\n");
927 return -1; 927 return -1;
928 } 928 }
929 929
930 printf("flash backup gpt image successfully\n"); 930 printf("flash backup gpt image successfully\n");
931 return 0; 931 return 0;
932 } 932 }
933 static int get_fastboot_target_dev(char *mmc_dev, struct fastboot_ptentry *ptn) 933 static int get_fastboot_target_dev(char *mmc_dev, struct fastboot_ptentry *ptn)
934 { 934 {
935 int dev = 0; 935 int dev = 0;
936 struct mmc *target_mmc; 936 struct mmc *target_mmc;
937 937
938 /* Support flash bootloader to mmc 'target_ubootdev' devices, if the 938 /* Support flash bootloader to mmc 'target_ubootdev' devices, if the
939 * 'target_ubootdev' env is not set just flash bootloader to current 939 * 'target_ubootdev' env is not set just flash bootloader to current
940 * mmc device. 940 * mmc device.
941 */ 941 */
942 if ((!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER, 942 if ((!strncmp(ptn->name, FASTBOOT_PARTITION_BOOTLOADER,
943 sizeof(FASTBOOT_PARTITION_BOOTLOADER))) && 943 sizeof(FASTBOOT_PARTITION_BOOTLOADER))) &&
944 (env_get("target_ubootdev"))) { 944 (env_get("target_ubootdev"))) {
945 dev = simple_strtoul(env_get("target_ubootdev"), NULL, 10); 945 dev = simple_strtoul(env_get("target_ubootdev"), NULL, 10);
946 946
947 /* if target_ubootdev is set, it must be that users want to change 947 /* if target_ubootdev is set, it must be that users want to change
948 * fastboot device, then fastboot environment need to be updated */ 948 * fastboot device, then fastboot environment need to be updated */
949 fastboot_setup(); 949 fastboot_setup();
950 950
951 target_mmc = find_mmc_device(dev); 951 target_mmc = find_mmc_device(dev);
952 if ((target_mmc == NULL) || mmc_init(target_mmc)) { 952 if ((target_mmc == NULL) || mmc_init(target_mmc)) {
953 printf("MMC card init failed!\n"); 953 printf("MMC card init failed!\n");
954 return -1; 954 return -1;
955 } else { 955 } else {
956 printf("Flash target is mmc%d\n", dev); 956 printf("Flash target is mmc%d\n", dev);
957 if (target_mmc->part_config != MMCPART_NOAVAILABLE) 957 if (target_mmc->part_config != MMCPART_NOAVAILABLE)
958 sprintf(mmc_dev, "mmc dev %x %x", dev, /*slot no*/ 958 sprintf(mmc_dev, "mmc dev %x %x", dev, /*slot no*/
959 FASTBOOT_MMC_BOOT_PARTITION_ID/*part no*/); 959 FASTBOOT_MMC_BOOT_PARTITION_ID/*part no*/);
960 else 960 else
961 sprintf(mmc_dev, "mmc dev %x", dev); 961 sprintf(mmc_dev, "mmc dev %x", dev);
962 } 962 }
963 } else if (ptn->partition_id != FASTBOOT_MMC_NONE_PARTITION_ID) 963 } else if (ptn->partition_id != FASTBOOT_MMC_NONE_PARTITION_ID)
964 sprintf(mmc_dev, "mmc dev %x %x", 964 sprintf(mmc_dev, "mmc dev %x %x",
965 fastboot_devinfo.dev_id, /*slot no*/ 965 fastboot_devinfo.dev_id, /*slot no*/
966 ptn->partition_id /*part no*/); 966 ptn->partition_id /*part no*/);
967 else 967 else
968 sprintf(mmc_dev, "mmc dev %x", 968 sprintf(mmc_dev, "mmc dev %x",
969 fastboot_devinfo.dev_id /*slot no*/); 969 fastboot_devinfo.dev_id /*slot no*/);
970 return 0; 970 return 0;
971 } 971 }
972 static void process_flash_mmc(const char *cmdbuf) 972 static void process_flash_mmc(const char *cmdbuf)
973 { 973 {
974 if (download_bytes) { 974 if (download_bytes) {
975 struct fastboot_ptentry *ptn; 975 struct fastboot_ptentry *ptn;
976 976
977 /* Next is the partition name */ 977 /* Next is the partition name */
978 ptn = fastboot_flash_find_ptn(cmdbuf); 978 ptn = fastboot_flash_find_ptn(cmdbuf);
979 if (ptn == NULL) { 979 if (ptn == NULL) {
980 fastboot_fail("partition does not exist"); 980 fastboot_fail("partition does not exist");
981 fastboot_flash_dump_ptn(); 981 fastboot_flash_dump_ptn();
982 } else if ((download_bytes > 982 } else if ((download_bytes >
983 ptn->length * MMC_SATA_BLOCK_SIZE) && 983 ptn->length * MMC_SATA_BLOCK_SIZE) &&
984 !(ptn->flags & FASTBOOT_PTENTRY_FLAGS_WRITE_ENV)) { 984 !(ptn->flags & FASTBOOT_PTENTRY_FLAGS_WRITE_ENV)) {
985 printf("Image too large for the partition\n"); 985 printf("Image too large for the partition\n");
986 fastboot_fail("image too large for partition"); 986 fastboot_fail("image too large for partition");
987 } else if (ptn->flags & FASTBOOT_PTENTRY_FLAGS_WRITE_ENV) { 987 } else if (ptn->flags & FASTBOOT_PTENTRY_FLAGS_WRITE_ENV) {
988 /* Since the response can only be 64 bytes, 988 /* Since the response can only be 64 bytes,
989 there is no point in having a large error message. */ 989 there is no point in having a large error message. */
990 char err_string[32]; 990 char err_string[32];
991 if (saveenv_to_ptn(ptn, &err_string[0])) { 991 if (saveenv_to_ptn(ptn, &err_string[0])) {
992 printf("savenv '%s' failed : %s\n", ptn->name, err_string); 992 printf("savenv '%s' failed : %s\n", ptn->name, err_string);
993 fastboot_fail(err_string); 993 fastboot_fail(err_string);
994 } else { 994 } else {
995 printf("partition '%s' saveenv-ed\n", ptn->name); 995 printf("partition '%s' saveenv-ed\n", ptn->name);
996 fastboot_okay(""); 996 fastboot_okay("");
997 } 997 }
998 } else { 998 } else {
999 unsigned int temp; 999 unsigned int temp;
1000 1000
1001 char blk_dev[128]; 1001 char blk_dev[128];
1002 char blk_write[128]; 1002 char blk_write[128];
1003 int blkret; 1003 int blkret;
1004 1004
1005 printf("writing to partition '%s'\n", ptn->name); 1005 printf("writing to partition '%s'\n", ptn->name);
1006 /* Get target flash device. */ 1006 /* Get target flash device. */
1007 if (get_fastboot_target_dev(blk_dev, ptn) != 0) 1007 if (get_fastboot_target_dev(blk_dev, ptn) != 0)
1008 return; 1008 return;
1009 1009
1010 if (!is_raw_partition(ptn) && 1010 if (!is_raw_partition(ptn) &&
1011 is_sparse_image(interface.transfer_buffer)) { 1011 is_sparse_image(interface.transfer_buffer)) {
1012 int dev_no = 0; 1012 int dev_no = 0;
1013 struct mmc *mmc; 1013 struct mmc *mmc;
1014 struct blk_desc *dev_desc; 1014 struct blk_desc *dev_desc;
1015 disk_partition_t info; 1015 disk_partition_t info;
1016 struct sparse_storage sparse; 1016 struct sparse_storage sparse;
1017 1017
1018 dev_no = fastboot_devinfo.dev_id; 1018 dev_no = fastboot_devinfo.dev_id;
1019 1019
1020 printf("sparse flash target is %s:%d\n", 1020 printf("sparse flash target is %s:%d\n",
1021 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", 1021 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc",
1022 dev_no); 1022 dev_no);
1023 if (fastboot_devinfo.type == DEV_MMC) { 1023 if (fastboot_devinfo.type == DEV_MMC) {
1024 mmc = find_mmc_device(dev_no); 1024 mmc = find_mmc_device(dev_no);
1025 if (mmc && mmc_init(mmc)) 1025 if (mmc && mmc_init(mmc))
1026 printf("MMC card init failed!\n"); 1026 printf("MMC card init failed!\n");
1027 } 1027 }
1028 1028
1029 dev_desc = blk_get_dev(fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", dev_no); 1029 dev_desc = blk_get_dev(fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", dev_no);
1030 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { 1030 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
1031 printf("** Block device %s %d not supported\n", 1031 printf("** Block device %s %d not supported\n",
1032 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", 1032 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc",
1033 dev_no); 1033 dev_no);
1034 return; 1034 return;
1035 } 1035 }
1036 1036
1037 if( strncmp(ptn->name, FASTBOOT_PARTITION_ALL, 1037 if( strncmp(ptn->name, FASTBOOT_PARTITION_ALL,
1038 strlen(FASTBOOT_PARTITION_ALL)) == 0) { 1038 strlen(FASTBOOT_PARTITION_ALL)) == 0) {
1039 info.blksz = dev_desc->blksz; 1039 info.blksz = dev_desc->blksz;
1040 info.size = dev_desc->lba; 1040 info.size = dev_desc->lba;
1041 info.start = 0; 1041 info.start = 0;
1042 } else { 1042 } else {
1043 1043
1044 if (part_get_info(dev_desc, 1044 if (part_get_info(dev_desc,
1045 ptn->partition_index, &info)) { 1045 ptn->partition_index, &info)) {
1046 printf("Bad partition index:%d for partition:%s\n", 1046 printf("Bad partition index:%d for partition:%s\n",
1047 ptn->partition_index, ptn->name); 1047 ptn->partition_index, ptn->name);
1048 return; 1048 return;
1049 } 1049 }
1050 } 1050 }
1051 printf("writing to partition '%s' for sparse, buffer size %d\n", 1051 printf("writing to partition '%s' for sparse, buffer size %d\n",
1052 ptn->name, download_bytes); 1052 ptn->name, download_bytes);
1053 1053
1054 sparse.blksz = info.blksz; 1054 sparse.blksz = info.blksz;
1055 sparse.start = info.start; 1055 sparse.start = info.start;
1056 sparse.size = info.size; 1056 sparse.size = info.size;
1057 sparse.write = mmc_sparse_write; 1057 sparse.write = mmc_sparse_write;
1058 sparse.reserve = mmc_sparse_reserve; 1058 sparse.reserve = mmc_sparse_reserve;
1059 printf("Flashing sparse image at offset " LBAFU "\n", 1059 printf("Flashing sparse image at offset " LBAFU "\n",
1060 sparse.start); 1060 sparse.start);
1061 1061
1062 sparse.priv = dev_desc; 1062 sparse.priv = dev_desc;
1063 write_sparse_image(&sparse, ptn->name, interface.transfer_buffer, 1063 write_sparse_image(&sparse, ptn->name, interface.transfer_buffer,
1064 download_bytes); 1064 download_bytes);
1065 1065
1066 } else { 1066 } else {
1067 /* Will flash images in below case: 1067 /* Will flash images in below case:
1068 * 1. Is not gpt partition. 1068 * 1. Is not gpt partition.
1069 * 2. Is gpt partition but no overlay detected. 1069 * 2. Is gpt partition but no overlay detected.
1070 * */ 1070 * */
1071 if (strncmp(ptn->name, "gpt", 3) || !bootloader_gpt_overlay()) { 1071 if (strncmp(ptn->name, "gpt", 3) || !bootloader_gpt_overlay()) {
1072 /* block count */ 1072 /* block count */
1073 if (strncmp(ptn->name, "gpt", 3) == 0) { 1073 if (strncmp(ptn->name, "gpt", 3) == 0) {
1074 temp = (ANDROID_GPT_END + 1074 temp = (ANDROID_GPT_END +
1075 MMC_SATA_BLOCK_SIZE - 1) / 1075 MMC_SATA_BLOCK_SIZE - 1) /
1076 MMC_SATA_BLOCK_SIZE; 1076 MMC_SATA_BLOCK_SIZE;
1077 } else { 1077 } else {
1078 temp = (download_bytes + 1078 temp = (download_bytes +
1079 MMC_SATA_BLOCK_SIZE - 1) / 1079 MMC_SATA_BLOCK_SIZE - 1) /
1080 MMC_SATA_BLOCK_SIZE; 1080 MMC_SATA_BLOCK_SIZE;
1081 } 1081 }
1082 1082
1083 sprintf(blk_write, "%s write 0x%x 0x%x 0x%x", 1083 sprintf(blk_write, "%s write 0x%x 0x%x 0x%x",
1084 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", 1084 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc",
1085 (unsigned int)(uintptr_t)interface.transfer_buffer, /*source*/ 1085 (unsigned int)(uintptr_t)interface.transfer_buffer, /*source*/
1086 ptn->start, /*dest*/ 1086 ptn->start, /*dest*/
1087 temp /*length*/); 1087 temp /*length*/);
1088 1088
1089 printf("Initializing '%s'\n", ptn->name); 1089 printf("Initializing '%s'\n", ptn->name);
1090 1090
1091 blkret = run_command(blk_dev, 0); 1091 blkret = run_command(blk_dev, 0);
1092 if (blkret) 1092 if (blkret)
1093 fastboot_fail("Init of BLK device failed"); 1093 fastboot_fail("Init of BLK device failed");
1094 else 1094 else
1095 fastboot_okay(""); 1095 fastboot_okay("");
1096 1096
1097 printf("Writing '%s'\n", ptn->name); 1097 printf("Writing '%s'\n", ptn->name);
1098 if (run_command(blk_write, 0)) { 1098 if (run_command(blk_write, 0)) {
1099 printf("Writing '%s' FAILED!\n", ptn->name); 1099 printf("Writing '%s' FAILED!\n", ptn->name);
1100 fastboot_fail("Write partition failed"); 1100 fastboot_fail("Write partition failed");
1101 } else { 1101 } else {
1102 printf("Writing '%s' DONE!\n", ptn->name); 1102 printf("Writing '%s' DONE!\n", ptn->name);
1103 fastboot_okay(""); 1103 fastboot_okay("");
1104 } 1104 }
1105 } 1105 }
1106 /* Write backup gpt image */ 1106 /* Write backup gpt image */
1107 if (strncmp(ptn->name, "gpt", 3) == 0) { 1107 if (strncmp(ptn->name, "gpt", 3) == 0) {
1108 if (write_backup_gpt()) 1108 if (write_backup_gpt())
1109 fastboot_fail("write backup GPT image fail"); 1109 fastboot_fail("write backup GPT image fail");
1110 else 1110 else
1111 fastboot_okay(""); 1111 fastboot_okay("");
1112 1112
1113 /* will force scan the device, 1113 /* will force scan the device,
1114 * so dev_desc can be re-inited 1114 * so dev_desc can be re-inited
1115 * with the latest data */ 1115 * with the latest data */
1116 run_command(blk_dev, 0); 1116 run_command(blk_dev, 0);
1117 } 1117 }
1118 } 1118 }
1119 } 1119 }
1120 } else { 1120 } else {
1121 fastboot_fail("no image downloaded"); 1121 fastboot_fail("no image downloaded");
1122 } 1122 }
1123 } 1123 }
1124 1124
1125 #endif 1125 #endif
1126 1126
1127 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1127 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1128 static void process_erase_mmc(const char *cmdbuf, char *response) 1128 static void process_erase_mmc(const char *cmdbuf, char *response)
1129 { 1129 {
1130 int mmc_no = 0; 1130 int mmc_no = 0;
1131 lbaint_t blks, blks_start, blks_size, grp_size; 1131 lbaint_t blks, blks_start, blks_size, grp_size;
1132 struct mmc *mmc; 1132 struct mmc *mmc;
1133 struct blk_desc *dev_desc; 1133 struct blk_desc *dev_desc;
1134 struct fastboot_ptentry *ptn; 1134 struct fastboot_ptentry *ptn;
1135 disk_partition_t info; 1135 disk_partition_t info;
1136 1136
1137 ptn = fastboot_flash_find_ptn(cmdbuf); 1137 ptn = fastboot_flash_find_ptn(cmdbuf);
1138 if ((ptn == NULL) || (ptn->flags & FASTBOOT_PTENTRY_FLAGS_UNERASEABLE)) { 1138 if ((ptn == NULL) || (ptn->flags & FASTBOOT_PTENTRY_FLAGS_UNERASEABLE)) {
1139 sprintf(response, "FAILpartition does not exist or uneraseable"); 1139 sprintf(response, "FAILpartition does not exist or uneraseable");
1140 fastboot_flash_dump_ptn(); 1140 fastboot_flash_dump_ptn();
1141 return; 1141 return;
1142 } 1142 }
1143 1143
1144 mmc_no = fastboot_devinfo.dev_id; 1144 mmc_no = fastboot_devinfo.dev_id;
1145 printf("erase target is MMC:%d\n", mmc_no); 1145 printf("erase target is MMC:%d\n", mmc_no);
1146 1146
1147 mmc = find_mmc_device(mmc_no); 1147 mmc = find_mmc_device(mmc_no);
1148 if ((mmc == NULL) || mmc_init(mmc)) { 1148 if ((mmc == NULL) || mmc_init(mmc)) {
1149 printf("MMC card init failed!\n"); 1149 printf("MMC card init failed!\n");
1150 return; 1150 return;
1151 } 1151 }
1152 1152
1153 dev_desc = blk_get_dev("mmc", mmc_no); 1153 dev_desc = blk_get_dev("mmc", mmc_no);
1154 if (NULL == dev_desc) { 1154 if (NULL == dev_desc) {
1155 printf("Block device MMC %d not supported\n", 1155 printf("Block device MMC %d not supported\n",
1156 mmc_no); 1156 mmc_no);
1157 sprintf(response, "FAILnot valid MMC card"); 1157 sprintf(response, "FAILnot valid MMC card");
1158 return; 1158 return;
1159 } 1159 }
1160 1160
1161 if (part_get_info(dev_desc, 1161 if (part_get_info(dev_desc,
1162 ptn->partition_index, &info)) { 1162 ptn->partition_index, &info)) {
1163 printf("Bad partition index:%d for partition:%s\n", 1163 printf("Bad partition index:%d for partition:%s\n",
1164 ptn->partition_index, ptn->name); 1164 ptn->partition_index, ptn->name);
1165 sprintf(response, "FAILerasing of MMC card"); 1165 sprintf(response, "FAILerasing of MMC card");
1166 return; 1166 return;
1167 } 1167 }
1168 1168
1169 /* Align blocks to erase group size to avoid erasing other partitions */ 1169 /* Align blocks to erase group size to avoid erasing other partitions */
1170 grp_size = mmc->erase_grp_size; 1170 grp_size = mmc->erase_grp_size;
1171 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); 1171 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
1172 if (info.size >= grp_size) 1172 if (info.size >= grp_size)
1173 blks_size = (info.size - (blks_start - info.start)) & 1173 blks_size = (info.size - (blks_start - info.start)) &
1174 (~(grp_size - 1)); 1174 (~(grp_size - 1));
1175 else 1175 else
1176 blks_size = 0; 1176 blks_size = 0;
1177 1177
1178 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", 1178 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
1179 blks_start, blks_start + blks_size); 1179 blks_start, blks_start + blks_size);
1180 1180
1181 blks = blk_derase(dev_desc, blks_start, blks_size); 1181 blks = blk_derase(dev_desc, blks_start, blks_size);
1182 if (blks != blks_size) { 1182 if (blks != blks_size) {
1183 printf("failed erasing from device %d", dev_desc->devnum); 1183 printf("failed erasing from device %d", dev_desc->devnum);
1184 sprintf(response, "FAILerasing of MMC card"); 1184 sprintf(response, "FAILerasing of MMC card");
1185 return; 1185 return;
1186 } 1186 }
1187 1187
1188 printf("........ erased " LBAFU " bytes from '%s'\n", 1188 printf("........ erased " LBAFU " bytes from '%s'\n",
1189 blks_size * info.blksz, cmdbuf); 1189 blks_size * info.blksz, cmdbuf);
1190 sprintf(response, "OKAY"); 1190 sprintf(response, "OKAY");
1191 1191
1192 return; 1192 return;
1193 } 1193 }
1194 #endif 1194 #endif
1195 1195
1196 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) 1196 #if defined(CONFIG_FASTBOOT_STORAGE_SATA)
1197 static void process_erase_sata(const char *cmdbuf, char *response) 1197 static void process_erase_sata(const char *cmdbuf, char *response)
1198 { 1198 {
1199 return; 1199 return;
1200 } 1200 }
1201 #endif 1201 #endif
1202 1202
1203 static void rx_process_erase(const char *cmdbuf, char *response) 1203 static void rx_process_erase(const char *cmdbuf, char *response)
1204 { 1204 {
1205 switch (fastboot_devinfo.type) { 1205 switch (fastboot_devinfo.type) {
1206 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) 1206 #if defined(CONFIG_FASTBOOT_STORAGE_SATA)
1207 case DEV_SATA: 1207 case DEV_SATA:
1208 process_erase_sata(cmdbuf, response); 1208 process_erase_sata(cmdbuf, response);
1209 break; 1209 break;
1210 #endif 1210 #endif
1211 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1211 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1212 case DEV_MMC: 1212 case DEV_MMC:
1213 process_erase_mmc(cmdbuf, response); 1213 process_erase_mmc(cmdbuf, response);
1214 break; 1214 break;
1215 #endif 1215 #endif
1216 default: 1216 default:
1217 printf("Not support flash command for current device %d\n", 1217 printf("Not support flash command for current device %d\n",
1218 fastboot_devinfo.type); 1218 fastboot_devinfo.type);
1219 sprintf(response, 1219 sprintf(response,
1220 "FAILfailed to flash device"); 1220 "FAILfailed to flash device");
1221 break; 1221 break;
1222 } 1222 }
1223 } 1223 }
1224 1224
1225 static void rx_process_flash(const char *cmdbuf) 1225 static void rx_process_flash(const char *cmdbuf)
1226 { 1226 {
1227 /* Check if we need to flash mcu firmware */ 1227 /* Check if we need to flash mcu firmware */
1228 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 1228 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
1229 if (!strncmp(cmdbuf, FASTBOOT_MCU_FIRMWARE_PARTITION, 1229 if (!strncmp(cmdbuf, FASTBOOT_MCU_FIRMWARE_PARTITION,
1230 sizeof(FASTBOOT_MCU_FIRMWARE_PARTITION))) { 1230 sizeof(FASTBOOT_MCU_FIRMWARE_PARTITION))) {
1231 switch (fastboot_firmwareinfo.type) { 1231 switch (fastboot_firmwareinfo.type) {
1232 case DEV_SF: 1232 case DEV_SF:
1233 process_flash_sf(cmdbuf); 1233 process_flash_sf(cmdbuf);
1234 break; 1234 break;
1235 #ifdef CONFIG_ARCH_IMX8M 1235 #ifdef CONFIG_ARCH_IMX8M
1236 case DEV_MMC: 1236 case DEV_MMC:
1237 if (is_tcm_image(interface.transfer_buffer)) 1237 if (is_tcm_image(interface.transfer_buffer))
1238 process_flash_mmc(cmdbuf); 1238 process_flash_mmc(cmdbuf);
1239 break; 1239 break;
1240 #endif 1240 #endif
1241 default: 1241 default:
1242 printf("Don't support flash firmware\n"); 1242 printf("Don't support flash firmware\n");
1243 } 1243 }
1244 return; 1244 return;
1245 } 1245 }
1246 #endif 1246 #endif
1247 /* Normal case */ 1247 /* Normal case */
1248 switch (fastboot_devinfo.type) { 1248 switch (fastboot_devinfo.type) {
1249 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) 1249 #if defined(CONFIG_FASTBOOT_STORAGE_SATA)
1250 case DEV_SATA: 1250 case DEV_SATA:
1251 process_flash_mmc(cmdbuf); 1251 process_flash_mmc(cmdbuf);
1252 break; 1252 break;
1253 #endif 1253 #endif
1254 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1254 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1255 case DEV_MMC: 1255 case DEV_MMC:
1256 process_flash_mmc(cmdbuf); 1256 process_flash_mmc(cmdbuf);
1257 break; 1257 break;
1258 #endif 1258 #endif
1259 default: 1259 default:
1260 printf("Not support flash command for current device %d\n", 1260 printf("Not support flash command for current device %d\n",
1261 fastboot_devinfo.type); 1261 fastboot_devinfo.type);
1262 fastboot_fail("failed to flash device"); 1262 fastboot_fail("failed to flash device");
1263 break; 1263 break;
1264 } 1264 }
1265 } 1265 }
1266 1266
1267 1267
1268 static void parameters_setup(void) 1268 static void parameters_setup(void)
1269 { 1269 {
1270 interface.nand_block_size = 0; 1270 interface.nand_block_size = 0;
1271 interface.transfer_buffer = 1271 interface.transfer_buffer =
1272 (unsigned char *)env_get_ulong("fastboot_buffer", 16, CONFIG_FASTBOOT_BUF_ADDR); 1272 (unsigned char *)env_get_ulong("fastboot_buffer", 16, CONFIG_FASTBOOT_BUF_ADDR);
1273 interface.transfer_buffer_size = 1273 interface.transfer_buffer_size =
1274 CONFIG_FASTBOOT_BUF_SIZE; 1274 CONFIG_FASTBOOT_BUF_SIZE;
1275 } 1275 }
1276 1276
1277 static int _fastboot_setup_dev(int *switched) 1277 static int _fastboot_setup_dev(int *switched)
1278 { 1278 {
1279 char *fastboot_env; 1279 char *fastboot_env;
1280 struct fastboot_device_info devinfo;; 1280 struct fastboot_device_info devinfo;;
1281 fastboot_env = env_get("fastboot_dev"); 1281 fastboot_env = env_get("fastboot_dev");
1282 1282
1283 if (fastboot_env) { 1283 if (fastboot_env) {
1284 if (!strcmp(fastboot_env, "sata")) { 1284 if (!strcmp(fastboot_env, "sata")) {
1285 devinfo.type = DEV_SATA; 1285 devinfo.type = DEV_SATA;
1286 devinfo.dev_id = 0; 1286 devinfo.dev_id = 0;
1287 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1287 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1288 } else if (!strncmp(fastboot_env, "mmc", 3)) { 1288 } else if (!strncmp(fastboot_env, "mmc", 3)) {
1289 devinfo.type = DEV_MMC; 1289 devinfo.type = DEV_MMC;
1290 if(env_get("target_ubootdev")) 1290 if(env_get("target_ubootdev"))
1291 devinfo.dev_id = simple_strtoul(env_get("target_ubootdev"), NULL, 10); 1291 devinfo.dev_id = simple_strtoul(env_get("target_ubootdev"), NULL, 10);
1292 else 1292 else
1293 devinfo.dev_id = mmc_get_env_dev(); 1293 devinfo.dev_id = mmc_get_env_dev();
1294 #endif 1294 #endif
1295 } else { 1295 } else {
1296 return 1; 1296 return 1;
1297 } 1297 }
1298 } else { 1298 } else {
1299 return 1; 1299 return 1;
1300 } 1300 }
1301 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 1301 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
1302 /* For imx7ulp, flash m4 images directly to spi nor-flash, M4 will 1302 /* For imx7ulp, flash m4 images directly to spi nor-flash, M4 will
1303 * run automatically after powered on. For imx8mq, flash m4 images to 1303 * run automatically after powered on. For imx8mq, flash m4 images to
1304 * physical partition 'm4_os', m4 will be kicked off by A core. */ 1304 * physical partition 'm4_os', m4 will be kicked off by A core. */
1305 fastboot_firmwareinfo.type = ANDROID_MCU_FRIMWARE_DEV_TYPE; 1305 fastboot_firmwareinfo.type = ANDROID_MCU_FRIMWARE_DEV_TYPE;
1306 #endif 1306 #endif
1307 1307
1308 if (switched) { 1308 if (switched) {
1309 if (devinfo.type != fastboot_devinfo.type || devinfo.dev_id != fastboot_devinfo.dev_id) 1309 if (devinfo.type != fastboot_devinfo.type || devinfo.dev_id != fastboot_devinfo.dev_id)
1310 *switched = 1; 1310 *switched = 1;
1311 else 1311 else
1312 *switched = 0; 1312 *switched = 0;
1313 } 1313 }
1314 1314
1315 fastboot_devinfo.type = devinfo.type; 1315 fastboot_devinfo.type = devinfo.type;
1316 fastboot_devinfo.dev_id = devinfo.dev_id; 1316 fastboot_devinfo.dev_id = devinfo.dev_id;
1317 1317
1318 return 0; 1318 return 0;
1319 } 1319 }
1320 1320
1321 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) \ 1321 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) \
1322 || defined(CONFIG_FASTBOOT_STORAGE_MMC) 1322 || defined(CONFIG_FASTBOOT_STORAGE_MMC)
1323 /** 1323 /**
1324 @mmc_dos_partition_index: the partition index in mbr. 1324 @mmc_dos_partition_index: the partition index in mbr.
1325 @mmc_partition_index: the boot partition or user partition index, 1325 @mmc_partition_index: the boot partition or user partition index,
1326 not related to the partition table. 1326 not related to the partition table.
1327 */ 1327 */
1328 static int _fastboot_parts_add_ptable_entry(int ptable_index, 1328 static int _fastboot_parts_add_ptable_entry(int ptable_index,
1329 int mmc_dos_partition_index, 1329 int mmc_dos_partition_index,
1330 int mmc_partition_index, 1330 int mmc_partition_index,
1331 const char *name, 1331 const char *name,
1332 const char *fstype, 1332 const char *fstype,
1333 struct blk_desc *dev_desc, 1333 struct blk_desc *dev_desc,
1334 struct fastboot_ptentry *ptable) 1334 struct fastboot_ptentry *ptable)
1335 { 1335 {
1336 disk_partition_t info; 1336 disk_partition_t info;
1337 1337
1338 if (part_get_info(dev_desc, 1338 if (part_get_info(dev_desc,
1339 mmc_dos_partition_index, &info)) { 1339 mmc_dos_partition_index, &info)) {
1340 debug("Bad partition index:%d for partition:%s\n", 1340 debug("Bad partition index:%d for partition:%s\n",
1341 mmc_dos_partition_index, name); 1341 mmc_dos_partition_index, name);
1342 return -1; 1342 return -1;
1343 } 1343 }
1344 ptable[ptable_index].start = info.start; 1344 ptable[ptable_index].start = info.start;
1345 ptable[ptable_index].length = info.size; 1345 ptable[ptable_index].length = info.size;
1346 ptable[ptable_index].partition_id = mmc_partition_index; 1346 ptable[ptable_index].partition_id = mmc_partition_index;
1347 ptable[ptable_index].partition_index = mmc_dos_partition_index; 1347 ptable[ptable_index].partition_index = mmc_dos_partition_index;
1348 strncpy(ptable[ptable_index].name, (const char *)info.name, 1348 strncpy(ptable[ptable_index].name, (const char *)info.name,
1349 sizeof(ptable[ptable_index].name) - 1); 1349 sizeof(ptable[ptable_index].name) - 1);
1350 1350
1351 #ifdef CONFIG_PARTITION_UUIDS 1351 #ifdef CONFIG_PARTITION_UUIDS
1352 strcpy(ptable[ptable_index].uuid, (const char *)info.uuid); 1352 strcpy(ptable[ptable_index].uuid, (const char *)info.uuid);
1353 #endif 1353 #endif
1354 #ifdef CONFIG_ANDROID_AB_SUPPORT 1354 #ifdef CONFIG_ANDROID_AB_SUPPORT
1355 if (!strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM_A) || 1355 if (!strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM_A) ||
1356 !strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM_B) || 1356 !strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM_B) ||
1357 !strcmp((const char *)info.name, FASTBOOT_PARTITION_OEM_A) || 1357 !strcmp((const char *)info.name, FASTBOOT_PARTITION_OEM_A) ||
1358 !strcmp((const char *)info.name, FASTBOOT_PARTITION_VENDOR_A) || 1358 !strcmp((const char *)info.name, FASTBOOT_PARTITION_VENDOR_A) ||
1359 !strcmp((const char *)info.name, FASTBOOT_PARTITION_OEM_B) || 1359 !strcmp((const char *)info.name, FASTBOOT_PARTITION_OEM_B) ||
1360 !strcmp((const char *)info.name, FASTBOOT_PARTITION_VENDOR_B) || 1360 !strcmp((const char *)info.name, FASTBOOT_PARTITION_VENDOR_B) ||
1361 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DATA)) 1361 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DATA))
1362 #else 1362 #else
1363 if (!strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM) || 1363 if (!strcmp((const char *)info.name, FASTBOOT_PARTITION_SYSTEM) ||
1364 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DATA) || 1364 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DATA) ||
1365 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DEVICE) || 1365 !strcmp((const char *)info.name, FASTBOOT_PARTITION_DEVICE) ||
1366 !strcmp((const char *)info.name, FASTBOOT_PARTITION_CACHE)) 1366 !strcmp((const char *)info.name, FASTBOOT_PARTITION_CACHE))
1367 #endif 1367 #endif
1368 strcpy(ptable[ptable_index].fstype, "ext4"); 1368 strcpy(ptable[ptable_index].fstype, "ext4");
1369 else 1369 else
1370 strcpy(ptable[ptable_index].fstype, "raw"); 1370 strcpy(ptable[ptable_index].fstype, "raw");
1371 return 0; 1371 return 0;
1372 } 1372 }
1373 1373
1374 static int _fastboot_parts_load_from_ptable(void) 1374 static int _fastboot_parts_load_from_ptable(void)
1375 { 1375 {
1376 int i; 1376 int i;
1377 #ifdef CONFIG_CMD_SATA 1377 #ifdef CONFIG_CMD_SATA
1378 int sata_device_no; 1378 int sata_device_no;
1379 #endif 1379 #endif
1380 1380
1381 /* mmc boot partition: -1 means no partition, 0 user part., 1 boot part. 1381 /* mmc boot partition: -1 means no partition, 0 user part., 1 boot part.
1382 * default is no partition, for emmc default user part, except emmc*/ 1382 * default is no partition, for emmc default user part, except emmc*/
1383 int boot_partition = FASTBOOT_MMC_NONE_PARTITION_ID; 1383 int boot_partition = FASTBOOT_MMC_NONE_PARTITION_ID;
1384 int user_partition = FASTBOOT_MMC_NONE_PARTITION_ID; 1384 int user_partition = FASTBOOT_MMC_NONE_PARTITION_ID;
1385 1385
1386 struct mmc *mmc; 1386 struct mmc *mmc;
1387 struct blk_desc *dev_desc; 1387 struct blk_desc *dev_desc;
1388 struct fastboot_ptentry ptable[MAX_PTN]; 1388 struct fastboot_ptentry ptable[MAX_PTN];
1389 1389
1390 /* sata case in env */ 1390 /* sata case in env */
1391 if (fastboot_devinfo.type == DEV_SATA) { 1391 if (fastboot_devinfo.type == DEV_SATA) {
1392 #ifdef CONFIG_CMD_SATA 1392 #ifdef CONFIG_CMD_SATA
1393 puts("flash target is SATA\n"); 1393 puts("flash target is SATA\n");
1394 if (sata_initialize()) 1394 if (sata_initialize())
1395 return -1; 1395 return -1;
1396 sata_device_no = CONFIG_FASTBOOT_SATA_NO; 1396 sata_device_no = CONFIG_FASTBOOT_SATA_NO;
1397 if (sata_device_no >= CONFIG_SYS_SATA_MAX_DEVICE) { 1397 if (sata_device_no >= CONFIG_SYS_SATA_MAX_DEVICE) {
1398 printf("Unknown SATA(%d) device for fastboot\n", 1398 printf("Unknown SATA(%d) device for fastboot\n",
1399 sata_device_no); 1399 sata_device_no);
1400 return -1; 1400 return -1;
1401 } 1401 }
1402 dev_desc = sata_get_dev(sata_device_no); 1402 dev_desc = sata_get_dev(sata_device_no);
1403 #else /*! CONFIG_CMD_SATA*/ 1403 #else /*! CONFIG_CMD_SATA*/
1404 puts("SATA isn't buildin\n"); 1404 puts("SATA isn't buildin\n");
1405 return -1; 1405 return -1;
1406 #endif /*! CONFIG_CMD_SATA*/ 1406 #endif /*! CONFIG_CMD_SATA*/
1407 } else if (fastboot_devinfo.type == DEV_MMC) { 1407 } else if (fastboot_devinfo.type == DEV_MMC) {
1408 int mmc_no = 0; 1408 int mmc_no = 0;
1409 mmc_no = fastboot_devinfo.dev_id; 1409 mmc_no = fastboot_devinfo.dev_id;
1410 1410
1411 printf("flash target is MMC:%d\n", mmc_no); 1411 printf("flash target is MMC:%d\n", mmc_no);
1412 mmc = find_mmc_device(mmc_no); 1412 mmc = find_mmc_device(mmc_no);
1413 1413
1414 if (mmc == NULL) { 1414 if (mmc == NULL) {
1415 printf("invalid mmc device %d\n", mmc_no); 1415 printf("invalid mmc device %d\n", mmc_no);
1416 return -1; 1416 return -1;
1417 } 1417 }
1418 1418
1419 /* Force to init mmc */ 1419 /* Force to init mmc */
1420 mmc->has_init = 0; 1420 mmc->has_init = 0;
1421 if (mmc_init(mmc)) 1421 if (mmc_init(mmc))
1422 printf("MMC card init failed!\n"); 1422 printf("MMC card init failed!\n");
1423 1423
1424 dev_desc = blk_get_dev("mmc", mmc_no); 1424 dev_desc = blk_get_dev("mmc", mmc_no);
1425 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { 1425 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
1426 printf("** Block device MMC %d not supported\n", 1426 printf("** Block device MMC %d not supported\n",
1427 mmc_no); 1427 mmc_no);
1428 return -1; 1428 return -1;
1429 } 1429 }
1430 1430
1431 /* multiple boot paritions for eMMC 4.3 later */ 1431 /* multiple boot paritions for eMMC 4.3 later */
1432 if (mmc->part_config != MMCPART_NOAVAILABLE) { 1432 if (mmc->part_config != MMCPART_NOAVAILABLE) {
1433 boot_partition = FASTBOOT_MMC_BOOT_PARTITION_ID; 1433 boot_partition = FASTBOOT_MMC_BOOT_PARTITION_ID;
1434 user_partition = FASTBOOT_MMC_USER_PARTITION_ID; 1434 user_partition = FASTBOOT_MMC_USER_PARTITION_ID;
1435 } 1435 }
1436 } else { 1436 } else {
1437 printf("Can't setup partition table on this device %d\n", 1437 printf("Can't setup partition table on this device %d\n",
1438 fastboot_devinfo.type); 1438 fastboot_devinfo.type);
1439 return -1; 1439 return -1;
1440 } 1440 }
1441 1441
1442 memset((char *)ptable, 0, 1442 memset((char *)ptable, 0,
1443 sizeof(struct fastboot_ptentry) * (MAX_PTN)); 1443 sizeof(struct fastboot_ptentry) * (MAX_PTN));
1444 /* GPT */ 1444 /* GPT */
1445 strcpy(ptable[PTN_GPT_INDEX].name, FASTBOOT_PARTITION_GPT); 1445 strcpy(ptable[PTN_GPT_INDEX].name, FASTBOOT_PARTITION_GPT);
1446 ptable[PTN_GPT_INDEX].start = ANDROID_GPT_OFFSET / dev_desc->blksz; 1446 ptable[PTN_GPT_INDEX].start = ANDROID_GPT_OFFSET / dev_desc->blksz;
1447 ptable[PTN_GPT_INDEX].length = ANDROID_GPT_SIZE / dev_desc->blksz; 1447 ptable[PTN_GPT_INDEX].length = ANDROID_GPT_SIZE / dev_desc->blksz;
1448 ptable[PTN_GPT_INDEX].partition_id = user_partition; 1448 ptable[PTN_GPT_INDEX].partition_id = user_partition;
1449 ptable[PTN_GPT_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE; 1449 ptable[PTN_GPT_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE;
1450 strcpy(ptable[PTN_GPT_INDEX].fstype, "raw"); 1450 strcpy(ptable[PTN_GPT_INDEX].fstype, "raw");
1451 1451
1452 #ifndef CONFIG_ARM64 1452 #ifndef CONFIG_ARM64
1453 /* Trusty OS */ 1453 /* Trusty OS */
1454 strcpy(ptable[PTN_TEE_INDEX].name, FASTBOOT_PARTITION_TEE); 1454 strcpy(ptable[PTN_TEE_INDEX].name, FASTBOOT_PARTITION_TEE);
1455 ptable[PTN_TEE_INDEX].start = 0; 1455 ptable[PTN_TEE_INDEX].start = 0;
1456 ptable[PTN_TEE_INDEX].length = TRUSTY_OS_MMC_BLKS; 1456 ptable[PTN_TEE_INDEX].length = TRUSTY_OS_MMC_BLKS;
1457 ptable[PTN_TEE_INDEX].partition_id = TEE_HWPARTITION_ID; 1457 ptable[PTN_TEE_INDEX].partition_id = TEE_HWPARTITION_ID;
1458 strcpy(ptable[PTN_TEE_INDEX].fstype, "raw"); 1458 strcpy(ptable[PTN_TEE_INDEX].fstype, "raw");
1459 #endif 1459 #endif
1460 1460
1461 /* Add m4_os partition if we support mcu firmware image flash */ 1461 /* Add m4_os partition if we support mcu firmware image flash */
1462 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 1462 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
1463 strcpy(ptable[PTN_M4_OS_INDEX].name, FASTBOOT_MCU_FIRMWARE_PARTITION); 1463 strcpy(ptable[PTN_M4_OS_INDEX].name, FASTBOOT_MCU_FIRMWARE_PARTITION);
1464 ptable[PTN_M4_OS_INDEX].start = ANDROID_MCU_FIRMWARE_START / dev_desc->blksz; 1464 ptable[PTN_M4_OS_INDEX].start = ANDROID_MCU_FIRMWARE_START / dev_desc->blksz;
1465 ptable[PTN_M4_OS_INDEX].length = ANDROID_MCU_FIRMWARE_SIZE / dev_desc->blksz; 1465 ptable[PTN_M4_OS_INDEX].length = ANDROID_MCU_FIRMWARE_SIZE / dev_desc->blksz;
1466 ptable[PTN_M4_OS_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE; 1466 ptable[PTN_M4_OS_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE;
1467 ptable[PTN_M4_OS_INDEX].partition_id = user_partition; 1467 ptable[PTN_M4_OS_INDEX].partition_id = user_partition;
1468 strcpy(ptable[PTN_M4_OS_INDEX].fstype, "raw"); 1468 strcpy(ptable[PTN_M4_OS_INDEX].fstype, "raw");
1469 #endif 1469 #endif
1470 1470
1471 strcpy(ptable[PTN_ALL_INDEX].name, FASTBOOT_PARTITION_ALL); 1471 strcpy(ptable[PTN_ALL_INDEX].name, FASTBOOT_PARTITION_ALL);
1472 ptable[PTN_ALL_INDEX].start = 0; 1472 ptable[PTN_ALL_INDEX].start = 0;
1473 ptable[PTN_ALL_INDEX].length = dev_desc->lba; 1473 ptable[PTN_ALL_INDEX].length = dev_desc->lba;
1474 ptable[PTN_ALL_INDEX].partition_id = user_partition; 1474 ptable[PTN_ALL_INDEX].partition_id = user_partition;
1475 strcpy(ptable[PTN_ALL_INDEX].fstype, "device"); 1475 strcpy(ptable[PTN_ALL_INDEX].fstype, "device");
1476 1476
1477 /* Bootloader */ 1477 /* Bootloader */
1478 strcpy(ptable[PTN_BOOTLOADER_INDEX].name, FASTBOOT_PARTITION_BOOTLOADER); 1478 strcpy(ptable[PTN_BOOTLOADER_INDEX].name, FASTBOOT_PARTITION_BOOTLOADER);
1479 ptable[PTN_BOOTLOADER_INDEX].start = 1479 ptable[PTN_BOOTLOADER_INDEX].start =
1480 bootloader_mmc_offset() / dev_desc->blksz; 1480 bootloader_mmc_offset() / dev_desc->blksz;
1481 ptable[PTN_BOOTLOADER_INDEX].length = 1481 ptable[PTN_BOOTLOADER_INDEX].length =
1482 ANDROID_BOOTLOADER_SIZE / dev_desc->blksz; 1482 ANDROID_BOOTLOADER_SIZE / dev_desc->blksz;
1483 ptable[PTN_BOOTLOADER_INDEX].partition_id = boot_partition; 1483 ptable[PTN_BOOTLOADER_INDEX].partition_id = boot_partition;
1484 ptable[PTN_BOOTLOADER_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE; 1484 ptable[PTN_BOOTLOADER_INDEX].flags = FASTBOOT_PTENTRY_FLAGS_UNERASEABLE;
1485 strcpy(ptable[PTN_BOOTLOADER_INDEX].fstype, "raw"); 1485 strcpy(ptable[PTN_BOOTLOADER_INDEX].fstype, "raw");
1486 1486
1487 int tbl_idx; 1487 int tbl_idx;
1488 int part_idx = 1; 1488 int part_idx = 1;
1489 int ret; 1489 int ret;
1490 for (tbl_idx = PTN_BOOTLOADER_INDEX + 1; tbl_idx < MAX_PTN; tbl_idx++) { 1490 for (tbl_idx = PTN_BOOTLOADER_INDEX + 1; tbl_idx < MAX_PTN; tbl_idx++) {
1491 ret = _fastboot_parts_add_ptable_entry(tbl_idx, 1491 ret = _fastboot_parts_add_ptable_entry(tbl_idx,
1492 part_idx++, 1492 part_idx++,
1493 user_partition, 1493 user_partition,
1494 NULL, 1494 NULL,
1495 NULL, 1495 NULL,
1496 dev_desc, ptable); 1496 dev_desc, ptable);
1497 if (ret) 1497 if (ret)
1498 break; 1498 break;
1499 } 1499 }
1500 for (i = 0; i < tbl_idx; i++) 1500 for (i = 0; i < tbl_idx; i++)
1501 fastboot_flash_add_ptn(&ptable[i]); 1501 fastboot_flash_add_ptn(&ptable[i]);
1502 1502
1503 return 0; 1503 return 0;
1504 } 1504 }
1505 #endif /*CONFIG_FASTBOOT_STORAGE_SATA || CONFIG_FASTBOOT_STORAGE_MMC*/ 1505 #endif /*CONFIG_FASTBOOT_STORAGE_SATA || CONFIG_FASTBOOT_STORAGE_MMC*/
1506 1506
1507 static void _fastboot_load_partitions(void) 1507 static void _fastboot_load_partitions(void)
1508 { 1508 {
1509 g_pcount = 0; 1509 g_pcount = 0;
1510 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) \ 1510 #if defined(CONFIG_FASTBOOT_STORAGE_SATA) \
1511 || defined(CONFIG_FASTBOOT_STORAGE_MMC) 1511 || defined(CONFIG_FASTBOOT_STORAGE_MMC)
1512 _fastboot_parts_load_from_ptable(); 1512 _fastboot_parts_load_from_ptable();
1513 #endif 1513 #endif
1514 } 1514 }
1515 1515
1516 /* 1516 /*
1517 * Android style flash utilties */ 1517 * Android style flash utilties */
1518 void fastboot_flash_add_ptn(struct fastboot_ptentry *ptn) 1518 void fastboot_flash_add_ptn(struct fastboot_ptentry *ptn)
1519 { 1519 {
1520 if (g_pcount < MAX_PTN) { 1520 if (g_pcount < MAX_PTN) {
1521 memcpy(g_ptable + g_pcount, ptn, sizeof(struct fastboot_ptentry)); 1521 memcpy(g_ptable + g_pcount, ptn, sizeof(struct fastboot_ptentry));
1522 g_pcount++; 1522 g_pcount++;
1523 } 1523 }
1524 } 1524 }
1525 1525
1526 void fastboot_flash_dump_ptn(void) 1526 void fastboot_flash_dump_ptn(void)
1527 { 1527 {
1528 unsigned int n; 1528 unsigned int n;
1529 for (n = 0; n < g_pcount; n++) { 1529 for (n = 0; n < g_pcount; n++) {
1530 struct fastboot_ptentry *ptn = g_ptable + n; 1530 struct fastboot_ptentry *ptn = g_ptable + n;
1531 printf("idx %d, ptn %d name='%s' start=%d len=%d\n", 1531 printf("idx %d, ptn %d name='%s' start=%d len=%d\n",
1532 n, ptn->partition_index, ptn->name, ptn->start, ptn->length); 1532 n, ptn->partition_index, ptn->name, ptn->start, ptn->length);
1533 } 1533 }
1534 } 1534 }
1535 1535
1536 1536
1537 struct fastboot_ptentry *fastboot_flash_find_ptn(const char *name) 1537 struct fastboot_ptentry *fastboot_flash_find_ptn(const char *name)
1538 { 1538 {
1539 unsigned int n; 1539 unsigned int n;
1540 1540
1541 for (n = 0; n < g_pcount; n++) { 1541 for (n = 0; n < g_pcount; n++) {
1542 /* Make sure a substring is not accepted */ 1542 /* Make sure a substring is not accepted */
1543 if (strlen(name) == strlen(g_ptable[n].name)) { 1543 if (strlen(name) == strlen(g_ptable[n].name)) {
1544 if (0 == strcmp(g_ptable[n].name, name)) 1544 if (0 == strcmp(g_ptable[n].name, name))
1545 return g_ptable + n; 1545 return g_ptable + n;
1546 } 1546 }
1547 } 1547 }
1548 1548
1549 return 0; 1549 return 0;
1550 } 1550 }
1551 1551
1552 int fastboot_flash_find_index(const char *name) 1552 int fastboot_flash_find_index(const char *name)
1553 { 1553 {
1554 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(name); 1554 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(name);
1555 if (ptentry == NULL) { 1555 if (ptentry == NULL) {
1556 printf("cannot get the partion info for %s\n",name); 1556 printf("cannot get the partion info for %s\n",name);
1557 fastboot_flash_dump_ptn(); 1557 fastboot_flash_dump_ptn();
1558 return -1; 1558 return -1;
1559 } 1559 }
1560 return ptentry->partition_index; 1560 return ptentry->partition_index;
1561 } 1561 }
1562 1562
1563 struct fastboot_ptentry *fastboot_flash_get_ptn(unsigned int n) 1563 struct fastboot_ptentry *fastboot_flash_get_ptn(unsigned int n)
1564 { 1564 {
1565 if (n < g_pcount) 1565 if (n < g_pcount)
1566 return g_ptable + n; 1566 return g_ptable + n;
1567 else 1567 else
1568 return 0; 1568 return 0;
1569 } 1569 }
1570 1570
1571 unsigned int fastboot_flash_get_ptn_count(void) 1571 unsigned int fastboot_flash_get_ptn_count(void)
1572 { 1572 {
1573 return g_pcount; 1573 return g_pcount;
1574 } 1574 }
1575 1575
1576 #ifdef CONFIG_FSL_FASTBOOT 1576 #ifdef CONFIG_FSL_FASTBOOT
1577 void board_fastboot_setup(void) 1577 void board_fastboot_setup(void)
1578 { 1578 {
1579 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1579 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1580 static char boot_dev_part[32]; 1580 static char boot_dev_part[32];
1581 u32 dev_no; 1581 u32 dev_no;
1582 #endif 1582 #endif
1583 switch (get_boot_device()) { 1583 switch (get_boot_device()) {
1584 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1584 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1585 case SD1_BOOT: 1585 case SD1_BOOT:
1586 case SD2_BOOT: 1586 case SD2_BOOT:
1587 case SD3_BOOT: 1587 case SD3_BOOT:
1588 case SD4_BOOT: 1588 case SD4_BOOT:
1589 case MMC1_BOOT: 1589 case MMC1_BOOT:
1590 case MMC2_BOOT: 1590 case MMC2_BOOT:
1591 case MMC3_BOOT: 1591 case MMC3_BOOT:
1592 case MMC4_BOOT: 1592 case MMC4_BOOT:
1593 dev_no = mmc_get_env_dev(); 1593 dev_no = mmc_get_env_dev();
1594 sprintf(boot_dev_part,"mmc%d",dev_no); 1594 sprintf(boot_dev_part,"mmc%d",dev_no);
1595 if (!env_get("fastboot_dev")) 1595 if (!env_get("fastboot_dev"))
1596 env_set("fastboot_dev", boot_dev_part); 1596 env_set("fastboot_dev", boot_dev_part);
1597 sprintf(boot_dev_part, "boota mmc%d", dev_no); 1597 sprintf(boot_dev_part, "boota mmc%d", dev_no);
1598 if (!env_get("bootcmd")) 1598 if (!env_get("bootcmd"))
1599 env_set("bootcmd", boot_dev_part); 1599 env_set("bootcmd", boot_dev_part);
1600 break; 1600 break;
1601 case USB_BOOT: 1601 case USB_BOOT:
1602 printf("Detect USB boot. Will enter fastboot mode!\n"); 1602 printf("Detect USB boot. Will enter fastboot mode!\n");
1603 if (!env_get("bootcmd")) 1603 if (!env_get("bootcmd"))
1604 env_set("bootcmd", "fastboot 0"); 1604 env_set("bootcmd", "fastboot 0");
1605 break; 1605 break;
1606 #endif /*CONFIG_FASTBOOT_STORAGE_MMC*/ 1606 #endif /*CONFIG_FASTBOOT_STORAGE_MMC*/
1607 default: 1607 default:
1608 if (!env_get("bootcmd")) 1608 if (!env_get("bootcmd"))
1609 printf("unsupported boot devices\n"); 1609 printf("unsupported boot devices\n");
1610 break; 1610 break;
1611 } 1611 }
1612 1612
1613 /* add soc type into bootargs */ 1613 /* add soc type into bootargs */
1614 if (is_mx6dqp()) { 1614 if (is_mx6dqp()) {
1615 if (!env_get("soc_type")) 1615 if (!env_get("soc_type"))
1616 env_set("soc_type", "imx6qp"); 1616 env_set("soc_type", "imx6qp");
1617 } else if (is_mx6dq()) { 1617 } else if (is_mx6dq()) {
1618 if (!env_get("soc_type")) 1618 if (!env_get("soc_type"))
1619 env_set("soc_type", "imx6q"); 1619 env_set("soc_type", "imx6q");
1620 } else if (is_mx6sdl()) { 1620 } else if (is_mx6sdl()) {
1621 if (!env_get("soc_type")) 1621 if (!env_get("soc_type"))
1622 env_set("soc_type", "imx6dl"); 1622 env_set("soc_type", "imx6dl");
1623 } else if (is_mx6sx()) { 1623 } else if (is_mx6sx()) {
1624 if (!env_get("soc_type")) 1624 if (!env_get("soc_type"))
1625 env_set("soc_type", "imx6sx"); 1625 env_set("soc_type", "imx6sx");
1626 } else if (is_mx6sl()) { 1626 } else if (is_mx6sl()) {
1627 if (!env_get("soc_type")) 1627 if (!env_get("soc_type"))
1628 env_set("soc_type", "imx6sl"); 1628 env_set("soc_type", "imx6sl");
1629 } else if (is_mx6ul()) { 1629 } else if (is_mx6ul()) {
1630 if (!env_get("soc_type")) 1630 if (!env_get("soc_type"))
1631 env_set("soc_type", "imx6ul"); 1631 env_set("soc_type", "imx6ul");
1632 } else if (is_mx7()) { 1632 } else if (is_mx7()) {
1633 if (!env_get("soc_type")) 1633 if (!env_get("soc_type"))
1634 env_set("soc_type", "imx7d"); 1634 env_set("soc_type", "imx7d");
1635 } else if (is_mx7ulp()) { 1635 } else if (is_mx7ulp()) {
1636 if (!env_get("soc_type")) 1636 if (!env_get("soc_type"))
1637 env_set("soc_type", "imx7ulp"); 1637 env_set("soc_type", "imx7ulp");
1638 } else if (is_imx8qm()) { 1638 } else if (is_imx8qm()) {
1639 if (!env_get("soc_type")) 1639 if (!env_get("soc_type"))
1640 env_set("soc_type", "imx8qm"); 1640 env_set("soc_type", "imx8qm");
1641 } else if (is_imx8qxp()) { 1641 } else if (is_imx8qxp()) {
1642 if (!env_get("soc_type")) 1642 if (!env_get("soc_type"))
1643 env_set("soc_type", "imx8qxp"); 1643 env_set("soc_type", "imx8qxp");
1644 } else if (is_imx8mq()) { 1644 } else if (is_imx8mq()) {
1645 if (!env_get("soc_type")) 1645 if (!env_get("soc_type"))
1646 env_set("soc_type", "imx8mq"); 1646 env_set("soc_type", "imx8mq");
1647 } else if (is_imx8mm()) { 1647 } else if (is_imx8mm()) {
1648 if (!env_get("soc_type")) 1648 if (!env_get("soc_type"))
1649 env_set("soc_type", "imx8mm"); 1649 env_set("soc_type", "imx8mm");
1650 } 1650 }
1651 } 1651 }
1652 1652
1653 #ifdef CONFIG_ANDROID_RECOVERY 1653 #ifdef CONFIG_ANDROID_RECOVERY
1654 void board_recovery_setup(void) 1654 void board_recovery_setup(void)
1655 { 1655 {
1656 /* boot from current mmc with avb verify */ 1656 /* boot from current mmc with avb verify */
1657 #ifdef CONFIG_AVB_SUPPORT 1657 #ifdef CONFIG_AVB_SUPPORT
1658 if (!env_get("bootcmd_android_recovery")) 1658 if (!env_get("bootcmd_android_recovery"))
1659 env_set("bootcmd_android_recovery", "boota recovery"); 1659 env_set("bootcmd_android_recovery", "boota recovery");
1660 #else 1660 #else
1661 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1661 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1662 static char boot_dev_part[32]; 1662 static char boot_dev_part[32];
1663 u32 dev_no; 1663 u32 dev_no;
1664 #endif 1664 #endif
1665 int bootdev = get_boot_device(); 1665 int bootdev = get_boot_device();
1666 switch (bootdev) { 1666 switch (bootdev) {
1667 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1667 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1668 case SD1_BOOT: 1668 case SD1_BOOT:
1669 case SD2_BOOT: 1669 case SD2_BOOT:
1670 case SD3_BOOT: 1670 case SD3_BOOT:
1671 case SD4_BOOT: 1671 case SD4_BOOT:
1672 case MMC1_BOOT: 1672 case MMC1_BOOT:
1673 case MMC2_BOOT: 1673 case MMC2_BOOT:
1674 case MMC3_BOOT: 1674 case MMC3_BOOT:
1675 case MMC4_BOOT: 1675 case MMC4_BOOT:
1676 dev_no = mmc_get_env_dev(); 1676 dev_no = mmc_get_env_dev();
1677 sprintf(boot_dev_part,"boota mmc%d recovery",dev_no); 1677 sprintf(boot_dev_part,"boota mmc%d recovery",dev_no);
1678 if (!env_get("bootcmd_android_recovery")) 1678 if (!env_get("bootcmd_android_recovery"))
1679 env_set("bootcmd_android_recovery", boot_dev_part); 1679 env_set("bootcmd_android_recovery", boot_dev_part);
1680 break; 1680 break;
1681 #endif /*CONFIG_FASTBOOT_STORAGE_MMC*/ 1681 #endif /*CONFIG_FASTBOOT_STORAGE_MMC*/
1682 default: 1682 default:
1683 printf("Unsupported bootup device for recovery: dev: %d\n", 1683 printf("Unsupported bootup device for recovery: dev: %d\n",
1684 bootdev); 1684 bootdev);
1685 return; 1685 return;
1686 } 1686 }
1687 #endif /* CONFIG_AVB_SUPPORT */ 1687 #endif /* CONFIG_AVB_SUPPORT */
1688 printf("setup env for recovery..\n"); 1688 printf("setup env for recovery..\n");
1689 env_set("bootcmd", env_get("bootcmd_android_recovery")); 1689 env_set("bootcmd", env_get("bootcmd_android_recovery"));
1690 } 1690 }
1691 #endif /*CONFIG_ANDROID_RECOVERY*/ 1691 #endif /*CONFIG_ANDROID_RECOVERY*/
1692 #endif /*CONFIG_FSL_FASTBOOT*/ 1692 #endif /*CONFIG_FSL_FASTBOOT*/
1693 1693
1694 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC) 1694 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC)
1695 static AvbABOps fsl_avb_ab_ops = { 1695 static AvbABOps fsl_avb_ab_ops = {
1696 .read_ab_metadata = fsl_read_ab_metadata, 1696 .read_ab_metadata = fsl_read_ab_metadata,
1697 .write_ab_metadata = fsl_write_ab_metadata, 1697 .write_ab_metadata = fsl_write_ab_metadata,
1698 .ops = NULL 1698 .ops = NULL
1699 }; 1699 };
1700 #ifdef CONFIG_AVB_ATX 1700 #ifdef CONFIG_AVB_ATX
1701 static AvbAtxOps fsl_avb_atx_ops = { 1701 static AvbAtxOps fsl_avb_atx_ops = {
1702 .ops = NULL, 1702 .ops = NULL,
1703 .read_permanent_attributes = fsl_read_permanent_attributes, 1703 .read_permanent_attributes = fsl_read_permanent_attributes,
1704 .read_permanent_attributes_hash = fsl_read_permanent_attributes_hash, 1704 .read_permanent_attributes_hash = fsl_read_permanent_attributes_hash,
1705 #ifdef CONFIG_IMX_TRUSTY_OS 1705 #ifdef CONFIG_IMX_TRUSTY_OS
1706 .set_key_version = fsl_write_rollback_index_rpmb, 1706 .set_key_version = fsl_write_rollback_index_rpmb,
1707 #else 1707 #else
1708 .set_key_version = fsl_set_key_version, 1708 .set_key_version = fsl_set_key_version,
1709 #endif 1709 #endif
1710 .get_random = fsl_get_random 1710 .get_random = fsl_get_random
1711 }; 1711 };
1712 #endif 1712 #endif
1713 static AvbOps fsl_avb_ops = { 1713 static AvbOps fsl_avb_ops = {
1714 .ab_ops = &fsl_avb_ab_ops, 1714 .ab_ops = &fsl_avb_ab_ops,
1715 #ifdef CONFIG_AVB_ATX 1715 #ifdef CONFIG_AVB_ATX
1716 .atx_ops = &fsl_avb_atx_ops, 1716 .atx_ops = &fsl_avb_atx_ops,
1717 #endif 1717 #endif
1718 .read_from_partition = fsl_read_from_partition_multi, 1718 .read_from_partition = fsl_read_from_partition_multi,
1719 .write_to_partition = fsl_write_to_partition, 1719 .write_to_partition = fsl_write_to_partition,
1720 #ifdef CONFIG_AVB_ATX 1720 #ifdef CONFIG_AVB_ATX
1721 .validate_vbmeta_public_key = avb_atx_validate_vbmeta_public_key, 1721 .validate_vbmeta_public_key = avb_atx_validate_vbmeta_public_key,
1722 #else 1722 #else
1723 .validate_vbmeta_public_key = fsl_validate_vbmeta_public_key_rpmb, 1723 .validate_vbmeta_public_key = fsl_validate_vbmeta_public_key_rpmb,
1724 #endif 1724 #endif
1725 .read_rollback_index = fsl_read_rollback_index_rpmb, 1725 .read_rollback_index = fsl_read_rollback_index_rpmb,
1726 .write_rollback_index = fsl_write_rollback_index_rpmb, 1726 .write_rollback_index = fsl_write_rollback_index_rpmb,
1727 .read_is_device_unlocked = fsl_read_is_device_unlocked, 1727 .read_is_device_unlocked = fsl_read_is_device_unlocked,
1728 .get_unique_guid_for_partition = fsl_get_unique_guid_for_partition, 1728 .get_unique_guid_for_partition = fsl_get_unique_guid_for_partition,
1729 .get_size_of_partition = fsl_get_size_of_partition 1729 .get_size_of_partition = fsl_get_size_of_partition
1730 }; 1730 };
1731 #endif 1731 #endif
1732 1732
1733 #ifdef CONFIG_IMX_TRUSTY_OS 1733 #ifdef CONFIG_IMX_TRUSTY_OS
1734 #ifdef CONFIG_ARM64 1734 #ifdef CONFIG_ARM64
1735 void tee_setup(void) 1735 void tee_setup(void)
1736 { 1736 {
1737 trusty_ipc_init(); 1737 trusty_ipc_init();
1738 } 1738 }
1739 1739
1740 #else 1740 #else
1741 extern bool tos_flashed; 1741 extern bool tos_flashed;
1742 1742
1743 void tee_setup(void) 1743 void tee_setup(void)
1744 { 1744 {
1745 /* load tee from boot1 of eMMC. */ 1745 /* load tee from boot1 of eMMC. */
1746 int mmcc = mmc_get_env_dev(); 1746 int mmcc = mmc_get_env_dev();
1747 struct blk_desc *dev_desc = NULL; 1747 struct blk_desc *dev_desc = NULL;
1748 1748
1749 struct mmc *mmc; 1749 struct mmc *mmc;
1750 mmc = find_mmc_device(mmcc); 1750 mmc = find_mmc_device(mmcc);
1751 if (!mmc) { 1751 if (!mmc) {
1752 printf("boota: cannot find '%d' mmc device\n", mmcc); 1752 printf("boota: cannot find '%d' mmc device\n", mmcc);
1753 goto fail; 1753 goto fail;
1754 } 1754 }
1755 1755
1756 dev_desc = blk_get_dev("mmc", mmcc); 1756 dev_desc = blk_get_dev("mmc", mmcc);
1757 if (NULL == dev_desc) { 1757 if (NULL == dev_desc) {
1758 printf("** Block device MMC %d not supported\n", mmcc); 1758 printf("** Block device MMC %d not supported\n", mmcc);
1759 goto fail; 1759 goto fail;
1760 } 1760 }
1761 1761
1762 /* below was i.MX mmc operation code */ 1762 /* below was i.MX mmc operation code */
1763 if (mmc_init(mmc)) { 1763 if (mmc_init(mmc)) {
1764 printf("mmc%d init failed\n", mmcc); 1764 printf("mmc%d init failed\n", mmcc);
1765 goto fail; 1765 goto fail;
1766 } 1766 }
1767 1767
1768 struct fastboot_ptentry *tee_pte; 1768 struct fastboot_ptentry *tee_pte;
1769 char *tee_ptn = FASTBOOT_PARTITION_TEE; 1769 char *tee_ptn = FASTBOOT_PARTITION_TEE;
1770 tee_pte = fastboot_flash_find_ptn(tee_ptn); 1770 tee_pte = fastboot_flash_find_ptn(tee_ptn);
1771 mmc_switch_part(mmc, TEE_HWPARTITION_ID); 1771 mmc_switch_part(mmc, TEE_HWPARTITION_ID);
1772 if (!tee_pte) { 1772 if (!tee_pte) {
1773 printf("boota: cannot find tee partition!\n"); 1773 printf("boota: cannot find tee partition!\n");
1774 fastboot_flash_dump_ptn(); 1774 fastboot_flash_dump_ptn();
1775 } 1775 }
1776 1776
1777 if (blk_dread(dev_desc, tee_pte->start, 1777 if (blk_dread(dev_desc, tee_pte->start,
1778 tee_pte->length, (void *)TRUSTY_OS_ENTRY) < 0) { 1778 tee_pte->length, (void *)TRUSTY_OS_ENTRY) < 0) {
1779 printf("Failed to load tee."); 1779 printf("Failed to load tee.");
1780 } 1780 }
1781 mmc_switch_part(mmc, FASTBOOT_MMC_USER_PARTITION_ID); 1781 mmc_switch_part(mmc, FASTBOOT_MMC_USER_PARTITION_ID);
1782 1782
1783 tos_flashed = false; 1783 tos_flashed = false;
1784 if(!valid_tos()) { 1784 if(!valid_tos()) {
1785 printf("TOS not flashed! Will enter TOS recovery mode. Everything will be wiped!\n"); 1785 printf("TOS not flashed! Will enter TOS recovery mode. Everything will be wiped!\n");
1786 fastboot_wipe_all(); 1786 fastboot_wipe_all();
1787 run_command("fastboot 0", 0); 1787 run_command("fastboot 0", 0);
1788 goto fail; 1788 goto fail;
1789 } 1789 }
1790 #ifdef NON_SECURE_FASTBOOT 1790 #ifdef NON_SECURE_FASTBOOT
1791 armv7_init_nonsec(); 1791 armv7_init_nonsec();
1792 trusty_os_init(); 1792 trusty_os_init();
1793 trusty_ipc_init(); 1793 trusty_ipc_init();
1794 #endif 1794 #endif
1795 1795
1796 fail: 1796 fail:
1797 return; 1797 return;
1798 1798
1799 } 1799 }
1800 #endif /* CONFIG_ARM64 */ 1800 #endif /* CONFIG_ARM64 */
1801 #endif /* CONFIG_IMX_TRUSTY_OS */ 1801 #endif /* CONFIG_IMX_TRUSTY_OS */
1802 1802
1803 void fastboot_setup(void) 1803 void fastboot_setup(void)
1804 { 1804 {
1805 int sw, ret; 1805 int sw, ret;
1806 #ifdef CONFIG_USB_GADGET 1806 #ifdef CONFIG_USB_GADGET
1807 struct tag_serialnr serialnr; 1807 struct tag_serialnr serialnr;
1808 char serial[17]; 1808 char serial[17];
1809 1809
1810 get_board_serial(&serialnr); 1810 get_board_serial(&serialnr);
1811 sprintf(serial, "%08x%08x", serialnr.high, serialnr.low); 1811 sprintf(serial, "%08x%08x", serialnr.high, serialnr.low);
1812 g_dnl_set_serialnumber(serial); 1812 g_dnl_set_serialnumber(serial);
1813 #endif 1813 #endif
1814 /*execute board relevant initilizations for preparing fastboot */ 1814 /*execute board relevant initilizations for preparing fastboot */
1815 board_fastboot_setup(); 1815 board_fastboot_setup();
1816 1816
1817 /*get the fastboot dev*/ 1817 /*get the fastboot dev*/
1818 ret = _fastboot_setup_dev(&sw); 1818 ret = _fastboot_setup_dev(&sw);
1819 1819
1820 /*load partitions information for the fastboot dev*/ 1820 /*load partitions information for the fastboot dev*/
1821 if (!ret && sw) 1821 if (!ret && sw)
1822 _fastboot_load_partitions(); 1822 _fastboot_load_partitions();
1823 1823
1824 parameters_setup(); 1824 parameters_setup();
1825 #ifdef CONFIG_AVB_SUPPORT 1825 #ifdef CONFIG_AVB_SUPPORT
1826 fsl_avb_ab_ops.ops = &fsl_avb_ops; 1826 fsl_avb_ab_ops.ops = &fsl_avb_ops;
1827 #ifdef CONFIG_AVB_ATX 1827 #ifdef CONFIG_AVB_ATX
1828 fsl_avb_atx_ops.ops = &fsl_avb_ops; 1828 fsl_avb_atx_ops.ops = &fsl_avb_ops;
1829 #endif 1829 #endif
1830 #endif 1830 #endif
1831 } 1831 }
1832 1832
1833 /* Write the bcb with fastboot bootloader commands */ 1833 /* Write the bcb with fastboot bootloader commands */
1834 static void enable_fastboot_command(void) 1834 static void enable_fastboot_command(void)
1835 { 1835 {
1836 #ifdef CONFIG_BCB_SUPPORT 1836 #ifdef CONFIG_BCB_SUPPORT
1837 char fastboot_command[32] = {0}; 1837 char fastboot_command[32] = {0};
1838 strncpy(fastboot_command, FASTBOOT_BCB_CMD, 31); 1838 strncpy(fastboot_command, FASTBOOT_BCB_CMD, 31);
1839 bcb_write_command(fastboot_command); 1839 bcb_write_command(fastboot_command);
1840 #endif 1840 #endif
1841 } 1841 }
1842 1842
1843 /* Get the Boot mode from BCB cmd or Key pressed */ 1843 /* Get the Boot mode from BCB cmd or Key pressed */
1844 static FbBootMode fastboot_get_bootmode(void) 1844 static FbBootMode fastboot_get_bootmode(void)
1845 { 1845 {
1846 int boot_mode = BOOTMODE_NORMAL; 1846 int boot_mode = BOOTMODE_NORMAL;
1847 #ifdef CONFIG_ANDROID_RECOVERY 1847 #ifdef CONFIG_ANDROID_RECOVERY
1848 if(is_recovery_key_pressing()) { 1848 if(is_recovery_key_pressing()) {
1849 boot_mode = BOOTMODE_RECOVERY_KEY_PRESSED; 1849 boot_mode = BOOTMODE_RECOVERY_KEY_PRESSED;
1850 return boot_mode; 1850 return boot_mode;
1851 } 1851 }
1852 #endif 1852 #endif
1853 #ifdef CONFIG_BCB_SUPPORT 1853 #ifdef CONFIG_BCB_SUPPORT
1854 int ret = 0; 1854 int ret = 0;
1855 char command[32]; 1855 char command[32];
1856 ret = bcb_read_command(command); 1856 ret = bcb_read_command(command);
1857 if (ret < 0) { 1857 if (ret < 0) {
1858 printf("read command failed\n"); 1858 printf("read command failed\n");
1859 return boot_mode; 1859 return boot_mode;
1860 } 1860 }
1861 if (!strcmp(command, FASTBOOT_BCB_CMD)) { 1861 if (!strcmp(command, FASTBOOT_BCB_CMD)) {
1862 boot_mode = BOOTMODE_FASTBOOT_BCB_CMD; 1862 boot_mode = BOOTMODE_FASTBOOT_BCB_CMD;
1863 } 1863 }
1864 #ifdef CONFIG_ANDROID_RECOVERY 1864 #ifdef CONFIG_ANDROID_RECOVERY
1865 else if (!strcmp(command, RECOVERY_BCB_CMD)) { 1865 else if (!strcmp(command, RECOVERY_BCB_CMD)) {
1866 boot_mode = BOOTMODE_RECOVERY_BCB_CMD; 1866 boot_mode = BOOTMODE_RECOVERY_BCB_CMD;
1867 } 1867 }
1868 #endif 1868 #endif
1869 1869
1870 /* Clean the mode once its read out, 1870 /* Clean the mode once its read out,
1871 no matter what in the mode string */ 1871 no matter what in the mode string */
1872 memset(command, 0, 32); 1872 memset(command, 0, 32);
1873 bcb_write_command(command); 1873 bcb_write_command(command);
1874 #endif 1874 #endif
1875 return boot_mode; 1875 return boot_mode;
1876 } 1876 }
1877 1877
1878 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT 1878 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT
1879 /* Setup booargs for taking the system parition as ramdisk */ 1879 /* Setup booargs for taking the system parition as ramdisk */
1880 static void fastboot_setup_system_boot_args(const char *slot, bool append_root) 1880 static void fastboot_setup_system_boot_args(const char *slot, bool append_root)
1881 { 1881 {
1882 const char *system_part_name = NULL; 1882 const char *system_part_name = NULL;
1883 if(slot == NULL) 1883 if(slot == NULL)
1884 return; 1884 return;
1885 if(!strncmp(slot, "_a", strlen("_a")) || !strncmp(slot, "boot_a", strlen("boot_a"))) { 1885 if(!strncmp(slot, "_a", strlen("_a")) || !strncmp(slot, "boot_a", strlen("boot_a"))) {
1886 system_part_name = FASTBOOT_PARTITION_SYSTEM_A; 1886 system_part_name = FASTBOOT_PARTITION_SYSTEM_A;
1887 } 1887 }
1888 else if(!strncmp(slot, "_b", strlen("_b")) || !strncmp(slot, "boot_b", strlen("boot_b"))) { 1888 else if(!strncmp(slot, "_b", strlen("_b")) || !strncmp(slot, "boot_b", strlen("boot_b"))) {
1889 system_part_name = FASTBOOT_PARTITION_SYSTEM_B; 1889 system_part_name = FASTBOOT_PARTITION_SYSTEM_B;
1890 } else { 1890 } else {
1891 printf("slot invalid!\n"); 1891 printf("slot invalid!\n");
1892 return; 1892 return;
1893 } 1893 }
1894 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(system_part_name); 1894 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(system_part_name);
1895 if(ptentry != NULL) { 1895 if(ptentry != NULL) {
1896 char bootargs_3rd[ANDR_BOOT_ARGS_SIZE]; 1896 char bootargs_3rd[ANDR_BOOT_ARGS_SIZE];
1897 #if defined(CONFIG_FASTBOOT_STORAGE_MMC) 1897 #if defined(CONFIG_FASTBOOT_STORAGE_MMC)
1898 if (append_root) { 1898 if (append_root) {
1899 u32 dev_no = mmc_map_to_kernel_blk(mmc_get_env_dev()); 1899 u32 dev_no = mmc_map_to_kernel_blk(mmc_get_env_dev());
1900 sprintf(bootargs_3rd, "skip_initramfs root=/dev/mmcblk%dp%d", 1900 sprintf(bootargs_3rd, "skip_initramfs root=/dev/mmcblk%dp%d",
1901 dev_no, 1901 dev_no,
1902 ptentry->partition_index); 1902 ptentry->partition_index);
1903 } else { 1903 } else {
1904 sprintf(bootargs_3rd, "skip_initramfs"); 1904 sprintf(bootargs_3rd, "skip_initramfs");
1905 } 1905 }
1906 strcat(bootargs_3rd, " rootwait"); 1906 strcat(bootargs_3rd, " rootwait");
1907 env_set("bootargs_3rd", bootargs_3rd); 1907 env_set("bootargs_3rd", bootargs_3rd);
1908 #endif 1908 #endif
1909 } else { 1909 } else {
1910 printf("Can't find partition: %s\n", system_part_name); 1910 printf("Can't find partition: %s\n", system_part_name);
1911 fastboot_flash_dump_ptn(); 1911 fastboot_flash_dump_ptn();
1912 } 1912 }
1913 } 1913 }
1914 #endif 1914 #endif
1915 /* export to lib_arm/board.c */ 1915 /* export to lib_arm/board.c */
1916 void fastboot_run_bootmode(void) 1916 void fastboot_run_bootmode(void)
1917 { 1917 {
1918 FbBootMode boot_mode = fastboot_get_bootmode(); 1918 FbBootMode boot_mode = fastboot_get_bootmode();
1919 switch(boot_mode){ 1919 switch(boot_mode){
1920 case BOOTMODE_FASTBOOT_BCB_CMD: 1920 case BOOTMODE_FASTBOOT_BCB_CMD:
1921 /* Make the boot into fastboot mode*/ 1921 /* Make the boot into fastboot mode*/
1922 puts("Fastboot: Got bootloader commands!\n"); 1922 puts("Fastboot: Got bootloader commands!\n");
1923 run_command("fastboot 0", 0); 1923 run_command("fastboot 0", 0);
1924 break; 1924 break;
1925 #ifdef CONFIG_ANDROID_RECOVERY 1925 #ifdef CONFIG_ANDROID_RECOVERY
1926 case BOOTMODE_RECOVERY_BCB_CMD: 1926 case BOOTMODE_RECOVERY_BCB_CMD:
1927 case BOOTMODE_RECOVERY_KEY_PRESSED: 1927 case BOOTMODE_RECOVERY_KEY_PRESSED:
1928 /* Make the boot into recovery mode */ 1928 /* Make the boot into recovery mode */
1929 puts("Fastboot: Got Recovery key pressing or recovery commands!\n"); 1929 puts("Fastboot: Got Recovery key pressing or recovery commands!\n");
1930 board_recovery_setup(); 1930 board_recovery_setup();
1931 break; 1931 break;
1932 #endif 1932 #endif
1933 default: 1933 default:
1934 /* skip special mode boot*/ 1934 /* skip special mode boot*/
1935 puts("Fastboot: Normal\n"); 1935 puts("Fastboot: Normal\n");
1936 break; 1936 break;
1937 } 1937 }
1938 } 1938 }
1939 1939
1940 #ifdef CONFIG_CMD_BOOTA 1940 #ifdef CONFIG_CMD_BOOTA
1941 /* Section for Android bootimage format support 1941 /* Section for Android bootimage format support
1942 * Refer: 1942 * Refer:
1943 * http://android.git.kernel.org/?p=platform/system/core.git;a=blob; 1943 * http://android.git.kernel.org/?p=platform/system/core.git;a=blob;
1944 * f=mkbootimg/bootimg.h 1944 * f=mkbootimg/bootimg.h
1945 */ 1945 */
1946 1946
1947 void 1947 void
1948 bootimg_print_image_hdr(struct andr_img_hdr *hdr) 1948 bootimg_print_image_hdr(struct andr_img_hdr *hdr)
1949 { 1949 {
1950 #ifdef DEBUG 1950 #ifdef DEBUG
1951 int i; 1951 int i;
1952 printf(" Image magic: %s\n", hdr->magic); 1952 printf(" Image magic: %s\n", hdr->magic);
1953 1953
1954 printf(" kernel_size: 0x%x\n", hdr->kernel_size); 1954 printf(" kernel_size: 0x%x\n", hdr->kernel_size);
1955 printf(" kernel_addr: 0x%x\n", hdr->kernel_addr); 1955 printf(" kernel_addr: 0x%x\n", hdr->kernel_addr);
1956 1956
1957 printf(" rdisk_size: 0x%x\n", hdr->ramdisk_size); 1957 printf(" rdisk_size: 0x%x\n", hdr->ramdisk_size);
1958 printf(" rdisk_addr: 0x%x\n", hdr->ramdisk_addr); 1958 printf(" rdisk_addr: 0x%x\n", hdr->ramdisk_addr);
1959 1959
1960 printf(" second_size: 0x%x\n", hdr->second_size); 1960 printf(" second_size: 0x%x\n", hdr->second_size);
1961 printf(" second_addr: 0x%x\n", hdr->second_addr); 1961 printf(" second_addr: 0x%x\n", hdr->second_addr);
1962 1962
1963 printf(" tags_addr: 0x%x\n", hdr->tags_addr); 1963 printf(" tags_addr: 0x%x\n", hdr->tags_addr);
1964 printf(" page_size: 0x%x\n", hdr->page_size); 1964 printf(" page_size: 0x%x\n", hdr->page_size);
1965 1965
1966 printf(" name: %s\n", hdr->name); 1966 printf(" name: %s\n", hdr->name);
1967 printf(" cmdline: %s\n", hdr->cmdline); 1967 printf(" cmdline: %s\n", hdr->cmdline);
1968 1968
1969 for (i = 0; i < 8; i++) 1969 for (i = 0; i < 8; i++)
1970 printf(" id[%d]: 0x%x\n", i, hdr->id[i]); 1970 printf(" id[%d]: 0x%x\n", i, hdr->id[i]);
1971 #endif 1971 #endif
1972 } 1972 }
1973 1973
1974 #if !defined(CONFIG_AVB_SUPPORT) || !defined(CONFIG_MMC) 1974 #if !defined(CONFIG_AVB_SUPPORT) || !defined(CONFIG_MMC)
1975 static struct andr_img_hdr boothdr __aligned(ARCH_DMA_MINALIGN); 1975 static struct andr_img_hdr boothdr __aligned(ARCH_DMA_MINALIGN);
1976 #endif 1976 #endif
1977 1977
1978 #ifdef CONFIG_IMX_TRUSTY_OS 1978 #ifdef CONFIG_IMX_TRUSTY_OS
1979 #ifdef CONFIG_DUAL_BOOTLOADER 1979 #ifdef CONFIG_DUAL_BOOTLOADER
1980 static int sha256_concatenation(uint8_t *hash_buf, uint8_t *vbh, uint8_t *image_hash) 1980 static int sha256_concatenation(uint8_t *hash_buf, uint8_t *vbh, uint8_t *image_hash)
1981 { 1981 {
1982 if ((hash_buf == NULL) || (vbh == NULL) || (image_hash == NULL)) { 1982 if ((hash_buf == NULL) || (vbh == NULL) || (image_hash == NULL)) {
1983 printf("sha256_concatenation: null buffer found!\n"); 1983 printf("sha256_concatenation: null buffer found!\n");
1984 return -1; 1984 return -1;
1985 } 1985 }
1986 1986
1987 memcpy(hash_buf, vbh, AVB_SHA256_DIGEST_SIZE); 1987 memcpy(hash_buf, vbh, AVB_SHA256_DIGEST_SIZE);
1988 memcpy(hash_buf + AVB_SHA256_DIGEST_SIZE, 1988 memcpy(hash_buf + AVB_SHA256_DIGEST_SIZE,
1989 image_hash, AVB_SHA256_DIGEST_SIZE); 1989 image_hash, AVB_SHA256_DIGEST_SIZE);
1990 sha256_csum_wd((unsigned char *)hash_buf, 2 * AVB_SHA256_DIGEST_SIZE, 1990 sha256_csum_wd((unsigned char *)hash_buf, 2 * AVB_SHA256_DIGEST_SIZE,
1991 (unsigned char *)vbh, CHUNKSZ_SHA256); 1991 (unsigned char *)vbh, CHUNKSZ_SHA256);
1992 1992
1993 return 0; 1993 return 0;
1994 } 1994 }
1995 1995
1996 /* Since we use fit format to organize the atf, tee, u-boot and u-boot dtb, 1996 /* Since we use fit format to organize the atf, tee, u-boot and u-boot dtb,
1997 * so calculate the hash of fit is enough. 1997 * so calculate the hash of fit is enough.
1998 */ 1998 */
1999 static int vbh_bootloader(uint8_t *image_hash) 1999 static int vbh_bootloader(uint8_t *image_hash)
2000 { 2000 {
2001 char* slot_suffixes[2] = {"_a", "_b"}; 2001 char* slot_suffixes[2] = {"_a", "_b"};
2002 char partition_name[20]; 2002 char partition_name[20];
2003 AvbABData ab_data; 2003 AvbABData ab_data;
2004 uint8_t *image_buf = NULL; 2004 uint8_t *image_buf = NULL;
2005 uint32_t image_size; 2005 uint32_t image_size;
2006 size_t image_num_read; 2006 size_t image_num_read;
2007 int target_slot; 2007 int target_slot;
2008 int ret = 0; 2008 int ret = 0;
2009 2009
2010 /* Load A/B metadata and decide which slot we are going to load */ 2010 /* Load A/B metadata and decide which slot we are going to load */
2011 if (fsl_avb_ab_ops.read_ab_metadata(&fsl_avb_ab_ops, &ab_data) != 2011 if (fsl_avb_ab_ops.read_ab_metadata(&fsl_avb_ab_ops, &ab_data) !=
2012 AVB_IO_RESULT_OK) { 2012 AVB_IO_RESULT_OK) {
2013 ret = -1; 2013 ret = -1;
2014 goto fail ; 2014 goto fail ;
2015 } 2015 }
2016 target_slot = get_curr_slot(&ab_data); 2016 target_slot = get_curr_slot(&ab_data);
2017 sprintf(partition_name, "bootloader%s", slot_suffixes[target_slot]); 2017 sprintf(partition_name, "bootloader%s", slot_suffixes[target_slot]);
2018 2018
2019 /* Read image header to find the image size */ 2019 /* Read image header to find the image size */
2020 image_buf = (uint8_t *)malloc(MMC_SATA_BLOCK_SIZE); 2020 image_buf = (uint8_t *)malloc(MMC_SATA_BLOCK_SIZE);
2021 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name, 2021 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name,
2022 0, MMC_SATA_BLOCK_SIZE, 2022 0, MMC_SATA_BLOCK_SIZE,
2023 image_buf, &image_num_read)) { 2023 image_buf, &image_num_read)) {
2024 printf("bootloader image load error!\n"); 2024 printf("bootloader image load error!\n");
2025 ret = -1; 2025 ret = -1;
2026 goto fail; 2026 goto fail;
2027 } 2027 }
2028 image_size = fdt_totalsize((struct image_header *)image_buf); 2028 image_size = fdt_totalsize((struct image_header *)image_buf);
2029 image_size = (image_size + 3) & ~3; 2029 image_size = (image_size + 3) & ~3;
2030 free(image_buf); 2030 free(image_buf);
2031 2031
2032 /* Load full fit image */ 2032 /* Load full fit image */
2033 image_buf = (uint8_t *)malloc(image_size); 2033 image_buf = (uint8_t *)malloc(image_size);
2034 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name, 2034 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name,
2035 0, image_size, 2035 0, image_size,
2036 image_buf, &image_num_read)) { 2036 image_buf, &image_num_read)) {
2037 printf("bootloader image load error!\n"); 2037 printf("bootloader image load error!\n");
2038 ret = -1; 2038 ret = -1;
2039 goto fail; 2039 goto fail;
2040 } 2040 }
2041 /* Calculate hash */ 2041 /* Calculate hash */
2042 sha256_csum_wd((unsigned char *)image_buf, image_size, 2042 sha256_csum_wd((unsigned char *)image_buf, image_size,
2043 (unsigned char *)image_hash, CHUNKSZ_SHA256); 2043 (unsigned char *)image_hash, CHUNKSZ_SHA256);
2044 2044
2045 fail: 2045 fail:
2046 if (image_buf != NULL) 2046 if (image_buf != NULL)
2047 free(image_buf); 2047 free(image_buf);
2048 return ret; 2048 return ret;
2049 } 2049 }
2050 2050
2051 int vbh_calculate(uint8_t *vbh, AvbSlotVerifyData *avb_out_data) 2051 int vbh_calculate(uint8_t *vbh, AvbSlotVerifyData *avb_out_data)
2052 { 2052 {
2053 uint8_t image_hash[AVB_SHA256_DIGEST_SIZE]; 2053 uint8_t image_hash[AVB_SHA256_DIGEST_SIZE];
2054 uint8_t hash_buf[2 * AVB_SHA256_DIGEST_SIZE]; 2054 uint8_t hash_buf[2 * AVB_SHA256_DIGEST_SIZE];
2055 uint8_t* image_buf = NULL; 2055 uint8_t* image_buf = NULL;
2056 uint32_t image_size; 2056 uint32_t image_size;
2057 size_t image_num_read; 2057 size_t image_num_read;
2058 int ret = 0; 2058 int ret = 0;
2059 2059
2060 if (vbh == NULL) 2060 if (vbh == NULL)
2061 return -1; 2061 return -1;
2062 2062
2063 /* Initial VBH (VBH0) should be 32 bytes 0 */ 2063 /* Initial VBH (VBH0) should be 32 bytes 0 */
2064 memset(vbh, 0, AVB_SHA256_DIGEST_SIZE); 2064 memset(vbh, 0, AVB_SHA256_DIGEST_SIZE);
2065 /* Load and calculate the sha256 hash of spl.bin */ 2065 /* Load and calculate the sha256 hash of spl.bin */
2066 image_size = (ANDROID_SPL_SIZE + MMC_SATA_BLOCK_SIZE -1) / 2066 image_size = (ANDROID_SPL_SIZE + MMC_SATA_BLOCK_SIZE -1) /
2067 MMC_SATA_BLOCK_SIZE; 2067 MMC_SATA_BLOCK_SIZE;
2068 image_buf = (uint8_t *)malloc(image_size); 2068 image_buf = (uint8_t *)malloc(image_size);
2069 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, 2069 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops,
2070 FASTBOOT_PARTITION_BOOTLOADER, 2070 FASTBOOT_PARTITION_BOOTLOADER,
2071 0, image_size, 2071 0, image_size,
2072 image_buf, &image_num_read)) { 2072 image_buf, &image_num_read)) {
2073 printf("spl image load error!\n"); 2073 printf("spl image load error!\n");
2074 ret = -1; 2074 ret = -1;
2075 goto fail; 2075 goto fail;
2076 } 2076 }
2077 sha256_csum_wd((unsigned char *)image_buf, image_size, 2077 sha256_csum_wd((unsigned char *)image_buf, image_size,
2078 (unsigned char *)image_hash, CHUNKSZ_SHA256); 2078 (unsigned char *)image_hash, CHUNKSZ_SHA256);
2079 /* Calculate VBH1 */ 2079 /* Calculate VBH1 */
2080 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 2080 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
2081 ret = -1; 2081 ret = -1;
2082 goto fail; 2082 goto fail;
2083 } 2083 }
2084 free(image_buf); 2084 free(image_buf);
2085 2085
2086 /* Load and calculate hash of bootloader.img */ 2086 /* Load and calculate hash of bootloader.img */
2087 if (vbh_bootloader(image_hash)) { 2087 if (vbh_bootloader(image_hash)) {
2088 ret = -1; 2088 ret = -1;
2089 goto fail; 2089 goto fail;
2090 } 2090 }
2091 /* Calculate VBH2 */ 2091 /* Calculate VBH2 */
2092 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 2092 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
2093 ret = -1; 2093 ret = -1;
2094 goto fail; 2094 goto fail;
2095 } 2095 }
2096 2096
2097 /* Calculate the hash of vbmeta.img */ 2097 /* Calculate the hash of vbmeta.img */
2098 avb_slot_verify_data_calculate_vbmeta_digest(avb_out_data, 2098 avb_slot_verify_data_calculate_vbmeta_digest(avb_out_data,
2099 AVB_DIGEST_TYPE_SHA256, 2099 AVB_DIGEST_TYPE_SHA256,
2100 image_hash); 2100 image_hash);
2101 /* Calculate VBH3 */ 2101 /* Calculate VBH3 */
2102 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 2102 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
2103 ret = -1; 2103 ret = -1;
2104 goto fail; 2104 goto fail;
2105 } 2105 }
2106 2106
2107 fail: 2107 fail:
2108 if (image_buf != NULL) 2108 if (image_buf != NULL)
2109 free(image_buf); 2109 free(image_buf);
2110 return ret; 2110 return ret;
2111 } 2111 }
2112 2112
2113 #endif 2113 #endif
2114 int trusty_setbootparameter(struct andr_img_hdr *hdr, AvbABFlowResult avb_result, 2114 int trusty_setbootparameter(struct andr_img_hdr *hdr, AvbABFlowResult avb_result,
2115 AvbSlotVerifyData *avb_out_data) { 2115 AvbSlotVerifyData *avb_out_data) {
2116 #ifdef CONFIG_DUAL_BOOTLOADER 2116 #ifdef CONFIG_DUAL_BOOTLOADER
2117 uint8_t vbh[AVB_SHA256_DIGEST_SIZE]; 2117 uint8_t vbh[AVB_SHA256_DIGEST_SIZE];
2118 #endif 2118 #endif
2119 int ret = 0; 2119 int ret = 0;
2120 u32 os_ver = hdr->os_version >> 11; 2120 u32 os_ver = hdr->os_version >> 11;
2121 u32 os_ver_km = (((os_ver >> 14) & 0x7F) * 100 + ((os_ver >> 7) & 0x7F)) * 100 2121 u32 os_ver_km = (((os_ver >> 14) & 0x7F) * 100 + ((os_ver >> 7) & 0x7F)) * 100
2122 + (os_ver & 0x7F); 2122 + (os_ver & 0x7F);
2123 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 2123 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
2124 u32 os_lvl_km = ((os_lvl >> 4) + 2000) * 100 + (os_lvl & 0x0F); 2124 u32 os_lvl_km = ((os_lvl >> 4) + 2000) * 100 + (os_lvl & 0x0F);
2125 keymaster_verified_boot_t vbstatus; 2125 keymaster_verified_boot_t vbstatus;
2126 FbLockState lock_status = fastboot_get_lock_stat(); 2126 FbLockState lock_status = fastboot_get_lock_stat();
2127 2127
2128 uint8_t permanent_attributes_hash[AVB_SHA256_DIGEST_SIZE]; 2128 uint8_t permanent_attributes_hash[AVB_SHA256_DIGEST_SIZE];
2129 #ifdef CONFIG_AVB_ATX 2129 #ifdef CONFIG_AVB_ATX
2130 if (fsl_read_permanent_attributes_hash(&fsl_avb_atx_ops, permanent_attributes_hash)) { 2130 if (fsl_read_permanent_attributes_hash(&fsl_avb_atx_ops, permanent_attributes_hash)) {
2131 printf("ERROR - failed to read permanent attributes hash for keymaster\n"); 2131 printf("ERROR - failed to read permanent attributes hash for keymaster\n");
2132 memset(permanent_attributes_hash, 0, AVB_SHA256_DIGEST_SIZE); 2132 memset(permanent_attributes_hash, 0, AVB_SHA256_DIGEST_SIZE);
2133 } 2133 }
2134 #endif 2134 #endif
2135 2135
2136 bool lock = (lock_status == FASTBOOT_LOCK)? true: false; 2136 bool lock = (lock_status == FASTBOOT_LOCK)? true: false;
2137 if (avb_result == AVB_AB_FLOW_RESULT_OK) 2137 if (avb_result == AVB_AB_FLOW_RESULT_OK)
2138 vbstatus = KM_VERIFIED_BOOT_VERIFIED; 2138 vbstatus = KM_VERIFIED_BOOT_VERIFIED;
2139 else 2139 else
2140 vbstatus = KM_VERIFIED_BOOT_FAILED; 2140 vbstatus = KM_VERIFIED_BOOT_FAILED;
2141 2141
2142 /* Calculate VBH */ 2142 /* Calculate VBH */
2143 #ifdef CONFIG_DUAL_BOOTLOADER 2143 #ifdef CONFIG_DUAL_BOOTLOADER
2144 if (vbh_calculate(vbh, avb_out_data)) { 2144 if (vbh_calculate(vbh, avb_out_data)) {
2145 ret = -1; 2145 ret = -1;
2146 goto fail; 2146 goto fail;
2147 } 2147 }
2148 2148
2149 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock, 2149 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock,
2150 permanent_attributes_hash, AVB_SHA256_DIGEST_SIZE, 2150 permanent_attributes_hash, AVB_SHA256_DIGEST_SIZE,
2151 vbh, AVB_SHA256_DIGEST_SIZE); 2151 vbh, AVB_SHA256_DIGEST_SIZE);
2152 #else 2152 #else
2153 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock, 2153 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock,
2154 permanent_attributes_hash, AVB_SHA256_DIGEST_SIZE, 2154 permanent_attributes_hash, AVB_SHA256_DIGEST_SIZE,
2155 NULL, 0); 2155 NULL, 0);
2156 #endif 2156 #endif
2157 2157
2158 fail: 2158 fail:
2159 return ret; 2159 return ret;
2160 } 2160 }
2161 #endif 2161 #endif
2162 2162
2163 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC) 2163 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC)
2164 /* we can use avb to verify Trusty if we want */ 2164 /* we can use avb to verify Trusty if we want */
2165 const char *requested_partitions_boot[] = {"boot", FDT_PART_NAME, NULL}; 2165 const char *requested_partitions_boot[] = {"boot", FDT_PART_NAME, NULL};
2166 const char *requested_partitions_recovery[] = {"recovery", FDT_PART_NAME, NULL}; 2166 const char *requested_partitions_recovery[] = {"recovery", FDT_PART_NAME, NULL};
2167 2167
2168 static bool is_load_fdt_from_part(void) 2168 static bool is_load_fdt_from_part(void)
2169 { 2169 {
2170 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) 2170 #if defined(CONFIG_ANDROID_THINGS_SUPPORT)
2171 if (fastboot_flash_find_ptn("oem_bootloader_a") && 2171 if (fastboot_flash_find_ptn("oem_bootloader_a") &&
2172 fastboot_flash_find_ptn("oem_bootloader_b")) { 2172 fastboot_flash_find_ptn("oem_bootloader_b")) {
2173 #elif defined(CONFIG_ANDROID_AB_SUPPORT) 2173 #elif defined(CONFIG_ANDROID_AB_SUPPORT)
2174 if (fastboot_flash_find_ptn("dtbo_a") && 2174 if (fastboot_flash_find_ptn("dtbo_a") &&
2175 fastboot_flash_find_ptn("dtbo_b")) { 2175 fastboot_flash_find_ptn("dtbo_b")) {
2176 #else 2176 #else
2177 /* for legacy platfrom (imx6/7), we don't support A/B slot. */ 2177 /* for legacy platfrom (imx6/7), we don't support A/B slot. */
2178 if (fastboot_flash_find_ptn("dtbo")) { 2178 if (fastboot_flash_find_ptn("dtbo")) {
2179 #endif 2179 #endif
2180 return true; 2180 return true;
2181 } else { 2181 } else {
2182 return false; 2182 return false;
2183 } 2183 }
2184 } 2184 }
2185 2185
2186 static int find_partition_data_by_name(char* part_name, 2186 static int find_partition_data_by_name(char* part_name,
2187 AvbSlotVerifyData* avb_out_data, AvbPartitionData** avb_loadpart) 2187 AvbSlotVerifyData* avb_out_data, AvbPartitionData** avb_loadpart)
2188 { 2188 {
2189 int num = 0; 2189 int num = 0;
2190 AvbPartitionData* loadpart = NULL; 2190 AvbPartitionData* loadpart = NULL;
2191 2191
2192 for (num = 0; num < avb_out_data->num_loaded_partitions; num++) { 2192 for (num = 0; num < avb_out_data->num_loaded_partitions; num++) {
2193 loadpart = &(avb_out_data->loaded_partitions[num]); 2193 loadpart = &(avb_out_data->loaded_partitions[num]);
2194 if (!(strncmp(loadpart->partition_name, 2194 if (!(strncmp(loadpart->partition_name,
2195 part_name, strlen(part_name)))) { 2195 part_name, strlen(part_name)))) {
2196 *avb_loadpart = loadpart; 2196 *avb_loadpart = loadpart;
2197 break; 2197 break;
2198 } 2198 }
2199 } 2199 }
2200 if (num == avb_out_data->num_loaded_partitions) { 2200 if (num == avb_out_data->num_loaded_partitions) {
2201 printf("Error! Can't find %s partition from avb partition data!\n", 2201 printf("Error! Can't find %s partition from avb partition data!\n",
2202 part_name); 2202 part_name);
2203 return -1; 2203 return -1;
2204 } 2204 }
2205 else 2205 else
2206 return 0; 2206 return 0;
2207 } 2207 }
2208 2208
2209 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { 2209 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) {
2210 2210
2211 ulong addr = 0; 2211 ulong addr = 0;
2212 struct andr_img_hdr *hdr = NULL; 2212 struct andr_img_hdr *hdr = NULL;
2213 void *boot_buf = NULL; 2213 void *boot_buf = NULL;
2214 ulong image_size; 2214 ulong image_size;
2215 u32 avb_metric; 2215 u32 avb_metric;
2216 bool check_image_arm64 = false; 2216 bool check_image_arm64 = false;
2217 bool is_recovery_mode = false; 2217 bool is_recovery_mode = false;
2218 2218
2219 AvbABFlowResult avb_result; 2219 AvbABFlowResult avb_result;
2220 AvbSlotVerifyData *avb_out_data = NULL; 2220 AvbSlotVerifyData *avb_out_data = NULL;
2221 AvbPartitionData *avb_loadpart = NULL; 2221 AvbPartitionData *avb_loadpart = NULL;
2222 2222
2223 /* get bootmode, default to boot "boot" */ 2223 /* get bootmode, default to boot "boot" */
2224 if (argc > 1) { 2224 if (argc > 1) {
2225 is_recovery_mode = 2225 is_recovery_mode =
2226 (strncmp(argv[1], "recovery", sizeof("recovery")) != 0) ? false: true; 2226 (strncmp(argv[1], "recovery", sizeof("recovery")) != 0) ? false: true;
2227 if (is_recovery_mode) 2227 if (is_recovery_mode)
2228 printf("Will boot from recovery!\n"); 2228 printf("Will boot from recovery!\n");
2229 } 2229 }
2230 2230
2231 /* check lock state */ 2231 /* check lock state */
2232 FbLockState lock_status = fastboot_get_lock_stat(); 2232 FbLockState lock_status = fastboot_get_lock_stat();
2233 if (lock_status == FASTBOOT_LOCK_ERROR) { 2233 if (lock_status == FASTBOOT_LOCK_ERROR) {
2234 #ifdef CONFIG_AVB_ATX 2234 #ifdef CONFIG_AVB_ATX
2235 printf("In boota get fastboot lock status error, enter fastboot mode.\n"); 2235 printf("In boota get fastboot lock status error, enter fastboot mode.\n");
2236 goto fail; 2236 goto fail;
2237 #else 2237 #else
2238 printf("In boota get fastboot lock status error. Set lock status\n"); 2238 printf("In boota get fastboot lock status error. Set lock status\n");
2239 fastboot_set_lock_stat(FASTBOOT_LOCK); 2239 fastboot_set_lock_stat(FASTBOOT_LOCK);
2240 lock_status = FASTBOOT_LOCK; 2240 lock_status = FASTBOOT_LOCK;
2241 #endif 2241 #endif
2242 } 2242 }
2243 bool allow_fail = (lock_status == FASTBOOT_UNLOCK ? true : false); 2243 bool allow_fail = (lock_status == FASTBOOT_UNLOCK ? true : false);
2244 avb_metric = get_timer(0); 2244 avb_metric = get_timer(0);
2245 /* we don't need to verify fdt partition if we don't have it. */ 2245 /* we don't need to verify fdt partition if we don't have it. */
2246 if (!is_load_fdt_from_part()) { 2246 if (!is_load_fdt_from_part()) {
2247 requested_partitions_boot[1] = NULL; 2247 requested_partitions_boot[1] = NULL;
2248 requested_partitions_recovery[1] = NULL; 2248 requested_partitions_recovery[1] = NULL;
2249 } 2249 }
2250 #ifndef CONFIG_ANDROID_AB_SUPPORT 2250 #ifndef CONFIG_ANDROID_AB_SUPPORT
2251 else if (is_recovery_mode){ 2251 else if (is_recovery_mode){
2252 requested_partitions_recovery[1] = NULL; 2252 requested_partitions_recovery[1] = NULL;
2253 } 2253 }
2254 #endif 2254 #endif
2255 2255
2256 /* if in lock state, do avb verify */ 2256 /* if in lock state, do avb verify */
2257 #ifndef CONFIG_DUAL_BOOTLOADER 2257 #ifndef CONFIG_DUAL_BOOTLOADER
2258 /* For imx6 on Android, we don't have a/b slot and we want to verify 2258 /* For imx6 on Android, we don't have a/b slot and we want to verify
2259 * boot/recovery with AVB. For imx8 and Android Things we don't have 2259 * boot/recovery with AVB. For imx8 and Android Things we don't have
2260 * recovery and support a/b slot for boot */ 2260 * recovery and support a/b slot for boot */
2261 #ifdef CONFIG_ANDROID_AB_SUPPORT 2261 #ifdef CONFIG_ANDROID_AB_SUPPORT
2262 /* we can use avb to verify Trusty if we want */ 2262 /* we can use avb to verify Trusty if we want */
2263 avb_result = avb_ab_flow_fast(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 2263 avb_result = avb_ab_flow_fast(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
2264 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data); 2264 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data);
2265 #else 2265 #else
2266 if (!is_recovery_mode) { 2266 if (!is_recovery_mode) {
2267 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 2267 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
2268 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data); 2268 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data);
2269 } else { 2269 } else {
2270 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_recovery, allow_fail, 2270 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_recovery, allow_fail,
2271 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data); 2271 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data);
2272 } 2272 }
2273 #endif 2273 #endif
2274 #else /* !CONFIG_DUAL_BOOTLOADER */ 2274 #else /* !CONFIG_DUAL_BOOTLOADER */
2275 /* We will only verify single one slot which has been selected in SPL */ 2275 /* We will only verify single one slot which has been selected in SPL */
2276 avb_result = avb_flow_dual_uboot(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 2276 avb_result = avb_flow_dual_uboot(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
2277 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data); 2277 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data);
2278 2278
2279 /* Goto fail early if current slot is not bootable. */ 2279 /* Goto fail early if current slot is not bootable. */
2280 if (avb_result == AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS) { 2280 if (avb_result == AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS) {
2281 printf("boota: slot verify fail!\n"); 2281 printf("boota: slot verify fail!\n");
2282 goto fail; 2282 goto fail;
2283 } 2283 }
2284 #endif /* !CONFIG_DUAL_BOOTLOADER */ 2284 #endif /* !CONFIG_DUAL_BOOTLOADER */
2285 2285
2286 /* get the duration of avb */ 2286 /* get the duration of avb */
2287 metrics.avb = get_timer(avb_metric); 2287 metrics.avb = get_timer(avb_metric);
2288 2288
2289 if ((avb_result == AVB_AB_FLOW_RESULT_OK) || 2289 if ((avb_result == AVB_AB_FLOW_RESULT_OK) ||
2290 (avb_result == AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR)) { 2290 (avb_result == AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR)) {
2291 assert(avb_out_data != NULL); 2291 assert(avb_out_data != NULL);
2292 /* We may have more than one partition loaded by AVB, find the boot 2292 /* We may have more than one partition loaded by AVB, find the boot
2293 * partition first. 2293 * partition first.
2294 */ 2294 */
2295 #ifdef CONFIG_ANDROID_AB_SUPPORT 2295 #ifdef CONFIG_ANDROID_AB_SUPPORT
2296 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart)) 2296 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart))
2297 goto fail; 2297 goto fail;
2298 #else 2298 #else
2299 if (!is_recovery_mode) { 2299 if (!is_recovery_mode) {
2300 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart)) 2300 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart))
2301 goto fail; 2301 goto fail;
2302 } else { 2302 } else {
2303 if (find_partition_data_by_name("recovery", avb_out_data, &avb_loadpart)) 2303 if (find_partition_data_by_name("recovery", avb_out_data, &avb_loadpart))
2304 goto fail; 2304 goto fail;
2305 } 2305 }
2306 #endif 2306 #endif
2307 assert(avb_loadpart != NULL); 2307 assert(avb_loadpart != NULL);
2308 /* we should use avb_part_data->data as boot image */ 2308 /* we should use avb_part_data->data as boot image */
2309 /* boot image is already read by avb */ 2309 /* boot image is already read by avb */
2310 hdr = (struct andr_img_hdr *)avb_loadpart->data; 2310 hdr = (struct andr_img_hdr *)avb_loadpart->data;
2311 if (android_image_check_header(hdr)) { 2311 if (android_image_check_header(hdr)) {
2312 printf("boota: bad boot image magic\n"); 2312 printf("boota: bad boot image magic\n");
2313 goto fail; 2313 goto fail;
2314 } 2314 }
2315 if (avb_result == AVB_AB_FLOW_RESULT_OK) 2315 if (avb_result == AVB_AB_FLOW_RESULT_OK)
2316 printf(" verify OK, boot '%s%s'\n", 2316 printf(" verify OK, boot '%s%s'\n",
2317 avb_loadpart->partition_name, avb_out_data->ab_suffix); 2317 avb_loadpart->partition_name, avb_out_data->ab_suffix);
2318 else { 2318 else {
2319 printf(" verify FAIL, state: UNLOCK\n"); 2319 printf(" verify FAIL, state: UNLOCK\n");
2320 printf(" boot '%s%s' still\n", 2320 printf(" boot '%s%s' still\n",
2321 avb_loadpart->partition_name, avb_out_data->ab_suffix); 2321 avb_loadpart->partition_name, avb_out_data->ab_suffix);
2322 } 2322 }
2323 char bootargs_sec[ANDR_BOOT_EXTRA_ARGS_SIZE]; 2323 char bootargs_sec[ANDR_BOOT_EXTRA_ARGS_SIZE];
2324 if (lock_status == FASTBOOT_LOCK) { 2324 if (lock_status == FASTBOOT_LOCK) {
2325 snprintf(bootargs_sec, sizeof(bootargs_sec), 2325 snprintf(bootargs_sec, sizeof(bootargs_sec),
2326 "androidboot.verifiedbootstate=green androidboot.slot_suffix=%s %s", 2326 "androidboot.verifiedbootstate=green androidboot.slot_suffix=%s %s",
2327 avb_out_data->ab_suffix, avb_out_data->cmdline); 2327 avb_out_data->ab_suffix, avb_out_data->cmdline);
2328 } else { 2328 } else {
2329 snprintf(bootargs_sec, sizeof(bootargs_sec), 2329 snprintf(bootargs_sec, sizeof(bootargs_sec),
2330 "androidboot.verifiedbootstate=orange androidboot.slot_suffix=%s %s", 2330 "androidboot.verifiedbootstate=orange androidboot.slot_suffix=%s %s",
2331 avb_out_data->ab_suffix, avb_out_data->cmdline); 2331 avb_out_data->ab_suffix, avb_out_data->cmdline);
2332 } 2332 }
2333 env_set("bootargs_sec", bootargs_sec); 2333 env_set("bootargs_sec", bootargs_sec);
2334 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT 2334 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT
2335 if(!is_recovery_mode) { 2335 if(!is_recovery_mode) {
2336 if(avb_out_data->cmdline != NULL && strstr(avb_out_data->cmdline, "root=")) 2336 if(avb_out_data->cmdline != NULL && strstr(avb_out_data->cmdline, "root="))
2337 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, false); 2337 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, false);
2338 else 2338 else
2339 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, true); 2339 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, true);
2340 } 2340 }
2341 #endif /* CONFIG_SYSTEM_RAMDISK_SUPPORT */ 2341 #endif /* CONFIG_SYSTEM_RAMDISK_SUPPORT */
2342 image_size = avb_loadpart->data_size; 2342 image_size = avb_loadpart->data_size;
2343 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M) 2343 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M)
2344 /* If we are using uncompressed kernel image, copy it directly to 2344 /* If we are using uncompressed kernel image, copy it directly to
2345 * hdr->kernel_addr, if we are using compressed lz4 kernel image, 2345 * hdr->kernel_addr, if we are using compressed lz4 kernel image,
2346 * we need to decompress the kernel image first. */ 2346 * we need to decompress the kernel image first. */
2347 if (image_arm64((void *)((ulong)hdr + hdr->page_size))) { 2347 if (image_arm64((void *)((ulong)hdr + hdr->page_size))) {
2348 memcpy((void *)(long)hdr->kernel_addr, 2348 memcpy((void *)(long)hdr->kernel_addr,
2349 (void *)((ulong)hdr + hdr->page_size), hdr->kernel_size); 2349 (void *)((ulong)hdr + hdr->page_size), hdr->kernel_size);
2350 } else { 2350 } else {
2351 #ifdef CONFIG_LZ4 2351 #ifdef CONFIG_LZ4
2352 size_t lz4_len = DST_DECOMPRESS_LEN; 2352 size_t lz4_len = DST_DECOMPRESS_LEN;
2353 if (ulz4fn((void *)((ulong)hdr + hdr->page_size), 2353 if (ulz4fn((void *)((ulong)hdr + hdr->page_size),
2354 hdr->kernel_size, (void *)(ulong)hdr->kernel_addr, &lz4_len) != 0) { 2354 hdr->kernel_size, (void *)(ulong)hdr->kernel_addr, &lz4_len) != 0) {
2355 printf("Decompress kernel fail!\n"); 2355 printf("Decompress kernel fail!\n");
2356 goto fail; 2356 goto fail;
2357 } 2357 }
2358 #else /* CONFIG_LZ4 */ 2358 #else /* CONFIG_LZ4 */
2359 printf("please enable CONFIG_LZ4 if we're using compressed lz4 kernel image!\n"); 2359 printf("please enable CONFIG_LZ4 if we're using compressed lz4 kernel image!\n");
2360 goto fail; 2360 goto fail;
2361 #endif /* CONFIG_LZ4 */ 2361 #endif /* CONFIG_LZ4 */
2362 } 2362 }
2363 #else /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */ 2363 #else /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */
2364 /* copy kernel image and boot header to hdr->kernel_addr - hdr->page_size */ 2364 /* copy kernel image and boot header to hdr->kernel_addr - hdr->page_size */
2365 memcpy((void *)(ulong)(hdr->kernel_addr - hdr->page_size), (void *)hdr, 2365 memcpy((void *)(ulong)(hdr->kernel_addr - hdr->page_size), (void *)hdr,
2366 hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size)); 2366 hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size));
2367 #endif /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */ 2367 #endif /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */
2368 } else { 2368 } else {
2369 /* Fall into fastboot mode if get unacceptable error from avb 2369 /* Fall into fastboot mode if get unacceptable error from avb
2370 * or verify fail in lock state. 2370 * or verify fail in lock state.
2371 */ 2371 */
2372 if (lock_status == FASTBOOT_LOCK) 2372 if (lock_status == FASTBOOT_LOCK)
2373 printf(" verify FAIL, state: LOCK\n"); 2373 printf(" verify FAIL, state: LOCK\n");
2374 2374
2375 goto fail; 2375 goto fail;
2376 } 2376 }
2377 2377
2378 flush_cache((ulong)load_addr, image_size); 2378 flush_cache((ulong)load_addr, image_size);
2379 check_image_arm64 = image_arm64((void *)(ulong)hdr->kernel_addr); 2379 check_image_arm64 = image_arm64((void *)(ulong)hdr->kernel_addr);
2380 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT 2380 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT
2381 if (is_recovery_mode) 2381 if (is_recovery_mode)
2382 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size 2382 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size
2383 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 2383 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
2384 #else 2384 #else
2385 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size 2385 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size
2386 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 2386 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
2387 #endif 2387 #endif
2388 #ifdef CONFIG_OF_LIBFDT 2388 #ifdef CONFIG_OF_LIBFDT
2389 /* load the dtb file */ 2389 /* load the dtb file */
2390 u32 fdt_size = 0; 2390 u32 fdt_size = 0;
2391 struct dt_table_header *dt_img = NULL; 2391 struct dt_table_header *dt_img = NULL;
2392 2392
2393 if (is_load_fdt_from_part()) { 2393 if (is_load_fdt_from_part()) {
2394 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 2394 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
2395 if (find_partition_data_by_name("oem_bootloader", 2395 if (find_partition_data_by_name("oem_bootloader",
2396 avb_out_data, &avb_loadpart)) { 2396 avb_out_data, &avb_loadpart)) {
2397 goto fail; 2397 goto fail;
2398 } else 2398 } else
2399 dt_img = (struct dt_table_header *)avb_loadpart->data; 2399 dt_img = (struct dt_table_header *)avb_loadpart->data;
2400 #elif defined(CONFIG_ANDROID_AB_SUPPORT) 2400 #elif defined(CONFIG_ANDROID_AB_SUPPORT)
2401 if (find_partition_data_by_name("dtbo", 2401 if (find_partition_data_by_name("dtbo",
2402 avb_out_data, &avb_loadpart)) { 2402 avb_out_data, &avb_loadpart)) {
2403 goto fail; 2403 goto fail;
2404 } else 2404 } else
2405 dt_img = (struct dt_table_header *)avb_loadpart->data; 2405 dt_img = (struct dt_table_header *)avb_loadpart->data;
2406 #else 2406 #else
2407 if (is_recovery_mode) { 2407 if (is_recovery_mode) {
2408 if (hdr->header_version != 1) { 2408 if (hdr->header_version != 1) {
2409 printf("boota: boot image header version error!\n"); 2409 printf("boota: boot image header version error!\n");
2410 goto fail; 2410 goto fail;
2411 } 2411 }
2412 2412
2413 dt_img = (struct dt_table_header *)((void *)(ulong)hdr + 2413 dt_img = (struct dt_table_header *)((void *)(ulong)hdr +
2414 hdr->page_size + 2414 hdr->page_size +
2415 ALIGN(hdr->kernel_size, hdr->page_size) + 2415 ALIGN(hdr->kernel_size, hdr->page_size) +
2416 ALIGN(hdr->ramdisk_size, hdr->page_size) + 2416 ALIGN(hdr->ramdisk_size, hdr->page_size) +
2417 ALIGN(hdr->second_size, hdr->page_size)); 2417 ALIGN(hdr->second_size, hdr->page_size));
2418 } else if (find_partition_data_by_name("dtbo", 2418 } else if (find_partition_data_by_name("dtbo",
2419 avb_out_data, &avb_loadpart)) { 2419 avb_out_data, &avb_loadpart)) {
2420 goto fail; 2420 goto fail;
2421 } else 2421 } else
2422 dt_img = (struct dt_table_header *)avb_loadpart->data; 2422 dt_img = (struct dt_table_header *)avb_loadpart->data;
2423 #endif 2423 #endif
2424 2424
2425 if (be32_to_cpu(dt_img->magic) != DT_TABLE_MAGIC) { 2425 if (be32_to_cpu(dt_img->magic) != DT_TABLE_MAGIC) {
2426 printf("boota: bad dt table magic %08x\n", 2426 printf("boota: bad dt table magic %08x\n",
2427 be32_to_cpu(dt_img->magic)); 2427 be32_to_cpu(dt_img->magic));
2428 goto fail; 2428 goto fail;
2429 } else if (!be32_to_cpu(dt_img->dt_entry_count)) { 2429 } else if (!be32_to_cpu(dt_img->dt_entry_count)) {
2430 printf("boota: no dt entries\n"); 2430 printf("boota: no dt entries\n");
2431 goto fail; 2431 goto fail;
2432 } 2432 }
2433 2433
2434 struct dt_table_entry *dt_entry; 2434 struct dt_table_entry *dt_entry;
2435 dt_entry = (struct dt_table_entry *)((ulong)dt_img + 2435 dt_entry = (struct dt_table_entry *)((ulong)dt_img +
2436 be32_to_cpu(dt_img->dt_entries_offset)); 2436 be32_to_cpu(dt_img->dt_entries_offset));
2437 fdt_size = be32_to_cpu(dt_entry->dt_size); 2437 fdt_size = be32_to_cpu(dt_entry->dt_size);
2438 memcpy((void *)(ulong)hdr->second_addr, (void *)((ulong)dt_img + 2438 memcpy((void *)(ulong)hdr->second_addr, (void *)((ulong)dt_img +
2439 be32_to_cpu(dt_entry->dt_offset)), fdt_size); 2439 be32_to_cpu(dt_entry->dt_offset)), fdt_size);
2440 } else { 2440 } else {
2441 if (hdr->second_size && hdr->second_addr) { 2441 if (hdr->second_size && hdr->second_addr) {
2442 memcpy((void *)(ulong)hdr->second_addr, 2442 memcpy((void *)(ulong)hdr->second_addr,
2443 (void *)(ulong)hdr + hdr->page_size 2443 (void *)(ulong)hdr + hdr->page_size
2444 + ALIGN(hdr->kernel_size, hdr->page_size) 2444 + ALIGN(hdr->kernel_size, hdr->page_size)
2445 + ALIGN(hdr->ramdisk_size, hdr->page_size), 2445 + ALIGN(hdr->ramdisk_size, hdr->page_size),
2446 hdr->second_size); 2446 hdr->second_size);
2447 } 2447 }
2448 } 2448 }
2449 #endif /*CONFIG_OF_LIBFDT*/ 2449 #endif /*CONFIG_OF_LIBFDT*/
2450 2450
2451 if (check_image_arm64) { 2451 if (check_image_arm64) {
2452 android_image_get_kernel(hdr, 0, NULL, NULL); 2452 android_image_get_kernel(hdr, 0, NULL, NULL);
2453 addr = hdr->kernel_addr; 2453 addr = hdr->kernel_addr;
2454 } else { 2454 } else {
2455 addr = (ulong)(hdr->kernel_addr - hdr->page_size); 2455 addr = (ulong)(hdr->kernel_addr - hdr->page_size);
2456 } 2456 }
2457 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size); 2457 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size);
2458 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size); 2458 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size);
2459 #ifdef CONFIG_OF_LIBFDT 2459 #ifdef CONFIG_OF_LIBFDT
2460 if (is_load_fdt_from_part()) { 2460 if (is_load_fdt_from_part()) {
2461 if (fdt_size) 2461 if (fdt_size)
2462 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size); 2462 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size);
2463 } else { 2463 } else {
2464 if (hdr->second_size) 2464 if (hdr->second_size)
2465 printf("fdt @ %08x (%d)\n", hdr->second_addr, hdr->second_size); 2465 printf("fdt @ %08x (%d)\n", hdr->second_addr, hdr->second_size);
2466 } 2466 }
2467 #endif /*CONFIG_OF_LIBFDT*/ 2467 #endif /*CONFIG_OF_LIBFDT*/
2468 2468
2469 char boot_addr_start[12]; 2469 char boot_addr_start[12];
2470 char ramdisk_addr[25]; 2470 char ramdisk_addr[25];
2471 char fdt_addr[12]; 2471 char fdt_addr[12];
2472 2472
2473 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr}; 2473 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr};
2474 if (check_image_arm64) 2474 if (check_image_arm64)
2475 boot_args[0] = "booti"; 2475 boot_args[0] = "booti";
2476 else 2476 else
2477 boot_args[0] = "bootm"; 2477 boot_args[0] = "bootm";
2478 2478
2479 sprintf(boot_addr_start, "0x%lx", addr); 2479 sprintf(boot_addr_start, "0x%lx", addr);
2480 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size); 2480 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size);
2481 sprintf(fdt_addr, "0x%x", hdr->second_addr); 2481 sprintf(fdt_addr, "0x%x", hdr->second_addr);
2482 2482
2483 /* no need to pass ramdisk addr for normal boot mode when enable CONFIG_SYSTEM_RAMDISK_SUPPORT*/ 2483 /* no need to pass ramdisk addr for normal boot mode when enable CONFIG_SYSTEM_RAMDISK_SUPPORT*/
2484 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT 2484 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT
2485 if (!is_recovery_mode) 2485 if (!is_recovery_mode)
2486 boot_args[2] = NULL; 2486 boot_args[2] = NULL;
2487 #endif 2487 #endif
2488 2488
2489 #ifdef CONFIG_IMX_TRUSTY_OS 2489 #ifdef CONFIG_IMX_TRUSTY_OS
2490 /* Trusty keymaster needs some parameters before it work */ 2490 /* Trusty keymaster needs some parameters before it work */
2491 if (trusty_setbootparameter(hdr, avb_result, avb_out_data)) 2491 if (trusty_setbootparameter(hdr, avb_result, avb_out_data))
2492 goto fail; 2492 goto fail;
2493 /* lock the boot status and rollback_idx preventing Linux modify it */ 2493 /* lock the boot status and rollback_idx preventing Linux modify it */
2494 trusty_lock_boot_state(); 2494 trusty_lock_boot_state();
2495 /* put ql-tipc to release resource for Linux */ 2495 /* put ql-tipc to release resource for Linux */
2496 trusty_ipc_shutdown(); 2496 trusty_ipc_shutdown();
2497 #endif 2497 #endif
2498 2498
2499 if (avb_out_data != NULL) 2499 if (avb_out_data != NULL)
2500 avb_slot_verify_data_free(avb_out_data); 2500 avb_slot_verify_data_free(avb_out_data);
2501 if (boot_buf != NULL) 2501 if (boot_buf != NULL)
2502 free(boot_buf); 2502 free(boot_buf);
2503 2503
2504 if (check_image_arm64) { 2504 if (check_image_arm64) {
2505 #ifdef CONFIG_CMD_BOOTI 2505 #ifdef CONFIG_CMD_BOOTI
2506 do_booti(NULL, 0, 4, boot_args); 2506 do_booti(NULL, 0, 4, boot_args);
2507 #else 2507 #else
2508 debug("please enable CONFIG_CMD_BOOTI when kernel are Image"); 2508 debug("please enable CONFIG_CMD_BOOTI when kernel are Image");
2509 #endif 2509 #endif
2510 } else { 2510 } else {
2511 do_bootm(NULL, 0, 4, boot_args); 2511 do_bootm(NULL, 0, 4, boot_args);
2512 } 2512 }
2513 2513
2514 /* This only happens if image is somehow faulty so we start over */ 2514 /* This only happens if image is somehow faulty so we start over */
2515 do_reset(NULL, 0, 0, NULL); 2515 do_reset(NULL, 0, 0, NULL);
2516 2516
2517 return 1; 2517 return 1;
2518 2518
2519 fail: 2519 fail:
2520 /* avb has no recovery */ 2520 /* avb has no recovery */
2521 if (avb_out_data != NULL) 2521 if (avb_out_data != NULL)
2522 avb_slot_verify_data_free(avb_out_data); 2522 avb_slot_verify_data_free(avb_out_data);
2523 2523
2524 return run_command("fastboot 0", 0); 2524 return run_command("fastboot 0", 0);
2525 } 2525 }
2526 2526
2527 U_BOOT_CMD( 2527 U_BOOT_CMD(
2528 boota, 2, 1, do_boota, 2528 boota, 2, 1, do_boota,
2529 "boota - boot android bootimg \n", 2529 "boota - boot android bootimg \n",
2530 "boot from current mmc with avb verify\n" 2530 "boot from current mmc with avb verify\n"
2531 ); 2531 );
2532 #else /* CONFIG_AVB_SUPPORT */ 2532 #else /* CONFIG_AVB_SUPPORT */
2533 /* boota <addr> [ mmc0 | mmc1 [ <partition> ] ] */ 2533 /* boota <addr> [ mmc0 | mmc1 [ <partition> ] ] */
2534 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 2534 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2535 { 2535 {
2536 ulong addr = 0; 2536 ulong addr = 0;
2537 char *ptn = "boot"; 2537 char *ptn = "boot";
2538 int mmcc = -1; 2538 int mmcc = -1;
2539 struct andr_img_hdr *hdr = &boothdr; 2539 struct andr_img_hdr *hdr = &boothdr;
2540 ulong image_size; 2540 ulong image_size;
2541 bool check_image_arm64 = false; 2541 bool check_image_arm64 = false;
2542 int i = 0; 2542 int i = 0;
2543 2543
2544 for (i = 0; i < argc; i++) 2544 for (i = 0; i < argc; i++)
2545 printf("%s ", argv[i]); 2545 printf("%s ", argv[i]);
2546 printf("\n"); 2546 printf("\n");
2547 2547
2548 if (argc < 2) 2548 if (argc < 2)
2549 return -1; 2549 return -1;
2550 2550
2551 mmcc = simple_strtoul(argv[1]+3, NULL, 10); 2551 mmcc = simple_strtoul(argv[1]+3, NULL, 10);
2552 2552
2553 if (argc > 2) 2553 if (argc > 2)
2554 ptn = argv[2]; 2554 ptn = argv[2];
2555 2555
2556 if (mmcc != -1) { 2556 if (mmcc != -1) {
2557 #ifdef CONFIG_MMC 2557 #ifdef CONFIG_MMC
2558 struct fastboot_ptentry *pte; 2558 struct fastboot_ptentry *pte;
2559 struct mmc *mmc; 2559 struct mmc *mmc;
2560 disk_partition_t info; 2560 disk_partition_t info;
2561 struct blk_desc *dev_desc = NULL; 2561 struct blk_desc *dev_desc = NULL;
2562 unsigned bootimg_sectors; 2562 unsigned bootimg_sectors;
2563 2563
2564 memset((void *)&info, 0 , sizeof(disk_partition_t)); 2564 memset((void *)&info, 0 , sizeof(disk_partition_t));
2565 /* i.MX use MBR as partition table, so this will have 2565 /* i.MX use MBR as partition table, so this will have
2566 to find the start block and length for the 2566 to find the start block and length for the
2567 partition name and register the fastboot pte we 2567 partition name and register the fastboot pte we
2568 define the partition number of each partition in 2568 define the partition number of each partition in
2569 config file 2569 config file
2570 */ 2570 */
2571 mmc = find_mmc_device(mmcc); 2571 mmc = find_mmc_device(mmcc);
2572 if (!mmc) { 2572 if (!mmc) {
2573 printf("boota: cannot find '%d' mmc device\n", mmcc); 2573 printf("boota: cannot find '%d' mmc device\n", mmcc);
2574 goto fail; 2574 goto fail;
2575 } 2575 }
2576 dev_desc = blk_get_dev("mmc", mmcc); 2576 dev_desc = blk_get_dev("mmc", mmcc);
2577 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { 2577 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
2578 printf("** Block device MMC %d not supported\n", mmcc); 2578 printf("** Block device MMC %d not supported\n", mmcc);
2579 goto fail; 2579 goto fail;
2580 } 2580 }
2581 2581
2582 /* below was i.MX mmc operation code */ 2582 /* below was i.MX mmc operation code */
2583 if (mmc_init(mmc)) { 2583 if (mmc_init(mmc)) {
2584 printf("mmc%d init failed\n", mmcc); 2584 printf("mmc%d init failed\n", mmcc);
2585 goto fail; 2585 goto fail;
2586 } 2586 }
2587 2587
2588 pte = fastboot_flash_find_ptn(ptn); 2588 pte = fastboot_flash_find_ptn(ptn);
2589 if (!pte) { 2589 if (!pte) {
2590 printf("boota: cannot find '%s' partition\n", ptn); 2590 printf("boota: cannot find '%s' partition\n", ptn);
2591 fastboot_flash_dump_ptn(); 2591 fastboot_flash_dump_ptn();
2592 goto fail; 2592 goto fail;
2593 } 2593 }
2594 2594
2595 if (blk_dread(dev_desc, pte->start, 2595 if (blk_dread(dev_desc, pte->start,
2596 1, (void *)hdr) < 0) { 2596 1, (void *)hdr) < 0) {
2597 printf("boota: mmc failed to read bootimg header\n"); 2597 printf("boota: mmc failed to read bootimg header\n");
2598 goto fail; 2598 goto fail;
2599 } 2599 }
2600 2600
2601 if (android_image_check_header(hdr)) { 2601 if (android_image_check_header(hdr)) {
2602 printf("boota: bad boot image magic\n"); 2602 printf("boota: bad boot image magic\n");
2603 goto fail; 2603 goto fail;
2604 } 2604 }
2605 2605
2606 image_size = android_image_get_end(hdr) - (ulong)hdr; 2606 image_size = android_image_get_end(hdr) - (ulong)hdr;
2607 bootimg_sectors = image_size/512; 2607 bootimg_sectors = image_size/512;
2608 2608
2609 if (blk_dread(dev_desc, pte->start, 2609 if (blk_dread(dev_desc, pte->start,
2610 bootimg_sectors, 2610 bootimg_sectors,
2611 (void *)(hdr->kernel_addr - hdr->page_size)) < 0) { 2611 (void *)(hdr->kernel_addr - hdr->page_size)) < 0) {
2612 printf("boota: mmc failed to read bootimage\n"); 2612 printf("boota: mmc failed to read bootimage\n");
2613 goto fail; 2613 goto fail;
2614 } 2614 }
2615 check_image_arm64 = image_arm64((void *)hdr->kernel_addr); 2615 check_image_arm64 = image_arm64((void *)hdr->kernel_addr);
2616 #ifdef CONFIG_FASTBOOT_LOCK 2616 #ifdef CONFIG_FASTBOOT_LOCK
2617 int verifyresult = -1; 2617 int verifyresult = -1;
2618 #endif 2618 #endif
2619 2619
2620 #ifdef CONFIG_FASTBOOT_LOCK 2620 #ifdef CONFIG_FASTBOOT_LOCK
2621 int lock_status = fastboot_get_lock_stat(); 2621 int lock_status = fastboot_get_lock_stat();
2622 if (lock_status == FASTBOOT_LOCK_ERROR) { 2622 if (lock_status == FASTBOOT_LOCK_ERROR) {
2623 printf("In boota get fastboot lock status error. Set lock status\n"); 2623 printf("In boota get fastboot lock status error. Set lock status\n");
2624 fastboot_set_lock_stat(FASTBOOT_LOCK); 2624 fastboot_set_lock_stat(FASTBOOT_LOCK);
2625 } 2625 }
2626 display_lock(fastboot_get_lock_stat(), verifyresult); 2626 display_lock(fastboot_get_lock_stat(), verifyresult);
2627 #endif 2627 #endif
2628 /* load the ramdisk file */ 2628 /* load the ramdisk file */
2629 memcpy((void *)hdr->ramdisk_addr, (void *)hdr->kernel_addr 2629 memcpy((void *)hdr->ramdisk_addr, (void *)hdr->kernel_addr
2630 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 2630 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
2631 2631
2632 #ifdef CONFIG_OF_LIBFDT 2632 #ifdef CONFIG_OF_LIBFDT
2633 u32 fdt_size = 0; 2633 u32 fdt_size = 0;
2634 /* load the dtb file */ 2634 /* load the dtb file */
2635 if (hdr->second_addr) { 2635 if (hdr->second_addr) {
2636 u32 zimage_size = ((u32 *)hdrload->kernel_addr)[ZIMAGE_END_ADDR] 2636 u32 zimage_size = ((u32 *)hdrload->kernel_addr)[ZIMAGE_END_ADDR]
2637 - ((u32 *)hdrload->kernel_addr)[ZIMAGE_START_ADDR]; 2637 - ((u32 *)hdrload->kernel_addr)[ZIMAGE_START_ADDR];
2638 fdt_size = hdrload->kernel_size - zimage_size; 2638 fdt_size = hdrload->kernel_size - zimage_size;
2639 memcpy((void *)(ulong)hdrload->second_addr, 2639 memcpy((void *)(ulong)hdrload->second_addr,
2640 (void*)(ulong)hdrload->kernel_addr + zimage_size, fdt_size); 2640 (void*)(ulong)hdrload->kernel_addr + zimage_size, fdt_size);
2641 } 2641 }
2642 #endif /*CONFIG_OF_LIBFDT*/ 2642 #endif /*CONFIG_OF_LIBFDT*/
2643 2643
2644 #else /*! CONFIG_MMC*/ 2644 #else /*! CONFIG_MMC*/
2645 return -1; 2645 return -1;
2646 #endif /*! CONFIG_MMC*/ 2646 #endif /*! CONFIG_MMC*/
2647 } else { 2647 } else {
2648 printf("boota: parameters is invalid. only support mmcX device\n"); 2648 printf("boota: parameters is invalid. only support mmcX device\n");
2649 return -1; 2649 return -1;
2650 } 2650 }
2651 2651
2652 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size); 2652 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size);
2653 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size); 2653 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size);
2654 #ifdef CONFIG_OF_LIBFDT 2654 #ifdef CONFIG_OF_LIBFDT
2655 if (fdt_size) 2655 if (fdt_size)
2656 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size); 2656 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size);
2657 #endif /*CONFIG_OF_LIBFDT*/ 2657 #endif /*CONFIG_OF_LIBFDT*/
2658 2658
2659 2659
2660 char boot_addr_start[12]; 2660 char boot_addr_start[12];
2661 char ramdisk_addr[25]; 2661 char ramdisk_addr[25];
2662 char fdt_addr[12]; 2662 char fdt_addr[12];
2663 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr}; 2663 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr};
2664 if (check_image_arm64 ) { 2664 if (check_image_arm64 ) {
2665 addr = hdr->kernel_addr; 2665 addr = hdr->kernel_addr;
2666 boot_args[0] = "booti"; 2666 boot_args[0] = "booti";
2667 } else { 2667 } else {
2668 addr = hdr->kernel_addr - hdr->page_size; 2668 addr = hdr->kernel_addr - hdr->page_size;
2669 boot_args[0] = "bootm"; 2669 boot_args[0] = "bootm";
2670 } 2670 }
2671 2671
2672 sprintf(boot_addr_start, "0x%lx", addr); 2672 sprintf(boot_addr_start, "0x%lx", addr);
2673 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size); 2673 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size);
2674 sprintf(fdt_addr, "0x%x", hdr->second_addr); 2674 sprintf(fdt_addr, "0x%x", hdr->second_addr);
2675 if (check_image_arm64) { 2675 if (check_image_arm64) {
2676 android_image_get_kernel(hdr, 0, NULL, NULL); 2676 android_image_get_kernel(hdr, 0, NULL, NULL);
2677 #ifdef CONFIG_CMD_BOOTI 2677 #ifdef CONFIG_CMD_BOOTI
2678 do_booti(NULL, 0, 4, boot_args); 2678 do_booti(NULL, 0, 4, boot_args);
2679 #else 2679 #else
2680 debug("please enable CONFIG_CMD_BOOTI when kernel are Image"); 2680 debug("please enable CONFIG_CMD_BOOTI when kernel are Image");
2681 #endif 2681 #endif
2682 } else { 2682 } else {
2683 do_bootm(NULL, 0, 4, boot_args); 2683 do_bootm(NULL, 0, 4, boot_args);
2684 } 2684 }
2685 /* This only happens if image is somehow faulty so we start over */ 2685 /* This only happens if image is somehow faulty so we start over */
2686 do_reset(NULL, 0, 0, NULL); 2686 do_reset(NULL, 0, 0, NULL);
2687 2687
2688 return 1; 2688 return 1;
2689 2689
2690 fail: 2690 fail:
2691 #if defined(CONFIG_FSL_FASTBOOT) 2691 #if defined(CONFIG_FSL_FASTBOOT)
2692 return run_command("fastboot 0", 0); 2692 return run_command("fastboot 0", 0);
2693 #else /*! CONFIG_FSL_FASTBOOT*/ 2693 #else /*! CONFIG_FSL_FASTBOOT*/
2694 return -1; 2694 return -1;
2695 #endif /*! CONFIG_FSL_FASTBOOT*/ 2695 #endif /*! CONFIG_FSL_FASTBOOT*/
2696 } 2696 }
2697 2697
2698 U_BOOT_CMD( 2698 U_BOOT_CMD(
2699 boota, 3, 1, do_boota, 2699 boota, 3, 1, do_boota,
2700 "boota - boot android bootimg from memory\n", 2700 "boota - boot android bootimg from memory\n",
2701 "[<addr> | mmc0 | mmc1 | mmc2 | mmcX] [<partition>]\n " 2701 "[<addr> | mmc0 | mmc1 | mmc2 | mmcX] [<partition>]\n "
2702 "- boot application image stored in memory or mmc\n" 2702 "- boot application image stored in memory or mmc\n"
2703 "\t'addr' should be the address of boot image " 2703 "\t'addr' should be the address of boot image "
2704 "which is zImage+ramdisk.img\n" 2704 "which is zImage+ramdisk.img\n"
2705 "\t'mmcX' is the mmc device you store your boot.img, " 2705 "\t'mmcX' is the mmc device you store your boot.img, "
2706 "which will read the boot.img from 1M offset('/boot' partition)\n" 2706 "which will read the boot.img from 1M offset('/boot' partition)\n"
2707 "\t 'partition' (optional) is the partition id of your device, " 2707 "\t 'partition' (optional) is the partition id of your device, "
2708 "if no partition give, will going to 'boot' partition\n" 2708 "if no partition give, will going to 'boot' partition\n"
2709 ); 2709 );
2710 #endif /* CONFIG_AVB_SUPPORT */ 2710 #endif /* CONFIG_AVB_SUPPORT */
2711 #endif /* CONFIG_CMD_BOOTA */ 2711 #endif /* CONFIG_CMD_BOOTA */
2712 #endif 2712 #endif
2713 2713
2714 void fastboot_fail(const char *reason) 2714 void fastboot_fail(const char *reason)
2715 { 2715 {
2716 strncpy(fb_response_str, "FAIL\0", 5); 2716 strncpy(fb_response_str, "FAIL\0", 5);
2717 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); 2717 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
2718 } 2718 }
2719 2719
2720 void fastboot_okay(const char *reason) 2720 void fastboot_okay(const char *reason)
2721 { 2721 {
2722 strncpy(fb_response_str, "OKAY\0", 5); 2722 strncpy(fb_response_str, "OKAY\0", 5);
2723 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); 2723 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
2724 } 2724 }
2725 2725
2726 static void fastboot_fifo_complete(struct usb_ep *ep, struct usb_request *req) 2726 static void fastboot_fifo_complete(struct usb_ep *ep, struct usb_request *req)
2727 { 2727 {
2728 int status = req->status; 2728 int status = req->status;
2729 usb_req *request; 2729 usb_req *request;
2730 2730
2731 if (!status) { 2731 if (!status) {
2732 if (fastboot_func->front != NULL) { 2732 if (fastboot_func->front != NULL) {
2733 request = fastboot_func->front; 2733 request = fastboot_func->front;
2734 fastboot_func->front = fastboot_func->front->next; 2734 fastboot_func->front = fastboot_func->front->next;
2735 usb_ep_free_request(ep, request->in_req); 2735 usb_ep_free_request(ep, request->in_req);
2736 free(request); 2736 free(request);
2737 } else { 2737 } else {
2738 printf("fail free request\n"); 2738 printf("fail free request\n");
2739 } 2739 }
2740 return; 2740 return;
2741 } 2741 }
2742 } 2742 }
2743 2743
2744 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) 2744 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
2745 { 2745 {
2746 int status = req->status; 2746 int status = req->status;
2747 if (!status) 2747 if (!status)
2748 return; 2748 return;
2749 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); 2749 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
2750 } 2750 }
2751 2751
2752 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) 2752 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
2753 { 2753 {
2754 int id; 2754 int id;
2755 struct usb_gadget *gadget = c->cdev->gadget; 2755 struct usb_gadget *gadget = c->cdev->gadget;
2756 struct f_fastboot *f_fb = func_to_fastboot(f); 2756 struct f_fastboot *f_fb = func_to_fastboot(f);
2757 const char *s; 2757 const char *s;
2758 2758
2759 /* DYNAMIC interface numbers assignments */ 2759 /* DYNAMIC interface numbers assignments */
2760 id = usb_interface_id(c, f); 2760 id = usb_interface_id(c, f);
2761 if (id < 0) 2761 if (id < 0)
2762 return id; 2762 return id;
2763 interface_desc.bInterfaceNumber = id; 2763 interface_desc.bInterfaceNumber = id;
2764 2764
2765 /* Enable OS and Extended Properties Feature Descriptor */ 2765 /* Enable OS and Extended Properties Feature Descriptor */
2766 c->cdev->use_os_string = 1; 2766 c->cdev->use_os_string = 1;
2767 f->os_desc_table = &fb_os_desc_table; 2767 f->os_desc_table = &fb_os_desc_table;
2768 f->os_desc_n = 1; 2768 f->os_desc_n = 1;
2769 f->os_desc_table->if_id = id; 2769 f->os_desc_table->if_id = id;
2770 INIT_LIST_HEAD(&fb_os_desc.ext_prop); 2770 INIT_LIST_HEAD(&fb_os_desc.ext_prop);
2771 fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2; 2771 fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2;
2772 fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len; 2772 fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len;
2773 fb_os_desc.ext_prop_count = 1; 2773 fb_os_desc.ext_prop_count = 1;
2774 fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2; 2774 fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2;
2775 fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4; 2775 fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4;
2776 list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop); 2776 list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop);
2777 2777
2778 id = usb_string_id(c->cdev); 2778 id = usb_string_id(c->cdev);
2779 if (id < 0) 2779 if (id < 0)
2780 return id; 2780 return id;
2781 fastboot_string_defs[0].id = id; 2781 fastboot_string_defs[0].id = id;
2782 interface_desc.iInterface = id; 2782 interface_desc.iInterface = id;
2783 2783
2784 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); 2784 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
2785 if (!f_fb->in_ep) 2785 if (!f_fb->in_ep)
2786 return -ENODEV; 2786 return -ENODEV;
2787 f_fb->in_ep->driver_data = c->cdev; 2787 f_fb->in_ep->driver_data = c->cdev;
2788 2788
2789 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); 2789 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
2790 if (!f_fb->out_ep) 2790 if (!f_fb->out_ep)
2791 return -ENODEV; 2791 return -ENODEV;
2792 f_fb->out_ep->driver_data = c->cdev; 2792 f_fb->out_ep->driver_data = c->cdev;
2793 2793
2794 f->descriptors = fb_fs_function; 2794 f->descriptors = fb_fs_function;
2795 2795
2796 if (gadget_is_dualspeed(gadget)) { 2796 if (gadget_is_dualspeed(gadget)) {
2797 /* Assume endpoint addresses are the same for both speeds */ 2797 /* Assume endpoint addresses are the same for both speeds */
2798 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; 2798 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
2799 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; 2799 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
2800 /* copy HS descriptors */ 2800 /* copy HS descriptors */
2801 f->hs_descriptors = fb_hs_function; 2801 f->hs_descriptors = fb_hs_function;
2802 } 2802 }
2803 2803
2804 if (gadget_is_superspeed(gadget)) { 2804 if (gadget_is_superspeed(gadget)) {
2805 ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; 2805 ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
2806 ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; 2806 ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
2807 f->ss_descriptors = fb_ss_function; 2807 f->ss_descriptors = fb_ss_function;
2808 } 2808 }
2809 2809
2810 s = env_get("serial#"); 2810 s = env_get("serial#");
2811 if (s) 2811 if (s)
2812 g_dnl_set_serialnumber((char *)s); 2812 g_dnl_set_serialnumber((char *)s);
2813 2813
2814 return 0; 2814 return 0;
2815 } 2815 }
2816 2816
2817 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) 2817 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
2818 { 2818 {
2819 f->os_desc_table = NULL; 2819 f->os_desc_table = NULL;
2820 list_del(&fb_os_desc.ext_prop); 2820 list_del(&fb_os_desc.ext_prop);
2821 memset(fastboot_func, 0, sizeof(*fastboot_func)); 2821 memset(fastboot_func, 0, sizeof(*fastboot_func));
2822 } 2822 }
2823 2823
2824 static void fastboot_disable(struct usb_function *f) 2824 static void fastboot_disable(struct usb_function *f)
2825 { 2825 {
2826 usb_req *req; 2826 usb_req *req;
2827 struct f_fastboot *f_fb = func_to_fastboot(f); 2827 struct f_fastboot *f_fb = func_to_fastboot(f);
2828 2828
2829 usb_ep_disable(f_fb->out_ep); 2829 usb_ep_disable(f_fb->out_ep);
2830 usb_ep_disable(f_fb->in_ep); 2830 usb_ep_disable(f_fb->in_ep);
2831 2831
2832 if (f_fb->out_req) { 2832 if (f_fb->out_req) {
2833 free(f_fb->out_req->buf); 2833 free(f_fb->out_req->buf);
2834 usb_ep_free_request(f_fb->out_ep, f_fb->out_req); 2834 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
2835 f_fb->out_req = NULL; 2835 f_fb->out_req = NULL;
2836 } 2836 }
2837 if (f_fb->in_req) { 2837 if (f_fb->in_req) {
2838 free(f_fb->in_req->buf); 2838 free(f_fb->in_req->buf);
2839 usb_ep_free_request(f_fb->in_ep, f_fb->in_req); 2839 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
2840 2840
2841 /* disable usb request FIFO */ 2841 /* disable usb request FIFO */
2842 while(f_fb->front != NULL) { 2842 while(f_fb->front != NULL) {
2843 req = f_fb->front; 2843 req = f_fb->front;
2844 f_fb->front = f_fb->front->next; 2844 f_fb->front = f_fb->front->next;
2845 free(req->in_req->buf); 2845 free(req->in_req->buf);
2846 usb_ep_free_request(f_fb->in_ep, req->in_req); 2846 usb_ep_free_request(f_fb->in_ep, req->in_req);
2847 free(req); 2847 free(req);
2848 } 2848 }
2849 2849
2850 f_fb->rear = NULL; 2850 f_fb->rear = NULL;
2851 f_fb->in_req = NULL; 2851 f_fb->in_req = NULL;
2852 } 2852 }
2853 } 2853 }
2854 2854
2855 static struct usb_request *fastboot_start_ep(struct usb_ep *ep) 2855 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
2856 { 2856 {
2857 struct usb_request *req; 2857 struct usb_request *req;
2858 2858
2859 req = usb_ep_alloc_request(ep, 0); 2859 req = usb_ep_alloc_request(ep, 0);
2860 if (!req) 2860 if (!req)
2861 return NULL; 2861 return NULL;
2862 2862
2863 req->length = EP_BUFFER_SIZE; 2863 req->length = EP_BUFFER_SIZE;
2864 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); 2864 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
2865 if (!req->buf) { 2865 if (!req->buf) {
2866 usb_ep_free_request(ep, req); 2866 usb_ep_free_request(ep, req);
2867 return NULL; 2867 return NULL;
2868 } 2868 }
2869 2869
2870 memset(req->buf, 0, req->length); 2870 memset(req->buf, 0, req->length);
2871 return req; 2871 return req;
2872 } 2872 }
2873 2873
2874 static int fastboot_set_alt(struct usb_function *f, 2874 static int fastboot_set_alt(struct usb_function *f,
2875 unsigned interface, unsigned alt) 2875 unsigned interface, unsigned alt)
2876 { 2876 {
2877 int ret; 2877 int ret;
2878 struct usb_composite_dev *cdev = f->config->cdev; 2878 struct usb_composite_dev *cdev = f->config->cdev;
2879 struct usb_gadget *gadget = cdev->gadget; 2879 struct usb_gadget *gadget = cdev->gadget;
2880 struct f_fastboot *f_fb = func_to_fastboot(f); 2880 struct f_fastboot *f_fb = func_to_fastboot(f);
2881 const struct usb_endpoint_descriptor *d; 2881 const struct usb_endpoint_descriptor *d;
2882 2882
2883 debug("%s: func: %s intf: %d alt: %d\n", 2883 debug("%s: func: %s intf: %d alt: %d\n",
2884 __func__, f->name, interface, alt); 2884 __func__, f->name, interface, alt);
2885 2885
2886 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out); 2886 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out);
2887 ret = usb_ep_enable(f_fb->out_ep, d); 2887 ret = usb_ep_enable(f_fb->out_ep, d);
2888 if (ret) { 2888 if (ret) {
2889 puts("failed to enable out ep\n"); 2889 puts("failed to enable out ep\n");
2890 return ret; 2890 return ret;
2891 } 2891 }
2892 2892
2893 f_fb->out_req = fastboot_start_ep(f_fb->out_ep); 2893 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
2894 if (!f_fb->out_req) { 2894 if (!f_fb->out_req) {
2895 puts("failed to alloc out req\n"); 2895 puts("failed to alloc out req\n");
2896 ret = -EINVAL; 2896 ret = -EINVAL;
2897 goto err; 2897 goto err;
2898 } 2898 }
2899 f_fb->out_req->complete = rx_handler_command; 2899 f_fb->out_req->complete = rx_handler_command;
2900 2900
2901 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in); 2901 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in);
2902 ret = usb_ep_enable(f_fb->in_ep, d); 2902 ret = usb_ep_enable(f_fb->in_ep, d);
2903 if (ret) { 2903 if (ret) {
2904 puts("failed to enable in ep\n"); 2904 puts("failed to enable in ep\n");
2905 goto err; 2905 goto err;
2906 } 2906 }
2907 2907
2908 f_fb->in_req = fastboot_start_ep(f_fb->in_ep); 2908 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
2909 if (!f_fb->in_req) { 2909 if (!f_fb->in_req) {
2910 puts("failed alloc req in\n"); 2910 puts("failed alloc req in\n");
2911 ret = -EINVAL; 2911 ret = -EINVAL;
2912 goto err; 2912 goto err;
2913 } 2913 }
2914 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 2914 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
2915 /* 2915 /*
2916 * fastboot host end implement to get data in one bulk package so need 2916 * fastboot host end implement to get data in one bulk package so need
2917 * large buffer for the "fastboot upload" and "fastboot get_staged". 2917 * large buffer for the "fastboot upload" and "fastboot get_staged".
2918 */ 2918 */
2919 if (f_fb->in_req->buf) 2919 if (f_fb->in_req->buf)
2920 free(f_fb->in_req->buf); 2920 free(f_fb->in_req->buf);
2921 f_fb->in_req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE * 32); 2921 f_fb->in_req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE * 32);
2922 #endif 2922 #endif
2923 f_fb->in_req->complete = fastboot_complete; 2923 f_fb->in_req->complete = fastboot_complete;
2924 2924
2925 f_fb->front = f_fb->rear = NULL; 2925 f_fb->front = f_fb->rear = NULL;
2926 2926
2927 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); 2927 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
2928 if (ret) 2928 if (ret)
2929 goto err; 2929 goto err;
2930 2930
2931 return 0; 2931 return 0;
2932 err: 2932 err:
2933 fastboot_disable(f); 2933 fastboot_disable(f);
2934 return ret; 2934 return ret;
2935 } 2935 }
2936 2936
2937 static int fastboot_add(struct usb_configuration *c) 2937 static int fastboot_add(struct usb_configuration *c)
2938 { 2938 {
2939 struct f_fastboot *f_fb = fastboot_func; 2939 struct f_fastboot *f_fb = fastboot_func;
2940 int status; 2940 int status;
2941 2941
2942 debug("%s: cdev: 0x%p\n", __func__, c->cdev); 2942 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
2943 2943
2944 if (!f_fb) { 2944 if (!f_fb) {
2945 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); 2945 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
2946 if (!f_fb) 2946 if (!f_fb)
2947 return -ENOMEM; 2947 return -ENOMEM;
2948 2948
2949 fastboot_func = f_fb; 2949 fastboot_func = f_fb;
2950 memset(f_fb, 0, sizeof(*f_fb)); 2950 memset(f_fb, 0, sizeof(*f_fb));
2951 } 2951 }
2952 2952
2953 f_fb->usb_function.name = "f_fastboot"; 2953 f_fb->usb_function.name = "f_fastboot";
2954 f_fb->usb_function.bind = fastboot_bind; 2954 f_fb->usb_function.bind = fastboot_bind;
2955 f_fb->usb_function.unbind = fastboot_unbind; 2955 f_fb->usb_function.unbind = fastboot_unbind;
2956 f_fb->usb_function.set_alt = fastboot_set_alt; 2956 f_fb->usb_function.set_alt = fastboot_set_alt;
2957 f_fb->usb_function.disable = fastboot_disable; 2957 f_fb->usb_function.disable = fastboot_disable;
2958 f_fb->usb_function.strings = fastboot_strings; 2958 f_fb->usb_function.strings = fastboot_strings;
2959 2959
2960 status = usb_add_function(c, &f_fb->usb_function); 2960 status = usb_add_function(c, &f_fb->usb_function);
2961 if (status) { 2961 if (status) {
2962 free(f_fb); 2962 free(f_fb);
2963 fastboot_func = NULL; 2963 fastboot_func = NULL;
2964 } 2964 }
2965 2965
2966 return status; 2966 return status;
2967 } 2967 }
2968 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); 2968 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
2969 2969
2970 static int fastboot_tx_write_more(const char *buffer) 2970 static int fastboot_tx_write_more(const char *buffer)
2971 { 2971 {
2972 int ret = 0; 2972 int ret = 0;
2973 2973
2974 /* alloc usb request FIFO node */ 2974 /* alloc usb request FIFO node */
2975 usb_req *req = (usb_req *)malloc(sizeof(usb_req)); 2975 usb_req *req = (usb_req *)malloc(sizeof(usb_req));
2976 if (!req) { 2976 if (!req) {
2977 printf("failed alloc usb req!\n"); 2977 printf("failed alloc usb req!\n");
2978 return -ENOMEM; 2978 return -ENOMEM;
2979 } 2979 }
2980 2980
2981 /* usb request node FIFO enquene */ 2981 /* usb request node FIFO enquene */
2982 if ((fastboot_func->front == NULL) && (fastboot_func->rear == NULL)) { 2982 if ((fastboot_func->front == NULL) && (fastboot_func->rear == NULL)) {
2983 fastboot_func->front = fastboot_func->rear = req; 2983 fastboot_func->front = fastboot_func->rear = req;
2984 req->next = NULL; 2984 req->next = NULL;
2985 } else { 2985 } else {
2986 fastboot_func->rear->next = req; 2986 fastboot_func->rear->next = req;
2987 fastboot_func->rear = req; 2987 fastboot_func->rear = req;
2988 req->next = NULL; 2988 req->next = NULL;
2989 } 2989 }
2990 2990
2991 /* alloc in request for current node */ 2991 /* alloc in request for current node */
2992 req->in_req = fastboot_start_ep(fastboot_func->in_ep); 2992 req->in_req = fastboot_start_ep(fastboot_func->in_ep);
2993 if (!req->in_req) { 2993 if (!req->in_req) {
2994 printf("failed alloc req in\n"); 2994 printf("failed alloc req in\n");
2995 fastboot_disable(&(fastboot_func->usb_function)); 2995 fastboot_disable(&(fastboot_func->usb_function));
2996 return -EINVAL; 2996 return -EINVAL;
2997 } 2997 }
2998 req->in_req->complete = fastboot_fifo_complete; 2998 req->in_req->complete = fastboot_fifo_complete;
2999 2999
3000 memcpy(req->in_req->buf, buffer, strlen(buffer)); 3000 memcpy(req->in_req->buf, buffer, strlen(buffer));
3001 req->in_req->length = strlen(buffer); 3001 req->in_req->length = strlen(buffer);
3002 3002
3003 ret = usb_ep_queue(fastboot_func->in_ep, req->in_req, 0); 3003 ret = usb_ep_queue(fastboot_func->in_ep, req->in_req, 0);
3004 if (ret) { 3004 if (ret) {
3005 printf("Error %d on queue\n", ret); 3005 printf("Error %d on queue\n", ret);
3006 return -EINVAL; 3006 return -EINVAL;
3007 } 3007 }
3008 3008
3009 ret = 0; 3009 ret = 0;
3010 return ret; 3010 return ret;
3011 } 3011 }
3012 3012
3013 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) 3013 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3014 { 3014 {
3015 struct usb_request *in_req = fastboot_func->in_req; 3015 struct usb_request *in_req = fastboot_func->in_req;
3016 int ret; 3016 int ret;
3017 3017
3018 memcpy(in_req->buf, buffer, buffer_size); 3018 memcpy(in_req->buf, buffer, buffer_size);
3019 in_req->length = buffer_size; 3019 in_req->length = buffer_size;
3020 3020
3021 usb_ep_dequeue(fastboot_func->in_ep, in_req); 3021 usb_ep_dequeue(fastboot_func->in_ep, in_req);
3022 3022
3023 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); 3023 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
3024 if (ret) 3024 if (ret)
3025 printf("Error %d on queue\n", ret); 3025 printf("Error %d on queue\n", ret);
3026 return 0; 3026 return 0;
3027 } 3027 }
3028 3028
3029 static int fastboot_tx_write_str(const char *buffer) 3029 static int fastboot_tx_write_str(const char *buffer)
3030 { 3030 {
3031 return fastboot_tx_write(buffer, strlen(buffer)); 3031 return fastboot_tx_write(buffer, strlen(buffer));
3032 } 3032 }
3033 3033
3034 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) 3034 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
3035 { 3035 {
3036 do_reset(NULL, 0, 0, NULL); 3036 do_reset(NULL, 0, 0, NULL);
3037 } 3037 }
3038 3038
3039 int __weak fb_set_reboot_flag(void) 3039 int __weak fb_set_reboot_flag(void)
3040 { 3040 {
3041 return -ENOSYS; 3041 return -ENOSYS;
3042 } 3042 }
3043 3043
3044 static void cb_reboot(struct usb_ep *ep, struct usb_request *req) 3044 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
3045 { 3045 {
3046 char *cmd = req->buf; 3046 char *cmd = req->buf;
3047 if (!strcmp_l1("reboot-bootloader", cmd)) { 3047 if (!strcmp_l1("reboot-bootloader", cmd)) {
3048 if (fb_set_reboot_flag()) { 3048 if (fb_set_reboot_flag()) {
3049 fastboot_tx_write_str("FAILCannot set reboot flag"); 3049 fastboot_tx_write_str("FAILCannot set reboot flag");
3050 return; 3050 return;
3051 } 3051 }
3052 } 3052 }
3053 fastboot_func->in_req->complete = compl_do_reset; 3053 fastboot_func->in_req->complete = compl_do_reset;
3054 fastboot_tx_write_str("OKAY"); 3054 fastboot_tx_write_str("OKAY");
3055 } 3055 }
3056 3056
3057 static int strcmp_l1(const char *s1, const char *s2) 3057 static int strcmp_l1(const char *s1, const char *s2)
3058 { 3058 {
3059 if (!s1 || !s2) 3059 if (!s1 || !s2)
3060 return -1; 3060 return -1;
3061 return strncmp(s1, s2, strlen(s1)); 3061 return strncmp(s1, s2, strlen(s1));
3062 } 3062 }
3063 3063
3064 #ifdef CONFIG_FSL_FASTBOOT 3064 #ifdef CONFIG_FSL_FASTBOOT
3065 static bool is_slotvar(char *cmd) 3065 static bool is_slotvar(char *cmd)
3066 { 3066 {
3067 assert(cmd != NULL); 3067 assert(cmd != NULL);
3068 if (!strcmp_l1("has-slot:", cmd) || 3068 if (!strcmp_l1("has-slot:", cmd) ||
3069 !strcmp_l1("slot-successful:", cmd) || 3069 !strcmp_l1("slot-successful:", cmd) ||
3070 !strcmp_l1("slot-count", cmd) || 3070 !strcmp_l1("slot-count", cmd) ||
3071 !strcmp_l1("slot-suffixes", cmd) || 3071 !strcmp_l1("slot-suffixes", cmd) ||
3072 !strcmp_l1("current-slot", cmd) || 3072 !strcmp_l1("current-slot", cmd) ||
3073 !strcmp_l1("slot-unbootable:", cmd) || 3073 !strcmp_l1("slot-unbootable:", cmd) ||
3074 !strcmp_l1("slot-retry-count:", cmd)) 3074 !strcmp_l1("slot-retry-count:", cmd))
3075 return true; 3075 return true;
3076 return false; 3076 return false;
3077 } 3077 }
3078 3078
3079 static char *get_serial(void) 3079 static char *get_serial(void)
3080 { 3080 {
3081 #ifdef CONFIG_SERIAL_TAG 3081 #ifdef CONFIG_SERIAL_TAG
3082 struct tag_serialnr serialnr; 3082 struct tag_serialnr serialnr;
3083 static char serial[32]; 3083 static char serial[32];
3084 get_board_serial(&serialnr); 3084 get_board_serial(&serialnr);
3085 sprintf(serial, "%08x%08x", serialnr.high, serialnr.low); 3085 sprintf(serial, "%08x%08x", serialnr.high, serialnr.low);
3086 return serial; 3086 return serial;
3087 #else 3087 #else
3088 return NULL; 3088 return NULL;
3089 #endif 3089 #endif
3090 } 3090 }
3091 3091
3092 #if !defined(PRODUCT_NAME) 3092 #if !defined(PRODUCT_NAME)
3093 #define PRODUCT_NAME "NXP i.MX" 3093 #define PRODUCT_NAME "NXP i.MX"
3094 #endif 3094 #endif
3095 3095
3096 #if !defined(VARIANT_NAME) 3096 #if !defined(VARIANT_NAME)
3097 #define VARIANT_NAME "NXP i.MX" 3097 #define VARIANT_NAME "NXP i.MX"
3098 #endif 3098 #endif
3099 3099
3100 static int get_block_size(void) { 3100 static int get_block_size(void) {
3101 int dev_no = 0; 3101 int dev_no = 0;
3102 struct blk_desc *dev_desc; 3102 struct blk_desc *dev_desc;
3103 3103
3104 dev_no = fastboot_devinfo.dev_id; 3104 dev_no = fastboot_devinfo.dev_id;
3105 dev_desc = blk_get_dev(fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", dev_no); 3105 dev_desc = blk_get_dev(fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", dev_no);
3106 if (NULL == dev_desc) { 3106 if (NULL == dev_desc) {
3107 printf("** Block device %s %d not supported\n", 3107 printf("** Block device %s %d not supported\n",
3108 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc", 3108 fastboot_devinfo.type == DEV_SATA ? "sata" : "mmc",
3109 dev_no); 3109 dev_no);
3110 return 0; 3110 return 0;
3111 } 3111 }
3112 return dev_desc->blksz; 3112 return dev_desc->blksz;
3113 } 3113 }
3114 3114
3115 static bool is_exist(char (*partition_base_name)[16], char *buffer, int count) 3115 static bool is_exist(char (*partition_base_name)[16], char *buffer, int count)
3116 { 3116 {
3117 int n; 3117 int n;
3118 3118
3119 for (n = 0; n < count; n++) { 3119 for (n = 0; n < count; n++) {
3120 if (!strcmp(partition_base_name[n],buffer)) 3120 if (!strcmp(partition_base_name[n],buffer))
3121 return true; 3121 return true;
3122 } 3122 }
3123 return false; 3123 return false;
3124 } 3124 }
3125 /*get partition base name from gpt without "_a/_b"*/ 3125 /*get partition base name from gpt without "_a/_b"*/
3126 static int get_partition_base_name(char (*partition_base_name)[16]) 3126 static int get_partition_base_name(char (*partition_base_name)[16])
3127 { 3127 {
3128 int n = 0; 3128 int n = 0;
3129 int count = 0; 3129 int count = 0;
3130 char *ptr1, *ptr2; 3130 char *ptr1, *ptr2;
3131 char buffer[20]; 3131 char buffer[20];
3132 3132
3133 for (n = 0; n < g_pcount; n++) { 3133 for (n = 0; n < g_pcount; n++) {
3134 strcpy(buffer,g_ptable[n].name); 3134 strcpy(buffer,g_ptable[n].name);
3135 ptr1 = strstr(buffer, "_a"); 3135 ptr1 = strstr(buffer, "_a");
3136 ptr2 = strstr(buffer, "_b"); 3136 ptr2 = strstr(buffer, "_b");
3137 if (ptr1 != NULL) { 3137 if (ptr1 != NULL) {
3138 *ptr1 = '\0'; 3138 *ptr1 = '\0';
3139 if (!is_exist(partition_base_name,buffer,count)) { 3139 if (!is_exist(partition_base_name,buffer,count)) {
3140 strcpy(partition_base_name[count++],buffer); 3140 strcpy(partition_base_name[count++],buffer);
3141 } 3141 }
3142 } else if (ptr2 != NULL) { 3142 } else if (ptr2 != NULL) {
3143 *ptr2 = '\0'; 3143 *ptr2 = '\0';
3144 if (!is_exist(partition_base_name,buffer,count)) { 3144 if (!is_exist(partition_base_name,buffer,count)) {
3145 strcpy(partition_base_name[count++],buffer); 3145 strcpy(partition_base_name[count++],buffer);
3146 } 3146 }
3147 } else { 3147 } else {
3148 strcpy(partition_base_name[count++],buffer); 3148 strcpy(partition_base_name[count++],buffer);
3149 } 3149 }
3150 } 3150 }
3151 return count; 3151 return count;
3152 } 3152 }
3153 3153
3154 static bool is_slot(void) 3154 static bool is_slot(void)
3155 { 3155 {
3156 char slot_suffix[2][5] = {"_a","_b"}; 3156 char slot_suffix[2][5] = {"_a","_b"};
3157 int n; 3157 int n;
3158 3158
3159 for (n = 0; n < g_pcount; n++) { 3159 for (n = 0; n < g_pcount; n++) {
3160 if (strstr(g_ptable[n].name, slot_suffix[0]) || 3160 if (strstr(g_ptable[n].name, slot_suffix[0]) ||
3161 strstr(g_ptable[n].name, slot_suffix[1])) 3161 strstr(g_ptable[n].name, slot_suffix[1]))
3162 return true; 3162 return true;
3163 } 3163 }
3164 return false; 3164 return false;
3165 } 3165 }
3166 #ifdef CONFIG_IMX_TRUSTY_OS 3166 #ifdef CONFIG_IMX_TRUSTY_OS
3167 static void uuid_hex2string(uint8_t *uuid, char* buf, uint32_t uuid_len, uint32_t uuid_strlen) { 3167 static void uuid_hex2string(uint8_t *uuid, char* buf, uint32_t uuid_len, uint32_t uuid_strlen) {
3168 uint32_t i; 3168 uint32_t i;
3169 if (!uuid || !buf) 3169 if (!uuid || !buf)
3170 return; 3170 return;
3171 char *cp = buf; 3171 char *cp = buf;
3172 char *buf_end = buf + uuid_strlen; 3172 char *buf_end = buf + uuid_strlen;
3173 for (i = 0; i < uuid_len; i++) { 3173 for (i = 0; i < uuid_len; i++) {
3174 cp += snprintf(cp, buf_end - cp, "%02x", uuid[i]); 3174 cp += snprintf(cp, buf_end - cp, "%02x", uuid[i]);
3175 } 3175 }
3176 } 3176 }
3177 #endif 3177 #endif
3178 3178
3179 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M) 3179 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M)
3180 int get_imx8m_baseboard_id(void); 3180 int get_imx8m_baseboard_id(void);
3181 #endif 3181 #endif
3182 3182
3183 static int get_single_var(char *cmd, char *response) 3183 static int get_single_var(char *cmd, char *response)
3184 { 3184 {
3185 char *str = cmd; 3185 char *str = cmd;
3186 int chars_left; 3186 int chars_left;
3187 const char *s; 3187 const char *s;
3188 struct mmc *mmc; 3188 struct mmc *mmc;
3189 int mmc_dev_no; 3189 int mmc_dev_no;
3190 int blksz; 3190 int blksz;
3191 3191
3192 chars_left = FASTBOOT_RESPONSE_LEN - strlen(response) - 1; 3192 chars_left = FASTBOOT_RESPONSE_LEN - strlen(response) - 1;
3193 3193
3194 if ((str = strstr(cmd, "partition-size:"))) { 3194 if ((str = strstr(cmd, "partition-size:"))) {
3195 str +=strlen("partition-size:"); 3195 str +=strlen("partition-size:");
3196 struct fastboot_ptentry* fb_part; 3196 struct fastboot_ptentry* fb_part;
3197 fb_part = fastboot_flash_find_ptn(str); 3197 fb_part = fastboot_flash_find_ptn(str);
3198 if (!fb_part) { 3198 if (!fb_part) {
3199 strncat(response, "Wrong partition name.", chars_left); 3199 strncat(response, "Wrong partition name.", chars_left);
3200 fastboot_flash_dump_ptn(); 3200 fastboot_flash_dump_ptn();
3201 return -1; 3201 return -1;
3202 } else { 3202 } else {
3203 snprintf(response + strlen(response), chars_left, 3203 snprintf(response + strlen(response), chars_left,
3204 "0x%llx", 3204 "0x%llx",
3205 (uint64_t)fb_part->length * get_block_size()); 3205 (uint64_t)fb_part->length * get_block_size());
3206 } 3206 }
3207 } else if ((str = strstr(cmd, "partition-type:"))) { 3207 } else if ((str = strstr(cmd, "partition-type:"))) {
3208 str +=strlen("partition-type:"); 3208 str +=strlen("partition-type:");
3209 struct fastboot_ptentry* fb_part; 3209 struct fastboot_ptentry* fb_part;
3210 fb_part = fastboot_flash_find_ptn(str); 3210 fb_part = fastboot_flash_find_ptn(str);
3211 if (!fb_part) { 3211 if (!fb_part) {
3212 strncat(response, "Wrong partition name.", chars_left); 3212 strncat(response, "Wrong partition name.", chars_left);
3213 fastboot_flash_dump_ptn(); 3213 fastboot_flash_dump_ptn();
3214 return -1; 3214 return -1;
3215 } else { 3215 } else {
3216 strncat(response, fb_part->fstype, chars_left); 3216 strncat(response, fb_part->fstype, chars_left);
3217 } 3217 }
3218 } else if (!strcmp_l1("version-baseband", cmd)) { 3218 } else if (!strcmp_l1("version-baseband", cmd)) {
3219 strncat(response, "N/A", chars_left); 3219 strncat(response, "N/A", chars_left);
3220 } else if (!strcmp_l1("version-bootloader", cmd) || 3220 } else if (!strcmp_l1("version-bootloader", cmd) ||
3221 !strcmp_l1("bootloader-version", cmd)) { 3221 !strcmp_l1("bootloader-version", cmd)) {
3222 strncat(response, U_BOOT_VERSION, chars_left); 3222 strncat(response, U_BOOT_VERSION, chars_left);
3223 } else if (!strcmp_l1("version", cmd)) { 3223 } else if (!strcmp_l1("version", cmd)) {
3224 strncat(response, FASTBOOT_VERSION, chars_left); 3224 strncat(response, FASTBOOT_VERSION, chars_left);
3225 } else if (!strcmp_l1("battery-voltage", cmd)) { 3225 } else if (!strcmp_l1("battery-voltage", cmd)) {
3226 strncat(response, "0mV", chars_left); 3226 strncat(response, "0mV", chars_left);
3227 } else if (!strcmp_l1("battery-soc-ok", cmd)) { 3227 } else if (!strcmp_l1("battery-soc-ok", cmd)) {
3228 strncat(response, "yes", chars_left); 3228 strncat(response, "yes", chars_left);
3229 } else if (!strcmp_l1("variant", cmd)) { 3229 } else if (!strcmp_l1("variant", cmd)) {
3230 strncat(response, VARIANT_NAME, chars_left); 3230 strncat(response, VARIANT_NAME, chars_left);
3231 } else if (!strcmp_l1("off-mode-charge", cmd)) { 3231 } else if (!strcmp_l1("off-mode-charge", cmd)) {
3232 strncat(response, "1", chars_left); 3232 strncat(response, "1", chars_left);
3233 } else if (!strcmp_l1("downloadsize", cmd) || 3233 } else if (!strcmp_l1("downloadsize", cmd) ||
3234 !strcmp_l1("max-download-size", cmd)) { 3234 !strcmp_l1("max-download-size", cmd)) {
3235 3235
3236 snprintf(response + strlen(response), chars_left, "0x%x", CONFIG_FASTBOOT_BUF_SIZE); 3236 snprintf(response + strlen(response), chars_left, "0x%x", CONFIG_FASTBOOT_BUF_SIZE);
3237 } else if (!strcmp_l1("erase-block-size", cmd)) { 3237 } else if (!strcmp_l1("erase-block-size", cmd)) {
3238 mmc_dev_no = mmc_get_env_dev(); 3238 mmc_dev_no = mmc_get_env_dev();
3239 mmc = find_mmc_device(mmc_dev_no); 3239 mmc = find_mmc_device(mmc_dev_no);
3240 blksz = get_block_size(); 3240 blksz = get_block_size();
3241 snprintf(response + strlen(response), chars_left, "0x%x", 3241 snprintf(response + strlen(response), chars_left, "0x%x",
3242 (blksz * mmc->erase_grp_size)); 3242 (blksz * mmc->erase_grp_size));
3243 } else if (!strcmp_l1("logical-block-size", cmd)) { 3243 } else if (!strcmp_l1("logical-block-size", cmd)) {
3244 blksz = get_block_size(); 3244 blksz = get_block_size();
3245 snprintf(response + strlen(response), chars_left, "0x%x", blksz); 3245 snprintf(response + strlen(response), chars_left, "0x%x", blksz);
3246 } else if (!strcmp_l1("serialno", cmd)) { 3246 } else if (!strcmp_l1("serialno", cmd)) {
3247 s = get_serial(); 3247 s = get_serial();
3248 if (s) 3248 if (s)
3249 strncat(response, s, chars_left); 3249 strncat(response, s, chars_left);
3250 else { 3250 else {
3251 strncat(response, "FAILValue not set", chars_left); 3251 strncat(response, "FAILValue not set", chars_left);
3252 return -1; 3252 return -1;
3253 } 3253 }
3254 } else if (!strcmp_l1("product", cmd)) { 3254 } else if (!strcmp_l1("product", cmd)) {
3255 strncat(response, PRODUCT_NAME, chars_left); 3255 strncat(response, PRODUCT_NAME, chars_left);
3256 } 3256 }
3257 #ifdef CONFIG_IMX_TRUSTY_OS 3257 #ifdef CONFIG_IMX_TRUSTY_OS
3258 else if(!strcmp_l1("at-attest-uuid", cmd)) { 3258 else if(!strcmp_l1("at-attest-uuid", cmd)) {
3259 char *uuid; 3259 char *uuid;
3260 char uuid_str[ATAP_UUID_STR_SIZE]; 3260 char uuid_str[ATAP_UUID_STR_SIZE];
3261 if (trusty_atap_read_uuid_str(&uuid)) { 3261 if (trusty_atap_read_uuid_str(&uuid)) {
3262 printf("ERROR read uuid failed!\n"); 3262 printf("ERROR read uuid failed!\n");
3263 strncat(response, "FAILCannot get uuid!", chars_left); 3263 strncat(response, "FAILCannot get uuid!", chars_left);
3264 return -1; 3264 return -1;
3265 } else { 3265 } else {
3266 uuid_hex2string((uint8_t*)uuid, uuid_str,ATAP_UUID_SIZE, ATAP_UUID_STR_SIZE); 3266 uuid_hex2string((uint8_t*)uuid, uuid_str,ATAP_UUID_SIZE, ATAP_UUID_STR_SIZE);
3267 strncat(response, uuid_str, chars_left); 3267 strncat(response, uuid_str, chars_left);
3268 trusty_free(uuid); 3268 trusty_free(uuid);
3269 } 3269 }
3270 } 3270 }
3271 else if(!strcmp_l1("at-attest-dh", cmd)) { 3271 else if(!strcmp_l1("at-attest-dh", cmd)) {
3272 strncat(response, "1:P256,2:curve25519", chars_left); 3272 strncat(response, "1:P256,2:curve25519", chars_left);
3273 } 3273 }
3274 #endif 3274 #endif
3275 #ifdef CONFIG_FASTBOOT_LOCK 3275 #ifdef CONFIG_FASTBOOT_LOCK
3276 else if (!strcmp_l1("secure", cmd)) { 3276 else if (!strcmp_l1("secure", cmd)) {
3277 strncat(response, FASTBOOT_VAR_YES, chars_left); 3277 strncat(response, FASTBOOT_VAR_YES, chars_left);
3278 } else if (!strcmp_l1("unlocked",cmd)){ 3278 } else if (!strcmp_l1("unlocked",cmd)){
3279 int status = fastboot_get_lock_stat(); 3279 int status = fastboot_get_lock_stat();
3280 if (status == FASTBOOT_UNLOCK) { 3280 if (status == FASTBOOT_UNLOCK) {
3281 strncat(response, FASTBOOT_VAR_YES, chars_left); 3281 strncat(response, FASTBOOT_VAR_YES, chars_left);
3282 } else { 3282 } else {
3283 strncat(response, FASTBOOT_VAR_NO, chars_left); 3283 strncat(response, FASTBOOT_VAR_NO, chars_left);
3284 } 3284 }
3285 } 3285 }
3286 #else 3286 #else
3287 else if (!strcmp_l1("secure", cmd)) { 3287 else if (!strcmp_l1("secure", cmd)) {
3288 strncat(response, FASTBOOT_VAR_NO, chars_left); 3288 strncat(response, FASTBOOT_VAR_NO, chars_left);
3289 } else if (!strcmp_l1("unlocked",cmd)) { 3289 } else if (!strcmp_l1("unlocked",cmd)) {
3290 strncat(response, FASTBOOT_VAR_NO, chars_left); 3290 strncat(response, FASTBOOT_VAR_NO, chars_left);
3291 } 3291 }
3292 #endif 3292 #endif
3293 else if (is_slotvar(cmd)) { 3293 else if (is_slotvar(cmd)) {
3294 #ifdef CONFIG_AVB_SUPPORT 3294 #ifdef CONFIG_AVB_SUPPORT
3295 if (get_slotvar_avb(&fsl_avb_ab_ops, cmd, 3295 if (get_slotvar_avb(&fsl_avb_ab_ops, cmd,
3296 response + strlen(response), chars_left + 1) < 0) 3296 response + strlen(response), chars_left + 1) < 0)
3297 return -1; 3297 return -1;
3298 #else 3298 #else
3299 strncat(response, FASTBOOT_VAR_NO, chars_left); 3299 strncat(response, FASTBOOT_VAR_NO, chars_left);
3300 #endif 3300 #endif
3301 } 3301 }
3302 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M) 3302 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) && defined(CONFIG_ARCH_IMX8M)
3303 else if (!strcmp_l1("baseboard_id", cmd)) { 3303 else if (!strcmp_l1("baseboard_id", cmd)) {
3304 int baseboard_id; 3304 int baseboard_id;
3305 3305
3306 baseboard_id = get_imx8m_baseboard_id(); 3306 baseboard_id = get_imx8m_baseboard_id();
3307 if (baseboard_id < 0) { 3307 if (baseboard_id < 0) {
3308 printf("Get baseboard id failed!\n"); 3308 printf("Get baseboard id failed!\n");
3309 strncat(response, "Get baseboard id failed!", chars_left); 3309 strncat(response, "Get baseboard id failed!", chars_left);
3310 return -1; 3310 return -1;
3311 } else 3311 } else
3312 snprintf(response + strlen(response), chars_left, "0x%x", baseboard_id); 3312 snprintf(response + strlen(response), chars_left, "0x%x", baseboard_id);
3313 } 3313 }
3314 #endif 3314 #endif
3315 #ifdef CONFIG_AVB_ATX 3315 #ifdef CONFIG_AVB_ATX
3316 else if (!strcmp_l1("bootloader-locked", cmd)) { 3316 else if (!strcmp_l1("bootloader-locked", cmd)) {
3317 3317
3318 /* Below is basically copied from is_hab_enabled() */ 3318 /* Below is basically copied from is_hab_enabled() */
3319 struct imx_sec_config_fuse_t *fuse = 3319 struct imx_sec_config_fuse_t *fuse =
3320 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse; 3320 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
3321 uint32_t reg; 3321 uint32_t reg;
3322 int ret; 3322 int ret;
3323 3323
3324 /* Read the secure boot status from fuse. */ 3324 /* Read the secure boot status from fuse. */
3325 ret = fuse_read(fuse->bank, fuse->word, &reg); 3325 ret = fuse_read(fuse->bank, fuse->word, &reg);
3326 if (ret) { 3326 if (ret) {
3327 printf("\nSecure boot fuse read error!\n"); 3327 printf("\nSecure boot fuse read error!\n");
3328 strncat(response, "Secure boot fuse read error!", chars_left); 3328 strncat(response, "Secure boot fuse read error!", chars_left);
3329 return -1; 3329 return -1;
3330 } 3330 }
3331 /* Check if the secure boot bit is enabled */ 3331 /* Check if the secure boot bit is enabled */
3332 if ((reg & 0x2000000) == 0x2000000) 3332 if ((reg & 0x2000000) == 0x2000000)
3333 strncat(response, "1", chars_left); 3333 strncat(response, "1", chars_left);
3334 else 3334 else
3335 strncat(response, "0", chars_left); 3335 strncat(response, "0", chars_left);
3336 } else if (!strcmp_l1("bootloader-min-versions", cmd)) { 3336 } else if (!strcmp_l1("bootloader-min-versions", cmd)) {
3337 #ifndef CONFIG_ARM64 3337 #ifndef CONFIG_ARM64
3338 /* We don't support bootloader rbindex protection for 3338 /* We don't support bootloader rbindex protection for
3339 * ARM32(like imx7d) and the format is: "bootloader,tee". */ 3339 * ARM32(like imx7d) and the format is: "bootloader,tee". */
3340 strncat(response, "-1,-1", chars_left); 3340 strncat(response, "-1,-1", chars_left);
3341 3341
3342 #elif defined(CONFIG_DUAL_BOOTLOADER) 3342 #elif defined(CONFIG_DUAL_BOOTLOADER)
3343 /* Rbindex protection for bootloader is supported only when the 3343 /* Rbindex protection for bootloader is supported only when the
3344 * 'dual bootloader' feature is enabled. U-boot will get the rbindx 3344 * 'dual bootloader' feature is enabled. U-boot will get the rbindx
3345 * from RAM which is passed by spl because we can only get the rbindex 3345 * from RAM which is passed by spl because we can only get the rbindex
3346 * at spl stage. The format in this case is: "spl,atf,tee,u-boot". 3346 * at spl stage. The format in this case is: "spl,atf,tee,u-boot".
3347 */ 3347 */
3348 struct bl_rbindex_package *bl_rbindex; 3348 struct bl_rbindex_package *bl_rbindex;
3349 uint32_t rbindex; 3349 uint32_t rbindex;
3350 3350
3351 bl_rbindex = (struct bl_rbindex_package *)BL_RBINDEX_LOAD_ADDR; 3351 bl_rbindex = (struct bl_rbindex_package *)BL_RBINDEX_LOAD_ADDR;
3352 if (!strncmp(bl_rbindex->magic, BL_RBINDEX_MAGIC, 3352 if (!strncmp(bl_rbindex->magic, BL_RBINDEX_MAGIC,
3353 BL_RBINDEX_MAGIC_LEN)) { 3353 BL_RBINDEX_MAGIC_LEN)) {
3354 rbindex = bl_rbindex->rbindex; 3354 rbindex = bl_rbindex->rbindex;
3355 snprintf(response + strlen(response), chars_left, 3355 snprintf(response + strlen(response), chars_left,
3356 "-1,%d,%d,%d",rbindex, rbindex, rbindex); 3356 "-1,%d,%d,%d",rbindex, rbindex, rbindex);
3357 } else { 3357 } else {
3358 printf("Error bootloader rbindex magic!\n"); 3358 printf("Error bootloader rbindex magic!\n");
3359 strncat(response, "Get bootloader rbindex fail!", chars_left); 3359 strncat(response, "Get bootloader rbindex fail!", chars_left);
3360 return -1; 3360 return -1;
3361 } 3361 }
3362 #else 3362 #else
3363 /* Return -1 for all partition if 'dual bootloader' feature 3363 /* Return -1 for all partition if 'dual bootloader' feature
3364 * is not enabled */ 3364 * is not enabled */
3365 strncat(response, "-1,-1,-1,-1", chars_left); 3365 strncat(response, "-1,-1,-1,-1", chars_left);
3366 #endif 3366 #endif
3367 } else if (!strcmp_l1("avb-perm-attr-set", cmd)) { 3367 } else if (!strcmp_l1("avb-perm-attr-set", cmd)) {
3368 if (perm_attr_are_fused()) 3368 if (perm_attr_are_fused())
3369 strncat(response, "1", chars_left); 3369 strncat(response, "1", chars_left);
3370 else 3370 else
3371 strncat(response, "0", chars_left); 3371 strncat(response, "0", chars_left);
3372 } else if (!strcmp_l1("avb-locked", cmd)) { 3372 } else if (!strcmp_l1("avb-locked", cmd)) {
3373 FbLockState status; 3373 FbLockState status;
3374 3374
3375 status = fastboot_get_lock_stat(); 3375 status = fastboot_get_lock_stat();
3376 if (status == FASTBOOT_LOCK) 3376 if (status == FASTBOOT_LOCK)
3377 strncat(response, "1", chars_left); 3377 strncat(response, "1", chars_left);
3378 else if (status == FASTBOOT_UNLOCK) 3378 else if (status == FASTBOOT_UNLOCK)
3379 strncat(response, "0", chars_left); 3379 strncat(response, "0", chars_left);
3380 else { 3380 else {
3381 printf("Get lock state error!\n"); 3381 printf("Get lock state error!\n");
3382 strncat(response, "Get lock state failed!", chars_left); 3382 strncat(response, "Get lock state failed!", chars_left);
3383 return -1; 3383 return -1;
3384 } 3384 }
3385 } else if (!strcmp_l1("avb-unlock-disabled", cmd)) { 3385 } else if (!strcmp_l1("avb-unlock-disabled", cmd)) {
3386 if (at_unlock_vboot_is_disabled()) 3386 if (at_unlock_vboot_is_disabled())
3387 strncat(response, "1", chars_left); 3387 strncat(response, "1", chars_left);
3388 else 3388 else
3389 strncat(response, "0", chars_left); 3389 strncat(response, "0", chars_left);
3390 } else if (!strcmp_l1("avb-min-versions", cmd)) { 3390 } else if (!strcmp_l1("avb-min-versions", cmd)) {
3391 int i = 0; 3391 int i = 0;
3392 /* rbindex location/value can be very large 3392 /* rbindex location/value can be very large
3393 * number so we reserve enough space here. 3393 * number so we reserve enough space here.
3394 */ 3394 */
3395 char buffer[35]; 3395 char buffer[35];
3396 uint32_t rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 2]; 3396 uint32_t rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 2];
3397 uint32_t location; 3397 uint32_t location;
3398 uint64_t rbindex; 3398 uint64_t rbindex;
3399 3399
3400 memset(buffer, '\0', sizeof(buffer)); 3400 memset(buffer, '\0', sizeof(buffer));
3401 3401
3402 /* Set rbindex locations. */ 3402 /* Set rbindex locations. */
3403 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) 3403 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++)
3404 rbindex_location[i] = i; 3404 rbindex_location[i] = i;
3405 3405
3406 /* Set Android Things key version rbindex locations */ 3406 /* Set Android Things key version rbindex locations */
3407 rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS] 3407 rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS]
3408 = AVB_ATX_PIK_VERSION_LOCATION; 3408 = AVB_ATX_PIK_VERSION_LOCATION;
3409 rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 1] 3409 rbindex_location[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 1]
3410 = AVB_ATX_PSK_VERSION_LOCATION; 3410 = AVB_ATX_PSK_VERSION_LOCATION;
3411 3411
3412 /* Read rollback index and set the reponse*/ 3412 /* Read rollback index and set the reponse*/
3413 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 2; i++) { 3413 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 2; i++) {
3414 location = rbindex_location[i]; 3414 location = rbindex_location[i];
3415 if (fsl_avb_ops.read_rollback_index(&fsl_avb_ops, 3415 if (fsl_avb_ops.read_rollback_index(&fsl_avb_ops,
3416 location, &rbindex) 3416 location, &rbindex)
3417 != AVB_IO_RESULT_OK) { 3417 != AVB_IO_RESULT_OK) {
3418 printf("Read rollback index error!\n"); 3418 printf("Read rollback index error!\n");
3419 snprintf(response, sizeof(response), 3419 snprintf(response, sizeof(response),
3420 "INFOread rollback index error when get avb-min-versions"); 3420 "INFOread rollback index error when get avb-min-versions");
3421 return -1; 3421 return -1;
3422 } 3422 }
3423 /* Generate the "location:value" pair */ 3423 /* Generate the "location:value" pair */
3424 snprintf(buffer, sizeof(buffer), "%d:%lld", location, rbindex); 3424 snprintf(buffer, sizeof(buffer), "%d:%lld", location, rbindex);
3425 if (i != AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 1) 3425 if (i != AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS + 1)
3426 strncat(buffer, ",", strlen(",")); 3426 strncat(buffer, ",", strlen(","));
3427 3427
3428 if ((chars_left - (int)strlen(buffer)) >= 0) { 3428 if ((chars_left - (int)strlen(buffer)) >= 0) {
3429 strncat(response, buffer, strlen(buffer)); 3429 strncat(response, buffer, strlen(buffer));
3430 chars_left -= strlen(buffer); 3430 chars_left -= strlen(buffer);
3431 } else { 3431 } else {
3432 strncat(response, buffer, chars_left); 3432 strncat(response, buffer, chars_left);
3433 /* reponse buffer is full, send it first */ 3433 /* reponse buffer is full, send it first */
3434 fastboot_tx_write_more(response); 3434 fastboot_tx_write_more(response);
3435 /* reset the reponse buffer for next round */ 3435 /* reset the reponse buffer for next round */
3436 memset(response, '\0', sizeof(response)); 3436 memset(response, '\0', sizeof(response));
3437 strncpy(response, "INFO", 5); 3437 strncpy(response, "INFO", 5);
3438 /* Copy left strings from 'buffer' to 'response' */ 3438 /* Copy left strings from 'buffer' to 'response' */
3439 strncat(response, buffer + chars_left, strlen(buffer)); 3439 strncat(response, buffer + chars_left, strlen(buffer));
3440 chars_left = FASTBOOT_RESPONSE_LEN - 3440 chars_left = FASTBOOT_RESPONSE_LEN -
3441 strlen(response) - 1; 3441 strlen(response) - 1;
3442 } 3442 }
3443 } 3443 }
3444 3444
3445 } 3445 }
3446 #endif 3446 #endif
3447 else { 3447 else {
3448 char envstr[32]; 3448 char envstr[32];
3449 3449
3450 snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd); 3450 snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd);
3451 s = env_get(envstr); 3451 s = env_get(envstr);
3452 if (s) { 3452 if (s) {
3453 strncat(response, s, chars_left); 3453 strncat(response, s, chars_left);
3454 } else { 3454 } else {
3455 snprintf(response, chars_left, "FAILunknown variable:%s",cmd); 3455 snprintf(response, chars_left, "FAILunknown variable:%s",cmd);
3456 printf("WARNING: unknown variable: %s\n", cmd); 3456 printf("WARNING: unknown variable: %s\n", cmd);
3457 return -1; 3457 return -1;
3458 } 3458 }
3459 } 3459 }
3460 return 0; 3460 return 0;
3461 } 3461 }
3462 3462
3463 static void cb_getvar(struct usb_ep *ep, struct usb_request *req) 3463 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
3464 { 3464 {
3465 int n = 0; 3465 int n = 0;
3466 int status = 0; 3466 int status = 0;
3467 int count = 0; 3467 int count = 0;
3468 char *cmd = req->buf; 3468 char *cmd = req->buf;
3469 char var_name[FASTBOOT_RESPONSE_LEN]; 3469 char var_name[FASTBOOT_RESPONSE_LEN];
3470 char partition_base_name[MAX_PTN][16]; 3470 char partition_base_name[MAX_PTN][16];
3471 char slot_suffix[2][5] = {"a","b"}; 3471 char slot_suffix[2][5] = {"a","b"};
3472 char response[FASTBOOT_RESPONSE_LEN]; 3472 char response[FASTBOOT_RESPONSE_LEN];
3473 3473
3474 strsep(&cmd, ":"); 3474 strsep(&cmd, ":");
3475 if (!cmd) { 3475 if (!cmd) {
3476 pr_err("missing variable"); 3476 pr_err("missing variable");
3477 fastboot_tx_write_str("FAILmissing var"); 3477 fastboot_tx_write_str("FAILmissing var");
3478 return; 3478 return;
3479 } 3479 }
3480 3480
3481 if (!strcmp_l1("all", cmd)) { 3481 if (!strcmp_l1("all", cmd)) {
3482 3482
3483 memset(response, '\0', FASTBOOT_RESPONSE_LEN); 3483 memset(response, '\0', FASTBOOT_RESPONSE_LEN);
3484 3484
3485 3485
3486 /* get common variables */ 3486 /* get common variables */
3487 for (n = 0; n < FASTBOOT_COMMON_VAR_NUM; n++) { 3487 for (n = 0; n < FASTBOOT_COMMON_VAR_NUM; n++) {
3488 snprintf(response, sizeof(response), "INFO%s:", fastboot_common_var[n]); 3488 snprintf(response, sizeof(response), "INFO%s:", fastboot_common_var[n]);
3489 get_single_var(fastboot_common_var[n], response); 3489 get_single_var(fastboot_common_var[n], response);
3490 fastboot_tx_write_more(response); 3490 fastboot_tx_write_more(response);
3491 } 3491 }
3492 3492
3493 /* get at-vboot-state variables */ 3493 /* get at-vboot-state variables */
3494 #ifdef CONFIG_AVB_ATX 3494 #ifdef CONFIG_AVB_ATX
3495 for (n = 0; n < AT_VBOOT_STATE_VAR_NUM; n++) { 3495 for (n = 0; n < AT_VBOOT_STATE_VAR_NUM; n++) {
3496 snprintf(response, sizeof(response), "INFO%s:", fastboot_at_vboot_state_var[n]); 3496 snprintf(response, sizeof(response), "INFO%s:", fastboot_at_vboot_state_var[n]);
3497 get_single_var(fastboot_at_vboot_state_var[n], response); 3497 get_single_var(fastboot_at_vboot_state_var[n], response);
3498 fastboot_tx_write_more(response); 3498 fastboot_tx_write_more(response);
3499 } 3499 }
3500 #endif 3500 #endif
3501 /* get partition type */ 3501 /* get partition type */
3502 for (n = 0; n < g_pcount; n++) { 3502 for (n = 0; n < g_pcount; n++) {
3503 snprintf(response, sizeof(response), "INFOpartition-type:%s:", g_ptable[n].name); 3503 snprintf(response, sizeof(response), "INFOpartition-type:%s:", g_ptable[n].name);
3504 snprintf(var_name, sizeof(var_name), "partition-type:%s", g_ptable[n].name); 3504 snprintf(var_name, sizeof(var_name), "partition-type:%s", g_ptable[n].name);
3505 get_single_var(var_name, response); 3505 get_single_var(var_name, response);
3506 fastboot_tx_write_more(response); 3506 fastboot_tx_write_more(response);
3507 } 3507 }
3508 /* get partition size */ 3508 /* get partition size */
3509 for (n = 0; n < g_pcount; n++) { 3509 for (n = 0; n < g_pcount; n++) {
3510 snprintf(response, sizeof(response), "INFOpartition-size:%s:", g_ptable[n].name); 3510 snprintf(response, sizeof(response), "INFOpartition-size:%s:", g_ptable[n].name);
3511 snprintf(var_name, sizeof(var_name), "partition-size:%s", g_ptable[n].name); 3511 snprintf(var_name, sizeof(var_name), "partition-size:%s", g_ptable[n].name);
3512 get_single_var(var_name,response); 3512 get_single_var(var_name,response);
3513 fastboot_tx_write_more(response); 3513 fastboot_tx_write_more(response);
3514 } 3514 }
3515 /* slot related variables */ 3515 /* slot related variables */
3516 if (is_slot()) { 3516 if (is_slot()) {
3517 /* get has-slot variables */ 3517 /* get has-slot variables */
3518 count = get_partition_base_name(partition_base_name); 3518 count = get_partition_base_name(partition_base_name);
3519 for (n = 0; n < count; n++) { 3519 for (n = 0; n < count; n++) {
3520 snprintf(response, sizeof(response), "INFOhas-slot:%s:", partition_base_name[n]); 3520 snprintf(response, sizeof(response), "INFOhas-slot:%s:", partition_base_name[n]);
3521 snprintf(var_name, sizeof(var_name), "has-slot:%s", partition_base_name[n]); 3521 snprintf(var_name, sizeof(var_name), "has-slot:%s", partition_base_name[n]);
3522 get_single_var(var_name,response); 3522 get_single_var(var_name,response);
3523 fastboot_tx_write_more(response); 3523 fastboot_tx_write_more(response);
3524 } 3524 }
3525 /* get current slot */ 3525 /* get current slot */
3526 strncpy(response, "INFOcurrent-slot:", sizeof(response)); 3526 strncpy(response, "INFOcurrent-slot:", sizeof(response));
3527 get_single_var("current-slot", response); 3527 get_single_var("current-slot", response);
3528 fastboot_tx_write_more(response); 3528 fastboot_tx_write_more(response);
3529 /* get slot count */ 3529 /* get slot count */
3530 strncpy(response, "INFOslot-count:", sizeof(response)); 3530 strncpy(response, "INFOslot-count:", sizeof(response));
3531 get_single_var("slot-count", response); 3531 get_single_var("slot-count", response);
3532 fastboot_tx_write_more(response); 3532 fastboot_tx_write_more(response);
3533 /* get slot-successful variable */ 3533 /* get slot-successful variable */
3534 for (n = 0; n < 2; n++) { 3534 for (n = 0; n < 2; n++) {
3535 snprintf(response, sizeof(response), "INFOslot-successful:%s:", slot_suffix[n]); 3535 snprintf(response, sizeof(response), "INFOslot-successful:%s:", slot_suffix[n]);
3536 snprintf(var_name, sizeof(var_name), "slot-successful:%s", slot_suffix[n]); 3536 snprintf(var_name, sizeof(var_name), "slot-successful:%s", slot_suffix[n]);
3537 get_single_var(var_name, response); 3537 get_single_var(var_name, response);
3538 fastboot_tx_write_more(response); 3538 fastboot_tx_write_more(response);
3539 } 3539 }
3540 /*get slot-unbootable variable*/ 3540 /*get slot-unbootable variable*/
3541 for (n = 0; n < 2; n++) { 3541 for (n = 0; n < 2; n++) {
3542 snprintf(response, sizeof(response), "INFOslot-unbootable:%s:", slot_suffix[n]); 3542 snprintf(response, sizeof(response), "INFOslot-unbootable:%s:", slot_suffix[n]);
3543 snprintf(var_name, sizeof(var_name), "slot-unbootable:%s", slot_suffix[n]); 3543 snprintf(var_name, sizeof(var_name), "slot-unbootable:%s", slot_suffix[n]);
3544 get_single_var(var_name, response); 3544 get_single_var(var_name, response);
3545 fastboot_tx_write_more(response); 3545 fastboot_tx_write_more(response);
3546 } 3546 }
3547 /*get slot-retry-count variable*/ 3547 /*get slot-retry-count variable*/
3548 for (n = 0; n < 2; n++) { 3548 for (n = 0; n < 2; n++) {
3549 snprintf(response, sizeof(response), "INFOslot-retry-count:%s:", slot_suffix[n]); 3549 snprintf(response, sizeof(response), "INFOslot-retry-count:%s:", slot_suffix[n]);
3550 snprintf(var_name, sizeof(var_name), "slot-retry-count:%s", slot_suffix[n]); 3550 snprintf(var_name, sizeof(var_name), "slot-retry-count:%s", slot_suffix[n]);
3551 get_single_var(var_name, response); 3551 get_single_var(var_name, response);
3552 fastboot_tx_write_more(response); 3552 fastboot_tx_write_more(response);
3553 } 3553 }
3554 } 3554 }
3555 3555
3556 strncpy(response, "OKAYDone!", 10); 3556 strncpy(response, "OKAYDone!", 10);
3557 fastboot_tx_write_more(response); 3557 fastboot_tx_write_more(response);
3558 3558
3559 return; 3559 return;
3560 } 3560 }
3561 #ifdef CONFIG_AVB_ATX 3561 #ifdef CONFIG_AVB_ATX
3562 else if (!strcmp_l1("at-vboot-state", cmd)) { 3562 else if (!strcmp_l1("at-vboot-state", cmd)) {
3563 /* get at-vboot-state variables */ 3563 /* get at-vboot-state variables */
3564 for (n = 0; n < AT_VBOOT_STATE_VAR_NUM; n++) { 3564 for (n = 0; n < AT_VBOOT_STATE_VAR_NUM; n++) {
3565 snprintf(response, sizeof(response), "INFO%s:", fastboot_at_vboot_state_var[n]); 3565 snprintf(response, sizeof(response), "INFO%s:", fastboot_at_vboot_state_var[n]);
3566 get_single_var(fastboot_at_vboot_state_var[n], response); 3566 get_single_var(fastboot_at_vboot_state_var[n], response);
3567 fastboot_tx_write_more(response); 3567 fastboot_tx_write_more(response);
3568 } 3568 }
3569 3569
3570 strncpy(response, "OKAY", 5); 3570 strncpy(response, "OKAY", 5);
3571 fastboot_tx_write_more(response); 3571 fastboot_tx_write_more(response);
3572 3572
3573 return; 3573 return;
3574 } else if ((!strcmp_l1("bootloader-locked", cmd)) || 3574 } else if ((!strcmp_l1("bootloader-locked", cmd)) ||
3575 (!strcmp_l1("bootloader-min-versions", cmd)) || 3575 (!strcmp_l1("bootloader-min-versions", cmd)) ||
3576 (!strcmp_l1("avb-perm-attr-set", cmd)) || 3576 (!strcmp_l1("avb-perm-attr-set", cmd)) ||
3577 (!strcmp_l1("avb-locked", cmd)) || 3577 (!strcmp_l1("avb-locked", cmd)) ||
3578 (!strcmp_l1("avb-unlock-disabled", cmd)) || 3578 (!strcmp_l1("avb-unlock-disabled", cmd)) ||
3579 (!strcmp_l1("avb-min-versions", cmd))) { 3579 (!strcmp_l1("avb-min-versions", cmd))) {
3580 3580
3581 printf("Can't get this variable alone, get 'at-vboot-state' instead!\n"); 3581 printf("Can't get this variable alone, get 'at-vboot-state' instead!\n");
3582 snprintf(response, sizeof(response), 3582 snprintf(response, sizeof(response),
3583 "FAILCan't get this variable alone, get 'at-vboot-state' instead."); 3583 "FAILCan't get this variable alone, get 'at-vboot-state' instead.");
3584 fastboot_tx_write_str(response); 3584 fastboot_tx_write_str(response);
3585 return; 3585 return;
3586 } 3586 }
3587 #endif 3587 #endif
3588 else { 3588 else {
3589 3589
3590 strncpy(response, "OKAY", 5); 3590 strncpy(response, "OKAY", 5);
3591 status = get_single_var(cmd, response); 3591 status = get_single_var(cmd, response);
3592 if (status != 0) { 3592 if (status != 0) {
3593 strncpy(response, "FAIL", 5); 3593 strncpy(response, "FAIL", 5);
3594 } 3594 }
3595 fastboot_tx_write_str(response); 3595 fastboot_tx_write_str(response);
3596 return; 3596 return;
3597 } 3597 }
3598 } 3598 }
3599 3599
3600 #ifdef CONFIG_FASTBOOT_LOCK 3600 #ifdef CONFIG_FASTBOOT_LOCK
3601 3601
3602 int do_lock_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { 3602 int do_lock_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) {
3603 FbLockState status = fastboot_get_lock_stat(); 3603 FbLockState status = fastboot_get_lock_stat();
3604 if (status != FASTBOOT_LOCK_ERROR) { 3604 if (status != FASTBOOT_LOCK_ERROR) {
3605 if (status == FASTBOOT_LOCK) 3605 if (status == FASTBOOT_LOCK)
3606 printf("fastboot lock status: locked.\n"); 3606 printf("fastboot lock status: locked.\n");
3607 else 3607 else
3608 printf("fastboot lock status: unlocked.\n"); 3608 printf("fastboot lock status: unlocked.\n");
3609 } else 3609 } else
3610 printf("fastboot lock status error!\n"); 3610 printf("fastboot lock status error!\n");
3611 3611
3612 display_lock(status, -1); 3612 display_lock(status, -1);
3613 3613
3614 return 0; 3614 return 0;
3615 3615
3616 } 3616 }
3617 3617
3618 U_BOOT_CMD( 3618 U_BOOT_CMD(
3619 lock_status, 2, 1, do_lock_status, 3619 lock_status, 2, 1, do_lock_status,
3620 "lock_status", 3620 "lock_status",
3621 "lock_status"); 3621 "lock_status");
3622 3622
3623 static void wipe_all_userdata(void) 3623 static void wipe_all_userdata(void)
3624 { 3624 {
3625 char response[FASTBOOT_RESPONSE_LEN]; 3625 char response[FASTBOOT_RESPONSE_LEN];
3626 3626
3627 /* Erase all user data */ 3627 /* Erase all user data */
3628 printf("Start userdata wipe process....\n"); 3628 printf("Start userdata wipe process....\n");
3629 /* Erase /data partition */ 3629 /* Erase /data partition */
3630 fastboot_wipe_data_partition(); 3630 fastboot_wipe_data_partition();
3631 3631
3632 #if defined (CONFIG_ANDROID_SUPPORT) || defined (CONFIG_ANDROID_AUTO_SUPPORT) 3632 #if defined (CONFIG_ANDROID_SUPPORT) || defined (CONFIG_ANDROID_AUTO_SUPPORT)
3633 /* Erase the misc partition. */ 3633 /* Erase the misc partition. */
3634 process_erase_mmc(FASTBOOT_PARTITION_MISC, response); 3634 process_erase_mmc(FASTBOOT_PARTITION_MISC, response);
3635 #endif 3635 #endif
3636 3636
3637 #ifndef CONFIG_ANDROID_AB_SUPPORT 3637 #ifndef CONFIG_ANDROID_AB_SUPPORT
3638 /* Erase the cache partition for legacy imx6/7 */ 3638 /* Erase the cache partition for legacy imx6/7 */
3639 process_erase_mmc(FASTBOOT_PARTITION_CACHE, response); 3639 process_erase_mmc(FASTBOOT_PARTITION_CACHE, response);
3640 #endif 3640 #endif
3641 /* The unlock permissive flag is set by user and should be wiped here. */ 3641 /* The unlock permissive flag is set by user and should be wiped here. */
3642 set_fastboot_lock_disable(); 3642 set_fastboot_lock_disable();
3643 3643
3644 3644
3645 #if defined(AVB_RPMB) && !defined(CONFIG_IMX_TRUSTY_OS) 3645 #if defined(AVB_RPMB) && !defined(CONFIG_IMX_TRUSTY_OS)
3646 printf("Start stored_rollback_index wipe process....\n"); 3646 printf("Start stored_rollback_index wipe process....\n");
3647 rbkidx_erase(); 3647 rbkidx_erase();
3648 printf("Wipe stored_rollback_index completed.\n"); 3648 printf("Wipe stored_rollback_index completed.\n");
3649 #endif 3649 #endif
3650 printf("Wipe userdata completed.\n"); 3650 printf("Wipe userdata completed.\n");
3651 } 3651 }
3652 3652
3653 static FbLockState do_fastboot_unlock(bool force) 3653 static FbLockState do_fastboot_unlock(bool force)
3654 { 3654 {
3655 int status; 3655 int status;
3656 3656
3657 if (fastboot_get_lock_stat() == FASTBOOT_UNLOCK) { 3657 if (fastboot_get_lock_stat() == FASTBOOT_UNLOCK) {
3658 printf("The device is already unlocked\n"); 3658 printf("The device is already unlocked\n");
3659 return FASTBOOT_UNLOCK; 3659 return FASTBOOT_UNLOCK;
3660 } 3660 }
3661 if ((fastboot_lock_enable() == FASTBOOT_UL_ENABLE) || force) { 3661 if ((fastboot_lock_enable() == FASTBOOT_UL_ENABLE) || force) {
3662 printf("It is able to unlock device. %d\n",fastboot_lock_enable()); 3662 printf("It is able to unlock device. %d\n",fastboot_lock_enable());
3663 status = fastboot_set_lock_stat(FASTBOOT_UNLOCK); 3663 status = fastboot_set_lock_stat(FASTBOOT_UNLOCK);
3664 if (status < 0) 3664 if (status < 0)
3665 return FASTBOOT_LOCK_ERROR; 3665 return FASTBOOT_LOCK_ERROR;
3666 3666
3667 wipe_all_userdata(); 3667 wipe_all_userdata();
3668 3668
3669 } else { 3669 } else {
3670 printf("It is not able to unlock device."); 3670 printf("It is not able to unlock device.");
3671 return FASTBOOT_LOCK_ERROR; 3671 return FASTBOOT_LOCK_ERROR;
3672 } 3672 }
3673 3673
3674 return FASTBOOT_UNLOCK; 3674 return FASTBOOT_UNLOCK;
3675 } 3675 }
3676 3676
3677 static FbLockState do_fastboot_lock(void) 3677 static FbLockState do_fastboot_lock(void)
3678 { 3678 {
3679 int status; 3679 int status;
3680 3680
3681 if (fastboot_get_lock_stat() == FASTBOOT_LOCK) { 3681 if (fastboot_get_lock_stat() == FASTBOOT_LOCK) {
3682 printf("The device is already locked\n"); 3682 printf("The device is already locked\n");
3683 return FASTBOOT_LOCK; 3683 return FASTBOOT_LOCK;
3684 } 3684 }
3685 status = fastboot_set_lock_stat(FASTBOOT_LOCK); 3685 status = fastboot_set_lock_stat(FASTBOOT_LOCK);
3686 if (status < 0) 3686 if (status < 0)
3687 return FASTBOOT_LOCK_ERROR; 3687 return FASTBOOT_LOCK_ERROR;
3688 3688
3689 wipe_all_userdata(); 3689 wipe_all_userdata();
3690 3690
3691 return FASTBOOT_LOCK; 3691 return FASTBOOT_LOCK;
3692 } 3692 }
3693 3693
3694 static bool endswith(char* s, char* subs) { 3694 static bool endswith(char* s, char* subs) {
3695 if (!s || !subs) 3695 if (!s || !subs)
3696 return false; 3696 return false;
3697 uint32_t len = strlen(s); 3697 uint32_t len = strlen(s);
3698 uint32_t sublen = strlen(subs); 3698 uint32_t sublen = strlen(subs);
3699 if (len < sublen) { 3699 if (len < sublen) {
3700 return false; 3700 return false;
3701 } 3701 }
3702 if (strncmp(s + len - sublen, subs, sublen)) { 3702 if (strncmp(s + len - sublen, subs, sublen)) {
3703 return false; 3703 return false;
3704 } 3704 }
3705 return true; 3705 return true;
3706 } 3706 }
3707 3707
3708 static void cb_flashing(struct usb_ep *ep, struct usb_request *req) 3708 static void cb_flashing(struct usb_ep *ep, struct usb_request *req)
3709 { 3709 {
3710 char *cmd = req->buf; 3710 char *cmd = req->buf;
3711 char response[FASTBOOT_RESPONSE_LEN]; 3711 char response[FASTBOOT_RESPONSE_LEN];
3712 FbLockState status; 3712 FbLockState status;
3713 FbLockEnableResult result; 3713 FbLockEnableResult result;
3714 if (endswith(cmd, "lock_critical")) { 3714 if (endswith(cmd, "lock_critical")) {
3715 strcpy(response, "OKAY"); 3715 strcpy(response, "OKAY");
3716 } 3716 }
3717 #ifdef CONFIG_AVB_ATX 3717 #ifdef CONFIG_AVB_ATX
3718 else if (endswith(cmd, FASTBOOT_AVB_AT_PERM_ATTR)) { 3718 else if (endswith(cmd, FASTBOOT_AVB_AT_PERM_ATTR)) {
3719 if (avb_atx_fuse_perm_attr(interface.transfer_buffer, download_bytes)) 3719 if (avb_atx_fuse_perm_attr(interface.transfer_buffer, download_bytes))
3720 strcpy(response, "FAILInternal error!"); 3720 strcpy(response, "FAILInternal error!");
3721 else 3721 else
3722 strcpy(response, "OKAY"); 3722 strcpy(response, "OKAY");
3723 } else if (endswith(cmd, FASTBOOT_AT_GET_UNLOCK_CHALLENGE)) { 3723 } else if (endswith(cmd, FASTBOOT_AT_GET_UNLOCK_CHALLENGE)) {
3724 if (avb_atx_get_unlock_challenge(fsl_avb_ops.atx_ops, 3724 if (avb_atx_get_unlock_challenge(fsl_avb_ops.atx_ops,
3725 interface.transfer_buffer, &download_bytes)) 3725 interface.transfer_buffer, &download_bytes))
3726 strcpy(response, "FAILInternal error!"); 3726 strcpy(response, "FAILInternal error!");
3727 else 3727 else
3728 strcpy(response, "OKAY"); 3728 strcpy(response, "OKAY");
3729 } else if (endswith(cmd, FASTBOOT_AT_UNLOCK_VBOOT)) { 3729 } else if (endswith(cmd, FASTBOOT_AT_UNLOCK_VBOOT)) {
3730 if (at_unlock_vboot_is_disabled()) { 3730 if (at_unlock_vboot_is_disabled()) {
3731 printf("unlock vboot already disabled, can't unlock the device!\n"); 3731 printf("unlock vboot already disabled, can't unlock the device!\n");
3732 strcpy(response, "FAILunlock vboot already disabled!."); 3732 strcpy(response, "FAILunlock vboot already disabled!.");
3733 } else { 3733 } else {
3734 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK 3734 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK
3735 if (avb_atx_verify_unlock_credential(fsl_avb_ops.atx_ops, 3735 if (avb_atx_verify_unlock_credential(fsl_avb_ops.atx_ops,
3736 interface.transfer_buffer)) 3736 interface.transfer_buffer))
3737 strcpy(response, "FAILIncorrect unlock credential!"); 3737 strcpy(response, "FAILIncorrect unlock credential!");
3738 else { 3738 else {
3739 #endif 3739 #endif
3740 status = do_fastboot_unlock(true); 3740 status = do_fastboot_unlock(true);
3741 if (status != FASTBOOT_LOCK_ERROR) 3741 if (status != FASTBOOT_LOCK_ERROR)
3742 strcpy(response, "OKAY"); 3742 strcpy(response, "OKAY");
3743 else 3743 else
3744 strcpy(response, "FAILunlock device failed."); 3744 strcpy(response, "FAILunlock device failed.");
3745 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK 3745 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK
3746 } 3746 }
3747 #endif 3747 #endif
3748 } 3748 }
3749 } else if (endswith(cmd, FASTBOOT_AT_LOCK_VBOOT)) { 3749 } else if (endswith(cmd, FASTBOOT_AT_LOCK_VBOOT)) {
3750 if (perm_attr_are_fused()) { 3750 if (perm_attr_are_fused()) {
3751 status = do_fastboot_lock(); 3751 status = do_fastboot_lock();
3752 if (status != FASTBOOT_LOCK_ERROR) 3752 if (status != FASTBOOT_LOCK_ERROR)
3753 strcpy(response, "OKAY"); 3753 strcpy(response, "OKAY");
3754 else 3754 else
3755 strcpy(response, "FAILlock device failed."); 3755 strcpy(response, "FAILlock device failed.");
3756 } else 3756 } else
3757 strcpy(response, "FAILpermanent attributes not fused!"); 3757 strcpy(response, "FAILpermanent attributes not fused!");
3758 } else if (endswith(cmd, FASTBOOT_AT_DISABLE_UNLOCK_VBOOT)) { 3758 } else if (endswith(cmd, FASTBOOT_AT_DISABLE_UNLOCK_VBOOT)) {
3759 /* This command can only be called after 'oem at-lock-vboot' */ 3759 /* This command can only be called after 'oem at-lock-vboot' */
3760 status = fastboot_get_lock_stat(); 3760 status = fastboot_get_lock_stat();
3761 if (status == FASTBOOT_LOCK) { 3761 if (status == FASTBOOT_LOCK) {
3762 if (at_unlock_vboot_is_disabled()) { 3762 if (at_unlock_vboot_is_disabled()) {
3763 printf("unlock vboot already disabled!\n"); 3763 printf("unlock vboot already disabled!\n");
3764 strcpy(response, "OKAY"); 3764 strcpy(response, "OKAY");
3765 } 3765 }
3766 else { 3766 else {
3767 if (!at_disable_vboot_unlock()) 3767 if (!at_disable_vboot_unlock())
3768 strcpy(response, "OKAY"); 3768 strcpy(response, "OKAY");
3769 else 3769 else
3770 strcpy(response, "FAILdisable unlock vboot fail!"); 3770 strcpy(response, "FAILdisable unlock vboot fail!");
3771 } 3771 }
3772 } else 3772 } else
3773 strcpy(response, "FAILplease lock the device first!"); 3773 strcpy(response, "FAILplease lock the device first!");
3774 } 3774 }
3775 #endif /* CONFIG_AVB_ATX */ 3775 #endif /* CONFIG_AVB_ATX */
3776 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 3776 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
3777 else if (endswith(cmd, FASTBOOT_BOOTLOADER_VBOOT_KEY)) { 3777 else if (endswith(cmd, FASTBOOT_BOOTLOADER_VBOOT_KEY)) {
3778 strcpy(response, "OKAY"); 3778 strcpy(response, "OKAY");
3779 } 3779 }
3780 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */ 3780 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */
3781 #ifdef CONFIG_IMX_TRUSTY_OS 3781 #ifdef CONFIG_IMX_TRUSTY_OS
3782 else if (endswith(cmd, FASTBOOT_GET_CA_REQ)) { 3782 else if (endswith(cmd, FASTBOOT_GET_CA_REQ)) {
3783 uint8_t *ca_output; 3783 uint8_t *ca_output;
3784 uint32_t ca_length, cp_length; 3784 uint32_t ca_length, cp_length;
3785 if (trusty_atap_get_ca_request(interface.transfer_buffer, download_bytes, 3785 if (trusty_atap_get_ca_request(interface.transfer_buffer, download_bytes,
3786 &(ca_output), &ca_length)) { 3786 &(ca_output), &ca_length)) {
3787 printf("ERROR get_ca_request failed!\n"); 3787 printf("ERROR get_ca_request failed!\n");
3788 strcpy(response, "FAILInternal error!"); 3788 strcpy(response, "FAILInternal error!");
3789 } else { 3789 } else {
3790 cp_length = min((uint32_t)CONFIG_FASTBOOT_BUF_SIZE, ca_length); 3790 cp_length = min((uint32_t)CONFIG_FASTBOOT_BUF_SIZE, ca_length);
3791 memcpy(interface.transfer_buffer, ca_output, cp_length); 3791 memcpy(interface.transfer_buffer, ca_output, cp_length);
3792 download_bytes = ca_length; 3792 download_bytes = ca_length;
3793 strcpy(response, "OKAY"); 3793 strcpy(response, "OKAY");
3794 } 3794 }
3795 3795
3796 } 3796 }
3797 else if (endswith(cmd, FASTBOOT_SET_CA_RESP)) { 3797 else if (endswith(cmd, FASTBOOT_SET_CA_RESP)) {
3798 if (trusty_atap_set_ca_response(interface.transfer_buffer,download_bytes)) { 3798 if (trusty_atap_set_ca_response(interface.transfer_buffer,download_bytes)) {
3799 printf("ERROR set_ca_response failed!\n"); 3799 printf("ERROR set_ca_response failed!\n");
3800 strcpy(response, "FAILInternal error!"); 3800 strcpy(response, "FAILInternal error!");
3801 } else 3801 } else
3802 strcpy(response, "OKAY"); 3802 strcpy(response, "OKAY");
3803 } 3803 }
3804 #ifndef CONFIG_AVB_ATX 3804 #ifndef CONFIG_AVB_ATX
3805 else if (endswith(cmd, FASTBOOT_SET_RPMB_KEY)) { 3805 else if (endswith(cmd, FASTBOOT_SET_RPMB_KEY)) {
3806 if (fastboot_set_rpmb_key(interface.transfer_buffer, download_bytes)) { 3806 if (fastboot_set_rpmb_key(interface.transfer_buffer, download_bytes)) {
3807 printf("ERROR set rpmb key failed!\n"); 3807 printf("ERROR set rpmb key failed!\n");
3808 strcpy(response, "FAILset rpmb key failed!"); 3808 strcpy(response, "FAILset rpmb key failed!");
3809 } else 3809 } else
3810 strcpy(response, "OKAY"); 3810 strcpy(response, "OKAY");
3811 } else if (endswith(cmd, FASTBOOT_SET_RPMB_RANDOM_KEY)) {
3812 if (fastboot_set_rpmb_random_key()) {
3813 printf("ERROR set rpmb random key failed!\n");
3814 strcpy(response, "FAILset rpmb random key failed!");
3815 } else
3816 strcpy(response, "OKAY");
3811 } else if (endswith(cmd, FASTBOOT_SET_VBMETA_PUBLIC_KEY)) { 3817 } else if (endswith(cmd, FASTBOOT_SET_VBMETA_PUBLIC_KEY)) {
3812 if (avb_set_public_key(interface.transfer_buffer, 3818 if (avb_set_public_key(interface.transfer_buffer,
3813 download_bytes)) 3819 download_bytes))
3814 strcpy(response, "FAILcan't set public key!"); 3820 strcpy(response, "FAILcan't set public key!");
3815 else 3821 else
3816 strcpy(response, "OKAY"); 3822 strcpy(response, "OKAY");
3817 } 3823 }
3818 #endif /* !CONFIG_AVB_ATX */ 3824 #endif /* !CONFIG_AVB_ATX */
3819 #endif /* CONFIG_IMX_TRUSTY_OS */ 3825 #endif /* CONFIG_IMX_TRUSTY_OS */
3820 else if (endswith(cmd, "unlock_critical")) { 3826 else if (endswith(cmd, "unlock_critical")) {
3821 strcpy(response, "OKAY"); 3827 strcpy(response, "OKAY");
3822 } else if (endswith(cmd, "unlock")) { 3828 } else if (endswith(cmd, "unlock")) {
3823 printf("flashing unlock.\n"); 3829 printf("flashing unlock.\n");
3824 #ifdef CONFIG_AVB_ATX 3830 #ifdef CONFIG_AVB_ATX
3825 /* We should do nothing here For Android Things which 3831 /* We should do nothing here For Android Things which
3826 * enables the authenticated unlock feature. 3832 * enables the authenticated unlock feature.
3827 */ 3833 */
3828 strcpy(response, "OKAY"); 3834 strcpy(response, "OKAY");
3829 #else 3835 #else
3830 status = do_fastboot_unlock(false); 3836 status = do_fastboot_unlock(false);
3831 if (status != FASTBOOT_LOCK_ERROR) 3837 if (status != FASTBOOT_LOCK_ERROR)
3832 strcpy(response, "OKAY"); 3838 strcpy(response, "OKAY");
3833 else 3839 else
3834 strcpy(response, "FAILunlock device failed."); 3840 strcpy(response, "FAILunlock device failed.");
3835 #endif 3841 #endif
3836 } else if (endswith(cmd, "lock")) { 3842 } else if (endswith(cmd, "lock")) {
3837 #ifdef CONFIG_AVB_ATX 3843 #ifdef CONFIG_AVB_ATX
3838 /* We should do nothing here For Android Things which 3844 /* We should do nothing here For Android Things which
3839 * enables the at-lock-vboot feature. 3845 * enables the at-lock-vboot feature.
3840 */ 3846 */
3841 strcpy(response, "OKAY"); 3847 strcpy(response, "OKAY");
3842 #else 3848 #else
3843 printf("flashing lock.\n"); 3849 printf("flashing lock.\n");
3844 status = do_fastboot_lock(); 3850 status = do_fastboot_lock();
3845 if (status != FASTBOOT_LOCK_ERROR) 3851 if (status != FASTBOOT_LOCK_ERROR)
3846 strcpy(response, "OKAY"); 3852 strcpy(response, "OKAY");
3847 else 3853 else
3848 strcpy(response, "FAILlock device failed."); 3854 strcpy(response, "FAILlock device failed.");
3849 #endif 3855 #endif
3850 } else if (endswith(cmd, "get_unlock_ability")) { 3856 } else if (endswith(cmd, "get_unlock_ability")) {
3851 result = fastboot_lock_enable(); 3857 result = fastboot_lock_enable();
3852 if (result == FASTBOOT_UL_ENABLE) { 3858 if (result == FASTBOOT_UL_ENABLE) {
3853 fastboot_tx_write_more("INFO1"); 3859 fastboot_tx_write_more("INFO1");
3854 strcpy(response, "OKAY"); 3860 strcpy(response, "OKAY");
3855 } else if (result == FASTBOOT_UL_DISABLE) { 3861 } else if (result == FASTBOOT_UL_DISABLE) {
3856 fastboot_tx_write_more("INFO0"); 3862 fastboot_tx_write_more("INFO0");
3857 strcpy(response, "OKAY"); 3863 strcpy(response, "OKAY");
3858 } else { 3864 } else {
3859 printf("flashing get_unlock_ability fail!\n"); 3865 printf("flashing get_unlock_ability fail!\n");
3860 strcpy(response, "FAILget unlock ability failed."); 3866 strcpy(response, "FAILget unlock ability failed.");
3861 } 3867 }
3862 } else { 3868 } else {
3863 printf("Unknown flashing command:%s\n", cmd); 3869 printf("Unknown flashing command:%s\n", cmd);
3864 strcpy(response, "FAILcommand not defined"); 3870 strcpy(response, "FAILcommand not defined");
3865 } 3871 }
3866 fastboot_tx_write_more(response); 3872 fastboot_tx_write_more(response);
3867 } 3873 }
3868 3874
3869 #endif /* CONFIG_FASTBOOT_LOCK */ 3875 #endif /* CONFIG_FASTBOOT_LOCK */
3870 3876
3871 #ifdef CONFIG_FSL_FASTBOOT 3877 #ifdef CONFIG_FSL_FASTBOOT
3872 #ifdef CONFIG_FASTBOOT_LOCK 3878 #ifdef CONFIG_FASTBOOT_LOCK
3873 static int partition_table_valid(void) 3879 static int partition_table_valid(void)
3874 { 3880 {
3875 int status, mmc_no; 3881 int status, mmc_no;
3876 struct blk_desc *dev_desc; 3882 struct blk_desc *dev_desc;
3877 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_ARM64) 3883 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_ARM64)
3878 //Prevent other partition accessing when no TOS flashed. 3884 //Prevent other partition accessing when no TOS flashed.
3879 if (!tos_flashed) 3885 if (!tos_flashed)
3880 return 0; 3886 return 0;
3881 #endif 3887 #endif
3882 disk_partition_t info; 3888 disk_partition_t info;
3883 mmc_no = fastboot_devinfo.dev_id; 3889 mmc_no = fastboot_devinfo.dev_id;
3884 dev_desc = blk_get_dev("mmc", mmc_no); 3890 dev_desc = blk_get_dev("mmc", mmc_no);
3885 if (dev_desc) 3891 if (dev_desc)
3886 status = part_get_info(dev_desc, 1, &info); 3892 status = part_get_info(dev_desc, 1, &info);
3887 else 3893 else
3888 status = -1; 3894 status = -1;
3889 return (status == 0); 3895 return (status == 0);
3890 } 3896 }
3891 #endif 3897 #endif
3892 #endif /* CONFIG_FASTBOOT_LOCK */ 3898 #endif /* CONFIG_FASTBOOT_LOCK */
3893 3899
3894 #ifdef CONFIG_FASTBOOT_FLASH 3900 #ifdef CONFIG_FASTBOOT_FLASH
3895 static void cb_flash(struct usb_ep *ep, struct usb_request *req) 3901 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
3896 { 3902 {
3897 char *cmd = req->buf; 3903 char *cmd = req->buf;
3898 char response[FASTBOOT_RESPONSE_LEN]; 3904 char response[FASTBOOT_RESPONSE_LEN];
3899 3905
3900 strsep(&cmd, ":"); 3906 strsep(&cmd, ":");
3901 if (!cmd) { 3907 if (!cmd) {
3902 pr_err("missing partition name"); 3908 pr_err("missing partition name");
3903 fastboot_tx_write_str("FAILmissing partition name"); 3909 fastboot_tx_write_str("FAILmissing partition name");
3904 return; 3910 return;
3905 } 3911 }
3906 3912
3907 /* initialize the response buffer */ 3913 /* initialize the response buffer */
3908 fb_response_str = response; 3914 fb_response_str = response;
3909 3915
3910 /* Always enable image flash for Android Things. */ 3916 /* Always enable image flash for Android Things. */
3911 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX) 3917 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX)
3912 int status; 3918 int status;
3913 status = fastboot_get_lock_stat(); 3919 status = fastboot_get_lock_stat();
3914 3920
3915 if (status == FASTBOOT_LOCK) { 3921 if (status == FASTBOOT_LOCK) {
3916 pr_err("device is LOCKed!\n"); 3922 pr_err("device is LOCKed!\n");
3917 strcpy(response, "FAIL device is locked."); 3923 strcpy(response, "FAIL device is locked.");
3918 fastboot_tx_write_str(response); 3924 fastboot_tx_write_str(response);
3919 return; 3925 return;
3920 3926
3921 } else if (status == FASTBOOT_LOCK_ERROR) { 3927 } else if (status == FASTBOOT_LOCK_ERROR) {
3922 pr_err("write lock status into device!\n"); 3928 pr_err("write lock status into device!\n");
3923 fastboot_set_lock_stat(FASTBOOT_LOCK); 3929 fastboot_set_lock_stat(FASTBOOT_LOCK);
3924 strcpy(response, "FAILdevice is locked."); 3930 strcpy(response, "FAILdevice is locked.");
3925 fastboot_tx_write_str(response); 3931 fastboot_tx_write_str(response);
3926 return; 3932 return;
3927 } 3933 }
3928 #endif 3934 #endif
3929 fastboot_fail("no flash device defined"); 3935 fastboot_fail("no flash device defined");
3930 3936
3931 rx_process_flash(cmd); 3937 rx_process_flash(cmd);
3932 3938
3933 #ifdef CONFIG_FASTBOOT_LOCK 3939 #ifdef CONFIG_FASTBOOT_LOCK
3934 if (strncmp(cmd, "gpt", 3) == 0) { 3940 if (strncmp(cmd, "gpt", 3) == 0) {
3935 int gpt_valid = 0; 3941 int gpt_valid = 0;
3936 gpt_valid = partition_table_valid(); 3942 gpt_valid = partition_table_valid();
3937 /* If gpt is valid, load partitons table into memory. 3943 /* If gpt is valid, load partitons table into memory.
3938 So if the next command is "fastboot reboot bootloader", 3944 So if the next command is "fastboot reboot bootloader",
3939 it can find the "misc" partition to r/w. */ 3945 it can find the "misc" partition to r/w. */
3940 if(gpt_valid) { 3946 if(gpt_valid) {
3941 _fastboot_load_partitions(); 3947 _fastboot_load_partitions();
3942 /* Unlock device if the gpt is valid */ 3948 /* Unlock device if the gpt is valid */
3943 do_fastboot_unlock(true); 3949 do_fastboot_unlock(true);
3944 } 3950 }
3945 } 3951 }
3946 3952
3947 #endif 3953 #endif
3948 fastboot_tx_write_str(response); 3954 fastboot_tx_write_str(response);
3949 } 3955 }
3950 #endif 3956 #endif
3951 3957
3952 #ifdef CONFIG_FASTBOOT_FLASH 3958 #ifdef CONFIG_FASTBOOT_FLASH
3953 static void cb_erase(struct usb_ep *ep, struct usb_request *req) 3959 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
3954 { 3960 {
3955 char *cmd = req->buf; 3961 char *cmd = req->buf;
3956 char response[FASTBOOT_RESPONSE_LEN]; 3962 char response[FASTBOOT_RESPONSE_LEN];
3957 3963
3958 strsep(&cmd, ":"); 3964 strsep(&cmd, ":");
3959 if (!cmd) { 3965 if (!cmd) {
3960 pr_err("missing partition name"); 3966 pr_err("missing partition name");
3961 fastboot_tx_write_str("FAILmissing partition name"); 3967 fastboot_tx_write_str("FAILmissing partition name");
3962 return; 3968 return;
3963 } 3969 }
3964 3970
3965 /* initialize the response buffer */ 3971 /* initialize the response buffer */
3966 fb_response_str = response; 3972 fb_response_str = response;
3967 3973
3968 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX) 3974 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX)
3969 FbLockState status; 3975 FbLockState status;
3970 status = fastboot_get_lock_stat(); 3976 status = fastboot_get_lock_stat();
3971 if (status == FASTBOOT_LOCK) { 3977 if (status == FASTBOOT_LOCK) {
3972 pr_err("device is LOCKed!\n"); 3978 pr_err("device is LOCKed!\n");
3973 strcpy(response, "FAIL device is locked."); 3979 strcpy(response, "FAIL device is locked.");
3974 fastboot_tx_write_str(response); 3980 fastboot_tx_write_str(response);
3975 return; 3981 return;
3976 } else if (status == FASTBOOT_LOCK_ERROR) { 3982 } else if (status == FASTBOOT_LOCK_ERROR) {
3977 pr_err("write lock status into device!\n"); 3983 pr_err("write lock status into device!\n");
3978 fastboot_set_lock_stat(FASTBOOT_LOCK); 3984 fastboot_set_lock_stat(FASTBOOT_LOCK);
3979 strcpy(response, "FAILdevice is locked."); 3985 strcpy(response, "FAILdevice is locked.");
3980 fastboot_tx_write_str(response); 3986 fastboot_tx_write_str(response);
3981 return; 3987 return;
3982 } 3988 }
3983 #endif 3989 #endif
3984 rx_process_erase(cmd, response); 3990 rx_process_erase(cmd, response);
3985 fastboot_tx_write_str(response); 3991 fastboot_tx_write_str(response);
3986 } 3992 }
3987 #endif 3993 #endif
3988 3994
3989 #ifndef CONFIG_NOT_UUU_BUILD 3995 #ifndef CONFIG_NOT_UUU_BUILD
3990 static void cb_run_uboot_cmd(struct usb_ep *ep, struct usb_request *req) 3996 static void cb_run_uboot_cmd(struct usb_ep *ep, struct usb_request *req)
3991 { 3997 {
3992 char *cmd = req->buf; 3998 char *cmd = req->buf;
3993 strsep(&cmd, ":"); 3999 strsep(&cmd, ":");
3994 if (!cmd) { 4000 if (!cmd) {
3995 pr_err("missing slot suffix\n"); 4001 pr_err("missing slot suffix\n");
3996 fastboot_tx_write_str("FAILmissing command"); 4002 fastboot_tx_write_str("FAILmissing command");
3997 return; 4003 return;
3998 } 4004 }
3999 if(run_command(cmd, 0)) { 4005 if(run_command(cmd, 0)) {
4000 fastboot_tx_write_str("FAIL"); 4006 fastboot_tx_write_str("FAIL");
4001 } else { 4007 } else {
4002 fastboot_tx_write_str("OKAY"); 4008 fastboot_tx_write_str("OKAY");
4003 /* cmd may impact fastboot related environment*/ 4009 /* cmd may impact fastboot related environment*/
4004 fastboot_setup(); 4010 fastboot_setup();
4005 } 4011 }
4006 return ; 4012 return ;
4007 } 4013 }
4008 4014
4009 static char g_a_cmd_buff[64]; 4015 static char g_a_cmd_buff[64];
4010 static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req) 4016 static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req)
4011 { 4017 {
4012 /* When usb dequeue complete will be called 4018 /* When usb dequeue complete will be called
4013 * Meed status value before call run_command. 4019 * Meed status value before call run_command.
4014 * otherwise, host can't get last message. 4020 * otherwise, host can't get last message.
4015 */ 4021 */
4016 if(req->status == 0) 4022 if(req->status == 0)
4017 run_command(g_a_cmd_buff, 0); 4023 run_command(g_a_cmd_buff, 0);
4018 } 4024 }
4019 4025
4020 static void cb_run_uboot_acmd(struct usb_ep *ep, struct usb_request *req) 4026 static void cb_run_uboot_acmd(struct usb_ep *ep, struct usb_request *req)
4021 { 4027 {
4022 char *cmd = req->buf; 4028 char *cmd = req->buf;
4023 strsep(&cmd, ":"); 4029 strsep(&cmd, ":");
4024 if (!cmd) { 4030 if (!cmd) {
4025 pr_err("missing slot suffix\n"); 4031 pr_err("missing slot suffix\n");
4026 fastboot_tx_write_str("FAILmissing command"); 4032 fastboot_tx_write_str("FAILmissing command");
4027 return; 4033 return;
4028 } 4034 }
4029 strcpy(g_a_cmd_buff, cmd); 4035 strcpy(g_a_cmd_buff, cmd);
4030 fastboot_func->in_req->complete = do_acmd_complete; 4036 fastboot_func->in_req->complete = do_acmd_complete;
4031 fastboot_tx_write_str("OKAY"); 4037 fastboot_tx_write_str("OKAY");
4032 } 4038 }
4033 #endif 4039 #endif
4034 4040
4035 #ifdef CONFIG_AVB_SUPPORT 4041 #ifdef CONFIG_AVB_SUPPORT
4036 static void cb_set_active_avb(struct usb_ep *ep, struct usb_request *req) 4042 static void cb_set_active_avb(struct usb_ep *ep, struct usb_request *req)
4037 { 4043 {
4038 AvbIOResult ret; 4044 AvbIOResult ret;
4039 int slot = 0; 4045 int slot = 0;
4040 char *cmd = req->buf; 4046 char *cmd = req->buf;
4041 4047
4042 strsep(&cmd, ":"); 4048 strsep(&cmd, ":");
4043 if (!cmd) { 4049 if (!cmd) {
4044 pr_err("missing slot suffix\n"); 4050 pr_err("missing slot suffix\n");
4045 fastboot_tx_write_str("FAILmissing slot suffix"); 4051 fastboot_tx_write_str("FAILmissing slot suffix");
4046 return; 4052 return;
4047 } 4053 }
4048 4054
4049 slot = slotidx_from_suffix(cmd); 4055 slot = slotidx_from_suffix(cmd);
4050 4056
4051 if (slot < 0) { 4057 if (slot < 0) {
4052 fastboot_tx_write_str("FAILerr slot suffix"); 4058 fastboot_tx_write_str("FAILerr slot suffix");
4053 return; 4059 return;
4054 } 4060 }
4055 4061
4056 ret = avb_ab_mark_slot_active(&fsl_avb_ab_ops, slot); 4062 ret = avb_ab_mark_slot_active(&fsl_avb_ab_ops, slot);
4057 if (ret != AVB_IO_RESULT_OK) 4063 if (ret != AVB_IO_RESULT_OK)
4058 fastboot_tx_write_str("avb IO error"); 4064 fastboot_tx_write_str("avb IO error");
4059 else 4065 else
4060 fastboot_tx_write_str("OKAY"); 4066 fastboot_tx_write_str("OKAY");
4061 4067
4062 return; 4068 return;
4063 } 4069 }
4064 #endif /*CONFIG_AVB_SUPPORT*/ 4070 #endif /*CONFIG_AVB_SUPPORT*/
4065 4071
4066 static void cb_reboot_bootloader(struct usb_ep *ep, struct usb_request *req) 4072 static void cb_reboot_bootloader(struct usb_ep *ep, struct usb_request *req)
4067 { 4073 {
4068 enable_fastboot_command(); 4074 enable_fastboot_command();
4069 fastboot_func->in_req->complete = compl_do_reset; 4075 fastboot_func->in_req->complete = compl_do_reset;
4070 fastboot_tx_write_str("OKAY"); 4076 fastboot_tx_write_str("OKAY");
4071 } 4077 }
4072 4078
4073 #else /* CONFIG_FSL_FASTBOOT */ 4079 #else /* CONFIG_FSL_FASTBOOT */
4074 4080
4075 static void cb_getvar(struct usb_ep *ep, struct usb_request *req) 4081 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
4076 { 4082 {
4077 char *cmd = req->buf; 4083 char *cmd = req->buf;
4078 char response[FASTBOOT_RESPONSE_LEN]; 4084 char response[FASTBOOT_RESPONSE_LEN];
4079 const char *s; 4085 const char *s;
4080 size_t chars_left; 4086 size_t chars_left;
4081 4087
4082 strcpy(response, "OKAY"); 4088 strcpy(response, "OKAY");
4083 chars_left = sizeof(response) - strlen(response) - 1; 4089 chars_left = sizeof(response) - strlen(response) - 1;
4084 4090
4085 strsep(&cmd, ":"); 4091 strsep(&cmd, ":");
4086 if (!cmd) { 4092 if (!cmd) {
4087 pr_err("missing variable"); 4093 pr_err("missing variable");
4088 fastboot_tx_write_str("FAILmissing var"); 4094 fastboot_tx_write_str("FAILmissing var");
4089 return; 4095 return;
4090 } 4096 }
4091 4097
4092 if (!strcmp_l1("version", cmd)) { 4098 if (!strcmp_l1("version", cmd)) {
4093 strncat(response, FASTBOOT_VERSION, chars_left); 4099 strncat(response, FASTBOOT_VERSION, chars_left);
4094 } else if (!strcmp_l1("bootloader-version", cmd)) { 4100 } else if (!strcmp_l1("bootloader-version", cmd)) {
4095 strncat(response, U_BOOT_VERSION, chars_left); 4101 strncat(response, U_BOOT_VERSION, chars_left);
4096 } else if (!strcmp_l1("downloadsize", cmd) || 4102 } else if (!strcmp_l1("downloadsize", cmd) ||
4097 !strcmp_l1("max-download-size", cmd)) { 4103 !strcmp_l1("max-download-size", cmd)) {
4098 char str_num[12]; 4104 char str_num[12];
4099 4105
4100 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE); 4106 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
4101 strncat(response, str_num, chars_left); 4107 strncat(response, str_num, chars_left);
4102 } else if (!strcmp_l1("serialno", cmd)) { 4108 } else if (!strcmp_l1("serialno", cmd)) {
4103 s = env_get("serial#"); 4109 s = env_get("serial#");
4104 if (s) 4110 if (s)
4105 strncat(response, s, chars_left); 4111 strncat(response, s, chars_left);
4106 else 4112 else
4107 strcpy(response, "FAILValue not set"); 4113 strcpy(response, "FAILValue not set");
4108 } else { 4114 } else {
4109 char *envstr; 4115 char *envstr;
4110 4116
4111 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1); 4117 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
4112 if (!envstr) { 4118 if (!envstr) {
4113 fastboot_tx_write_str("FAILmalloc error"); 4119 fastboot_tx_write_str("FAILmalloc error");
4114 return; 4120 return;
4115 } 4121 }
4116 4122
4117 sprintf(envstr, "fastboot.%s", cmd); 4123 sprintf(envstr, "fastboot.%s", cmd);
4118 s = env_get(envstr); 4124 s = env_get(envstr);
4119 if (s) { 4125 if (s) {
4120 strncat(response, s, chars_left); 4126 strncat(response, s, chars_left);
4121 } else { 4127 } else {
4122 printf("WARNING: unknown variable: %s\n", cmd); 4128 printf("WARNING: unknown variable: %s\n", cmd);
4123 strcpy(response, "FAILVariable not implemented"); 4129 strcpy(response, "FAILVariable not implemented");
4124 } 4130 }
4125 4131
4126 free(envstr); 4132 free(envstr);
4127 } 4133 }
4128 fastboot_tx_write_str(response); 4134 fastboot_tx_write_str(response);
4129 } 4135 }
4130 4136
4131 #ifdef CONFIG_FASTBOOT_FLASH 4137 #ifdef CONFIG_FASTBOOT_FLASH
4132 static void cb_flash(struct usb_ep *ep, struct usb_request *req) 4138 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
4133 { 4139 {
4134 char *cmd = req->buf; 4140 char *cmd = req->buf;
4135 char response[FASTBOOT_RESPONSE_LEN]; 4141 char response[FASTBOOT_RESPONSE_LEN];
4136 4142
4137 strsep(&cmd, ":"); 4143 strsep(&cmd, ":");
4138 if (!cmd) { 4144 if (!cmd) {
4139 pr_err("missing partition name"); 4145 pr_err("missing partition name");
4140 fastboot_tx_write_str("FAILmissing partition name"); 4146 fastboot_tx_write_str("FAILmissing partition name");
4141 return; 4147 return;
4142 } 4148 }
4143 4149
4144 /* initialize the response buffer */ 4150 /* initialize the response buffer */
4145 fb_response_str = response; 4151 fb_response_str = response;
4146 4152
4147 fastboot_fail("no flash device defined"); 4153 fastboot_fail("no flash device defined");
4148 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 4154 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
4149 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR, 4155 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
4150 download_bytes); 4156 download_bytes);
4151 #endif 4157 #endif
4152 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV 4158 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
4153 fb_nand_flash_write(cmd, 4159 fb_nand_flash_write(cmd,
4154 (void *)CONFIG_FASTBOOT_BUF_ADDR, 4160 (void *)CONFIG_FASTBOOT_BUF_ADDR,
4155 download_bytes); 4161 download_bytes);
4156 #endif 4162 #endif
4157 fastboot_tx_write_str(response); 4163 fastboot_tx_write_str(response);
4158 } 4164 }
4159 #endif 4165 #endif
4160 4166
4161 static void cb_oem(struct usb_ep *ep, struct usb_request *req) 4167 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
4162 { 4168 {
4163 char *cmd = req->buf; 4169 char *cmd = req->buf;
4164 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 4170 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
4165 if (strncmp("format", cmd + 4, 6) == 0) { 4171 if (strncmp("format", cmd + 4, 6) == 0) {
4166 char cmdbuf[32]; 4172 char cmdbuf[32];
4167 sprintf(cmdbuf, "gpt write mmc %x $partitions", 4173 sprintf(cmdbuf, "gpt write mmc %x $partitions",
4168 CONFIG_FASTBOOT_FLASH_MMC_DEV); 4174 CONFIG_FASTBOOT_FLASH_MMC_DEV);
4169 if (run_command(cmdbuf, 0)) 4175 if (run_command(cmdbuf, 0))
4170 fastboot_tx_write_str("FAIL"); 4176 fastboot_tx_write_str("FAIL");
4171 else 4177 else
4172 fastboot_tx_write_str("OKAY"); 4178 fastboot_tx_write_str("OKAY");
4173 } else 4179 } else
4174 #endif 4180 #endif
4175 if (strncmp("unlock", cmd + 4, 8) == 0) { 4181 if (strncmp("unlock", cmd + 4, 8) == 0) {
4176 fastboot_tx_write_str("FAILnot implemented"); 4182 fastboot_tx_write_str("FAILnot implemented");
4177 } 4183 }
4178 else { 4184 else {
4179 fastboot_tx_write_str("FAILunknown oem command"); 4185 fastboot_tx_write_str("FAILunknown oem command");
4180 } 4186 }
4181 } 4187 }
4182 4188
4183 #ifdef CONFIG_FASTBOOT_FLASH 4189 #ifdef CONFIG_FASTBOOT_FLASH
4184 static void cb_erase(struct usb_ep *ep, struct usb_request *req) 4190 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
4185 { 4191 {
4186 char *cmd = req->buf; 4192 char *cmd = req->buf;
4187 char response[FASTBOOT_RESPONSE_LEN]; 4193 char response[FASTBOOT_RESPONSE_LEN];
4188 4194
4189 strsep(&cmd, ":"); 4195 strsep(&cmd, ":");
4190 if (!cmd) { 4196 if (!cmd) {
4191 pr_err("missing partition name"); 4197 pr_err("missing partition name");
4192 fastboot_tx_write_str("FAILmissing partition name"); 4198 fastboot_tx_write_str("FAILmissing partition name");
4193 return; 4199 return;
4194 } 4200 }
4195 4201
4196 /* initialize the response buffer */ 4202 /* initialize the response buffer */
4197 fb_response_str = response; 4203 fb_response_str = response;
4198 4204
4199 fastboot_fail("no flash device defined"); 4205 fastboot_fail("no flash device defined");
4200 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 4206 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
4201 fb_mmc_erase(cmd); 4207 fb_mmc_erase(cmd);
4202 #endif 4208 #endif
4203 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV 4209 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
4204 fb_nand_erase(cmd); 4210 fb_nand_erase(cmd);
4205 #endif 4211 #endif
4206 fastboot_tx_write_str(response); 4212 fastboot_tx_write_str(response);
4207 } 4213 }
4208 #endif 4214 #endif
4209 4215
4210 #endif /* CONFIG_FSL_FASTBOOT*/ 4216 #endif /* CONFIG_FSL_FASTBOOT*/
4211 4217
4212 static unsigned int rx_bytes_expected(struct usb_ep *ep) 4218 static unsigned int rx_bytes_expected(struct usb_ep *ep)
4213 { 4219 {
4214 int rx_remain = download_size - download_bytes; 4220 int rx_remain = download_size - download_bytes;
4215 unsigned int rem; 4221 unsigned int rem;
4216 unsigned int maxpacket = usb_endpoint_maxp(ep->desc); 4222 unsigned int maxpacket = usb_endpoint_maxp(ep->desc);
4217 4223
4218 if (rx_remain <= 0) 4224 if (rx_remain <= 0)
4219 return 0; 4225 return 0;
4220 else if (rx_remain > EP_BUFFER_SIZE) 4226 else if (rx_remain > EP_BUFFER_SIZE)
4221 return EP_BUFFER_SIZE; 4227 return EP_BUFFER_SIZE;
4222 4228
4223 /* 4229 /*
4224 * Some controllers e.g. DWC3 don't like OUT transfers to be 4230 * Some controllers e.g. DWC3 don't like OUT transfers to be
4225 * not ending in maxpacket boundary. So just make them happy by 4231 * not ending in maxpacket boundary. So just make them happy by
4226 * always requesting for integral multiple of maxpackets. 4232 * always requesting for integral multiple of maxpackets.
4227 * This shouldn't bother controllers that don't care about it. 4233 * This shouldn't bother controllers that don't care about it.
4228 */ 4234 */
4229 rem = rx_remain % maxpacket; 4235 rem = rx_remain % maxpacket;
4230 if (rem > 0) 4236 if (rem > 0)
4231 rx_remain = rx_remain + (maxpacket - rem); 4237 rx_remain = rx_remain + (maxpacket - rem);
4232 4238
4233 return rx_remain; 4239 return rx_remain;
4234 } 4240 }
4235 4241
4236 #define BYTES_PER_DOT 0x20000 4242 #define BYTES_PER_DOT 0x20000
4237 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) 4243 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
4238 { 4244 {
4239 char response[FASTBOOT_RESPONSE_LEN]; 4245 char response[FASTBOOT_RESPONSE_LEN];
4240 unsigned int transfer_size = download_size - download_bytes; 4246 unsigned int transfer_size = download_size - download_bytes;
4241 const unsigned char *buffer = req->buf; 4247 const unsigned char *buffer = req->buf;
4242 unsigned int buffer_size = req->actual; 4248 unsigned int buffer_size = req->actual;
4243 unsigned int pre_dot_num, now_dot_num; 4249 unsigned int pre_dot_num, now_dot_num;
4244 void * base_addr = (void*)env_get_ulong("fastboot_buffer", 16, CONFIG_FASTBOOT_BUF_ADDR); 4250 void * base_addr = (void*)env_get_ulong("fastboot_buffer", 16, CONFIG_FASTBOOT_BUF_ADDR);
4245 4251
4246 if (req->status != 0) { 4252 if (req->status != 0) {
4247 printf("Bad status: %d\n", req->status); 4253 printf("Bad status: %d\n", req->status);
4248 return; 4254 return;
4249 } 4255 }
4250 4256
4251 if (buffer_size < transfer_size) 4257 if (buffer_size < transfer_size)
4252 transfer_size = buffer_size; 4258 transfer_size = buffer_size;
4253 4259
4254 memcpy(base_addr + download_bytes, 4260 memcpy(base_addr + download_bytes,
4255 buffer, transfer_size); 4261 buffer, transfer_size);
4256 4262
4257 pre_dot_num = download_bytes / BYTES_PER_DOT; 4263 pre_dot_num = download_bytes / BYTES_PER_DOT;
4258 download_bytes += transfer_size; 4264 download_bytes += transfer_size;
4259 now_dot_num = download_bytes / BYTES_PER_DOT; 4265 now_dot_num = download_bytes / BYTES_PER_DOT;
4260 4266
4261 if (pre_dot_num != now_dot_num) { 4267 if (pre_dot_num != now_dot_num) {
4262 putc('.'); 4268 putc('.');
4263 if (!(now_dot_num % 74)) 4269 if (!(now_dot_num % 74))
4264 putc('\n'); 4270 putc('\n');
4265 } 4271 }
4266 4272
4267 /* Check if transfer is done */ 4273 /* Check if transfer is done */
4268 if (download_bytes >= download_size) { 4274 if (download_bytes >= download_size) {
4269 /* 4275 /*
4270 * Reset global transfer variable, keep download_bytes because 4276 * Reset global transfer variable, keep download_bytes because
4271 * it will be used in the next possible flashing command 4277 * it will be used in the next possible flashing command
4272 */ 4278 */
4273 download_size = 0; 4279 download_size = 0;
4274 req->complete = rx_handler_command; 4280 req->complete = rx_handler_command;
4275 req->length = EP_BUFFER_SIZE; 4281 req->length = EP_BUFFER_SIZE;
4276 4282
4277 strcpy(response, "OKAY"); 4283 strcpy(response, "OKAY");
4278 fastboot_tx_write_str(response); 4284 fastboot_tx_write_str(response);
4279 env_set_hex("fastboot_bytes", download_bytes); 4285 env_set_hex("fastboot_bytes", download_bytes);
4280 4286
4281 printf("\ndownloading of %d bytes finished\n", download_bytes); 4287 printf("\ndownloading of %d bytes finished\n", download_bytes);
4282 } else { 4288 } else {
4283 req->length = rx_bytes_expected(ep); 4289 req->length = rx_bytes_expected(ep);
4284 } 4290 }
4285 4291
4286 req->actual = 0; 4292 req->actual = 0;
4287 usb_ep_queue(ep, req, 0); 4293 usb_ep_queue(ep, req, 0);
4288 } 4294 }
4289 4295
4290 static void cb_upload(struct usb_ep *ep, struct usb_request *req) 4296 static void cb_upload(struct usb_ep *ep, struct usb_request *req)
4291 { 4297 {
4292 char response[FASTBOOT_RESPONSE_LEN]; 4298 char response[FASTBOOT_RESPONSE_LEN];
4293 4299
4294 if (!download_bytes || download_bytes > (EP_BUFFER_SIZE * 32)) { 4300 if (!download_bytes || download_bytes > (EP_BUFFER_SIZE * 32)) {
4295 sprintf(response, "FAIL"); 4301 sprintf(response, "FAIL");
4296 fastboot_tx_write_str(response); 4302 fastboot_tx_write_str(response);
4297 return; 4303 return;
4298 } 4304 }
4299 4305
4300 printf("Will upload %d bytes.\n", download_bytes); 4306 printf("Will upload %d bytes.\n", download_bytes);
4301 snprintf(response, FASTBOOT_RESPONSE_LEN, "DATA%08x", download_bytes); 4307 snprintf(response, FASTBOOT_RESPONSE_LEN, "DATA%08x", download_bytes);
4302 fastboot_tx_write_more(response); 4308 fastboot_tx_write_more(response);
4303 4309
4304 fastboot_tx_write((const char *)(interface.transfer_buffer), download_bytes); 4310 fastboot_tx_write((const char *)(interface.transfer_buffer), download_bytes);
4305 4311
4306 snprintf(response,FASTBOOT_RESPONSE_LEN, "OKAY"); 4312 snprintf(response,FASTBOOT_RESPONSE_LEN, "OKAY");
4307 fastboot_tx_write_more(response); 4313 fastboot_tx_write_more(response);
4308 } 4314 }
4309 4315
4310 static void cb_download(struct usb_ep *ep, struct usb_request *req) 4316 static void cb_download(struct usb_ep *ep, struct usb_request *req)
4311 { 4317 {
4312 char *cmd = req->buf; 4318 char *cmd = req->buf;
4313 char response[FASTBOOT_RESPONSE_LEN]; 4319 char response[FASTBOOT_RESPONSE_LEN];
4314 4320
4315 strsep(&cmd, ":"); 4321 strsep(&cmd, ":");
4316 download_size = simple_strtoul(cmd, NULL, 16); 4322 download_size = simple_strtoul(cmd, NULL, 16);
4317 download_bytes = 0; 4323 download_bytes = 0;
4318 4324
4319 printf("Starting download of %d bytes\n", download_size); 4325 printf("Starting download of %d bytes\n", download_size);
4320 4326
4321 if (0 == download_size) { 4327 if (0 == download_size) {
4322 strcpy(response, "FAILdata invalid size"); 4328 strcpy(response, "FAILdata invalid size");
4323 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) { 4329 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
4324 download_size = 0; 4330 download_size = 0;
4325 strcpy(response, "FAILdata too large"); 4331 strcpy(response, "FAILdata too large");
4326 } else { 4332 } else {
4327 sprintf(response, "DATA%08x", download_size); 4333 sprintf(response, "DATA%08x", download_size);
4328 req->complete = rx_handler_dl_image; 4334 req->complete = rx_handler_dl_image;
4329 req->length = rx_bytes_expected(ep); 4335 req->length = rx_bytes_expected(ep);
4330 } 4336 }
4331 fastboot_tx_write_str(response); 4337 fastboot_tx_write_str(response);
4332 } 4338 }
4333 4339
4334 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) 4340 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
4335 { 4341 {
4336 char boot_addr_start[12]; 4342 char boot_addr_start[12];
4337 #ifdef CONFIG_FSL_FASTBOOT 4343 #ifdef CONFIG_FSL_FASTBOOT
4338 char *bootm_args[] = { "boota", boot_addr_start, NULL }; 4344 char *bootm_args[] = { "boota", boot_addr_start, NULL };
4339 sprintf(boot_addr_start, "0x%lx", load_addr); 4345 sprintf(boot_addr_start, "0x%lx", load_addr);
4340 #else 4346 #else
4341 char *bootm_args[] = { "bootm", boot_addr_start, NULL }; 4347 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
4342 sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR); 4348 sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
4343 #endif 4349 #endif
4344 4350
4345 puts("Booting kernel..\n"); 4351 puts("Booting kernel..\n");
4346 4352
4347 do_bootm(NULL, 0, 2, bootm_args); 4353 do_bootm(NULL, 0, 2, bootm_args);
4348 4354
4349 /* This only happens if image is somehow faulty so we start over */ 4355 /* This only happens if image is somehow faulty so we start over */
4350 do_reset(NULL, 0, 0, NULL); 4356 do_reset(NULL, 0, 0, NULL);
4351 } 4357 }
4352 4358
4353 static void cb_boot(struct usb_ep *ep, struct usb_request *req) 4359 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
4354 { 4360 {
4355 fastboot_func->in_req->complete = do_bootm_on_complete; 4361 fastboot_func->in_req->complete = do_bootm_on_complete;
4356 fastboot_tx_write_str("OKAY"); 4362 fastboot_tx_write_str("OKAY");
4357 } 4363 }
4358 4364
4359 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) 4365 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
4360 { 4366 {
4361 g_dnl_trigger_detach(); 4367 g_dnl_trigger_detach();
4362 } 4368 }
4363 4369
4364 static void cb_continue(struct usb_ep *ep, struct usb_request *req) 4370 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
4365 { 4371 {
4366 fastboot_func->in_req->complete = do_exit_on_complete; 4372 fastboot_func->in_req->complete = do_exit_on_complete;
4367 fastboot_tx_write_str("OKAY"); 4373 fastboot_tx_write_str("OKAY");
4368 } 4374 }
4369 4375
4370 struct cmd_dispatch_info { 4376 struct cmd_dispatch_info {
4371 char *cmd; 4377 char *cmd;
4372 void (*cb)(struct usb_ep *ep, struct usb_request *req); 4378 void (*cb)(struct usb_ep *ep, struct usb_request *req);
4373 }; 4379 };
4374 4380
4375 static const struct cmd_dispatch_info cmd_dispatch_info[] = { 4381 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
4376 #ifdef CONFIG_FSL_FASTBOOT 4382 #ifdef CONFIG_FSL_FASTBOOT
4377 { 4383 {
4378 .cmd = "reboot-bootloader", 4384 .cmd = "reboot-bootloader",
4379 .cb = cb_reboot_bootloader, 4385 .cb = cb_reboot_bootloader,
4380 }, 4386 },
4381 { 4387 {
4382 .cmd = "upload", 4388 .cmd = "upload",
4383 .cb = cb_upload, 4389 .cb = cb_upload,
4384 }, 4390 },
4385 { 4391 {
4386 .cmd = "get_staged", 4392 .cmd = "get_staged",
4387 .cb = cb_upload, 4393 .cb = cb_upload,
4388 }, 4394 },
4389 #ifdef CONFIG_FASTBOOT_LOCK 4395 #ifdef CONFIG_FASTBOOT_LOCK
4390 { 4396 {
4391 .cmd = "flashing", 4397 .cmd = "flashing",
4392 .cb = cb_flashing, 4398 .cb = cb_flashing,
4393 }, 4399 },
4394 { 4400 {
4395 .cmd = "oem", 4401 .cmd = "oem",
4396 .cb = cb_flashing, 4402 .cb = cb_flashing,
4397 }, 4403 },
4398 #endif 4404 #endif
4399 #ifdef CONFIG_AVB_SUPPORT 4405 #ifdef CONFIG_AVB_SUPPORT
4400 { 4406 {
4401 .cmd = "set_active", 4407 .cmd = "set_active",
4402 .cb = cb_set_active_avb, 4408 .cb = cb_set_active_avb,
4403 }, 4409 },
4404 #endif 4410 #endif
4405 #ifndef CONFIG_NOT_UUU_BUILD 4411 #ifndef CONFIG_NOT_UUU_BUILD
4406 { 4412 {
4407 .cmd = "UCmd:", 4413 .cmd = "UCmd:",
4408 .cb = cb_run_uboot_cmd, 4414 .cb = cb_run_uboot_cmd,
4409 }, 4415 },
4410 { .cmd ="ACmd:", 4416 { .cmd ="ACmd:",
4411 .cb = cb_run_uboot_acmd, 4417 .cb = cb_run_uboot_acmd,
4412 }, 4418 },
4413 #endif 4419 #endif
4414 #endif 4420 #endif
4415 { 4421 {
4416 .cmd = "reboot", 4422 .cmd = "reboot",
4417 .cb = cb_reboot, 4423 .cb = cb_reboot,
4418 }, { 4424 }, {
4419 .cmd = "getvar:", 4425 .cmd = "getvar:",
4420 .cb = cb_getvar, 4426 .cb = cb_getvar,
4421 }, { 4427 }, {
4422 .cmd = "download:", 4428 .cmd = "download:",
4423 .cb = cb_download, 4429 .cb = cb_download,
4424 }, { 4430 }, {
4425 .cmd = "boot", 4431 .cmd = "boot",
4426 .cb = cb_boot, 4432 .cb = cb_boot,
4427 }, { 4433 }, {
4428 .cmd = "continue", 4434 .cmd = "continue",
4429 .cb = cb_continue, 4435 .cb = cb_continue,
4430 }, 4436 },
4431 #ifdef CONFIG_FASTBOOT_FLASH 4437 #ifdef CONFIG_FASTBOOT_FLASH
4432 { 4438 {
4433 .cmd = "flash", 4439 .cmd = "flash",
4434 .cb = cb_flash, 4440 .cb = cb_flash,
4435 }, { 4441 }, {
4436 .cmd = "erase", 4442 .cmd = "erase",
4437 .cb = cb_erase, 4443 .cb = cb_erase,
4438 }, 4444 },
4439 #endif 4445 #endif
4440 #ifndef CONFIG_FSL_FASTBOOT 4446 #ifndef CONFIG_FSL_FASTBOOT
4441 { 4447 {
4442 .cmd = "oem", 4448 .cmd = "oem",
4443 .cb = cb_oem, 4449 .cb = cb_oem,
4444 }, 4450 },
4445 #endif 4451 #endif
4446 #ifdef CONFIG_AVB_ATX 4452 #ifdef CONFIG_AVB_ATX
4447 { 4453 {
4448 .cmd = "stage", 4454 .cmd = "stage",
4449 .cb = cb_download, 4455 .cb = cb_download,
4450 }, 4456 },
4451 #endif 4457 #endif
4452 }; 4458 };
4453 4459
4454 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) 4460 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
4455 { 4461 {
4456 char *cmdbuf = req->buf; 4462 char *cmdbuf = req->buf;
4457 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; 4463 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
4458 int i; 4464 int i;
4459 4465
4460 /* init in request FIFO pointer */ 4466 /* init in request FIFO pointer */
4461 fastboot_func->front = NULL; 4467 fastboot_func->front = NULL;
4462 fastboot_func->rear = NULL; 4468 fastboot_func->rear = NULL;
4463 4469
4464 if (req->status != 0 || req->length == 0) 4470 if (req->status != 0 || req->length == 0)
4465 return; 4471 return;
4466 4472
4467 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { 4473 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
4468 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) { 4474 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
4469 func_cb = cmd_dispatch_info[i].cb; 4475 func_cb = cmd_dispatch_info[i].cb;
4470 break; 4476 break;
4471 } 4477 }
4472 } 4478 }
4473 4479
4474 if (!func_cb) { 4480 if (!func_cb) {
4475 pr_err("unknown command: %.*s", req->actual, cmdbuf); 4481 pr_err("unknown command: %.*s", req->actual, cmdbuf);
4476 fastboot_tx_write_str("FAILunknown command"); 4482 fastboot_tx_write_str("FAILunknown command");
4477 } else { 4483 } else {
4478 if (req->actual < req->length) { 4484 if (req->actual < req->length) {
4479 u8 *buf = (u8 *)req->buf; 4485 u8 *buf = (u8 *)req->buf;
4480 buf[req->actual] = 0; 4486 buf[req->actual] = 0;
4481 func_cb(ep, req); 4487 func_cb(ep, req);
4482 } else { 4488 } else {
4483 pr_err("buffer overflow"); 4489 pr_err("buffer overflow");
4484 fastboot_tx_write_str("FAILbuffer overflow"); 4490 fastboot_tx_write_str("FAILbuffer overflow");
4485 } 4491 }
4486 } 4492 }
4487 4493
4488 *cmdbuf = '\0'; 4494 *cmdbuf = '\0';
4489 req->actual = 0; 4495 req->actual = 0;
4490 usb_ep_queue(ep, req, 0); 4496 usb_ep_queue(ep, req, 0);
4491 } 4497 }
4492 4498
1 /* 1 /*
2 * Copyright (C) 2016 Freescale Semiconductor, Inc. 2 * Copyright (C) 2016 Freescale Semiconductor, Inc.
3 * 3 *
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 */ 5 */
6 6
7 #ifndef __FSL_AVB_H__ 7 #ifndef __FSL_AVB_H__
8 #define __FSL_AVB_H__ 8 #define __FSL_AVB_H__
9 9
10 #include "../lib/avb/libavb_ab/libavb_ab.h" 10 #include "../lib/avb/libavb_ab/libavb_ab.h"
11 #include "../lib/avb/libavb_atx/libavb_atx.h" 11 #include "../lib/avb/libavb_atx/libavb_atx.h"
12 /* Reads |num_bytes| from offset |offset| from partition with name 12 /* Reads |num_bytes| from offset |offset| from partition with name
13 * |partition| (NUL-terminated UTF-8 string). If |offset| is 13 * |partition| (NUL-terminated UTF-8 string). If |offset| is
14 * negative, its absolute value should be interpreted as the number 14 * negative, its absolute value should be interpreted as the number
15 * of bytes from the end of the partition. 15 * of bytes from the end of the partition.
16 * 16 *
17 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if 17 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
18 * there is no partition with the given name, 18 * there is no partition with the given name,
19 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested 19 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
20 * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if 20 * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
21 * there was an I/O error from the underlying I/O subsystem. If the 21 * there was an I/O error from the underlying I/O subsystem. If the
22 * operation succeeds as requested AVB_IO_RESULT_OK is returned and 22 * operation succeeds as requested AVB_IO_RESULT_OK is returned and
23 * the data is available in |buffer|. 23 * the data is available in |buffer|.
24 * 24 *
25 * The only time partial I/O may occur is if reading beyond the end 25 * The only time partial I/O may occur is if reading beyond the end
26 * of the partition. In this case the value returned in 26 * of the partition. In this case the value returned in
27 * |out_num_read| may be smaller than |num_bytes|. 27 * |out_num_read| may be smaller than |num_bytes|.
28 */ 28 */
29 AvbIOResult fsl_read_from_partition(AvbOps* ops, const char* partition, 29 AvbIOResult fsl_read_from_partition(AvbOps* ops, const char* partition,
30 int64_t offset, size_t num_bytes, 30 int64_t offset, size_t num_bytes,
31 void* buffer, size_t* out_num_read); 31 void* buffer, size_t* out_num_read);
32 32
33 /* multi block read version 33 /* multi block read version
34 * */ 34 * */
35 AvbIOResult fsl_read_from_partition_multi(AvbOps* ops, const char* partition, 35 AvbIOResult fsl_read_from_partition_multi(AvbOps* ops, const char* partition,
36 int64_t offset, size_t num_bytes, 36 int64_t offset, size_t num_bytes,
37 void* buffer, size_t* out_num_read); 37 void* buffer, size_t* out_num_read);
38 38
39 /* Writes |num_bytes| from |bffer| at offset |offset| to partition 39 /* Writes |num_bytes| from |bffer| at offset |offset| to partition
40 * with name |partition| (NUL-terminated UTF-8 string). If |offset| 40 * with name |partition| (NUL-terminated UTF-8 string). If |offset|
41 * is negative, its absolute value should be interpreted as the 41 * is negative, its absolute value should be interpreted as the
42 * number of bytes from the end of the partition. 42 * number of bytes from the end of the partition.
43 * 43 *
44 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if 44 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
45 * there is no partition with the given name, 45 * there is no partition with the given name,
46 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested 46 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
47 * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO 47 * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
48 * if there was an I/O error from the underlying I/O subsystem. If 48 * if there was an I/O error from the underlying I/O subsystem. If
49 * the operation succeeds as requested AVB_IO_RESULT_OK is 49 * the operation succeeds as requested AVB_IO_RESULT_OK is
50 * returned. 50 * returned.
51 * 51 *
52 * This function never does any partial I/O, it either transfers all 52 * This function never does any partial I/O, it either transfers all
53 * of the requested bytes or returns an error. 53 * of the requested bytes or returns an error.
54 */ 54 */
55 AvbIOResult fsl_write_to_partition(AvbOps* ops, const char* partition, 55 AvbIOResult fsl_write_to_partition(AvbOps* ops, const char* partition,
56 int64_t offset, size_t num_bytes, 56 int64_t offset, size_t num_bytes,
57 const void* buffer); 57 const void* buffer);
58 58
59 /* Reads A/B metadata from persistent storage. Returned data is 59 /* Reads A/B metadata from persistent storage. Returned data is
60 * properly byteswapped. Returns AVB_IO_RESULT_OK on success, error 60 * properly byteswapped. Returns AVB_IO_RESULT_OK on success, error
61 * code otherwise. 61 * code otherwise.
62 * 62 *
63 * If the data read is invalid (e.g. wrong magic or CRC checksum 63 * If the data read is invalid (e.g. wrong magic or CRC checksum
64 * failure), the metadata shoule be reset using avb_ab_data_init() 64 * failure), the metadata shoule be reset using avb_ab_data_init()
65 * and then written to persistent storage. 65 * and then written to persistent storage.
66 * 66 *
67 * Implementations will typically want to use avb_ab_data_read() 67 * Implementations will typically want to use avb_ab_data_read()
68 * here to use the 'misc' partition for persistent storage. 68 * here to use the 'misc' partition for persistent storage.
69 */ 69 */
70 AvbIOResult fsl_read_ab_metadata(AvbABOps* ab_ops, struct AvbABData* data); 70 AvbIOResult fsl_read_ab_metadata(AvbABOps* ab_ops, struct AvbABData* data);
71 71
72 /* Writes A/B metadata to persistent storage. This will byteswap and 72 /* Writes A/B metadata to persistent storage. This will byteswap and
73 * update the CRC as needed. Returns AVB_IO_RESULT_OK on success, 73 * update the CRC as needed. Returns AVB_IO_RESULT_OK on success,
74 * error code otherwise. 74 * error code otherwise.
75 * 75 *
76 * Implementations will typically want to use avb_ab_data_write() 76 * Implementations will typically want to use avb_ab_data_write()
77 * here to use the 'misc' partition for persistent storage. 77 * here to use the 'misc' partition for persistent storage.
78 */ 78 */
79 AvbIOResult fsl_write_ab_metadata(AvbABOps* ab_ops, const struct AvbABData* data); 79 AvbIOResult fsl_write_ab_metadata(AvbABOps* ab_ops, const struct AvbABData* data);
80 80
81 /* Checks if the given public key used to sign the 'vbmeta' 81 /* Checks if the given public key used to sign the 'vbmeta'
82 * partition is trusted. Boot loaders typically compare this with 82 * partition is trusted. Boot loaders typically compare this with
83 * embedded key material generated with 'avbtool 83 * embedded key material generated with 'avbtool
84 * extract_public_key'. 84 * extract_public_key'.
85 * 85 *
86 * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set - 86 * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
87 * true if trusted or false if untrusted. 87 * true if trusted or false if untrusted.
88 */ 88 */
89 AvbIOResult fsl_validate_vbmeta_public_key_rpmb(AvbOps* ops, 89 AvbIOResult fsl_validate_vbmeta_public_key_rpmb(AvbOps* ops,
90 const uint8_t* public_key_data, 90 const uint8_t* public_key_data,
91 size_t public_key_length, 91 size_t public_key_length,
92 const uint8_t* public_key_metadata, 92 const uint8_t* public_key_metadata,
93 size_t public_key_metadata_length, 93 size_t public_key_metadata_length,
94 bool* out_is_trusted); 94 bool* out_is_trusted);
95 95
96 /* Gets the rollback index corresponding to the slot given by 96 /* Gets the rollback index corresponding to the slot given by
97 * |rollback_index_slot|. The value is returned in 97 * |rollback_index_slot|. The value is returned in
98 * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback 98 * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
99 * index was retrieved, otherwise an error code. 99 * index was retrieved, otherwise an error code.
100 * 100 *
101 * A device may have a limited amount of rollback index slots (say, 101 * A device may have a limited amount of rollback index slots (say,
102 * one or four) so may error out if |rollback_index_slot| exceeds 102 * one or four) so may error out if |rollback_index_slot| exceeds
103 * this number. 103 * this number.
104 */ 104 */
105 AvbIOResult fsl_read_rollback_index_rpmb(AvbOps* ops, size_t rollback_index_slot, 105 AvbIOResult fsl_read_rollback_index_rpmb(AvbOps* ops, size_t rollback_index_slot,
106 uint64_t* out_rollback_index); 106 uint64_t* out_rollback_index);
107 107
108 /* Sets the rollback index corresponding to the slot given by 108 /* Sets the rollback index corresponding to the slot given by
109 * |rollback_index_slot| to |rollback_index|. Returns 109 * |rollback_index_slot| to |rollback_index|. Returns
110 * AVB_IO_RESULT_OK if the rollback index was set, otherwise an 110 * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
111 * error code. 111 * error code.
112 * 112 *
113 * A device may have a limited amount of rollback index slots (say, 113 * A device may have a limited amount of rollback index slots (say,
114 * one or four) so may error out if |rollback_index_slot| exceeds 114 * one or four) so may error out if |rollback_index_slot| exceeds
115 * this number. 115 * this number.
116 */ 116 */
117 AvbIOResult fsl_write_rollback_index_rpmb(AvbOps* ops, size_t rollback_index_slot, 117 AvbIOResult fsl_write_rollback_index_rpmb(AvbOps* ops, size_t rollback_index_slot,
118 uint64_t rollback_index); 118 uint64_t rollback_index);
119 119
120 /* Gets whether the device is unlocked. The value is returned in 120 /* Gets whether the device is unlocked. The value is returned in
121 * |out_is_unlocked| (true if unlocked, false otherwise). Returns 121 * |out_is_unlocked| (true if unlocked, false otherwise). Returns
122 * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error 122 * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
123 * code. 123 * code.
124 */ 124 */
125 AvbIOResult fsl_read_is_device_unlocked(AvbOps* ops, bool* out_is_unlocked); 125 AvbIOResult fsl_read_is_device_unlocked(AvbOps* ops, bool* out_is_unlocked);
126 126
127 /* Gets the unique partition GUID for a partition with name in 127 /* Gets the unique partition GUID for a partition with name in
128 * |partition| (NUL-terminated UTF-8 string). The GUID is copied as 128 * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
129 * a string into |guid_buf| of size |guid_buf_size| and will be NUL 129 * a string into |guid_buf| of size |guid_buf_size| and will be NUL
130 * terminated. The string must be lower-case and properly 130 * terminated. The string must be lower-case and properly
131 * hyphenated. For example: 131 * hyphenated. For example:
132 * 132 *
133 * 527c1c6d-6361-4593-8842-3c78fcd39219 133 * 527c1c6d-6361-4593-8842-3c78fcd39219
134 * 134 *
135 * Returns AVB_IO_RESULT_OK on success, otherwise an error code. 135 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
136 */ 136 */
137 AvbIOResult fsl_get_unique_guid_for_partition(AvbOps* ops, 137 AvbIOResult fsl_get_unique_guid_for_partition(AvbOps* ops,
138 const char* partition, 138 const char* partition,
139 char* guid_buf, 139 char* guid_buf,
140 size_t guid_buf_size); 140 size_t guid_buf_size);
141 141
142 /* Gets the size of a partition with the name in |partition| 142 /* Gets the size of a partition with the name in |partition|
143 * (NUL-terminated UTF-8 string). Returns the value in 143 * (NUL-terminated UTF-8 string). Returns the value in
144 * |out_size_num_bytes|. 144 * |out_size_num_bytes|.
145 * Returns AVB_IO_RESULT_OK on success, otherwise an error code. 145 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
146 */ 146 */
147 AvbIOResult fsl_get_size_of_partition(AvbOps* ops, 147 AvbIOResult fsl_get_size_of_partition(AvbOps* ops,
148 const char* partition, 148 const char* partition,
149 uint64_t* out_size_num_bytes); 149 uint64_t* out_size_num_bytes);
150 /* check if the fastboot getvar cmd is for query [avb] bootctl's slot var 150 /* check if the fastboot getvar cmd is for query [avb] bootctl's slot var
151 * cmd is the fastboot getvar's cmd in 151 * cmd is the fastboot getvar's cmd in
152 * return true if it is a bootctl related cmd, false if it's not. 152 * return true if it is a bootctl related cmd, false if it's not.
153 * */ 153 * */
154 bool is_slotvar_avb(char *cmd); 154 bool is_slotvar_avb(char *cmd);
155 155
156 /* Get current bootable slot with higher priority. 156 /* Get current bootable slot with higher priority.
157 * return 0 for the first slot 157 * return 0 for the first slot
158 * return 1 for the second slot 158 * return 1 for the second slot
159 * return -1 for not supported slot 159 * return -1 for not supported slot
160 * */ 160 * */
161 int get_curr_slot(AvbABData *ab_data); 161 int get_curr_slot(AvbABData *ab_data);
162 162
163 /* return 0 for the first slot 163 /* return 0 for the first slot
164 * return 1 for the second slot 164 * return 1 for the second slot
165 * return -1 for not supported slot 165 * return -1 for not supported slot
166 * */ 166 * */
167 int slotidx_from_suffix(char *suffix); 167 int slotidx_from_suffix(char *suffix);
168 168
169 /* return fastboot's getvar cmd response 169 /* return fastboot's getvar cmd response
170 * cmd is the fastboot getvar's cmd in 170 * cmd is the fastboot getvar's cmd in
171 * if return 0, buffer is bootctl's slot var out 171 * if return 0, buffer is bootctl's slot var out
172 * if return -1, buffer is error string 172 * if return -1, buffer is error string
173 * */ 173 * */
174 int get_slotvar_avb(AvbABOps *ab_ops, char *cmd, char *buffer, size_t size); 174 int get_slotvar_avb(AvbABOps *ab_ops, char *cmd, char *buffer, size_t size);
175 175
176 /* reset rollback_index part in avbkey partition 176 /* reset rollback_index part in avbkey partition
177 * used in the switch from LOCK to UNLOCK 177 * used in the switch from LOCK to UNLOCK
178 * return 0 if success, non 0 if fail. 178 * return 0 if success, non 0 if fail.
179 * */ 179 * */
180 int rbkidx_erase(void); 180 int rbkidx_erase(void);
181 181
182 /* init the avbkey in rpmb partition, include the header/public key/rollback index 182 /* init the avbkey in rpmb partition, include the header/public key/rollback index
183 * for public key/rollback index part, use caam to do encrypt 183 * for public key/rollback index part, use caam to do encrypt
184 * return 0 if success, non 0 if fail. 184 * return 0 if success, non 0 if fail.
185 * */ 185 * */
186 int avbkey_init(uint8_t *plainkey, uint32_t keylen); 186 int avbkey_init(uint8_t *plainkey, uint32_t keylen);
187 187
188 /* read a/b metadata to get curr slot 188 /* read a/b metadata to get curr slot
189 * return slot suffix '_a'/'_b' or NULL */ 189 * return slot suffix '_a'/'_b' or NULL */
190 char *select_slot(AvbABOps *ab_ops); 190 char *select_slot(AvbABOps *ab_ops);
191 191
192 /* Reads permanent |attributes| data. There are no restrictions on where this 192 /* Reads permanent |attributes| data. There are no restrictions on where this
193 * data is stored. On success, returns AVB_IO_RESULT_OK and populates 193 * data is stored. On success, returns AVB_IO_RESULT_OK and populates
194 * |attributes|. 194 * |attributes|.
195 */ 195 */
196 AvbIOResult fsl_read_permanent_attributes( 196 AvbIOResult fsl_read_permanent_attributes(
197 AvbAtxOps* atx_ops, AvbAtxPermanentAttributes* attributes); 197 AvbAtxOps* atx_ops, AvbAtxPermanentAttributes* attributes);
198 198
199 /* Reads a |hash| of permanent attributes. This hash MUST be retrieved from a 199 /* Reads a |hash| of permanent attributes. This hash MUST be retrieved from a
200 * permanently read-only location (e.g. fuses) when a device is LOCKED. On 200 * permanently read-only location (e.g. fuses) when a device is LOCKED. On
201 * success, returned AVB_IO_RESULT_OK and populates |hash|. 201 * success, returned AVB_IO_RESULT_OK and populates |hash|.
202 */ 202 */
203 AvbIOResult fsl_read_permanent_attributes_hash(AvbAtxOps* atx_ops, 203 AvbIOResult fsl_read_permanent_attributes_hash(AvbAtxOps* atx_ops,
204 uint8_t hash[AVB_SHA256_DIGEST_SIZE]); 204 uint8_t hash[AVB_SHA256_DIGEST_SIZE]);
205 205
206 /* Provides the key version of a key used during verification. This may be 206 /* Provides the key version of a key used during verification. This may be
207 * useful for managing the minimum key version. 207 * useful for managing the minimum key version.
208 */ 208 */
209 void fsl_set_key_version(AvbAtxOps* atx_ops, 209 void fsl_set_key_version(AvbAtxOps* atx_ops,
210 size_t rollback_index_location, 210 size_t rollback_index_location,
211 uint64_t key_version); 211 uint64_t key_version);
212 212
213 /* This is the fast version of avb_ab_flow(), this function will 213 /* This is the fast version of avb_ab_flow(), this function will
214 * not check another slot if one slot can pass the verify (or verify 214 * not check another slot if one slot can pass the verify (or verify
215 * fail is acceptable). 215 * fail is acceptable).
216 */ 216 */
217 AvbABFlowResult avb_ab_flow_fast(AvbABOps* ab_ops, 217 AvbABFlowResult avb_ab_flow_fast(AvbABOps* ab_ops,
218 const char* const* requested_partitions, 218 const char* const* requested_partitions,
219 AvbSlotVerifyFlags flags, 219 AvbSlotVerifyFlags flags,
220 AvbHashtreeErrorMode hashtree_error_mode, 220 AvbHashtreeErrorMode hashtree_error_mode,
221 AvbSlotVerifyData** out_data); 221 AvbSlotVerifyData** out_data);
222 222
223 /* This is for legacy i.mx6/7 which don't enable A/B but want to 223 /* This is for legacy i.mx6/7 which don't enable A/B but want to
224 * verify boot/recovery with AVB */ 224 * verify boot/recovery with AVB */
225 AvbABFlowResult avb_single_flow(AvbABOps* ab_ops, 225 AvbABFlowResult avb_single_flow(AvbABOps* ab_ops,
226 const char* const* requested_partitions, 226 const char* const* requested_partitions,
227 AvbSlotVerifyFlags flags, 227 AvbSlotVerifyFlags flags,
228 AvbHashtreeErrorMode hashtree_error_mode, 228 AvbHashtreeErrorMode hashtree_error_mode,
229 AvbSlotVerifyData** out_data); 229 AvbSlotVerifyData** out_data);
230 230
231 /* Avb verify flow for dual bootloader, only the slot chosen by SPL will 231 /* Avb verify flow for dual bootloader, only the slot chosen by SPL will
232 * be verified. 232 * be verified.
233 */ 233 */
234 AvbABFlowResult avb_flow_dual_uboot(AvbABOps* ab_ops, 234 AvbABFlowResult avb_flow_dual_uboot(AvbABOps* ab_ops,
235 const char* const* requested_partitions, 235 const char* const* requested_partitions,
236 AvbSlotVerifyFlags flags, 236 AvbSlotVerifyFlags flags,
237 AvbHashtreeErrorMode hashtree_error_mode, 237 AvbHashtreeErrorMode hashtree_error_mode,
238 AvbSlotVerifyData** out_data); 238 AvbSlotVerifyData** out_data);
239 /* Generates |num_bytes| random bytes and stores them in |output|, 239 /* Generates |num_bytes| random bytes and stores them in |output|,
240 * which must point to a buffer large enough to store the bytes. 240 * which must point to a buffer large enough to store the bytes.
241 * 241 *
242 * Returns AVB_IO_RESULT_OK on success, otherwise an error code. 242 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
243 */ 243 */
244 AvbIOResult fsl_get_random(AvbAtxOps* atx_ops, 244 AvbIOResult fsl_get_random(AvbAtxOps* atx_ops,
245 size_t num_bytes, 245 size_t num_bytes,
246 uint8_t* output); 246 uint8_t* output);
247 247
248 /* Program ATX perm_attr into RPMB partition */ 248 /* Program ATX perm_attr into RPMB partition */
249 int avb_atx_fuse_perm_attr(uint8_t *staged_buffer, uint32_t size); 249 int avb_atx_fuse_perm_attr(uint8_t *staged_buffer, uint32_t size);
250 250
251 /* Initialize rpmb key with the staged key */ 251 /* Initialize rpmb key with the staged key */
252 int fastboot_set_rpmb_key(uint8_t *staged_buf, uint32_t key_size); 252 int fastboot_set_rpmb_key(uint8_t *staged_buf, uint32_t key_size);
253 253
254 /* Initialize rpmb key with random key which is generated by caam rng */
255 int fastboot_set_rpmb_random_key(void);
256
254 /* Generate ATX unlock challenge */ 257 /* Generate ATX unlock challenge */
255 int avb_atx_get_unlock_challenge(struct AvbAtxOps* atx_ops, 258 int avb_atx_get_unlock_challenge(struct AvbAtxOps* atx_ops,
256 uint8_t *upload_buffer, uint32_t *size); 259 uint8_t *upload_buffer, uint32_t *size);
257 /* Verify ATX unlock credential */ 260 /* Verify ATX unlock credential */
258 int avb_atx_verify_unlock_credential(struct AvbAtxOps* atx_ops, 261 int avb_atx_verify_unlock_credential(struct AvbAtxOps* atx_ops,
259 uint8_t *staged_buffer); 262 uint8_t *staged_buffer);
260 /* Check if the perm-attr have been fused. */ 263 /* Check if the perm-attr have been fused. */
261 bool perm_attr_are_fused(void); 264 bool perm_attr_are_fused(void);
262 265
263 /* Check if the unlock vboot is already disabled */ 266 /* Check if the unlock vboot is already disabled */
264 bool at_unlock_vboot_is_disabled(void); 267 bool at_unlock_vboot_is_disabled(void);
265 268
266 /* disable at unlock vboot */ 269 /* disable at unlock vboot */
267 int at_disable_vboot_unlock(void); 270 int at_disable_vboot_unlock(void);
268 271
269 /* Set vbmeta public key */ 272 /* Set vbmeta public key */
270 int avb_set_public_key(uint8_t *staged_buffer, uint32_t size); 273 int avb_set_public_key(uint8_t *staged_buffer, uint32_t size);
271 #endif /* __FSL_AVB_H__ */ 274 #endif /* __FSL_AVB_H__ */
272 275
include/fsl_fastboot.h
1 /* 1 /*
2 * Copyright (C) 2010-2016 Freescale Semiconductor, Inc. 2 * Copyright (C) 2010-2016 Freescale Semiconductor, Inc.
3 * Copyright 2017 NXP 3 * Copyright 2017 NXP
4 * 4 *
5 * SPDX-License-Identifier: GPL-2.0+ 5 * SPDX-License-Identifier: GPL-2.0+
6 */ 6 */
7 7
8 #ifndef FSL_FASTBOOT_H 8 #ifndef FSL_FASTBOOT_H
9 #define FSL_FASTBOOT_H 9 #define FSL_FASTBOOT_H
10 #include <stdbool.h> 10 #include <stdbool.h>
11 #include <linux/types.h> 11 #include <linux/types.h>
12 12
13 #define FASTBOOT_PTENTRY_FLAGS_REPEAT(n) (n & 0x0f) 13 #define FASTBOOT_PTENTRY_FLAGS_REPEAT(n) (n & 0x0f)
14 #define FASTBOOT_PTENTRY_FLAGS_REPEAT_MASK 0x0000000F 14 #define FASTBOOT_PTENTRY_FLAGS_REPEAT_MASK 0x0000000F
15 15
16 /* Writes happen a block at a time. 16 /* Writes happen a block at a time.
17 If the write fails, go to next block 17 If the write fails, go to next block
18 NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */ 18 NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */
19 #define FASTBOOT_PTENTRY_FLAGS_WRITE_NEXT_GOOD_BLOCK 0x00000010 19 #define FASTBOOT_PTENTRY_FLAGS_WRITE_NEXT_GOOD_BLOCK 0x00000010
20 20
21 /* Find a contiguous block big enough for a the whole file 21 /* Find a contiguous block big enough for a the whole file
22 NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */ 22 NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */
23 #define FASTBOOT_PTENTRY_FLAGS_WRITE_CONTIGUOUS_BLOCK 0x00000020 23 #define FASTBOOT_PTENTRY_FLAGS_WRITE_CONTIGUOUS_BLOCK 0x00000020
24 24
25 /* Write the file with write.i */ 25 /* Write the file with write.i */
26 #define FASTBOOT_PTENTRY_FLAGS_WRITE_I 0x00000100 26 #define FASTBOOT_PTENTRY_FLAGS_WRITE_I 0x00000100
27 27
28 /* Write the file with write.trimffs */ 28 /* Write the file with write.trimffs */
29 #define FASTBOOT_PTENTRY_FLAGS_WRITE_TRIMFFS 0x00000200 29 #define FASTBOOT_PTENTRY_FLAGS_WRITE_TRIMFFS 0x00000200
30 30
31 /* Write the file as a series of variable/value pairs 31 /* Write the file as a series of variable/value pairs
32 using the setenv and saveenv commands */ 32 using the setenv and saveenv commands */
33 #define FASTBOOT_PTENTRY_FLAGS_WRITE_ENV 0x00000400 33 #define FASTBOOT_PTENTRY_FLAGS_WRITE_ENV 0x00000400
34 34
35 /* Uneraseable partition */ 35 /* Uneraseable partition */
36 #define FASTBOOT_PTENTRY_FLAGS_UNERASEABLE 0x00000800 36 #define FASTBOOT_PTENTRY_FLAGS_UNERASEABLE 0x00000800
37 37
38 #define FASTBOOT_MMC_BOOT_PARTITION_ID 1 38 #define FASTBOOT_MMC_BOOT_PARTITION_ID 1
39 #define FASTBOOT_MMC_USER_PARTITION_ID 0 39 #define FASTBOOT_MMC_USER_PARTITION_ID 0
40 #define FASTBOOT_MMC_NONE_PARTITION_ID -1 40 #define FASTBOOT_MMC_NONE_PARTITION_ID -1
41 #define FASTBOOT_MMC_BOOT1_PARTITION_ID 2 41 #define FASTBOOT_MMC_BOOT1_PARTITION_ID 2
42 42
43 #define FASTBOOT_PARTITION_TEE "tos" 43 #define FASTBOOT_PARTITION_TEE "tos"
44 #define FASTBOOT_PARTITION_PRDATA "presistdata" 44 #define FASTBOOT_PARTITION_PRDATA "presistdata"
45 45
46 #ifdef CONFIG_AVB_SUPPORT 46 #ifdef CONFIG_AVB_SUPPORT
47 #define FASTBOOT_PARTITION_AVBKEY "avbkey" 47 #define FASTBOOT_PARTITION_AVBKEY "avbkey"
48 #endif 48 #endif
49 49
50 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 50 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
51 #define FASTBOOT_MCU_FIRMWARE_PARTITION "m4_os" 51 #define FASTBOOT_MCU_FIRMWARE_PARTITION "m4_os"
52 #endif 52 #endif
53 53
54 #ifdef CONFIG_ANDROID_AB_SUPPORT 54 #ifdef CONFIG_ANDROID_AB_SUPPORT
55 #define FASTBOOT_PARTITION_BOOT_A "boot_a" 55 #define FASTBOOT_PARTITION_BOOT_A "boot_a"
56 #define FASTBOOT_PARTITION_RECOVERY "recovery" 56 #define FASTBOOT_PARTITION_RECOVERY "recovery"
57 #define FASTBOOT_PARTITION_SYSTEM_A "system_a" 57 #define FASTBOOT_PARTITION_SYSTEM_A "system_a"
58 #define FASTBOOT_PARTITION_BOOTLOADER "bootloader0" 58 #define FASTBOOT_PARTITION_BOOTLOADER "bootloader0"
59 #define FASTBOOT_PARTITION_DATA "userdata" 59 #define FASTBOOT_PARTITION_DATA "userdata"
60 #define FASTBOOT_PARTITION_BOOT_B "boot_b" 60 #define FASTBOOT_PARTITION_BOOT_B "boot_b"
61 #define FASTBOOT_PARTITION_SYSTEM_B "system_b" 61 #define FASTBOOT_PARTITION_SYSTEM_B "system_b"
62 #define FASTBOOT_PARTITION_OEM_A "oem_a" 62 #define FASTBOOT_PARTITION_OEM_A "oem_a"
63 #define FASTBOOT_PARTITION_VENDOR_A "vendor_a" 63 #define FASTBOOT_PARTITION_VENDOR_A "vendor_a"
64 #define FASTBOOT_PARTITION_OEM_B "oem_b" 64 #define FASTBOOT_PARTITION_OEM_B "oem_b"
65 #define FASTBOOT_PARTITION_VENDOR_B "vendor_b" 65 #define FASTBOOT_PARTITION_VENDOR_B "vendor_b"
66 #ifdef CONFIG_AVB_SUPPORT 66 #ifdef CONFIG_AVB_SUPPORT
67 #define FASTBOOT_PARTITION_VBMETA_A "vbmeta_a" 67 #define FASTBOOT_PARTITION_VBMETA_A "vbmeta_a"
68 #define FASTBOOT_PARTITION_VBMETA_B "vbmeta_b" 68 #define FASTBOOT_PARTITION_VBMETA_B "vbmeta_b"
69 #endif 69 #endif
70 #define FASTBOOT_PARTITION_MISC "misc" 70 #define FASTBOOT_PARTITION_MISC "misc"
71 #define FASTBOOT_PARTITION_GPT "gpt" 71 #define FASTBOOT_PARTITION_GPT "gpt"
72 #define FASTBOOT_PARTITION_FBMISC "fbmisc" 72 #define FASTBOOT_PARTITION_FBMISC "fbmisc"
73 #else 73 #else
74 #define FASTBOOT_PARTITION_BOOT "boot" 74 #define FASTBOOT_PARTITION_BOOT "boot"
75 #define FASTBOOT_PARTITION_RECOVERY "recovery" 75 #define FASTBOOT_PARTITION_RECOVERY "recovery"
76 #define FASTBOOT_PARTITION_SYSTEM "system" 76 #define FASTBOOT_PARTITION_SYSTEM "system"
77 #define FASTBOOT_PARTITION_CACHE "cache" 77 #define FASTBOOT_PARTITION_CACHE "cache"
78 #define FASTBOOT_PARTITION_DEVICE "device" 78 #define FASTBOOT_PARTITION_DEVICE "device"
79 #define FASTBOOT_PARTITION_BOOTLOADER "bootloader" 79 #define FASTBOOT_PARTITION_BOOTLOADER "bootloader"
80 #define FASTBOOT_PARTITION_DATA "userdata" 80 #define FASTBOOT_PARTITION_DATA "userdata"
81 #define FASTBOOT_PARTITION_GPT "gpt" 81 #define FASTBOOT_PARTITION_GPT "gpt"
82 #define FASTBOOT_PARTITION_MISC "misc" 82 #define FASTBOOT_PARTITION_MISC "misc"
83 #define FASTBOOT_PARTITION_FBMISC "fbmisc" 83 #define FASTBOOT_PARTITION_FBMISC "fbmisc"
84 #endif 84 #endif
85 85
86 #ifdef CONFIG_IMX_TRUSTY_OS 86 #ifdef CONFIG_IMX_TRUSTY_OS
87 #ifndef CONFIG_AVB_ATX 87 #ifndef CONFIG_AVB_ATX
88 #define FASTBOOT_SET_RPMB_KEY "set-rpmb-key" 88 #define FASTBOOT_SET_RPMB_KEY "set-rpmb-key"
89 #define FASTBOOT_SET_RPMB_RANDOM_KEY "set-rpmb-random-key"
89 #define FASTBOOT_SET_VBMETA_PUBLIC_KEY "set-public-key" 90 #define FASTBOOT_SET_VBMETA_PUBLIC_KEY "set-public-key"
90 #endif 91 #endif
91 92
92 #define FASTBOOT_SET_CA_RESP "at-set-ca-response" 93 #define FASTBOOT_SET_CA_RESP "at-set-ca-response"
93 #define FASTBOOT_GET_CA_REQ "at-get-ca-request" 94 #define FASTBOOT_GET_CA_REQ "at-get-ca-request"
94 #endif 95 #endif
95 96
96 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 97 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
97 #define FASTBOOT_BOOTLOADER_VBOOT_KEY "fuse at-bootloader-vboot-key" 98 #define FASTBOOT_BOOTLOADER_VBOOT_KEY "fuse at-bootloader-vboot-key"
98 #ifdef CONFIG_AVB_ATX 99 #ifdef CONFIG_AVB_ATX
99 #define FASTBOOT_AVB_AT_PERM_ATTR "fuse at-perm-attr" 100 #define FASTBOOT_AVB_AT_PERM_ATTR "fuse at-perm-attr"
100 #define FASTBOOT_AT_UNLOCK_VBOOT "at-unlock-vboot" 101 #define FASTBOOT_AT_UNLOCK_VBOOT "at-unlock-vboot"
101 #define FASTBOOT_AT_LOCK_VBOOT "at-lock-vboot" 102 #define FASTBOOT_AT_LOCK_VBOOT "at-lock-vboot"
102 #define FASTBOOT_AT_DISABLE_UNLOCK_VBOOT "at-disable-unlock-vboot" 103 #define FASTBOOT_AT_DISABLE_UNLOCK_VBOOT "at-disable-unlock-vboot"
103 #define FASTBOOT_AT_GET_UNLOCK_CHALLENGE "at-get-vboot-unlock-challenge" 104 #define FASTBOOT_AT_GET_UNLOCK_CHALLENGE "at-get-vboot-unlock-challenge"
104 #endif /* CONFIG_AVB_ATX */ 105 #endif /* CONFIG_AVB_ATX */
105 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */ 106 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */
106 107
107 enum { 108 enum {
108 DEV_SATA, 109 DEV_SATA,
109 DEV_MMC, 110 DEV_MMC,
110 DEV_NAND, 111 DEV_NAND,
111 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT 112 #ifdef CONFIG_FLASH_MCUFIRMWARE_SUPPORT
112 /* SPI Flash */ 113 /* SPI Flash */
113 DEV_SF 114 DEV_SF
114 #endif 115 #endif
115 }; 116 };
116 117
117 typedef enum { 118 typedef enum {
118 #ifdef CONFIG_ANDROID_RECOVERY 119 #ifdef CONFIG_ANDROID_RECOVERY
119 /* Revoery boot due to combo keys pressed */ 120 /* Revoery boot due to combo keys pressed */
120 BOOTMODE_RECOVERY_KEY_PRESSED, 121 BOOTMODE_RECOVERY_KEY_PRESSED,
121 /* Recovery boot due to boot-recovery cmd in misc parition */ 122 /* Recovery boot due to boot-recovery cmd in misc parition */
122 BOOTMODE_RECOVERY_BCB_CMD, 123 BOOTMODE_RECOVERY_BCB_CMD,
123 #endif 124 #endif
124 /* Fastboot boot due to bootonce-bootloader cmd in misc parition */ 125 /* Fastboot boot due to bootonce-bootloader cmd in misc parition */
125 BOOTMODE_FASTBOOT_BCB_CMD, 126 BOOTMODE_FASTBOOT_BCB_CMD,
126 /* Normal boot */ 127 /* Normal boot */
127 BOOTMODE_NORMAL 128 BOOTMODE_NORMAL
128 }FbBootMode; 129 }FbBootMode;
129 130
130 131
131 struct cmd_fastboot_interface { 132 struct cmd_fastboot_interface {
132 /* This function is called when a buffer has been 133 /* This function is called when a buffer has been
133 recieved from the client app. 134 recieved from the client app.
134 The buffer is a supplied by the board layer and must be unmodified. 135 The buffer is a supplied by the board layer and must be unmodified.
135 The buffer_size is how much data is passed in. 136 The buffer_size is how much data is passed in.
136 Returns 0 on success 137 Returns 0 on success
137 Returns 1 on failure 138 Returns 1 on failure
138 139
139 Set by cmd_fastboot */ 140 Set by cmd_fastboot */
140 int (*rx_handler)(const unsigned char *buffer, 141 int (*rx_handler)(const unsigned char *buffer,
141 unsigned int buffer_size); 142 unsigned int buffer_size);
142 143
143 /* This function is called when an exception has 144 /* This function is called when an exception has
144 occurred in the device code and the state 145 occurred in the device code and the state
145 off fastboot needs to be reset 146 off fastboot needs to be reset
146 147
147 Set by cmd_fastboot */ 148 Set by cmd_fastboot */
148 void (*reset_handler)(void); 149 void (*reset_handler)(void);
149 150
150 /* A getvar string for the product name 151 /* A getvar string for the product name
151 It can have a maximum of 60 characters 152 It can have a maximum of 60 characters
152 153
153 Set by board */ 154 Set by board */
154 char *product_name; 155 char *product_name;
155 156
156 /* A getvar string for the serial number 157 /* A getvar string for the serial number
157 It can have a maximum of 60 characters 158 It can have a maximum of 60 characters
158 159
159 Set by board */ 160 Set by board */
160 char *serial_no; 161 char *serial_no;
161 162
162 /* Nand block size 163 /* Nand block size
163 Supports the write option WRITE_NEXT_GOOD_BLOCK 164 Supports the write option WRITE_NEXT_GOOD_BLOCK
164 165
165 Set by board */ 166 Set by board */
166 unsigned int nand_block_size; 167 unsigned int nand_block_size;
167 168
168 /* Nand oob size 169 /* Nand oob size
169 Set by board */ 170 Set by board */
170 unsigned int nand_oob_size; 171 unsigned int nand_oob_size;
171 172
172 /* Transfer buffer, for handling flash updates 173 /* Transfer buffer, for handling flash updates
173 Should be multiple of the nand_block_size 174 Should be multiple of the nand_block_size
174 Care should be take so it does not overrun bootloader memory 175 Care should be take so it does not overrun bootloader memory
175 Controlled by the configure variable CFG_FASTBOOT_TRANSFER_BUFFER 176 Controlled by the configure variable CFG_FASTBOOT_TRANSFER_BUFFER
176 177
177 Set by board */ 178 Set by board */
178 unsigned char *transfer_buffer; 179 unsigned char *transfer_buffer;
179 180
180 /* How big is the transfer buffer 181 /* How big is the transfer buffer
181 Controlled by the configure variable 182 Controlled by the configure variable
182 CFG_FASTBOOT_TRANSFER_BUFFER_SIZE 183 CFG_FASTBOOT_TRANSFER_BUFFER_SIZE
183 184
184 Set by board */ 185 Set by board */
185 unsigned int transfer_buffer_size; 186 unsigned int transfer_buffer_size;
186 187
187 }; 188 };
188 189
189 /* flash partitions are defined in terms of blocks 190 /* flash partitions are defined in terms of blocks
190 ** (flash erase units) 191 ** (flash erase units)
191 */ 192 */
192 struct fastboot_ptentry { 193 struct fastboot_ptentry {
193 /* The logical name for this partition, null terminated */ 194 /* The logical name for this partition, null terminated */
194 char name[20]; 195 char name[20];
195 /* The start wrt the nand part, must be multiple of nand block size */ 196 /* The start wrt the nand part, must be multiple of nand block size */
196 unsigned int start; 197 unsigned int start;
197 /* The length of the partition, must be multiple of nand block size */ 198 /* The length of the partition, must be multiple of nand block size */
198 unsigned int length; 199 unsigned int length;
199 /* Controls the details of how operations are done on the partition 200 /* Controls the details of how operations are done on the partition
200 See the FASTBOOT_PTENTRY_FLAGS_*'s defined below */ 201 See the FASTBOOT_PTENTRY_FLAGS_*'s defined below */
201 unsigned int flags; 202 unsigned int flags;
202 /* partition id: 0 - normal partition; 1 - boot partition */ 203 /* partition id: 0 - normal partition; 1 - boot partition */
203 unsigned int partition_id; 204 unsigned int partition_id;
204 /* partition number in block device */ 205 /* partition number in block device */
205 unsigned int partition_index; 206 unsigned int partition_index;
206 /* partition file system type in string */ 207 /* partition file system type in string */
207 char fstype[16]; 208 char fstype[16];
208 /* filesystem UUID as string, if exists */ 209 /* filesystem UUID as string, if exists */
209 #ifdef CONFIG_PARTITION_UUIDS 210 #ifdef CONFIG_PARTITION_UUIDS
210 char uuid[37]; 211 char uuid[37];
211 #endif 212 #endif
212 }; 213 };
213 214
214 struct fastboot_device_info { 215 struct fastboot_device_info {
215 unsigned char type; 216 unsigned char type;
216 unsigned char dev_id; 217 unsigned char dev_id;
217 }; 218 };
218 219
219 extern struct fastboot_device_info fastboot_devinfo; 220 extern struct fastboot_device_info fastboot_devinfo;
220 221
221 /* Prepare the fastboot environments, 222 /* Prepare the fastboot environments,
222 * should be executed before "fastboot" cmd 223 * should be executed before "fastboot" cmd
223 */ 224 */
224 void fastboot_setup(void); 225 void fastboot_setup(void);
225 226
226 227
227 /* The Android-style flash handling */ 228 /* The Android-style flash handling */
228 229
229 /* tools to populate and query the partition table */ 230 /* tools to populate and query the partition table */
230 void fastboot_flash_add_ptn(struct fastboot_ptentry *ptn); 231 void fastboot_flash_add_ptn(struct fastboot_ptentry *ptn);
231 struct fastboot_ptentry *fastboot_flash_find_ptn(const char *name); 232 struct fastboot_ptentry *fastboot_flash_find_ptn(const char *name);
232 struct fastboot_ptentry *fastboot_flash_get_ptn(unsigned n); 233 struct fastboot_ptentry *fastboot_flash_get_ptn(unsigned n);
233 unsigned int fastboot_flash_get_ptn_count(void); 234 unsigned int fastboot_flash_get_ptn_count(void);
234 void fastboot_flash_dump_ptn(void); 235 void fastboot_flash_dump_ptn(void);
235 236
236 /* Make board into special boot mode */ 237 /* Make board into special boot mode */
237 void fastboot_run_bootmode(void); 238 void fastboot_run_bootmode(void);
238 239
239 /*Setup board-relative fastboot environment */ 240 /*Setup board-relative fastboot environment */
240 void board_fastboot_setup(void); 241 void board_fastboot_setup(void);
241 242
242 /*return partition index according name*/ 243 /*return partition index according name*/
243 int fastboot_flash_find_index(const char *name); 244 int fastboot_flash_find_index(const char *name);
244 245
245 /*check whether bootloader is overlay with GPT table*/ 246 /*check whether bootloader is overlay with GPT table*/
246 bool bootloader_gpt_overlay(void); 247 bool bootloader_gpt_overlay(void);
247 /* Check whether the combo keys pressed 248 /* Check whether the combo keys pressed
248 * Return 1 if combo keys pressed for recovery boot 249 * Return 1 if combo keys pressed for recovery boot
249 * Return 0 if no combo keys pressed 250 * Return 0 if no combo keys pressed
250 */ 251 */
251 int is_recovery_key_pressing(void); 252 int is_recovery_key_pressing(void);
252 253
253 #ifdef CONFIG_FASTBOOT_STORAGE_NAND 254 #ifdef CONFIG_FASTBOOT_STORAGE_NAND
254 /*Save parameters for NAND storage partitions */ 255 /*Save parameters for NAND storage partitions */
255 void save_parts_values(struct fastboot_ptentry *ptn, 256 void save_parts_values(struct fastboot_ptentry *ptn,
256 unsigned int offset, unsigned int size); 257 unsigned int offset, unsigned int size);
257 258
258 /* Checks parameters for NAND storage partitions 259 /* Checks parameters for NAND storage partitions
259 * Return 1 if the parameter is not set 260 * Return 1 if the parameter is not set
260 * Return 0 if the parameter has been set 261 * Return 0 if the parameter has been set
261 */ 262 */
262 int check_parts_values(struct fastboot_ptentry *ptn); 263 int check_parts_values(struct fastboot_ptentry *ptn);
263 #endif /*CONFIG_FASTBOOT_STORAGE_NAND*/ 264 #endif /*CONFIG_FASTBOOT_STORAGE_NAND*/
264 265
265 /* Reads |num_bytes| from offset |offset| from partition with name 266 /* Reads |num_bytes| from offset |offset| from partition with name
266 * |partition| (NUL-terminated UTF-8 string). If |offset| is 267 * |partition| (NUL-terminated UTF-8 string). If |offset| is
267 * negative, its absolute value should be interpreted as the number 268 * negative, its absolute value should be interpreted as the number
268 * of bytes from the end of the partition. 269 * of bytes from the end of the partition.
269 * It's basically copied from fsl_read_from_partition_multi() because 270 * It's basically copied from fsl_read_from_partition_multi() because
270 * we may want to read partition when AVB is not enabled. */ 271 * we may want to read partition when AVB is not enabled. */
271 int read_from_partition_multi(const char* partition, 272 int read_from_partition_multi(const char* partition,
272 int64_t offset, size_t num_bytes,void* buffer, size_t* out_num_read); 273 int64_t offset, size_t num_bytes,void* buffer, size_t* out_num_read);
273 #endif /* FSL_FASTBOOT_H */ 274 #endif /* FSL_FASTBOOT_H */
274 275
lib/avb/fsl/fsl_avbkey.c
1 /* 1 /*
2 * Copyright (C) 2016 Freescale Semiconductor, Inc. 2 * Copyright (C) 2016 Freescale Semiconductor, Inc.
3 * Copyright 2017 NXP 3 * Copyright 2017 NXP
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 * 5 *
6 */ 6 */
7 #include <common.h> 7 #include <common.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #ifdef CONFIG_FSL_CAAM_KB 9 #ifdef CONFIG_FSL_CAAM_KB
10 #include <fsl_caam.h> 10 #include <fsl_caam.h>
11 #endif 11 #endif
12 #include <fuse.h> 12 #include <fuse.h>
13 #include <mmc.h> 13 #include <mmc.h>
14 #include <hash.h> 14 #include <hash.h>
15 #include <mapmem.h> 15 #include <mapmem.h>
16 16
17 #include <fsl_avb.h> 17 #include <fsl_avb.h>
18 #include "trusty/avb.h" 18 #include "trusty/avb.h"
19 #ifdef CONFIG_IMX_TRUSTY_OS 19 #ifdef CONFIG_IMX_TRUSTY_OS
20 #include <trusty/libtipc.h> 20 #include <trusty/libtipc.h>
21 #endif 21 #endif
22 #include "fsl_avbkey.h" 22 #include "fsl_avbkey.h"
23 #include "utils.h" 23 #include "utils.h"
24 #include "debug.h" 24 #include "debug.h"
25 #include <memalign.h> 25 #include <memalign.h>
26 #include "trusty/hwcrypto.h" 26 #include "trusty/hwcrypto.h"
27 #include "fsl_atx_attributes.h" 27 #include "fsl_atx_attributes.h"
28 28
29 #define INITFLAG_FUSE_OFFSET 0 29 #define INITFLAG_FUSE_OFFSET 0
30 #define INITFLAG_FUSE_MASK 0x00000001 30 #define INITFLAG_FUSE_MASK 0x00000001
31 #define INITFLAG_FUSE 0x00000001 31 #define INITFLAG_FUSE 0x00000001
32 32
33 #define RPMB_BLKSZ 256 33 #define RPMB_BLKSZ 256
34 #define RPMBKEY_LENGTH 32 34 #define RPMBKEY_LENGTH 32
35 #define RPMBKEY_BLOB_LEN ((RPMBKEY_LENGTH) + (CAAM_PAD)) 35 #define RPMBKEY_BLOB_LEN ((RPMBKEY_LENGTH) + (CAAM_PAD))
36 36
37 extern int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value); 37 extern int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value);
38 38
39 #ifdef AVB_RPMB 39 #ifdef AVB_RPMB
40 static int mmc_dev_no = -1; 40 static int mmc_dev_no = -1;
41 41
42 struct mmc *get_mmc(void) { 42 struct mmc *get_mmc(void) {
43 extern int mmc_get_env_devno(void); 43 extern int mmc_get_env_devno(void);
44 struct mmc *mmc; 44 struct mmc *mmc;
45 if (mmc_dev_no < 0 && (mmc_dev_no = mmc_get_env_dev()) < 0) 45 if (mmc_dev_no < 0 && (mmc_dev_no = mmc_get_env_dev()) < 0)
46 return NULL; 46 return NULL;
47 mmc = find_mmc_device(mmc_dev_no); 47 mmc = find_mmc_device(mmc_dev_no);
48 if (!mmc || mmc_init(mmc)) 48 if (!mmc || mmc_init(mmc))
49 return NULL; 49 return NULL;
50 return mmc; 50 return mmc;
51 } 51 }
52 52
53 void fill_secure_keyslot_package(struct keyslot_package *kp) { 53 void fill_secure_keyslot_package(struct keyslot_package *kp) {
54 54
55 memcpy((void*)CAAM_ARB_BASE_ADDR, kp, sizeof(struct keyslot_package)); 55 memcpy((void*)CAAM_ARB_BASE_ADDR, kp, sizeof(struct keyslot_package));
56 56
57 /* invalidate the cache to make sure no critical information left in it */ 57 /* invalidate the cache to make sure no critical information left in it */
58 memset(kp, 0, sizeof(struct keyslot_package)); 58 memset(kp, 0, sizeof(struct keyslot_package));
59 invalidate_dcache_range(((ulong)kp) & 0xffffffc0,(((((ulong)kp) + 59 invalidate_dcache_range(((ulong)kp) & 0xffffffc0,(((((ulong)kp) +
60 sizeof(struct keyslot_package)) & 0xffffff00) + 60 sizeof(struct keyslot_package)) & 0xffffff00) +
61 0x100)); 61 0x100));
62 } 62 }
63 63
64 int read_keyslot_package(struct keyslot_package* kp) { 64 int read_keyslot_package(struct keyslot_package* kp) {
65 char original_part; 65 char original_part;
66 int blksz; 66 int blksz;
67 unsigned char* fill = NULL; 67 unsigned char* fill = NULL;
68 int ret = 0; 68 int ret = 0;
69 /* load tee from boot1 of eMMC. */ 69 /* load tee from boot1 of eMMC. */
70 int mmcc = mmc_get_env_dev(); 70 int mmcc = mmc_get_env_dev();
71 struct blk_desc *dev_desc = NULL; 71 struct blk_desc *dev_desc = NULL;
72 72
73 struct mmc *mmc; 73 struct mmc *mmc;
74 mmc = find_mmc_device(mmcc); 74 mmc = find_mmc_device(mmcc);
75 if (!mmc) { 75 if (!mmc) {
76 printf("boota: cannot find '%d' mmc device\n", mmcc); 76 printf("boota: cannot find '%d' mmc device\n", mmcc);
77 return -1; 77 return -1;
78 } 78 }
79 #ifndef CONFIG_BLK 79 #ifndef CONFIG_BLK
80 original_part = mmc->block_dev.hwpart; 80 original_part = mmc->block_dev.hwpart;
81 dev_desc = blk_get_dev("mmc", mmcc); 81 dev_desc = blk_get_dev("mmc", mmcc);
82 #else 82 #else
83 dev_desc = mmc_get_blk_desc(mmc); 83 dev_desc = mmc_get_blk_desc(mmc);
84 original_part = dev_desc->hwpart; 84 original_part = dev_desc->hwpart;
85 #endif 85 #endif
86 if (NULL == dev_desc) { 86 if (NULL == dev_desc) {
87 printf("** Block device MMC %d not supported\n", mmcc); 87 printf("** Block device MMC %d not supported\n", mmcc);
88 return -1; 88 return -1;
89 } 89 }
90 90
91 blksz = dev_desc->blksz; 91 blksz = dev_desc->blksz;
92 fill = (unsigned char *)memalign(ALIGN_BYTES, blksz); 92 fill = (unsigned char *)memalign(ALIGN_BYTES, blksz);
93 93
94 /* below was i.MX mmc operation code */ 94 /* below was i.MX mmc operation code */
95 if (mmc_init(mmc)) { 95 if (mmc_init(mmc)) {
96 printf("mmc%d init failed\n", mmcc); 96 printf("mmc%d init failed\n", mmcc);
97 ret = -1; 97 ret = -1;
98 goto fail;; 98 goto fail;;
99 } 99 }
100 100
101 mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID); 101 mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID);
102 #ifndef CONFIG_BLK 102 #ifndef CONFIG_BLK
103 mmc->block_dev.hwpart = KEYSLOT_HWPARTITION_ID; 103 mmc->block_dev.hwpart = KEYSLOT_HWPARTITION_ID;
104 #else 104 #else
105 dev_desc->hwpart = KEYSLOT_HWPARTITION_ID; 105 dev_desc->hwpart = KEYSLOT_HWPARTITION_ID;
106 #endif 106 #endif
107 if (blk_dread(dev_desc, KEYSLOT_BLKS, 107 if (blk_dread(dev_desc, KEYSLOT_BLKS,
108 1, fill) != 1) { 108 1, fill) != 1) {
109 printf("Failed to read rpmbkeyblob."); 109 printf("Failed to read rpmbkeyblob.");
110 ret = -1; 110 ret = -1;
111 goto fail; 111 goto fail;
112 } else { 112 } else {
113 memcpy(kp, fill, sizeof(struct keyslot_package)); 113 memcpy(kp, fill, sizeof(struct keyslot_package));
114 } 114 }
115 115
116 fail: 116 fail:
117 /* Free allocated memory. */ 117 /* Free allocated memory. */
118 if (fill != NULL) 118 if (fill != NULL)
119 free(fill); 119 free(fill);
120 /* Return to original partition */ 120 /* Return to original partition */
121 #ifndef CONFIG_BLK 121 #ifndef CONFIG_BLK
122 if (mmc->block_dev.hwpart != original_part) { 122 if (mmc->block_dev.hwpart != original_part) {
123 if (mmc_switch_part(mmc, original_part) != 0) 123 if (mmc_switch_part(mmc, original_part) != 0)
124 return -1; 124 return -1;
125 mmc->block_dev.hwpart = original_part; 125 mmc->block_dev.hwpart = original_part;
126 } 126 }
127 #else 127 #else
128 if (dev_desc->hwpart != original_part) { 128 if (dev_desc->hwpart != original_part) {
129 if (mmc_switch_part(mmc, original_part) != 0) 129 if (mmc_switch_part(mmc, original_part) != 0)
130 return -1; 130 return -1;
131 dev_desc->hwpart = original_part; 131 dev_desc->hwpart = original_part;
132 } 132 }
133 #endif 133 #endif
134 return ret; 134 return ret;
135 } 135 }
136 136
137 #ifdef CONFIG_FSL_CAAM_KB 137 #ifdef CONFIG_FSL_CAAM_KB
138 int rpmb_read(struct mmc *mmc, uint8_t *buffer, size_t num_bytes, int64_t offset) { 138 int rpmb_read(struct mmc *mmc, uint8_t *buffer, size_t num_bytes, int64_t offset) {
139 139
140 unsigned char *bdata = NULL; 140 unsigned char *bdata = NULL;
141 unsigned char *out_buf = (unsigned char *)buffer; 141 unsigned char *out_buf = (unsigned char *)buffer;
142 unsigned long s, cnt; 142 unsigned long s, cnt;
143 unsigned long blksz; 143 unsigned long blksz;
144 size_t num_read = 0; 144 size_t num_read = 0;
145 unsigned short part_start, part_length, part_end, bs, be; 145 unsigned short part_start, part_length, part_end, bs, be;
146 margin_pos_t margin; 146 margin_pos_t margin;
147 char original_part; 147 char original_part;
148 uint8_t *blob = NULL; 148 uint8_t *blob = NULL;
149 struct blk_desc *desc = mmc_get_blk_desc(mmc); 149 struct blk_desc *desc = mmc_get_blk_desc(mmc);
150 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, extract_key, RPMBKEY_LENGTH); 150 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, extract_key, RPMBKEY_LENGTH);
151 151
152 struct keyslot_package kp; 152 struct keyslot_package kp;
153 int ret; 153 int ret;
154 154
155 blksz = RPMB_BLKSZ; 155 blksz = RPMB_BLKSZ;
156 part_length = mmc->capacity_rpmb >> 8; 156 part_length = mmc->capacity_rpmb >> 8;
157 part_start = 0; 157 part_start = 0;
158 part_end = part_start + part_length - 1; 158 part_end = part_start + part_length - 1;
159 159
160 DEBUGAVB("[rpmb]: offset=%ld, num_bytes=%zu\n", (long)offset, num_bytes); 160 DEBUGAVB("[rpmb]: offset=%ld, num_bytes=%zu\n", (long)offset, num_bytes);
161 161
162 if(get_margin_pos(part_start, part_end, blksz, 162 if(get_margin_pos(part_start, part_end, blksz,
163 &margin, offset, num_bytes, false)) 163 &margin, offset, num_bytes, false))
164 return -1; 164 return -1;
165 165
166 bs = (unsigned short)margin.blk_start; 166 bs = (unsigned short)margin.blk_start;
167 be = (unsigned short)margin.blk_end; 167 be = (unsigned short)margin.blk_end;
168 s = margin.start; 168 s = margin.start;
169 169
170 /* Switch to the RPMB partition */ 170 /* Switch to the RPMB partition */
171 original_part = desc->hwpart; 171 original_part = desc->hwpart;
172 if (desc->hwpart != MMC_PART_RPMB) { 172 if (desc->hwpart != MMC_PART_RPMB) {
173 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) 173 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0)
174 return -1; 174 return -1;
175 desc->hwpart = MMC_PART_RPMB; 175 desc->hwpart = MMC_PART_RPMB;
176 } 176 }
177 177
178 /* get rpmb key */ 178 /* get rpmb key */
179 blob = (uint8_t *)memalign(ARCH_DMA_MINALIGN, RPMBKEY_BLOB_LEN); 179 blob = (uint8_t *)memalign(ARCH_DMA_MINALIGN, RPMBKEY_BLOB_LEN);
180 if (read_keyslot_package(&kp)) { 180 if (read_keyslot_package(&kp)) {
181 ERR("read rpmb key error\n"); 181 ERR("read rpmb key error\n");
182 ret = -1; 182 ret = -1;
183 goto fail; 183 goto fail;
184 } 184 }
185 /* copy rpmb key to blob */ 185 /* copy rpmb key to blob */
186 memcpy(blob, kp.rpmb_keyblob, RPMBKEY_BLOB_LEN); 186 memcpy(blob, kp.rpmb_keyblob, RPMBKEY_BLOB_LEN);
187 caam_open(); 187 caam_open();
188 if (caam_decap_blob((ulong)extract_key, (ulong)blob, 188 if (caam_decap_blob((ulong)extract_key, (ulong)blob,
189 RPMBKEY_LENGTH)) { 189 RPMBKEY_LENGTH)) {
190 ERR("decap rpmb key error\n"); 190 ERR("decap rpmb key error\n");
191 ret = -1; 191 ret = -1;
192 goto fail; 192 goto fail;
193 } 193 }
194 194
195 /* alloc a blksz mem */ 195 /* alloc a blksz mem */
196 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz); 196 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz);
197 if (bdata == NULL) { 197 if (bdata == NULL) {
198 ret = -1; 198 ret = -1;
199 goto fail; 199 goto fail;
200 } 200 }
201 /* one block a time */ 201 /* one block a time */
202 while (bs <= be) { 202 while (bs <= be) {
203 memset(bdata, 0, blksz); 203 memset(bdata, 0, blksz);
204 if (mmc_rpmb_read(mmc, bdata, bs, 1, extract_key) != 1) { 204 if (mmc_rpmb_read(mmc, bdata, bs, 1, extract_key) != 1) {
205 ret = -1; 205 ret = -1;
206 goto fail; 206 goto fail;
207 } 207 }
208 cnt = blksz - s; 208 cnt = blksz - s;
209 if (num_read + cnt > num_bytes) 209 if (num_read + cnt > num_bytes)
210 cnt = num_bytes - num_read; 210 cnt = num_bytes - num_read;
211 VDEBUG("cur: bs=%d, start=%ld, cnt=%ld bdata=0x%p\n", 211 VDEBUG("cur: bs=%d, start=%ld, cnt=%ld bdata=0x%p\n",
212 bs, s, cnt, bdata); 212 bs, s, cnt, bdata);
213 memcpy(out_buf, bdata + s, cnt); 213 memcpy(out_buf, bdata + s, cnt);
214 bs++; 214 bs++;
215 num_read += cnt; 215 num_read += cnt;
216 out_buf += cnt; 216 out_buf += cnt;
217 s = 0; 217 s = 0;
218 } 218 }
219 ret = 0; 219 ret = 0;
220 220
221 fail: 221 fail:
222 /* Return to original partition */ 222 /* Return to original partition */
223 if (desc->hwpart != original_part) { 223 if (desc->hwpart != original_part) {
224 if (mmc_switch_part(mmc, original_part) != 0) 224 if (mmc_switch_part(mmc, original_part) != 0)
225 return -1; 225 return -1;
226 desc->hwpart = original_part; 226 desc->hwpart = original_part;
227 } 227 }
228 if (blob != NULL) 228 if (blob != NULL)
229 free(blob); 229 free(blob);
230 if (bdata != NULL) 230 if (bdata != NULL)
231 free(bdata); 231 free(bdata);
232 return ret; 232 return ret;
233 233
234 } 234 }
235 235
236 int rpmb_write(struct mmc *mmc, uint8_t *buffer, size_t num_bytes, int64_t offset) { 236 int rpmb_write(struct mmc *mmc, uint8_t *buffer, size_t num_bytes, int64_t offset) {
237 237
238 unsigned char *bdata = NULL; 238 unsigned char *bdata = NULL;
239 unsigned char *in_buf = (unsigned char *)buffer; 239 unsigned char *in_buf = (unsigned char *)buffer;
240 unsigned long s, cnt; 240 unsigned long s, cnt;
241 unsigned long blksz; 241 unsigned long blksz;
242 size_t num_write = 0; 242 size_t num_write = 0;
243 unsigned short part_start, part_length, part_end, bs; 243 unsigned short part_start, part_length, part_end, bs;
244 margin_pos_t margin; 244 margin_pos_t margin;
245 char original_part; 245 char original_part;
246 uint8_t *blob = NULL; 246 uint8_t *blob = NULL;
247 struct blk_desc *desc = mmc_get_blk_desc(mmc); 247 struct blk_desc *desc = mmc_get_blk_desc(mmc);
248 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, extract_key, RPMBKEY_LENGTH); 248 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, extract_key, RPMBKEY_LENGTH);
249 249
250 struct keyslot_package kp; 250 struct keyslot_package kp;
251 int ret; 251 int ret;
252 252
253 blksz = RPMB_BLKSZ; 253 blksz = RPMB_BLKSZ;
254 part_length = mmc->capacity_rpmb >> 8; 254 part_length = mmc->capacity_rpmb >> 8;
255 part_start = 0; 255 part_start = 0;
256 part_end = part_start + part_length - 1; 256 part_end = part_start + part_length - 1;
257 257
258 DEBUGAVB("[rpmb]: offset=%ld, num_bytes=%zu\n", (long)offset, num_bytes); 258 DEBUGAVB("[rpmb]: offset=%ld, num_bytes=%zu\n", (long)offset, num_bytes);
259 259
260 if(get_margin_pos(part_start, part_end, blksz, 260 if(get_margin_pos(part_start, part_end, blksz,
261 &margin, offset, num_bytes, false)) { 261 &margin, offset, num_bytes, false)) {
262 ERR("get_margin_pos err\n"); 262 ERR("get_margin_pos err\n");
263 return -1; 263 return -1;
264 } 264 }
265 265
266 bs = (unsigned short)margin.blk_start; 266 bs = (unsigned short)margin.blk_start;
267 s = margin.start; 267 s = margin.start;
268 268
269 /* Switch to the RPMB partition */ 269 /* Switch to the RPMB partition */
270 original_part = desc->hwpart; 270 original_part = desc->hwpart;
271 if (desc->hwpart != MMC_PART_RPMB) { 271 if (desc->hwpart != MMC_PART_RPMB) {
272 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) 272 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0)
273 return -1; 273 return -1;
274 desc->hwpart = MMC_PART_RPMB; 274 desc->hwpart = MMC_PART_RPMB;
275 } 275 }
276 276
277 /* get rpmb key */ 277 /* get rpmb key */
278 blob = (uint8_t *)memalign(ARCH_DMA_MINALIGN, RPMBKEY_BLOB_LEN); 278 blob = (uint8_t *)memalign(ARCH_DMA_MINALIGN, RPMBKEY_BLOB_LEN);
279 if (read_keyslot_package(&kp)) { 279 if (read_keyslot_package(&kp)) {
280 ERR("read rpmb key error\n"); 280 ERR("read rpmb key error\n");
281 ret = -1; 281 ret = -1;
282 goto fail; 282 goto fail;
283 } 283 }
284 /* copy rpmb key to blob */ 284 /* copy rpmb key to blob */
285 memcpy(blob, kp.rpmb_keyblob, RPMBKEY_BLOB_LEN); 285 memcpy(blob, kp.rpmb_keyblob, RPMBKEY_BLOB_LEN);
286 caam_open(); 286 caam_open();
287 if (caam_decap_blob((ulong)extract_key, (ulong)blob, 287 if (caam_decap_blob((ulong)extract_key, (ulong)blob,
288 RPMBKEY_LENGTH)) { 288 RPMBKEY_LENGTH)) {
289 ERR("decap rpmb key error\n"); 289 ERR("decap rpmb key error\n");
290 ret = -1; 290 ret = -1;
291 goto fail; 291 goto fail;
292 } 292 }
293 /* alloc a blksz mem */ 293 /* alloc a blksz mem */
294 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz); 294 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz);
295 if (bdata == NULL) { 295 if (bdata == NULL) {
296 ret = -1; 296 ret = -1;
297 goto fail; 297 goto fail;
298 } 298 }
299 while (num_write < num_bytes) { 299 while (num_write < num_bytes) {
300 memset(bdata, 0, blksz); 300 memset(bdata, 0, blksz);
301 cnt = blksz - s; 301 cnt = blksz - s;
302 if (num_write + cnt > num_bytes) 302 if (num_write + cnt > num_bytes)
303 cnt = num_bytes - num_write; 303 cnt = num_bytes - num_write;
304 if (!s || cnt != blksz) { /* read blk first */ 304 if (!s || cnt != blksz) { /* read blk first */
305 if (mmc_rpmb_read(mmc, bdata, bs, 1, extract_key) != 1) { 305 if (mmc_rpmb_read(mmc, bdata, bs, 1, extract_key) != 1) {
306 ERR("mmc_rpmb_read err, mmc= 0x%08x\n", (uint32_t)(ulong)mmc); 306 ERR("mmc_rpmb_read err, mmc= 0x%08x\n", (uint32_t)(ulong)mmc);
307 ret = -1; 307 ret = -1;
308 goto fail; 308 goto fail;
309 } 309 }
310 } 310 }
311 memcpy(bdata + s, in_buf, cnt); /* change data */ 311 memcpy(bdata + s, in_buf, cnt); /* change data */
312 VDEBUG("cur: bs=%d, start=%ld, cnt=%ld\n", bs, s, cnt); 312 VDEBUG("cur: bs=%d, start=%ld, cnt=%ld\n", bs, s, cnt);
313 if (mmc_rpmb_write(mmc, bdata, bs, 1, extract_key) != 1) { 313 if (mmc_rpmb_write(mmc, bdata, bs, 1, extract_key) != 1) {
314 ret = -1; 314 ret = -1;
315 goto fail; 315 goto fail;
316 } 316 }
317 bs++; 317 bs++;
318 num_write += cnt; 318 num_write += cnt;
319 in_buf += cnt; 319 in_buf += cnt;
320 if (s != 0) 320 if (s != 0)
321 s = 0; 321 s = 0;
322 } 322 }
323 ret = 0; 323 ret = 0;
324 324
325 fail: 325 fail:
326 /* Return to original partition */ 326 /* Return to original partition */
327 if (desc->hwpart != original_part) { 327 if (desc->hwpart != original_part) {
328 if (mmc_switch_part(mmc, original_part) != 0) 328 if (mmc_switch_part(mmc, original_part) != 0)
329 return -1; 329 return -1;
330 desc->hwpart = original_part; 330 desc->hwpart = original_part;
331 } 331 }
332 if (blob != NULL) 332 if (blob != NULL)
333 free(blob); 333 free(blob);
334 if (bdata != NULL) 334 if (bdata != NULL)
335 free(bdata); 335 free(bdata);
336 336
337 return ret; 337 return ret;
338 338
339 } 339 }
340 340
341 int rpmb_init(void) { 341 int rpmb_init(void) {
342 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_DUAL_BOOTLOADER) 342 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_DUAL_BOOTLOADER)
343 int i; 343 int i;
344 #endif 344 #endif
345 kblb_hdr_t hdr; 345 kblb_hdr_t hdr;
346 kblb_tag_t *tag; 346 kblb_tag_t *tag;
347 struct mmc *mmc_dev; 347 struct mmc *mmc_dev;
348 uint32_t offset; 348 uint32_t offset;
349 uint32_t rbidx_len; 349 uint32_t rbidx_len;
350 uint8_t *rbidx; 350 uint8_t *rbidx;
351 351
352 /* check init status first */ 352 /* check init status first */
353 if ((mmc_dev = get_mmc()) == NULL) { 353 if ((mmc_dev = get_mmc()) == NULL) {
354 ERR("ERROR - get mmc device\n"); 354 ERR("ERROR - get mmc device\n");
355 return -1; 355 return -1;
356 } 356 }
357 /* The bootloader rollback index is stored in the last 8 blocks of 357 /* The bootloader rollback index is stored in the last 8 blocks of
358 * RPMB which is different from the rollback index for vbmeta and 358 * RPMB which is different from the rollback index for vbmeta and
359 * ATX key versions. 359 * ATX key versions.
360 */ 360 */
361 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER) 361 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER)
362 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 362 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr),
363 BOOTLOADER_RBIDX_OFFSET) != 0) { 363 BOOTLOADER_RBIDX_OFFSET) != 0) {
364 #else 364 #else
365 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) { 365 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) {
366 #endif 366 #endif
367 ERR("read RPMB error\n"); 367 ERR("read RPMB error\n");
368 return -1; 368 return -1;
369 } 369 }
370 if (!memcmp(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN)) 370 if (!memcmp(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN))
371 return 0; 371 return 0;
372 else 372 else
373 printf("initialize rollback index...\n"); 373 printf("initialize rollback index...\n");
374 /* init rollback index */ 374 /* init rollback index */
375 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER) 375 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER)
376 offset = BOOTLOADER_RBIDX_START; 376 offset = BOOTLOADER_RBIDX_START;
377 rbidx_len = BOOTLOADER_RBIDX_LEN; 377 rbidx_len = BOOTLOADER_RBIDX_LEN;
378 rbidx = malloc(rbidx_len); 378 rbidx = malloc(rbidx_len);
379 if (rbidx == NULL) { 379 if (rbidx == NULL) {
380 ERR("failed to allocate memory!\n"); 380 ERR("failed to allocate memory!\n");
381 return -1; 381 return -1;
382 } 382 }
383 memset(rbidx, 0, rbidx_len); 383 memset(rbidx, 0, rbidx_len);
384 *(uint64_t *)rbidx = BOOTLOADER_RBIDX_INITVAL; 384 *(uint64_t *)rbidx = BOOTLOADER_RBIDX_INITVAL;
385 tag = &hdr.bootloader_rbk_tags; 385 tag = &hdr.bootloader_rbk_tags;
386 tag->offset = offset; 386 tag->offset = offset;
387 tag->len = rbidx_len; 387 tag->len = rbidx_len;
388 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) { 388 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) {
389 ERR("write RBKIDX RPMB error\n"); 389 ERR("write RBKIDX RPMB error\n");
390 free(rbidx); 390 free(rbidx);
391 return -1; 391 return -1;
392 } 392 }
393 if (rbidx != NULL) 393 if (rbidx != NULL)
394 free(rbidx); 394 free(rbidx);
395 #else /* CONFIG_SPL_BUILD && CONFIG_DUAL_BOOTLOADER */ 395 #else /* CONFIG_SPL_BUILD && CONFIG_DUAL_BOOTLOADER */
396 offset = AVB_RBIDX_START; 396 offset = AVB_RBIDX_START;
397 rbidx_len = AVB_RBIDX_LEN; 397 rbidx_len = AVB_RBIDX_LEN;
398 rbidx = malloc(rbidx_len); 398 rbidx = malloc(rbidx_len);
399 if (rbidx == NULL) 399 if (rbidx == NULL)
400 return -1; 400 return -1;
401 memset(rbidx, 0, rbidx_len); 401 memset(rbidx, 0, rbidx_len);
402 *(uint64_t *)rbidx = AVB_RBIDX_INITVAL; 402 *(uint64_t *)rbidx = AVB_RBIDX_INITVAL;
403 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) { 403 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
404 tag = &hdr.rbk_tags[i]; 404 tag = &hdr.rbk_tags[i];
405 tag->flag = AVB_RBIDX_FLAG; 405 tag->flag = AVB_RBIDX_FLAG;
406 tag->offset = offset; 406 tag->offset = offset;
407 tag->len = rbidx_len; 407 tag->len = rbidx_len;
408 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) { 408 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) {
409 ERR("write RBKIDX RPMB error\n"); 409 ERR("write RBKIDX RPMB error\n");
410 free(rbidx); 410 free(rbidx);
411 return -1; 411 return -1;
412 } 412 }
413 offset += AVB_RBIDX_ALIGN; 413 offset += AVB_RBIDX_ALIGN;
414 } 414 }
415 if (rbidx != NULL) 415 if (rbidx != NULL)
416 free(rbidx); 416 free(rbidx);
417 #ifdef CONFIG_AVB_ATX 417 #ifdef CONFIG_AVB_ATX
418 /* init rollback index for Android Things key versions */ 418 /* init rollback index for Android Things key versions */
419 offset = ATX_RBIDX_START; 419 offset = ATX_RBIDX_START;
420 rbidx_len = ATX_RBIDX_LEN; 420 rbidx_len = ATX_RBIDX_LEN;
421 rbidx = malloc(rbidx_len); 421 rbidx = malloc(rbidx_len);
422 if (rbidx == NULL) 422 if (rbidx == NULL)
423 return -1; 423 return -1;
424 memset(rbidx, 0, rbidx_len); 424 memset(rbidx, 0, rbidx_len);
425 *(uint64_t *)rbidx = ATX_RBIDX_INITVAL; 425 *(uint64_t *)rbidx = ATX_RBIDX_INITVAL;
426 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) { 426 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
427 tag = &hdr.atx_rbk_tags[i]; 427 tag = &hdr.atx_rbk_tags[i];
428 tag->flag = ATX_RBIDX_FLAG; 428 tag->flag = ATX_RBIDX_FLAG;
429 tag->offset = offset; 429 tag->offset = offset;
430 tag->len = rbidx_len; 430 tag->len = rbidx_len;
431 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) { 431 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) {
432 ERR("write ATX_RBKIDX RPMB error\n"); 432 ERR("write ATX_RBKIDX RPMB error\n");
433 free(rbidx); 433 free(rbidx);
434 return -1; 434 return -1;
435 } 435 }
436 offset += ATX_RBIDX_ALIGN; 436 offset += ATX_RBIDX_ALIGN;
437 } 437 }
438 if (rbidx != NULL) 438 if (rbidx != NULL)
439 free(rbidx); 439 free(rbidx);
440 #endif 440 #endif
441 #endif /* CONFIG_SPL_BUILD && CONFIG_DUAL_BOOTLOADER */ 441 #endif /* CONFIG_SPL_BUILD && CONFIG_DUAL_BOOTLOADER */
442 442
443 /* init hdr */ 443 /* init hdr */
444 memcpy(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN); 444 memcpy(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN);
445 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER) 445 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_DUAL_BOOTLOADER)
446 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 446 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr),
447 BOOTLOADER_RBIDX_OFFSET) != 0) { 447 BOOTLOADER_RBIDX_OFFSET) != 0) {
448 #else 448 #else
449 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) { 449 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) {
450 #endif 450 #endif
451 ERR("write RPMB hdr error\n"); 451 ERR("write RPMB hdr error\n");
452 return -1; 452 return -1;
453 } 453 }
454 454
455 return 0; 455 return 0;
456 } 456 }
457 457
458 int gen_rpmb_key(struct keyslot_package *kp) { 458 int gen_rpmb_key(struct keyslot_package *kp) {
459 char original_part; 459 char original_part;
460 unsigned char* fill = NULL; 460 unsigned char* fill = NULL;
461 int blksz; 461 int blksz;
462 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, plain_key, RPMBKEY_LENGTH); 462 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, plain_key, RPMBKEY_LENGTH);
463 463
464 kp->rpmb_keyblob_len = RPMBKEY_LEN; 464 kp->rpmb_keyblob_len = RPMBKEY_LEN;
465 strcpy(kp->magic, KEYPACK_MAGIC); 465 strcpy(kp->magic, KEYPACK_MAGIC);
466 466
467 int ret = -1; 467 int ret = -1;
468 /* load tee from boot1 of eMMC. */ 468 /* load tee from boot1 of eMMC. */
469 int mmcc = mmc_get_env_dev(); 469 int mmcc = mmc_get_env_dev();
470 struct blk_desc *dev_desc = NULL; 470 struct blk_desc *dev_desc = NULL;
471 471
472 struct mmc *mmc; 472 struct mmc *mmc;
473 mmc = find_mmc_device(mmcc); 473 mmc = find_mmc_device(mmcc);
474 if (!mmc) { 474 if (!mmc) {
475 printf("boota: cannot find '%d' mmc device\n", mmcc); 475 printf("boota: cannot find '%d' mmc device\n", mmcc);
476 return -1; 476 return -1;
477 } 477 }
478 #ifndef CONFIG_BLK 478 #ifndef CONFIG_BLK
479 original_part = mmc->block_dev.hwpart; 479 original_part = mmc->block_dev.hwpart;
480 dev_desc = blk_get_dev("mmc", mmcc); 480 dev_desc = blk_get_dev("mmc", mmcc);
481 #else 481 #else
482 dev_desc = mmc_get_blk_desc(mmc); 482 dev_desc = mmc_get_blk_desc(mmc);
483 original_part = dev_desc->hwpart; 483 original_part = dev_desc->hwpart;
484 #endif 484 #endif
485 if (NULL == dev_desc) { 485 if (NULL == dev_desc) {
486 printf("** Block device MMC %d not supported\n", mmcc); 486 printf("** Block device MMC %d not supported\n", mmcc);
487 goto fail; 487 goto fail;
488 } 488 }
489 489
490 blksz = dev_desc->blksz; 490 blksz = dev_desc->blksz;
491 fill = (unsigned char *)memalign(ALIGN_BYTES, blksz); 491 fill = (unsigned char *)memalign(ALIGN_BYTES, blksz);
492 492
493 /* below was i.MX mmc operation code */ 493 /* below was i.MX mmc operation code */
494 if (mmc_init(mmc)) { 494 if (mmc_init(mmc)) {
495 printf("mmc%d init failed\n", mmcc); 495 printf("mmc%d init failed\n", mmcc);
496 goto fail; 496 goto fail;
497 } 497 }
498 498
499 /* Switch to the RPMB partition */ 499 /* Switch to the RPMB partition */
500 500
501 /* use caam hwrng to generate */ 501 /* use caam hwrng to generate */
502 caam_open(); 502 caam_open();
503 503
504 #ifdef TRUSTY_RPMB_RANDOM_KEY 504 #ifdef TRUSTY_RPMB_RANDOM_KEY
505 /* 505 /*
506 * Since boot1 is a bit easy to be erase during development 506 * Since boot1 is a bit easy to be erase during development
507 * so that before production stage use full 0 rpmb key 507 * so that before production stage use full 0 rpmb key
508 */ 508 */
509 if (caam_hwrng(plain_key, RPMBKEY_LENGTH)) { 509 if (caam_hwrng(plain_key, RPMBKEY_LENGTH)) {
510 ERR("ERROR - caam rng\n"); 510 ERR("ERROR - caam rng\n");
511 goto fail; 511 goto fail;
512 } 512 }
513 #else 513 #else
514 memset(plain_key, 0, RPMBKEY_LENGTH); 514 memset(plain_key, 0, RPMBKEY_LENGTH);
515 #endif 515 #endif
516 516
517 /* generate keyblob and program to boot1 partition */ 517 /* generate keyblob and program to boot1 partition */
518 if (caam_gen_blob((ulong)plain_key, (ulong)(kp->rpmb_keyblob), 518 if (caam_gen_blob((ulong)plain_key, (ulong)(kp->rpmb_keyblob),
519 RPMBKEY_LENGTH)) { 519 RPMBKEY_LENGTH)) {
520 ERR("gen rpmb key blb error\n"); 520 ERR("gen rpmb key blb error\n");
521 goto fail; 521 goto fail;
522 } 522 }
523 memcpy(fill, kp, sizeof(struct keyslot_package)); 523 memcpy(fill, kp, sizeof(struct keyslot_package));
524 524
525 mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID); 525 mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID);
526 if (blk_dwrite(dev_desc, KEYSLOT_BLKS, 526 if (blk_dwrite(dev_desc, KEYSLOT_BLKS,
527 1, (void *)fill) != 1) { 527 1, (void *)fill) != 1) {
528 printf("Failed to write rpmbkeyblob."); 528 printf("Failed to write rpmbkeyblob.");
529 goto fail; 529 goto fail;
530 } 530 }
531 531
532 /* program key to mmc */ 532 /* program key to mmc */
533 #ifndef CONFIG_BLK 533 #ifndef CONFIG_BLK
534 if (mmc->block_dev.hwpart != MMC_PART_RPMB) { 534 if (mmc->block_dev.hwpart != MMC_PART_RPMB) {
535 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) 535 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0)
536 return -1; 536 return -1;
537 mmc->block_dev.hwpart = MMC_PART_RPMB; 537 mmc->block_dev.hwpart = MMC_PART_RPMB;
538 } 538 }
539 #else 539 #else
540 if (dev_desc->hwpart != MMC_PART_RPMB) { 540 if (dev_desc->hwpart != MMC_PART_RPMB) {
541 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) 541 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0)
542 return -1; 542 return -1;
543 dev_desc->hwpart = MMC_PART_RPMB; 543 dev_desc->hwpart = MMC_PART_RPMB;
544 } 544 }
545 #endif 545 #endif
546 if (mmc_rpmb_set_key(mmc, plain_key)) { 546 if (mmc_rpmb_set_key(mmc, plain_key)) {
547 ERR("Key already programmed ?\n"); 547 ERR("Key already programmed ?\n");
548 goto fail; 548 goto fail;
549 } 549 }
550 550
551 ret = 0; 551 ret = 0;
552 552
553 fail: 553 fail:
554 if (fill != NULL) 554 if (fill != NULL)
555 free(fill); 555 free(fill);
556 556
557 /* Return to original partition */ 557 /* Return to original partition */
558 #ifndef CONFIG_BLK 558 #ifndef CONFIG_BLK
559 if (mmc->block_dev.hwpart != original_part) { 559 if (mmc->block_dev.hwpart != original_part) {
560 if (mmc_switch_part(mmc, original_part) != 0) 560 if (mmc_switch_part(mmc, original_part) != 0)
561 return -1; 561 return -1;
562 mmc->block_dev.hwpart = original_part; 562 mmc->block_dev.hwpart = original_part;
563 } 563 }
564 #else 564 #else
565 if (dev_desc->hwpart != original_part) { 565 if (dev_desc->hwpart != original_part) {
566 if (mmc_switch_part(mmc, original_part) != 0) 566 if (mmc_switch_part(mmc, original_part) != 0)
567 return -1; 567 return -1;
568 dev_desc->hwpart = original_part; 568 dev_desc->hwpart = original_part;
569 } 569 }
570 #endif 570 #endif
571 return ret; 571 return ret;
572 572
573 } 573 }
574 574
575 int init_avbkey(void) { 575 int init_avbkey(void) {
576 struct keyslot_package kp; 576 struct keyslot_package kp;
577 read_keyslot_package(&kp); 577 read_keyslot_package(&kp);
578 if (strcmp(kp.magic, KEYPACK_MAGIC)) { 578 if (strcmp(kp.magic, KEYPACK_MAGIC)) {
579 printf("keyslot package magic error. Will generate new one\n"); 579 printf("keyslot package magic error. Will generate new one\n");
580 gen_rpmb_key(&kp); 580 gen_rpmb_key(&kp);
581 } 581 }
582 #ifndef CONFIG_IMX_TRUSTY_OS 582 #ifndef CONFIG_IMX_TRUSTY_OS
583 if (rpmb_init()) 583 if (rpmb_init())
584 return RESULT_ERROR; 584 return RESULT_ERROR;
585 #endif 585 #endif
586 #if defined(CONFIG_AVB_ATX) && !defined(CONFIG_IMX_TRUSTY_OS) 586 #if defined(CONFIG_AVB_ATX) && !defined(CONFIG_IMX_TRUSTY_OS)
587 if (init_permanent_attributes_fuse()) 587 if (init_permanent_attributes_fuse())
588 return RESULT_ERROR; 588 return RESULT_ERROR;
589 #endif 589 #endif
590 fill_secure_keyslot_package(&kp); 590 fill_secure_keyslot_package(&kp);
591 return RESULT_OK; 591 return RESULT_OK;
592 } 592 }
593 593
594 #ifndef CONFIG_IMX_TRUSTY_OS 594 #ifndef CONFIG_IMX_TRUSTY_OS
595 int rbkidx_erase(void) { 595 int rbkidx_erase(void) {
596 int i; 596 int i;
597 kblb_hdr_t hdr; 597 kblb_hdr_t hdr;
598 kblb_tag_t *tag; 598 kblb_tag_t *tag;
599 struct mmc *mmc_dev; 599 struct mmc *mmc_dev;
600 600
601 if ((mmc_dev = get_mmc()) == NULL) { 601 if ((mmc_dev = get_mmc()) == NULL) {
602 ERR("err get mmc device\n"); 602 ERR("err get mmc device\n");
603 return -1; 603 return -1;
604 } 604 }
605 605
606 /* read the kblb header */ 606 /* read the kblb header */
607 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) { 607 if (rpmb_read(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) {
608 ERR("read RPMB error\n"); 608 ERR("read RPMB error\n");
609 return -1; 609 return -1;
610 } 610 }
611 if (memcmp(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN) != 0) { 611 if (memcmp(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN) != 0) {
612 ERR("magic not match\n"); 612 ERR("magic not match\n");
613 return -1; 613 return -1;
614 } 614 }
615 615
616 /* reset rollback index */ 616 /* reset rollback index */
617 uint32_t offset = AVB_RBIDX_START; 617 uint32_t offset = AVB_RBIDX_START;
618 uint32_t rbidx_len = AVB_RBIDX_LEN; 618 uint32_t rbidx_len = AVB_RBIDX_LEN;
619 uint8_t *rbidx = malloc(rbidx_len); 619 uint8_t *rbidx = malloc(rbidx_len);
620 if (rbidx == NULL) 620 if (rbidx == NULL)
621 return -1; 621 return -1;
622 memset(rbidx, 0, rbidx_len); 622 memset(rbidx, 0, rbidx_len);
623 *(uint64_t *)rbidx = AVB_RBIDX_INITVAL; 623 *(uint64_t *)rbidx = AVB_RBIDX_INITVAL;
624 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) { 624 for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
625 tag = &hdr.rbk_tags[i]; 625 tag = &hdr.rbk_tags[i];
626 tag->flag = AVB_RBIDX_FLAG; 626 tag->flag = AVB_RBIDX_FLAG;
627 tag->offset = offset; 627 tag->offset = offset;
628 tag->len = rbidx_len; 628 tag->len = rbidx_len;
629 /* write */ 629 /* write */
630 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) { 630 if (rpmb_write(mmc_dev, rbidx, tag->len, tag->offset) != 0) {
631 ERR("write RBKIDX RPMB error\n"); 631 ERR("write RBKIDX RPMB error\n");
632 free(rbidx); 632 free(rbidx);
633 return -1; 633 return -1;
634 } 634 }
635 offset += AVB_RBIDX_ALIGN; 635 offset += AVB_RBIDX_ALIGN;
636 } 636 }
637 free(rbidx); 637 free(rbidx);
638 /* write back hdr */ 638 /* write back hdr */
639 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) { 639 if (rpmb_write(mmc_dev, (uint8_t *)&hdr, sizeof(hdr), 0) != 0) {
640 ERR("write RPMB hdr error\n"); 640 ERR("write RPMB hdr error\n");
641 return -1; 641 return -1;
642 } 642 }
643 return 0; 643 return 0;
644 } 644 }
645 #endif /* CONFIG_FSL_CAAM_KB */ 645 #endif /* CONFIG_FSL_CAAM_KB */
646 #endif /* CONFIG_IMX_TRUSTY_OS */ 646 #endif /* CONFIG_IMX_TRUSTY_OS */
647 #else /* AVB_RPMB */ 647 #else /* AVB_RPMB */
648 int rbkidx_erase(void) { 648 int rbkidx_erase(void) {
649 return 0; 649 return 0;
650 } 650 }
651 #endif /* AVB_RPMB */ 651 #endif /* AVB_RPMB */
652 652
653 #ifdef CONFIG_SPL_BUILD 653 #ifdef CONFIG_SPL_BUILD
654 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_AVB_ATX) 654 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_AVB_ATX)
655 int check_rpmb_blob(struct mmc *mmc) 655 int check_rpmb_blob(struct mmc *mmc)
656 { 656 {
657 int ret = 0; 657 int ret = 0;
658 char original_part; 658 char original_part;
659 struct keyslot_package kp; 659 struct keyslot_package kp;
660 660
661 read_keyslot_package(&kp); 661 read_keyslot_package(&kp);
662 if (strcmp(kp.magic, KEYPACK_MAGIC)) { 662 if (strcmp(kp.magic, KEYPACK_MAGIC)) {
663 printf("keyslot package magic error, do nothing here!\n"); 663 printf("keyslot package magic error, do nothing here!\n");
664 return 0; 664 return 0;
665 } 665 }
666 /* If keyslot package valid, copy it to secure memory */ 666 /* If keyslot package valid, copy it to secure memory */
667 fill_secure_keyslot_package(&kp); 667 fill_secure_keyslot_package(&kp);
668 668
669 /* switch to boot1 partition. */ 669 /* switch to boot1 partition. */
670 original_part = mmc->block_dev.hwpart; 670 original_part = mmc->block_dev.hwpart;
671 if (mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID) != 0) { 671 if (mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID) != 0) {
672 printf("ERROR - can't switch to boot1 partition! \n"); 672 printf("ERROR - can't switch to boot1 partition! \n");
673 ret = -1; 673 ret = -1;
674 goto fail; 674 goto fail;
675 } else 675 } else
676 mmc->block_dev.hwpart = KEYSLOT_HWPARTITION_ID; 676 mmc->block_dev.hwpart = KEYSLOT_HWPARTITION_ID;
677 /* write power-on write protection for boot1 partition. */ 677 /* write power-on write protection for boot1 partition. */
678 if (mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 678 if (mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
679 EXT_CSD_BOOT_WP, BOOT1_PWR_WP)) { 679 EXT_CSD_BOOT_WP, BOOT1_PWR_WP)) {
680 printf("ERROR - unable to set power-on write protection!\n"); 680 printf("ERROR - unable to set power-on write protection!\n");
681 ret = -1; 681 ret = -1;
682 goto fail; 682 goto fail;
683 } 683 }
684 fail: 684 fail:
685 /* return to original partition. */ 685 /* return to original partition. */
686 if (mmc->block_dev.hwpart != original_part) { 686 if (mmc->block_dev.hwpart != original_part) {
687 if (mmc_switch_part(mmc, original_part) != 0) 687 if (mmc_switch_part(mmc, original_part) != 0)
688 return -1; 688 return -1;
689 mmc->block_dev.hwpart = original_part; 689 mmc->block_dev.hwpart = original_part;
690 } 690 }
691 691
692 return ret; 692 return ret;
693 } 693 }
694 #endif /* CONFIG_IMX_TRUSTY_OS && !defined(CONFIG_AVB_ATX) */ 694 #endif /* CONFIG_IMX_TRUSTY_OS && !defined(CONFIG_AVB_ATX) */
695 #else /* CONFIG_SPL_BUILD */ 695 #else /* CONFIG_SPL_BUILD */
696 #ifdef CONFIG_AVB_ATX 696 #ifdef CONFIG_AVB_ATX
697 static int fsl_fuse_ops(uint32_t *buffer, uint32_t length, uint32_t offset, 697 static int fsl_fuse_ops(uint32_t *buffer, uint32_t length, uint32_t offset,
698 const uint8_t read) { 698 const uint8_t read) {
699 699
700 unsigned short bs, ws, bksz, cnt; 700 unsigned short bs, ws, bksz, cnt;
701 unsigned short num_done = 0; 701 unsigned short num_done = 0;
702 margin_pos_t margin; 702 margin_pos_t margin;
703 int i; 703 int i;
704 704
705 /* read from fuse */ 705 /* read from fuse */
706 bksz = CONFIG_AVB_FUSE_BANK_SIZEW; 706 bksz = CONFIG_AVB_FUSE_BANK_SIZEW;
707 if(get_margin_pos(CONFIG_AVB_FUSE_BANK_START, CONFIG_AVB_FUSE_BANK_END, bksz, 707 if(get_margin_pos(CONFIG_AVB_FUSE_BANK_START, CONFIG_AVB_FUSE_BANK_END, bksz,
708 &margin, offset, length, false)) 708 &margin, offset, length, false))
709 return -1; 709 return -1;
710 bs = (unsigned short)margin.blk_start; 710 bs = (unsigned short)margin.blk_start;
711 ws = (unsigned short)margin.start; 711 ws = (unsigned short)margin.start;
712 712
713 while (num_done < length) { 713 while (num_done < length) {
714 cnt = bksz - ws; 714 cnt = bksz - ws;
715 if (num_done + cnt > length) 715 if (num_done + cnt > length)
716 cnt = length - num_done; 716 cnt = length - num_done;
717 for (i = 0; i < cnt; i++) { 717 for (i = 0; i < cnt; i++) {
718 VDEBUG("cur: bank=%d, word=%d\n",bs, ws); 718 VDEBUG("cur: bank=%d, word=%d\n",bs, ws);
719 if (read) { 719 if (read) {
720 if (fuse_sense(bs, ws, buffer)) { 720 if (fuse_sense(bs, ws, buffer)) {
721 ERR("read fuse bank %d, word %d error\n", bs, ws); 721 ERR("read fuse bank %d, word %d error\n", bs, ws);
722 return -1; 722 return -1;
723 } 723 }
724 } else { 724 } else {
725 #ifdef CONFIG_AVB_FUSE 725 #ifdef CONFIG_AVB_FUSE
726 if (fuse_prog(bs, ws, *buffer)) { 726 if (fuse_prog(bs, ws, *buffer)) {
727 #else 727 #else
728 if (fuse_override(bs, ws, *buffer)) { 728 if (fuse_override(bs, ws, *buffer)) {
729 #endif 729 #endif
730 ERR("write fuse bank %d, word %d error\n", bs, ws); 730 ERR("write fuse bank %d, word %d error\n", bs, ws);
731 return -1; 731 return -1;
732 } 732 }
733 } 733 }
734 ws++; 734 ws++;
735 buffer++; 735 buffer++;
736 } 736 }
737 bs++; 737 bs++;
738 num_done += cnt; 738 num_done += cnt;
739 ws = 0; 739 ws = 0;
740 } 740 }
741 return 0; 741 return 0;
742 } 742 }
743 743
744 int fsl_fuse_read(uint32_t *buffer, uint32_t length, uint32_t offset) { 744 int fsl_fuse_read(uint32_t *buffer, uint32_t length, uint32_t offset) {
745 745
746 return fsl_fuse_ops( 746 return fsl_fuse_ops(
747 buffer, 747 buffer,
748 length, 748 length,
749 offset, 749 offset,
750 1 750 1
751 ); 751 );
752 } 752 }
753 753
754 int fsl_fuse_write(const uint32_t *buffer, uint32_t length, uint32_t offset) { 754 int fsl_fuse_write(const uint32_t *buffer, uint32_t length, uint32_t offset) {
755 755
756 return fsl_fuse_ops( 756 return fsl_fuse_ops(
757 (uint32_t *)buffer, 757 (uint32_t *)buffer,
758 length, 758 length,
759 offset, 759 offset,
760 0 760 0
761 ); 761 );
762 } 762 }
763 763
764 static int sha256(unsigned char* data, int len, unsigned char* output) { 764 static int sha256(unsigned char* data, int len, unsigned char* output) {
765 struct hash_algo *algo; 765 struct hash_algo *algo;
766 void *buf; 766 void *buf;
767 767
768 if (hash_lookup_algo("sha256", &algo)) { 768 if (hash_lookup_algo("sha256", &algo)) {
769 printf("error in lookup sha256 algo!\n"); 769 printf("error in lookup sha256 algo!\n");
770 return RESULT_ERROR; 770 return RESULT_ERROR;
771 } 771 }
772 buf = map_sysmem((ulong)data, len); 772 buf = map_sysmem((ulong)data, len);
773 algo->hash_func_ws(buf, len, output, algo->chunk_size); 773 algo->hash_func_ws(buf, len, output, algo->chunk_size);
774 unmap_sysmem(buf); 774 unmap_sysmem(buf);
775 775
776 return algo->digest_size; 776 return algo->digest_size;
777 } 777 }
778 778
779 int permanent_attributes_sha256_hash(unsigned char* output) { 779 int permanent_attributes_sha256_hash(unsigned char* output) {
780 AvbAtxPermanentAttributes attributes; 780 AvbAtxPermanentAttributes attributes;
781 781
782 #ifdef CONFIG_IMX_TRUSTY_OS 782 #ifdef CONFIG_IMX_TRUSTY_OS
783 if(!trusty_read_permanent_attributes((uint8_t *)(&attributes), 783 if(!trusty_read_permanent_attributes((uint8_t *)(&attributes),
784 sizeof(AvbAtxPermanentAttributes))) { 784 sizeof(AvbAtxPermanentAttributes))) {
785 goto calc_sha256; 785 goto calc_sha256;
786 } else { 786 } else {
787 ERR("No perm-attr fused. Will use hard code one.\n"); 787 ERR("No perm-attr fused. Will use hard code one.\n");
788 } 788 }
789 #endif 789 #endif
790 /* get permanent attributes */ 790 /* get permanent attributes */
791 attributes.version = fsl_version; 791 attributes.version = fsl_version;
792 memcpy(attributes.product_root_public_key, fsl_product_root_public_key, 792 memcpy(attributes.product_root_public_key, fsl_product_root_public_key,
793 sizeof(fsl_product_root_public_key)); 793 sizeof(fsl_product_root_public_key));
794 memcpy(attributes.product_id, fsl_atx_product_id, 794 memcpy(attributes.product_id, fsl_atx_product_id,
795 sizeof(fsl_atx_product_id)); 795 sizeof(fsl_atx_product_id));
796 #ifdef CONFIG_IMX_TRUSTY_OS 796 #ifdef CONFIG_IMX_TRUSTY_OS
797 calc_sha256: 797 calc_sha256:
798 #endif 798 #endif
799 /* calculate sha256(permanent attributes) hash */ 799 /* calculate sha256(permanent attributes) hash */
800 if (sha256((unsigned char *)&attributes, sizeof(AvbAtxPermanentAttributes), 800 if (sha256((unsigned char *)&attributes, sizeof(AvbAtxPermanentAttributes),
801 output) == RESULT_ERROR) { 801 output) == RESULT_ERROR) {
802 printf("ERROR - calculate permanent attributes hash error"); 802 printf("ERROR - calculate permanent attributes hash error");
803 return RESULT_ERROR; 803 return RESULT_ERROR;
804 } 804 }
805 805
806 return RESULT_OK; 806 return RESULT_OK;
807 } 807 }
808 808
809 static int init_permanent_attributes_fuse(void) { 809 static int init_permanent_attributes_fuse(void) {
810 810
811 #ifdef CONFIG_ARM64 811 #ifdef CONFIG_ARM64
812 return RESULT_OK; 812 return RESULT_OK;
813 #else 813 #else
814 uint8_t sha256_hash[AVB_SHA256_DIGEST_SIZE]; 814 uint8_t sha256_hash[AVB_SHA256_DIGEST_SIZE];
815 uint32_t buffer[ATX_FUSE_BANK_NUM]; 815 uint32_t buffer[ATX_FUSE_BANK_NUM];
816 int num = 0; 816 int num = 0;
817 817
818 /* read first 112 bits of sha256(permanent attributes) from fuse */ 818 /* read first 112 bits of sha256(permanent attributes) from fuse */
819 if (fsl_fuse_read(buffer, ATX_FUSE_BANK_NUM, PERMANENT_ATTRIBUTE_HASH_OFFSET)) { 819 if (fsl_fuse_read(buffer, ATX_FUSE_BANK_NUM, PERMANENT_ATTRIBUTE_HASH_OFFSET)) {
820 printf("ERROR - read permanent attributes hash from fuse error\n"); 820 printf("ERROR - read permanent attributes hash from fuse error\n");
821 return RESULT_ERROR; 821 return RESULT_ERROR;
822 } 822 }
823 /* only take the lower 2 bytes of the last bank */ 823 /* only take the lower 2 bytes of the last bank */
824 buffer[ATX_FUSE_BANK_NUM - 1] &= ATX_FUSE_BANK_MASK; 824 buffer[ATX_FUSE_BANK_NUM - 1] &= ATX_FUSE_BANK_MASK;
825 825
826 /* return RESULT_OK if fuse has been initialized before */ 826 /* return RESULT_OK if fuse has been initialized before */
827 for (num = 0; num < ATX_FUSE_BANK_NUM; num++) { 827 for (num = 0; num < ATX_FUSE_BANK_NUM; num++) {
828 if (buffer[num]) 828 if (buffer[num])
829 return RESULT_OK; 829 return RESULT_OK;
830 } 830 }
831 831
832 /* calculate sha256(permanent attributes) */ 832 /* calculate sha256(permanent attributes) */
833 if (permanent_attributes_sha256_hash(sha256_hash) != RESULT_OK) { 833 if (permanent_attributes_sha256_hash(sha256_hash) != RESULT_OK) {
834 printf("ERROR - calculating permanent attributes SHA256 error!\n"); 834 printf("ERROR - calculating permanent attributes SHA256 error!\n");
835 return RESULT_ERROR; 835 return RESULT_ERROR;
836 } 836 }
837 837
838 /* write first 112 bits of sha256(permanent attributes) into fuse */ 838 /* write first 112 bits of sha256(permanent attributes) into fuse */
839 memset(buffer, 0, sizeof(buffer)); 839 memset(buffer, 0, sizeof(buffer));
840 memcpy(buffer, sha256_hash, ATX_HASH_LENGTH); 840 memcpy(buffer, sha256_hash, ATX_HASH_LENGTH);
841 if (fsl_fuse_write(buffer, ATX_FUSE_BANK_NUM, PERMANENT_ATTRIBUTE_HASH_OFFSET)) { 841 if (fsl_fuse_write(buffer, ATX_FUSE_BANK_NUM, PERMANENT_ATTRIBUTE_HASH_OFFSET)) {
842 printf("ERROR - write permanent attributes hash to fuse error\n"); 842 printf("ERROR - write permanent attributes hash to fuse error\n");
843 return RESULT_ERROR; 843 return RESULT_ERROR;
844 } 844 }
845 845
846 return RESULT_OK; 846 return RESULT_OK;
847 #endif /* CONFIG_ARM64 */ 847 #endif /* CONFIG_ARM64 */
848 } 848 }
849 849
850 int avb_atx_fuse_perm_attr(uint8_t *staged_buffer, uint32_t size) { 850 int avb_atx_fuse_perm_attr(uint8_t *staged_buffer, uint32_t size) {
851 851
852 if (staged_buffer == NULL) { 852 if (staged_buffer == NULL) {
853 ERR("Error. Get null staged_buffer\n"); 853 ERR("Error. Get null staged_buffer\n");
854 return -1; 854 return -1;
855 } 855 }
856 if (size != sizeof(AvbAtxPermanentAttributes)) { 856 if (size != sizeof(AvbAtxPermanentAttributes)) {
857 ERR("Error. expect perm_attr length %u, but get %u.\n", 857 ERR("Error. expect perm_attr length %u, but get %u.\n",
858 (uint32_t)sizeof(AvbAtxPermanentAttributes), size); 858 (uint32_t)sizeof(AvbAtxPermanentAttributes), size);
859 return -1; 859 return -1;
860 } 860 }
861 #ifdef CONFIG_IMX_TRUSTY_OS 861 #ifdef CONFIG_IMX_TRUSTY_OS
862 if (trusty_write_permanent_attributes(staged_buffer, size)) { 862 if (trusty_write_permanent_attributes(staged_buffer, size)) {
863 ERR("Error. Failed to write permanent attributes into secure storage\n"); 863 ERR("Error. Failed to write permanent attributes into secure storage\n");
864 return -1; 864 return -1;
865 } 865 }
866 else 866 else
867 return init_permanent_attributes_fuse(); 867 return init_permanent_attributes_fuse();
868 #else 868 #else
869 /* 869 /*
870 * TODO: 870 * TODO:
871 * Need to handle this when no Trusty OS support. 871 * Need to handle this when no Trusty OS support.
872 * But now every Android Things will have Trusty OS support. 872 * But now every Android Things will have Trusty OS support.
873 */ 873 */
874 ERR("No Trusty OS enabled in bootloader.\n"); 874 ERR("No Trusty OS enabled in bootloader.\n");
875 return 0; 875 return 0;
876 #endif 876 #endif
877 } 877 }
878 878
879 int avb_atx_get_unlock_challenge(struct AvbAtxOps* atx_ops, 879 int avb_atx_get_unlock_challenge(struct AvbAtxOps* atx_ops,
880 uint8_t *upload_buffer, uint32_t *upload_size) 880 uint8_t *upload_buffer, uint32_t *upload_size)
881 { 881 {
882 struct AvbAtxUnlockChallenge *buf = NULL; 882 struct AvbAtxUnlockChallenge *buf = NULL;
883 int ret, size; 883 int ret, size;
884 884
885 size = sizeof(struct AvbAtxUnlockChallenge); 885 size = sizeof(struct AvbAtxUnlockChallenge);
886 buf = (struct AvbAtxUnlockChallenge *)malloc(size); 886 buf = (struct AvbAtxUnlockChallenge *)malloc(size);
887 if (buf == NULL) { 887 if (buf == NULL) {
888 ERR("unable to alloc memory!\n"); 888 ERR("unable to alloc memory!\n");
889 return -1; 889 return -1;
890 } 890 }
891 891
892 if (avb_atx_generate_unlock_challenge(atx_ops, buf) != 892 if (avb_atx_generate_unlock_challenge(atx_ops, buf) !=
893 AVB_IO_RESULT_OK) { 893 AVB_IO_RESULT_OK) {
894 ERR("generate unlock challenge fail!\n"); 894 ERR("generate unlock challenge fail!\n");
895 ret = -1; 895 ret = -1;
896 goto fail; 896 goto fail;
897 } 897 }
898 /* Current avbtool only accept 16 bytes random numbers as unlock 898 /* Current avbtool only accept 16 bytes random numbers as unlock
899 * challenge, need to return the whole 'AvbAtxUnlockChallenge' 899 * challenge, need to return the whole 'AvbAtxUnlockChallenge'
900 * when avbtool is ready. 900 * when avbtool is ready.
901 */ 901 */
902 memcpy(upload_buffer, buf->challenge, AVB_ATX_UNLOCK_CHALLENGE_SIZE); 902 memcpy(upload_buffer, buf->challenge, AVB_ATX_UNLOCK_CHALLENGE_SIZE);
903 *upload_size = AVB_ATX_UNLOCK_CHALLENGE_SIZE; 903 *upload_size = AVB_ATX_UNLOCK_CHALLENGE_SIZE;
904 ret = 0; 904 ret = 0;
905 fail: 905 fail:
906 if (buf != NULL) 906 if (buf != NULL)
907 free(buf); 907 free(buf);
908 return ret; 908 return ret;
909 } 909 }
910 910
911 int avb_atx_verify_unlock_credential(struct AvbAtxOps* atx_ops, 911 int avb_atx_verify_unlock_credential(struct AvbAtxOps* atx_ops,
912 uint8_t *staged_buffer) 912 uint8_t *staged_buffer)
913 { 913 {
914 bool out_is_trusted; 914 bool out_is_trusted;
915 AvbIOResult ret; 915 AvbIOResult ret;
916 const AvbAtxUnlockCredential* buf = NULL; 916 const AvbAtxUnlockCredential* buf = NULL;
917 917
918 buf = (const AvbAtxUnlockCredential*)staged_buffer; 918 buf = (const AvbAtxUnlockCredential*)staged_buffer;
919 ret = avb_atx_validate_unlock_credential(atx_ops, buf, &out_is_trusted); 919 ret = avb_atx_validate_unlock_credential(atx_ops, buf, &out_is_trusted);
920 if ((ret != AVB_IO_RESULT_OK) || (out_is_trusted != true)) { 920 if ((ret != AVB_IO_RESULT_OK) || (out_is_trusted != true)) {
921 ERR("validate unlock credential fail!\n"); 921 ERR("validate unlock credential fail!\n");
922 return -1; 922 return -1;
923 } else 923 } else
924 return 0; 924 return 0;
925 } 925 }
926 926
927 bool perm_attr_are_fused(void) 927 bool perm_attr_are_fused(void)
928 { 928 {
929 #ifdef CONFIG_IMX_TRUSTY_OS 929 #ifdef CONFIG_IMX_TRUSTY_OS
930 AvbAtxPermanentAttributes attributes; 930 AvbAtxPermanentAttributes attributes;
931 if(!trusty_read_permanent_attributes((uint8_t *)(&attributes), 931 if(!trusty_read_permanent_attributes((uint8_t *)(&attributes),
932 sizeof(AvbAtxPermanentAttributes))) { 932 sizeof(AvbAtxPermanentAttributes))) {
933 return true; 933 return true;
934 } else { 934 } else {
935 ERR("No perm-attr fused, please fuse your perm-attr first!.\n"); 935 ERR("No perm-attr fused, please fuse your perm-attr first!.\n");
936 return false; 936 return false;
937 } 937 }
938 #else 938 #else
939 /* We hard code the perm-attr if trusty is not enabled. */ 939 /* We hard code the perm-attr if trusty is not enabled. */
940 return true; 940 return true;
941 #endif 941 #endif
942 } 942 }
943 943
944 bool at_unlock_vboot_is_disabled(void) 944 bool at_unlock_vboot_is_disabled(void)
945 { 945 {
946 uint32_t unlock_vboot_status; 946 uint32_t unlock_vboot_status;
947 947
948 if (fsl_fuse_read(&unlock_vboot_status, 1, 948 if (fsl_fuse_read(&unlock_vboot_status, 1,
949 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) { 949 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) {
950 printf("Read at unlock vboot status error!\n"); 950 printf("Read at unlock vboot status error!\n");
951 return false; 951 return false;
952 } 952 }
953 953
954 if (unlock_vboot_status & (1 << UNLOCK_VBOOT_STATUS_OFFSET_IN_BIT)) 954 if (unlock_vboot_status & (1 << UNLOCK_VBOOT_STATUS_OFFSET_IN_BIT))
955 return true; 955 return true;
956 else 956 else
957 return false; 957 return false;
958 } 958 }
959 959
960 int at_disable_vboot_unlock(void) 960 int at_disable_vboot_unlock(void)
961 { 961 {
962 uint32_t unlock_vboot_status = 0; 962 uint32_t unlock_vboot_status = 0;
963 963
964 /* Read the status first */ 964 /* Read the status first */
965 if (fsl_fuse_read(&unlock_vboot_status, 1, 965 if (fsl_fuse_read(&unlock_vboot_status, 1,
966 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) { 966 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) {
967 ERR("Read unlock vboot status error!\n"); 967 ERR("Read unlock vboot status error!\n");
968 return -1; 968 return -1;
969 } 969 }
970 970
971 /* Set the disable unlock vboot bit */ 971 /* Set the disable unlock vboot bit */
972 unlock_vboot_status |= (1 << UNLOCK_VBOOT_STATUS_OFFSET_IN_BIT); 972 unlock_vboot_status |= (1 << UNLOCK_VBOOT_STATUS_OFFSET_IN_BIT);
973 973
974 /* Write disable unlock vboot bit to fuse */ 974 /* Write disable unlock vboot bit to fuse */
975 if (fsl_fuse_write(&unlock_vboot_status, 1, 975 if (fsl_fuse_write(&unlock_vboot_status, 1,
976 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) { 976 UNLOCK_VBOOT_STATUS_OFFSET_IN_WORD)) {
977 ERR("Write unlock vboot status fail!\n"); 977 ERR("Write unlock vboot status fail!\n");
978 return -1; 978 return -1;
979 } 979 }
980 980
981 return 0; 981 return 0;
982 } 982 }
983 #endif /* CONFIG_AVB_ATX */ 983 #endif /* CONFIG_AVB_ATX */
984 984
985 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_AVB_ATX) 985 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_AVB_ATX)
986 bool rpmbkey_is_set(void) 986 bool rpmbkey_is_set(void)
987 { 987 {
988 int mmcc; 988 int mmcc;
989 bool ret; 989 bool ret;
990 uint8_t *buf; 990 uint8_t *buf;
991 struct mmc *mmc; 991 struct mmc *mmc;
992 char original_part; 992 char original_part;
993 struct blk_desc *desc = NULL; 993 struct blk_desc *desc = NULL;
994 994
995 /* Get current mmc device. */ 995 /* Get current mmc device. */
996 mmcc = mmc_get_env_dev(); 996 mmcc = mmc_get_env_dev();
997 mmc = find_mmc_device(mmcc); 997 mmc = find_mmc_device(mmcc);
998 if (!mmc) { 998 if (!mmc) {
999 printf("error - cannot find '%d' mmc device\n", mmcc); 999 printf("error - cannot find '%d' mmc device\n", mmcc);
1000 return false; 1000 return false;
1001 } 1001 }
1002 1002
1003 desc = mmc_get_blk_desc(mmc); 1003 desc = mmc_get_blk_desc(mmc);
1004 original_part = desc->hwpart; 1004 original_part = desc->hwpart;
1005 1005
1006 /* Switch to the RPMB partition */ 1006 /* Switch to the RPMB partition */
1007 if (desc->hwpart != MMC_PART_RPMB) { 1007 if (desc->hwpart != MMC_PART_RPMB) {
1008 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) { 1008 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) {
1009 printf("ERROR - can't switch to rpmb partition \n"); 1009 printf("ERROR - can't switch to rpmb partition \n");
1010 return false; 1010 return false;
1011 } 1011 }
1012 desc->hwpart = MMC_PART_RPMB; 1012 desc->hwpart = MMC_PART_RPMB;
1013 } 1013 }
1014 1014
1015 /* Try to read the first one block, return count '1' means the rpmb 1015 /* Try to read the first one block, return count '1' means the rpmb
1016 * key has been set, otherwise means the key hasn't been set. 1016 * key has been set, otherwise means the key hasn't been set.
1017 */ 1017 */
1018 buf = (uint8_t *)memalign(ALIGN_BYTES, desc->blksz); 1018 buf = (uint8_t *)memalign(ALIGN_BYTES, desc->blksz);
1019 if (mmc_rpmb_read(mmc, buf, 0, 1, NULL) != 1) 1019 if (mmc_rpmb_read(mmc, buf, 0, 1, NULL) != 1)
1020 ret = false; 1020 ret = false;
1021 else 1021 else
1022 ret = true; 1022 ret = true;
1023 1023
1024 /* return to original partition. */ 1024 /* return to original partition. */
1025 if (desc->hwpart != original_part) { 1025 if (desc->hwpart != original_part) {
1026 if (mmc_switch_part(mmc, original_part) != 0) 1026 if (mmc_switch_part(mmc, original_part) != 0)
1027 ret = false; 1027 ret = false;
1028 desc->hwpart = original_part; 1028 desc->hwpart = original_part;
1029 } 1029 }
1030 /* remember to free the buffer */ 1030 /* remember to free the buffer */
1031 if (buf != NULL) 1031 if (buf != NULL)
1032 free(buf); 1032 free(buf);
1033 1033
1034 return ret; 1034 return ret;
1035 } 1035 }
1036 1036
1037 int fastboot_set_rpmb_key(uint8_t *staged_buf, uint32_t key_size) 1037 int do_rpmb_key_set(uint8_t *key, uint32_t key_size)
1038 { 1038 {
1039 int ret = 0; 1039 int ret = 0;
1040 int mmcc; 1040 int mmcc;
1041 struct mmc *mmc; 1041 struct mmc *mmc;
1042 char original_part; 1042 char original_part;
1043 struct keyslot_package kp; 1043 struct keyslot_package kp;
1044 struct blk_desc *desc = NULL; 1044 struct blk_desc *desc = NULL;
1045 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_key, RPMBKEY_LENGTH); 1045 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_key, RPMBKEY_LENGTH);
1046 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, blob, 1046 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, blob,
1047 RPMBKEY_LENGTH + CAAM_PAD); 1047 RPMBKEY_LENGTH + CAAM_PAD);
1048 1048
1049 if (memcmp(staged_buf, RPMB_KEY_MAGIC, strlen(RPMB_KEY_MAGIC))) { 1049 /* copy rpmb key to cache aligned buffer. */
1050 printf("ERROR - rpmb magic doesn't match!\n"); 1050 memset(rpmb_key, 0, RPMBKEY_LENGTH);
1051 return -1; 1051 memcpy(rpmb_key, key, RPMBKEY_LENGTH);
1052 }
1053 1052
1054 /* Get current mmc device. */ 1053 /* Get current mmc device. */
1055 mmcc = mmc_get_env_dev(); 1054 mmcc = mmc_get_env_dev();
1056 mmc = find_mmc_device(mmcc); 1055 mmc = find_mmc_device(mmcc);
1057 if (!mmc) { 1056 if (!mmc) {
1058 printf("error - cannot find '%d' mmc device\n", mmcc); 1057 printf("error - cannot find '%d' mmc device\n", mmcc);
1059 return -1; 1058 return -1;
1060 } 1059 }
1061 desc = mmc_get_blk_desc(mmc); 1060 desc = mmc_get_blk_desc(mmc);
1062 original_part = desc->hwpart; 1061 original_part = desc->hwpart;
1063 1062
1064 /* Switch to the RPMB partition */ 1063 /* Switch to the RPMB partition */
1065 if (desc->hwpart != MMC_PART_RPMB) { 1064 if (desc->hwpart != MMC_PART_RPMB) {
1066 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) { 1065 if (mmc_switch_part(mmc, MMC_PART_RPMB) != 0) {
1067 printf("ERROR - can't switch to rpmb partition \n"); 1066 printf("ERROR - can't switch to rpmb partition \n");
1068 return -1; 1067 return -1;
1069 } 1068 }
1070 desc->hwpart = MMC_PART_RPMB; 1069 desc->hwpart = MMC_PART_RPMB;
1071 } 1070 }
1072 1071
1073 /* Set rpmb key. */
1074 memset(rpmb_key, 0, RPMBKEY_LENGTH);
1075 memcpy(rpmb_key,
1076 staged_buf + strlen(RPMB_KEY_MAGIC), RPMBKEY_LENGTH);
1077
1078 if (mmc_rpmb_set_key(mmc, rpmb_key)) { 1072 if (mmc_rpmb_set_key(mmc, rpmb_key)) {
1079 printf("ERROR - Key already programmed ?\n"); 1073 printf("ERROR - Key already programmed ?\n");
1080 ret = -1; 1074 ret = -1;
1081 goto fail; 1075 goto fail;
1082 } else 1076 } else
1083 printf("RPMB key programed successfully!\n"); 1077 printf("RPMB key programed successfully!\n");
1084 1078
1085 /* Generate keyblob with CAAM. */ 1079 /* Generate keyblob with CAAM. */
1086 kp.rpmb_keyblob_len = RPMBKEY_LENGTH + CAAM_PAD; 1080 kp.rpmb_keyblob_len = RPMBKEY_LENGTH + CAAM_PAD;
1087 strcpy(kp.magic, KEYPACK_MAGIC); 1081 strcpy(kp.magic, KEYPACK_MAGIC);
1088 if (hwcrypto_gen_blob((uint32_t)(ulong)rpmb_key, RPMBKEY_LENGTH, 1082 if (hwcrypto_gen_blob((uint32_t)(ulong)rpmb_key, RPMBKEY_LENGTH,
1089 (uint32_t)(ulong)blob) != 0) { 1083 (uint32_t)(ulong)blob) != 0) {
1090 printf("ERROR - generate rpmb key blob error!\n"); 1084 printf("ERROR - generate rpmb key blob error!\n");
1091 ret = -1; 1085 ret = -1;
1092 goto fail; 1086 goto fail;
1093 } else 1087 } else
1094 printf("RPMB key blob generated!\n"); 1088 printf("RPMB key blob generated!\n");
1095 1089
1096 memcpy(kp.rpmb_keyblob, blob, kp.rpmb_keyblob_len); 1090 memcpy(kp.rpmb_keyblob, blob, kp.rpmb_keyblob_len);
1097 1091
1098 /* Store the rpmb key blob to last block of boot1 partition. */ 1092 /* Store the rpmb key blob to last block of boot1 partition. */
1099 if (mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID) != 0) { 1093 if (mmc_switch_part(mmc, KEYSLOT_HWPARTITION_ID) != 0) {
1100 printf("ERROR - can't switch to boot1 partition! \n"); 1094 printf("ERROR - can't switch to boot1 partition! \n");
1101 ret = -1; 1095 ret = -1;
1102 goto fail; 1096 goto fail;
1103 } else 1097 } else
1104 desc->hwpart = KEYSLOT_HWPARTITION_ID; 1098 desc->hwpart = KEYSLOT_HWPARTITION_ID;
1105 if (blk_dwrite(desc, KEYSLOT_BLKS, 1, (void *)&kp) != 1) { 1099 if (blk_dwrite(desc, KEYSLOT_BLKS, 1, (void *)&kp) != 1) {
1106 printf("ERROR - failed to write rpmbkeyblob!"); 1100 printf("ERROR - failed to write rpmbkeyblob!");
1107 ret = -1; 1101 ret = -1;
1108 goto fail; 1102 goto fail;
1109 } 1103 }
1110 /* Set power-on write protection to boot1 partition. */ 1104 /* Set power-on write protection to boot1 partition. */
1111 if (mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 1105 if (mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1112 EXT_CSD_BOOT_WP, BOOT1_PWR_WP)) { 1106 EXT_CSD_BOOT_WP, BOOT1_PWR_WP)) {
1113 printf("ERROR - unable to set power-on write protection!\n"); 1107 printf("ERROR - unable to set power-on write protection!\n");
1114 ret = -1; 1108 ret = -1;
1115 goto fail; 1109 goto fail;
1116 } 1110 }
1117 1111
1118 /* Erase the key buffer. */ 1112 /* Erase the key buffer. */
1119 memset(rpmb_key, 0, RPMBKEY_LENGTH); 1113 memset(rpmb_key, 0, RPMBKEY_LENGTH);
1114 memset(key, 0, RPMBKEY_LENGTH);
1120 1115
1121 fail: 1116 fail:
1122 /* Return to original partition */ 1117 /* Return to original partition */
1123 if (desc->hwpart != original_part) { 1118 if (desc->hwpart != original_part) {
1124 if (mmc_switch_part(mmc, original_part) != 0) 1119 if (mmc_switch_part(mmc, original_part) != 0)
1125 return -1; 1120 return -1;
1126 desc->hwpart = original_part; 1121 desc->hwpart = original_part;
1127 } 1122 }
1128 1123
1129 return ret; 1124 return ret;
1125 }
1126
1127 int fastboot_set_rpmb_key(uint8_t *staged_buf, uint32_t key_size)
1128 {
1129
1130 if (memcmp(staged_buf, RPMB_KEY_MAGIC, strlen(RPMB_KEY_MAGIC))) {
1131 printf("ERROR - rpmb magic doesn't match!\n");
1132 return -1;
1133 }
1134
1135 return do_rpmb_key_set(staged_buf + strlen(RPMB_KEY_MAGIC),
1136 RPMBKEY_LENGTH);
1137 }
1138
1139 int fastboot_set_rpmb_random_key(void)
1140 {
1141 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_key, RPMBKEY_LENGTH);
1142
1143 if (hwcrypto_gen_rng((ulong)rpmb_key, RPMBKEY_LENGTH)) {
1144 printf("error - can't generate random key!\n");
1145 return -1;
1146 }
1147
1148 return do_rpmb_key_set(rpmb_key, RPMBKEY_LENGTH);
1130 } 1149 }
1131 1150
1132 int avb_set_public_key(uint8_t *staged_buffer, uint32_t size) { 1151 int avb_set_public_key(uint8_t *staged_buffer, uint32_t size) {
1133 1152
1134 if ((staged_buffer == NULL) || (size <= 0)) { 1153 if ((staged_buffer == NULL) || (size <= 0)) {
1135 ERR("Error. Get null staged_buffer\n"); 1154 ERR("Error. Get null staged_buffer\n");
1136 return -1; 1155 return -1;
1137 } 1156 }
1138 if (trusty_write_vbmeta_public_key(staged_buffer, size)) { 1157 if (trusty_write_vbmeta_public_key(staged_buffer, size)) {
1139 ERR("Error. Failed to write vbmeta public key into secure storage\n"); 1158 ERR("Error. Failed to write vbmeta public key into secure storage\n");
1140 return -1; 1159 return -1;
1141 } else 1160 } else
1142 printf("Set vbmeta public key successfully!\n"); 1161 printf("Set vbmeta public key successfully!\n");