Commit e66d1fb4f6c58258d0c3cc59b4d8616c73dde078

Authored by Li Jun
1 parent f85da6b1fc

MLK-20305 usb: gadget: fastboot: fix GUID data length error

Current code uses strlen of string to get the property data
length, which is wrong for unicode string, also the whole
property length also should be corrected(descriptor length
minus head length, 142-10=132), detail data format of single
GUID see below table:

Table 4a: Microsoft Extended Properties Feature Descriptor
===================================================================
         Value          |       TYPE       |      Description
===================================================================
0x8E, 0x00, 0x00, 0x00  | DWORD (LE)       |  Descriptor length
                        |                  |  (142 bytes)
--------------------------------------------------------------------
0x00, 0x01              |  BCD WORD (LE)   |  Version ('1.0')
--------------------------------------------------------------------
0x05, 0x00              |  WORD (LE)       |  Extended Property
                        |                  |  Descriptor index (5)
--------------------------------------------------------------------
0x01, 0x00              |  WORD (LE)       |  Number of sections (1)
--------------------------------------------------------------------
0x84, 0x00, 0x00, 0x00  |  DWORD (LE)      |  Size of the property
                        |                  |  section (132 bytes)
--------------------------------------------------------------------
0x01, 0x00, 0x00, 0x00  |  DWORD (LE)      |  Property data type
                        |                  |   (1 = Unicode
                        |                  |    REG_SZ,
                        |                  |    see table below)
--------------------------------------------------------------------
0x28, 0x00              |  WORD (LE)       |  Property name
                        |                  |   length (40 bytes)
--------------------------------------------------------------------
0x44, 0x00, 0x65, 0x00, |  NUL-terminated  |  Property name
(...)                   |  Unicode String  |  "DeviceInterfaceGUID"
0x74, 0x00, 0x00, 0x00  |  (LE)            |
--------------------------------------------------------------------
0x4e, 0x00, 0x00, 0x00  |  DWORD (LE)      |  Property data
                        |                  |  length (78 bytes)
--------------------------------------------------------------------
0x7b, 0x00, 0x46, 0x00, |  NUL-terminated  |  Property name
(...)                   |  Unicode String  |  "{xxxxxxxx-xxxx-
0x7d, 0x00, 0x00, 0x00  |  (LE)            |  xxxx-xxxx-
                        |                  |  xxxxxxxxxxxx}\0"
--------------------------------------------------------------------

Details of WCID see below link:
https://github.com/pbatard/libwdi/wiki/WCID-Devices

Reviewed-by: Ye Li <ye.li@nxp.com>
Signed-off-by: Li Jun <jun.li@nxp.com>

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