Commit 0e8b4af40671aee9e6fa656870aa00efb44a4116

Authored by Ji Luo
1 parent 6192aff568

MA-13629 [Trusty] Add commands to set vbmeta public key

Add commands to write/read vbmeta public key to/from secure
storage. The vbmeta public key can only be set once.
Comands to set the public key:
  fastboot stage <path-to-your-public-key>
  fastboot oem set-public-key

Test: build and boot on imx8qxp_mek.

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

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