diff options
author | 2023-10-10 14:33:42 +0000 | |
---|---|---|
committer | 2023-10-10 14:33:42 +0000 | |
commit | af1a266670d040d2f4083ff309d732d648afba2a (patch) | |
tree | 2fc46203448ddcc6f81546d379abfaeb323575e9 /roms/u-boot/drivers/fastboot | |
parent | e02cda008591317b1625707ff8e115a4841aa889 (diff) |
Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
Diffstat (limited to 'roms/u-boot/drivers/fastboot')
-rw-r--r-- | roms/u-boot/drivers/fastboot/Kconfig | 217 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/Makefile | 7 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/fb_command.c | 516 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/fb_common.c | 185 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/fb_getvar.c | 298 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/fb_mmc.c | 704 | ||||
-rw-r--r-- | roms/u-boot/drivers/fastboot/fb_nand.c | 263 |
7 files changed, 2190 insertions, 0 deletions
diff --git a/roms/u-boot/drivers/fastboot/Kconfig b/roms/u-boot/drivers/fastboot/Kconfig new file mode 100644 index 000000000..2d1836a80 --- /dev/null +++ b/roms/u-boot/drivers/fastboot/Kconfig @@ -0,0 +1,217 @@ +menu "Fastboot support" + +config FASTBOOT + bool + imply ANDROID_BOOT_IMAGE + imply CMD_FASTBOOT + +config USB_FUNCTION_FASTBOOT + bool "Enable USB fastboot gadget" + depends on USB_GADGET + default y if ARCH_SUNXI && USB_MUSB_GADGET + select FASTBOOT + select USB_GADGET_DOWNLOAD + help + This enables the USB part of the fastboot gadget. + +config UDP_FUNCTION_FASTBOOT + depends on NET + select FASTBOOT + bool "Enable fastboot protocol over UDP" + help + This enables the fastboot protocol over UDP. + +if FASTBOOT + +config FASTBOOT_BUF_ADDR + hex "Define FASTBOOT buffer address" + default 0x82000000 if MX6SX || MX6SL || MX6UL || MX6SLL + default 0x81000000 if ARCH_OMAP2PLUS + default 0x42000000 if ARCH_SUNXI && !MACH_SUN9I + default 0x22000000 if ARCH_SUNXI && MACH_SUN9I + default 0x60800800 if ROCKCHIP_RK3036 || ROCKCHIP_RK3188 || \ + ROCKCHIP_RK322X + default 0x800800 if ROCKCHIP_RK3288 || ROCKCHIP_RK3329 || \ + ROCKCHIP_RK3399 + default 0x280000 if ROCKCHIP_RK3368 + default 0x100000 if ARCH_ZYNQMP + default 0 if SANDBOX + help + The fastboot protocol requires a large memory buffer for + downloads. Define this to the starting RAM address to use for + downloaded images. + +config FASTBOOT_BUF_SIZE + hex "Define FASTBOOT buffer size" + default 0x8000000 if ARCH_ROCKCHIP + default 0x6000000 if ARCH_ZYNQMP + default 0x2000000 if ARCH_SUNXI + default 0x8192 if SANDBOX + default 0x7000000 + help + The fastboot protocol requires a large memory buffer for + downloads. This buffer should be as large as possible for a + platform. Define this to the size available RAM for fastboot. + +config FASTBOOT_USB_DEV + int "USB controller number" + depends on USB_FUNCTION_FASTBOOT + default 0 + help + Some boards have USB OTG controller other than 0. Define this + option so it can be used in compiled environment (e.g. in + CONFIG_BOOTCOMMAND). + +config FASTBOOT_FLASH + bool "Enable FASTBOOT FLASH command" + default y if ARCH_SUNXI || ARCH_ROCKCHIP + depends on MMC || (MTD_RAW_NAND && CMD_MTDPARTS) + select IMAGE_SPARSE + help + The fastboot protocol includes a "flash" command for writing + the downloaded image to a non-volatile storage device. Define + this to enable the "fastboot flash" command. + +config FASTBOOT_UUU_SUPPORT + bool "Enable FASTBOOT i.MX UUU special command" + default n + help + The fastboot protocol includes "UCmd" and "ACmd" command. + Be aware that you provide full access to any U-Boot command, + including working with memory and may open a huge backdoor, + when enabling this option. + +choice + prompt "Flash provider for FASTBOOT" + depends on FASTBOOT_FLASH + +config FASTBOOT_FLASH_MMC + bool "FASTBOOT on MMC" + depends on MMC + +config FASTBOOT_FLASH_NAND + bool "FASTBOOT on NAND" + depends on MTD_RAW_NAND && CMD_MTDPARTS + +endchoice + +config FASTBOOT_FLASH_MMC_DEV + int "Define FASTBOOT MMC FLASH default device" + depends on FASTBOOT_FLASH_MMC + default 0 if ARCH_ROCKCHIP + default 0 if ARCH_SUNXI && MMC_SUNXI_SLOT_EXTRA = -1 + default 1 if ARCH_SUNXI && MMC_SUNXI_SLOT_EXTRA != -1 + help + The fastboot "flash" command requires additional information + regarding the non-volatile storage device. Define this to + the eMMC device that fastboot should use to store the image. + +config FASTBOOT_FLASH_NAND_TRIMFFS + bool "Skip empty pages when flashing NAND" + depends on FASTBOOT_FLASH_NAND + help + When flashing NAND enable the DROP_FFS flag to drop trailing all-0xff + pages. + +config FASTBOOT_MMC_BOOT_SUPPORT + bool "Enable EMMC_BOOT flash/erase" + depends on FASTBOOT_FLASH_MMC + help + The fastboot "flash" and "erase" commands normally does operations + on eMMC userdata. Define this to enable the special commands to + flash/erase eMMC boot partition. + The default target name for updating eMMC boot partition 1/2 is + CONFIG_FASTBOOT_MMC_BOOT1_NAME/CONFIG_FASTBOOT_MMC_BOOT2_NAME. + +config FASTBOOT_MMC_BOOT1_NAME + string "Target name for updating EMMC_BOOT1" + depends on FASTBOOT_MMC_BOOT_SUPPORT + default "mmc0boot0" + help + The fastboot "flash" and "erase" commands support operations on + EMMC_BOOT1. This occurs when the specified "EMMC_BOOT1 name" on + the "fastboot flash" and "fastboot erase" commands match the value + defined here. + The default target name for updating EMMC_BOOT1 is "mmc0boot0". + +config FASTBOOT_MMC_BOOT2_NAME + string "Target name for updating EMMC_BOOT2" + depends on FASTBOOT_MMC_BOOT_SUPPORT + default "mmc0boot1" + help + The fastboot "flash" and "erase" commands support operations on + EMMC_BOOT2. This occurs when the specified "EMMC_BOOT2 name" on + the "fastboot flash" and "fastboot erase" commands match the value + defined here. + The default target name for updating EMMC_BOOT2 is "mmc0boot1". + +config FASTBOOT_MMC_USER_SUPPORT + bool "Enable eMMC userdata partition flash/erase" + depends on FASTBOOT_FLASH_MMC + help + Define this to enable the support "flash" and "erase" command on + eMMC userdata. The "flash" command only update the MBR and GPT + header when CONFIG_EFI_PARTITION is supported. + The "erase" command erase all the userdata. + This occurs when the specified "partition name" on the + fastboot command line matches the value CONFIG_FASTBOOT_MMC_USER_NAME. + +config FASTBOOT_MMC_USER_NAME + string "Target name for updating EMMC_USER" + depends on FASTBOOT_MMC_USER_SUPPORT + default "mmc0" + help + The fastboot "flash" and "erase" command supports EMMC_USER. + This occurs when the specified "EMMC_USER name" on the + "fastboot flash" and the "fastboot erase" commands match the value + defined here. + The default target name for erasing EMMC_USER is "mmc0". + +config FASTBOOT_GPT_NAME + string "Target name for updating GPT" + depends on FASTBOOT_FLASH_MMC && EFI_PARTITION + default "gpt" + help + The fastboot "flash" command supports writing the downloaded + image to the Protective MBR and the Primary GUID Partition + Table. (Additionally, this downloaded image is post-processed + to generate and write the Backup GUID Partition Table.) + This occurs when the specified "partition name" on the + "fastboot flash" command line matches the value defined here. + The default target name for updating GPT is "gpt". + +config FASTBOOT_MBR_NAME + string "Target name for updating MBR" + depends on FASTBOOT_FLASH_MMC && DOS_PARTITION + default "mbr" + help + The fastboot "flash" command allows to write the downloaded image + to the Master Boot Record. This occurs when the "partition name" + specified on the "fastboot flash" command line matches the value + defined here. The default target name for updating MBR is "mbr". + +config FASTBOOT_CMD_OEM_FORMAT + bool "Enable the 'oem format' command" + depends on FASTBOOT_FLASH_MMC && CMD_GPT + help + Add support for the "oem format" command from a client. This + relies on the env variable partitions to contain the list of + partitions as required by the gpt command. + +config FASTBOOT_CMD_OEM_PARTCONF + bool "Enable the 'oem partconf' command" + depends on FASTBOOT_FLASH_MMC && SUPPORT_EMMC_BOOT + help + Add support for the "oem partconf" command from a client. This set + the mmc boot-partition for the selecting eMMC device. + +config FASTBOOT_CMD_OEM_BOOTBUS + bool "Enable the 'oem bootbus' command" + depends on FASTBOOT_FLASH_MMC && SUPPORT_EMMC_BOOT + help + Add support for the "oem bootbus" command from a client. This set + the mmc boot configuration for the selecting eMMC device. + +endif # FASTBOOT + +endmenu diff --git a/roms/u-boot/drivers/fastboot/Makefile b/roms/u-boot/drivers/fastboot/Makefile new file mode 100644 index 000000000..048af5aa8 --- /dev/null +++ b/roms/u-boot/drivers/fastboot/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0+ + +obj-y += fb_common.o +obj-y += fb_getvar.o +obj-y += fb_command.o +obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_mmc.o +obj-$(CONFIG_FASTBOOT_FLASH_NAND) += fb_nand.o diff --git a/roms/u-boot/drivers/fastboot/fb_command.c b/roms/u-boot/drivers/fastboot/fb_command.c new file mode 100644 index 000000000..3a5db5b08 --- /dev/null +++ b/roms/u-boot/drivers/fastboot/fb_command.c @@ -0,0 +1,516 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (C) 2016 The Android Open Source Project + */ + +#include <common.h> +#include <command.h> +#include <env.h> +#include <fastboot.h> +#include <fastboot-internal.h> +#include <fb_mmc.h> +#include <fb_nand.h> +#include <flash.h> +#include <part.h> +#include <stdlib.h> + +/** + * image_size - final fastboot image size + */ +static u32 image_size; + +/** + * fastboot_bytes_received - number of bytes received in the current download + */ +static u32 fastboot_bytes_received; + +/** + * fastboot_bytes_expected - number of bytes expected in the current download + */ +static u32 fastboot_bytes_expected; + +static void okay(char *, char *); +static void getvar(char *, char *); +static void download(char *, char *); +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +static void flash(char *, char *); +static void erase(char *, char *); +#endif +static void reboot_bootloader(char *, char *); +static void reboot_fastbootd(char *, char *); +static void reboot_recovery(char *, char *); +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT) +static void oem_format(char *, char *); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF) +static void oem_partconf(char *, char *); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS) +static void oem_bootbus(char *, char *); +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) +static void run_ucmd(char *, char *); +static void run_acmd(char *, char *); +#endif + +static const struct { + const char *command; + void (*dispatch)(char *cmd_parameter, char *response); +} commands[FASTBOOT_COMMAND_COUNT] = { + [FASTBOOT_COMMAND_GETVAR] = { + .command = "getvar", + .dispatch = getvar + }, + [FASTBOOT_COMMAND_DOWNLOAD] = { + .command = "download", + .dispatch = download + }, +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) + [FASTBOOT_COMMAND_FLASH] = { + .command = "flash", + .dispatch = flash + }, + [FASTBOOT_COMMAND_ERASE] = { + .command = "erase", + .dispatch = erase + }, +#endif + [FASTBOOT_COMMAND_BOOT] = { + .command = "boot", + .dispatch = okay + }, + [FASTBOOT_COMMAND_CONTINUE] = { + .command = "continue", + .dispatch = okay + }, + [FASTBOOT_COMMAND_REBOOT] = { + .command = "reboot", + .dispatch = okay + }, + [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = { + .command = "reboot-bootloader", + .dispatch = reboot_bootloader + }, + [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = { + .command = "reboot-fastboot", + .dispatch = reboot_fastbootd + }, + [FASTBOOT_COMMAND_REBOOT_RECOVERY] = { + .command = "reboot-recovery", + .dispatch = reboot_recovery + }, + [FASTBOOT_COMMAND_SET_ACTIVE] = { + .command = "set_active", + .dispatch = okay + }, +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT) + [FASTBOOT_COMMAND_OEM_FORMAT] = { + .command = "oem format", + .dispatch = oem_format, + }, +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF) + [FASTBOOT_COMMAND_OEM_PARTCONF] = { + .command = "oem partconf", + .dispatch = oem_partconf, + }, +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS) + [FASTBOOT_COMMAND_OEM_BOOTBUS] = { + .command = "oem bootbus", + .dispatch = oem_bootbus, + }, +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) + [FASTBOOT_COMMAND_UCMD] = { + .command = "UCmd", + .dispatch = run_ucmd, + }, + [FASTBOOT_COMMAND_ACMD] = { + .command = "ACmd", + .dispatch = run_acmd, + }, +#endif +}; + +/** + * fastboot_handle_command - Handle fastboot command + * + * @cmd_string: Pointer to command string + * @response: Pointer to fastboot response buffer + * + * Return: Executed command, or -1 if not recognized + */ +int fastboot_handle_command(char *cmd_string, char *response) +{ + int i; + char *cmd_parameter; + + cmd_parameter = cmd_string; + strsep(&cmd_parameter, ":"); + + for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) { + if (!strcmp(commands[i].command, cmd_string)) { + if (commands[i].dispatch) { + commands[i].dispatch(cmd_parameter, + response); + return i; + } else { + break; + } + } + } + + pr_err("command %s not recognized.\n", cmd_string); + fastboot_fail("unrecognized command", response); + return -1; +} + +/** + * okay() - Send bare OKAY response + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + * + * Send a bare OKAY fastboot response. This is used where the command is + * valid, but all the work is done after the response has been sent (e.g. + * boot, reboot etc.) + */ +static void okay(char *cmd_parameter, char *response) +{ + fastboot_okay(NULL, response); +} + +/** + * getvar() - Read a config/version variable + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void getvar(char *cmd_parameter, char *response) +{ + fastboot_getvar(cmd_parameter, response); +} + +/** + * fastboot_download() - Start a download transfer from the client + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void download(char *cmd_parameter, char *response) +{ + char *tmp; + + if (!cmd_parameter) { + fastboot_fail("Expected command parameter", response); + return; + } + fastboot_bytes_received = 0; + fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16); + if (fastboot_bytes_expected == 0) { + fastboot_fail("Expected nonzero image size", response); + return; + } + /* + * Nothing to download yet. Response is of the form: + * [DATA|FAIL]$cmd_parameter + * + * where cmd_parameter is an 8 digit hexadecimal number + */ + if (fastboot_bytes_expected > fastboot_buf_size) { + fastboot_fail(cmd_parameter, response); + } else { + printf("Starting download of %d bytes\n", + fastboot_bytes_expected); + fastboot_response("DATA", response, "%s", cmd_parameter); + } +} + +/** + * fastboot_data_remaining() - return bytes remaining in current transfer + * + * Return: Number of bytes left in the current download + */ +u32 fastboot_data_remaining(void) +{ + return fastboot_bytes_expected - fastboot_bytes_received; +} + +/** + * fastboot_data_download() - Copy image data to fastboot_buf_addr. + * + * @fastboot_data: Pointer to received fastboot data + * @fastboot_data_len: Length of received fastboot data + * @response: Pointer to fastboot response buffer + * + * Copies image data from fastboot_data to fastboot_buf_addr. Writes to + * response. fastboot_bytes_received is updated to indicate the number + * of bytes that have been transferred. + * + * On completion sets image_size and ${filesize} to the total size of the + * downloaded image. + */ +void fastboot_data_download(const void *fastboot_data, + unsigned int fastboot_data_len, + char *response) +{ +#define BYTES_PER_DOT 0x20000 + u32 pre_dot_num, now_dot_num; + + if (fastboot_data_len == 0 || + (fastboot_bytes_received + fastboot_data_len) > + fastboot_bytes_expected) { + fastboot_fail("Received invalid data length", + response); + return; + } + /* Download data to fastboot_buf_addr */ + memcpy(fastboot_buf_addr + fastboot_bytes_received, + fastboot_data, fastboot_data_len); + + pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT; + fastboot_bytes_received += fastboot_data_len; + now_dot_num = fastboot_bytes_received / BYTES_PER_DOT; + + if (pre_dot_num != now_dot_num) { + putc('.'); + if (!(now_dot_num % 74)) + putc('\n'); + } + *response = '\0'; +} + +/** + * fastboot_data_complete() - Mark current transfer complete + * + * @response: Pointer to fastboot response buffer + * + * Set image_size and ${filesize} to the total size of the downloaded image. + */ +void fastboot_data_complete(char *response) +{ + /* Download complete. Respond with "OKAY" */ + fastboot_okay(NULL, response); + printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received); + image_size = fastboot_bytes_received; + env_set_hex("filesize", image_size); + fastboot_bytes_expected = 0; + fastboot_bytes_received = 0; +} + +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +/** + * flash() - write the downloaded image to the indicated partition. + * + * @cmd_parameter: Pointer to partition name + * @response: Pointer to fastboot response buffer + * + * Writes the previously downloaded image to the partition indicated by + * cmd_parameter. Writes to response. + */ +static void flash(char *cmd_parameter, char *response) +{ +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) + fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size, + response); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND) + fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size, + response); +#endif +} + +/** + * erase() - erase the indicated partition. + * + * @cmd_parameter: Pointer to partition name + * @response: Pointer to fastboot response buffer + * + * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes + * to response. + */ +static void erase(char *cmd_parameter, char *response) +{ +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) + fastboot_mmc_erase(cmd_parameter, response); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND) + fastboot_nand_erase(cmd_parameter, response); +#endif +} +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT) +/** + * run_ucmd() - Execute the UCmd command + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void run_ucmd(char *cmd_parameter, char *response) +{ + if (!cmd_parameter) { + pr_err("missing slot suffix\n"); + fastboot_fail("missing command", response); + return; + } + + if (run_command(cmd_parameter, 0)) + fastboot_fail("", response); + else + fastboot_okay(NULL, response); +} + +static char g_a_cmd_buff[64]; + +void fastboot_acmd_complete(void) +{ + run_command(g_a_cmd_buff, 0); +} + +/** + * run_acmd() - Execute the ACmd command + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void run_acmd(char *cmd_parameter, char *response) +{ + if (!cmd_parameter) { + pr_err("missing slot suffix\n"); + fastboot_fail("missing command", response); + return; + } + + if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) { + pr_err("too long command\n"); + fastboot_fail("too long command", response); + return; + } + + strcpy(g_a_cmd_buff, cmd_parameter); + fastboot_okay(NULL, response); +} +#endif + +/** + * reboot_bootloader() - Sets reboot bootloader flag. + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void reboot_bootloader(char *cmd_parameter, char *response) +{ + if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER)) + fastboot_fail("Cannot set reboot flag", response); + else + fastboot_okay(NULL, response); +} + +/** + * reboot_fastbootd() - Sets reboot fastboot flag. + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void reboot_fastbootd(char *cmd_parameter, char *response) +{ + if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD)) + fastboot_fail("Cannot set fastboot flag", response); + else + fastboot_okay(NULL, response); +} + +/** + * reboot_recovery() - Sets reboot recovery flag. + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void reboot_recovery(char *cmd_parameter, char *response) +{ + if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY)) + fastboot_fail("Cannot set recovery flag", response); + else + fastboot_okay(NULL, response); +} + +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT) +/** + * oem_format() - Execute the OEM format command + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void oem_format(char *cmd_parameter, char *response) +{ + char cmdbuf[32]; + + if (!env_get("partitions")) { + fastboot_fail("partitions not set", response); + } else { + sprintf(cmdbuf, "gpt write mmc %x $partitions", + CONFIG_FASTBOOT_FLASH_MMC_DEV); + if (run_command(cmdbuf, 0)) + fastboot_fail("", response); + else + fastboot_okay(NULL, response); + } +} +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF) +/** + * oem_partconf() - Execute the OEM partconf command + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void oem_partconf(char *cmd_parameter, char *response) +{ + char cmdbuf[32]; + + if (!cmd_parameter) { + fastboot_fail("Expected command parameter", response); + return; + } + + /* execute 'mmc partconfg' command with cmd_parameter arguments*/ + snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0", + CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter); + printf("Execute: %s\n", cmdbuf); + if (run_command(cmdbuf, 0)) + fastboot_fail("Cannot set oem partconf", response); + else + fastboot_okay(NULL, response); +} +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS) +/** + * oem_bootbus() - Execute the OEM bootbus command + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + */ +static void oem_bootbus(char *cmd_parameter, char *response) +{ + char cmdbuf[32]; + + if (!cmd_parameter) { + fastboot_fail("Expected command parameter", response); + return; + } + + /* execute 'mmc bootbus' command with cmd_parameter arguments*/ + snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s", + CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter); + printf("Execute: %s\n", cmdbuf); + if (run_command(cmdbuf, 0)) + fastboot_fail("Cannot set oem bootbus", response); + else + fastboot_okay(NULL, response); +} +#endif diff --git a/roms/u-boot/drivers/fastboot/fb_common.c b/roms/u-boot/drivers/fastboot/fb_common.c new file mode 100644 index 000000000..cbcc3683c --- /dev/null +++ b/roms/u-boot/drivers/fastboot/fb_common.c @@ -0,0 +1,185 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2008 - 2009 + * Windriver, <www.windriver.com> + * Tom Rix <Tom.Rix@windriver.com> + * + * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> + * + * Copyright 2014 Linaro, Ltd. + * Rob Herring <robh@kernel.org> + */ + +#include <bcb.h> +#include <common.h> +#include <command.h> +#include <env.h> +#include <fastboot.h> +#include <net/fastboot.h> + +/** + * fastboot_buf_addr - base address of the fastboot download buffer + */ +void *fastboot_buf_addr; + +/** + * fastboot_buf_size - size of the fastboot download buffer + */ +u32 fastboot_buf_size; + +/** + * fastboot_progress_callback - callback executed during long operations + */ +void (*fastboot_progress_callback)(const char *msg); + +/** + * fastboot_response() - Writes a response of the form "$tag$reason". + * + * @tag: The first part of the response + * @response: Pointer to fastboot response buffer + * @format: printf style format string + */ +void fastboot_response(const char *tag, char *response, + const char *format, ...) +{ + va_list args; + + strlcpy(response, tag, FASTBOOT_RESPONSE_LEN); + if (format) { + va_start(args, format); + vsnprintf(response + strlen(response), + FASTBOOT_RESPONSE_LEN - strlen(response) - 1, + format, args); + va_end(args); + } +} + +/** + * fastboot_fail() - Write a FAIL response of the form "FAIL$reason". + * + * @reason: Pointer to returned reason string + * @response: Pointer to fastboot response buffer + */ +void fastboot_fail(const char *reason, char *response) +{ + fastboot_response("FAIL", response, "%s", reason); +} + +/** + * fastboot_okay() - Write an OKAY response of the form "OKAY$reason". + * + * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY" + * @response: Pointer to fastboot response buffer + */ +void fastboot_okay(const char *reason, char *response) +{ + if (reason) + fastboot_response("OKAY", response, "%s", reason); + else + fastboot_response("OKAY", response, NULL); +} + +/** + * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader + * + * Set flag which indicates that we should reboot into the bootloader + * following the reboot that fastboot executes after this function. + * + * This function should be overridden in your board file with one + * which sets whatever flag your board specific Android bootloader flow + * requires in order to re-enter the bootloader. + */ +int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) +{ +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC_DEV) + static const char * const boot_cmds[] = { + [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader", + [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot", + [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery" + }; + + if (reason >= FASTBOOT_REBOOT_REASONS_COUNT) + return -EINVAL; + + return bcb_write_reboot_reason(CONFIG_FASTBOOT_FLASH_MMC_DEV, "misc", boot_cmds[reason]); +#else + return -EINVAL; +#endif +} + +/** + * fastboot_get_progress_callback() - Return progress callback + * + * Return: Pointer to function called during long operations + */ +void (*fastboot_get_progress_callback(void))(const char *) +{ + return fastboot_progress_callback; +} + +/** + * fastboot_boot() - Execute fastboot boot command + * + * If ${fastboot_bootcmd} is set, run that command to execute the boot + * process, if that returns, then exit the fastboot server and return + * control to the caller. + * + * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset + * the board. + */ +void fastboot_boot(void) +{ + char *s; + + s = env_get("fastboot_bootcmd"); + if (s) { + run_command(s, CMD_FLAG_ENV); + } else { + static char boot_addr_start[20]; + static char *const bootm_args[] = { + "bootm", boot_addr_start, NULL + }; + + snprintf(boot_addr_start, sizeof(boot_addr_start) - 1, + "0x%p", fastboot_buf_addr); + printf("Booting kernel at %s...\n\n\n", boot_addr_start); + + do_bootm(NULL, 0, 2, bootm_args); + + /* + * This only happens if image is somehow faulty so we start + * over. We deliberately leave this policy to the invocation + * of fastbootcmd if that's what's being run + */ + do_reset(NULL, 0, 0, NULL); + } +} + +/** + * fastboot_set_progress_callback() - set progress callback + * + * @progress: Pointer to progress callback + * + * Set a callback which is invoked periodically during long running operations + * (flash and erase). This can be used (for example) by the UDP transport to + * send INFO responses to keep the client alive whilst those commands are + * executing. + */ +void fastboot_set_progress_callback(void (*progress)(const char *msg)) +{ + fastboot_progress_callback = progress; +} + +/* + * fastboot_init() - initialise new fastboot protocol session + * + * @buf_addr: Pointer to download buffer, or NULL for default + * @buf_size: Size of download buffer, or zero for default + */ +void fastboot_init(void *buf_addr, u32 buf_size) +{ + fastboot_buf_addr = buf_addr ? buf_addr : + (void *)CONFIG_FASTBOOT_BUF_ADDR; + fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE; + fastboot_set_progress_callback(NULL); +} diff --git a/roms/u-boot/drivers/fastboot/fb_getvar.c b/roms/u-boot/drivers/fastboot/fb_getvar.c new file mode 100644 index 000000000..d43f2cfee --- /dev/null +++ b/roms/u-boot/drivers/fastboot/fb_getvar.c @@ -0,0 +1,298 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (C) 2016 The Android Open Source Project + */ + +#include <common.h> +#include <env.h> +#include <fastboot.h> +#include <fastboot-internal.h> +#include <fb_mmc.h> +#include <fb_nand.h> +#include <fs.h> +#include <part.h> +#include <version.h> + +static void getvar_version(char *var_parameter, char *response); +static void getvar_version_bootloader(char *var_parameter, char *response); +static void getvar_downloadsize(char *var_parameter, char *response); +static void getvar_serialno(char *var_parameter, char *response); +static void getvar_version_baseband(char *var_parameter, char *response); +static void getvar_product(char *var_parameter, char *response); +static void getvar_platform(char *var_parameter, char *response); +static void getvar_current_slot(char *var_parameter, char *response); +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +static void getvar_has_slot(char *var_parameter, char *response); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) +static void getvar_partition_type(char *part_name, char *response); +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +static void getvar_partition_size(char *part_name, char *response); +#endif +static void getvar_is_userspace(char *var_parameter, char *response); + +static const struct { + const char *variable; + void (*dispatch)(char *var_parameter, char *response); +} getvar_dispatch[] = { + { + .variable = "version", + .dispatch = getvar_version + }, { + .variable = "version-bootloader", + .dispatch = getvar_version_bootloader + }, { + .variable = "downloadsize", + .dispatch = getvar_downloadsize + }, { + .variable = "max-download-size", + .dispatch = getvar_downloadsize + }, { + .variable = "serialno", + .dispatch = getvar_serialno + }, { + .variable = "version-baseband", + .dispatch = getvar_version_baseband + }, { + .variable = "product", + .dispatch = getvar_product + }, { + .variable = "platform", + .dispatch = getvar_platform + }, { + .variable = "current-slot", + .dispatch = getvar_current_slot +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) + }, { + .variable = "has-slot", + .dispatch = getvar_has_slot +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) + }, { + .variable = "partition-type", + .dispatch = getvar_partition_type +#endif +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) + }, { + .variable = "partition-size", + .dispatch = getvar_partition_size +#endif + }, { + .variable = "is-userspace", + .dispatch = getvar_is_userspace + } +}; + +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +/** + * Get partition number and size for any storage type. + * + * Can be used to check if partition with specified name exists. + * + * If error occurs, this function guarantees to fill @p response with fail + * string. @p response can be rewritten in caller, if needed. + * + * @param[in] part_name Info for which partition name to look for + * @param[in,out] response Pointer to fastboot response buffer + * @param[out] size If not NULL, will contain partition size + * @return Partition number or negative value on error + */ +static int getvar_get_part_info(const char *part_name, char *response, + size_t *size) +{ + int r; +# if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) + struct blk_desc *dev_desc; + struct disk_partition part_info; + + r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info, + response); + if (r >= 0 && size) + *size = part_info.size * part_info.blksz; +# elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND) + struct part_info *part_info; + + r = fastboot_nand_get_part_info(part_name, &part_info, response); + if (r >= 0 && size) + *size = part_info->size; +# else + fastboot_fail("this storage is not supported in bootloader", response); + r = -ENODEV; +# endif + + return r; +} +#endif + +static void getvar_version(char *var_parameter, char *response) +{ + fastboot_okay(FASTBOOT_VERSION, response); +} + +static void getvar_version_bootloader(char *var_parameter, char *response) +{ + fastboot_okay(U_BOOT_VERSION, response); +} + +static void getvar_downloadsize(char *var_parameter, char *response) +{ + fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size); +} + +static void getvar_serialno(char *var_parameter, char *response) +{ + const char *tmp = env_get("serial#"); + + if (tmp) + fastboot_okay(tmp, response); + else + fastboot_fail("Value not set", response); +} + +static void getvar_version_baseband(char *var_parameter, char *response) +{ + fastboot_okay("N/A", response); +} + +static void getvar_product(char *var_parameter, char *response) +{ + const char *board = env_get("board"); + + if (board) + fastboot_okay(board, response); + else + fastboot_fail("Board not set", response); +} + +static void getvar_platform(char *var_parameter, char *response) +{ + const char *p = env_get("platform"); + + if (p) + fastboot_okay(p, response); + else + fastboot_fail("platform not set", response); +} + +static void getvar_current_slot(char *var_parameter, char *response) +{ + /* A/B not implemented, for now always return "a" */ + fastboot_okay("a", response); +} + +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +static void getvar_has_slot(char *part_name, char *response) +{ + char part_name_wslot[PART_NAME_LEN]; + size_t len; + int r; + + if (!part_name || part_name[0] == '\0') + goto fail; + + /* part_name_wslot = part_name + "_a" */ + len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3); + if (len > PART_NAME_LEN - 3) + goto fail; + strcat(part_name_wslot, "_a"); + + r = getvar_get_part_info(part_name_wslot, response, NULL); + if (r >= 0) { + fastboot_okay("yes", response); /* part exists and slotted */ + return; + } + + r = getvar_get_part_info(part_name, response, NULL); + if (r >= 0) + fastboot_okay("no", response); /* part exists but not slotted */ + + /* At this point response is filled with okay or fail string */ + return; + +fail: + fastboot_fail("invalid partition name", response); +} +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC) +static void getvar_partition_type(char *part_name, char *response) +{ + int r; + struct blk_desc *dev_desc; + struct disk_partition part_info; + + r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info, + response); + if (r >= 0) { + r = fs_set_blk_dev_with_part(dev_desc, r); + if (r < 0) + fastboot_fail("failed to set partition", response); + else + fastboot_okay(fs_get_type_name(), response); + } +} +#endif + +#if CONFIG_IS_ENABLED(FASTBOOT_FLASH) +static void getvar_partition_size(char *part_name, char *response) +{ + int r; + size_t size; + + r = getvar_get_part_info(part_name, response, &size); + if (r >= 0) + fastboot_response("OKAY", response, "0x%016zx", size); +} +#endif + +static void getvar_is_userspace(char *var_parameter, char *response) +{ + fastboot_okay("no", response); +} + +/** + * fastboot_getvar() - Writes variable indicated by cmd_parameter to response. + * + * @cmd_parameter: Pointer to command parameter + * @response: Pointer to fastboot response buffer + * + * Look up cmd_parameter first as an environment variable of the form + * fastboot.<cmd_parameter>, if that exists return use its value to set + * response. + * + * Otherwise lookup the name of variable and execute the appropriate + * function to return the requested value. + */ +void fastboot_getvar(char *cmd_parameter, char *response) +{ + if (!cmd_parameter) { + fastboot_fail("missing var", response); + } else { +#define FASTBOOT_ENV_PREFIX "fastboot." + int i; + char *var_parameter = cmd_parameter; + char envstr[FASTBOOT_RESPONSE_LEN]; + const char *s; + + snprintf(envstr, sizeof(envstr) - 1, + FASTBOOT_ENV_PREFIX "%s", cmd_parameter); + s = env_get(envstr); + if (s) { + fastboot_response("OKAY", response, "%s", s); + return; + } + + strsep(&var_parameter, ":"); + for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) { + if (!strcmp(getvar_dispatch[i].variable, + cmd_parameter)) { + getvar_dispatch[i].dispatch(var_parameter, + response); + return; + } + } + pr_warn("WARNING: unknown variable: %s\n", cmd_parameter); + fastboot_fail("Variable not implemented", response); + } +} diff --git a/roms/u-boot/drivers/fastboot/fb_mmc.c b/roms/u-boot/drivers/fastboot/fb_mmc.c new file mode 100644 index 000000000..2f3837e55 --- /dev/null +++ b/roms/u-boot/drivers/fastboot/fb_mmc.c @@ -0,0 +1,704 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2014 Broadcom Corporation. + */ + +#include <config.h> +#include <common.h> +#include <blk.h> +#include <env.h> +#include <fastboot.h> +#include <fastboot-internal.h> +#include <fb_mmc.h> +#include <flash.h> +#include <image-sparse.h> +#include <image.h> +#include <log.h> +#include <part.h> +#include <mmc.h> +#include <div64.h> +#include <linux/compat.h> +#include <android_image.h> + +#define FASTBOOT_MAX_BLK_WRITE 16384 + +#define BOOT_PARTITION_NAME "boot" + +struct fb_mmc_sparse { + struct blk_desc *dev_desc; +}; + +static int raw_part_get_info_by_name(struct blk_desc *dev_desc, + const char *name, + struct disk_partition *info) +{ + /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */ + char env_desc_name[23 + PART_NAME_LEN + 1]; + char *raw_part_desc; + const char *argv[2]; + const char **parg = argv; + + /* check for raw partition descriptor */ + strcpy(env_desc_name, "fastboot_raw_partition_"); + strlcat(env_desc_name, name, PART_NAME_LEN); + raw_part_desc = strdup(env_get(env_desc_name)); + if (raw_part_desc == NULL) + return -ENODEV; + + /* + * parse partition descriptor + * + * <lba_start> <lba_size> [mmcpart <num>] + */ + for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { + *parg = strsep(&raw_part_desc, " "); + if (*parg == NULL) { + pr_err("Invalid number of arguments.\n"); + return -ENODEV; + } + } + + info->start = simple_strtoul(argv[0], NULL, 0); + info->size = simple_strtoul(argv[1], NULL, 0); + info->blksz = dev_desc->blksz; + strlcpy((char *)info->name, name, PART_NAME_LEN); + + if (raw_part_desc) { + if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) { + ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0); + int ret = blk_dselect_hwpart(dev_desc, mmcpart); + + if (ret) + return ret; + } + } + + return 0; +} + +static int do_get_part_info(struct blk_desc **dev_desc, const char *name, + struct disk_partition *info) +{ + int ret; + + /* First try partition names on the default device */ + *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); + if (*dev_desc) { + ret = part_get_info_by_name(*dev_desc, name, info); + if (ret >= 0) + return ret; + + /* Then try raw partitions */ + ret = raw_part_get_info_by_name(*dev_desc, name, info); + if (ret >= 0) + return ret; + } + + /* Then try dev.hwpart:part */ + ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc, + info, true); + return ret; +} + +static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc, + const char *name, + struct disk_partition *info) +{ + int ret; + + ret = do_get_part_info(dev_desc, name, info); + if (ret < 0) { + /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */ + char env_alias_name[25 + PART_NAME_LEN + 1]; + char *aliased_part_name; + + /* check for alias */ + strcpy(env_alias_name, "fastboot_partition_alias_"); + strlcat(env_alias_name, name, PART_NAME_LEN); + aliased_part_name = env_get(env_alias_name); + if (aliased_part_name != NULL) + ret = do_get_part_info(dev_desc, aliased_part_name, + info); + } + return ret; +} + +/** + * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE + * + * @block_dev: Pointer to block device + * @start: First block to write/erase + * @blkcnt: Count of blocks + * @buffer: Pointer to data buffer for write or NULL for erase + */ +static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start, + lbaint_t blkcnt, const void *buffer) +{ + lbaint_t blk = start; + lbaint_t blks_written; + lbaint_t cur_blkcnt; + lbaint_t blks = 0; + int i; + + for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) { + cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE); + if (buffer) { + if (fastboot_progress_callback) + fastboot_progress_callback("writing"); + blks_written = blk_dwrite(block_dev, blk, cur_blkcnt, + buffer + (i * block_dev->blksz)); + } else { + if (fastboot_progress_callback) + fastboot_progress_callback("erasing"); + blks_written = blk_derase(block_dev, blk, cur_blkcnt); + } + blk += blks_written; + blks += blks_written; + } + return blks; +} + +static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt, const void *buffer) +{ + struct fb_mmc_sparse *sparse = info->priv; + struct blk_desc *dev_desc = sparse->dev_desc; + + return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer); +} + +static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt) +{ + return blkcnt; +} + +static void write_raw_image(struct blk_desc *dev_desc, + struct disk_partition *info, const char *part_name, + void *buffer, u32 download_bytes, char *response) +{ + lbaint_t blkcnt; + lbaint_t blks; + + /* determine number of blocks to write */ + blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); + blkcnt = lldiv(blkcnt, info->blksz); + + if (blkcnt > info->size) { + pr_err("too large for partition: '%s'\n", part_name); + fastboot_fail("too large for partition", response); + return; + } + + puts("Flashing Raw Image\n"); + + blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer); + + if (blks != blkcnt) { + pr_err("failed writing to device %d\n", dev_desc->devnum); + fastboot_fail("failed writing to device", response); + return; + } + + printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, + part_name); + fastboot_okay(NULL, response); +} + +#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \ + defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT) +static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc) +{ + lbaint_t blks; + + debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart); + + blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL); + + if (blks != dev_desc->lba) { + pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart); + return 1; + } + + printf("........ erased %lu bytes from mmc hwpart[%u]\n", + dev_desc->lba * dev_desc->blksz, dev_desc->hwpart); + + return 0; +} +#endif + +#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT +static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, + int hwpart, u32 buff_sz, char *response) +{ + lbaint_t blkcnt; + lbaint_t blks; + unsigned long blksz; + + // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart + if (blk_dselect_hwpart(dev_desc, hwpart)) { + pr_err("Failed to select hwpart\n"); + fastboot_fail("Failed to select hwpart", response); + return; + } + + if (buffer) { /* flash */ + + /* determine number of blocks to write */ + blksz = dev_desc->blksz; + blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1)); + blkcnt = lldiv(blkcnt, blksz); + + if (blkcnt > dev_desc->lba) { + pr_err("Image size too large\n"); + fastboot_fail("Image size too large", response); + return; + } + + debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart); + + blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer); + + if (blks != blkcnt) { + pr_err("Failed to write EMMC_BOOT%d\n", hwpart); + fastboot_fail("Failed to write EMMC_BOOT part", + response); + return; + } + + printf("........ wrote %lu bytes to EMMC_BOOT%d\n", + blkcnt * blksz, hwpart); + } else { /* erase */ + if (fb_mmc_erase_mmc_hwpart(dev_desc)) { + pr_err("Failed to erase EMMC_BOOT%d\n", hwpart); + fastboot_fail("Failed to erase EMMC_BOOT part", + response); + return; + } + } + + fastboot_okay(NULL, response); +} +#endif + +#ifdef CONFIG_ANDROID_BOOT_IMAGE +/** + * Read Android boot image header from boot partition. + * + * @param[in] dev_desc MMC device descriptor + * @param[in] info Boot partition info + * @param[out] hdr Where to store read boot image header + * + * @return Boot image header sectors count or 0 on error + */ +static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc, + struct disk_partition *info, + struct andr_img_hdr *hdr, + char *response) +{ + ulong sector_size; /* boot partition sector size */ + lbaint_t hdr_sectors; /* boot image header sectors count */ + int res; + + /* Calculate boot image sectors count */ + sector_size = info->blksz; + hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size); + if (hdr_sectors == 0) { + pr_err("invalid number of boot sectors: 0\n"); + fastboot_fail("invalid number of boot sectors: 0", response); + return 0; + } + + /* Read the boot image header */ + res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr); + if (res != hdr_sectors) { + pr_err("cannot read header from boot partition\n"); + fastboot_fail("cannot read header from boot partition", + response); + return 0; + } + + /* Check boot header magic string */ + res = android_image_check_header(hdr); + if (res != 0) { + pr_err("bad boot image magic\n"); + fastboot_fail("boot partition not initialized", response); + return 0; + } + + return hdr_sectors; +} + +/** + * Write downloaded zImage to boot partition and repack it properly. + * + * @param dev_desc MMC device descriptor + * @param download_buffer Address to fastboot buffer with zImage in it + * @param download_bytes Size of fastboot buffer, in bytes + * + * @return 0 on success or -1 on error + */ +static int fb_mmc_update_zimage(struct blk_desc *dev_desc, + void *download_buffer, + u32 download_bytes, + char *response) +{ + uintptr_t hdr_addr; /* boot image header address */ + struct andr_img_hdr *hdr; /* boot image header */ + lbaint_t hdr_sectors; /* boot image header sectors */ + u8 *ramdisk_buffer; + u32 ramdisk_sector_start; + u32 ramdisk_sectors; + u32 kernel_sector_start; + u32 kernel_sectors; + u32 sectors_per_page; + struct disk_partition info; + int res; + + puts("Flashing zImage\n"); + + /* Get boot partition info */ + res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info); + if (res < 0) { + pr_err("cannot find boot partition\n"); + fastboot_fail("cannot find boot partition", response); + return -1; + } + + /* Put boot image header in fastboot buffer after downloaded zImage */ + hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE); + hdr = (struct andr_img_hdr *)hdr_addr; + + /* Read boot image header */ + hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response); + if (hdr_sectors == 0) { + pr_err("unable to read boot image header\n"); + fastboot_fail("unable to read boot image header", response); + return -1; + } + + /* Check if boot image has second stage in it (we don't support it) */ + if (hdr->second_size > 0) { + pr_err("moving second stage is not supported yet\n"); + fastboot_fail("moving second stage is not supported yet", + response); + return -1; + } + + /* Extract ramdisk location */ + sectors_per_page = hdr->page_size / info.blksz; + ramdisk_sector_start = info.start + sectors_per_page; + ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * + sectors_per_page; + ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) * + sectors_per_page; + + /* Read ramdisk and put it in fastboot buffer after boot image header */ + ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz); + res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors, + ramdisk_buffer); + if (res != ramdisk_sectors) { + pr_err("cannot read ramdisk from boot partition\n"); + fastboot_fail("cannot read ramdisk from boot partition", + response); + return -1; + } + + /* Write new kernel size to boot image header */ + hdr->kernel_size = download_bytes; + res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr); + if (res == 0) { + pr_err("cannot writeback boot image header\n"); + fastboot_fail("cannot write back boot image header", response); + return -1; + } + + /* Write the new downloaded kernel */ + kernel_sector_start = info.start + sectors_per_page; + kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * + sectors_per_page; + res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors, + download_buffer); + if (res == 0) { + pr_err("cannot write new kernel\n"); + fastboot_fail("cannot write new kernel", response); + return -1; + } + + /* Write the saved ramdisk back */ + ramdisk_sector_start = info.start + sectors_per_page; + ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * + sectors_per_page; + res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors, + ramdisk_buffer); + if (res == 0) { + pr_err("cannot write back original ramdisk\n"); + fastboot_fail("cannot write back original ramdisk", response); + return -1; + } + + puts("........ zImage was updated in boot partition\n"); + fastboot_okay(NULL, response); + return 0; +} +#endif + +/** + * fastboot_mmc_get_part_info() - Lookup eMMC partion by name + * + * @part_name: Named partition to lookup + * @dev_desc: Pointer to returned blk_desc pointer + * @part_info: Pointer to returned struct disk_partition + * @response: Pointer to fastboot response buffer + */ +int fastboot_mmc_get_part_info(const char *part_name, + struct blk_desc **dev_desc, + struct disk_partition *part_info, char *response) +{ + int ret; + + if (!part_name || !strcmp(part_name, "")) { + fastboot_fail("partition not given", response); + return -ENOENT; + } + + ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info); + if (ret < 0) { + switch (ret) { + case -ENOSYS: + case -EINVAL: + fastboot_fail("invalid partition or device", response); + break; + case -ENODEV: + fastboot_fail("no such device", response); + break; + case -ENOENT: + fastboot_fail("no such partition", response); + break; + case -EPROTONOSUPPORT: + fastboot_fail("unknown partition table type", response); + break; + default: + fastboot_fail("unanticipated error", response); + break; + } + } + + return ret; +} + +static struct blk_desc *fastboot_mmc_get_dev(char *response) +{ + struct blk_desc *ret = blk_get_dev("mmc", + CONFIG_FASTBOOT_FLASH_MMC_DEV); + + if (!ret || ret->type == DEV_TYPE_UNKNOWN) { + pr_err("invalid mmc device\n"); + fastboot_fail("invalid mmc device", response); + return NULL; + } + return ret; +} + +/** + * fastboot_mmc_flash_write() - Write image to eMMC for fastboot + * + * @cmd: Named partition to write image to + * @download_buffer: Pointer to image data + * @download_bytes: Size of image data + * @response: Pointer to fastboot response buffer + */ +void fastboot_mmc_flash_write(const char *cmd, void *download_buffer, + u32 download_bytes, char *response) +{ + struct blk_desc *dev_desc; + struct disk_partition info; + +#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT + if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { + dev_desc = fastboot_mmc_get_dev(response); + if (dev_desc) + fb_mmc_boot_ops(dev_desc, download_buffer, 1, + download_bytes, response); + return; + } + if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { + dev_desc = fastboot_mmc_get_dev(response); + if (dev_desc) + fb_mmc_boot_ops(dev_desc, download_buffer, 1, + download_bytes, response); + return; + } +#endif + +#if CONFIG_IS_ENABLED(EFI_PARTITION) +#ifndef CONFIG_FASTBOOT_MMC_USER_SUPPORT + if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { +#else + if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0 || + strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { +#endif + dev_desc = fastboot_mmc_get_dev(response); + if (!dev_desc) + return; + + printf("%s: updating MBR, Primary and Backup GPT(s)\n", + __func__); + if (is_valid_gpt_buf(dev_desc, download_buffer)) { + printf("%s: invalid GPT - refusing to write to flash\n", + __func__); + fastboot_fail("invalid GPT partition", response); + return; + } + if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { + printf("%s: writing GPT partitions failed\n", __func__); + fastboot_fail("writing GPT partitions failed", + response); + return; + } + part_init(dev_desc); + printf("........ success\n"); + fastboot_okay(NULL, response); + return; + } +#endif + +#if CONFIG_IS_ENABLED(DOS_PARTITION) + if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) { + dev_desc = fastboot_mmc_get_dev(response); + if (!dev_desc) + return; + + printf("%s: updating MBR\n", __func__); + if (is_valid_dos_buf(download_buffer)) { + printf("%s: invalid MBR - refusing to write to flash\n", + __func__); + fastboot_fail("invalid MBR partition", response); + return; + } + if (write_mbr_sector(dev_desc, download_buffer)) { + printf("%s: writing MBR partition failed\n", __func__); + fastboot_fail("writing MBR partition failed", + response); + return; + } + part_init(dev_desc); + printf("........ success\n"); + fastboot_okay(NULL, response); + return; + } +#endif + +#ifdef CONFIG_ANDROID_BOOT_IMAGE + if (strncasecmp(cmd, "zimage", 6) == 0) { + dev_desc = fastboot_mmc_get_dev(response); + if (dev_desc) + fb_mmc_update_zimage(dev_desc, download_buffer, + download_bytes, response); + return; + } +#endif + + if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) + return; + + if (is_sparse_image(download_buffer)) { + struct fb_mmc_sparse sparse_priv; + struct sparse_storage sparse; + int err; + + sparse_priv.dev_desc = dev_desc; + + sparse.blksz = info.blksz; + sparse.start = info.start; + sparse.size = info.size; + sparse.write = fb_mmc_sparse_write; + sparse.reserve = fb_mmc_sparse_reserve; + sparse.mssg = fastboot_fail; + + printf("Flashing sparse image at offset " LBAFU "\n", + sparse.start); + + sparse.priv = &sparse_priv; + err = write_sparse_image(&sparse, cmd, download_buffer, + response); + if (!err) + fastboot_okay(NULL, response); + } else { + write_raw_image(dev_desc, &info, cmd, download_buffer, + download_bytes, response); + } +} + +/** + * fastboot_mmc_flash_erase() - Erase eMMC for fastboot + * + * @cmd: Named partition to erase + * @response: Pointer to fastboot response buffer + */ +void fastboot_mmc_erase(const char *cmd, char *response) +{ + struct blk_desc *dev_desc; + struct disk_partition info; + lbaint_t blks, blks_start, blks_size, grp_size; + struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); + +#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT + if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { + /* erase EMMC boot1 */ + dev_desc = fastboot_mmc_get_dev(response); + if (dev_desc) + fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response); + return; + } + if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { + /* erase EMMC boot2 */ + dev_desc = fastboot_mmc_get_dev(response); + if (dev_desc) + fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response); + return; + } +#endif + +#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT + if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { + /* erase EMMC userdata */ + dev_desc = fastboot_mmc_get_dev(response); + if (!dev_desc) + return; + + if (fb_mmc_erase_mmc_hwpart(dev_desc)) + fastboot_fail("Failed to erase EMMC_USER", response); + else + fastboot_okay(NULL, response); + return; + } +#endif + + if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) + return; + + /* Align blocks to erase group size to avoid erasing other partitions */ + grp_size = mmc->erase_grp_size; + blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); + if (info.size >= grp_size) + blks_size = (info.size - (blks_start - info.start)) & + (~(grp_size - 1)); + else + blks_size = 0; + + printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", + blks_start, blks_start + blks_size); + + blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL); + + if (blks != blks_size) { + pr_err("failed erasing from device %d\n", dev_desc->devnum); + fastboot_fail("failed erasing from device", response); + return; + } + + printf("........ erased " LBAFU " bytes from '%s'\n", + blks_size * info.blksz, cmd); + fastboot_okay(NULL, response); +} diff --git a/roms/u-boot/drivers/fastboot/fb_nand.c b/roms/u-boot/drivers/fastboot/fb_nand.c new file mode 100644 index 000000000..eb8a36f29 --- /dev/null +++ b/roms/u-boot/drivers/fastboot/fb_nand.c @@ -0,0 +1,263 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2014 Broadcom Corporation. + * Copyright 2015 Free Electrons. + */ + +#include <config.h> +#include <common.h> +#include <blk.h> +#include <flash.h> + +#include <fastboot.h> +#include <image-sparse.h> + +#include <linux/mtd/mtd.h> +#include <jffs2/jffs2.h> +#include <nand.h> + +struct fb_nand_sparse { + struct mtd_info *mtd; + struct part_info *part; +}; + +__weak int board_fastboot_erase_partition_setup(char *name) +{ + return 0; +} + +__weak int board_fastboot_write_partition_setup(char *name) +{ + return 0; +} + +static int fb_nand_lookup(const char *partname, + struct mtd_info **mtd, + struct part_info **part, + char *response) +{ + struct mtd_device *dev; + int ret; + u8 pnum; + + ret = mtdparts_init(); + if (ret) { + pr_err("Cannot initialize MTD partitions\n"); + fastboot_fail("cannot init mtdparts", response); + return ret; + } + + ret = find_dev_and_part(partname, &dev, &pnum, part); + if (ret) { + pr_err("cannot find partition: '%s'", partname); + fastboot_fail("cannot find partition", response); + return ret; + } + + if (dev->id->type != MTD_DEV_TYPE_NAND) { + pr_err("partition '%s' is not stored on a NAND device", + partname); + fastboot_fail("not a NAND device", response); + return -EINVAL; + } + + *mtd = get_nand_dev_by_index(dev->id->num); + + return 0; +} + +static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part) +{ + nand_erase_options_t opts; + int ret; + + memset(&opts, 0, sizeof(opts)); + opts.offset = part->offset; + opts.length = part->size; + opts.quiet = 1; + + printf("Erasing blocks 0x%llx to 0x%llx\n", + part->offset, part->offset + part->size); + + ret = nand_erase_opts(mtd, &opts); + if (ret) + return ret; + + printf("........ erased 0x%llx bytes from '%s'\n", + part->size, part->name); + + return 0; +} + +static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part, + void *buffer, u32 offset, + size_t length, size_t *written) +{ + int flags = WITH_WR_VERIFY; + +#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS + flags |= WITH_DROP_FFS; +#endif + + return nand_write_skip_bad(mtd, offset, &length, written, + part->size - (offset - part->offset), + buffer, flags); +} + +static lbaint_t fb_nand_sparse_write(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt, const void *buffer) +{ + struct fb_nand_sparse *sparse = info->priv; + size_t written; + int ret; + + ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer, + blk * info->blksz, + blkcnt * info->blksz, &written); + if (ret < 0) { + printf("Failed to write sparse chunk\n"); + return ret; + } + +/* TODO - verify that the value "written" includes the "bad-blocks" ... */ + + /* + * the return value must be 'blkcnt' ("good-blocks") plus the + * number of "bad-blocks" encountered within this space... + */ + return written / info->blksz; +} + +static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt) +{ + int bad_blocks = 0; + +/* + * TODO - implement a function to determine the total number + * of blocks which must be used in order to reserve the specified + * number ("blkcnt") of "good-blocks", starting at "blk"... + * ( possibly something like the "check_skip_len()" function ) + */ + + /* + * the return value must be 'blkcnt' ("good-blocks") plus the + * number of "bad-blocks" encountered within this space... + */ + return blkcnt + bad_blocks; +} + +/** + * fastboot_nand_get_part_info() - Lookup NAND partion by name + * + * @part_name: Named device to lookup + * @part_info: Pointer to returned part_info pointer + * @response: Pointer to fastboot response buffer + */ +int fastboot_nand_get_part_info(const char *part_name, + struct part_info **part_info, char *response) +{ + struct mtd_info *mtd = NULL; + + return fb_nand_lookup(part_name, &mtd, part_info, response); +} + +/** + * fastboot_nand_flash_write() - Write image to NAND for fastboot + * + * @cmd: Named device to write image to + * @download_buffer: Pointer to image data + * @download_bytes: Size of image data + * @response: Pointer to fastboot response buffer + */ +void fastboot_nand_flash_write(const char *cmd, void *download_buffer, + u32 download_bytes, char *response) +{ + struct part_info *part; + struct mtd_info *mtd = NULL; + int ret; + + ret = fb_nand_lookup(cmd, &mtd, &part, response); + if (ret) { + pr_err("invalid NAND device"); + fastboot_fail("invalid NAND device", response); + return; + } + + ret = board_fastboot_write_partition_setup(part->name); + if (ret) + return; + + if (is_sparse_image(download_buffer)) { + struct fb_nand_sparse sparse_priv; + struct sparse_storage sparse; + + sparse_priv.mtd = mtd; + sparse_priv.part = part; + + sparse.blksz = mtd->writesize; + sparse.start = part->offset / sparse.blksz; + sparse.size = part->size / sparse.blksz; + sparse.write = fb_nand_sparse_write; + sparse.reserve = fb_nand_sparse_reserve; + sparse.mssg = fastboot_fail; + + printf("Flashing sparse image at offset " LBAFU "\n", + sparse.start); + + sparse.priv = &sparse_priv; + ret = write_sparse_image(&sparse, cmd, download_buffer, + response); + if (!ret) + fastboot_okay(NULL, response); + } else { + printf("Flashing raw image at offset 0x%llx\n", + part->offset); + + ret = _fb_nand_write(mtd, part, download_buffer, part->offset, + download_bytes, NULL); + + printf("........ wrote %u bytes to '%s'\n", + download_bytes, part->name); + } + + if (ret) { + fastboot_fail("error writing the image", response); + return; + } + + fastboot_okay(NULL, response); +} + +/** + * fastboot_nand_flash_erase() - Erase NAND for fastboot + * + * @cmd: Named device to erase + * @response: Pointer to fastboot response buffer + */ +void fastboot_nand_erase(const char *cmd, char *response) +{ + struct part_info *part; + struct mtd_info *mtd = NULL; + int ret; + + ret = fb_nand_lookup(cmd, &mtd, &part, response); + if (ret) { + pr_err("invalid NAND device"); + fastboot_fail("invalid NAND device", response); + return; + } + + ret = board_fastboot_erase_partition_setup(part->name); + if (ret) + return; + + ret = _fb_nand_erase(mtd, part); + if (ret) { + pr_err("failed erasing from device %s", mtd->name); + fastboot_fail("failed erasing from device", response); + return; + } + + fastboot_okay(NULL, response); +} |