Commit 68261e4ca35d6b5eaa0c75a0579ec59d8c6f4503

Authored by faqiang.zhu
1 parent 5da847a4fa

MA-13303 fix the issue with 8qm boot from eMMC to flash SD

For mek_8qm, bootloader offset in SD card is 32kb, in eMMC
bootpartition, it's 0kb. This patch fix the issue that bootloader offset
used in uboot is always based on the device that the board boot from.

Now, if uboot environment variable "target_ubootdev" is set, bootloader
offset used in uboot is based on the device "target_ubootdev" specified.
So boot from eMMC to flash SD, the board can boot from SD and vice versa.

Change-Id: Ia2cd64ba6ece02b98ea3d2addbce00b1661077de
Signed-off-by: faqiang.zhu <faqiang.zhu@nxp.com>

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