| 1 | /** @file\r |
| 2 | A shell application that triggers capsule update process.\r |
| 3 | \r |
| 4 | Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r |
| 5 | This program and the accompanying materials\r |
| 6 | are licensed and made available under the terms and conditions of the BSD License\r |
| 7 | which accompanies this distribution. The full text of the license may be found at\r |
| 8 | http://opensource.org/licenses/bsd-license.php\r |
| 9 | \r |
| 10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
| 11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
| 12 | \r |
| 13 | **/\r |
| 14 | \r |
| 15 | #include <Uefi.h>\r |
| 16 | #include <Library/BaseLib.h>\r |
| 17 | #include <Library/DebugLib.h>\r |
| 18 | #include <Library/BaseMemoryLib.h>\r |
| 19 | #include <Library/MemoryAllocationLib.h>\r |
| 20 | #include <Library/UefiBootServicesTableLib.h>\r |
| 21 | #include <Protocol/SimpleFileSystem.h>\r |
| 22 | #include <Protocol/ShellParameters.h>\r |
| 23 | #include <Protocol/Shell.h>\r |
| 24 | #include <Guid/FileInfo.h>\r |
| 25 | \r |
| 26 | UINTN Argc;\r |
| 27 | CHAR16 **Argv;\r |
| 28 | EFI_SHELL_PROTOCOL *mShellProtocol = NULL;\r |
| 29 | \r |
| 30 | /**\r |
| 31 | \r |
| 32 | This function parse application ARG.\r |
| 33 | \r |
| 34 | @return Status\r |
| 35 | **/\r |
| 36 | EFI_STATUS\r |
| 37 | GetArg (\r |
| 38 | VOID\r |
| 39 | )\r |
| 40 | {\r |
| 41 | EFI_STATUS Status;\r |
| 42 | EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;\r |
| 43 | \r |
| 44 | Status = gBS->HandleProtocol (\r |
| 45 | gImageHandle,\r |
| 46 | &gEfiShellParametersProtocolGuid,\r |
| 47 | (VOID**)&ShellParameters\r |
| 48 | );\r |
| 49 | if (EFI_ERROR(Status)) {\r |
| 50 | return Status;\r |
| 51 | }\r |
| 52 | \r |
| 53 | Argc = ShellParameters->Argc;\r |
| 54 | Argv = ShellParameters->Argv;\r |
| 55 | return EFI_SUCCESS;\r |
| 56 | }\r |
| 57 | \r |
| 58 | /**\r |
| 59 | Get shell protocol.\r |
| 60 | \r |
| 61 | @return Pointer to shell protocol.\r |
| 62 | **/\r |
| 63 | EFI_SHELL_PROTOCOL *\r |
| 64 | GetShellProtocol (\r |
| 65 | VOID\r |
| 66 | )\r |
| 67 | {\r |
| 68 | EFI_STATUS Status;\r |
| 69 | \r |
| 70 | if (mShellProtocol == NULL) {\r |
| 71 | Status = gBS->LocateProtocol (\r |
| 72 | &gEfiShellProtocolGuid,\r |
| 73 | NULL,\r |
| 74 | (VOID **) &mShellProtocol\r |
| 75 | );\r |
| 76 | if (EFI_ERROR (Status)) {\r |
| 77 | mShellProtocol = NULL;\r |
| 78 | }\r |
| 79 | }\r |
| 80 | \r |
| 81 | return mShellProtocol;\r |
| 82 | }\r |
| 83 | \r |
| 84 | /**\r |
| 85 | Read a file.\r |
| 86 | \r |
| 87 | @param[in] FileName The file to be read.\r |
| 88 | @param[out] BufferSize The file buffer size\r |
| 89 | @param[out] Buffer The file buffer\r |
| 90 | \r |
| 91 | @retval EFI_SUCCESS Read file successfully\r |
| 92 | @retval EFI_NOT_FOUND Shell protocol or file not found\r |
| 93 | @retval others Read file failed\r |
| 94 | **/\r |
| 95 | EFI_STATUS\r |
| 96 | ReadFileToBuffer (\r |
| 97 | IN CHAR16 *FileName,\r |
| 98 | OUT UINTN *BufferSize,\r |
| 99 | OUT VOID **Buffer\r |
| 100 | )\r |
| 101 | {\r |
| 102 | EFI_STATUS Status;\r |
| 103 | EFI_SHELL_PROTOCOL *ShellProtocol;\r |
| 104 | SHELL_FILE_HANDLE Handle;\r |
| 105 | UINT64 FileSize;\r |
| 106 | UINTN TempBufferSize;\r |
| 107 | VOID *TempBuffer;\r |
| 108 | \r |
| 109 | ShellProtocol = GetShellProtocol();\r |
| 110 | if (ShellProtocol == NULL) {\r |
| 111 | return EFI_NOT_FOUND;\r |
| 112 | }\r |
| 113 | \r |
| 114 | //\r |
| 115 | // Open file by FileName.\r |
| 116 | //\r |
| 117 | Status = ShellProtocol->OpenFileByName (\r |
| 118 | FileName,\r |
| 119 | &Handle,\r |
| 120 | EFI_FILE_MODE_READ\r |
| 121 | );\r |
| 122 | if (EFI_ERROR (Status)) {\r |
| 123 | return Status;\r |
| 124 | }\r |
| 125 | \r |
| 126 | //\r |
| 127 | // Get the file size.\r |
| 128 | //\r |
| 129 | Status = ShellProtocol->GetFileSize (Handle, &FileSize);\r |
| 130 | if (EFI_ERROR (Status)) {\r |
| 131 | ShellProtocol->CloseFile (Handle);\r |
| 132 | return Status;\r |
| 133 | }\r |
| 134 | \r |
| 135 | TempBufferSize = (UINTN) FileSize;\r |
| 136 | TempBuffer = AllocateZeroPool (TempBufferSize);\r |
| 137 | if (TempBuffer == NULL) {\r |
| 138 | ShellProtocol->CloseFile (Handle);\r |
| 139 | return EFI_OUT_OF_RESOURCES;\r |
| 140 | }\r |
| 141 | \r |
| 142 | //\r |
| 143 | // Read the file data to the buffer\r |
| 144 | //\r |
| 145 | Status = ShellProtocol->ReadFile (\r |
| 146 | Handle,\r |
| 147 | &TempBufferSize,\r |
| 148 | TempBuffer\r |
| 149 | );\r |
| 150 | if (EFI_ERROR (Status)) {\r |
| 151 | ShellProtocol->CloseFile (Handle);\r |
| 152 | return Status;\r |
| 153 | }\r |
| 154 | \r |
| 155 | ShellProtocol->CloseFile (Handle);\r |
| 156 | \r |
| 157 | *BufferSize = TempBufferSize;\r |
| 158 | *Buffer = TempBuffer;\r |
| 159 | return EFI_SUCCESS;\r |
| 160 | }\r |
| 161 | \r |
| 162 | /**\r |
| 163 | Write a file.\r |
| 164 | \r |
| 165 | @param[in] FileName The file to be written.\r |
| 166 | @param[in] BufferSize The file buffer size\r |
| 167 | @param[in] Buffer The file buffer\r |
| 168 | \r |
| 169 | @retval EFI_SUCCESS Write file successfully\r |
| 170 | @retval EFI_NOT_FOUND Shell protocol not found\r |
| 171 | @retval others Write file failed\r |
| 172 | **/\r |
| 173 | EFI_STATUS\r |
| 174 | WriteFileFromBuffer (\r |
| 175 | IN CHAR16 *FileName,\r |
| 176 | IN UINTN BufferSize,\r |
| 177 | IN VOID *Buffer\r |
| 178 | )\r |
| 179 | {\r |
| 180 | EFI_STATUS Status;\r |
| 181 | EFI_SHELL_PROTOCOL *ShellProtocol;\r |
| 182 | SHELL_FILE_HANDLE Handle;\r |
| 183 | EFI_FILE_INFO *FileInfo;\r |
| 184 | UINTN TempBufferSize;\r |
| 185 | \r |
| 186 | ShellProtocol = GetShellProtocol();\r |
| 187 | if (ShellProtocol == NULL) {\r |
| 188 | return EFI_NOT_FOUND;\r |
| 189 | }\r |
| 190 | \r |
| 191 | //\r |
| 192 | // Open file by FileName.\r |
| 193 | //\r |
| 194 | Status = ShellProtocol->OpenFileByName (\r |
| 195 | FileName,\r |
| 196 | &Handle,\r |
| 197 | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE\r |
| 198 | );\r |
| 199 | if (EFI_ERROR (Status)) {\r |
| 200 | return Status;\r |
| 201 | }\r |
| 202 | \r |
| 203 | //\r |
| 204 | // Empty the file contents.\r |
| 205 | //\r |
| 206 | FileInfo = ShellProtocol->GetFileInfo (Handle);\r |
| 207 | if (FileInfo == NULL) {\r |
| 208 | ShellProtocol->CloseFile (Handle);\r |
| 209 | return EFI_DEVICE_ERROR;\r |
| 210 | }\r |
| 211 | \r |
| 212 | //\r |
| 213 | // If the file size is already 0, then it has been empty.\r |
| 214 | //\r |
| 215 | if (FileInfo->FileSize != 0) {\r |
| 216 | //\r |
| 217 | // Set the file size to 0.\r |
| 218 | //\r |
| 219 | FileInfo->FileSize = 0;\r |
| 220 | Status = ShellProtocol->SetFileInfo (Handle, FileInfo);\r |
| 221 | if (EFI_ERROR (Status)) {\r |
| 222 | FreePool (FileInfo);\r |
| 223 | ShellProtocol->CloseFile (Handle);\r |
| 224 | return Status;\r |
| 225 | }\r |
| 226 | }\r |
| 227 | FreePool (FileInfo);\r |
| 228 | \r |
| 229 | //\r |
| 230 | // Write the file data from the buffer\r |
| 231 | //\r |
| 232 | TempBufferSize = BufferSize;\r |
| 233 | Status = ShellProtocol->WriteFile (\r |
| 234 | Handle,\r |
| 235 | &TempBufferSize,\r |
| 236 | Buffer\r |
| 237 | );\r |
| 238 | if (EFI_ERROR (Status)) {\r |
| 239 | ShellProtocol->CloseFile (Handle);\r |
| 240 | return Status;\r |
| 241 | }\r |
| 242 | \r |
| 243 | ShellProtocol->CloseFile (Handle);\r |
| 244 | \r |
| 245 | return EFI_SUCCESS;\r |
| 246 | }\r |
| 247 | \r |