Commit 7ade5b407fe6164c0d07f32f72e487ae5f6f3964

Authored by Ji Luo
1 parent de975d8500

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