Commit 4fa7886f4e811b5bda272aee325d5135e639da80

Authored by Ji Luo
Committed by Ye Li
1 parent 460f3c0ea4

MA-18635 Android: eliminate build warnings

Fix warning: unused variable 'status'.

Test: build.

Change-Id: I948a33686892f940d6be0e315756c23e0ae561ae
Signed-off-by: Ji Luo <ji.luo@nxp.com>
(cherry picked from commit 0bb05e819f427780a3a74b95a78f12b6dd0acaa7)

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

drivers/fastboot/fb_fsl/fb_fsl_command.c
1 // SPDX-License-Identifier: GPL-2.0+ 1 // SPDX-License-Identifier: GPL-2.0+
2 /* 2 /*
3 * Copyright 2019 NXP 3 * Copyright 2019 NXP
4 */ 4 */
5 5
6 #include <asm/mach-imx/sys_proto.h> 6 #include <asm/mach-imx/sys_proto.h>
7 #include <fb_fsl.h> 7 #include <fb_fsl.h>
8 #include <fastboot.h> 8 #include <fastboot.h>
9 #include <fastboot-internal.h> 9 #include <fastboot-internal.h>
10 #include <mmc.h> 10 #include <mmc.h>
11 #include <android_image.h> 11 #include <android_image.h>
12 #include <asm/bootm.h> 12 #include <asm/bootm.h>
13 #include <nand.h> 13 #include <nand.h>
14 #include <part.h> 14 #include <part.h>
15 #include <sparse_format.h> 15 #include <sparse_format.h>
16 #include <image-sparse.h> 16 #include <image-sparse.h>
17 #include <image.h> 17 #include <image.h>
18 #include <asm/mach-imx/boot_mode.h> 18 #include <asm/mach-imx/boot_mode.h>
19 #include <asm/arch/sys_proto.h> 19 #include <asm/arch/sys_proto.h>
20 #include <asm/setup.h> 20 #include <asm/setup.h>
21 #include <env.h> 21 #include <env.h>
22 #ifdef CONFIG_ANDROID_RECOVERY 22 #ifdef CONFIG_ANDROID_RECOVERY
23 #include <recovery.h> 23 #include <recovery.h>
24 #endif 24 #endif
25 25
26 #ifdef CONFIG_BCB_SUPPORT 26 #ifdef CONFIG_BCB_SUPPORT
27 #include "bcb.h" 27 #include "bcb.h"
28 #endif 28 #endif
29 29
30 #ifdef CONFIG_AVB_SUPPORT 30 #ifdef CONFIG_AVB_SUPPORT
31 #include <dt_table.h> 31 #include <dt_table.h>
32 #include <fsl_avb.h> 32 #include <fsl_avb.h>
33 #endif 33 #endif
34 34
35 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 35 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
36 #include <asm-generic/gpio.h> 36 #include <asm-generic/gpio.h>
37 #include <asm/mach-imx/gpio.h> 37 #include <asm/mach-imx/gpio.h>
38 #include "../lib/avb/fsl/fsl_avbkey.h" 38 #include "../lib/avb/fsl/fsl_avbkey.h"
39 #include "../arch/arm/include/asm/mach-imx/hab.h" 39 #include "../arch/arm/include/asm/mach-imx/hab.h"
40 #endif 40 #endif
41 41
42 #if defined(CONFIG_FASTBOOT_LOCK) 42 #if defined(CONFIG_FASTBOOT_LOCK)
43 #include "fastboot_lock_unlock.h" 43 #include "fastboot_lock_unlock.h"
44 #endif 44 #endif
45 45
46 #ifdef CONFIG_IMX_TRUSTY_OS 46 #ifdef CONFIG_IMX_TRUSTY_OS
47 #include "u-boot/sha256.h" 47 #include "u-boot/sha256.h"
48 #include <trusty/libtipc.h> 48 #include <trusty/libtipc.h>
49 #endif 49 #endif
50 50
51 #include "fb_fsl_common.h" 51 #include "fb_fsl_common.h"
52 #include "fb_fsl_virtual_ab.h" 52 #include "fb_fsl_virtual_ab.h"
53 53
54 #define EP_BUFFER_SIZE 4096 54 #define EP_BUFFER_SIZE 4096
55 55
56 /** 56 /**
57 * fastboot_bytes_received - number of bytes received in the current download 57 * fastboot_bytes_received - number of bytes received in the current download
58 */ 58 */
59 static u32 fastboot_bytes_received; 59 static u32 fastboot_bytes_received;
60 60
61 /** 61 /**
62 * fastboot_bytes_expected - number of bytes expected in the current download 62 * fastboot_bytes_expected - number of bytes expected in the current download
63 */ 63 */
64 static u32 fastboot_bytes_expected; 64 static u32 fastboot_bytes_expected;
65 65
66 /* Write the bcb with fastboot bootloader commands */ 66 /* Write the bcb with fastboot bootloader commands */
67 static void enable_fastboot_command(void) 67 static void enable_fastboot_command(void)
68 { 68 {
69 #ifdef CONFIG_BCB_SUPPORT 69 #ifdef CONFIG_BCB_SUPPORT
70 char fastboot_command[32] = {0}; 70 char fastboot_command[32] = {0};
71 strncpy(fastboot_command, FASTBOOT_BCB_CMD, 31); 71 strncpy(fastboot_command, FASTBOOT_BCB_CMD, 31);
72 bcb_write_command(fastboot_command); 72 bcb_write_command(fastboot_command);
73 #endif 73 #endif
74 } 74 }
75 75
76 #ifdef CONFIG_ANDROID_RECOVERY 76 #ifdef CONFIG_ANDROID_RECOVERY
77 /* Write the recovery options with fastboot bootloader commands */ 77 /* Write the recovery options with fastboot bootloader commands */
78 static void enable_recovery_fastboot(void) 78 static void enable_recovery_fastboot(void)
79 { 79 {
80 #ifdef CONFIG_BCB_SUPPORT 80 #ifdef CONFIG_BCB_SUPPORT
81 char msg[32] = {0}; 81 char msg[32] = {0};
82 strncpy(msg, RECOVERY_BCB_CMD, 31); 82 strncpy(msg, RECOVERY_BCB_CMD, 31);
83 bcb_write_command(msg); 83 bcb_write_command(msg);
84 strncpy(msg, RECOVERY_FASTBOOT_ARG, 31); 84 strncpy(msg, RECOVERY_FASTBOOT_ARG, 31);
85 bcb_write_recovery_opt(msg); 85 bcb_write_recovery_opt(msg);
86 #endif 86 #endif
87 } 87 }
88 #endif 88 #endif
89 89
90 /* Get the Boot mode from BCB cmd or Key pressed */ 90 /* Get the Boot mode from BCB cmd or Key pressed */
91 static FbBootMode fastboot_get_bootmode(void) 91 static FbBootMode fastboot_get_bootmode(void)
92 { 92 {
93 int boot_mode = BOOTMODE_NORMAL; 93 int boot_mode = BOOTMODE_NORMAL;
94 #ifdef CONFIG_ANDROID_RECOVERY 94 #ifdef CONFIG_ANDROID_RECOVERY
95 if(is_recovery_key_pressing()) { 95 if(is_recovery_key_pressing()) {
96 boot_mode = BOOTMODE_RECOVERY_KEY_PRESSED; 96 boot_mode = BOOTMODE_RECOVERY_KEY_PRESSED;
97 return boot_mode; 97 return boot_mode;
98 } 98 }
99 #endif 99 #endif
100 #ifdef CONFIG_BCB_SUPPORT 100 #ifdef CONFIG_BCB_SUPPORT
101 int ret = 0; 101 int ret = 0;
102 char command[32]; 102 char command[32];
103 ret = bcb_read_command(command); 103 ret = bcb_read_command(command);
104 if (ret < 0) { 104 if (ret < 0) {
105 printf("read command failed\n"); 105 printf("read command failed\n");
106 return boot_mode; 106 return boot_mode;
107 } 107 }
108 if (!strcmp(command, FASTBOOT_BCB_CMD)) { 108 if (!strcmp(command, FASTBOOT_BCB_CMD)) {
109 boot_mode = BOOTMODE_FASTBOOT_BCB_CMD; 109 boot_mode = BOOTMODE_FASTBOOT_BCB_CMD;
110 } 110 }
111 #ifdef CONFIG_ANDROID_RECOVERY 111 #ifdef CONFIG_ANDROID_RECOVERY
112 else if (!strcmp(command, RECOVERY_BCB_CMD)) { 112 else if (!strcmp(command, RECOVERY_BCB_CMD)) {
113 boot_mode = BOOTMODE_RECOVERY_BCB_CMD; 113 boot_mode = BOOTMODE_RECOVERY_BCB_CMD;
114 } 114 }
115 #endif 115 #endif
116 116
117 /* Clean the mode once its read out, 117 /* Clean the mode once its read out,
118 no matter what in the mode string */ 118 no matter what in the mode string */
119 memset(command, 0, 32); 119 memset(command, 0, 32);
120 bcb_write_command(command); 120 bcb_write_command(command);
121 #endif 121 #endif
122 return boot_mode; 122 return boot_mode;
123 } 123 }
124 124
125 /* export to lib_arm/board.c */ 125 /* export to lib_arm/board.c */
126 void fastboot_run_bootmode(void) 126 void fastboot_run_bootmode(void)
127 { 127 {
128 FbBootMode boot_mode = fastboot_get_bootmode(); 128 FbBootMode boot_mode = fastboot_get_bootmode();
129 switch(boot_mode){ 129 switch(boot_mode){
130 case BOOTMODE_FASTBOOT_BCB_CMD: 130 case BOOTMODE_FASTBOOT_BCB_CMD:
131 /* Make the boot into fastboot mode*/ 131 /* Make the boot into fastboot mode*/
132 puts("Fastboot: Got bootloader commands!\n"); 132 puts("Fastboot: Got bootloader commands!\n");
133 run_command("fastboot 0", 0); 133 run_command("fastboot 0", 0);
134 break; 134 break;
135 #ifdef CONFIG_ANDROID_RECOVERY 135 #ifdef CONFIG_ANDROID_RECOVERY
136 case BOOTMODE_RECOVERY_BCB_CMD: 136 case BOOTMODE_RECOVERY_BCB_CMD:
137 case BOOTMODE_RECOVERY_KEY_PRESSED: 137 case BOOTMODE_RECOVERY_KEY_PRESSED:
138 /* Make the boot into recovery mode */ 138 /* Make the boot into recovery mode */
139 puts("Fastboot: Got Recovery key pressing or recovery commands!\n"); 139 puts("Fastboot: Got Recovery key pressing or recovery commands!\n");
140 board_recovery_setup(); 140 board_recovery_setup();
141 break; 141 break;
142 #endif 142 #endif
143 default: 143 default:
144 /* skip special mode boot*/ 144 /* skip special mode boot*/
145 puts("Fastboot: Normal\n"); 145 puts("Fastboot: Normal\n");
146 break; 146 break;
147 } 147 }
148 } 148 }
149 149
150 150
151 151
152 /** 152 /**
153 * okay() - Send bare OKAY response 153 * okay() - Send bare OKAY response
154 * 154 *
155 * @cmd_parameter: Pointer to command parameter 155 * @cmd_parameter: Pointer to command parameter
156 * @response: Pointer to fastboot response buffer 156 * @response: Pointer to fastboot response buffer
157 * 157 *
158 * Send a bare OKAY fastboot response. This is used where the command is 158 * Send a bare OKAY fastboot response. This is used where the command is
159 * valid, but all the work is done after the response has been sent (e.g. 159 * valid, but all the work is done after the response has been sent (e.g.
160 * boot, reboot etc.) 160 * boot, reboot etc.)
161 */ 161 */
162 static void okay(char *cmd_parameter, char *response) 162 static void okay(char *cmd_parameter, char *response)
163 { 163 {
164 fastboot_okay(NULL, response); 164 fastboot_okay(NULL, response);
165 } 165 }
166 166
167 /** 167 /**
168 * getvar() - Read a config/version variable 168 * getvar() - Read a config/version variable
169 * 169 *
170 * @cmd_parameter: Pointer to command parameter 170 * @cmd_parameter: Pointer to command parameter
171 * @response: Pointer to fastboot response buffer 171 * @response: Pointer to fastboot response buffer
172 */ 172 */
173 static void getvar(char *cmd_parameter, char *response) 173 static void getvar(char *cmd_parameter, char *response)
174 { 174 {
175 fastboot_getvar(cmd_parameter, response); 175 fastboot_getvar(cmd_parameter, response);
176 } 176 }
177 177
178 /** 178 /**
179 * reboot_bootloader() - Sets reboot bootloader flag. 179 * reboot_bootloader() - Sets reboot bootloader flag.
180 * 180 *
181 * @cmd_parameter: Pointer to command parameter 181 * @cmd_parameter: Pointer to command parameter
182 * @response: Pointer to fastboot response buffer 182 * @response: Pointer to fastboot response buffer
183 */ 183 */
184 static void reboot_bootloader(char *cmd_parameter, char *response) 184 static void reboot_bootloader(char *cmd_parameter, char *response)
185 { 185 {
186 enable_fastboot_command(); 186 enable_fastboot_command();
187 187
188 if (fastboot_set_reboot_flag()) 188 if (fastboot_set_reboot_flag())
189 fastboot_fail("Cannot set reboot flag", response); 189 fastboot_fail("Cannot set reboot flag", response);
190 else 190 else
191 fastboot_okay(NULL, response); 191 fastboot_okay(NULL, response);
192 } 192 }
193 193
194 #ifdef CONFIG_ANDROID_RECOVERY 194 #ifdef CONFIG_ANDROID_RECOVERY
195 /** 195 /**
196 * reboot_fastboot() - Sets reboot fastboot flag. 196 * reboot_fastboot() - Sets reboot fastboot flag.
197 * 197 *
198 * @cmd_parameter: Pointer to command parameter 198 * @cmd_parameter: Pointer to command parameter
199 * @response: Pointer to fastboot response buffer 199 * @response: Pointer to fastboot response buffer
200 */ 200 */
201 static void reboot_fastboot(char *cmd_parameter, char *response) 201 static void reboot_fastboot(char *cmd_parameter, char *response)
202 { 202 {
203 enable_recovery_fastboot(); 203 enable_recovery_fastboot();
204 204
205 if (fastboot_set_reboot_flag()) 205 if (fastboot_set_reboot_flag())
206 fastboot_fail("Cannot set reboot flag", response); 206 fastboot_fail("Cannot set reboot flag", response);
207 else 207 else
208 fastboot_okay(NULL, response); 208 fastboot_okay(NULL, response);
209 } 209 }
210 #endif 210 #endif
211 211
212 static void upload(char *cmd_parameter, char *response) 212 static void upload(char *cmd_parameter, char *response)
213 { 213 {
214 if (!fastboot_bytes_received || fastboot_bytes_received > (EP_BUFFER_SIZE * 32)) { 214 if (!fastboot_bytes_received || fastboot_bytes_received > (EP_BUFFER_SIZE * 32)) {
215 fastboot_fail("", response); 215 fastboot_fail("", response);
216 return; 216 return;
217 } 217 }
218 218
219 printf("Will upload %d bytes.\n", fastboot_bytes_received); 219 printf("Will upload %d bytes.\n", fastboot_bytes_received);
220 snprintf(response, FASTBOOT_RESPONSE_LEN, "DATA%08x", fastboot_bytes_received); 220 snprintf(response, FASTBOOT_RESPONSE_LEN, "DATA%08x", fastboot_bytes_received);
221 fastboot_tx_write_more(response); 221 fastboot_tx_write_more(response);
222 222
223 fastboot_tx_write((const char *)(fastboot_buf_addr), fastboot_bytes_received); 223 fastboot_tx_write((const char *)(fastboot_buf_addr), fastboot_bytes_received);
224 224
225 snprintf(response,FASTBOOT_RESPONSE_LEN, "OKAY"); 225 snprintf(response,FASTBOOT_RESPONSE_LEN, "OKAY");
226 fastboot_tx_write_more(response); 226 fastboot_tx_write_more(response);
227 227
228 fastboot_none_resp(response); 228 fastboot_none_resp(response);
229 } 229 }
230 230
231 /** 231 /**
232 * fastboot_download() - Start a download transfer from the client 232 * fastboot_download() - Start a download transfer from the client
233 * 233 *
234 * @cmd_parameter: Pointer to command parameter 234 * @cmd_parameter: Pointer to command parameter
235 * @response: Pointer to fastboot response buffer 235 * @response: Pointer to fastboot response buffer
236 */ 236 */
237 static void download(char *cmd_parameter, char *response) 237 static void download(char *cmd_parameter, char *response)
238 { 238 {
239 char *tmp; 239 char *tmp;
240 240
241 if (!cmd_parameter) { 241 if (!cmd_parameter) {
242 fastboot_fail("Expected command parameter", response); 242 fastboot_fail("Expected command parameter", response);
243 return; 243 return;
244 } 244 }
245 fastboot_bytes_received = 0; 245 fastboot_bytes_received = 0;
246 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16); 246 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
247 if (fastboot_bytes_expected == 0) { 247 if (fastboot_bytes_expected == 0) {
248 fastboot_fail("Expected nonzero image size", response); 248 fastboot_fail("Expected nonzero image size", response);
249 return; 249 return;
250 } 250 }
251 /* 251 /*
252 * Nothing to download yet. Response is of the form: 252 * Nothing to download yet. Response is of the form:
253 * [DATA|FAIL]$cmd_parameter 253 * [DATA|FAIL]$cmd_parameter
254 * 254 *
255 * where cmd_parameter is an 8 digit hexadecimal number 255 * where cmd_parameter is an 8 digit hexadecimal number
256 */ 256 */
257 if (fastboot_bytes_expected > fastboot_buf_size) { 257 if (fastboot_bytes_expected > fastboot_buf_size) {
258 fastboot_fail(cmd_parameter, response); 258 fastboot_fail(cmd_parameter, response);
259 } else { 259 } else {
260 printf("Starting download of %d bytes\n", 260 printf("Starting download of %d bytes\n",
261 fastboot_bytes_expected); 261 fastboot_bytes_expected);
262 fastboot_response("DATA", response, "%s", cmd_parameter); 262 fastboot_response("DATA", response, "%s", cmd_parameter);
263 } 263 }
264 } 264 }
265 265
266 /** 266 /**
267 * fastboot_data_remaining() - return bytes remaining in current transfer 267 * fastboot_data_remaining() - return bytes remaining in current transfer
268 * 268 *
269 * Return: Number of bytes left in the current download 269 * Return: Number of bytes left in the current download
270 */ 270 */
271 u32 fastboot_data_remaining(void) 271 u32 fastboot_data_remaining(void)
272 { 272 {
273 if (fastboot_bytes_received >= fastboot_bytes_expected) 273 if (fastboot_bytes_received >= fastboot_bytes_expected)
274 return 0; 274 return 0;
275 275
276 return fastboot_bytes_expected - fastboot_bytes_received; 276 return fastboot_bytes_expected - fastboot_bytes_received;
277 } 277 }
278 278
279 /** 279 /**
280 * fastboot_data_download() - Copy image data to fastboot_buf_addr. 280 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
281 * 281 *
282 * @fastboot_data: Pointer to received fastboot data 282 * @fastboot_data: Pointer to received fastboot data
283 * @fastboot_data_len: Length of received fastboot data 283 * @fastboot_data_len: Length of received fastboot data
284 * @response: Pointer to fastboot response buffer 284 * @response: Pointer to fastboot response buffer
285 * 285 *
286 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to 286 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
287 * response. fastboot_bytes_received is updated to indicate the number 287 * response. fastboot_bytes_received is updated to indicate the number
288 * of bytes that have been transferred. 288 * of bytes that have been transferred.
289 * 289 *
290 * On completion sets image_size and ${filesize} to the total size of the 290 * On completion sets image_size and ${filesize} to the total size of the
291 * downloaded image. 291 * downloaded image.
292 */ 292 */
293 void fastboot_data_download(const void *fastboot_data, 293 void fastboot_data_download(const void *fastboot_data,
294 unsigned int fastboot_data_len, 294 unsigned int fastboot_data_len,
295 char *response) 295 char *response)
296 { 296 {
297 #define BYTES_PER_DOT 0x20000 297 #define BYTES_PER_DOT 0x20000
298 u32 pre_dot_num, now_dot_num; 298 u32 pre_dot_num, now_dot_num;
299 299
300 if (fastboot_data_len == 0 || 300 if (fastboot_data_len == 0 ||
301 (fastboot_bytes_received + fastboot_data_len) > 301 (fastboot_bytes_received + fastboot_data_len) >
302 fastboot_bytes_expected) { 302 fastboot_bytes_expected) {
303 fastboot_fail("Received invalid data length", 303 fastboot_fail("Received invalid data length",
304 response); 304 response);
305 return; 305 return;
306 } 306 }
307 /* Download data to fastboot_buf_addr */ 307 /* Download data to fastboot_buf_addr */
308 memcpy(fastboot_buf_addr + fastboot_bytes_received, 308 memcpy(fastboot_buf_addr + fastboot_bytes_received,
309 fastboot_data, fastboot_data_len); 309 fastboot_data, fastboot_data_len);
310 310
311 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT; 311 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
312 fastboot_bytes_received += fastboot_data_len; 312 fastboot_bytes_received += fastboot_data_len;
313 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT; 313 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
314 314
315 if (pre_dot_num != now_dot_num) { 315 if (pre_dot_num != now_dot_num) {
316 putc('.'); 316 putc('.');
317 if (!(now_dot_num % 74)) 317 if (!(now_dot_num % 74))
318 putc('\n'); 318 putc('\n');
319 } 319 }
320 *response = '\0'; 320 *response = '\0';
321 } 321 }
322 322
323 /** 323 /**
324 * fastboot_data_complete() - Mark current transfer complete 324 * fastboot_data_complete() - Mark current transfer complete
325 * 325 *
326 * @response: Pointer to fastboot response buffer 326 * @response: Pointer to fastboot response buffer
327 * 327 *
328 * Set image_size and ${filesize} to the total size of the downloaded image. 328 * Set image_size and ${filesize} to the total size of the downloaded image.
329 */ 329 */
330 void fastboot_data_complete(char *response) 330 void fastboot_data_complete(char *response)
331 { 331 {
332 /* Download complete. Respond with "OKAY" */ 332 /* Download complete. Respond with "OKAY" */
333 fastboot_okay(NULL, response); 333 fastboot_okay(NULL, response);
334 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received); 334 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
335 env_set_hex("filesize", fastboot_bytes_received); 335 env_set_hex("filesize", fastboot_bytes_received);
336 env_set_hex("fastboot_bytes", fastboot_bytes_received); 336 env_set_hex("fastboot_bytes", fastboot_bytes_received);
337 fastboot_bytes_expected = 0; 337 fastboot_bytes_expected = 0;
338 } 338 }
339 339
340 #if defined(CONFIG_FASTBOOT_LOCK) 340 #if defined(CONFIG_FASTBOOT_LOCK)
341 static int partition_table_valid(void) 341 static int partition_table_valid(void)
342 { 342 {
343 int status, mmc_no; 343 int status, mmc_no;
344 struct blk_desc *dev_desc; 344 struct blk_desc *dev_desc;
345 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_ARM64) 345 #if defined(CONFIG_IMX_TRUSTY_OS) && !defined(CONFIG_ARM64)
346 /* Prevent other partition accessing when no TOS flashed. */ 346 /* Prevent other partition accessing when no TOS flashed. */
347 if (!tos_flashed) 347 if (!tos_flashed)
348 return 0; 348 return 0;
349 #endif 349 #endif
350 disk_partition_t info; 350 disk_partition_t info;
351 mmc_no = fastboot_devinfo.dev_id; 351 mmc_no = fastboot_devinfo.dev_id;
352 dev_desc = blk_get_dev("mmc", mmc_no); 352 dev_desc = blk_get_dev("mmc", mmc_no);
353 if (dev_desc) 353 if (dev_desc)
354 status = part_get_info(dev_desc, 1, &info); 354 status = part_get_info(dev_desc, 1, &info);
355 else 355 else
356 status = -1; 356 status = -1;
357 return (status == 0); 357 return (status == 0);
358 } 358 }
359 359
360 static void wipe_all_userdata(void) 360 static void wipe_all_userdata(void)
361 { 361 {
362 char response[FASTBOOT_RESPONSE_LEN]; 362 char response[FASTBOOT_RESPONSE_LEN];
363 363
364 /* Erase all user data */ 364 /* Erase all user data */
365 printf("Start userdata wipe process....\n"); 365 printf("Start userdata wipe process....\n");
366 /* Erase /data partition */ 366 /* Erase /data partition */
367 fastboot_wipe_data_partition(); 367 fastboot_wipe_data_partition();
368 368
369 #if defined (CONFIG_ANDROID_SUPPORT) || defined (CONFIG_ANDROID_AUTO_SUPPORT) 369 #if defined (CONFIG_ANDROID_SUPPORT) || defined (CONFIG_ANDROID_AUTO_SUPPORT)
370 /* Erase the misc partition. */ 370 /* Erase the misc partition. */
371 process_erase_mmc(FASTBOOT_PARTITION_MISC, response); 371 process_erase_mmc(FASTBOOT_PARTITION_MISC, response);
372 #endif 372 #endif
373 373
374 #ifndef CONFIG_ANDROID_AB_SUPPORT 374 #ifndef CONFIG_ANDROID_AB_SUPPORT
375 /* Erase the cache partition for legacy imx6/7 */ 375 /* Erase the cache partition for legacy imx6/7 */
376 process_erase_mmc(FASTBOOT_PARTITION_CACHE, response); 376 process_erase_mmc(FASTBOOT_PARTITION_CACHE, response);
377 #endif 377 #endif
378 378
379 #if defined(AVB_RPMB) && !defined(CONFIG_IMX_TRUSTY_OS) 379 #if defined(AVB_RPMB) && !defined(CONFIG_IMX_TRUSTY_OS)
380 printf("Start stored_rollback_index wipe process....\n"); 380 printf("Start stored_rollback_index wipe process....\n");
381 rbkidx_erase(); 381 rbkidx_erase();
382 printf("Wipe stored_rollback_index completed.\n"); 382 printf("Wipe stored_rollback_index completed.\n");
383 #endif 383 #endif
384 process_erase_mmc(FASTBOOT_PARTITION_METADATA, response); 384 process_erase_mmc(FASTBOOT_PARTITION_METADATA, response);
385 printf("Wipe userdata completed.\n"); 385 printf("Wipe userdata completed.\n");
386 } 386 }
387 387
388 static FbLockState do_fastboot_unlock(bool force) 388 static FbLockState do_fastboot_unlock(bool force)
389 { 389 {
390 int status; 390 int status;
391 391
392 if (fastboot_get_lock_stat() == FASTBOOT_UNLOCK) { 392 if (fastboot_get_lock_stat() == FASTBOOT_UNLOCK) {
393 printf("The device is already unlocked\n"); 393 printf("The device is already unlocked\n");
394 return FASTBOOT_UNLOCK; 394 return FASTBOOT_UNLOCK;
395 } 395 }
396 if ((fastboot_lock_enable() == FASTBOOT_UL_ENABLE) || force) { 396 if ((fastboot_lock_enable() == FASTBOOT_UL_ENABLE) || force) {
397 printf("It is able to unlock device. %d\n",fastboot_lock_enable()); 397 printf("It is able to unlock device. %d\n",fastboot_lock_enable());
398 398
399 #if defined(CONFIG_SECURE_UNLOCK) && defined(CONFIG_IMX_TRUSTY_OS) 399 #if defined(CONFIG_SECURE_UNLOCK) && defined(CONFIG_IMX_TRUSTY_OS)
400 if ((fastboot_bytes_received == 0) || !hab_is_enabled()) { 400 if ((fastboot_bytes_received == 0) || !hab_is_enabled()) {
401 printf("No unlock credential found or hab is not closed!\n"); 401 printf("No unlock credential found or hab is not closed!\n");
402 return FASTBOOT_LOCK_ERROR; 402 return FASTBOOT_LOCK_ERROR;
403 } else { 403 } else {
404 char *serial = get_serial(); 404 char *serial = get_serial();
405 status = trusty_verify_secure_unlock(fastboot_buf_addr, 405 status = trusty_verify_secure_unlock(fastboot_buf_addr,
406 fastboot_bytes_received, 406 fastboot_bytes_received,
407 (uint8_t *)serial, 16); 407 (uint8_t *)serial, 16);
408 if (status < 0) { 408 if (status < 0) {
409 printf("verify secure unlock credential fail due Trusty return %d\n", status); 409 printf("verify secure unlock credential fail due Trusty return %d\n", status);
410 return FASTBOOT_LOCK_ERROR; 410 return FASTBOOT_LOCK_ERROR;
411 } 411 }
412 } 412 }
413 #endif 413 #endif
414 414
415 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 415 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
416 if (virtual_ab_update_is_merging() || 416 if (virtual_ab_update_is_merging() ||
417 (virtual_ab_update_is_snapshoted() && !virtual_ab_slot_match())) { 417 (virtual_ab_update_is_snapshoted() && !virtual_ab_slot_match())) {
418 printf("Can not erase userdata while a snapshot update is in progress!\n"); 418 printf("Can not erase userdata while a snapshot update is in progress!\n");
419 return FASTBOOT_LOCK_ERROR; 419 return FASTBOOT_LOCK_ERROR;
420 } 420 }
421 #endif 421 #endif
422 422
423 wipe_all_userdata(); 423 wipe_all_userdata();
424 status = fastboot_set_lock_stat(FASTBOOT_UNLOCK); 424 status = fastboot_set_lock_stat(FASTBOOT_UNLOCK);
425 if (status < 0) 425 if (status < 0)
426 return FASTBOOT_LOCK_ERROR; 426 return FASTBOOT_LOCK_ERROR;
427 } else { 427 } else {
428 printf("It is not able to unlock device."); 428 printf("It is not able to unlock device.");
429 return FASTBOOT_LOCK_ERROR; 429 return FASTBOOT_LOCK_ERROR;
430 } 430 }
431 431
432 return FASTBOOT_UNLOCK; 432 return FASTBOOT_UNLOCK;
433 } 433 }
434 434
435 static FbLockState do_fastboot_lock(void) 435 static FbLockState do_fastboot_lock(void)
436 { 436 {
437 int status; 437 int status;
438 438
439 if (fastboot_get_lock_stat() == FASTBOOT_LOCK) { 439 if (fastboot_get_lock_stat() == FASTBOOT_LOCK) {
440 printf("The device is already locked\n"); 440 printf("The device is already locked\n");
441 return FASTBOOT_LOCK; 441 return FASTBOOT_LOCK;
442 } 442 }
443 443
444 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 444 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
445 if (virtual_ab_update_is_merging() || 445 if (virtual_ab_update_is_merging() ||
446 (virtual_ab_update_is_snapshoted() && !virtual_ab_slot_match())) { 446 (virtual_ab_update_is_snapshoted() && !virtual_ab_slot_match())) {
447 printf("Can not erase userdata while a snapshot update is in progress!\n"); 447 printf("Can not erase userdata while a snapshot update is in progress!\n");
448 return FASTBOOT_LOCK_ERROR; 448 return FASTBOOT_LOCK_ERROR;
449 } 449 }
450 #endif 450 #endif
451 451
452 wipe_all_userdata(); 452 wipe_all_userdata();
453 status = fastboot_set_lock_stat(FASTBOOT_LOCK); 453 status = fastboot_set_lock_stat(FASTBOOT_LOCK);
454 if (status < 0) 454 if (status < 0)
455 return FASTBOOT_LOCK_ERROR; 455 return FASTBOOT_LOCK_ERROR;
456 456
457 return FASTBOOT_LOCK; 457 return FASTBOOT_LOCK;
458 } 458 }
459 459
460 static bool endswith(char* s, char* subs) { 460 static bool endswith(char* s, char* subs) {
461 if (!s || !subs) 461 if (!s || !subs)
462 return false; 462 return false;
463 uint32_t len = strlen(s); 463 uint32_t len = strlen(s);
464 uint32_t sublen = strlen(subs); 464 uint32_t sublen = strlen(subs);
465 if (len < sublen) { 465 if (len < sublen) {
466 return false; 466 return false;
467 } 467 }
468 if (strncmp(s + len - sublen, subs, sublen)) { 468 if (strncmp(s + len - sublen, subs, sublen)) {
469 return false; 469 return false;
470 } 470 }
471 return true; 471 return true;
472 } 472 }
473 473
474 static void flashing(char *cmd, char *response) 474 static void flashing(char *cmd, char *response)
475 { 475 {
476 FbLockState status; 476 FbLockState status;
477 FbLockEnableResult result; 477 FbLockEnableResult result;
478 if (endswith(cmd, "lock_critical")) { 478 if (endswith(cmd, "lock_critical")) {
479 strcpy(response, "OKAY"); 479 strcpy(response, "OKAY");
480 } 480 }
481 #ifdef CONFIG_AVB_ATX 481 #ifdef CONFIG_AVB_ATX
482 else if (endswith(cmd, FASTBOOT_AVB_AT_PERM_ATTR)) { 482 else if (endswith(cmd, FASTBOOT_AVB_AT_PERM_ATTR)) {
483 if (avb_atx_fuse_perm_attr(fastboot_buf_addr, fastboot_bytes_received)) 483 if (avb_atx_fuse_perm_attr(fastboot_buf_addr, fastboot_bytes_received))
484 strcpy(response, "FAILInternal error!"); 484 strcpy(response, "FAILInternal error!");
485 else 485 else
486 strcpy(response, "OKAY"); 486 strcpy(response, "OKAY");
487 } else if (endswith(cmd, FASTBOOT_AT_GET_UNLOCK_CHALLENGE)) { 487 } else if (endswith(cmd, FASTBOOT_AT_GET_UNLOCK_CHALLENGE)) {
488 if (avb_atx_get_unlock_challenge(fsl_avb_ops.atx_ops, 488 if (avb_atx_get_unlock_challenge(fsl_avb_ops.atx_ops,
489 fastboot_buf_addr, &fastboot_bytes_received)) 489 fastboot_buf_addr, &fastboot_bytes_received))
490 strcpy(response, "FAILInternal error!"); 490 strcpy(response, "FAILInternal error!");
491 else 491 else
492 strcpy(response, "OKAY"); 492 strcpy(response, "OKAY");
493 } else if (endswith(cmd, FASTBOOT_AT_UNLOCK_VBOOT)) { 493 } else if (endswith(cmd, FASTBOOT_AT_UNLOCK_VBOOT)) {
494 if (at_unlock_vboot_is_disabled()) { 494 if (at_unlock_vboot_is_disabled()) {
495 printf("unlock vboot already disabled, can't unlock the device!\n"); 495 printf("unlock vboot already disabled, can't unlock the device!\n");
496 strcpy(response, "FAILunlock vboot already disabled!."); 496 strcpy(response, "FAILunlock vboot already disabled!.");
497 } else { 497 } else {
498 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK 498 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK
499 if (avb_atx_verify_unlock_credential(fsl_avb_ops.atx_ops, 499 if (avb_atx_verify_unlock_credential(fsl_avb_ops.atx_ops,
500 fastboot_buf_addr)) 500 fastboot_buf_addr))
501 strcpy(response, "FAILIncorrect unlock credential!"); 501 strcpy(response, "FAILIncorrect unlock credential!");
502 else { 502 else {
503 #endif 503 #endif
504 status = do_fastboot_unlock(true); 504 status = do_fastboot_unlock(true);
505 if (status != FASTBOOT_LOCK_ERROR) 505 if (status != FASTBOOT_LOCK_ERROR)
506 strcpy(response, "OKAY"); 506 strcpy(response, "OKAY");
507 else 507 else
508 strcpy(response, "FAILunlock device failed."); 508 strcpy(response, "FAILunlock device failed.");
509 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK 509 #ifdef CONFIG_AT_AUTHENTICATE_UNLOCK
510 } 510 }
511 #endif 511 #endif
512 } 512 }
513 } else if (endswith(cmd, FASTBOOT_AT_LOCK_VBOOT)) { 513 } else if (endswith(cmd, FASTBOOT_AT_LOCK_VBOOT)) {
514 if (perm_attr_are_fused()) { 514 if (perm_attr_are_fused()) {
515 status = do_fastboot_lock(); 515 status = do_fastboot_lock();
516 if (status != FASTBOOT_LOCK_ERROR) 516 if (status != FASTBOOT_LOCK_ERROR)
517 strcpy(response, "OKAY"); 517 strcpy(response, "OKAY");
518 else 518 else
519 strcpy(response, "FAILlock device failed."); 519 strcpy(response, "FAILlock device failed.");
520 } else 520 } else
521 strcpy(response, "FAILpermanent attributes not fused!"); 521 strcpy(response, "FAILpermanent attributes not fused!");
522 } else if (endswith(cmd, FASTBOOT_AT_DISABLE_UNLOCK_VBOOT)) { 522 } else if (endswith(cmd, FASTBOOT_AT_DISABLE_UNLOCK_VBOOT)) {
523 /* This command can only be called after 'oem at-lock-vboot' */ 523 /* This command can only be called after 'oem at-lock-vboot' */
524 status = fastboot_get_lock_stat(); 524 status = fastboot_get_lock_stat();
525 if (status == FASTBOOT_LOCK) { 525 if (status == FASTBOOT_LOCK) {
526 if (at_unlock_vboot_is_disabled()) { 526 if (at_unlock_vboot_is_disabled()) {
527 printf("unlock vboot already disabled!\n"); 527 printf("unlock vboot already disabled!\n");
528 strcpy(response, "OKAY"); 528 strcpy(response, "OKAY");
529 } 529 }
530 else { 530 else {
531 if (!at_disable_vboot_unlock()) 531 if (!at_disable_vboot_unlock())
532 strcpy(response, "OKAY"); 532 strcpy(response, "OKAY");
533 else 533 else
534 strcpy(response, "FAILdisable unlock vboot fail!"); 534 strcpy(response, "FAILdisable unlock vboot fail!");
535 } 535 }
536 } else 536 } else
537 strcpy(response, "FAILplease lock the device first!"); 537 strcpy(response, "FAILplease lock the device first!");
538 } 538 }
539 #endif /* CONFIG_AVB_ATX */ 539 #endif /* CONFIG_AVB_ATX */
540 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 540 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
541 else if (endswith(cmd, FASTBOOT_BOOTLOADER_VBOOT_KEY)) { 541 else if (endswith(cmd, FASTBOOT_BOOTLOADER_VBOOT_KEY)) {
542 strcpy(response, "OKAY"); 542 strcpy(response, "OKAY");
543 } 543 }
544 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */ 544 #endif /* CONFIG_ANDROID_THINGS_SUPPORT */
545 #ifdef CONFIG_IMX_TRUSTY_OS 545 #ifdef CONFIG_IMX_TRUSTY_OS
546 else if (endswith(cmd, FASTBOOT_GET_CA_REQ)) { 546 else if (endswith(cmd, FASTBOOT_GET_CA_REQ)) {
547 uint8_t *ca_output; 547 uint8_t *ca_output;
548 uint32_t ca_length, cp_length; 548 uint32_t ca_length, cp_length;
549 if (trusty_atap_get_ca_request(fastboot_buf_addr, fastboot_bytes_received, 549 if (trusty_atap_get_ca_request(fastboot_buf_addr, fastboot_bytes_received,
550 &(ca_output), &ca_length)) { 550 &(ca_output), &ca_length)) {
551 printf("ERROR get_ca_request failed!\n"); 551 printf("ERROR get_ca_request failed!\n");
552 strcpy(response, "FAILInternal error!"); 552 strcpy(response, "FAILInternal error!");
553 } else { 553 } else {
554 cp_length = min((uint32_t)CONFIG_FASTBOOT_BUF_SIZE, ca_length); 554 cp_length = min((uint32_t)CONFIG_FASTBOOT_BUF_SIZE, ca_length);
555 memcpy(fastboot_buf_addr, ca_output, cp_length); 555 memcpy(fastboot_buf_addr, ca_output, cp_length);
556 fastboot_bytes_received = ca_length; 556 fastboot_bytes_received = ca_length;
557 strcpy(response, "OKAY"); 557 strcpy(response, "OKAY");
558 } 558 }
559 559
560 } else if (endswith(cmd, FASTBOOT_SET_CA_RESP)) { 560 } else if (endswith(cmd, FASTBOOT_SET_CA_RESP)) {
561 if (trusty_atap_set_ca_response(fastboot_buf_addr, fastboot_bytes_received)) { 561 if (trusty_atap_set_ca_response(fastboot_buf_addr, fastboot_bytes_received)) {
562 printf("ERROR set_ca_response failed!\n"); 562 printf("ERROR set_ca_response failed!\n");
563 strcpy(response, "FAILInternal error!"); 563 strcpy(response, "FAILInternal error!");
564 } else 564 } else
565 strcpy(response, "OKAY"); 565 strcpy(response, "OKAY");
566 } else if (endswith(cmd, FASTBOOT_SET_RSA_ATTESTATION_KEY_ENC)) { 566 } else if (endswith(cmd, FASTBOOT_SET_RSA_ATTESTATION_KEY_ENC)) {
567 if (trusty_set_attestation_key_enc(fastboot_buf_addr, 567 if (trusty_set_attestation_key_enc(fastboot_buf_addr,
568 fastboot_bytes_received, 568 fastboot_bytes_received,
569 KM_ALGORITHM_RSA)) { 569 KM_ALGORITHM_RSA)) {
570 printf("ERROR set rsa attestation key failed!\n"); 570 printf("ERROR set rsa attestation key failed!\n");
571 strcpy(response, "FAILInternal error!"); 571 strcpy(response, "FAILInternal error!");
572 } else { 572 } else {
573 printf("Set rsa attestation key successfully!\n"); 573 printf("Set rsa attestation key successfully!\n");
574 strcpy(response, "OKAY"); 574 strcpy(response, "OKAY");
575 } 575 }
576 } else if (endswith(cmd, FASTBOOT_SET_EC_ATTESTATION_KEY_ENC)) { 576 } else if (endswith(cmd, FASTBOOT_SET_EC_ATTESTATION_KEY_ENC)) {
577 if (trusty_set_attestation_key_enc(fastboot_buf_addr, 577 if (trusty_set_attestation_key_enc(fastboot_buf_addr,
578 fastboot_bytes_received, 578 fastboot_bytes_received,
579 KM_ALGORITHM_EC)) { 579 KM_ALGORITHM_EC)) {
580 printf("ERROR set ec attestation key failed!\n"); 580 printf("ERROR set ec attestation key failed!\n");
581 strcpy(response, "FAILInternal error!"); 581 strcpy(response, "FAILInternal error!");
582 } else { 582 } else {
583 printf("Set ec attestation key successfully!\n"); 583 printf("Set ec attestation key successfully!\n");
584 strcpy(response, "OKAY"); 584 strcpy(response, "OKAY");
585 } 585 }
586 } else if (endswith(cmd, FASTBOOT_APPEND_RSA_ATTESTATION_CERT_ENC)) { 586 } else if (endswith(cmd, FASTBOOT_APPEND_RSA_ATTESTATION_CERT_ENC)) {
587 if (trusty_append_attestation_cert_chain_enc(fastboot_buf_addr, 587 if (trusty_append_attestation_cert_chain_enc(fastboot_buf_addr,
588 fastboot_bytes_received, 588 fastboot_bytes_received,
589 KM_ALGORITHM_RSA)) { 589 KM_ALGORITHM_RSA)) {
590 printf("ERROR append rsa attestation cert chain failed!\n"); 590 printf("ERROR append rsa attestation cert chain failed!\n");
591 strcpy(response, "FAILInternal error!"); 591 strcpy(response, "FAILInternal error!");
592 } else { 592 } else {
593 printf("Append rsa attestation key successfully!\n"); 593 printf("Append rsa attestation key successfully!\n");
594 strcpy(response, "OKAY"); 594 strcpy(response, "OKAY");
595 } 595 }
596 } else if (endswith(cmd, FASTBOOT_APPEND_EC_ATTESTATION_CERT_ENC)) { 596 } else if (endswith(cmd, FASTBOOT_APPEND_EC_ATTESTATION_CERT_ENC)) {
597 if (trusty_append_attestation_cert_chain_enc(fastboot_buf_addr, 597 if (trusty_append_attestation_cert_chain_enc(fastboot_buf_addr,
598 fastboot_bytes_received, 598 fastboot_bytes_received,
599 KM_ALGORITHM_EC)) { 599 KM_ALGORITHM_EC)) {
600 printf("ERROR append ec attestation cert chain failed!\n"); 600 printf("ERROR append ec attestation cert chain failed!\n");
601 strcpy(response, "FAILInternal error!"); 601 strcpy(response, "FAILInternal error!");
602 } else { 602 } else {
603 printf("Append ec attestation key successfully!\n"); 603 printf("Append ec attestation key successfully!\n");
604 strcpy(response, "OKAY"); 604 strcpy(response, "OKAY");
605 } 605 }
606 } else if (endswith(cmd, FASTBOOT_SET_RSA_ATTESTATION_KEY)) { 606 } else if (endswith(cmd, FASTBOOT_SET_RSA_ATTESTATION_KEY)) {
607 if (trusty_set_attestation_key(fastboot_buf_addr, 607 if (trusty_set_attestation_key(fastboot_buf_addr,
608 fastboot_bytes_received, 608 fastboot_bytes_received,
609 KM_ALGORITHM_RSA)) { 609 KM_ALGORITHM_RSA)) {
610 printf("ERROR set rsa attestation key failed!\n"); 610 printf("ERROR set rsa attestation key failed!\n");
611 strcpy(response, "FAILInternal error!"); 611 strcpy(response, "FAILInternal error!");
612 } else { 612 } else {
613 printf("Set rsa attestation key successfully!\n"); 613 printf("Set rsa attestation key successfully!\n");
614 strcpy(response, "OKAY"); 614 strcpy(response, "OKAY");
615 } 615 }
616 } else if (endswith(cmd, FASTBOOT_SET_EC_ATTESTATION_KEY)) { 616 } else if (endswith(cmd, FASTBOOT_SET_EC_ATTESTATION_KEY)) {
617 if (trusty_set_attestation_key(fastboot_buf_addr, 617 if (trusty_set_attestation_key(fastboot_buf_addr,
618 fastboot_bytes_received, 618 fastboot_bytes_received,
619 KM_ALGORITHM_EC)) { 619 KM_ALGORITHM_EC)) {
620 printf("ERROR set ec attestation key failed!\n"); 620 printf("ERROR set ec attestation key failed!\n");
621 strcpy(response, "FAILInternal error!"); 621 strcpy(response, "FAILInternal error!");
622 } else { 622 } else {
623 printf("Set ec attestation key successfully!\n"); 623 printf("Set ec attestation key successfully!\n");
624 strcpy(response, "OKAY"); 624 strcpy(response, "OKAY");
625 } 625 }
626 } else if (endswith(cmd, FASTBOOT_APPEND_RSA_ATTESTATION_CERT)) { 626 } else if (endswith(cmd, FASTBOOT_APPEND_RSA_ATTESTATION_CERT)) {
627 if (trusty_append_attestation_cert_chain(fastboot_buf_addr, 627 if (trusty_append_attestation_cert_chain(fastboot_buf_addr,
628 fastboot_bytes_received, 628 fastboot_bytes_received,
629 KM_ALGORITHM_RSA)) { 629 KM_ALGORITHM_RSA)) {
630 printf("ERROR append rsa attestation cert chain failed!\n"); 630 printf("ERROR append rsa attestation cert chain failed!\n");
631 strcpy(response, "FAILInternal error!"); 631 strcpy(response, "FAILInternal error!");
632 } else { 632 } else {
633 printf("Append rsa attestation key successfully!\n"); 633 printf("Append rsa attestation key successfully!\n");
634 strcpy(response, "OKAY"); 634 strcpy(response, "OKAY");
635 } 635 }
636 } else if (endswith(cmd, FASTBOOT_APPEND_EC_ATTESTATION_CERT)) { 636 } else if (endswith(cmd, FASTBOOT_APPEND_EC_ATTESTATION_CERT)) {
637 if (trusty_append_attestation_cert_chain(fastboot_buf_addr, 637 if (trusty_append_attestation_cert_chain(fastboot_buf_addr,
638 fastboot_bytes_received, 638 fastboot_bytes_received,
639 KM_ALGORITHM_EC)) { 639 KM_ALGORITHM_EC)) {
640 printf("ERROR append ec attestation cert chain failed!\n"); 640 printf("ERROR append ec attestation cert chain failed!\n");
641 strcpy(response, "FAILInternal error!"); 641 strcpy(response, "FAILInternal error!");
642 } else { 642 } else {
643 printf("Append ec attestation key successfully!\n"); 643 printf("Append ec attestation key successfully!\n");
644 strcpy(response, "OKAY"); 644 strcpy(response, "OKAY");
645 } 645 }
646 } else if (endswith(cmd, FASTBOOT_GET_MPPUBK)) { 646 } else if (endswith(cmd, FASTBOOT_GET_MPPUBK)) {
647 if (fastboot_get_mppubk(fastboot_buf_addr, &fastboot_bytes_received)) { 647 if (fastboot_get_mppubk(fastboot_buf_addr, &fastboot_bytes_received)) {
648 printf("ERROR Generate mppubk failed!\n"); 648 printf("ERROR Generate mppubk failed!\n");
649 strcpy(response, "FAILGenerate mppubk failed!"); 649 strcpy(response, "FAILGenerate mppubk failed!");
650 } else { 650 } else {
651 printf("mppubk generated!\n"); 651 printf("mppubk generated!\n");
652 strcpy(response, "OKAY"); 652 strcpy(response, "OKAY");
653 } 653 }
654 } else if (endswith(cmd, FASTBOOT_GET_SERIAL_NUMBER)) { 654 } else if (endswith(cmd, FASTBOOT_GET_SERIAL_NUMBER)) {
655 char *serial = get_serial(); 655 char *serial = get_serial();
656 656
657 if (!serial) 657 if (!serial)
658 strcpy(response, "FAILSerial number not support!"); 658 strcpy(response, "FAILSerial number not support!");
659 else { 659 else {
660 /* Serial number will not exceed 16 bytes.*/ 660 /* Serial number will not exceed 16 bytes.*/
661 strncpy(fastboot_buf_addr, serial, 16); 661 strncpy(fastboot_buf_addr, serial, 16);
662 fastboot_bytes_received = 16; 662 fastboot_bytes_received = 16;
663 printf("Serial number generated!\n"); 663 printf("Serial number generated!\n");
664 strcpy(response, "OKAY"); 664 strcpy(response, "OKAY");
665 } 665 }
666 } 666 }
667 #ifdef CONFIG_ID_ATTESTATION 667 #ifdef CONFIG_ID_ATTESTATION
668 else if (endswith(cmd, FASTBOOT_APPEND_ATTESTATION_ID)) { 668 else if (endswith(cmd, FASTBOOT_APPEND_ATTESTATION_ID)) {
669 if (trusty_append_attestation_id(ATTESTATION_ID_BRAND, strlen(ATTESTATION_ID_BRAND))) { 669 if (trusty_append_attestation_id(ATTESTATION_ID_BRAND, strlen(ATTESTATION_ID_BRAND))) {
670 printf("Error append ATTESTATION_ID_BRAND failed!\n"); 670 printf("Error append ATTESTATION_ID_BRAND failed!\n");
671 strcpy(response, "FAILAppend ATTESTATION_ID_BRAND failed!"); 671 strcpy(response, "FAILAppend ATTESTATION_ID_BRAND failed!");
672 } else if (trusty_append_attestation_id(ATTESTATION_ID_DEVICE, strlen(ATTESTATION_ID_DEVICE))) { 672 } else if (trusty_append_attestation_id(ATTESTATION_ID_DEVICE, strlen(ATTESTATION_ID_DEVICE))) {
673 printf("Error append ATTESTATION_ID_DEVICE failed!\n"); 673 printf("Error append ATTESTATION_ID_DEVICE failed!\n");
674 strcpy(response, "FAILAppend ATTESTATION_ID_DEVICE failed!"); 674 strcpy(response, "FAILAppend ATTESTATION_ID_DEVICE failed!");
675 } else if (trusty_append_attestation_id(CONFIG_ATTESTATION_ID_PRODUCT, strlen(CONFIG_ATTESTATION_ID_PRODUCT))) { 675 } else if (trusty_append_attestation_id(CONFIG_ATTESTATION_ID_PRODUCT, strlen(CONFIG_ATTESTATION_ID_PRODUCT))) {
676 printf("Error append ATTESTATION_ID_PRODUCT failed!\n"); 676 printf("Error append ATTESTATION_ID_PRODUCT failed!\n");
677 strcpy(response, "FAILAppend ATTESTATION_ID_PRODUCT failed!"); 677 strcpy(response, "FAILAppend ATTESTATION_ID_PRODUCT failed!");
678 } else if (trusty_append_attestation_id(ATTESTATION_ID_MANUFACTURER, strlen(ATTESTATION_ID_MANUFACTURER))) { 678 } else if (trusty_append_attestation_id(ATTESTATION_ID_MANUFACTURER, strlen(ATTESTATION_ID_MANUFACTURER))) {
679 printf("Error append ATTESTATION_ID_MANUFACTURER failed!\n"); 679 printf("Error append ATTESTATION_ID_MANUFACTURER failed!\n");
680 strcpy(response, "FAILAppend ATTESTATION_ID_MANUFACTURER failed!"); 680 strcpy(response, "FAILAppend ATTESTATION_ID_MANUFACTURER failed!");
681 } else if (trusty_append_attestation_id(ATTESTATION_ID_MODEL, strlen(ATTESTATION_ID_MODEL))) { 681 } else if (trusty_append_attestation_id(ATTESTATION_ID_MODEL, strlen(ATTESTATION_ID_MODEL))) {
682 printf("Error append ATTESTATION_ID_MODEL failed!\n"); 682 printf("Error append ATTESTATION_ID_MODEL failed!\n");
683 strcpy(response, "FAILAppend ATTESTATION_ID_MODEL failed!"); 683 strcpy(response, "FAILAppend ATTESTATION_ID_MODEL failed!");
684 } else { 684 } else {
685 char *serial = get_serial(); 685 char *serial = get_serial();
686 686
687 if (!serial) { 687 if (!serial) {
688 printf("Error Failed to append the serial number!\n"); 688 printf("Error Failed to append the serial number!\n");
689 strcpy(response, "FAIL Failed to append the serial number!"); 689 strcpy(response, "FAIL Failed to append the serial number!");
690 } else if (trusty_append_attestation_id(serial, 16)) { 690 } else if (trusty_append_attestation_id(serial, 16)) {
691 printf("Error Failed to append the serial number!\n"); 691 printf("Error Failed to append the serial number!\n");
692 strcpy(response, "FAILFailed to append the serial number!"); 692 strcpy(response, "FAILFailed to append the serial number!");
693 } else 693 } else
694 strcpy(response, "OKAY"); 694 strcpy(response, "OKAY");
695 } 695 }
696 } 696 }
697 #endif 697 #endif
698 #ifndef CONFIG_AVB_ATX 698 #ifndef CONFIG_AVB_ATX
699 else if (endswith(cmd, FASTBOOT_SET_RPMB_KEY)) { 699 else if (endswith(cmd, FASTBOOT_SET_RPMB_KEY)) {
700 if (fastboot_set_rpmb_key(fastboot_buf_addr, fastboot_bytes_received)) { 700 if (fastboot_set_rpmb_key(fastboot_buf_addr, fastboot_bytes_received)) {
701 printf("ERROR set rpmb key failed!\n"); 701 printf("ERROR set rpmb key failed!\n");
702 strcpy(response, "FAILset rpmb key failed!"); 702 strcpy(response, "FAILset rpmb key failed!");
703 } else 703 } else
704 strcpy(response, "OKAY"); 704 strcpy(response, "OKAY");
705 } else if (endswith(cmd, FASTBOOT_SET_RPMB_RANDOM_KEY)) { 705 } else if (endswith(cmd, FASTBOOT_SET_RPMB_RANDOM_KEY)) {
706 if (fastboot_set_rpmb_random_key()) { 706 if (fastboot_set_rpmb_random_key()) {
707 printf("ERROR set rpmb random key failed!\n"); 707 printf("ERROR set rpmb random key failed!\n");
708 strcpy(response, "FAILset rpmb random key failed!"); 708 strcpy(response, "FAILset rpmb random key failed!");
709 } else 709 } else
710 strcpy(response, "OKAY"); 710 strcpy(response, "OKAY");
711 } else if (endswith(cmd, FASTBOOT_SET_VBMETA_PUBLIC_KEY)) { 711 } else if (endswith(cmd, FASTBOOT_SET_VBMETA_PUBLIC_KEY)) {
712 if (avb_set_public_key(fastboot_buf_addr, 712 if (avb_set_public_key(fastboot_buf_addr,
713 fastboot_bytes_received)) 713 fastboot_bytes_received))
714 strcpy(response, "FAILcan't set public key!"); 714 strcpy(response, "FAILcan't set public key!");
715 else 715 else
716 strcpy(response, "OKAY"); 716 strcpy(response, "OKAY");
717 } 717 }
718 #endif /* !CONFIG_AVB_ATX */ 718 #endif /* !CONFIG_AVB_ATX */
719 #endif /* CONFIG_IMX_TRUSTY_OS */ 719 #endif /* CONFIG_IMX_TRUSTY_OS */
720 else if (endswith(cmd, "unlock_critical")) { 720 else if (endswith(cmd, "unlock_critical")) {
721 strcpy(response, "OKAY"); 721 strcpy(response, "OKAY");
722 } else if (endswith(cmd, "unlock")) { 722 } else if (endswith(cmd, "unlock")) {
723 printf("flashing unlock.\n"); 723 printf("flashing unlock.\n");
724 #ifdef CONFIG_AVB_ATX 724 #ifdef CONFIG_AVB_ATX
725 /* We should do nothing here For Android Things which 725 /* We should do nothing here For Android Things which
726 * enables the authenticated unlock feature. 726 * enables the authenticated unlock feature.
727 */ 727 */
728 strcpy(response, "OKAY"); 728 strcpy(response, "OKAY");
729 #else 729 #else
730 status = do_fastboot_unlock(false); 730 status = do_fastboot_unlock(false);
731 if (status != FASTBOOT_LOCK_ERROR) 731 if (status != FASTBOOT_LOCK_ERROR)
732 strcpy(response, "OKAY"); 732 strcpy(response, "OKAY");
733 else 733 else
734 strcpy(response, "FAILunlock device failed."); 734 strcpy(response, "FAILunlock device failed.");
735 #endif 735 #endif
736 } else if (endswith(cmd, "lock")) { 736 } else if (endswith(cmd, "lock")) {
737 #ifdef CONFIG_AVB_ATX 737 #ifdef CONFIG_AVB_ATX
738 /* We should do nothing here For Android Things which 738 /* We should do nothing here For Android Things which
739 * enables the at-lock-vboot feature. 739 * enables the at-lock-vboot feature.
740 */ 740 */
741 strcpy(response, "OKAY"); 741 strcpy(response, "OKAY");
742 #else 742 #else
743 printf("flashing lock.\n"); 743 printf("flashing lock.\n");
744 status = do_fastboot_lock(); 744 status = do_fastboot_lock();
745 if (status != FASTBOOT_LOCK_ERROR) 745 if (status != FASTBOOT_LOCK_ERROR)
746 strcpy(response, "OKAY"); 746 strcpy(response, "OKAY");
747 else 747 else
748 strcpy(response, "FAILlock device failed."); 748 strcpy(response, "FAILlock device failed.");
749 #endif 749 #endif
750 } else if (endswith(cmd, "get_unlock_ability")) { 750 } else if (endswith(cmd, "get_unlock_ability")) {
751 result = fastboot_lock_enable(); 751 result = fastboot_lock_enable();
752 if (result == FASTBOOT_UL_ENABLE) { 752 if (result == FASTBOOT_UL_ENABLE) {
753 fastboot_tx_write_more("INFO1"); 753 fastboot_tx_write_more("INFO1");
754 strcpy(response, "OKAY"); 754 strcpy(response, "OKAY");
755 } else if (result == FASTBOOT_UL_DISABLE) { 755 } else if (result == FASTBOOT_UL_DISABLE) {
756 fastboot_tx_write_more("INFO0"); 756 fastboot_tx_write_more("INFO0");
757 strcpy(response, "OKAY"); 757 strcpy(response, "OKAY");
758 } else { 758 } else {
759 printf("flashing get_unlock_ability fail!\n"); 759 printf("flashing get_unlock_ability fail!\n");
760 strcpy(response, "FAILget unlock ability failed."); 760 strcpy(response, "FAILget unlock ability failed.");
761 } 761 }
762 } else { 762 } else {
763 printf("Unknown flashing command:%s\n", cmd); 763 printf("Unknown flashing command:%s\n", cmd);
764 strcpy(response, "FAILcommand not defined"); 764 strcpy(response, "FAILcommand not defined");
765 } 765 }
766 fastboot_tx_write_more(response); 766 fastboot_tx_write_more(response);
767 767
768 /* Must call fastboot_none_resp before returning from the dispatch function 768 /* Must call fastboot_none_resp before returning from the dispatch function
769 * which uses fastboot_tx_write_more 769 * which uses fastboot_tx_write_more
770 */ 770 */
771 fastboot_none_resp(response); 771 fastboot_none_resp(response);
772 } 772 }
773 #endif /* CONFIG_FASTBOOT_LOCK */ 773 #endif /* CONFIG_FASTBOOT_LOCK */
774 774
775 #ifdef CONFIG_AVB_SUPPORT 775 #ifdef CONFIG_AVB_SUPPORT
776 static void set_active_avb(char *cmd, char *response) 776 static void set_active_avb(char *cmd, char *response)
777 { 777 {
778 AvbIOResult ret; 778 AvbIOResult ret;
779 int slot = 0; 779 int slot = 0;
780 780
781 if (!cmd) { 781 if (!cmd) {
782 pr_err("missing slot suffix\n"); 782 pr_err("missing slot suffix\n");
783 fastboot_fail("missing slot suffix", response); 783 fastboot_fail("missing slot suffix", response);
784 return; 784 return;
785 } 785 }
786 786
787 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 787 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
788 if (virtual_ab_update_is_merging()) { 788 if (virtual_ab_update_is_merging()) {
789 printf("Can not switch slot while snapshot merge is in progress!\n"); 789 printf("Can not switch slot while snapshot merge is in progress!\n");
790 fastboot_fail("Snapshot merge is in progress!", response); 790 fastboot_fail("Snapshot merge is in progress!", response);
791 return; 791 return;
792 } 792 }
793 793
794 /* Only output a warning when the image is snapshoted. */ 794 /* Only output a warning when the image is snapshoted. */
795 if (virtual_ab_update_is_snapshoted()) 795 if (virtual_ab_update_is_snapshoted())
796 printf("Warning: changing the active slot with a snapshot applied may cancel the update!\n"); 796 printf("Warning: changing the active slot with a snapshot applied may cancel the update!\n");
797 else 797 else
798 printf("Warning: Virtual A/B is enabled, switch slot may make the system fail to boot. \n"); 798 printf("Warning: Virtual A/B is enabled, switch slot may make the system fail to boot. \n");
799 #endif 799 #endif
800 800
801 slot = slotidx_from_suffix(cmd); 801 slot = slotidx_from_suffix(cmd);
802 802
803 if (slot < 0) { 803 if (slot < 0) {
804 fastboot_fail("err slot suffix", response); 804 fastboot_fail("err slot suffix", response);
805 return; 805 return;
806 } 806 }
807 807
808 ret = fsl_avb_ab_mark_slot_active(&fsl_avb_ab_ops, slot); 808 ret = fsl_avb_ab_mark_slot_active(&fsl_avb_ab_ops, slot);
809 if (ret != AVB_IO_RESULT_OK) 809 if (ret != AVB_IO_RESULT_OK)
810 fastboot_fail("avb IO error", response); 810 fastboot_fail("avb IO error", response);
811 else 811 else
812 fastboot_okay(NULL, response); 812 fastboot_okay(NULL, response);
813 813
814 return; 814 return;
815 } 815 }
816 #endif /*CONFIG_AVB_SUPPORT*/ 816 #endif /*CONFIG_AVB_SUPPORT*/
817 817
818 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH) 818 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
819 static void flash(char *cmd, char *response) 819 static void flash(char *cmd, char *response)
820 { 820 {
821 if (!cmd) { 821 if (!cmd) {
822 pr_err("missing partition name"); 822 pr_err("missing partition name");
823 fastboot_fail("missing partition name", response); 823 fastboot_fail("missing partition name", response);
824 return; 824 return;
825 } 825 }
826 826
827 /* Always enable image flash for Android Things. */ 827 /* Always enable image flash for Android Things. */
828 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX) 828 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX)
829 int status; 829 int status;
830 status = fastboot_get_lock_stat(); 830 status = fastboot_get_lock_stat();
831 831
832 if (status == FASTBOOT_LOCK) { 832 if (status == FASTBOOT_LOCK) {
833 pr_err("device is LOCKed!\n"); 833 pr_err("device is LOCKed!\n");
834 fastboot_fail("device is locked.", response); 834 fastboot_fail("device is locked.", response);
835 return; 835 return;
836 836
837 } else if (status == FASTBOOT_LOCK_ERROR) { 837 } else if (status == FASTBOOT_LOCK_ERROR) {
838 pr_err("write lock status into device!\n"); 838 pr_err("write lock status into device!\n");
839 fastboot_set_lock_stat(FASTBOOT_LOCK); 839 fastboot_set_lock_stat(FASTBOOT_LOCK);
840 fastboot_fail("device is locked.", response); 840 fastboot_fail("device is locked.", response);
841 return; 841 return;
842 } 842 }
843 #endif 843 #endif
844 844
845 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 845 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
846 if (partition_is_protected_during_merge(cmd)) { 846 if (partition_is_protected_during_merge(cmd)) {
847 printf("Can not flash partition %s while a snapshot update is in progress!\n", cmd); 847 printf("Can not flash partition %s while a snapshot update is in progress!\n", cmd);
848 fastboot_fail("Snapshot update is in progress", response); 848 fastboot_fail("Snapshot update is in progress", response);
849 return; 849 return;
850 } 850 }
851 #endif 851 #endif
852 852
853 fastboot_process_flash(cmd, fastboot_buf_addr, 853 fastboot_process_flash(cmd, fastboot_buf_addr,
854 fastboot_bytes_received, response); 854 fastboot_bytes_received, response);
855 855
856 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 856 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
857 /* Cancel virtual AB update after image flash */ 857 /* Cancel virtual AB update after image flash */
858 if (virtual_ab_update_is_merging() || virtual_ab_update_is_snapshoted()) 858 if (virtual_ab_update_is_merging() || virtual_ab_update_is_snapshoted())
859 virtual_ab_cancel_update(); 859 virtual_ab_cancel_update();
860 #endif 860 #endif
861 861
862 #if defined(CONFIG_FASTBOOT_LOCK) 862 #if defined(CONFIG_FASTBOOT_LOCK)
863 if (strncmp(cmd, "gpt", 3) == 0) { 863 if (strncmp(cmd, "gpt", 3) == 0) {
864 int gpt_valid = 0; 864 int gpt_valid = 0;
865 int status, mmc_no; 865 int mmc_no;
866 struct blk_desc *dev_desc; 866 struct blk_desc *dev_desc;
867 mmc_no = fastboot_devinfo.dev_id; 867 mmc_no = fastboot_devinfo.dev_id;
868 dev_desc = blk_get_dev("mmc", mmc_no); 868 dev_desc = blk_get_dev("mmc", mmc_no);
869 if (dev_desc) { 869 if (dev_desc) {
870 if (dev_desc->part_type != PART_TYPE_EFI) 870 if (dev_desc->part_type != PART_TYPE_EFI)
871 dev_desc->part_type = PART_TYPE_EFI; 871 dev_desc->part_type = PART_TYPE_EFI;
872 } 872 }
873 else { 873 else {
874 fastboot_fail("", response); 874 fastboot_fail("", response);
875 return; 875 return;
876 } 876 }
877 gpt_valid = partition_table_valid(); 877 gpt_valid = partition_table_valid();
878 /* If gpt is valid, load partitons table into memory. 878 /* If gpt is valid, load partitons table into memory.
879 So if the next command is "fastboot reboot bootloader", 879 So if the next command is "fastboot reboot bootloader",
880 it can find the "misc" partition to r/w. */ 880 it can find the "misc" partition to r/w. */
881 if(gpt_valid) { 881 if(gpt_valid) {
882 fastboot_load_partitions(); 882 fastboot_load_partitions();
883 /* Unlock device if the gpt is valid */ 883 /* Unlock device if the gpt is valid */
884 do_fastboot_unlock(true); 884 do_fastboot_unlock(true);
885 } 885 }
886 } 886 }
887 887
888 #endif 888 #endif
889 } 889 }
890 890
891 static void erase(char *cmd, char *response) 891 static void erase(char *cmd, char *response)
892 { 892 {
893 if (!cmd) { 893 if (!cmd) {
894 pr_err("missing partition name"); 894 pr_err("missing partition name");
895 fastboot_fail("missing partition name", response); 895 fastboot_fail("missing partition name", response);
896 return; 896 return;
897 } 897 }
898 898
899 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX) 899 #if defined(CONFIG_FASTBOOT_LOCK) && !defined(CONFIG_AVB_ATX)
900 FbLockState status; 900 FbLockState status;
901 status = fastboot_get_lock_stat(); 901 status = fastboot_get_lock_stat();
902 if (status == FASTBOOT_LOCK) { 902 if (status == FASTBOOT_LOCK) {
903 pr_err("device is LOCKed!\n"); 903 pr_err("device is LOCKed!\n");
904 fastboot_fail("device is locked.", response); 904 fastboot_fail("device is locked.", response);
905 return; 905 return;
906 } else if (status == FASTBOOT_LOCK_ERROR) { 906 } else if (status == FASTBOOT_LOCK_ERROR) {
907 pr_err("write lock status into device!\n"); 907 pr_err("write lock status into device!\n");
908 fastboot_set_lock_stat(FASTBOOT_LOCK); 908 fastboot_set_lock_stat(FASTBOOT_LOCK);
909 fastboot_fail("device is locked.", response); 909 fastboot_fail("device is locked.", response);
910 return; 910 return;
911 } 911 }
912 #endif 912 #endif
913 913
914 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 914 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
915 if (partition_is_protected_during_merge(cmd)) { 915 if (partition_is_protected_during_merge(cmd)) {
916 printf("Can not erase partition %s while a snapshot update is in progress!", cmd); 916 printf("Can not erase partition %s while a snapshot update is in progress!", cmd);
917 fastboot_fail("Snapshot update is in progress", response); 917 fastboot_fail("Snapshot update is in progress", response);
918 return; 918 return;
919 } 919 }
920 #endif 920 #endif
921 921
922 fastboot_process_erase(cmd, response); 922 fastboot_process_erase(cmd, response);
923 } 923 }
924 #endif 924 #endif
925 925
926 /** 926 /**
927 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader 927 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
928 * 928 *
929 * This is a redefinition, since BSP dose not need the function of 929 * This is a redefinition, since BSP dose not need the function of
930 * "reboot into bootloader", and with BCB support, the flag can be 930 * "reboot into bootloader", and with BCB support, the flag can be
931 * set with another way. Redefine this function to override the weak 931 * set with another way. Redefine this function to override the weak
932 * definition to avoid error return value. 932 * definition to avoid error return value.
933 */ 933 */
934 int fastboot_set_reboot_flag(void) 934 int fastboot_set_reboot_flag(void)
935 { 935 {
936 return 0; 936 return 0;
937 } 937 }
938 938
939 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) 939 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
940 /** 940 /**
941 * run_ucmd() - Execute the UCmd command 941 * run_ucmd() - Execute the UCmd command
942 * 942 *
943 * @cmd_parameter: Pointer to command parameter 943 * @cmd_parameter: Pointer to command parameter
944 * @response: Pointer to fastboot response buffer 944 * @response: Pointer to fastboot response buffer
945 */ 945 */
946 static void run_ucmd(char *cmd_parameter, char *response) 946 static void run_ucmd(char *cmd_parameter, char *response)
947 { 947 {
948 if (!cmd_parameter) { 948 if (!cmd_parameter) {
949 pr_err("missing slot suffix\n"); 949 pr_err("missing slot suffix\n");
950 fastboot_fail("missing command", response); 950 fastboot_fail("missing command", response);
951 return; 951 return;
952 } 952 }
953 if(run_command(cmd_parameter, 0)) { 953 if(run_command(cmd_parameter, 0)) {
954 fastboot_fail("", response); 954 fastboot_fail("", response);
955 } else { 955 } else {
956 fastboot_okay(NULL, response); 956 fastboot_okay(NULL, response);
957 /* cmd may impact fastboot related environment*/ 957 /* cmd may impact fastboot related environment*/
958 fastboot_setup(); 958 fastboot_setup();
959 } 959 }
960 } 960 }
961 961
962 static char g_a_cmd_buff[64]; 962 static char g_a_cmd_buff[64];
963 963
964 void fastboot_acmd_complete(void) 964 void fastboot_acmd_complete(void)
965 { 965 {
966 run_command(g_a_cmd_buff, 0); 966 run_command(g_a_cmd_buff, 0);
967 } 967 }
968 968
969 /** 969 /**
970 * run_acmd() - Execute the ACmd command 970 * run_acmd() - Execute the ACmd command
971 * 971 *
972 * @cmd_parameter: Pointer to command parameter 972 * @cmd_parameter: Pointer to command parameter
973 * @response: Pointer to fastboot response buffer 973 * @response: Pointer to fastboot response buffer
974 */ 974 */
975 static void run_acmd(char *cmd_parameter, char *response) 975 static void run_acmd(char *cmd_parameter, char *response)
976 { 976 {
977 if (!cmd_parameter) { 977 if (!cmd_parameter) {
978 pr_err("missing slot suffix\n"); 978 pr_err("missing slot suffix\n");
979 fastboot_fail("missing command", response); 979 fastboot_fail("missing command", response);
980 return; 980 return;
981 } 981 }
982 982
983 if (strlen(cmd_parameter) >= sizeof(g_a_cmd_buff)) { 983 if (strlen(cmd_parameter) >= sizeof(g_a_cmd_buff)) {
984 pr_err("input acmd is too long\n"); 984 pr_err("input acmd is too long\n");
985 fastboot_fail("too long command", response); 985 fastboot_fail("too long command", response);
986 return; 986 return;
987 } 987 }
988 988
989 strcpy(g_a_cmd_buff, cmd_parameter); 989 strcpy(g_a_cmd_buff, cmd_parameter);
990 fastboot_okay(NULL, response); 990 fastboot_okay(NULL, response);
991 } 991 }
992 #endif 992 #endif
993 993
994 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 994 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
995 static void snapshot_update(char *cmd_parameter, char *response) 995 static void snapshot_update(char *cmd_parameter, char *response)
996 { 996 {
997 if (endswith(cmd_parameter, "cancel")) { 997 if (endswith(cmd_parameter, "cancel")) {
998 FbLockState status; 998 FbLockState status;
999 status = fastboot_get_lock_stat(); 999 status = fastboot_get_lock_stat();
1000 if ((status == FASTBOOT_LOCK) || (status == FASTBOOT_LOCK_ERROR)) { 1000 if ((status == FASTBOOT_LOCK) || (status == FASTBOOT_LOCK_ERROR)) {
1001 printf("Can not cancel snapshot update when the device is locked!\n"); 1001 printf("Can not cancel snapshot update when the device is locked!\n");
1002 fastboot_fail("device is locked!", response); 1002 fastboot_fail("device is locked!", response);
1003 } else if (virtual_ab_update_is_merging() || virtual_ab_update_is_snapshoted()) { 1003 } else if (virtual_ab_update_is_merging() || virtual_ab_update_is_snapshoted()) {
1004 if (virtual_ab_cancel_update() != -1) 1004 if (virtual_ab_cancel_update() != -1)
1005 fastboot_okay(NULL, response); 1005 fastboot_okay(NULL, response);
1006 else 1006 else
1007 fastboot_fail("Can't cancel snapshot update!", response); 1007 fastboot_fail("Can't cancel snapshot update!", response);
1008 } else { 1008 } else {
1009 printf("Device is not in 'merging' or 'snapshotted' state, do nothing...\n"); 1009 printf("Device is not in 'merging' or 'snapshotted' state, do nothing...\n");
1010 fastboot_okay(NULL, response); 1010 fastboot_okay(NULL, response);
1011 } 1011 }
1012 1012
1013 return; 1013 return;
1014 } else { 1014 } else {
1015 printf("Error! Only 'cancel' is supported!"); 1015 printf("Error! Only 'cancel' is supported!");
1016 strcpy(response, "FAILInternal error!"); 1016 strcpy(response, "FAILInternal error!");
1017 } 1017 }
1018 1018
1019 return; 1019 return;
1020 } 1020 }
1021 #endif 1021 #endif
1022 1022
1023 static const struct { 1023 static const struct {
1024 const char *command; 1024 const char *command;
1025 void (*dispatch)(char *cmd_parameter, char *response); 1025 void (*dispatch)(char *cmd_parameter, char *response);
1026 } commands[FASTBOOT_COMMAND_COUNT] = { 1026 } commands[FASTBOOT_COMMAND_COUNT] = {
1027 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = { 1027 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
1028 .command = "reboot-bootloader", 1028 .command = "reboot-bootloader",
1029 .dispatch = reboot_bootloader, 1029 .dispatch = reboot_bootloader,
1030 }, 1030 },
1031 [FASTBOOT_COMMAND_UPLOAD] = { 1031 [FASTBOOT_COMMAND_UPLOAD] = {
1032 .command = "upload", 1032 .command = "upload",
1033 .dispatch = upload, 1033 .dispatch = upload,
1034 }, 1034 },
1035 [FASTBOOT_COMMAND_GETSTAGED] = { 1035 [FASTBOOT_COMMAND_GETSTAGED] = {
1036 .command = "get_staged", 1036 .command = "get_staged",
1037 .dispatch = upload, 1037 .dispatch = upload,
1038 }, 1038 },
1039 #if defined(CONFIG_FASTBOOT_LOCK) 1039 #if defined(CONFIG_FASTBOOT_LOCK)
1040 [FASTBOOT_COMMAND_FLASHING] = { 1040 [FASTBOOT_COMMAND_FLASHING] = {
1041 .command = "flashing", 1041 .command = "flashing",
1042 .dispatch = flashing, 1042 .dispatch = flashing,
1043 }, 1043 },
1044 [FASTBOOT_COMMAND_OEM] = { 1044 [FASTBOOT_COMMAND_OEM] = {
1045 .command = "oem", 1045 .command = "oem",
1046 .dispatch = flashing, 1046 .dispatch = flashing,
1047 }, 1047 },
1048 #endif 1048 #endif
1049 #ifdef CONFIG_AVB_SUPPORT 1049 #ifdef CONFIG_AVB_SUPPORT
1050 [FASTBOOT_COMMAND_SETACTIVE] = { 1050 [FASTBOOT_COMMAND_SETACTIVE] = {
1051 .command = "set_active", 1051 .command = "set_active",
1052 .dispatch = set_active_avb, 1052 .dispatch = set_active_avb,
1053 }, 1053 },
1054 #endif 1054 #endif
1055 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) 1055 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
1056 [FASTBOOT_COMMAND_UCMD] = { 1056 [FASTBOOT_COMMAND_UCMD] = {
1057 .command = "UCmd", 1057 .command = "UCmd",
1058 .dispatch = run_ucmd, 1058 .dispatch = run_ucmd,
1059 }, 1059 },
1060 [FASTBOOT_COMMAND_ACMD] = { 1060 [FASTBOOT_COMMAND_ACMD] = {
1061 .command ="ACmd", 1061 .command ="ACmd",
1062 .dispatch = run_acmd, 1062 .dispatch = run_acmd,
1063 }, 1063 },
1064 #endif 1064 #endif
1065 [FASTBOOT_COMMAND_REBOOT] = { 1065 [FASTBOOT_COMMAND_REBOOT] = {
1066 .command = "reboot", 1066 .command = "reboot",
1067 .dispatch = okay, 1067 .dispatch = okay,
1068 }, 1068 },
1069 [FASTBOOT_COMMAND_GETVAR] = { 1069 [FASTBOOT_COMMAND_GETVAR] = {
1070 .command = "getvar", 1070 .command = "getvar",
1071 .dispatch = getvar, 1071 .dispatch = getvar,
1072 }, 1072 },
1073 [FASTBOOT_COMMAND_DOWNLOAD] = { 1073 [FASTBOOT_COMMAND_DOWNLOAD] = {
1074 .command = "download", 1074 .command = "download",
1075 .dispatch = download, 1075 .dispatch = download,
1076 }, 1076 },
1077 [FASTBOOT_COMMAND_BOOT] = { 1077 [FASTBOOT_COMMAND_BOOT] = {
1078 .command = "boot", 1078 .command = "boot",
1079 .dispatch = okay, 1079 .dispatch = okay,
1080 }, 1080 },
1081 [FASTBOOT_COMMAND_CONTINUE] = { 1081 [FASTBOOT_COMMAND_CONTINUE] = {
1082 .command = "continue", 1082 .command = "continue",
1083 .dispatch = okay, 1083 .dispatch = okay,
1084 }, 1084 },
1085 #ifdef CONFIG_FASTBOOT_FLASH 1085 #ifdef CONFIG_FASTBOOT_FLASH
1086 [FASTBOOT_COMMAND_FLASH] = { 1086 [FASTBOOT_COMMAND_FLASH] = {
1087 .command = "flash", 1087 .command = "flash",
1088 .dispatch = flash, 1088 .dispatch = flash,
1089 }, 1089 },
1090 [FASTBOOT_COMMAND_ERASE] = { 1090 [FASTBOOT_COMMAND_ERASE] = {
1091 .command = "erase", 1091 .command = "erase",
1092 .dispatch = erase, 1092 .dispatch = erase,
1093 }, 1093 },
1094 #endif 1094 #endif
1095 #ifdef CONFIG_AVB_ATX 1095 #ifdef CONFIG_AVB_ATX
1096 [FASTBOOT_COMMAND_STAGE] = { 1096 [FASTBOOT_COMMAND_STAGE] = {
1097 .command = "stage", 1097 .command = "stage",
1098 .dispatch = download, 1098 .dispatch = download,
1099 }, 1099 },
1100 #endif 1100 #endif
1101 #ifdef CONFIG_ANDROID_RECOVERY 1101 #ifdef CONFIG_ANDROID_RECOVERY
1102 [FASTBOOT_COMMAND_RECOVERY_FASTBOOT] = { 1102 [FASTBOOT_COMMAND_RECOVERY_FASTBOOT] = {
1103 .command = "reboot-fastboot", 1103 .command = "reboot-fastboot",
1104 .dispatch = reboot_fastboot, 1104 .dispatch = reboot_fastboot,
1105 }, 1105 },
1106 #endif 1106 #endif
1107 #ifdef CONFIG_VIRTUAL_AB_SUPPORT 1107 #ifdef CONFIG_VIRTUAL_AB_SUPPORT
1108 [FASTBOOT_COMMAND_SNAPSHOT_UPDATE] = { 1108 [FASTBOOT_COMMAND_SNAPSHOT_UPDATE] = {
1109 .command = "snapshot-update", 1109 .command = "snapshot-update",
1110 .dispatch = snapshot_update, 1110 .dispatch = snapshot_update,
1111 }, 1111 },
1112 #endif 1112 #endif
1113 }; 1113 };
1114 1114
1115 /** 1115 /**
1116 * fastboot_handle_command - Handle fastboot command 1116 * fastboot_handle_command - Handle fastboot command
1117 * 1117 *
1118 * @cmd_string: Pointer to command string 1118 * @cmd_string: Pointer to command string
1119 * @response: Pointer to fastboot response buffer 1119 * @response: Pointer to fastboot response buffer
1120 * 1120 *
1121 * Return: Executed command, or -1 if not recognized 1121 * Return: Executed command, or -1 if not recognized
1122 */ 1122 */
1123 int fastboot_handle_command(char *cmd_string, char *response) 1123 int fastboot_handle_command(char *cmd_string, char *response)
1124 { 1124 {
1125 int i; 1125 int i;
1126 char *cmd_parameter; 1126 char *cmd_parameter;
1127 1127
1128 cmd_parameter = cmd_string; 1128 cmd_parameter = cmd_string;
1129 strsep(&cmd_parameter, ":"); 1129 strsep(&cmd_parameter, ":");
1130 /* separate cmdstring for "fastboot oem/flashing" with a blank */ 1130 /* separate cmdstring for "fastboot oem/flashing" with a blank */
1131 if(cmd_parameter == NULL) 1131 if(cmd_parameter == NULL)
1132 { 1132 {
1133 cmd_parameter = cmd_string; 1133 cmd_parameter = cmd_string;
1134 strsep(&cmd_parameter, " "); 1134 strsep(&cmd_parameter, " ");
1135 } 1135 }
1136 1136
1137 for (i = 0; i < ARRAY_SIZE(commands); i++) { 1137 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1138 if (commands[i].command != NULL && 1138 if (commands[i].command != NULL &&
1139 !strcmp(commands[i].command, cmd_string)) { 1139 !strcmp(commands[i].command, cmd_string)) {
1140 if (commands[i].dispatch) { 1140 if (commands[i].dispatch) {
1141 commands[i].dispatch(cmd_parameter, 1141 commands[i].dispatch(cmd_parameter,
1142 response); 1142 response);
1143 return i; 1143 return i;
1144 } else { 1144 } else {
1145 break; 1145 break;
1146 } 1146 }
1147 } 1147 }
1148 } 1148 }
1149 1149
1150 pr_err("command %s not recognized.\n", cmd_string); 1150 pr_err("command %s not recognized.\n", cmd_string);
1151 fastboot_fail("unrecognized command", response); 1151 fastboot_fail("unrecognized command", response);
1152 return -1; 1152 return -1;
1153 } 1153 }
1154 1154