| // SPDX-License-Identifier: MIT |
| /* |
| * Copyright (c) 2016 MediaTek Inc. |
| * |
| * Permission is hereby granted, free of charge, to any person obtaining |
| * a copy of this software and associated documentation files |
| * (the "Software"), to deal in the Software without restriction, |
| * including without limitation the rights to use, copy, modify, merge, |
| * publish, distribute, sublicense, and/or sell copies of the Software, |
| * and to permit persons to whom the Software is furnished to do so, |
| * subject to the following conditions: |
| * |
| * The above copyright notice and this permission notice shall be |
| * included in all copies or substantial portions of the Software. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| |
| #include <dev/udc.h> |
| #include <kernel/timer.h> |
| #include <lib/bio.h> |
| #include <lib/dl_commands.h> |
| #include <lib/fastboot.h> |
| #include <lib/fastboot_oem_cmd.h> |
| #include <lib/mempool.h> |
| #include <platform/mtk_wdt.h> |
| #include <reg.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <target/cust_usb.h> |
| |
| #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| static timer_t wdt_timer; |
| |
| static void register_commands(void) |
| { |
| fastboot_register("getvar:", cmd_getvar); |
| fastboot_register("download:", cmd_download); |
| fastboot_register("flash:", cmd_flash); |
| fastboot_register("erase:", cmd_erase); |
| fastboot_register("reboot", cmd_reboot); |
| fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
| fastboot_register("reboot-fastboot", cmd_reboot_fastbootd); |
| fastboot_register("oem reboot-recovery", cmd_reboot_recovery); |
| fastboot_register("oem continue", cmd_continue_boot); |
| #ifdef AVB_ENABLE_DEVICE_STATE_CHANGE |
| fastboot_register("flashing", cmd_flashing); |
| #endif |
| } |
| |
| static void register_oem_fastboot_cmds(void) |
| { |
| extern const struct fastboot_oem_cmd_descriptor __fb_oem_cmd_start[]; |
| extern const struct fastboot_oem_cmd_descriptor __fb_oem_cmd_end[]; |
| |
| const struct fastboot_oem_cmd_descriptor *cmd; |
| |
| for (cmd = __fb_oem_cmd_start; cmd != __fb_oem_cmd_end; cmd++) { |
| if (cmd->cmd_handler) |
| fastboot_register(cmd->cmd_str, cmd->cmd_handler); |
| } |
| } |
| |
| static void publish_attributes(void) |
| { |
| char buffer_size[11]; |
| int n; |
| n = sprintf(buffer_size, "0x%x", SCRATCH_SIZE); |
| if (n < 0) |
| return; |
| fastboot_publish("version", "0.5"); |
| fastboot_publish("max-download-size", buffer_size); |
| fastboot_publish("is-userspace", "no"); |
| } |
| |
| static void storage_init(void) |
| { |
| /* init storage that only use in fastboot, currently none */ |
| bio_dump_devices(); |
| } |
| |
| void ext_boot(void) |
| { |
| void *scratch_buf; |
| |
| |
| storage_init(); |
| mtk_wdt_disable(); |
| timer_initialize(&wdt_timer); |
| timer_set_periodic(&wdt_timer, 5000, (timer_callback)mtk_wdt_restart, NULL); |
| register_commands(); |
| register_oem_fastboot_cmds(); |
| publish_attributes(); |
| scratch_buf = mempool_alloc(SCRATCH_SIZE, MEMPOOL_ANY); |
| if (!scratch_buf) |
| return; |
| |
| fastboot_init(scratch_buf, (unsigned long long)SCRATCH_SIZE); |
| mempool_free(scratch_buf); |
| |
| fastboot_unregister_all(); |
| } |