Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
jianzhou | 894cb18 | 2019-09-16 16:54:51 +0800 | [diff] [blame] | 5 | * Copyright (c) 2015-2018, 2020, The Linux Foundation. All rights reserved. |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following |
| 14 | * disclaimer in the documentation and/or other materials provided |
| 15 | * with the distribution. |
| 16 | * * Neither the name of The Linux Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #ifndef __FASTBOOT_CMDS_H__ |
| 34 | #define __FASTBOOT_CMDS_H__ |
| 35 | |
| 36 | #include <Library/BaseLib.h> |
| 37 | #include <Library/DebugLib.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 38 | #include <Library/LinuxLoaderLib.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 39 | #include <Library/MemoryAllocationLib.h> |
Vijay Kumar Pendoti | c3e0a4f | 2016-08-27 03:31:40 +0530 | [diff] [blame] | 40 | #include <Library/PartitionTableUpdate.h> |
jianzhou | 894cb18 | 2019-09-16 16:54:51 +0800 | [diff] [blame] | 41 | #include <Protocol/EFIKernelInterface.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 42 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 43 | #define ENDPOINT_IN 0x01 |
| 44 | #define ENDPOINT_OUT 0x81 |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 45 | |
lijuang | cf68954 | 2018-06-05 10:25:29 +0800 | [diff] [blame] | 46 | #define MAX_WRITE_SIZE (1024 * 1024) |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 47 | #define MAX_RSP_SIZE 64 |
| 48 | #define ERASE_BUFF_SIZE 256 * 1024 |
| 49 | #define ERASE_BUFF_BLOCKS 256 * 2 |
| 50 | #define USB_BUFFER_SIZE 1024 * 1024 * 16 |
| 51 | #define VERSION_STR_LEN 96 |
| 52 | #define FASTBOOT_STRING_MAX_LENGTH 256 |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 53 | #define FASTBOOT_COMMAND_MAX_LENGTH 64 |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 54 | #define MAX_GET_VAR_NAME_SIZE 32 |
| 55 | #define SIGACTUAL 4096 |
Vijay Kumar Pendoti | c3e0a4f | 2016-08-27 03:31:40 +0530 | [diff] [blame] | 56 | #define SLOT_SUFFIX_ARRAY_SIZE 10 |
| 57 | #define SLOT_ATTR_SIZE 32 |
| 58 | #define ATTR_RESP_SIZE 4 |
Vijay Kumar Pendoti | 39a1fc5 | 2016-10-05 17:36:14 +0530 | [diff] [blame] | 59 | #define MAX_FASTBOOT_COMMAND_SIZE 64 |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 60 | #define RECOVERY_WIPE_DATA \ |
| 61 | "recovery\n--wipe_data\n--reason=MasterClearConfirm\n--locale=en_US\n" |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 62 | |
Mayank Grover | a2d6d01 | 2018-04-25 12:37:39 +0530 | [diff] [blame] | 63 | /* Fs detection macros and definitions */ |
| 64 | #define RAW_FS_STR "raw" |
Mayank Grover | c904eba | 2018-05-02 17:39:23 +0530 | [diff] [blame] | 65 | #define EXT_FS_STR "ext4" |
| 66 | #define F2FS_FS_STR "f2fs" |
| 67 | #define FS_SUPERBLOCK_OFFSET 0x400 |
| 68 | #define EXT_MAGIC_OFFSET_SB 0x38 |
| 69 | #define EXT_FS_MAGIC 0xEF53 |
| 70 | #define F2FS_MAGIC_OFFSET_SB 0x0 |
| 71 | #define F2FS_FS_MAGIC 0xF2F52010 |
| 72 | |
Mayank Grover | e7c9307 | 2018-11-27 16:05:54 +0530 | [diff] [blame] | 73 | /* Divide allocatable free Memory to 3/4th */ |
| 74 | #define EFI_FREE_MEM_DIVISOR(BYTES) (((BYTES) * 3) / 4) |
| 75 | /* 64MB */ |
| 76 | #define MIN_BUFFER_SIZE (67108864) |
| 77 | /* 1.5GB */ |
| 78 | #define MAX_BUFFER_SIZE (1610612736) |
| 79 | |
Mayank Grover | c904eba | 2018-05-02 17:39:23 +0530 | [diff] [blame] | 80 | typedef enum FsSignature { |
| 81 | EXT_FS_SIGNATURE = 1, |
| 82 | F2FS_FS_SIGNATURE, |
| 83 | UNKNOWN_FS_SIGNATURE |
| 84 | } FS_SIGNATURE; |
Mayank Grover | a2d6d01 | 2018-04-25 12:37:39 +0530 | [diff] [blame] | 85 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 86 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
| 87 | |
| 88 | /* Fastboot Command descriptor */ |
| 89 | struct FastbootCmdDesc { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 90 | CHAR8 *name; |
| 91 | fastboot_cmd_fn cb; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* Fastboot Variable list */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 95 | typedef struct _FASTBOOT_VAR { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 96 | struct _FASTBOOT_VAR *next; |
| 97 | CONST CHAR8 *name; |
| 98 | CONST CHAR8 *value; |
| 99 | } FASTBOOT_VAR; |
| 100 | |
| 101 | /* Partition info fastboot variable */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 102 | struct GetVarPartitionInfo { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 103 | const CHAR8 part_name[MAX_GET_VAR_NAME_SIZE]; |
| 104 | CHAR8 getvar_size_str[MAX_GET_VAR_NAME_SIZE]; |
| 105 | CHAR8 getvar_type_str[MAX_GET_VAR_NAME_SIZE]; |
| 106 | CHAR8 size_response[MAX_RSP_SIZE]; |
| 107 | CHAR8 type_response[MAX_RSP_SIZE]; |
| 108 | }; |
| 109 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 110 | /* Fastboot State */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 111 | typedef enum { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 112 | ExpectCmdState, |
| 113 | ExpectDataState, |
| 114 | FastbootStateMax |
| 115 | } ANDROID_FASTBOOT_STATE; |
| 116 | |
| 117 | /* Data structure to store the command list */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 118 | typedef struct _FASTBOOT_CMD { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 119 | struct _FASTBOOT_CMD *next; |
| 120 | CONST CHAR8 *prefix; |
| 121 | UINT32 prefix_len; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 122 | VOID (*handle) (CONST CHAR8 *arg, VOID *data, UINT32 sz); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 123 | } FASTBOOT_CMD; |
| 124 | |
| 125 | /* Returns the number of bytes left in the |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 126 | * download. You must be expecting a download to |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 127 | * call this function |
| 128 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 129 | UINTN GetXfrSize (VOID); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 130 | |
| 131 | /* Registers commands and publishes Variables */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 132 | EFI_STATUS |
| 133 | FastbootEnvSetup (VOID *xfer_buffer, UINT32 max); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 134 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 135 | /* register a command handler |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 136 | * - command handlers will be called if their prefix matches |
| 137 | * - they are expected to call fastboot_okay() or fastboot_fail() |
| 138 | * to indicate success/failure before returning |
| 139 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 140 | VOID |
| 141 | FastbootRegister (CONST CHAR8 *prefix, |
| 142 | VOID (*handle) (CONST CHAR8 *arg, VOID *data, UINT32 size)); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 143 | |
| 144 | /* Only callable from within a command handler |
| 145 | * One of thse functions must be called to be a valid command |
| 146 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 147 | VOID |
| 148 | FastbootOkay (CONST CHAR8 *result); |
| 149 | VOID |
| 150 | FastbootFail (CONST CHAR8 *reason); |
| 151 | VOID |
| 152 | FastbootInfo (CONST CHAR8 *Info); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 153 | |
| 154 | /* Initializes the Fastboot App */ |
| 155 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 156 | FastbootCmdsInit (VOID); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 157 | |
| 158 | /* Uninitializes the Fastboot App */ |
| 159 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 160 | FastbootCmdsUnInit (VOID); |
| 161 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 162 | /* Called when a message/download data passed to the app */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 163 | VOID |
| 164 | DataReady (IN UINT64 Size, IN VOID *Data); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 165 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 166 | BOOLEAN FastbootFatal (VOID); |
| 167 | VOID PartitionDump (VOID); |
Channagoud Kadabi | b854cc4 | 2016-02-24 17:55:09 -0800 | [diff] [blame] | 168 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 169 | VOID *FastbootDloadBuffer (VOID); |
Channagoud Kadabi | b854cc4 | 2016-02-24 17:55:09 -0800 | [diff] [blame] | 170 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 171 | ANDROID_FASTBOOT_STATE FastbootCurrentState (VOID); |
Channagoud Kadabi | b854cc4 | 2016-02-24 17:55:09 -0800 | [diff] [blame] | 172 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 173 | EFI_STATUS |
| 174 | UpdateDevInfo (CHAR16 *Pname, CHAR8 *ImgVersion); |
| 175 | VOID |
| 176 | GetDevInfo (DeviceInfo **DevinfoPtr); |
jianzhou | 894cb18 | 2019-09-16 16:54:51 +0800 | [diff] [blame] | 177 | BOOLEAN IsFlashSplitNeeded (VOID); |
| 178 | BOOLEAN FlashComplete (VOID); |
| 179 | BOOLEAN IsDisableParallelDownloadFlash (VOID); |
| 180 | BOOLEAN IsUseMThreadParallel (VOID); |
| 181 | VOID ThreadSleep (TimeDuration Delay); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 182 | #endif |