blob: edfc0cdf4d11ce4e2379669243e2ac86aefd009e [file] [log] [blame]
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2016 The Android Open Source Project
4 */
5
6#include <common.h>
Simon Glass288b29e2019-11-14 12:57:43 -07007#include <command.h>
Simon Glassc7694dd2019-08-01 09:46:46 -06008#include <env.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +00009#include <fastboot.h>
10#include <fastboot-internal.h>
11#include <fb_mmc.h>
12#include <fb_nand.h>
13#include <part.h>
14#include <stdlib.h>
Xindong Xud9441422024-01-18 10:20:45 +080015#ifdef CONFIG_AMLOGIC_MODIFY
16#include <amlogic/emmc_partitions.h>
17#include <amlogic/storage.h>
18#include <amlogic/aml_efuse.h>
19#include <amlogic/aml_mmc.h>
20
21#include <android_image.h>
22#include <image.h>
23#include <amlogic/store_wrapper.h>
24#include <malloc.h>
25#include <amlogic/image_check.h>
26#include <fs.h>
27#if defined(CONFIG_AML_ANTIROLLBACK) || defined(CONFIG_AML_AVB2_ANTIROLLBACK)
28#include <amlogic/anti-rollback.h>
29#endif
30#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000031
32/**
33 * image_size - final fastboot image size
34 */
35static u32 image_size;
36
37/**
38 * fastboot_bytes_received - number of bytes received in the current download
39 */
40static u32 fastboot_bytes_received;
41
42/**
43 * fastboot_bytes_expected - number of bytes expected in the current download
44 */
45static u32 fastboot_bytes_expected;
46
Xindong Xud9441422024-01-18 10:20:45 +080047#ifdef CONFIG_AMLOGIC_MODIFY
48int busy_flag;
49#endif
50
Alex Kiernanf73a7df2018-05-29 15:30:53 +000051static void okay(char *, char *);
52static void getvar(char *, char *);
Xindong Xud9441422024-01-18 10:20:45 +080053
54#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernanf73a7df2018-05-29 15:30:53 +000055static void download(char *, char *);
56#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
57static void flash(char *, char *);
58static void erase(char *, char *);
59#endif
Xindong Xud9441422024-01-18 10:20:45 +080060#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000061static void reboot_bootloader(char *, char *);
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +030062static void reboot_fastbootd(char *, char *);
63static void reboot_recovery(char *, char *);
Xindong Xud9441422024-01-18 10:20:45 +080064#ifdef CONFIG_FASTBOOT_WRITING_CMD
65#ifdef CONFIG_AMLOGIC_MODIFY
66static void fetch(char *, char *);
67#if !CONFIG_IS_ENABLED(NO_FASTBOOT_FLASHING)
68static void flashing(char *, char *);
69#endif
70#endif
Alex Kiernan3845b902018-05-29 15:30:54 +000071#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
72static void oem_format(char *, char *);
73#endif
Patrick Delaunayb2f6b972021-01-27 14:46:48 +010074#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
75static void oem_partconf(char *, char *);
76#endif
Patrick Delaunay0c0394b2021-01-27 14:46:49 +010077#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
78static void oem_bootbus(char *, char *);
79#endif
Xindong Xud9441422024-01-18 10:20:45 +080080static void set_active_cmd(char *, char *);
81#endif
82
83#ifdef CONFIG_AMLOGIC_MODIFY
84extern int is_partition_logical(char* partition_name);
85#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000086
Heiko Schocherbc820d52021-02-10 09:29:03 +010087#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
88static void run_ucmd(char *, char *);
89static void run_acmd(char *, char *);
90#endif
91
Alex Kiernanf73a7df2018-05-29 15:30:53 +000092static const struct {
93 const char *command;
94 void (*dispatch)(char *cmd_parameter, char *response);
95} commands[FASTBOOT_COMMAND_COUNT] = {
96 [FASTBOOT_COMMAND_GETVAR] = {
97 .command = "getvar",
98 .dispatch = getvar
99 },
Xindong Xud9441422024-01-18 10:20:45 +0800100#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000101 [FASTBOOT_COMMAND_DOWNLOAD] = {
102 .command = "download",
103 .dispatch = download
104 },
Xindong Xud9441422024-01-18 10:20:45 +0800105#ifdef CONFIG_AMLOGIC_MODIFY
106#if !CONFIG_IS_ENABLED(NO_FASTBOOT_FLASHING)
107 [FASTBOOT_COMMAND_FLASHING] = {
108 .command = "flashing",
109 .dispatch = flashing
110 },
111#endif
112 [FASTBOOT_COMMAND_FETCH] = {
113 .command = "fetch",
114 .dispatch = fetch
115 },
116#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000117#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
118 [FASTBOOT_COMMAND_FLASH] = {
119 .command = "flash",
120 .dispatch = flash
121 },
122 [FASTBOOT_COMMAND_ERASE] = {
123 .command = "erase",
124 .dispatch = erase
125 },
126#endif
127 [FASTBOOT_COMMAND_BOOT] = {
128 .command = "boot",
129 .dispatch = okay
130 },
Xindong Xud9441422024-01-18 10:20:45 +0800131#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000132 [FASTBOOT_COMMAND_CONTINUE] = {
133 .command = "continue",
134 .dispatch = okay
135 },
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000136 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
137 .command = "reboot-bootloader",
138 .dispatch = reboot_bootloader
139 },
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +0300140 [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = {
141 .command = "reboot-fastboot",
142 .dispatch = reboot_fastbootd
143 },
144 [FASTBOOT_COMMAND_REBOOT_RECOVERY] = {
145 .command = "reboot-recovery",
146 .dispatch = reboot_recovery
147 },
Xindong Xud9441422024-01-18 10:20:45 +0800148 [FASTBOOT_COMMAND_REBOOT] = {
149 .command = "reboot",
150 .dispatch = okay
151 },
152#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000153 [FASTBOOT_COMMAND_SET_ACTIVE] = {
154 .command = "set_active",
Xindong Xud9441422024-01-18 10:20:45 +0800155#ifdef CONFIG_AMLOGIC_MODIFY
156 .dispatch = set_active_cmd
157#else
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000158 .dispatch = okay
Xindong Xud9441422024-01-18 10:20:45 +0800159#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000160 },
Alex Kiernan3845b902018-05-29 15:30:54 +0000161#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
162 [FASTBOOT_COMMAND_OEM_FORMAT] = {
163 .command = "oem format",
164 .dispatch = oem_format,
165 },
166#endif
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100167#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
168 [FASTBOOT_COMMAND_OEM_PARTCONF] = {
169 .command = "oem partconf",
170 .dispatch = oem_partconf,
171 },
172#endif
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100173#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
174 [FASTBOOT_COMMAND_OEM_BOOTBUS] = {
175 .command = "oem bootbus",
176 .dispatch = oem_bootbus,
177 },
178#endif
Xindong Xud9441422024-01-18 10:20:45 +0800179#endif
Heiko Schocherbc820d52021-02-10 09:29:03 +0100180#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
181 [FASTBOOT_COMMAND_UCMD] = {
182 .command = "UCmd",
183 .dispatch = run_ucmd,
184 },
185 [FASTBOOT_COMMAND_ACMD] = {
186 .command = "ACmd",
187 .dispatch = run_acmd,
188 },
189#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000190};
191
Xindong Xud9441422024-01-18 10:20:45 +0800192#ifdef CONFIG_AMLOGIC_MODIFY
193struct fastboot_read fastboot_readInfo;
194
195static int strcmp_l1(const char *s1, const char *s2)
196{
197 if (!s1 || !s2)
198 return -1;
199 return strncmp(s1, s2, strlen(s1));
200}
201#endif
202
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000203/**
204 * fastboot_handle_command - Handle fastboot command
205 *
206 * @cmd_string: Pointer to command string
207 * @response: Pointer to fastboot response buffer
208 *
209 * Return: Executed command, or -1 if not recognized
210 */
211int fastboot_handle_command(char *cmd_string, char *response)
212{
213 int i;
214 char *cmd_parameter;
215
216 cmd_parameter = cmd_string;
217 strsep(&cmd_parameter, ":");
218
219 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
Xindong Xud9441422024-01-18 10:20:45 +0800220#ifdef CONFIG_AMLOGIC_MODIFY
221 if (!strcmp_l1(commands[i].command, cmd_string)) {
222 if (commands[i].dispatch) {
223 if (cmd_parameter) {
224 commands[i].dispatch(cmd_parameter,
225 response);
226 } else {
227 commands[i].dispatch(cmd_string,
228 response);
229 }
230 return i;
231 } else {
232 break;
233 }
234 }
235#else
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000236 if (!strcmp(commands[i].command, cmd_string)) {
237 if (commands[i].dispatch) {
238 commands[i].dispatch(cmd_parameter,
239 response);
240 return i;
241 } else {
242 break;
243 }
244 }
Xindong Xud9441422024-01-18 10:20:45 +0800245#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000246 }
247
248 pr_err("command %s not recognized.\n", cmd_string);
249 fastboot_fail("unrecognized command", response);
250 return -1;
251}
252
253/**
254 * okay() - Send bare OKAY response
255 *
256 * @cmd_parameter: Pointer to command parameter
257 * @response: Pointer to fastboot response buffer
258 *
259 * Send a bare OKAY fastboot response. This is used where the command is
260 * valid, but all the work is done after the response has been sent (e.g.
261 * boot, reboot etc.)
262 */
263static void okay(char *cmd_parameter, char *response)
264{
265 fastboot_okay(NULL, response);
266}
267
Xindong Xud9441422024-01-18 10:20:45 +0800268#ifdef CONFIG_AMLOGIC_MODIFY
269void dump_lock_info(LockData_t info)
270{
271#if 0
272 printf("info.version_major = %d\n", info.version_major);
273 printf("info.version_minor = %d\n", info.version_minor);
274 printf("info.unlock_ability = %d\n", info.unlock_ability);
275 printf("info.lock_state = %d\n", info.lock_state);
276 printf("info.lock_critical_state = %d\n", info.lock_critical_state);
277 printf("info.lock_bootloader = %d\n", info.lock_bootloader);
278#endif
279}
280
281static const char* getvar_list[] = {
282 "version-baseband", "version-bootloader", "version", "hw-revision", "max-download-size",
283 "serialno", "product", "off-mode-charge", "variant", "battery-soc-ok",
284 "battery-voltage", "partition-type:boot", "partition-size:boot",
285 "partition-type:system", "partition-size:system", "partition-type:vendor", "partition-size:vendor",
286 "partition-type:odm", "partition-size:odm", "partition-type:data", "partition-size:data",
287 "erase-block-size", "logical-block-size", "secure", "unlocked",
288};
289
290static const char* getvar_list_dynamic[] = {
291 "hw-revision", "battery-voltage", "is-userspace", "is-logical:data",
292 "is-logical:metadata", "is-logical:misc", "is-logical:super", "is-logical:boot",
293 "is-logical:system", "is-logical:vendor", "is-logical:product", "is-logical:odm",
294 "slot-count", "max-download-size", "serialno", "product", "unlocked",
295 "secure", "super-partition-name", "version-baseband", "version-bootloader",
296 "partition-size:boot", "partition-size:metadata", "partition-size:misc",
297 "partition-size:super", "partition-size:data", "version",
298};
299
300static const char* getvar_list_dynamic_ab[] = {
301 "hw-revision", "battery-voltage", "is-userspace", "is-logical:data",
302 "is-logical:misc", "is-logical:super",
303 "is-logical:boot_a", "is-logical:boot_b", "is-logical:system_a", "is-logical:system_b",
304 "is-logical:vendor_a", "is-logical:vendor_b", "is-logical:product_a", "is-logical:product_b",
305 "is-logical:odm_a", "is-logical:odm_b",
306 "slot-count", "max-download-size", "serialno", "product", "unlocked", "has-slot:data",
307 "has-slot:metadata", "has-slot:misc", "has-slot:super", "has-slot:boot",
308 "has-slot:system", "has-slot:vendor", "has-slot:product", "has-slot:odm", "current-slot",
309 "secure", "super-partition-name", "version-baseband", "version-bootloader",
310 "partition-size:super",
311 "partition-size:boot_a", "partition-size:boot_b", "partition-size:misc",
312 "partition-size:data", "version",
313};
314
315
316static const char* getvar_list_ab[] = {
317 "version-baseband", "version-bootloader", "version", "hw-revision", "max-download-size",
318 "serialno", "product", "off-mode-charge", "variant", "battery-soc-ok",
319 "battery-voltage", "partition-type:boot_a", "partition-size:boot_a",
320 "partition-type:system_a", "partition-size:system_a", "partition-type:vendor_a", "partition-size:vendor_a",
321 "partition-type:odm_a", "partition-size:odm_a", "partition-type:data", "partition-size:data",
322 "erase-block-size", "logical-block-size", "secure", "unlocked",
323 "slot-count", "slot-suffixes","current-slot", "has-slot:bootloader", "has-slot:boot",
324 "has-slot:system", "has-slot:vendor", "has-slot:odm", "has-slot:vbmeta",
325 "has-slot:metadata", "has-slot:product", "has-slot:dtbo",
326 "slot-successful:a", "slot-unbootable:a", "slot-retry-count:a",
327 "slot-successful:b", "slot-unbootable:b", "slot-retry-count:b",
328};
329#endif
330
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000331/**
332 * getvar() - Read a config/version variable
333 *
334 * @cmd_parameter: Pointer to command parameter
335 * @response: Pointer to fastboot response buffer
336 */
337static void getvar(char *cmd_parameter, char *response)
338{
Xindong Xud9441422024-01-18 10:20:45 +0800339#ifdef CONFIG_AMLOGIC_MODIFY
340 run_command("get_valid_slot", 0);
341 if (!strncmp(cmd_parameter, "all", 3)) {
342 static int cmdIndex = 0;
343 int getvar_num = 0;
344 char* cmd=cmd_parameter;
345
346 busy_flag = 1;
347
348 if (dynamic_partition && has_boot_slot == 1 && strlen(getvar_list_dynamic_ab[cmdIndex]) < 64) {
349 strncpy(cmd, getvar_list_dynamic_ab[cmdIndex], 64);
350 getvar_num = (sizeof(getvar_list_dynamic_ab) / sizeof(getvar_list_dynamic_ab[0]));
351 } else if (has_boot_slot == 1 && strlen(getvar_list_ab[cmdIndex]) < 64) {
352 strncpy(cmd, getvar_list_ab[cmdIndex], 64);
353 getvar_num = (sizeof(getvar_list_ab) / sizeof(getvar_list_ab[0]));
354 } else if (dynamic_partition && strlen(getvar_list_dynamic[cmdIndex]) < 64) {
355 strncpy(cmd, getvar_list_dynamic[cmdIndex], 64);//only support no-arg cmd
356 getvar_num = (sizeof(getvar_list_dynamic) / sizeof(getvar_list_dynamic[0]));
357 } else if (strlen(getvar_list[cmdIndex]) < 64) {
358 strncpy(cmd, getvar_list[cmdIndex], 64);//only support no-arg cmd
359 getvar_num = (sizeof(getvar_list) / sizeof(getvar_list[0]));
360 }
361 //printf("getvar_num: %d\n", getvar_num);
362 //printf("all cmd:%s\n", cmd);
363 if ( ++cmdIndex >= getvar_num) {
364 cmdIndex = 0;
365 busy_flag = 0;
366 fastboot_okay(NULL, response);
367 } else {
368 fastboot_getvar(cmd, response);
369 }
370
371 }else {
372#endif
373 fastboot_getvar(cmd_parameter, response);
374#ifdef CONFIG_AMLOGIC_MODIFY
375 }
376#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000377}
378
379/**
380 * fastboot_download() - Start a download transfer from the client
381 *
382 * @cmd_parameter: Pointer to command parameter
383 * @response: Pointer to fastboot response buffer
384 */
Xindong Xud9441422024-01-18 10:20:45 +0800385#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000386static void download(char *cmd_parameter, char *response)
387{
388 char *tmp;
389
Xindong Xud9441422024-01-18 10:20:45 +0800390#ifdef CONFIG_AMLOGIC_MODIFY
391 if (check_lock() == 1) {
392 printf("device is locked, can not run this cmd.Please flashing unlock & flashing unlock_critical\n");
393 fastboot_fail("locked device", response);
394 return;
395 }
396#endif
397
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000398 if (!cmd_parameter) {
399 fastboot_fail("Expected command parameter", response);
400 return;
401 }
402 fastboot_bytes_received = 0;
Simon Glass7e5f4602021-07-24 09:03:29 -0600403 fastboot_bytes_expected = hextoul(cmd_parameter, &tmp);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000404 if (fastboot_bytes_expected == 0) {
405 fastboot_fail("Expected nonzero image size", response);
406 return;
407 }
408 /*
409 * Nothing to download yet. Response is of the form:
410 * [DATA|FAIL]$cmd_parameter
411 *
412 * where cmd_parameter is an 8 digit hexadecimal number
413 */
414 if (fastboot_bytes_expected > fastboot_buf_size) {
Xindong Xud9441422024-01-18 10:20:45 +0800415#ifdef CONFIG_AMLOGIC_MODIFY
416 char str[128] = {0};
417
418 printf("fastboot_bytes_expected %d > fastboot_buf_size %d\n",
419 fastboot_bytes_expected, fastboot_buf_size);
420 sprintf(str, "data too large, please add -S %dM",
421 (fastboot_buf_size / 1024 / 1024));
422 fastboot_fail(str, response);
423#else
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000424 fastboot_fail(cmd_parameter, response);
Xindong Xud9441422024-01-18 10:20:45 +0800425#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000426 } else {
427 printf("Starting download of %d bytes\n",
428 fastboot_bytes_expected);
429 fastboot_response("DATA", response, "%s", cmd_parameter);
430 }
431}
Xindong Xud9441422024-01-18 10:20:45 +0800432#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000433/**
434 * fastboot_data_remaining() - return bytes remaining in current transfer
435 *
436 * Return: Number of bytes left in the current download
437 */
438u32 fastboot_data_remaining(void)
439{
440 return fastboot_bytes_expected - fastboot_bytes_received;
441}
442
443/**
444 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
445 *
446 * @fastboot_data: Pointer to received fastboot data
447 * @fastboot_data_len: Length of received fastboot data
448 * @response: Pointer to fastboot response buffer
449 *
450 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
451 * response. fastboot_bytes_received is updated to indicate the number
452 * of bytes that have been transferred.
453 *
454 * On completion sets image_size and ${filesize} to the total size of the
455 * downloaded image.
456 */
457void fastboot_data_download(const void *fastboot_data,
458 unsigned int fastboot_data_len,
459 char *response)
460{
461#define BYTES_PER_DOT 0x20000
462 u32 pre_dot_num, now_dot_num;
463
464 if (fastboot_data_len == 0 ||
465 (fastboot_bytes_received + fastboot_data_len) >
466 fastboot_bytes_expected) {
467 fastboot_fail("Received invalid data length",
468 response);
469 return;
470 }
471 /* Download data to fastboot_buf_addr */
472 memcpy(fastboot_buf_addr + fastboot_bytes_received,
473 fastboot_data, fastboot_data_len);
474
475 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
476 fastboot_bytes_received += fastboot_data_len;
477 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
478
479 if (pre_dot_num != now_dot_num) {
480 putc('.');
481 if (!(now_dot_num % 74))
482 putc('\n');
483 }
484 *response = '\0';
485}
486
487/**
488 * fastboot_data_complete() - Mark current transfer complete
489 *
490 * @response: Pointer to fastboot response buffer
491 *
492 * Set image_size and ${filesize} to the total size of the downloaded image.
493 */
494void fastboot_data_complete(char *response)
495{
496 /* Download complete. Respond with "OKAY" */
497 fastboot_okay(NULL, response);
498 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
499 image_size = fastboot_bytes_received;
500 env_set_hex("filesize", image_size);
501 fastboot_bytes_expected = 0;
502 fastboot_bytes_received = 0;
503}
504
Xindong Xud9441422024-01-18 10:20:45 +0800505#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000506#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Xindong Xud9441422024-01-18 10:20:45 +0800507
508#ifdef CONFIG_AMLOGIC_MODIFY
509static void write_dts_reserve(void)
510{
511 int ret;
512 void *addr = NULL;
513 char *mem_addr;
514
515 if (run_command("imgread dtb ${boot_part} ${dtb_mem_addr}", 0)) {
516 printf("Fail in load dtb\n");
517 } else {
518 mem_addr = env_get("dtb_mem_addr");
519
520 if (mem_addr) {
521 addr = (void *)simple_strtoul(mem_addr, NULL, 16);
522 ret = dtb_write(addr);
523 if (ret)
524 printf("write dtb error\n");
525 else
526 printf("write dtb ok\n");
527 }
528 }
529}
530
Xindong Xu22e8daf2024-03-12 18:08:41 +0800531static void set_fastboot_flag(int flag)
Xindong Xud9441422024-01-18 10:20:45 +0800532{
533 env_set("default_env", "1");
Xindong Xu22e8daf2024-03-12 18:08:41 +0800534 if (flag == 1)
535 env_set("fastboot_step", "1");
Xindong Xud9441422024-01-18 10:20:45 +0800536#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
537 run_command("update_env_part -p default_env;", 0);
Xindong Xu22e8daf2024-03-12 18:08:41 +0800538 if (flag == 1)
539 run_command("update_env_part -p fastboot_step;", 0);
Xindong Xud9441422024-01-18 10:20:45 +0800540#else
541 run_command("defenv_reserve;", 0);
542 run_command("setenv default_env 1;", 0);
Xindong Xu22e8daf2024-03-12 18:08:41 +0800543 if (flag == 1)
544 run_command("setenv fastboot_step 1;", 0);
Xindong Xud9441422024-01-18 10:20:45 +0800545 run_command("saveenv;", 0);
546#endif//#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
547}
548#endif
549
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000550/**
551 * flash() - write the downloaded image to the indicated partition.
552 *
553 * @cmd_parameter: Pointer to partition name
554 * @response: Pointer to fastboot response buffer
555 *
556 * Writes the previously downloaded image to the partition indicated by
557 * cmd_parameter. Writes to response.
558 */
559static void flash(char *cmd_parameter, char *response)
560{
Xindong Xud9441422024-01-18 10:20:45 +0800561#ifdef CONFIG_AMLOGIC_MODIFY
562 char name[32] = {0};
563 u64 rc = 0;
564 int gpt_flag = -1;
565 int ret = -1;
566
567 if (check_lock() == 1) {
568 printf("device is locked, can not run this cmd.Please flashing unlock & flashing unlock_critical\n");
569 fastboot_fail("locked device", response);
570 return;
571 }
572
573 printf("cmd_parameter: %s\n", cmd_parameter);
574
575#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
576 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
577 if (mmc && strcmp(cmd_parameter, "bootloader") == 0) {
578 printf("try to read gpt data from bootloader.img\n");
579 struct blk_desc *dev_desc;
580 int erase_flag = 0;
Xindong Xu22e8daf2024-03-12 18:08:41 +0800581 char *bootloaderindex;
582 char *slot_name = NULL;
583 char partname[32] = {0};
Xindong Xud9441422024-01-18 10:20:45 +0800584
Xindong Xu22e8daf2024-03-12 18:08:41 +0800585 slot_name = env_get("active_slot");
586 if (slot_name && (strcmp(slot_name, "_a") == 0))
587 strcpy((char *)partname, "bootloader_a");
588 else if (slot_name && (strcmp(slot_name, "_b") == 0))
589 strcpy((char *)partname, "bootloader_b");
590 else
591 strcpy((char *)partname, "bootloader_up");
592
593 rc = store_part_size(partname);
Xindong Xud9441422024-01-18 10:20:45 +0800594 if (rc != -1) {
Xindong Xu22e8daf2024-03-12 18:08:41 +0800595 fastboot_mmc_erase(partname, response);
596 fastboot_mmc_flash_write(partname, fastboot_buf_addr, image_size,
Xindong Xud9441422024-01-18 10:20:45 +0800597 response);
598 }
599
600 /* the max size of bootloader.img is 4M, we reserve 128k for gpt.bin
601 * so we put gpt.bin at offset 0x3DFE00
602 * 0 ~ 512 bootloader secure boot, we don't care it here.
603 * 512 ~ 0x3DFDFF original bootloader.img and 0
604 * 0x3DFE00 ~ end gpt.bin
605 */
606
607 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
608 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
609 printf("invalid mmc device\n");
610 fastboot_fail("invalid mmc device", response);
611 return;
612 }
613
614 if (is_valid_gpt_buf(dev_desc, fastboot_buf_addr + 0x3DFE00)) {
615 printf("printf normal bootloader.img, no gpt partition table\n");
616 } else {
617 printf("find gpt partition table, update it\n"
618 "and write bootloader to boot0/boot1\n");
619
620 erase_flag = check_gpt_part(dev_desc, fastboot_buf_addr + 0x3DFE00);
621
622 if (erase_flag == 1) {
623 printf("partition changes, erase emmc\n");
624 //run_command("store erase.chip 0;", 0);
625 if (usb_burn_erase_data(0x3)) {
626 printf("partition erase failed!\n");
627 return;
628 }
629 }
630
631 if (write_mbr_and_gpt_partitions(dev_desc, fastboot_buf_addr + 0x3DFE00)) {
632 printf("%s: writing GPT partitions failed\n", __func__);
633 fastboot_fail("writing GPT partitions failed", response);
634 return;
635 }
636
637 if (mmc_device_init(mmc) != 0) {
638 printf(" update gpt partition table fail\n");
639 fastboot_fail("fastboot update gpt partition fail", response);
640 return;
641 }
642 printf("%s: writing GPT partitions ok\n", __func__);
643
644 char *mem_addr;
645 void *addr = NULL;
646 int ret;
647
648 mem_addr = env_get("dtb_mem_addr");
649
650 if (mem_addr && erase_flag == 1) {
651 printf("partition changes, erase emmc\n");
652 //run_command("store erase.chip 0;", 0);
653 if (usb_burn_erase_data(0x3)) {
654 printf("partition erase failed!\n");
655 return;
656 }
657 printf("write _aml_dtb\n");
658 addr = (void *)simple_strtoul(mem_addr, NULL, 16);
659 ret = dtb_write(addr);
660 if (ret)
661 printf("write _aml_dtb error\n");
662 else
663 printf("write _aml_dtb ok\n");
664 }
665 }
666
667 gpt_flag = aml_gpt_valid(mmc);
668 if (gpt_flag == 0)
669 ret = 0;
670
671#if defined(CONFIG_EFUSE_OBJ_API) && defined(CONFIG_CMD_EFUSE)
672 run_command("efuse_obj get FEAT_DISABLE_EMMC_USER", 0);
673
674 if (*efuse_field.data == 1) {
675 wrnP("efuse_field.data == 1\n");
676 ret = 0;
677 env_set("nocs_mode", "true");
678 } else {
679 wrnP("efuse_field.data != 1\n");
680 env_set("nocs_mode", "false");
681 }
682#endif//#ifdef CONFIG_EFUSE_OBJ_API
683
Xindong Xu22e8daf2024-03-12 18:08:41 +0800684 bootloaderindex = env_get("forUpgrade_bootloaderIndex");
685
686 if (ret == 0) {
687 printf("gpt/nocs mode\n");
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000688#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
Xindong Xu22e8daf2024-03-12 18:08:41 +0800689 fastboot_mmc_flash_write("bootloader-boot1", fastboot_buf_addr, image_size,
690 response);
691 if (strcmp(bootloaderindex, "1") != 0) {
692 printf("boot from boot1, means boot0 is error, rewrite it\n");
693 fastboot_mmc_flash_write("bootloader-boot0",
694 fastboot_buf_addr, image_size, response);
695 run_command("mmc dev 1 0;", 0);
696 set_fastboot_flag(0);
697 } else {
698 printf("need to set fastboot_step\n");
699 run_command("mmc dev 1 0;", 0);
700 set_fastboot_flag(1);
701 }
Xindong Xud9441422024-01-18 10:20:45 +0800702#endif
Xindong Xu22e8daf2024-03-12 18:08:41 +0800703 return;
704 } else {
705 printf("normal mode\n");
706#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
707 fastboot_mmc_flash_write("bootloader-boot0", fastboot_buf_addr, image_size,
708 response);
709 fastboot_mmc_flash_write("bootloader-boot1", fastboot_buf_addr, image_size,
710 response);
711 if (strcmp(bootloaderindex, "0") != 0) {
712 printf("boot from boot0, rewrite user bootloader is error\n");
713 fastboot_mmc_flash_write("bootloader",
714 fastboot_buf_addr, image_size, response);
715 run_command("mmc dev 1 0;", 0);
716 set_fastboot_flag(0);
717 } else {
718 run_command("mmc dev 1 0;", 0);
719 set_fastboot_flag(1);
720 }
721#endif
722 return;
723 }
Xindong Xud9441422024-01-18 10:20:45 +0800724 }
725#endif
726
727 if (strcmp(cmd_parameter, "avb_custom_key") == 0) {
728 char* buffer = NULL;
729 char *partition = "misc";
730 AvbKey_t key;
731 printf("avb_custom_key image_size: %d\n", image_size);
732
733 if (image_size > AVB_CUSTOM_KEY_LEN_MAX - sizeof(AvbKey_t)) {
734 printf("key size is too large\n");
735 fastboot_fail("size error", response);
736 return;
737 }
738
739 memcpy(key.magic_name, "AVBK", 4);
740 key.size = image_size;
741 rc = store_part_size(partition);
742
743 buffer = (char *)malloc(AVB_CUSTOM_KEY_LEN_MAX);
744 if (!buffer) {
745 printf("malloc error\n");
746 fastboot_fail("malloc error", response);
747 return;
748 }
749 memset(buffer, 0, AVB_CUSTOM_KEY_LEN_MAX);
750 memcpy(buffer, &key, sizeof(AvbKey_t));
751 memcpy(buffer + sizeof(AvbKey_t), fastboot_buf_addr, image_size);
752
753 store_write((const char *)partition, rc - AVB_CUSTOM_KEY_LEN_MAX,
754 AVB_CUSTOM_KEY_LEN_MAX, (unsigned char *)buffer);
755
756 fastboot_okay(NULL, response);
757 free(buffer);
758 return;
759 }
760
761 if (strcmp(cmd_parameter, "userdata") == 0 || strcmp(cmd_parameter, "data") == 0) {
762 fastboot_okay(NULL, response);
763 return;
764 } else if (strcmp(cmd_parameter, "dts") == 0) {
765 strcpy(name, "dtb");
766 } else {
767 strncpy(name, cmd_parameter, 31);
768 }
769 strcat(name, "\0");
770
771#ifdef CONFIG_BOOTLOADER_CONTROL_BLOCK
772 if (dynamic_partition) {
773 if (is_partition_logical(name) == 0) {
774 printf("logic partition, can not write here.......\n");
775 fastboot_fail("logic partition", response);
776 return;
777 }
778 }
779#endif
780
781#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
782 fastboot_mmc_flash_write(name, fastboot_buf_addr, image_size,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000783 response);
784#endif
785#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
Xindong Xud9441422024-01-18 10:20:45 +0800786 fastboot_nand_flash_write(name, fastboot_buf_addr, image_size,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000787 response);
788#endif
Xindong Xud9441422024-01-18 10:20:45 +0800789
790 if (strcmp(name, "bootloader") == 0) {
791 env_set("default_env", "1");
792#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
793 run_command("update_env_part -p default_env;", 0);
794#else
795 run_command("defenv_reserve;saveenv;", 0);
796#endif// #if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
797 }
798
799 if (strcmp(name, "bootloader-boot0") == 0 ||
800 strcmp(name, "bootloader-boot1") == 0) {
801 printf("try to switch back to user\n");
802 run_command("mmc dev 1 0;", 0);
803 }
804
805#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
806 if (mmc && aml_gpt_valid(mmc) == 0) {
807 if (vendor_boot_partition) {
808 if (strcmp_l1("vendor_boot", name) == 0) {
809 printf("gpt mode, write dts to reserve\n");
810 write_dts_reserve();
811 }
812 } else {
813 if (strcmp_l1("boot", name) == 0) {
814 printf("gpt mode, write dts to reserve\n");
815 write_dts_reserve();
816 }
817 }
818 }
819#endif
820
821#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000822}
823
824/**
825 * erase() - erase the indicated partition.
826 *
827 * @cmd_parameter: Pointer to partition name
828 * @response: Pointer to fastboot response buffer
829 *
830 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
831 * to response.
832 */
833static void erase(char *cmd_parameter, char *response)
834{
Xindong Xud9441422024-01-18 10:20:45 +0800835#ifdef CONFIG_AMLOGIC_MODIFY
836 char name[32] = {0};
837 u64 rc = 0;
838
839 if (check_lock() == 1) {
840 printf("device is locked, can not run this cmd.Please flashing unlock & flashing unlock_critical\n");
841 fastboot_fail("locked device", response);
842 return;
843 }
844
845 printf("cmd_parameter: %s\n", cmd_parameter);
846
847#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
848 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
849 if ((mmc != NULL) && strcmp(cmd_parameter, "bootloader") == 0 && (aml_gpt_valid(mmc) == 0)) {
850 printf("we write gpt partition table to bootloader now\n");
851 printf("plese write bootloader to bootloader-boot0/bootloader-boot1\n");
852 fastboot_okay("gpt mode, skip", response);
853 return;
854 }
855#endif
856
857 if (strcmp(cmd_parameter, "avb_custom_key") == 0) {
858 char* buffer = NULL;
859 char *partition = "misc";
860
861 rc = store_part_size(partition);
862 buffer = (char *)malloc(AVB_CUSTOM_KEY_LEN_MAX);
863 if (!buffer) {
864 printf("malloc error\n");
865 fastboot_fail("malloc error", response);
866 return;
867 }
868 memset(buffer, 0, AVB_CUSTOM_KEY_LEN_MAX);
869
870 store_write((const char *)partition, rc - AVB_CUSTOM_KEY_LEN_MAX,
871 AVB_CUSTOM_KEY_LEN_MAX, (unsigned char *)buffer);
872
873 fastboot_okay(NULL, response);
874 free(buffer);
875 return;
876 }
877
878 if (strcmp(cmd_parameter, "env") == 0) {
879 char *fastboot_step = env_get("fastboot_step");
880
881 if (fastboot_step && strcmp(fastboot_step, "0")) {
882 printf("fastboot_step: %s, run defenv_reserv\n", fastboot_step);
883 run_command("defenv_reserv; setenv upgrade_step 2; saveenv;", 0);
884 run_command("run bcb_cmd", 0);
885 fastboot_okay(NULL, response);
886 return;
887 }
888 }
889
890 if (strcmp(cmd_parameter, "misc") == 0) {
891 char* buffer = NULL;
892 char *partition = "misc";
893
894 rc = store_part_size(partition);
895 buffer = (char *)malloc(rc - AVB_CUSTOM_KEY_LEN_MAX);
896 if (!buffer) {
897 printf("malloc error\n");
898 fastboot_fail("malloc error", response);
899 return;
900 }
901 memset(buffer, 0, rc - AVB_CUSTOM_KEY_LEN_MAX);
902
903 store_write((const char *)partition, 0, rc - AVB_CUSTOM_KEY_LEN_MAX,
904 (unsigned char *)buffer);
905
906 fastboot_okay(NULL, response);
907 free(buffer);
908 return;
909 }
910
911 if (strcmp(cmd_parameter, "userdata") == 0 || strcmp(cmd_parameter, "data") == 0) {
912 rc = store_part_size("userdata");
913 if (-1 == rc)
914 strcpy(name, "data");
915 else
916 strcpy(name, "userdata");
917 } else if (strcmp(cmd_parameter, "dts") == 0) {
918 strcpy(name, "dtb");
919 } else {
920 strncpy(name, cmd_parameter, 31);
921 }
922 strcat(name, "\0");
923
924#ifdef CONFIG_BOOTLOADER_CONTROL_BLOCK
925 if (dynamic_partition) {
926 if (is_partition_logical(name) == 0) {
927 printf("logic partition, can not erase here.......\n");
928 fastboot_fail("logic partition", response);
929 return;
930 }
931 }
932#endif
933
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000934#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
Xindong Xud9441422024-01-18 10:20:45 +0800935 fastboot_mmc_erase(name, response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000936#endif
937#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
Xindong Xud9441422024-01-18 10:20:45 +0800938 fastboot_nand_erase(name, response);
939#endif
940
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000941#endif
942}
943#endif
Xindong Xud9441422024-01-18 10:20:45 +0800944#endif
945
946#ifdef CONFIG_FASTBOOT_WRITING_CMD
947
948#ifdef CONFIG_AMLOGIC_MODIFY
949static u64 my_ato11(const char *str)
950{
951 u64 result = 0;
952 int len, i;
953
954 len = strlen(str);
955 for (i = 0; i < len; i++) {
956 if (*str >= '0' && *str <= '9')
957 result = result * 16 + (*str - '0');
958 else if (*str >= 'A' && *str <= 'F')
959 result = result * 16 + (*str - 'A') + 10;
960 else if (*str >= 'a' && *str <= 'f')
961 result = result * 16 + (*str - 'a') + 10;
962 str++;
963 }
964
965 return result;
966}
967
968static void fetch(char *cmd_parameter, char *response)
969{
970 int len;
971 int i = 0;
972 char *cmd;
973 char name[32] = {0};
974 u64 offset = 0;
975 u64 read_size = 0;
976 size_t size = 0;
977
978 if (check_lock() == 1) {
979 printf("device is locked, can not run this cmd\n");
980 fastboot_fail("locked device", response);
981 return;
982 }
983
984 cmd = cmd_parameter;
985 len = strlen(cmd_parameter);
986 while (strsep(&cmd, ":"))
987 i++;
988
989 for (cmd = cmd_parameter, i = 0; cmd < (cmd_parameter + len); i++) {
990 /* Skip to next assignment */
991 if (i == 0) {
992 strncpy(name, cmd, 31);
993 strcat(name, "\0");
994 } else if (i == 1) {
995 offset = my_ato11(cmd);
996 } else if (i == 2) {
997 read_size = my_ato11(cmd);
998 }
999
1000 for (cmd += strlen(cmd); cmd < (cmd_parameter + len) && !*cmd;)
1001 cmd++;
1002 }
1003
1004 printf("name: %s\n", name);
1005 if (strncmp("vendor_boot", name, strlen("vendor_boot")) != 0) {
1006 printf("We can only %s vendor_boot\n", __func__);
1007 fastboot_fail("Fetch is only allowed on vendor_boot", response);
1008 return;
1009 }
1010
1011#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
1012 struct blk_desc *dev_desc;
1013 disk_partition_t part_info;
1014 int r;
1015
1016 r = fastboot_mmc_get_part_info(name, &dev_desc, &part_info,
1017 response);
1018 if (r >= 0)
1019 size = part_info.size * 512;
1020#endif
1021#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
1022 struct part_info *part_info;
1023 int r;
1024
1025 r = fastboot_nand_get_part_info(name, &part_info, response);
1026 if (r >= 0)
1027 size = part_info->size * 512;
1028#endif
1029
1030 if (offset > size) {
1031 printf("Invalid offset: 0x%llx, partition size is 0x%lx\n",
1032 offset, size);
1033 fastboot_fail("Invalid offset", response);
1034 return;
1035 }
1036
1037 if (read_size == 0 || read_size > size - offset ||
1038 read_size > kMaxFetchSizeDefault) {
1039 printf("Invalid read size: 0x%llx, partition size is 0x%lx\n",
1040 offset, size);
1041 fastboot_fail("Invalid read size", response);
1042 return;
1043 }
1044
1045 printf("Start read %s partition datas!\n", name);
1046 void *buffer;
1047 char str[128] = {0};
1048
1049 buffer = (void *)CONFIG_FASTBOOT_BUF_ADDR;
1050 sprintf(str, "DATA%12llx", read_size);
1051 printf("str: %s, len: %ld\n", str, strlen(str));
1052 memcpy(buffer, str, strlen(str));
1053
1054 if (store_read((const char *)name, offset, read_size,
1055 (unsigned char *)buffer + strlen(str)) < 0) {
1056 printf("failed to store read %s.\n", name);
1057 fastboot_fail("read partition data error", response);
1058 return;
1059 }
1060
1061 fastboot_readInfo.transferredBytes = 0;
1062 fastboot_readInfo.totalBytes = read_size + strlen(str);
1063 fastboot_response("DATA", response, "%12llx", read_size);
1064}
1065
1066static void set_active_cmd(char *cmd_parameter, char *response)
1067{
1068 char *cmd;
1069 int ret = 0;
1070 char str[128];
1071
1072 printf("cmd cb_set_active is %s\n", cmd_parameter);
1073 cmd = cmd_parameter;
1074
1075 if (check_lock() == 1) {
1076 printf("device is locked, can not run this cmd.Please flashing unlock & flashing unlock_critical\n");
1077 fastboot_fail("locked device", response);
1078 return;
1079 }
1080
1081 sprintf(str, "set_active_slot %s", cmd);
1082 printf("command: %s\n", str);
1083 ret = run_command(str, 0);
1084 printf("ret = %d\n", ret);
1085 if (ret == 0)
1086 fastboot_okay(NULL, response);
1087 else
1088 fastboot_fail("set slot error", response);
1089}
1090
1091#if !CONFIG_IS_ENABLED(NO_FASTBOOT_FLASHING)
1092static void try_unlock_dev(u64 rc)
1093{
1094#if defined(CONFIG_AML_ANTIROLLBACK) || defined(CONFIG_AML_AVB2_ANTIROLLBACK)
1095 if (is_avb_arb_available()) {
1096 if (avb_unlock()) {
1097 if (-1 == rc) {
1098 printf("unlocking device. Erasing data partition!\n");
1099 run_command("store erase data 0 0", 0);
1100 } else {
1101 printf("unlocking device. Erasing userdata partition!\n");
1102 run_command("store erase userdata 0 0", 0);
1103 }
1104 printf("unlocking device. Erasing metadata partition!\n");
1105 run_command("store erase metadata 0 0", 0);
1106 } else {
1107 printf("unlock failed!\n");
1108 }
1109
1110 return;
1111 }
1112#endif
1113 if (-1 == rc) {
1114 printf("unlocking device. Erasing data partition!\n");
1115 run_command("store erase data 0 0", 0);
1116 } else {
1117 printf("unlocking device. Erasing userdata partition!\n");
1118 run_command("store erase userdata 0 0", 0);
1119 }
1120 printf("unlocking device. Erasing metadata partition!\n");
1121 run_command("store erase metadata 0 0", 0);
1122}
1123
1124static void try_lock_dev(u64 rc)
1125{
1126#if defined(CONFIG_AML_ANTIROLLBACK) || defined(CONFIG_AML_AVB2_ANTIROLLBACK)
1127 if (is_avb_arb_available()) {
1128 if (avb_lock()) {
1129 if (-1 == rc) {
1130 printf("locking device. Erasing data partition!\n");
1131 run_command("store erase data 0 0", 0);
1132 } else {
1133 printf("locking device. Erasing userdata partition!\n");
1134 run_command("store erase userdata 0 0", 0);
1135 }
1136 printf("locking device. Erasing metadata partition!\n");
1137 run_command("store erase metadata 0 0", 0);
1138 } else {
1139 printf("lock failed!\n");
1140 }
1141
1142 return;
1143 }
1144#endif
1145 if (-1 == rc) {
1146 printf("locking device. Erasing data partition!\n");
1147 run_command("store erase data 0 0", 0);
1148 } else {
1149 printf("locking device. Erasing userdata partition!\n");
1150 run_command("store erase userdata 0 0", 0);
1151 }
1152 printf("locking device. Erasing metadata partition!\n");
1153 run_command("store erase metadata 0 0", 0);
1154}
1155
1156/**
1157 * flashing() - lock/unlock.
1158 *
1159 * @cmd_parameter: Pointer to partition name
1160 * @response: Pointer to fastboot response buffer
1161 *
1162 * Writes the previously downloaded image to the partition indicated by
1163 * cmd_parameter. Writes to response.
1164 */
1165static void flashing(char *cmd_parameter, char *response)
1166{
1167 char *cmd;
1168 char* lock_s;
1169 LockData_t info = {0};
1170 char lock_d[LOCK_DATA_SIZE];
1171 u64 rc;
1172 int debug_flag = 0;
1173
1174 if (IS_FEAT_BOOT_VERIFY()) {
1175 printf("device is secure mode, can not run this cmd.\n");
1176 fastboot_fail("secure boot device", response);
1177 return;
1178 }
1179
1180 lock_s = env_get("lock");
1181 if (!lock_s) {
1182 printf("lock state is NULL \n");
1183 memcpy(lock_d, "10101000", 8);
1184 lock_s = "10101000";
1185 env_set("lock", "10101000");
1186#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
1187 run_command("update_env_part -p lock;", 0);
1188#else
1189 run_command("defenv_reserv; saveenv;", 0);
1190#endif//#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
1191 } else {
1192 printf("lock state: %s\n", lock_s);
1193 if (strlen(lock_s) > 15)
1194 strncpy(lock_d, lock_s, 15);
1195 else
1196 strncpy(lock_d, lock_s, strlen(lock_s));
1197 }
1198
1199 info.version_major = (int)(lock_d[0] - '0');
1200 info.version_minor = (int)(lock_d[1] - '0');
1201 info.unlock_ability = (int)(lock_d[2] - '0');
1202 info.lock_state = (int)(lock_d[4] - '0');
1203 info.lock_critical_state = (int)(lock_d[5] - '0');
1204 info.lock_bootloader = (int)(lock_d[6] - '0');
1205 dump_lock_info(info);
1206
1207 printf("cb_flashing cmd_parameter: %s\n", cmd_parameter);
1208 cmd = cmd_parameter;
1209 strsep(&cmd, " ");
1210 printf("cb_flashing: %s\n", cmd);
1211 if (!cmd) {
1212 printf("missing variable\n");
1213 fastboot_fail("missing var", response);
1214 return;
1215 }
1216
1217 boot_img_hdr_t *hdr_addr = NULL;
1218 const int preloadsz = 0x1000 * 2;//4k not enough for signed
1219 unsigned char *pbuffpreload = 0;
1220 char partname[32] = {0};
1221 char *slot_name;
1222 char cmdline[4096];
1223 char *result = NULL;
1224
1225 slot_name = env_get("slot-suffixes");
1226 if (slot_name && (strcmp(slot_name, "0") == 0))
1227 strcpy((char *)partname, "boot_a");
1228 else if (slot_name && (strcmp(slot_name, "1") == 0))
1229 strcpy((char *)partname, "boot_b");
1230 else
1231 strcpy((char *)partname, "boot");
1232
1233 pbuffpreload = malloc(preloadsz);
1234 if (!pbuffpreload) {
1235 printf("Fail to allocate memory!\n");
1236 goto next;
1237 }
1238
1239 hdr_addr = (boot_img_hdr_t *)pbuffpreload;
1240
1241 printf("read from part: %s\n", partname);
1242 rc = store_logic_read(partname, 0, preloadsz, pbuffpreload);
1243 if (rc) {
1244 printf("Fail to read 0x%xB from part[%s] at offset 0\n",
1245 preloadsz, partname);
1246 free(pbuffpreload);
1247 pbuffpreload = 0;
1248 goto next;
1249 }
1250
1251 if (is_android_r_image((void *)hdr_addr)) {
1252 char partname_r[32] = {0};
1253 const int preloadsz_r = 0x1000 * 2;//4k not enough for signed
1254 unsigned char *pbuffpreload_r = 0;
1255 int ret_r = __LINE__;
1256
1257 if (slot_name && (strcmp(slot_name, "0") == 0))
1258 strcpy((char *)partname_r, "vendor_boot_a");
1259 else if (slot_name && (strcmp(slot_name, "1") == 0))
1260 strcpy((char *)partname_r, "vendor_boot_b");
1261 else
1262 strcpy((char *)partname_r, "vendor_boot");
1263
1264 pbuffpreload_r = malloc(preloadsz_r);
1265
1266 if (!pbuffpreload_r) {
1267 printf("Fail to allocate memory for %s!\n",
1268 partname_r);
1269 goto next;
1270 }
1271
1272 printf("read from part: %s\n", partname_r);
1273 ret_r = store_logic_read(partname_r, 0,
1274 preloadsz_r, pbuffpreload_r);
1275 if (ret_r) {
1276 printf("Fail to read 0x%xB from part[%s] at offset 0\n",
1277 preloadsz_r, partname_r);
1278 free(pbuffpreload_r);
1279 pbuffpreload_r = 0;
1280 goto next;
1281 }
1282
1283 p_vendor_boot_img_hdr_t vb_hdr = (p_vendor_boot_img_hdr_t)pbuffpreload_r;
1284
1285 if (*vb_hdr->cmdline) {
1286 printf("Kernel command line: %s\n", vb_hdr->cmdline);
1287 sprintf(cmdline, "%s", vb_hdr->cmdline);
1288 }
1289 free(pbuffpreload_r);
1290 pbuffpreload_r = 0;
1291 } else {
1292 if (*hdr_addr->cmdline) {
1293 printf("Kernel command line: %s\n", hdr_addr->cmdline);
1294 sprintf(cmdline, "%s", hdr_addr->cmdline);
1295 }
1296 }
1297
1298 free(pbuffpreload);
1299 pbuffpreload = 0;
1300
1301 result = strtok(cmdline, " ");
1302 while (result) {
1303 printf("result: %s\n", result);
1304 if (strcmp(result, "buildvariant=userdebug") == 0 ||
1305 strcmp(result, "buildvariant=eng") == 0)
1306 debug_flag = 1;
1307 result = strtok(NULL, " ");
1308 }
1309
1310 if (info.unlock_ability == 0 && debug_flag == 1) {
1311 printf("userdebug mode can ignore\n");
1312 info.unlock_ability = 1;
1313 }
1314
1315next:
1316 rc = store_part_size("userdata");
1317
1318 if (!strcmp_l1("unlock_critical", cmd)) {
1319 info.lock_critical_state = 0;
1320 fastboot_okay(NULL, response);
1321 } else if (!strcmp_l1("lock_critical", cmd)) {
1322 info.lock_critical_state = 1;
1323 fastboot_okay(NULL, response);
1324 } else if (!strcmp_l1("get_unlock_ability", cmd)) {
1325 char str[32];
1326 static bool is_unlock_ability_sent = false;
1327 if (is_unlock_ability_sent) {
1328 is_unlock_ability_sent = false;
1329 fastboot_okay(NULL, response);
1330 busy_flag = 0;
1331 } else {
1332 sprintf(str, "get_unlock_ability: %d",
1333 info.unlock_ability);
1334 fastboot_response("INFO", response, "%s", str);
1335 is_unlock_ability_sent = true;
1336 busy_flag = 1;
1337 }
1338 return;
1339 } else if (!strcmp_l1("get_unlock_bootloader_nonce", cmd)) {
1340 char str_num[8];
1341 sprintf(str_num, "%d", info.lock_critical_state);
1342 fastboot_response("OKAY", response, "%s", str_num);
1343 } else if (!strcmp_l1("lock_bootloader", cmd)) {
1344 info.lock_bootloader = 1;
1345 } else if (!strcmp_l1("unlock", cmd)) {
1346 if (info.unlock_ability == 1) {
1347 if (info.lock_state == 1) {
1348 char *avb_s;
1349
1350 run_command("get_avb_mode;", 0);
1351 avb_s = env_get("avb2");
1352 printf("avb2: %s\n", avb_s);
1353 if (strcmp(avb_s, "1") == 0) {
1354 try_unlock_dev(rc);
1355 }
1356 }
1357 info.lock_state = 0;
1358 info.lock_critical_state = 0;
1359 env_set("lock_state", "green");
1360 fastboot_okay(NULL, response);
1361 } else {
1362 printf("unlock_ability is 0, can not unlock, please set it in android setting\n");
1363 fastboot_response("FAIL", response, "%s", "unlock_ability is 0, can not unlock");
1364 }
1365 } else if (!strcmp_l1("lock", cmd)) {
1366 if (info.lock_state == 0) {
1367 char *avb_s;
1368
1369 run_command("get_avb_mode;", 0);
1370 avb_s = env_get("avb2");
1371 printf("avb2: %s\n", avb_s);
1372 if (strcmp(avb_s, "1") == 0) {
1373 try_lock_dev(rc);
1374 }
1375 }
1376 info.lock_state = 1;
1377 env_set("lock_state", "orange");
1378 fastboot_okay(NULL, response);
1379 } else {
1380 printf("unknown variable: %s\n", cmd);
1381 fastboot_response("FAIL", response, "%s", "Variable not implemented");
1382 }
1383
1384 sprintf(lock_d, "%d%d%d0%d%d%d0", info.version_major, info.version_minor,
1385 info.unlock_ability, info.lock_state, info.lock_critical_state,
1386 info.lock_bootloader);
1387 printf("lock_d state: %s\n", lock_d);
1388 env_set("lock", lock_d);
1389#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
1390 run_command("update_env_part -p lock;", 0);
1391#else
1392 run_command("defenv_reserv; saveenv;", 0);
1393#endif//#if CONFIG_IS_ENABLED(AML_UPDATE_ENV)
1394 return;
1395}
1396#endif// #if !CONFIG_IS_ENABLED(NO_FASTBOOT_FLASHING)
1397#endif
1398#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001399
Heiko Schocherbc820d52021-02-10 09:29:03 +01001400#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
1401/**
1402 * run_ucmd() - Execute the UCmd command
1403 *
1404 * @cmd_parameter: Pointer to command parameter
1405 * @response: Pointer to fastboot response buffer
1406 */
1407static void run_ucmd(char *cmd_parameter, char *response)
1408{
1409 if (!cmd_parameter) {
1410 pr_err("missing slot suffix\n");
1411 fastboot_fail("missing command", response);
1412 return;
1413 }
1414
1415 if (run_command(cmd_parameter, 0))
1416 fastboot_fail("", response);
1417 else
1418 fastboot_okay(NULL, response);
1419}
1420
1421static char g_a_cmd_buff[64];
1422
1423void fastboot_acmd_complete(void)
1424{
1425 run_command(g_a_cmd_buff, 0);
1426}
1427
1428/**
1429 * run_acmd() - Execute the ACmd command
1430 *
1431 * @cmd_parameter: Pointer to command parameter
1432 * @response: Pointer to fastboot response buffer
1433 */
1434static void run_acmd(char *cmd_parameter, char *response)
1435{
1436 if (!cmd_parameter) {
1437 pr_err("missing slot suffix\n");
1438 fastboot_fail("missing command", response);
1439 return;
1440 }
1441
1442 if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) {
1443 pr_err("too long command\n");
1444 fastboot_fail("too long command", response);
1445 return;
1446 }
1447
1448 strcpy(g_a_cmd_buff, cmd_parameter);
1449 fastboot_okay(NULL, response);
1450}
1451#endif
1452
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001453/**
1454 * reboot_bootloader() - Sets reboot bootloader flag.
1455 *
1456 * @cmd_parameter: Pointer to command parameter
1457 * @response: Pointer to fastboot response buffer
1458 */
1459static void reboot_bootloader(char *cmd_parameter, char *response)
1460{
Xindong Xud9441422024-01-18 10:20:45 +08001461#ifndef CONFIG_AMLOGIC_MODIFY
Roman Kovalivskyi851737a2020-07-28 23:35:32 +03001462 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER))
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001463 fastboot_fail("Cannot set reboot flag", response);
1464 else
Xindong Xud9441422024-01-18 10:20:45 +08001465#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001466 fastboot_okay(NULL, response);
1467}
Alex Kiernan3845b902018-05-29 15:30:54 +00001468
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001469/**
1470 * reboot_fastbootd() - Sets reboot fastboot flag.
1471 *
1472 * @cmd_parameter: Pointer to command parameter
1473 * @response: Pointer to fastboot response buffer
1474 */
1475static void reboot_fastbootd(char *cmd_parameter, char *response)
1476{
Xindong Xud9441422024-01-18 10:20:45 +08001477#ifndef CONFIG_AMLOGIC_MODIFY
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001478 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD))
Xindong Xud9441422024-01-18 10:20:45 +08001479 fastboot_fail("Cannot set reboot flag", response);
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001480 else
Xindong Xud9441422024-01-18 10:20:45 +08001481#endif
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001482 fastboot_okay(NULL, response);
1483}
1484
1485/**
1486 * reboot_recovery() - Sets reboot recovery flag.
1487 *
1488 * @cmd_parameter: Pointer to command parameter
1489 * @response: Pointer to fastboot response buffer
1490 */
1491static void reboot_recovery(char *cmd_parameter, char *response)
1492{
Xindong Xud9441422024-01-18 10:20:45 +08001493#ifndef CONFIG_AMLOGIC_MODIFY
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001494 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY))
Xindong Xud9441422024-01-18 10:20:45 +08001495 fastboot_fail("Cannot set reboot flag", response);
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001496 else
Xindong Xud9441422024-01-18 10:20:45 +08001497#endif
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +03001498 fastboot_okay(NULL, response);
1499}
1500
Xindong Xud9441422024-01-18 10:20:45 +08001501#ifdef CONFIG_FASTBOOT_WRITING_CMD
Alex Kiernan3845b902018-05-29 15:30:54 +00001502#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
1503/**
1504 * oem_format() - Execute the OEM format command
1505 *
1506 * @cmd_parameter: Pointer to command parameter
1507 * @response: Pointer to fastboot response buffer
1508 */
1509static void oem_format(char *cmd_parameter, char *response)
1510{
1511 char cmdbuf[32];
1512
Xindong Xud9441422024-01-18 10:20:45 +08001513#ifdef CONFIG_AMLOGIC_MODIFY
1514 if (IS_FEAT_BOOT_VERIFY()) {
1515 printf("device is secure mode, can not run this cmd.\n");
1516 fastboot_fail("secure boot device", response);
1517 return;
1518 }
1519
1520 if (check_lock() == 1) {
1521 printf("device is locked, can not run this cmd.Please flashing unlock & flashing unlock_critical\n");
1522 fastboot_fail("locked device", response);
1523 return;
1524 }
1525#endif
1526
Alex Kiernan3845b902018-05-29 15:30:54 +00001527 if (!env_get("partitions")) {
1528 fastboot_fail("partitions not set", response);
1529 } else {
1530 sprintf(cmdbuf, "gpt write mmc %x $partitions",
1531 CONFIG_FASTBOOT_FLASH_MMC_DEV);
1532 if (run_command(cmdbuf, 0))
1533 fastboot_fail("", response);
1534 else
1535 fastboot_okay(NULL, response);
1536 }
1537}
1538#endif
Patrick Delaunayb2f6b972021-01-27 14:46:48 +01001539
1540#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
1541/**
1542 * oem_partconf() - Execute the OEM partconf command
1543 *
1544 * @cmd_parameter: Pointer to command parameter
1545 * @response: Pointer to fastboot response buffer
1546 */
1547static void oem_partconf(char *cmd_parameter, char *response)
1548{
1549 char cmdbuf[32];
1550
1551 if (!cmd_parameter) {
1552 fastboot_fail("Expected command parameter", response);
1553 return;
1554 }
1555
1556 /* execute 'mmc partconfg' command with cmd_parameter arguments*/
1557 snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0",
1558 CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter);
1559 printf("Execute: %s\n", cmdbuf);
1560 if (run_command(cmdbuf, 0))
1561 fastboot_fail("Cannot set oem partconf", response);
1562 else
1563 fastboot_okay(NULL, response);
1564}
1565#endif
Patrick Delaunay0c0394b2021-01-27 14:46:49 +01001566
1567#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
1568/**
1569 * oem_bootbus() - Execute the OEM bootbus command
1570 *
1571 * @cmd_parameter: Pointer to command parameter
1572 * @response: Pointer to fastboot response buffer
1573 */
1574static void oem_bootbus(char *cmd_parameter, char *response)
1575{
1576 char cmdbuf[32];
1577
1578 if (!cmd_parameter) {
1579 fastboot_fail("Expected command parameter", response);
1580 return;
1581 }
1582
1583 /* execute 'mmc bootbus' command with cmd_parameter arguments*/
1584 snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s",
1585 CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter);
1586 printf("Execute: %s\n", cmdbuf);
1587 if (run_command(cmdbuf, 0))
1588 fastboot_fail("Cannot set oem bootbus", response);
1589 else
1590 fastboot_okay(NULL, response);
1591}
1592#endif
Xindong Xud9441422024-01-18 10:20:45 +08001593#endif