jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1 | /** @file
|
| 2 | Provides interface to shell functionality for shell commands and applications.
|
| 3 |
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 4 | Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 5 | This program and the accompanying materials
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 6 | are licensed and made available under the terms and conditions of the BSD License
|
| 7 | which accompanies this distribution. The full text of the license may be found at
|
| 8 | http://opensource.org/licenses/bsd-license.php
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 9 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
| 11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 12 |
|
| 13 | **/
|
| 14 |
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 15 | #include "UefiShellLib.h"
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 16 | #include <ShellBase.h>
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 17 | #include <Library/SortLib.h>
|
Laszlo Ersek | 3fe23dc | 2015-01-14 16:25:48 +0000 | [diff] [blame] | 18 | #include <Library/BaseLib.h>
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 19 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 20 | #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
|
| 21 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 22 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 23 | // globals...
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 24 | //
|
| 25 | SHELL_PARAM_ITEM EmptyParamList[] = {
|
| 26 | {NULL, TypeMax}
|
| 27 | };
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 28 | SHELL_PARAM_ITEM SfoParamList[] = {
|
| 29 | {L"-sfo", TypeFlag},
|
| 30 | {NULL, TypeMax}
|
| 31 | };
|
| 32 | EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
|
| 33 | EFI_SHELL_INTERFACE *mEfiShellInterface;
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 34 | EFI_SHELL_PROTOCOL *gEfiShellProtocol;
|
| 35 | EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 36 | EFI_HANDLE mEfiShellEnvironment2Handle;
|
| 37 | FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 38 |
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 39 | /**
|
| 40 | Check if a Unicode character is a hexadecimal character.
|
| 41 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 42 | This internal function checks if a Unicode character is a
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 43 | numeric character. The valid hexadecimal characters are
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 44 | L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
|
| 45 |
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 46 | @param Char The character to check against.
|
| 47 |
|
| 48 | @retval TRUE If the Char is a hexadecmial character.
|
| 49 | @retval FALSE If the Char is not a hexadecmial character.
|
| 50 |
|
| 51 | **/
|
| 52 | BOOLEAN
|
| 53 | EFIAPI
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 54 | ShellIsHexaDecimalDigitCharacter (
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 55 | IN CHAR16 Char
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 56 | )
|
| 57 | {
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 58 | return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
|
| 59 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 60 |
|
| 61 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 62 | Check if a Unicode character is a decimal character.
|
| 63 |
|
| 64 | This internal function checks if a Unicode character is a
|
| 65 | decimal character. The valid characters are
|
| 66 | L'0' to L'9'.
|
| 67 |
|
| 68 |
|
| 69 | @param Char The character to check against.
|
| 70 |
|
| 71 | @retval TRUE If the Char is a hexadecmial character.
|
| 72 | @retval FALSE If the Char is not a hexadecmial character.
|
| 73 |
|
| 74 | **/
|
| 75 | BOOLEAN
|
| 76 | EFIAPI
|
| 77 | ShellIsDecimalDigitCharacter (
|
| 78 | IN CHAR16 Char
|
| 79 | )
|
| 80 | {
|
| 81 | return (BOOLEAN) (Char >= L'0' && Char <= L'9');
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | Helper function to find ShellEnvironment2 for constructor.
|
| 86 |
|
| 87 | @param[in] ImageHandle A copy of the calling image's handle.
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 88 |
|
| 89 | @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 90 | **/
|
| 91 | EFI_STATUS
|
| 92 | EFIAPI
|
| 93 | ShellFindSE2 (
|
| 94 | IN EFI_HANDLE ImageHandle
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 95 | )
|
| 96 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 97 | EFI_STATUS Status;
|
| 98 | EFI_HANDLE *Buffer;
|
| 99 | UINTN BufferSize;
|
| 100 | UINTN HandleIndex;
|
| 101 |
|
| 102 | BufferSize = 0;
|
| 103 | Buffer = NULL;
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 104 | Status = gBS->OpenProtocol(ImageHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 105 | &gEfiShellEnvironment2Guid,
|
| 106 | (VOID **)&mEfiShellEnvironment2,
|
| 107 | ImageHandle,
|
| 108 | NULL,
|
| 109 | EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 110 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 111 | //
|
| 112 | // look for the mEfiShellEnvironment2 protocol at a higher level
|
| 113 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 114 | if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 115 | //
|
| 116 | // figure out how big of a buffer we need.
|
| 117 | //
|
| 118 | Status = gBS->LocateHandle (ByProtocol,
|
| 119 | &gEfiShellEnvironment2Guid,
|
| 120 | NULL, // ignored for ByProtocol
|
| 121 | &BufferSize,
|
| 122 | Buffer
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 123 | );
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 124 | //
|
| 125 | // maybe it's not there???
|
| 126 | //
|
| 127 | if (Status == EFI_BUFFER_TOO_SMALL) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 128 | Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 129 | if (Buffer == NULL) {
|
| 130 | return (EFI_OUT_OF_RESOURCES);
|
| 131 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 132 | Status = gBS->LocateHandle (ByProtocol,
|
| 133 | &gEfiShellEnvironment2Guid,
|
| 134 | NULL, // ignored for ByProtocol
|
| 135 | &BufferSize,
|
| 136 | Buffer
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 137 | );
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 138 | }
|
jcarsey | 1cd45e7 | 2010-01-29 15:07:44 +0000 | [diff] [blame] | 139 | if (!EFI_ERROR (Status) && Buffer != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 140 | //
|
| 141 | // now parse the list of returned handles
|
| 142 | //
|
| 143 | Status = EFI_NOT_FOUND;
|
| 144 | for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 145 | Status = gBS->OpenProtocol(Buffer[HandleIndex],
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 146 | &gEfiShellEnvironment2Guid,
|
| 147 | (VOID **)&mEfiShellEnvironment2,
|
| 148 | ImageHandle,
|
| 149 | NULL,
|
| 150 | EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 151 | );
|
| 152 | if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 153 | mEfiShellEnvironment2Handle = Buffer[HandleIndex];
|
| 154 | Status = EFI_SUCCESS;
|
| 155 | break;
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 | if (Buffer != NULL) {
|
| 161 | FreePool (Buffer);
|
| 162 | }
|
| 163 | return (Status);
|
| 164 | }
|
| 165 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 166 | /**
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 167 | Function to do most of the work of the constructor. Allows for calling
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 168 | multiple times without complete re-initialization.
|
| 169 |
|
| 170 | @param[in] ImageHandle A copy of the ImageHandle.
|
| 171 | @param[in] SystemTable A pointer to the SystemTable for the application.
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 172 |
|
| 173 | @retval EFI_SUCCESS The operationw as successful.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 174 | **/
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 175 | EFI_STATUS
|
| 176 | EFIAPI
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 177 | ShellLibConstructorWorker (
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 178 | IN EFI_HANDLE ImageHandle,
|
| 179 | IN EFI_SYSTEM_TABLE *SystemTable
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 180 | )
|
| 181 | {
|
| 182 | EFI_STATUS Status;
|
jcarsey | ecd3d59 | 2009-12-07 18:05:00 +0000 | [diff] [blame] | 183 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 184 | //
|
| 185 | // UEFI 2.0 shell interfaces (used preferentially)
|
| 186 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 187 | Status = gBS->OpenProtocol(
|
| 188 | ImageHandle,
|
| 189 | &gEfiShellProtocolGuid,
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 190 | (VOID **)&gEfiShellProtocol,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 191 | ImageHandle,
|
| 192 | NULL,
|
| 193 | EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
| 194 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 195 | if (EFI_ERROR(Status)) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 196 | //
|
| 197 | // Search for the shell protocol
|
| 198 | //
|
| 199 | Status = gBS->LocateProtocol(
|
| 200 | &gEfiShellProtocolGuid,
|
| 201 | NULL,
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 202 | (VOID **)&gEfiShellProtocol
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 203 | );
|
| 204 | if (EFI_ERROR(Status)) {
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 205 | gEfiShellProtocol = NULL;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 206 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 207 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 208 | Status = gBS->OpenProtocol(
|
| 209 | ImageHandle,
|
| 210 | &gEfiShellParametersProtocolGuid,
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 211 | (VOID **)&gEfiShellParametersProtocol,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 212 | ImageHandle,
|
| 213 | NULL,
|
| 214 | EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
| 215 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 216 | if (EFI_ERROR(Status)) {
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 217 | gEfiShellParametersProtocol = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 218 | }
|
| 219 |
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 220 | if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 221 | //
|
| 222 | // Moved to seperate function due to complexity
|
| 223 | //
|
| 224 | Status = ShellFindSE2(ImageHandle);
|
| 225 |
|
| 226 | if (EFI_ERROR(Status)) {
|
| 227 | DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
|
| 228 | mEfiShellEnvironment2 = NULL;
|
| 229 | }
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 230 | Status = gBS->OpenProtocol(ImageHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 231 | &gEfiShellInterfaceGuid,
|
| 232 | (VOID **)&mEfiShellInterface,
|
| 233 | ImageHandle,
|
| 234 | NULL,
|
| 235 | EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 236 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 237 | if (EFI_ERROR(Status)) {
|
| 238 | mEfiShellInterface = NULL;
|
| 239 | }
|
| 240 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 241 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 242 | //
|
| 243 | // only success getting 2 of either the old or new, but no 1/2 and 1/2
|
| 244 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 245 | if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 246 | (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
|
| 247 | if (gEfiShellProtocol != NULL) {
|
| 248 | FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
|
| 249 | FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
|
| 250 | FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
|
| 251 | FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
|
| 252 | FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
|
| 253 | FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
|
| 254 | FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
|
| 255 | FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
|
| 256 | FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
|
| 257 | FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 258 | } else {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 259 | FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
|
| 260 | FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
|
| 261 | FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
|
| 262 | FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
|
| 263 | FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
|
| 264 | FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
|
| 265 | FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
|
| 266 | FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
|
| 267 | FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
|
| 268 | FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 269 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 270 | return (EFI_SUCCESS);
|
| 271 | }
|
| 272 | return (EFI_NOT_FOUND);
|
| 273 | }
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 274 | /**
|
| 275 | Constructor for the Shell library.
|
| 276 |
|
| 277 | Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
|
| 278 |
|
| 279 | @param ImageHandle the image handle of the process
|
| 280 | @param SystemTable the EFI System Table pointer
|
| 281 |
|
| 282 | @retval EFI_SUCCESS the initialization was complete sucessfully
|
| 283 | @return others an error ocurred during initialization
|
| 284 | **/
|
| 285 | EFI_STATUS
|
| 286 | EFIAPI
|
| 287 | ShellLibConstructor (
|
| 288 | IN EFI_HANDLE ImageHandle,
|
| 289 | IN EFI_SYSTEM_TABLE *SystemTable
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 290 | )
|
| 291 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 292 | mEfiShellEnvironment2 = NULL;
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 293 | gEfiShellProtocol = NULL;
|
| 294 | gEfiShellParametersProtocol = NULL;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 295 | mEfiShellInterface = NULL;
|
| 296 | mEfiShellEnvironment2Handle = NULL;
|
| 297 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 298 | //
|
| 299 | // verify that auto initialize is not set false
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 300 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 301 | if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
|
| 302 | return (EFI_SUCCESS);
|
| 303 | }
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 304 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 305 | return (ShellLibConstructorWorker(ImageHandle, SystemTable));
|
| 306 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 307 |
|
| 308 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 309 | Destructor for the library. free any resources.
|
| 310 |
|
| 311 | @param[in] ImageHandle A copy of the ImageHandle.
|
| 312 | @param[in] SystemTable A pointer to the SystemTable for the application.
|
| 313 |
|
| 314 | @retval EFI_SUCCESS The operation was successful.
|
| 315 | @return An error from the CloseProtocol function.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 316 | **/
|
| 317 | EFI_STATUS
|
| 318 | EFIAPI
|
| 319 | ShellLibDestructor (
|
| 320 | IN EFI_HANDLE ImageHandle,
|
| 321 | IN EFI_SYSTEM_TABLE *SystemTable
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 322 | )
|
| 323 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 324 | if (mEfiShellEnvironment2 != NULL) {
|
| 325 | gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
|
| 326 | &gEfiShellEnvironment2Guid,
|
| 327 | ImageHandle,
|
| 328 | NULL);
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 329 | mEfiShellEnvironment2 = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 330 | }
|
| 331 | if (mEfiShellInterface != NULL) {
|
| 332 | gBS->CloseProtocol(ImageHandle,
|
| 333 | &gEfiShellInterfaceGuid,
|
| 334 | ImageHandle,
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 335 | NULL);
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 336 | mEfiShellInterface = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 337 | }
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 338 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 339 | gBS->CloseProtocol(ImageHandle,
|
| 340 | &gEfiShellProtocolGuid,
|
| 341 | ImageHandle,
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 342 | NULL);
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 343 | gEfiShellProtocol = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 344 | }
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 345 | if (gEfiShellParametersProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 346 | gBS->CloseProtocol(ImageHandle,
|
| 347 | &gEfiShellParametersProtocolGuid,
|
| 348 | ImageHandle,
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 349 | NULL);
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 350 | gEfiShellParametersProtocol = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 351 | }
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 352 | mEfiShellEnvironment2Handle = NULL;
|
jcarsey | ecd3d59 | 2009-12-07 18:05:00 +0000 | [diff] [blame] | 353 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 354 | return (EFI_SUCCESS);
|
| 355 | }
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 356 |
|
| 357 | /**
|
| 358 | This function causes the shell library to initialize itself. If the shell library
|
| 359 | is already initialized it will de-initialize all the current protocol poitners and
|
| 360 | re-populate them again.
|
| 361 |
|
| 362 | When the library is used with PcdShellLibAutoInitialize set to true this function
|
| 363 | will return EFI_SUCCESS and perform no actions.
|
| 364 |
|
| 365 | This function is intended for internal access for shell commands only.
|
| 366 |
|
| 367 | @retval EFI_SUCCESS the initialization was complete sucessfully
|
| 368 |
|
| 369 | **/
|
| 370 | EFI_STATUS
|
| 371 | EFIAPI
|
| 372 | ShellInitialize (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 373 | )
|
| 374 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 375 | //
|
| 376 | // if auto initialize is not false then skip
|
| 377 | //
|
| 378 | if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
|
| 379 | return (EFI_SUCCESS);
|
| 380 | }
|
| 381 |
|
| 382 | //
|
| 383 | // deinit the current stuff
|
| 384 | //
|
| 385 | ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
|
| 386 |
|
| 387 | //
|
| 388 | // init the new stuff
|
| 389 | //
|
| 390 | return (ShellLibConstructorWorker(gImageHandle, gST));
|
| 391 | }
|
| 392 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 393 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 394 | This function will retrieve the information about the file for the handle
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 395 | specified and store it in allocated pool memory.
|
| 396 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 397 | This function allocates a buffer to store the file's information. It is the
|
qhuang8 | 69817bf | 2009-05-20 14:42:48 +0000 | [diff] [blame] | 398 | caller's responsibility to free the buffer
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 399 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 400 | @param FileHandle The file handle of the file for which information is
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 401 | being requested.
|
| 402 |
|
| 403 | @retval NULL information could not be retrieved.
|
| 404 |
|
| 405 | @return the information about the file
|
| 406 | **/
|
| 407 | EFI_FILE_INFO*
|
| 408 | EFIAPI
|
| 409 | ShellGetFileInfo (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 410 | IN SHELL_FILE_HANDLE FileHandle
|
| 411 | )
|
| 412 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 413 | return (FileFunctionMap.GetFileInfo(FileHandle));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 414 | }
|
| 415 |
|
| 416 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 417 | This function sets the information about the file for the opened handle
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 418 | specified.
|
| 419 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 420 | @param[in] FileHandle The file handle of the file for which information
|
| 421 | is being set.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 422 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 423 | @param[in] FileInfo The information to set.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 424 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 425 | @retval EFI_SUCCESS The information was set.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 426 | @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
|
| 427 | @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 428 | @retval EFI_NO_MEDIA The device has no medium.
|
| 429 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 430 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 431 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 432 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
| 433 | @retval EFI_VOLUME_FULL The volume is full.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 434 | **/
|
| 435 | EFI_STATUS
|
| 436 | EFIAPI
|
| 437 | ShellSetFileInfo (
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 438 | IN SHELL_FILE_HANDLE FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 439 | IN EFI_FILE_INFO *FileInfo
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 440 | )
|
| 441 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 442 | return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 443 | }
|
| 444 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 445 | /**
|
| 446 | This function will open a file or directory referenced by DevicePath.
|
| 447 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 448 | This function opens a file with the open mode according to the file path. The
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 449 | Attributes is valid only for EFI_FILE_MODE_CREATE.
|
| 450 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 451 | @param FilePath on input the device path to the file. On output
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 452 | the remaining device path.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 453 | @param DeviceHandle pointer to the system device handle.
|
| 454 | @param FileHandle pointer to the file handle.
|
| 455 | @param OpenMode the mode to open the file with.
|
| 456 | @param Attributes the file's file attributes.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 457 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 458 | @retval EFI_SUCCESS The information was set.
|
| 459 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
| 460 | @retval EFI_UNSUPPORTED Could not open the file path.
|
| 461 | @retval EFI_NOT_FOUND The specified file could not be found on the
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 462 | device or the file system could not be found on
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 463 | the device.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 464 | @retval EFI_NO_MEDIA The device has no medium.
|
| 465 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 466 | medium is no longer supported.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 467 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 468 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 469 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
| 470 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
| 471 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 472 | file.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 473 | @retval EFI_VOLUME_FULL The volume is full.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 474 | **/
|
| 475 | EFI_STATUS
|
| 476 | EFIAPI
|
| 477 | ShellOpenFileByDevicePath(
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 478 | IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
|
| 479 | OUT EFI_HANDLE *DeviceHandle,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 480 | OUT SHELL_FILE_HANDLE *FileHandle,
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 481 | IN UINT64 OpenMode,
|
| 482 | IN UINT64 Attributes
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 483 | )
|
| 484 | {
|
| 485 | CHAR16 *FileName;
|
| 486 | EFI_STATUS Status;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 487 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 488 | EFI_FILE_PROTOCOL *Handle1;
|
| 489 | EFI_FILE_PROTOCOL *Handle2;
|
ydong10 | 0b6cb33 | 2013-01-25 02:00:22 +0000 | [diff] [blame] | 490 | CHAR16 *FnafPathName;
|
| 491 | UINTN PathLen;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 492 |
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 493 | if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
|
| 494 | return (EFI_INVALID_PARAMETER);
|
| 495 | }
|
| 496 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 497 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 498 | // which shell interface should we use
|
| 499 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 500 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 501 | //
|
| 502 | // use UEFI Shell 2.0 method.
|
| 503 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 504 | FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 505 | if (FileName == NULL) {
|
| 506 | return (EFI_INVALID_PARAMETER);
|
| 507 | }
|
| 508 | Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
|
| 509 | FreePool(FileName);
|
| 510 | return (Status);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 511 | }
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 512 |
|
| 513 |
|
| 514 | //
|
| 515 | // use old shell method.
|
| 516 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 517 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
|
| 518 | FilePath,
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 519 | DeviceHandle);
|
| 520 | if (EFI_ERROR (Status)) {
|
| 521 | return Status;
|
| 522 | }
|
| 523 | Status = gBS->OpenProtocol(*DeviceHandle,
|
| 524 | &gEfiSimpleFileSystemProtocolGuid,
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 525 | (VOID**)&EfiSimpleFileSystemProtocol,
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 526 | gImageHandle,
|
| 527 | NULL,
|
| 528 | EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
| 529 | if (EFI_ERROR (Status)) {
|
| 530 | return Status;
|
| 531 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 532 | Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 533 | if (EFI_ERROR (Status)) {
|
| 534 | FileHandle = NULL;
|
| 535 | return Status;
|
| 536 | }
|
| 537 |
|
| 538 | //
|
| 539 | // go down directories one node at a time.
|
| 540 | //
|
| 541 | while (!IsDevicePathEnd (*FilePath)) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 542 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 543 | // For file system access each node should be a file path component
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 544 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 545 | if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
|
| 546 | DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 547 | ) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 548 | FileHandle = NULL;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 549 | return (EFI_INVALID_PARAMETER);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 550 | }
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 551 | //
|
| 552 | // Open this file path node
|
| 553 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 554 | Handle2 = Handle1;
|
| 555 | Handle1 = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 556 |
|
| 557 | //
|
ydong10 | 0b6cb33 | 2013-01-25 02:00:22 +0000 | [diff] [blame] | 558 | // File Name Alignment Fix (FNAF)
|
| 559 | // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
|
| 560 | // The structure pointed to by FilePath may be not CHAR16 aligned.
|
| 561 | // This code copies the potentially unaligned PathName data from the
|
| 562 | // FilePath structure to the aligned FnafPathName for use in the
|
| 563 | // calls to Handl2->Open.
|
| 564 | //
|
| 565 |
|
| 566 | //
|
| 567 | // Determine length of PathName, in bytes.
|
| 568 | //
|
| 569 | PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
|
| 570 |
|
| 571 | //
|
| 572 | // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
|
| 573 | // Copy bytes from possibly unaligned location to aligned location
|
| 574 | //
|
| 575 | FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
|
| 576 | if (FnafPathName == NULL) {
|
| 577 | return EFI_OUT_OF_RESOURCES;
|
| 578 | }
|
| 579 |
|
| 580 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 581 | // Try to test opening an existing file
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 582 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 583 | Status = Handle2->Open (
|
| 584 | Handle2,
|
| 585 | &Handle1,
|
ydong10 | 0b6cb33 | 2013-01-25 02:00:22 +0000 | [diff] [blame] | 586 | FnafPathName,
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 587 | OpenMode &~EFI_FILE_MODE_CREATE,
|
| 588 | 0
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 589 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 590 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 591 | //
|
| 592 | // see if the error was that it needs to be created
|
| 593 | //
|
| 594 | if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 595 | Status = Handle2->Open (
|
| 596 | Handle2,
|
| 597 | &Handle1,
|
ydong10 | 0b6cb33 | 2013-01-25 02:00:22 +0000 | [diff] [blame] | 598 | FnafPathName,
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 599 | OpenMode,
|
| 600 | Attributes
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 601 | );
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 602 | }
|
ydong10 | 0b6cb33 | 2013-01-25 02:00:22 +0000 | [diff] [blame] | 603 |
|
| 604 | //
|
| 605 | // Free the alignment buffer
|
| 606 | //
|
| 607 | FreePool(FnafPathName);
|
| 608 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 609 | //
|
| 610 | // Close the last node
|
| 611 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 612 | Handle2->Close (Handle2);
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 613 |
|
| 614 | if (EFI_ERROR(Status)) {
|
| 615 | return (Status);
|
| 616 | }
|
| 617 |
|
| 618 | //
|
| 619 | // Get the next node
|
| 620 | //
|
| 621 | *FilePath = NextDevicePathNode (*FilePath);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 622 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 623 |
|
| 624 | //
|
| 625 | // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
|
| 626 | //
|
| 627 | *FileHandle = (VOID*)Handle1;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 628 | return (EFI_SUCCESS);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 629 | }
|
| 630 |
|
| 631 | /**
|
| 632 | This function will open a file or directory referenced by filename.
|
| 633 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 634 | If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
|
| 635 | otherwise, the Filehandle is NULL. The Attributes is valid only for
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 636 | EFI_FILE_MODE_CREATE.
|
| 637 |
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 638 | if FileName is NULL then ASSERT()
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 639 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 640 | @param FileName pointer to file name
|
| 641 | @param FileHandle pointer to the file handle.
|
| 642 | @param OpenMode the mode to open the file with.
|
| 643 | @param Attributes the file's file attributes.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 644 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 645 | @retval EFI_SUCCESS The information was set.
|
| 646 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
| 647 | @retval EFI_UNSUPPORTED Could not open the file path.
|
| 648 | @retval EFI_NOT_FOUND The specified file could not be found on the
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 649 | device or the file system could not be found
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 650 | on the device.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 651 | @retval EFI_NO_MEDIA The device has no medium.
|
| 652 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 653 | medium is no longer supported.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 654 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 655 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 656 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
| 657 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
| 658 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 659 | file.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 660 | @retval EFI_VOLUME_FULL The volume is full.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 661 | **/
|
| 662 | EFI_STATUS
|
| 663 | EFIAPI
|
| 664 | ShellOpenFileByName(
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 665 | IN CONST CHAR16 *FileName,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 666 | OUT SHELL_FILE_HANDLE *FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 667 | IN UINT64 OpenMode,
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 668 | IN UINT64 Attributes
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 669 | )
|
| 670 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 671 | EFI_HANDLE DeviceHandle;
|
| 672 | EFI_DEVICE_PATH_PROTOCOL *FilePath;
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 673 | EFI_STATUS Status;
|
| 674 | EFI_FILE_INFO *FileInfo;
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 675 | CHAR16 *FileNameCopy;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 676 |
|
| 677 | //
|
| 678 | // ASSERT if FileName is NULL
|
| 679 | //
|
| 680 | ASSERT(FileName != NULL);
|
| 681 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 682 | if (FileName == NULL) {
|
| 683 | return (EFI_INVALID_PARAMETER);
|
| 684 | }
|
| 685 |
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 686 | if (gEfiShellProtocol != NULL) {
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 687 | if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {
|
| 688 |
|
| 689 | //
|
| 690 | // Create only a directory
|
| 691 | //
|
| 692 | if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
|
| 693 | return ShellCreateDirectory(FileName, FileHandle);
|
| 694 | }
|
| 695 |
|
| 696 | //
|
| 697 | // Create the directory to create the file in
|
| 698 | //
|
| 699 | FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);
|
| 700 | if (FileName == NULL) {
|
| 701 | return (EFI_OUT_OF_RESOURCES);
|
| 702 | }
|
| 703 | PathCleanUpDirectories (FileNameCopy);
|
| 704 | if (PathRemoveLastItem (FileNameCopy)) {
|
Jaben Carsey | 983fffd | 2015-07-28 20:22:26 +0000 | [diff] [blame] | 705 | if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) {
|
| 706 | ShellCloseFile (FileHandle);
|
| 707 | }
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 708 | }
|
| 709 | SHELL_FREE_NON_NULL (FileNameCopy);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 710 | }
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 711 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 712 | //
|
jaben carsey | 21a86a7 | 2015-01-13 22:16:41 +0000 | [diff] [blame] | 713 | // Use UEFI Shell 2.0 method to create the file
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 714 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 715 | Status = gEfiShellProtocol->OpenFileByName(FileName,
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 716 | FileHandle,
|
| 717 | OpenMode);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 718 | if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 719 | FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 720 | ASSERT(FileInfo != NULL);
|
| 721 | FileInfo->Attribute = Attributes;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 722 | Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
|
| 723 | FreePool(FileInfo);
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 724 | }
|
| 725 | return (Status);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 726 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 727 | //
|
| 728 | // Using EFI Shell version
|
| 729 | // this means convert name to path and call that function
|
| 730 | // since this will use EFI method again that will open it.
|
| 731 | //
|
| 732 | ASSERT(mEfiShellEnvironment2 != NULL);
|
jcarsey | b82bfcc | 2009-06-29 16:28:23 +0000 | [diff] [blame] | 733 | FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
|
xdu2 | 90bfa22 | 2010-07-19 05:21:27 +0000 | [diff] [blame] | 734 | if (FilePath != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 735 | return (ShellOpenFileByDevicePath(&FilePath,
|
| 736 | &DeviceHandle,
|
| 737 | FileHandle,
|
| 738 | OpenMode,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 739 | Attributes));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 740 | }
|
| 741 | return (EFI_DEVICE_ERROR);
|
| 742 | }
|
| 743 | /**
|
| 744 | This function create a directory
|
| 745 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 746 | If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
|
| 747 | otherwise, the Filehandle is NULL. If the directory already existed, this
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 748 | function opens the existing directory.
|
| 749 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 750 | @param DirectoryName pointer to directory name
|
| 751 | @param FileHandle pointer to the file handle.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 752 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 753 | @retval EFI_SUCCESS The information was set.
|
| 754 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
| 755 | @retval EFI_UNSUPPORTED Could not open the file path.
|
| 756 | @retval EFI_NOT_FOUND The specified file could not be found on the
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 757 | device or the file system could not be found
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 758 | on the device.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 759 | @retval EFI_NO_MEDIA The device has no medium.
|
| 760 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 761 | medium is no longer supported.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 762 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 763 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 764 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
| 765 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
| 766 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 767 | file.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 768 | @retval EFI_VOLUME_FULL The volume is full.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 769 | @sa ShellOpenFileByName
|
| 770 | **/
|
| 771 | EFI_STATUS
|
| 772 | EFIAPI
|
| 773 | ShellCreateDirectory(
|
jcarsey | b82bfcc | 2009-06-29 16:28:23 +0000 | [diff] [blame] | 774 | IN CONST CHAR16 *DirectoryName,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 775 | OUT SHELL_FILE_HANDLE *FileHandle
|
| 776 | )
|
| 777 | {
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 778 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 779 | //
|
| 780 | // Use UEFI Shell 2.0 method
|
| 781 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 782 | return (gEfiShellProtocol->CreateFile(DirectoryName,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 783 | EFI_FILE_DIRECTORY,
|
| 784 | FileHandle
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 785 | ));
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 786 | } else {
|
| 787 | return (ShellOpenFileByName(DirectoryName,
|
| 788 | FileHandle,
|
| 789 | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
|
| 790 | EFI_FILE_DIRECTORY
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 791 | ));
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 792 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 793 | }
|
| 794 |
|
| 795 | /**
|
| 796 | This function reads information from an opened file.
|
| 797 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 798 | If FileHandle is not a directory, the function reads the requested number of
|
| 799 | bytes from the file at the file's current position and returns them in Buffer.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 800 | If the read goes beyond the end of the file, the read length is truncated to the
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 801 | end of the file. The file's current position is increased by the number of bytes
|
| 802 | returned. If FileHandle is a directory, the function reads the directory entry
|
| 803 | at the file's current position and returns the entry in Buffer. If the Buffer
|
| 804 | is not large enough to hold the current directory entry, then
|
| 805 | EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
|
| 806 | BufferSize is set to be the size of the buffer needed to read the entry. On
|
| 807 | success, the current position is updated to the next directory entry. If there
|
| 808 | are no more directory entries, the read returns a zero-length buffer.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 809 | EFI_FILE_INFO is the structure returned as the directory entry.
|
| 810 |
|
| 811 | @param FileHandle the opened file handle
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 812 | @param BufferSize on input the size of buffer in bytes. on return
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 813 | the number of bytes written.
|
| 814 | @param Buffer the buffer to put read data into.
|
| 815 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 816 | @retval EFI_SUCCESS Data was read.
|
| 817 | @retval EFI_NO_MEDIA The device has no media.
|
| 818 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 819 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 820 | @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 821 | size.
|
| 822 |
|
| 823 | **/
|
| 824 | EFI_STATUS
|
| 825 | EFIAPI
|
| 826 | ShellReadFile(
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 827 | IN SHELL_FILE_HANDLE FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 828 | IN OUT UINTN *BufferSize,
|
| 829 | OUT VOID *Buffer
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 830 | )
|
| 831 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 832 | return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 833 | }
|
| 834 |
|
| 835 |
|
| 836 | /**
|
| 837 | Write data to a file.
|
| 838 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 839 | This function writes the specified number of bytes to the file at the current
|
| 840 | file position. The current file position is advanced the actual number of bytes
|
| 841 | written, which is returned in BufferSize. Partial writes only occur when there
|
| 842 | has been a data error during the write attempt (such as "volume space full").
|
| 843 | The file is automatically grown to hold the data if required. Direct writes to
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 844 | opened directories are not supported.
|
| 845 |
|
| 846 | @param FileHandle The opened file for writing
|
| 847 | @param BufferSize on input the number of bytes in Buffer. On output
|
| 848 | the number of bytes written.
|
| 849 | @param Buffer the buffer containing data to write is stored.
|
| 850 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 851 | @retval EFI_SUCCESS Data was written.
|
| 852 | @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
|
| 853 | @retval EFI_NO_MEDIA The device has no media.
|
| 854 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 855 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 856 | @retval EFI_WRITE_PROTECTED The device is write-protected.
|
| 857 | @retval EFI_ACCESS_DENIED The file was open for read only.
|
| 858 | @retval EFI_VOLUME_FULL The volume is full.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 859 | **/
|
| 860 | EFI_STATUS
|
| 861 | EFIAPI
|
| 862 | ShellWriteFile(
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 863 | IN SHELL_FILE_HANDLE FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 864 | IN OUT UINTN *BufferSize,
|
| 865 | IN VOID *Buffer
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 866 | )
|
| 867 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 868 | return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 869 | }
|
| 870 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 871 | /**
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 872 | Close an open file handle.
|
| 873 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 874 | This function closes a specified file handle. All "dirty" cached file data is
|
| 875 | flushed to the device, and the file is closed. In all cases the handle is
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 876 | closed.
|
| 877 |
|
| 878 | @param FileHandle the file handle to close.
|
| 879 |
|
| 880 | @retval EFI_SUCCESS the file handle was closed sucessfully.
|
| 881 | **/
|
| 882 | EFI_STATUS
|
| 883 | EFIAPI
|
| 884 | ShellCloseFile (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 885 | IN SHELL_FILE_HANDLE *FileHandle
|
| 886 | )
|
| 887 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 888 | return (FileFunctionMap.CloseFile(*FileHandle));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 889 | }
|
| 890 |
|
| 891 | /**
|
| 892 | Delete a file and close the handle
|
| 893 |
|
| 894 | This function closes and deletes a file. In all cases the file handle is closed.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 895 | If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 896 | returned, but the handle is still closed.
|
| 897 |
|
| 898 | @param FileHandle the file handle to delete
|
| 899 |
|
| 900 | @retval EFI_SUCCESS the file was closed sucessfully
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 901 | @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 902 | deleted
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 903 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 904 | **/
|
| 905 | EFI_STATUS
|
| 906 | EFIAPI
|
| 907 | ShellDeleteFile (
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 908 | IN SHELL_FILE_HANDLE *FileHandle
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 909 | )
|
| 910 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 911 | return (FileFunctionMap.DeleteFile(*FileHandle));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 912 | }
|
| 913 |
|
| 914 | /**
|
| 915 | Set the current position in a file.
|
| 916 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 917 | This function sets the current file position for the handle to the position
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 918 | supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 919 | absolute positioning is supported, and seeking past the end of the file is
|
| 920 | allowed (a subsequent write would grow the file). Seeking to position
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 921 | 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 922 | If FileHandle is a directory, the only position that may be set is zero. This
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 923 | has the effect of starting the read process of the directory entries over.
|
| 924 |
|
| 925 | @param FileHandle The file handle on which the position is being set
|
| 926 | @param Position Byte position from begining of file
|
| 927 |
|
| 928 | @retval EFI_SUCCESS Operation completed sucessfully.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 929 | @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 930 | directories.
|
| 931 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
| 932 | **/
|
| 933 | EFI_STATUS
|
| 934 | EFIAPI
|
| 935 | ShellSetFilePosition (
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 936 | IN SHELL_FILE_HANDLE FileHandle,
|
| 937 | IN UINT64 Position
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 938 | )
|
| 939 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 940 | return (FileFunctionMap.SetFilePosition(FileHandle, Position));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 941 | }
|
| 942 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 943 | /**
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 944 | Gets a file's current position
|
| 945 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 946 | This function retrieves the current file position for the file handle. For
|
| 947 | directories, the current file position has no meaning outside of the file
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 948 | system driver and as such the operation is not supported. An error is returned
|
| 949 | if FileHandle is a directory.
|
| 950 |
|
| 951 | @param FileHandle The open file handle on which to get the position.
|
| 952 | @param Position Byte position from begining of file.
|
| 953 |
|
| 954 | @retval EFI_SUCCESS the operation completed sucessfully.
|
| 955 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
| 956 | @retval EFI_UNSUPPORTED the request is not valid on directories.
|
| 957 | **/
|
| 958 | EFI_STATUS
|
| 959 | EFIAPI
|
| 960 | ShellGetFilePosition (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 961 | IN SHELL_FILE_HANDLE FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 962 | OUT UINT64 *Position
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 963 | )
|
| 964 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 965 | return (FileFunctionMap.GetFilePosition(FileHandle, Position));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 966 | }
|
| 967 | /**
|
| 968 | Flushes data on a file
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 969 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 970 | This function flushes all modified data associated with a file to a device.
|
| 971 |
|
| 972 | @param FileHandle The file handle on which to flush data
|
| 973 |
|
| 974 | @retval EFI_SUCCESS The data was flushed.
|
| 975 | @retval EFI_NO_MEDIA The device has no media.
|
| 976 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 977 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 978 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
| 979 | @retval EFI_ACCESS_DENIED The file was opened for read only.
|
| 980 | **/
|
| 981 | EFI_STATUS
|
| 982 | EFIAPI
|
| 983 | ShellFlushFile (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 984 | IN SHELL_FILE_HANDLE FileHandle
|
| 985 | )
|
| 986 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 987 | return (FileFunctionMap.FlushFile(FileHandle));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 988 | }
|
| 989 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 990 | /** Retrieve first entry from a directory.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 991 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 992 | This function takes an open directory handle and gets information from the
|
| 993 | first entry in the directory. A buffer is allocated to contain
|
| 994 | the information and a pointer to the buffer is returned in *Buffer. The
|
| 995 | caller can use ShellFindNextFile() to get subsequent directory entries.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 996 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 997 | The buffer will be freed by ShellFindNextFile() when the last directory
|
| 998 | entry is read. Otherwise, the caller must free the buffer, using FreePool,
|
| 999 | when finished with it.
|
| 1000 |
|
| 1001 | @param[in] DirHandle The file handle of the directory to search.
|
| 1002 | @param[out] Buffer The pointer to the buffer for the file's information.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1003 |
|
| 1004 | @retval EFI_SUCCESS Found the first file.
|
| 1005 | @retval EFI_NOT_FOUND Cannot find the directory.
|
| 1006 | @retval EFI_NO_MEDIA The device has no media.
|
| 1007 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 1008 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 1009 | @return Others status of ShellGetFileInfo, ShellSetFilePosition,
|
| 1010 | or ShellReadFile
|
| 1011 | **/
|
| 1012 | EFI_STATUS
|
| 1013 | EFIAPI
|
| 1014 | ShellFindFirstFile (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1015 | IN SHELL_FILE_HANDLE DirHandle,
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1016 | OUT EFI_FILE_INFO **Buffer
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1017 | )
|
| 1018 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1019 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1020 | // pass to file handle lib
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1021 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1022 | return (FileHandleFindFirstFile(DirHandle, Buffer));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1023 | }
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 1024 | /** Retrieve next entries from a directory.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1025 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 1026 | To use this function, the caller must first call the ShellFindFirstFile()
|
| 1027 | function to get the first directory entry. Subsequent directory entries are
|
| 1028 | retrieved by using the ShellFindNextFile() function. This function can
|
| 1029 | be called several times to get each entry from the directory. If the call of
|
| 1030 | ShellFindNextFile() retrieved the last directory entry, the next call of
|
| 1031 | this function will set *NoFile to TRUE and free the buffer.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1032 |
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 1033 | @param[in] DirHandle The file handle of the directory.
|
| 1034 | @param[out] Buffer The pointer to buffer for file's information.
|
| 1035 | @param[out] NoFile The pointer to boolean when last file is found.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1036 |
|
| 1037 | @retval EFI_SUCCESS Found the next file, or reached last file
|
| 1038 | @retval EFI_NO_MEDIA The device has no media.
|
| 1039 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 1040 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 1041 | **/
|
| 1042 | EFI_STATUS
|
| 1043 | EFIAPI
|
| 1044 | ShellFindNextFile(
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1045 | IN SHELL_FILE_HANDLE DirHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1046 | OUT EFI_FILE_INFO *Buffer,
|
| 1047 | OUT BOOLEAN *NoFile
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1048 | )
|
| 1049 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1050 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1051 | // pass to file handle lib
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1052 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1053 | return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1054 | }
|
| 1055 | /**
|
| 1056 | Retrieve the size of a file.
|
| 1057 |
|
| 1058 | if FileHandle is NULL then ASSERT()
|
| 1059 | if Size is NULL then ASSERT()
|
| 1060 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1061 | This function extracts the file size info from the FileHandle's EFI_FILE_INFO
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1062 | data.
|
| 1063 |
|
| 1064 | @param FileHandle file handle from which size is retrieved
|
| 1065 | @param Size pointer to size
|
| 1066 |
|
| 1067 | @retval EFI_SUCCESS operation was completed sucessfully
|
| 1068 | @retval EFI_DEVICE_ERROR cannot access the file
|
| 1069 | **/
|
| 1070 | EFI_STATUS
|
| 1071 | EFIAPI
|
| 1072 | ShellGetFileSize (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1073 | IN SHELL_FILE_HANDLE FileHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1074 | OUT UINT64 *Size
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1075 | )
|
| 1076 | {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1077 | return (FileFunctionMap.GetFileSize(FileHandle, Size));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1078 | }
|
| 1079 | /**
|
| 1080 | Retrieves the status of the break execution flag
|
| 1081 |
|
| 1082 | this function is useful to check whether the application is being asked to halt by the shell.
|
| 1083 |
|
| 1084 | @retval TRUE the execution break is enabled
|
| 1085 | @retval FALSE the execution break is not enabled
|
| 1086 | **/
|
| 1087 | BOOLEAN
|
| 1088 | EFIAPI
|
| 1089 | ShellGetExecutionBreakFlag(
|
| 1090 | VOID
|
| 1091 | )
|
| 1092 | {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1093 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1094 | // Check for UEFI Shell 2.0 protocols
|
| 1095 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1096 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1097 |
|
| 1098 | //
|
| 1099 | // We are using UEFI Shell 2.0; see if the event has been triggered
|
| 1100 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1101 | if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1102 | return (FALSE);
|
| 1103 | }
|
| 1104 | return (TRUE);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1105 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1106 |
|
| 1107 | //
|
| 1108 | // using EFI Shell; call the function to check
|
| 1109 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1110 | if (mEfiShellEnvironment2 != NULL) {
|
| 1111 | return (mEfiShellEnvironment2->GetExecutionBreak());
|
| 1112 | }
|
| 1113 |
|
| 1114 | return (FALSE);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1115 | }
|
| 1116 | /**
|
| 1117 | return the value of an environment variable
|
| 1118 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1119 | this function gets the value of the environment variable set by the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1120 | ShellSetEnvironmentVariable function
|
| 1121 |
|
| 1122 | @param EnvKey The key name of the environment variable.
|
| 1123 |
|
| 1124 | @retval NULL the named environment variable does not exist.
|
| 1125 | @return != NULL pointer to the value of the environment variable
|
| 1126 | **/
|
| 1127 | CONST CHAR16*
|
| 1128 | EFIAPI
|
| 1129 | ShellGetEnvironmentVariable (
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1130 | IN CONST CHAR16 *EnvKey
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1131 | )
|
| 1132 | {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1133 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1134 | // Check for UEFI Shell 2.0 protocols
|
| 1135 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1136 | if (gEfiShellProtocol != NULL) {
|
| 1137 | return (gEfiShellProtocol->GetEnv(EnvKey));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1138 | }
|
| 1139 |
|
| 1140 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1141 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1142 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1143 | if (mEfiShellEnvironment2 != NULL) {
|
| 1144 | return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
|
| 1145 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1146 |
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1147 | return NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1148 | }
|
| 1149 | /**
|
| 1150 | set the value of an environment variable
|
| 1151 |
|
| 1152 | This function changes the current value of the specified environment variable. If the
|
| 1153 | environment variable exists and the Value is an empty string, then the environment
|
| 1154 | variable is deleted. If the environment variable exists and the Value is not an empty
|
| 1155 | string, then the value of the environment variable is changed. If the environment
|
| 1156 | variable does not exist and the Value is an empty string, there is no action. If the
|
| 1157 | environment variable does not exist and the Value is a non-empty string, then the
|
| 1158 | environment variable is created and assigned the specified value.
|
| 1159 |
|
| 1160 | This is not supported pre-UEFI Shell 2.0.
|
| 1161 |
|
| 1162 | @param EnvKey The key name of the environment variable.
|
| 1163 | @param EnvVal The Value of the environment variable
|
| 1164 | @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
|
| 1165 |
|
| 1166 | @retval EFI_SUCCESS the operation was completed sucessfully
|
| 1167 | @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
|
| 1168 | **/
|
| 1169 | EFI_STATUS
|
| 1170 | EFIAPI
|
| 1171 | ShellSetEnvironmentVariable (
|
| 1172 | IN CONST CHAR16 *EnvKey,
|
| 1173 | IN CONST CHAR16 *EnvVal,
|
| 1174 | IN BOOLEAN Volatile
|
| 1175 | )
|
| 1176 | {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1177 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1178 | // Check for UEFI Shell 2.0 protocols
|
| 1179 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1180 | if (gEfiShellProtocol != NULL) {
|
| 1181 | return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1182 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1183 |
|
| 1184 | //
|
| 1185 | // This feature does not exist under EFI shell
|
| 1186 | //
|
| 1187 | return (EFI_UNSUPPORTED);
|
| 1188 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1189 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1190 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1191 | Cause the shell to parse and execute a command line.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1192 |
|
| 1193 | This function creates a nested instance of the shell and executes the specified
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1194 | command (CommandLine) with the specified environment (Environment). Upon return,
|
| 1195 | the status code returned by the specified command is placed in StatusCode.
|
| 1196 | If Environment is NULL, then the current environment is used and all changes made
|
| 1197 | by the commands executed will be reflected in the current environment. If the
|
| 1198 | Environment is non-NULL, then the changes made will be discarded.
|
| 1199 | The CommandLine is executed from the current working directory on the current
|
| 1200 | device.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1201 |
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1202 | The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1203 | environment. The values pointed to by the parameters will be unchanged by the
|
| 1204 | ShellExecute() function. The Output parameter has no effect in a
|
| 1205 | UEFI Shell 2.0 environment.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1206 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1207 | @param[in] ParentHandle The parent image starting the operation.
|
| 1208 | @param[in] CommandLine The pointer to a NULL terminated command line.
|
| 1209 | @param[in] Output True to display debug output. False to hide it.
|
| 1210 | @param[in] EnvironmentVariables Optional pointer to array of environment variables
|
| 1211 | in the form "x=y". If NULL, the current set is used.
|
| 1212 | @param[out] Status The status of the run command line.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1213 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1214 | @retval EFI_SUCCESS The operation completed sucessfully. Status
|
| 1215 | contains the status code returned.
|
| 1216 | @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
|
| 1217 | @retval EFI_OUT_OF_RESOURCES Out of resources.
|
| 1218 | @retval EFI_UNSUPPORTED The operation is not allowed.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1219 | **/
|
| 1220 | EFI_STATUS
|
| 1221 | EFIAPI
|
| 1222 | ShellExecute (
|
| 1223 | IN EFI_HANDLE *ParentHandle,
|
| 1224 | IN CHAR16 *CommandLine OPTIONAL,
|
| 1225 | IN BOOLEAN Output OPTIONAL,
|
| 1226 | IN CHAR16 **EnvironmentVariables OPTIONAL,
|
| 1227 | OUT EFI_STATUS *Status OPTIONAL
|
| 1228 | )
|
| 1229 | {
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1230 | EFI_STATUS CmdStatus;
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1231 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1232 | // Check for UEFI Shell 2.0 protocols
|
| 1233 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1234 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1235 | //
|
| 1236 | // Call UEFI Shell 2.0 version (not using Output parameter)
|
| 1237 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1238 | return (gEfiShellProtocol->Execute(ParentHandle,
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1239 | CommandLine,
|
| 1240 | EnvironmentVariables,
|
| 1241 | Status));
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1242 | }
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1243 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1244 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1245 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1246 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1247 | if (mEfiShellEnvironment2 != NULL) {
|
| 1248 | //
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1249 | // Call EFI Shell version.
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1250 | // Due to oddity in the EFI shell we want to dereference the ParentHandle here
|
| 1251 | //
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1252 | CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1253 | CommandLine,
|
| 1254 | Output));
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1255 | //
|
| 1256 | // No Status output parameter so just use the returned status
|
| 1257 | //
|
| 1258 | if (Status != NULL) {
|
| 1259 | *Status = CmdStatus;
|
| 1260 | }
|
| 1261 | //
|
| 1262 | // If there was an error, we can't tell if it was from the command or from
|
| 1263 | // the Execute() function, so we'll just assume the shell ran successfully
|
| 1264 | // and the error came from the command.
|
| 1265 | //
|
| 1266 | return EFI_SUCCESS;
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1267 | }
|
| 1268 |
|
| 1269 | return (EFI_UNSUPPORTED);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1270 | }
|
Brendan Jackman | 3877d0f | 2014-01-24 22:31:07 +0000 | [diff] [blame] | 1271 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1272 | /**
|
| 1273 | Retreives the current directory path
|
| 1274 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1275 | If the DeviceName is NULL, it returns the current device's current directory
|
| 1276 | name. If the DeviceName is not NULL, it returns the current directory name
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1277 | on specified drive.
|
| 1278 |
|
Qiu Shumin | fbd2dfa | 2015-10-23 02:03:20 +0000 | [diff] [blame] | 1279 | Note that the current directory string should exclude the tailing backslash character.
|
| 1280 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1281 | @param DeviceName the name of the drive to get directory on
|
| 1282 |
|
| 1283 | @retval NULL the directory does not exist
|
| 1284 | @return != NULL the directory
|
| 1285 | **/
|
| 1286 | CONST CHAR16*
|
| 1287 | EFIAPI
|
| 1288 | ShellGetCurrentDir (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1289 | IN CHAR16 * CONST DeviceName OPTIONAL
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1290 | )
|
| 1291 | {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1292 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1293 | // Check for UEFI Shell 2.0 protocols
|
| 1294 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1295 | if (gEfiShellProtocol != NULL) {
|
| 1296 | return (gEfiShellProtocol->GetCurDir(DeviceName));
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1297 | }
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1298 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1299 | //
|
jcarsey | 8bd282b | 2011-06-27 16:45:41 +0000 | [diff] [blame] | 1300 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1301 | //
|
jcarsey | 8bd282b | 2011-06-27 16:45:41 +0000 | [diff] [blame] | 1302 | if (mEfiShellEnvironment2 != NULL) {
|
| 1303 | return (mEfiShellEnvironment2->CurDir(DeviceName));
|
| 1304 | }
|
| 1305 |
|
| 1306 | return (NULL);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1307 | }
|
| 1308 | /**
|
| 1309 | sets (enabled or disabled) the page break mode
|
| 1310 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1311 | when page break mode is enabled the screen will stop scrolling
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1312 | and wait for operator input before scrolling a subsequent screen.
|
| 1313 |
|
| 1314 | @param CurrentState TRUE to enable and FALSE to disable
|
| 1315 | **/
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1316 | VOID
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1317 | EFIAPI
|
| 1318 | ShellSetPageBreakMode (
|
| 1319 | IN BOOLEAN CurrentState
|
| 1320 | )
|
| 1321 | {
|
| 1322 | //
|
| 1323 | // check for enabling
|
| 1324 | //
|
| 1325 | if (CurrentState != 0x00) {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1326 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1327 | // check for UEFI Shell 2.0
|
| 1328 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1329 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1330 | //
|
| 1331 | // Enable with UEFI 2.0 Shell
|
| 1332 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1333 | gEfiShellProtocol->EnablePageBreak();
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1334 | return;
|
| 1335 | } else {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1336 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1337 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1338 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1339 | if (mEfiShellEnvironment2 != NULL) {
|
| 1340 | //
|
| 1341 | // Enable with EFI Shell
|
| 1342 | //
|
| 1343 | mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
|
| 1344 | return;
|
| 1345 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1346 | }
|
| 1347 | } else {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1348 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1349 | // check for UEFI Shell 2.0
|
| 1350 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1351 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1352 | //
|
| 1353 | // Disable with UEFI 2.0 Shell
|
| 1354 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1355 | gEfiShellProtocol->DisablePageBreak();
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1356 | return;
|
| 1357 | } else {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1358 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1359 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1360 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1361 | if (mEfiShellEnvironment2 != NULL) {
|
| 1362 | //
|
| 1363 | // Disable with EFI Shell
|
| 1364 | //
|
| 1365 | mEfiShellEnvironment2->DisablePageBreak ();
|
| 1366 | return;
|
| 1367 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1368 | }
|
| 1369 | }
|
| 1370 | }
|
| 1371 |
|
| 1372 | ///
|
| 1373 | /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
|
| 1374 | /// This allows for the struct to be populated.
|
| 1375 | ///
|
| 1376 | typedef struct {
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1377 | LIST_ENTRY Link;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1378 | EFI_STATUS Status;
|
| 1379 | CHAR16 *FullName;
|
| 1380 | CHAR16 *FileName;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1381 | SHELL_FILE_HANDLE Handle;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1382 | EFI_FILE_INFO *Info;
|
| 1383 | } EFI_SHELL_FILE_INFO_NO_CONST;
|
| 1384 |
|
| 1385 | /**
|
| 1386 | Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
|
| 1387 |
|
| 1388 | if OldStyleFileList is NULL then ASSERT()
|
| 1389 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1390 | this function will convert a SHELL_FILE_ARG based list into a callee allocated
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1391 | EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
|
| 1392 | the ShellCloseFileMetaArg function.
|
| 1393 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 1394 | @param[in] FileList the EFI shell list type
|
| 1395 | @param[in, out] ListHead the list to add to
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1396 |
|
| 1397 | @retval the resultant head of the double linked new format list;
|
| 1398 | **/
|
| 1399 | LIST_ENTRY*
|
| 1400 | EFIAPI
|
| 1401 | InternalShellConvertFileListType (
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1402 | IN LIST_ENTRY *FileList,
|
| 1403 | IN OUT LIST_ENTRY *ListHead
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1404 | )
|
| 1405 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1406 | SHELL_FILE_ARG *OldInfo;
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1407 | LIST_ENTRY *Link;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1408 | EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
|
| 1409 |
|
| 1410 | //
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1411 | // ASSERTs
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1412 | //
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1413 | ASSERT(FileList != NULL);
|
| 1414 | ASSERT(ListHead != NULL);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1415 |
|
| 1416 | //
|
| 1417 | // enumerate through each member of the old list and copy
|
| 1418 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1419 | for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1420 | OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1421 | ASSERT(OldInfo != NULL);
|
| 1422 |
|
| 1423 | //
|
| 1424 | // Skip ones that failed to open...
|
| 1425 | //
|
| 1426 | if (OldInfo->Status != EFI_SUCCESS) {
|
| 1427 | continue;
|
| 1428 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1429 |
|
| 1430 | //
|
| 1431 | // make sure the old list was valid
|
| 1432 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1433 | ASSERT(OldInfo->Info != NULL);
|
| 1434 | ASSERT(OldInfo->FullName != NULL);
|
| 1435 | ASSERT(OldInfo->FileName != NULL);
|
| 1436 |
|
| 1437 | //
|
| 1438 | // allocate a new EFI_SHELL_FILE_INFO object
|
| 1439 | //
|
| 1440 | NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1441 | if (NewInfo == NULL) {
|
jcarsey | 7a95efd | 2011-10-13 16:08:18 +0000 | [diff] [blame] | 1442 | ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 1443 | ListHead = NULL;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1444 | break;
|
| 1445 | }
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1446 |
|
| 1447 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1448 | // copy the simple items
|
| 1449 | //
|
| 1450 | NewInfo->Handle = OldInfo->Handle;
|
| 1451 | NewInfo->Status = OldInfo->Status;
|
| 1452 |
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1453 | // old shell checks for 0 not NULL
|
| 1454 | OldInfo->Handle = 0;
|
| 1455 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1456 | //
|
| 1457 | // allocate new space to copy strings and structure
|
| 1458 | //
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 1459 | NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
|
| 1460 | NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
|
| 1461 | NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1462 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1463 | //
|
| 1464 | // make sure all the memory allocations were sucessful
|
| 1465 | //
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 1466 | if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 1467 | //
|
| 1468 | // Free the partially allocated new node
|
| 1469 | //
|
| 1470 | SHELL_FREE_NON_NULL(NewInfo->FullName);
|
| 1471 | SHELL_FREE_NON_NULL(NewInfo->FileName);
|
| 1472 | SHELL_FREE_NON_NULL(NewInfo->Info);
|
| 1473 | SHELL_FREE_NON_NULL(NewInfo);
|
| 1474 |
|
| 1475 | //
|
| 1476 | // Free the previously converted stuff
|
| 1477 | //
|
jcarsey | 7a95efd | 2011-10-13 16:08:18 +0000 | [diff] [blame] | 1478 | ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 1479 | ListHead = NULL;
|
| 1480 | break;
|
| 1481 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1482 |
|
| 1483 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1484 | // add that to the list
|
| 1485 | //
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1486 | InsertTailList(ListHead, &NewInfo->Link);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1487 | }
|
| 1488 | return (ListHead);
|
| 1489 | }
|
| 1490 | /**
|
| 1491 | Opens a group of files based on a path.
|
| 1492 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1493 | This function uses the Arg to open all the matching files. Each matched
|
Brendan Jackman | 7087931 | 2014-01-24 22:29:53 +0000 | [diff] [blame] | 1494 | file has a SHELL_FILE_INFO structure to record the file information. These
|
| 1495 | structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1496 | structures from ListHead to access each file. This function supports wildcards
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1497 | and will process '?' and '*' as such. the list must be freed with a call to
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1498 | ShellCloseFileMetaArg().
|
| 1499 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1500 | If you are NOT appending to an existing list *ListHead must be NULL. If
|
jcarsey | 5f7431d | 2009-07-10 18:06:01 +0000 | [diff] [blame] | 1501 | *ListHead is NULL then it must be callee freed.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1502 |
|
| 1503 | @param Arg pointer to path string
|
| 1504 | @param OpenMode mode to open files with
|
| 1505 | @param ListHead head of linked list of results
|
| 1506 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1507 | @retval EFI_SUCCESS the operation was sucessful and the list head
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1508 | contains the list of opened files
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1509 | @return != EFI_SUCCESS the operation failed
|
| 1510 |
|
| 1511 | @sa InternalShellConvertFileListType
|
| 1512 | **/
|
| 1513 | EFI_STATUS
|
| 1514 | EFIAPI
|
| 1515 | ShellOpenFileMetaArg (
|
| 1516 | IN CHAR16 *Arg,
|
| 1517 | IN UINT64 OpenMode,
|
| 1518 | IN OUT EFI_SHELL_FILE_INFO **ListHead
|
| 1519 | )
|
| 1520 | {
|
| 1521 | EFI_STATUS Status;
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1522 | LIST_ENTRY mOldStyleFileList;
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1523 | CHAR16 *CleanFilePathStr;
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1524 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1525 | //
|
| 1526 | // ASSERT that Arg and ListHead are not NULL
|
| 1527 | //
|
| 1528 | ASSERT(Arg != NULL);
|
| 1529 | ASSERT(ListHead != NULL);
|
| 1530 |
|
Qiu Shumin | 715096c | 2014-09-19 01:32:05 +0000 | [diff] [blame] | 1531 | CleanFilePathStr = NULL;
|
| 1532 |
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1533 | Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
|
| 1534 | if (EFI_ERROR (Status)) {
|
| 1535 | return Status;
|
| 1536 | }
|
| 1537 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1538 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1539 | // Check for UEFI Shell 2.0 protocols
|
| 1540 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1541 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 5f7431d | 2009-07-10 18:06:01 +0000 | [diff] [blame] | 1542 | if (*ListHead == NULL) {
|
| 1543 | *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
|
| 1544 | if (*ListHead == NULL) {
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1545 | FreePool(CleanFilePathStr);
|
jcarsey | 5f7431d | 2009-07-10 18:06:01 +0000 | [diff] [blame] | 1546 | return (EFI_OUT_OF_RESOURCES);
|
| 1547 | }
|
| 1548 | InitializeListHead(&((*ListHead)->Link));
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1549 | }
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1550 | Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1551 | OpenMode,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1552 | ListHead);
|
| 1553 | if (EFI_ERROR(Status)) {
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1554 | gEfiShellProtocol->RemoveDupInFileList(ListHead);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1555 | } else {
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1556 | Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1557 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1558 | if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
|
| 1559 | FreePool(*ListHead);
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1560 | FreePool(CleanFilePathStr);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1561 | *ListHead = NULL;
|
| 1562 | return (EFI_NOT_FOUND);
|
| 1563 | }
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1564 | FreePool(CleanFilePathStr);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1565 | return (Status);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1566 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1567 |
|
| 1568 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1569 | // Check for EFI shell
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1570 | //
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1571 | if (mEfiShellEnvironment2 != NULL) {
|
| 1572 | //
|
| 1573 | // make sure the list head is initialized
|
| 1574 | //
|
| 1575 | InitializeListHead(&mOldStyleFileList);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1576 |
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1577 | //
|
| 1578 | // Get the EFI Shell list of files
|
| 1579 | //
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1580 | Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1581 | if (EFI_ERROR(Status)) {
|
| 1582 | *ListHead = NULL;
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1583 | FreePool(CleanFilePathStr);
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1584 | return (Status);
|
| 1585 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1586 |
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1587 | if (*ListHead == NULL) {
|
| 1588 | *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
|
| 1589 | if (*ListHead == NULL) {
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1590 | FreePool(CleanFilePathStr);
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1591 | return (EFI_OUT_OF_RESOURCES);
|
| 1592 | }
|
| 1593 | InitializeListHead(&((*ListHead)->Link));
|
| 1594 | }
|
| 1595 |
|
| 1596 | //
|
| 1597 | // Convert that to equivalent of UEFI Shell 2.0 structure
|
| 1598 | //
|
| 1599 | InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
|
| 1600 |
|
| 1601 | //
|
| 1602 | // Free the EFI Shell version that was converted.
|
| 1603 | //
|
| 1604 | mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
|
| 1605 |
|
| 1606 | if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
|
| 1607 | FreePool(*ListHead);
|
| 1608 | *ListHead = NULL;
|
| 1609 | Status = EFI_NOT_FOUND;
|
| 1610 | }
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1611 | FreePool(CleanFilePathStr);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1612 | return (Status);
|
| 1613 | }
|
| 1614 |
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 1615 | FreePool(CleanFilePathStr);
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1616 | return (EFI_UNSUPPORTED);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1617 | }
|
| 1618 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1619 | Free the linked list returned from ShellOpenFileMetaArg.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1620 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1621 | if ListHead is NULL then ASSERT().
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1622 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1623 | @param ListHead the pointer to free.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1624 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1625 | @retval EFI_SUCCESS the operation was sucessful.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1626 | **/
|
| 1627 | EFI_STATUS
|
| 1628 | EFIAPI
|
| 1629 | ShellCloseFileMetaArg (
|
| 1630 | IN OUT EFI_SHELL_FILE_INFO **ListHead
|
| 1631 | )
|
| 1632 | {
|
| 1633 | LIST_ENTRY *Node;
|
| 1634 |
|
| 1635 | //
|
| 1636 | // ASSERT that ListHead is not NULL
|
| 1637 | //
|
| 1638 | ASSERT(ListHead != NULL);
|
| 1639 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1640 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1641 | // Check for UEFI Shell 2.0 protocols
|
| 1642 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 1643 | if (gEfiShellProtocol != NULL) {
|
| 1644 | return (gEfiShellProtocol->FreeFileList(ListHead));
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1645 | } else if (mEfiShellEnvironment2 != NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1646 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1647 | // Since this is EFI Shell version we need to free our internally made copy
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1648 | // of the list
|
| 1649 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1650 | for ( Node = GetFirstNode(&(*ListHead)->Link)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1651 | ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1652 | ; Node = GetFirstNode(&(*ListHead)->Link)) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1653 | RemoveEntryList(Node);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1654 | ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1655 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
|
| 1656 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
|
| 1657 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
|
| 1658 | FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
|
| 1659 | }
|
Jaben Carsey | 75a5e2e | 2014-01-10 16:42:45 +0000 | [diff] [blame] | 1660 | SHELL_FREE_NON_NULL(*ListHead);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1661 | return EFI_SUCCESS;
|
| 1662 | }
|
jcarsey | 92a5447 | 2011-06-27 20:33:13 +0000 | [diff] [blame] | 1663 |
|
| 1664 | return (EFI_UNSUPPORTED);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1665 | }
|
| 1666 |
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1667 | /**
|
| 1668 | Find a file by searching the CWD and then the path.
|
| 1669 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1670 | If FileName is NULL then ASSERT.
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1671 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1672 | If the return value is not NULL then the memory must be caller freed.
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1673 |
|
| 1674 | @param FileName Filename string.
|
| 1675 |
|
| 1676 | @retval NULL the file was not found
|
| 1677 | @return !NULL the full path to the file.
|
| 1678 | **/
|
| 1679 | CHAR16 *
|
| 1680 | EFIAPI
|
| 1681 | ShellFindFilePath (
|
| 1682 | IN CONST CHAR16 *FileName
|
| 1683 | )
|
| 1684 | {
|
| 1685 | CONST CHAR16 *Path;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1686 | SHELL_FILE_HANDLE Handle;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1687 | EFI_STATUS Status;
|
| 1688 | CHAR16 *RetVal;
|
| 1689 | CHAR16 *TestPath;
|
| 1690 | CONST CHAR16 *Walker;
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 1691 | UINTN Size;
|
jcarsey | 1cd45e7 | 2010-01-29 15:07:44 +0000 | [diff] [blame] | 1692 | CHAR16 *TempChar;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1693 |
|
| 1694 | RetVal = NULL;
|
| 1695 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1696 | //
|
| 1697 | // First make sure its not an absolute path.
|
| 1698 | //
|
| 1699 | Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
|
| 1700 | if (!EFI_ERROR(Status)){
|
| 1701 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
| 1702 | ASSERT(RetVal == NULL);
|
| 1703 | RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
|
| 1704 | ShellCloseFile(&Handle);
|
| 1705 | return (RetVal);
|
| 1706 | } else {
|
| 1707 | ShellCloseFile(&Handle);
|
| 1708 | }
|
| 1709 | }
|
| 1710 |
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1711 | Path = ShellGetEnvironmentVariable(L"cwd");
|
| 1712 | if (Path != NULL) {
|
Qiu Shumin | fbd2dfa | 2015-10-23 02:03:20 +0000 | [diff] [blame] | 1713 | Size = StrSize(Path) + sizeof(CHAR16);
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 1714 | Size += StrSize(FileName);
|
| 1715 | TestPath = AllocateZeroPool(Size);
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1716 | if (TestPath == NULL) {
|
| 1717 | return (NULL);
|
| 1718 | }
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1719 | StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
|
Qiu Shumin | fbd2dfa | 2015-10-23 02:03:20 +0000 | [diff] [blame] | 1720 | StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1721 | StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1722 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
|
| 1723 | if (!EFI_ERROR(Status)){
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1724 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
| 1725 | ASSERT(RetVal == NULL);
|
| 1726 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
|
| 1727 | ShellCloseFile(&Handle);
|
| 1728 | FreePool(TestPath);
|
| 1729 | return (RetVal);
|
| 1730 | } else {
|
| 1731 | ShellCloseFile(&Handle);
|
| 1732 | }
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1733 | }
|
| 1734 | FreePool(TestPath);
|
| 1735 | }
|
| 1736 | Path = ShellGetEnvironmentVariable(L"path");
|
| 1737 | if (Path != NULL) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1738 | Size = StrSize(Path)+sizeof(CHAR16);
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 1739 | Size += StrSize(FileName);
|
| 1740 | TestPath = AllocateZeroPool(Size);
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 1741 | if (TestPath == NULL) {
|
| 1742 | return (NULL);
|
| 1743 | }
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1744 | Walker = (CHAR16*)Path;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1745 | do {
|
| 1746 | CopyMem(TestPath, Walker, StrSize(Walker));
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 1747 | if (TestPath != NULL) {
|
| 1748 | TempChar = StrStr(TestPath, L";");
|
| 1749 | if (TempChar != NULL) {
|
| 1750 | *TempChar = CHAR_NULL;
|
| 1751 | }
|
| 1752 | if (TestPath[StrLen(TestPath)-1] != L'\\') {
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1753 | StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 1754 | }
|
jcarsey | 89e8537 | 2011-04-13 23:37:50 +0000 | [diff] [blame] | 1755 | if (FileName[0] == L'\\') {
|
| 1756 | FileName++;
|
| 1757 | }
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1758 | StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 1759 | if (StrStr(Walker, L";") != NULL) {
|
| 1760 | Walker = StrStr(Walker, L";") + 1;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1761 | } else {
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 1762 | Walker = NULL;
|
| 1763 | }
|
| 1764 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
|
| 1765 | if (!EFI_ERROR(Status)){
|
| 1766 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
| 1767 | ASSERT(RetVal == NULL);
|
| 1768 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
|
| 1769 | ShellCloseFile(&Handle);
|
| 1770 | break;
|
| 1771 | } else {
|
| 1772 | ShellCloseFile(&Handle);
|
| 1773 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1774 | }
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 1775 | }
|
| 1776 | } while (Walker != NULL && Walker[0] != CHAR_NULL);
|
| 1777 | FreePool(TestPath);
|
| 1778 | }
|
| 1779 | return (RetVal);
|
| 1780 | }
|
| 1781 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1782 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1783 | Find a file by searching the CWD and then the path with a variable set of file
|
| 1784 | extensions. If the file is not found it will append each extension in the list
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1785 | in the order provided and return the first one that is successful.
|
| 1786 |
|
| 1787 | If FileName is NULL, then ASSERT.
|
| 1788 | If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
|
| 1789 |
|
| 1790 | If the return value is not NULL then the memory must be caller freed.
|
| 1791 |
|
| 1792 | @param[in] FileName Filename string.
|
| 1793 | @param[in] FileExtension Semi-colon delimeted list of possible extensions.
|
| 1794 |
|
| 1795 | @retval NULL The file was not found.
|
| 1796 | @retval !NULL The path to the file.
|
| 1797 | **/
|
| 1798 | CHAR16 *
|
| 1799 | EFIAPI
|
| 1800 | ShellFindFilePathEx (
|
| 1801 | IN CONST CHAR16 *FileName,
|
| 1802 | IN CONST CHAR16 *FileExtension
|
| 1803 | )
|
| 1804 | {
|
| 1805 | CHAR16 *TestPath;
|
| 1806 | CHAR16 *RetVal;
|
| 1807 | CONST CHAR16 *ExtensionWalker;
|
jcarsey | 9e926b6 | 2010-01-14 20:26:39 +0000 | [diff] [blame] | 1808 | UINTN Size;
|
jcarsey | 1cd45e7 | 2010-01-29 15:07:44 +0000 | [diff] [blame] | 1809 | CHAR16 *TempChar;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1810 | CHAR16 *TempChar2;
|
jcarsey | 1cd45e7 | 2010-01-29 15:07:44 +0000 | [diff] [blame] | 1811 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1812 | ASSERT(FileName != NULL);
|
| 1813 | if (FileExtension == NULL) {
|
| 1814 | return (ShellFindFilePath(FileName));
|
| 1815 | }
|
| 1816 | RetVal = ShellFindFilePath(FileName);
|
| 1817 | if (RetVal != NULL) {
|
| 1818 | return (RetVal);
|
| 1819 | }
|
jcarsey | 9e926b6 | 2010-01-14 20:26:39 +0000 | [diff] [blame] | 1820 | Size = StrSize(FileName);
|
| 1821 | Size += StrSize(FileExtension);
|
| 1822 | TestPath = AllocateZeroPool(Size);
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1823 | if (TestPath == NULL) {
|
| 1824 | return (NULL);
|
| 1825 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1826 | for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1827 | StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1828 | if (ExtensionWalker != NULL) {
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 1829 | StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1830 | }
|
jcarsey | 1cd45e7 | 2010-01-29 15:07:44 +0000 | [diff] [blame] | 1831 | TempChar = StrStr(TestPath, L";");
|
| 1832 | if (TempChar != NULL) {
|
| 1833 | *TempChar = CHAR_NULL;
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1834 | }
|
| 1835 | RetVal = ShellFindFilePath(TestPath);
|
| 1836 | if (RetVal != NULL) {
|
| 1837 | break;
|
| 1838 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1839 | ASSERT(ExtensionWalker != NULL);
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 1840 | TempChar2 = StrStr(ExtensionWalker, L";");
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 1841 | }
|
| 1842 | FreePool(TestPath);
|
| 1843 | return (RetVal);
|
| 1844 | }
|
| 1845 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1846 | typedef struct {
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 1847 | LIST_ENTRY Link;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1848 | CHAR16 *Name;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1849 | SHELL_PARAM_TYPE Type;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1850 | CHAR16 *Value;
|
| 1851 | UINTN OriginalPosition;
|
| 1852 | } SHELL_PARAM_PACKAGE;
|
| 1853 |
|
| 1854 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1855 | Checks the list of valid arguments and returns TRUE if the item was found. If the
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1856 | return value is TRUE then the type parameter is set also.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1857 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1858 | if CheckList is NULL then ASSERT();
|
| 1859 | if Name is NULL then ASSERT();
|
| 1860 | if Type is NULL then ASSERT();
|
| 1861 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1862 | @param Name pointer to Name of parameter found
|
| 1863 | @param CheckList List to check against
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1864 | @param Type pointer to type of parameter if it was found
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1865 |
|
| 1866 | @retval TRUE the Parameter was found. Type is valid.
|
| 1867 | @retval FALSE the Parameter was not found. Type is not valid.
|
| 1868 | **/
|
| 1869 | BOOLEAN
|
| 1870 | EFIAPI
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1871 | InternalIsOnCheckList (
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1872 | IN CONST CHAR16 *Name,
|
| 1873 | IN CONST SHELL_PARAM_ITEM *CheckList,
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 1874 | OUT SHELL_PARAM_TYPE *Type
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1875 | )
|
| 1876 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1877 | SHELL_PARAM_ITEM *TempListItem;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 1878 | CHAR16 *TempString;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1879 |
|
| 1880 | //
|
| 1881 | // ASSERT that all 3 pointer parameters aren't NULL
|
| 1882 | //
|
| 1883 | ASSERT(CheckList != NULL);
|
| 1884 | ASSERT(Type != NULL);
|
| 1885 | ASSERT(Name != NULL);
|
| 1886 |
|
| 1887 | //
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1888 | // question mark and page break mode are always supported
|
| 1889 | //
|
| 1890 | if ((StrCmp(Name, L"-?") == 0) ||
|
| 1891 | (StrCmp(Name, L"-b") == 0)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1892 | ) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 1893 | *Type = TypeFlag;
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1894 | return (TRUE);
|
| 1895 | }
|
| 1896 |
|
| 1897 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1898 | // Enumerate through the list
|
| 1899 | //
|
| 1900 | for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
|
| 1901 | //
|
jcarsey | 9eb53ac | 2009-07-08 17:26:58 +0000 | [diff] [blame] | 1902 | // If the Type is TypeStart only check the first characters of the passed in param
|
| 1903 | // If it matches set the type and return TRUE
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1904 | //
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 1905 | if (TempListItem->Type == TypeStart) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 1906 | if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
|
| 1907 | *Type = TempListItem->Type;
|
| 1908 | return (TRUE);
|
| 1909 | }
|
| 1910 | TempString = NULL;
|
| 1911 | TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
|
| 1912 | if (TempString != NULL) {
|
| 1913 | if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
|
| 1914 | *Type = TempListItem->Type;
|
| 1915 | FreePool(TempString);
|
| 1916 | return (TRUE);
|
| 1917 | }
|
| 1918 | FreePool(TempString);
|
| 1919 | }
|
| 1920 | } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1921 | *Type = TempListItem->Type;
|
| 1922 | return (TRUE);
|
| 1923 | }
|
| 1924 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1925 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1926 | return (FALSE);
|
| 1927 | }
|
| 1928 | /**
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1929 | Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1930 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1931 | @param[in] Name pointer to Name of parameter found
|
| 1932 | @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 1933 | @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1934 |
|
| 1935 | @retval TRUE the Parameter is a flag.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1936 | @retval FALSE the Parameter not a flag.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1937 | **/
|
| 1938 | BOOLEAN
|
| 1939 | EFIAPI
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1940 | InternalIsFlag (
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1941 | IN CONST CHAR16 *Name,
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 1942 | IN CONST BOOLEAN AlwaysAllowNumbers,
|
| 1943 | IN CONST BOOLEAN TimeNumbers
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1944 | )
|
| 1945 | {
|
| 1946 | //
|
| 1947 | // ASSERT that Name isn't NULL
|
| 1948 | //
|
| 1949 | ASSERT(Name != NULL);
|
| 1950 |
|
| 1951 | //
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1952 | // If we accept numbers then dont return TRUE. (they will be values)
|
| 1953 | //
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 1954 | if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 1955 | return (FALSE);
|
| 1956 | }
|
| 1957 |
|
| 1958 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1959 | // If the Name has a /, +, or - as the first character return TRUE
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1960 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1961 | if ((Name[0] == L'/') ||
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1962 | (Name[0] == L'-') ||
|
| 1963 | (Name[0] == L'+')
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1964 | ) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1965 | return (TRUE);
|
| 1966 | }
|
| 1967 | return (FALSE);
|
| 1968 | }
|
| 1969 |
|
| 1970 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1971 | Checks the command line arguments passed against the list of valid ones.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1972 |
|
| 1973 | If no initialization is required, then return RETURN_SUCCESS.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1974 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1975 | @param[in] CheckList pointer to list of parameters to check
|
| 1976 | @param[out] CheckPackage pointer to pointer to list checked values
|
| 1977 | @param[out] ProblemParam optional pointer to pointer to unicode string for
|
jcarsey | d2b4564 | 2009-05-11 18:02:16 +0000 | [diff] [blame] | 1978 | the paramater that caused failure. If used then the
|
| 1979 | caller is responsible for freeing the memory.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 1980 | @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
|
| 1981 | @param[in] Argv pointer to array of parameters
|
| 1982 | @param[in] Argc Count of parameters in Argv
|
| 1983 | @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1984 |
|
| 1985 | @retval EFI_SUCCESS The operation completed sucessfully.
|
| 1986 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed
|
| 1987 | @retval EFI_INVALID_PARAMETER A parameter was invalid
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1988 | @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
|
| 1989 | duplicated. the duplicated command line argument
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1990 | was returned in ProblemParam if provided.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 1991 | @retval EFI_NOT_FOUND a argument required a value that was missing.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 1992 | the invalid command line argument was returned in
|
| 1993 | ProblemParam if provided.
|
| 1994 | **/
|
| 1995 | EFI_STATUS
|
| 1996 | EFIAPI
|
| 1997 | InternalCommandLineParse (
|
| 1998 | IN CONST SHELL_PARAM_ITEM *CheckList,
|
| 1999 | OUT LIST_ENTRY **CheckPackage,
|
| 2000 | OUT CHAR16 **ProblemParam OPTIONAL,
|
| 2001 | IN BOOLEAN AutoPageBreak,
|
| 2002 | IN CONST CHAR16 **Argv,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2003 | IN UINTN Argc,
|
| 2004 | IN BOOLEAN AlwaysAllowNumbers
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2005 | )
|
| 2006 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2007 | UINTN LoopCounter;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2008 | SHELL_PARAM_TYPE CurrentItemType;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2009 | SHELL_PARAM_PACKAGE *CurrentItemPackage;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2010 | UINTN GetItemValue;
|
| 2011 | UINTN ValueSize;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2012 | UINTN Count;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2013 | CONST CHAR16 *TempPointer;
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 2014 | UINTN CurrentValueSize;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2015 |
|
| 2016 | CurrentItemPackage = NULL;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2017 | GetItemValue = 0;
|
| 2018 | ValueSize = 0;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2019 | Count = 0;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2020 |
|
| 2021 | //
|
| 2022 | // If there is only 1 item we dont need to do anything
|
| 2023 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2024 | if (Argc < 1) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2025 | *CheckPackage = NULL;
|
| 2026 | return (EFI_SUCCESS);
|
| 2027 | }
|
| 2028 |
|
| 2029 | //
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2030 | // ASSERTs
|
| 2031 | //
|
| 2032 | ASSERT(CheckList != NULL);
|
| 2033 | ASSERT(Argv != NULL);
|
| 2034 |
|
| 2035 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2036 | // initialize the linked list
|
| 2037 | //
|
| 2038 | *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
|
sfu5 | 02a758c | 2011-10-08 02:55:30 +0000 | [diff] [blame] | 2039 | if (*CheckPackage == NULL) {
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2040 | return (EFI_OUT_OF_RESOURCES);
|
sfu5 | 02a758c | 2011-10-08 02:55:30 +0000 | [diff] [blame] | 2041 | }
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2042 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2043 | InitializeListHead(*CheckPackage);
|
| 2044 |
|
| 2045 | //
|
| 2046 | // loop through each of the arguments
|
| 2047 | //
|
| 2048 | for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
|
| 2049 | if (Argv[LoopCounter] == NULL) {
|
| 2050 | //
|
| 2051 | // do nothing for NULL argv
|
| 2052 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2053 | } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2054 | //
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2055 | // We might have leftover if last parameter didnt have optional value
|
| 2056 | //
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2057 | if (GetItemValue != 0) {
|
| 2058 | GetItemValue = 0;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2059 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
| 2060 | }
|
| 2061 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2062 | // this is a flag
|
| 2063 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2064 | CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2065 | if (CurrentItemPackage == NULL) {
|
| 2066 | ShellCommandLineFreeVarList(*CheckPackage);
|
| 2067 | *CheckPackage = NULL;
|
| 2068 | return (EFI_OUT_OF_RESOURCES);
|
| 2069 | }
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 2070 | CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2071 | if (CurrentItemPackage->Name == NULL) {
|
| 2072 | ShellCommandLineFreeVarList(*CheckPackage);
|
| 2073 | *CheckPackage = NULL;
|
| 2074 | return (EFI_OUT_OF_RESOURCES);
|
| 2075 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2076 | CurrentItemPackage->Type = CurrentItemType;
|
| 2077 | CurrentItemPackage->OriginalPosition = (UINTN)(-1);
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2078 | CurrentItemPackage->Value = NULL;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2079 |
|
| 2080 | //
|
| 2081 | // Does this flag require a value
|
| 2082 | //
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2083 | switch (CurrentItemPackage->Type) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2084 | //
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2085 | // possibly trigger the next loop(s) to populate the value of this item
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2086 | //
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2087 | case TypeValue:
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 2088 | case TypeTimeValue:
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2089 | GetItemValue = 1;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2090 | ValueSize = 0;
|
| 2091 | break;
|
| 2092 | case TypeDoubleValue:
|
| 2093 | GetItemValue = 2;
|
| 2094 | ValueSize = 0;
|
| 2095 | break;
|
| 2096 | case TypeMaxValue:
|
| 2097 | GetItemValue = (UINTN)(-1);
|
| 2098 | ValueSize = 0;
|
| 2099 | break;
|
| 2100 | default:
|
| 2101 | //
|
| 2102 | // this item has no value expected; we are done
|
| 2103 | //
|
| 2104 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
| 2105 | ASSERT(GetItemValue == 0);
|
| 2106 | break;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2107 | }
|
Qiu Shumin | af7a3a5 | 2015-03-13 02:04:17 +0000 | [diff] [blame] | 2108 | } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2109 | //
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2110 | // get the item VALUE for a previous flag
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2111 | //
|
Qiu Shumin | 484dd08 | 2015-04-01 00:49:05 +0000 | [diff] [blame] | 2112 | CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
| 2113 | CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
|
| 2114 | ASSERT(CurrentItemPackage->Value != NULL);
|
| 2115 | if (ValueSize == 0) {
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 2116 | StrCpyS( CurrentItemPackage->Value,
|
| 2117 | CurrentValueSize/sizeof(CHAR16),
|
| 2118 | Argv[LoopCounter]
|
| 2119 | );
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2120 | } else {
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 2121 | StrCatS( CurrentItemPackage->Value,
|
| 2122 | CurrentValueSize/sizeof(CHAR16),
|
| 2123 | L" "
|
| 2124 | );
|
| 2125 | StrCatS( CurrentItemPackage->Value,
|
| 2126 | CurrentValueSize/sizeof(CHAR16),
|
| 2127 | Argv[LoopCounter]
|
| 2128 | );
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2129 | }
|
Qiu Shumin | 484dd08 | 2015-04-01 00:49:05 +0000 | [diff] [blame] | 2130 | ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
| 2131 |
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2132 | GetItemValue--;
|
| 2133 | if (GetItemValue == 0) {
|
| 2134 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
| 2135 | }
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 2136 | } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2137 | //
|
| 2138 | // add this one as a non-flag
|
| 2139 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2140 |
|
| 2141 | TempPointer = Argv[LoopCounter];
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 2142 | if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2143 | || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
|
| 2144 | || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
|
| 2145 | ){
|
| 2146 | TempPointer++;
|
| 2147 | }
|
| 2148 | CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2149 | if (CurrentItemPackage == NULL) {
|
| 2150 | ShellCommandLineFreeVarList(*CheckPackage);
|
| 2151 | *CheckPackage = NULL;
|
| 2152 | return (EFI_OUT_OF_RESOURCES);
|
| 2153 | }
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2154 | CurrentItemPackage->Name = NULL;
|
| 2155 | CurrentItemPackage->Type = TypePosition;
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 2156 | CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2157 | if (CurrentItemPackage->Value == NULL) {
|
| 2158 | ShellCommandLineFreeVarList(*CheckPackage);
|
| 2159 | *CheckPackage = NULL;
|
| 2160 | return (EFI_OUT_OF_RESOURCES);
|
| 2161 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2162 | CurrentItemPackage->OriginalPosition = Count++;
|
jcarsey | 9b3bf08 | 2009-06-23 21:15:07 +0000 | [diff] [blame] | 2163 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2164 | } else {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2165 | //
|
| 2166 | // this was a non-recognised flag... error!
|
| 2167 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2168 | if (ProblemParam != NULL) {
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 2169 | *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2170 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2171 | ShellCommandLineFreeVarList(*CheckPackage);
|
| 2172 | *CheckPackage = NULL;
|
| 2173 | return (EFI_VOLUME_CORRUPTED);
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2174 | }
|
| 2175 | }
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2176 | if (GetItemValue != 0) {
|
| 2177 | GetItemValue = 0;
|
| 2178 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
| 2179 | }
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2180 | //
|
| 2181 | // support for AutoPageBreak
|
| 2182 | //
|
| 2183 | if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
|
| 2184 | ShellSetPageBreakMode(TRUE);
|
| 2185 | }
|
| 2186 | return (EFI_SUCCESS);
|
| 2187 | }
|
| 2188 |
|
| 2189 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2190 | Checks the command line arguments passed against the list of valid ones.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2191 | Optionally removes NULL values first.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2192 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2193 | If no initialization is required, then return RETURN_SUCCESS.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2194 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2195 | @param[in] CheckList The pointer to list of parameters to check.
|
| 2196 | @param[out] CheckPackage The package of checked values.
|
| 2197 | @param[out] ProblemParam Optional pointer to pointer to unicode string for
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2198 | the paramater that caused failure.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2199 | @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
|
| 2200 | @param[in] AlwaysAllowNumbers Will never fail for number based flags.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2201 |
|
| 2202 | @retval EFI_SUCCESS The operation completed sucessfully.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2203 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
|
| 2204 | @retval EFI_INVALID_PARAMETER A parameter was invalid.
|
| 2205 | @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
|
| 2206 | @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2207 | of the command line arguments was returned in
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2208 | ProblemParam if provided.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2209 | @retval EFI_NOT_FOUND A argument required a value that was missing.
|
| 2210 | The invalid command line argument was returned in
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2211 | ProblemParam if provided.
|
| 2212 | **/
|
| 2213 | EFI_STATUS
|
| 2214 | EFIAPI
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2215 | ShellCommandLineParseEx (
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2216 | IN CONST SHELL_PARAM_ITEM *CheckList,
|
| 2217 | OUT LIST_ENTRY **CheckPackage,
|
| 2218 | OUT CHAR16 **ProblemParam OPTIONAL,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2219 | IN BOOLEAN AutoPageBreak,
|
| 2220 | IN BOOLEAN AlwaysAllowNumbers
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2221 | )
|
| 2222 | {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2223 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2224 | // ASSERT that CheckList and CheckPackage aren't NULL
|
| 2225 | //
|
| 2226 | ASSERT(CheckList != NULL);
|
| 2227 | ASSERT(CheckPackage != NULL);
|
| 2228 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2229 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2230 | // Check for UEFI Shell 2.0 protocols
|
| 2231 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 2232 | if (gEfiShellParametersProtocol != NULL) {
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2233 | return (InternalCommandLineParse(CheckList,
|
| 2234 | CheckPackage,
|
| 2235 | ProblemParam,
|
| 2236 | AutoPageBreak,
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 2237 | (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
|
| 2238 | gEfiShellParametersProtocol->Argc,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2239 | AlwaysAllowNumbers));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2240 | }
|
| 2241 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2242 | //
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2243 | // ASSERT That EFI Shell is not required
|
| 2244 | //
|
| 2245 | ASSERT (mEfiShellInterface != NULL);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2246 | return (InternalCommandLineParse(CheckList,
|
| 2247 | CheckPackage,
|
| 2248 | ProblemParam,
|
| 2249 | AutoPageBreak,
|
jljusten | 08d7f8e | 2009-06-15 18:42:13 +0000 | [diff] [blame] | 2250 | (CONST CHAR16**) mEfiShellInterface->Argv,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2251 | mEfiShellInterface->Argc,
|
| 2252 | AlwaysAllowNumbers));
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2253 | }
|
| 2254 |
|
| 2255 | /**
|
| 2256 | Frees shell variable list that was returned from ShellCommandLineParse.
|
| 2257 |
|
| 2258 | This function will free all the memory that was used for the CheckPackage
|
| 2259 | list of postprocessed shell arguments.
|
| 2260 |
|
| 2261 | this function has no return value.
|
| 2262 |
|
| 2263 | if CheckPackage is NULL, then return
|
| 2264 |
|
| 2265 | @param CheckPackage the list to de-allocate
|
| 2266 | **/
|
| 2267 | VOID
|
| 2268 | EFIAPI
|
| 2269 | ShellCommandLineFreeVarList (
|
| 2270 | IN LIST_ENTRY *CheckPackage
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2271 | )
|
| 2272 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2273 | LIST_ENTRY *Node;
|
| 2274 |
|
| 2275 | //
|
| 2276 | // check for CheckPackage == NULL
|
| 2277 | //
|
| 2278 | if (CheckPackage == NULL) {
|
| 2279 | return;
|
| 2280 | }
|
| 2281 |
|
| 2282 | //
|
| 2283 | // for each node in the list
|
| 2284 | //
|
jcarsey | 9eb53ac | 2009-07-08 17:26:58 +0000 | [diff] [blame] | 2285 | for ( Node = GetFirstNode(CheckPackage)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2286 | ; !IsListEmpty(CheckPackage)
|
jcarsey | 9eb53ac | 2009-07-08 17:26:58 +0000 | [diff] [blame] | 2287 | ; Node = GetFirstNode(CheckPackage)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2288 | ){
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2289 | //
|
| 2290 | // Remove it from the list
|
| 2291 | //
|
| 2292 | RemoveEntryList(Node);
|
| 2293 |
|
| 2294 | //
|
| 2295 | // if it has a name free the name
|
| 2296 | //
|
| 2297 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
|
| 2298 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
|
| 2299 | }
|
| 2300 |
|
| 2301 | //
|
| 2302 | // if it has a value free the value
|
| 2303 | //
|
| 2304 | if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
|
| 2305 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
|
| 2306 | }
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2307 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2308 | //
|
| 2309 | // free the node structure
|
| 2310 | //
|
| 2311 | FreePool((SHELL_PARAM_PACKAGE*)Node);
|
| 2312 | }
|
| 2313 | //
|
| 2314 | // free the list head node
|
| 2315 | //
|
| 2316 | FreePool(CheckPackage);
|
| 2317 | }
|
| 2318 | /**
|
| 2319 | Checks for presence of a flag parameter
|
| 2320 |
|
| 2321 | flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
|
| 2322 |
|
| 2323 | if CheckPackage is NULL then return FALSE.
|
| 2324 | if KeyString is NULL then ASSERT()
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2325 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2326 | @param CheckPackage The package of parsed command line arguments
|
| 2327 | @param KeyString the Key of the command line argument to check for
|
| 2328 |
|
| 2329 | @retval TRUE the flag is on the command line
|
| 2330 | @retval FALSE the flag is not on the command line
|
| 2331 | **/
|
| 2332 | BOOLEAN
|
| 2333 | EFIAPI
|
| 2334 | ShellCommandLineGetFlag (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2335 | IN CONST LIST_ENTRY * CONST CheckPackage,
|
| 2336 | IN CONST CHAR16 * CONST KeyString
|
| 2337 | )
|
| 2338 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2339 | LIST_ENTRY *Node;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2340 | CHAR16 *TempString;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2341 |
|
| 2342 | //
|
ydong10 | 0c1950b | 2011-10-13 02:37:35 +0000 | [diff] [blame] | 2343 | // return FALSE for no package or KeyString is NULL
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2344 | //
|
ydong10 | 0c1950b | 2011-10-13 02:37:35 +0000 | [diff] [blame] | 2345 | if (CheckPackage == NULL || KeyString == NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2346 | return (FALSE);
|
| 2347 | }
|
| 2348 |
|
| 2349 | //
|
| 2350 | // enumerate through the list of parametrs
|
| 2351 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2352 | for ( Node = GetFirstNode(CheckPackage)
|
| 2353 | ; !IsNull (CheckPackage, Node)
|
| 2354 | ; Node = GetNextNode(CheckPackage, Node)
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2355 | ){
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2356 | //
|
| 2357 | // If the Name matches, return TRUE (and there may be NULL name)
|
| 2358 | //
|
| 2359 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
|
jcarsey | 9eb53ac | 2009-07-08 17:26:58 +0000 | [diff] [blame] | 2360 | //
|
| 2361 | // If Type is TypeStart then only compare the begining of the strings
|
| 2362 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2363 | if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
|
| 2364 | if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
|
| 2365 | return (TRUE);
|
| 2366 | }
|
| 2367 | TempString = NULL;
|
| 2368 | TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
|
| 2369 | if (TempString != NULL) {
|
| 2370 | if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
|
| 2371 | FreePool(TempString);
|
| 2372 | return (TRUE);
|
| 2373 | }
|
| 2374 | FreePool(TempString);
|
| 2375 | }
|
| 2376 | } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2377 | return (TRUE);
|
| 2378 | }
|
| 2379 | }
|
| 2380 | }
|
| 2381 | return (FALSE);
|
| 2382 | }
|
| 2383 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2384 | Returns value from command line argument.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2385 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2386 | Value parameters are in the form of "-<Key> value" or "/<Key> value".
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2387 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2388 | If CheckPackage is NULL, then return NULL.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2389 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2390 | @param[in] CheckPackage The package of parsed command line arguments.
|
| 2391 | @param[in] KeyString The Key of the command line argument to check for.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2392 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2393 | @retval NULL The flag is not on the command line.
|
| 2394 | @retval !=NULL The pointer to unicode string of the value.
|
| 2395 | **/
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2396 | CONST CHAR16*
|
| 2397 | EFIAPI
|
| 2398 | ShellCommandLineGetValue (
|
| 2399 | IN CONST LIST_ENTRY *CheckPackage,
|
| 2400 | IN CHAR16 *KeyString
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2401 | )
|
| 2402 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2403 | LIST_ENTRY *Node;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2404 | CHAR16 *TempString;
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2405 |
|
| 2406 | //
|
ydong10 | 0c1950b | 2011-10-13 02:37:35 +0000 | [diff] [blame] | 2407 | // return NULL for no package or KeyString is NULL
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2408 | //
|
ydong10 | 0c1950b | 2011-10-13 02:37:35 +0000 | [diff] [blame] | 2409 | if (CheckPackage == NULL || KeyString == NULL) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2410 | return (NULL);
|
| 2411 | }
|
| 2412 |
|
| 2413 | //
|
| 2414 | // enumerate through the list of parametrs
|
| 2415 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2416 | for ( Node = GetFirstNode(CheckPackage)
|
| 2417 | ; !IsNull (CheckPackage, Node)
|
| 2418 | ; Node = GetNextNode(CheckPackage, Node)
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2419 | ){
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2420 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2421 | // If the Name matches, return TRUE (and there may be NULL name)
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2422 | //
|
| 2423 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
|
jcarsey | 9eb53ac | 2009-07-08 17:26:58 +0000 | [diff] [blame] | 2424 | //
|
| 2425 | // If Type is TypeStart then only compare the begining of the strings
|
| 2426 | //
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2427 | if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
|
| 2428 | if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
|
| 2429 | return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
|
| 2430 | }
|
| 2431 | TempString = NULL;
|
| 2432 | TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
|
| 2433 | if (TempString != NULL) {
|
| 2434 | if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
|
| 2435 | FreePool(TempString);
|
| 2436 | return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
|
| 2437 | }
|
| 2438 | FreePool(TempString);
|
| 2439 | }
|
| 2440 | } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2441 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);
|
| 2442 | }
|
| 2443 | }
|
| 2444 | }
|
| 2445 | return (NULL);
|
| 2446 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2447 |
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2448 | /**
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2449 | Returns raw value from command line argument.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2450 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2451 | Raw value parameters are in the form of "value" in a specific position in the list.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2452 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2453 | If CheckPackage is NULL, then return NULL.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2454 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2455 | @param[in] CheckPackage The package of parsed command line arguments.
|
| 2456 | @param[in] Position The position of the value.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2457 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2458 | @retval NULL The flag is not on the command line.
|
| 2459 | @retval !=NULL The pointer to unicode string of the value.
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2460 | **/
|
| 2461 | CONST CHAR16*
|
| 2462 | EFIAPI
|
| 2463 | ShellCommandLineGetRawValue (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2464 | IN CONST LIST_ENTRY * CONST CheckPackage,
|
| 2465 | IN UINTN Position
|
| 2466 | )
|
| 2467 | {
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2468 | LIST_ENTRY *Node;
|
| 2469 |
|
| 2470 | //
|
| 2471 | // check for CheckPackage == NULL
|
| 2472 | //
|
| 2473 | if (CheckPackage == NULL) {
|
| 2474 | return (NULL);
|
| 2475 | }
|
| 2476 |
|
| 2477 | //
|
| 2478 | // enumerate through the list of parametrs
|
| 2479 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2480 | for ( Node = GetFirstNode(CheckPackage)
|
| 2481 | ; !IsNull (CheckPackage, Node)
|
| 2482 | ; Node = GetNextNode(CheckPackage, Node)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2483 | ){
|
jcarsey | 94b17fa | 2009-05-07 18:46:18 +0000 | [diff] [blame] | 2484 | //
|
| 2485 | // If the position matches, return the value
|
| 2486 | //
|
| 2487 | if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
|
| 2488 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);
|
| 2489 | }
|
| 2490 | }
|
| 2491 | return (NULL);
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2492 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2493 |
|
| 2494 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2495 | returns the number of command line value parameters that were parsed.
|
| 2496 |
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2497 | this will not include flags.
|
| 2498 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2499 | @param[in] CheckPackage The package of parsed command line arguments.
|
| 2500 |
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2501 | @retval (UINTN)-1 No parsing has ocurred
|
| 2502 | @return other The number of value parameters found
|
| 2503 | **/
|
| 2504 | UINTN
|
| 2505 | EFIAPI
|
| 2506 | ShellCommandLineGetCount(
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2507 | IN CONST LIST_ENTRY *CheckPackage
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 2508 | )
|
| 2509 | {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2510 | LIST_ENTRY *Node1;
|
| 2511 | UINTN Count;
|
| 2512 |
|
| 2513 | if (CheckPackage == NULL) {
|
| 2514 | return (0);
|
| 2515 | }
|
| 2516 | for ( Node1 = GetFirstNode(CheckPackage), Count = 0
|
| 2517 | ; !IsNull (CheckPackage, Node1)
|
| 2518 | ; Node1 = GetNextNode(CheckPackage, Node1)
|
| 2519 | ){
|
| 2520 | if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
|
| 2521 | Count++;
|
| 2522 | }
|
| 2523 | }
|
| 2524 | return (Count);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2525 | }
|
| 2526 |
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2527 | /**
|
Bruce Cran | cceb4eb | 2015-07-08 01:54:46 +0000 | [diff] [blame] | 2528 | Determines if a parameter is duplicated.
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 2529 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2530 | If Param is not NULL then it will point to a callee allocated string buffer
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 2531 | with the parameter value if a duplicate is found.
|
| 2532 |
|
| 2533 | If CheckPackage is NULL, then ASSERT.
|
| 2534 |
|
| 2535 | @param[in] CheckPackage The package of parsed command line arguments.
|
| 2536 | @param[out] Param Upon finding one, a pointer to the duplicated parameter.
|
| 2537 |
|
| 2538 | @retval EFI_SUCCESS No parameters were duplicated.
|
| 2539 | @retval EFI_DEVICE_ERROR A duplicate was found.
|
| 2540 | **/
|
| 2541 | EFI_STATUS
|
| 2542 | EFIAPI
|
| 2543 | ShellCommandLineCheckDuplicate (
|
| 2544 | IN CONST LIST_ENTRY *CheckPackage,
|
| 2545 | OUT CHAR16 **Param
|
| 2546 | )
|
| 2547 | {
|
| 2548 | LIST_ENTRY *Node1;
|
| 2549 | LIST_ENTRY *Node2;
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2550 |
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 2551 | ASSERT(CheckPackage != NULL);
|
| 2552 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2553 | for ( Node1 = GetFirstNode(CheckPackage)
|
| 2554 | ; !IsNull (CheckPackage, Node1)
|
| 2555 | ; Node1 = GetNextNode(CheckPackage, Node1)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2556 | ){
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2557 | for ( Node2 = GetNextNode(CheckPackage, Node1)
|
| 2558 | ; !IsNull (CheckPackage, Node2)
|
| 2559 | ; Node2 = GetNextNode(CheckPackage, Node2)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2560 | ){
|
| 2561 | if ((((SHELL_PARAM_PACKAGE*)Node1)->Name != NULL) && (((SHELL_PARAM_PACKAGE*)Node2)->Name != NULL) && StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 2562 | if (Param != NULL) {
|
| 2563 | *Param = NULL;
|
| 2564 | *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
|
| 2565 | }
|
| 2566 | return (EFI_DEVICE_ERROR);
|
| 2567 | }
|
| 2568 | }
|
| 2569 | }
|
| 2570 | return (EFI_SUCCESS);
|
| 2571 | }
|
| 2572 |
|
| 2573 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2574 | This is a find and replace function. Upon successful return the NewString is a copy of
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2575 | SourceString with each instance of FindTarget replaced with ReplaceWith.
|
| 2576 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 2577 | If SourceString and NewString overlap the behavior is undefined.
|
| 2578 |
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2579 | If the string would grow bigger than NewSize it will halt and return error.
|
| 2580 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 2581 | @param[in] SourceString The string with source buffer.
|
| 2582 | @param[in, out] NewString The string with resultant buffer.
|
| 2583 | @param[in] NewSize The size in bytes of NewString.
|
| 2584 | @param[in] FindTarget The string to look for.
|
| 2585 | @param[in] ReplaceWith The string to replace FindTarget with.
|
| 2586 | @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
|
| 2587 | immediately before it.
|
| 2588 | @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2589 |
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 2590 | @retval EFI_INVALID_PARAMETER SourceString was NULL.
|
| 2591 | @retval EFI_INVALID_PARAMETER NewString was NULL.
|
| 2592 | @retval EFI_INVALID_PARAMETER FindTarget was NULL.
|
| 2593 | @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
|
| 2594 | @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
|
| 2595 | @retval EFI_INVALID_PARAMETER SourceString had length < 1.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2596 | @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 2597 | the new string (truncation occurred).
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2598 | @retval EFI_SUCCESS The string was successfully copied with replacement.
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2599 | **/
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2600 | EFI_STATUS
|
| 2601 | EFIAPI
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2602 | ShellCopySearchAndReplace(
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2603 | IN CHAR16 CONST *SourceString,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2604 | IN OUT CHAR16 *NewString,
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2605 | IN UINTN NewSize,
|
| 2606 | IN CONST CHAR16 *FindTarget,
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 2607 | IN CONST CHAR16 *ReplaceWith,
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2608 | IN CONST BOOLEAN SkipPreCarrot,
|
| 2609 | IN CONST BOOLEAN ParameterReplacing
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2610 | )
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2611 | {
|
jcarsey | 0158294 | 2009-07-10 19:46:17 +0000 | [diff] [blame] | 2612 | UINTN Size;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2613 | CHAR16 *Replace;
|
| 2614 |
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2615 | if ( (SourceString == NULL)
|
| 2616 | || (NewString == NULL)
|
| 2617 | || (FindTarget == NULL)
|
| 2618 | || (ReplaceWith == NULL)
|
| 2619 | || (StrLen(FindTarget) < 1)
|
| 2620 | || (StrLen(SourceString) < 1)
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2621 | ){
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2622 | return (EFI_INVALID_PARAMETER);
|
| 2623 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2624 | Replace = NULL;
|
| 2625 | if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
|
| 2626 | Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
|
| 2627 | } else {
|
| 2628 | Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 2629 | if (Replace != NULL) {
|
| 2630 | UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
|
| 2631 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2632 | }
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 2633 | if (Replace == NULL) {
|
| 2634 | return (EFI_OUT_OF_RESOURCES);
|
| 2635 | }
|
Jaben Carsey | 98c16be | 2014-08-19 21:00:34 +0000 | [diff] [blame] | 2636 | NewString = ZeroMem(NewString, NewSize);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2637 | while (*SourceString != CHAR_NULL) {
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 2638 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2639 | // if we find the FindTarget and either Skip == FALSE or Skip and we
|
jcarsey | 969c783 | 2010-01-13 16:46:33 +0000 | [diff] [blame] | 2640 | // dont have a carrot do a replace...
|
| 2641 | //
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2642 | if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2643 | && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
|
| 2644 | ){
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2645 | SourceString += StrLen(FindTarget);
|
jcarsey | 0158294 | 2009-07-10 19:46:17 +0000 | [diff] [blame] | 2646 | Size = StrSize(NewString);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2647 | if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
|
| 2648 | FreePool(Replace);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2649 | return (EFI_BUFFER_TOO_SMALL);
|
| 2650 | }
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 2651 | StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2652 | } else {
|
jcarsey | 0158294 | 2009-07-10 19:46:17 +0000 | [diff] [blame] | 2653 | Size = StrSize(NewString);
|
| 2654 | if (Size + sizeof(CHAR16) > NewSize) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2655 | FreePool(Replace);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2656 | return (EFI_BUFFER_TOO_SMALL);
|
| 2657 | }
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 2658 | StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2659 | SourceString++;
|
| 2660 | }
|
| 2661 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2662 | FreePool(Replace);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2663 | return (EFI_SUCCESS);
|
| 2664 | }
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2665 |
|
| 2666 | /**
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2667 | Internal worker function to output a string.
|
| 2668 |
|
| 2669 | This function will output a string to the correct StdOut.
|
| 2670 |
|
| 2671 | @param[in] String The string to print out.
|
| 2672 |
|
| 2673 | @retval EFI_SUCCESS The operation was sucessful.
|
| 2674 | @retval !EFI_SUCCESS The operation failed.
|
| 2675 | **/
|
| 2676 | EFI_STATUS
|
| 2677 | EFIAPI
|
| 2678 | InternalPrintTo (
|
| 2679 | IN CONST CHAR16 *String
|
| 2680 | )
|
| 2681 | {
|
| 2682 | UINTN Size;
|
| 2683 | Size = StrSize(String) - sizeof(CHAR16);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2684 | if (Size == 0) {
|
| 2685 | return (EFI_SUCCESS);
|
| 2686 | }
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 2687 | if (gEfiShellParametersProtocol != NULL) {
|
| 2688 | return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2689 | }
|
| 2690 | if (mEfiShellInterface != NULL) {
|
jcarsey | 06c355b | 2012-03-26 21:00:39 +0000 | [diff] [blame] | 2691 | if (mEfiShellInterface->RedirArgc == 0) {
|
jcarsey | 49bd498 | 2012-01-27 18:42:43 +0000 | [diff] [blame] | 2692 | //
|
| 2693 | // Divide in half for old shell. Must be string length not size.
|
jcarsey | 06c355b | 2012-03-26 21:00:39 +0000 | [diff] [blame] | 2694 | //
|
| 2695 | Size /=2; // Divide in half only when no redirection.
|
| 2696 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2697 | return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2698 | }
|
| 2699 | ASSERT(FALSE);
|
| 2700 | return (EFI_UNSUPPORTED);
|
| 2701 | }
|
| 2702 |
|
| 2703 | /**
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2704 | Print at a specific location on the screen.
|
| 2705 |
|
jcarsey | f1b87e7 | 2009-06-17 00:52:11 +0000 | [diff] [blame] | 2706 | This function will move the cursor to a given screen location and print the specified string
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2707 |
|
| 2708 | If -1 is specified for either the Row or Col the current screen location for BOTH
|
jcarsey | f1b87e7 | 2009-06-17 00:52:11 +0000 | [diff] [blame] | 2709 | will be used.
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2710 |
|
| 2711 | if either Row or Col is out of range for the current console, then ASSERT
|
| 2712 | if Format is NULL, then ASSERT
|
| 2713 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2714 | In addition to the standard %-based flags as supported by UefiLib Print() this supports
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2715 | the following additional flags:
|
| 2716 | %N - Set output attribute to normal
|
| 2717 | %H - Set output attribute to highlight
|
| 2718 | %E - Set output attribute to error
|
| 2719 | %B - Set output attribute to blue color
|
| 2720 | %V - Set output attribute to green color
|
| 2721 |
|
| 2722 | Note: The background color is controlled by the shell command cls.
|
| 2723 |
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2724 | @param[in] Col the column to print at
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2725 | @param[in] Row the row to print at
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2726 | @param[in] Format the format string
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2727 | @param[in] Marker the marker for the variable argument list
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2728 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2729 | @return EFI_SUCCESS The operation was successful.
|
| 2730 | @return EFI_DEVICE_ERROR The console device reported an error.
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2731 | **/
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2732 | EFI_STATUS
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2733 | EFIAPI
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2734 | InternalShellPrintWorker(
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2735 | IN INT32 Col OPTIONAL,
|
| 2736 | IN INT32 Row OPTIONAL,
|
| 2737 | IN CONST CHAR16 *Format,
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 2738 | IN VA_LIST Marker
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2739 | )
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2740 | {
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2741 | EFI_STATUS Status;
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2742 | CHAR16 *ResumeLocation;
|
| 2743 | CHAR16 *FormatWalker;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2744 | UINTN OriginalAttribute;
|
jcarsey | 89e8537 | 2011-04-13 23:37:50 +0000 | [diff] [blame] | 2745 | CHAR16 *mPostReplaceFormat;
|
| 2746 | CHAR16 *mPostReplaceFormat2;
|
| 2747 |
|
| 2748 | mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
|
| 2749 | mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2750 |
|
jcarsey | f8d3e68 | 2011-04-19 17:54:42 +0000 | [diff] [blame] | 2751 | if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
|
| 2752 | SHELL_FREE_NON_NULL(mPostReplaceFormat);
|
| 2753 | SHELL_FREE_NON_NULL(mPostReplaceFormat2);
|
| 2754 | return (EFI_OUT_OF_RESOURCES);
|
| 2755 | }
|
| 2756 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2757 | Status = EFI_SUCCESS;
|
| 2758 | OriginalAttribute = gST->ConOut->Mode->Attribute;
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2759 |
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2760 | //
|
| 2761 | // Back and forth each time fixing up 1 of our flags...
|
| 2762 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2763 | Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2764 | ASSERT_EFI_ERROR(Status);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2765 | Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2766 | ASSERT_EFI_ERROR(Status);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2767 | Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2768 | ASSERT_EFI_ERROR(Status);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2769 | Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2770 | ASSERT_EFI_ERROR(Status);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2771 | Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2772 | ASSERT_EFI_ERROR(Status);
|
| 2773 |
|
| 2774 | //
|
| 2775 | // Use the last buffer from replacing to print from...
|
| 2776 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2777 | UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2778 |
|
| 2779 | if (Col != -1 && Row != -1) {
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2780 | Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2781 | }
|
| 2782 |
|
jcarsey | ecd3d59 | 2009-12-07 18:05:00 +0000 | [diff] [blame] | 2783 | FormatWalker = mPostReplaceFormat2;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2784 | while (*FormatWalker != CHAR_NULL) {
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2785 | //
|
| 2786 | // Find the next attribute change request
|
| 2787 | //
|
| 2788 | ResumeLocation = StrStr(FormatWalker, L"%");
|
| 2789 | if (ResumeLocation != NULL) {
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2790 | *ResumeLocation = CHAR_NULL;
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2791 | }
|
| 2792 | //
|
| 2793 | // print the current FormatWalker string
|
| 2794 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2795 | if (StrLen(FormatWalker)>0) {
|
| 2796 | Status = InternalPrintTo(FormatWalker);
|
| 2797 | if (EFI_ERROR(Status)) {
|
| 2798 | break;
|
| 2799 | }
|
| 2800 | }
|
| 2801 |
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2802 | //
|
| 2803 | // update the attribute
|
| 2804 | //
|
| 2805 | if (ResumeLocation != NULL) {
|
jcarsey | 5d46f17 | 2011-08-23 15:34:23 +0000 | [diff] [blame] | 2806 | if (*(ResumeLocation-1) == L'^') {
|
| 2807 | //
|
jcarsey | 8bb7441 | 2012-01-30 18:44:41 +0000 | [diff] [blame] | 2808 | // Move cursor back 1 position to overwrite the ^
|
| 2809 | //
|
| 2810 | gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
|
| 2811 |
|
| 2812 | //
|
jcarsey | 5d46f17 | 2011-08-23 15:34:23 +0000 | [diff] [blame] | 2813 | // Print a simple '%' symbol
|
| 2814 | //
|
| 2815 | Status = InternalPrintTo(L"%");
|
| 2816 | ResumeLocation = ResumeLocation - 1;
|
| 2817 | } else {
|
| 2818 | switch (*(ResumeLocation+1)) {
|
| 2819 | case (L'N'):
|
| 2820 | gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2821 | break;
|
jcarsey | 5d46f17 | 2011-08-23 15:34:23 +0000 | [diff] [blame] | 2822 | case (L'E'):
|
| 2823 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
|
| 2824 | break;
|
| 2825 | case (L'H'):
|
| 2826 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
|
| 2827 | break;
|
| 2828 | case (L'B'):
|
| 2829 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
|
| 2830 | break;
|
| 2831 | case (L'V'):
|
| 2832 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
|
| 2833 | break;
|
| 2834 | default:
|
| 2835 | //
|
| 2836 | // Print a simple '%' symbol
|
| 2837 | //
|
| 2838 | Status = InternalPrintTo(L"%");
|
| 2839 | if (EFI_ERROR(Status)) {
|
| 2840 | break;
|
| 2841 | }
|
| 2842 | ResumeLocation = ResumeLocation - 1;
|
| 2843 | break;
|
| 2844 | }
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2845 | }
|
| 2846 | } else {
|
| 2847 | //
|
| 2848 | // reset to normal now...
|
| 2849 | //
|
jcarsey | 975136a | 2009-06-16 19:03:54 +0000 | [diff] [blame] | 2850 | break;
|
| 2851 | }
|
| 2852 |
|
| 2853 | //
|
| 2854 | // update FormatWalker to Resume + 2 (skip the % and the indicator)
|
| 2855 | //
|
| 2856 | FormatWalker = ResumeLocation + 2;
|
| 2857 | }
|
jcarsey | b1f95a0 | 2009-06-16 00:23:19 +0000 | [diff] [blame] | 2858 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2859 | gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
|
jcarsey | 89e8537 | 2011-04-13 23:37:50 +0000 | [diff] [blame] | 2860 |
|
| 2861 | SHELL_FREE_NON_NULL(mPostReplaceFormat);
|
| 2862 | SHELL_FREE_NON_NULL(mPostReplaceFormat2);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2863 | return (Status);
|
jcarsey | 5f7431d | 2009-07-10 18:06:01 +0000 | [diff] [blame] | 2864 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2865 |
|
| 2866 | /**
|
| 2867 | Print at a specific location on the screen.
|
| 2868 |
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2869 | This function will move the cursor to a given screen location and print the specified string.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2870 |
|
| 2871 | If -1 is specified for either the Row or Col the current screen location for BOTH
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2872 | will be used.
|
| 2873 |
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2874 | If either Row or Col is out of range for the current console, then ASSERT.
|
| 2875 | If Format is NULL, then ASSERT.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2876 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2877 | In addition to the standard %-based flags as supported by UefiLib Print() this supports
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2878 | the following additional flags:
|
| 2879 | %N - Set output attribute to normal
|
| 2880 | %H - Set output attribute to highlight
|
| 2881 | %E - Set output attribute to error
|
| 2882 | %B - Set output attribute to blue color
|
| 2883 | %V - Set output attribute to green color
|
| 2884 |
|
| 2885 | Note: The background color is controlled by the shell command cls.
|
| 2886 |
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2887 | @param[in] Col the column to print at
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2888 | @param[in] Row the row to print at
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2889 | @param[in] Format the format string
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2890 | @param[in] ... The variable argument list.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2891 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2892 | @return EFI_SUCCESS The printing was successful.
|
| 2893 | @return EFI_DEVICE_ERROR The console device reported an error.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2894 | **/
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2895 | EFI_STATUS
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2896 | EFIAPI
|
| 2897 | ShellPrintEx(
|
| 2898 | IN INT32 Col OPTIONAL,
|
| 2899 | IN INT32 Row OPTIONAL,
|
| 2900 | IN CONST CHAR16 *Format,
|
| 2901 | ...
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2902 | )
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2903 | {
|
| 2904 | VA_LIST Marker;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2905 | EFI_STATUS RetVal;
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 2906 | if (Format == NULL) {
|
| 2907 | return (EFI_INVALID_PARAMETER);
|
| 2908 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2909 | VA_START (Marker, Format);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2910 | RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2911 | VA_END(Marker);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2912 | return(RetVal);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2913 | }
|
| 2914 |
|
| 2915 | /**
|
| 2916 | Print at a specific location on the screen.
|
| 2917 |
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2918 | This function will move the cursor to a given screen location and print the specified string.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2919 |
|
| 2920 | If -1 is specified for either the Row or Col the current screen location for BOTH
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2921 | will be used.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2922 |
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2923 | If either Row or Col is out of range for the current console, then ASSERT.
|
| 2924 | If Format is NULL, then ASSERT.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2925 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2926 | In addition to the standard %-based flags as supported by UefiLib Print() this supports
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2927 | the following additional flags:
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2928 | %N - Set output attribute to normal.
|
| 2929 | %H - Set output attribute to highlight.
|
| 2930 | %E - Set output attribute to error.
|
| 2931 | %B - Set output attribute to blue color.
|
| 2932 | %V - Set output attribute to green color.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2933 |
|
| 2934 | Note: The background color is controlled by the shell command cls.
|
| 2935 |
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2936 | @param[in] Col The column to print at.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2937 | @param[in] Row The row to print at.
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2938 | @param[in] Language The language of the string to retrieve. If this parameter
|
| 2939 | is NULL, then the current platform language is used.
|
| 2940 | @param[in] HiiFormatStringId The format string Id for getting from Hii.
|
| 2941 | @param[in] HiiFormatHandle The format string Handle for getting from Hii.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2942 | @param[in] ... The variable argument list.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2943 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2944 | @return EFI_SUCCESS The printing was successful.
|
| 2945 | @return EFI_DEVICE_ERROR The console device reported an error.
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2946 | **/
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2947 | EFI_STATUS
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2948 | EFIAPI
|
| 2949 | ShellPrintHiiEx(
|
| 2950 | IN INT32 Col OPTIONAL,
|
| 2951 | IN INT32 Row OPTIONAL,
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2952 | IN CONST CHAR8 *Language OPTIONAL,
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2953 | IN CONST EFI_STRING_ID HiiFormatStringId,
|
| 2954 | IN CONST EFI_HANDLE HiiFormatHandle,
|
| 2955 | ...
|
| 2956 | )
|
| 2957 | {
|
| 2958 | VA_LIST Marker;
|
| 2959 | CHAR16 *HiiFormatString;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2960 | EFI_STATUS RetVal;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2961 |
|
| 2962 | VA_START (Marker, HiiFormatHandle);
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 2963 | HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2964 | ASSERT(HiiFormatString != NULL);
|
| 2965 |
|
| 2966 | RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
|
| 2967 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2968 | SHELL_FREE_NON_NULL(HiiFormatString);
|
jcarsey | e2f8297 | 2009-12-01 05:40:24 +0000 | [diff] [blame] | 2969 | VA_END(Marker);
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2970 |
|
| 2971 | return (RetVal);
|
| 2972 | }
|
| 2973 |
|
| 2974 | /**
|
| 2975 | Function to determine if a given filename represents a file or a directory.
|
| 2976 |
|
| 2977 | @param[in] DirName Path to directory to test.
|
| 2978 |
|
jcarsey | c8c2259 | 2011-10-17 17:49:21 +0000 | [diff] [blame] | 2979 | @retval EFI_SUCCESS The Path represents a directory
|
| 2980 | @retval EFI_NOT_FOUND The Path does not represent a directory
|
| 2981 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
|
| 2982 | @return The path failed to open
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2983 | **/
|
| 2984 | EFI_STATUS
|
| 2985 | EFIAPI
|
| 2986 | ShellIsDirectory(
|
| 2987 | IN CONST CHAR16 *DirName
|
| 2988 | )
|
| 2989 | {
|
| 2990 | EFI_STATUS Status;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2991 | SHELL_FILE_HANDLE Handle;
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 2992 | CHAR16 *TempLocation;
|
| 2993 | CHAR16 *TempLocation2;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2994 |
|
jcarsey | ecd3d59 | 2009-12-07 18:05:00 +0000 | [diff] [blame] | 2995 | ASSERT(DirName != NULL);
|
| 2996 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 2997 | Handle = NULL;
|
| 2998 | TempLocation = NULL;
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 2999 |
|
| 3000 | Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
|
| 3001 | if (EFI_ERROR(Status)) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3002 | //
|
| 3003 | // try good logic first.
|
| 3004 | //
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 3005 | if (gEfiShellProtocol != NULL) {
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 3006 | TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
|
jcarsey | c8c2259 | 2011-10-17 17:49:21 +0000 | [diff] [blame] | 3007 | if (TempLocation == NULL) {
|
| 3008 | ShellCloseFile(&Handle);
|
| 3009 | return (EFI_OUT_OF_RESOURCES);
|
| 3010 | }
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 3011 | TempLocation2 = StrStr(TempLocation, L":");
|
| 3012 | if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
|
| 3013 | *(TempLocation2+1) = CHAR_NULL;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3014 | }
|
jcarsey | 366f81a | 2011-06-27 21:04:22 +0000 | [diff] [blame] | 3015 | if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3016 | FreePool(TempLocation);
|
| 3017 | return (EFI_SUCCESS);
|
| 3018 | }
|
| 3019 | FreePool(TempLocation);
|
| 3020 | } else {
|
| 3021 | //
|
| 3022 | // probably a map name?!?!!?
|
| 3023 | //
|
| 3024 | TempLocation = StrStr(DirName, L"\\");
|
| 3025 | if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
|
| 3026 | return (EFI_SUCCESS);
|
| 3027 | }
|
| 3028 | }
|
jcarsey | 2247dde | 2009-11-09 18:08:58 +0000 | [diff] [blame] | 3029 | return (Status);
|
| 3030 | }
|
| 3031 |
|
| 3032 | if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
|
| 3033 | ShellCloseFile(&Handle);
|
| 3034 | return (EFI_SUCCESS);
|
| 3035 | }
|
| 3036 | ShellCloseFile(&Handle);
|
| 3037 | return (EFI_NOT_FOUND);
|
| 3038 | }
|
| 3039 |
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3040 | /**
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 3041 | Function to determine if a given filename represents a file.
|
| 3042 |
|
| 3043 | @param[in] Name Path to file to test.
|
| 3044 |
|
| 3045 | @retval EFI_SUCCESS The Path represents a file.
|
| 3046 | @retval EFI_NOT_FOUND The Path does not represent a file.
|
| 3047 | @retval other The path failed to open.
|
| 3048 | **/
|
| 3049 | EFI_STATUS
|
| 3050 | EFIAPI
|
| 3051 | ShellIsFile(
|
| 3052 | IN CONST CHAR16 *Name
|
| 3053 | )
|
| 3054 | {
|
| 3055 | EFI_STATUS Status;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3056 | SHELL_FILE_HANDLE Handle;
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 3057 |
|
jcarsey | ecd3d59 | 2009-12-07 18:05:00 +0000 | [diff] [blame] | 3058 | ASSERT(Name != NULL);
|
| 3059 |
|
jcarsey | 36a9d67 | 2009-11-20 21:13:41 +0000 | [diff] [blame] | 3060 | Handle = NULL;
|
| 3061 |
|
| 3062 | Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
|
| 3063 | if (EFI_ERROR(Status)) {
|
| 3064 | return (Status);
|
| 3065 | }
|
| 3066 |
|
| 3067 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
| 3068 | ShellCloseFile(&Handle);
|
| 3069 | return (EFI_SUCCESS);
|
| 3070 | }
|
| 3071 | ShellCloseFile(&Handle);
|
| 3072 | return (EFI_NOT_FOUND);
|
| 3073 | }
|
| 3074 |
|
| 3075 | /**
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 3076 | Function to determine if a given filename represents a file.
|
| 3077 |
|
| 3078 | This will search the CWD and then the Path.
|
| 3079 |
|
| 3080 | If Name is NULL, then ASSERT.
|
| 3081 |
|
| 3082 | @param[in] Name Path to file to test.
|
| 3083 |
|
| 3084 | @retval EFI_SUCCESS The Path represents a file.
|
| 3085 | @retval EFI_NOT_FOUND The Path does not represent a file.
|
| 3086 | @retval other The path failed to open.
|
| 3087 | **/
|
| 3088 | EFI_STATUS
|
| 3089 | EFIAPI
|
| 3090 | ShellIsFileInPath(
|
| 3091 | IN CONST CHAR16 *Name
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3092 | )
|
| 3093 | {
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 3094 | CHAR16 *NewName;
|
| 3095 | EFI_STATUS Status;
|
| 3096 |
|
| 3097 | if (!EFI_ERROR(ShellIsFile(Name))) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3098 | return (EFI_SUCCESS);
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 3099 | }
|
| 3100 |
|
| 3101 | NewName = ShellFindFilePath(Name);
|
| 3102 | if (NewName == NULL) {
|
| 3103 | return (EFI_NOT_FOUND);
|
| 3104 | }
|
| 3105 | Status = ShellIsFile(NewName);
|
| 3106 | FreePool(NewName);
|
| 3107 | return (Status);
|
| 3108 | }
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3109 |
|
jcarsey | b3011f4 | 2010-01-11 21:49:04 +0000 | [diff] [blame] | 3110 | /**
|
Jaben Carsey | 74b0fb8 | 2013-11-22 21:37:34 +0000 | [diff] [blame] | 3111 | Function return the number converted from a hex representation of a number.
|
| 3112 |
|
| 3113 | Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
|
| 3114 | result. Use ShellConvertStringToUint64 instead.
|
| 3115 |
|
| 3116 | @param[in] String String representation of a number.
|
| 3117 |
|
| 3118 | @return The unsigned integer result of the conversion.
|
| 3119 | @retval (UINTN)(-1) An error occured.
|
| 3120 | **/
|
| 3121 | UINTN
|
| 3122 | EFIAPI
|
| 3123 | ShellHexStrToUintn(
|
| 3124 | IN CONST CHAR16 *String
|
| 3125 | )
|
| 3126 | {
|
| 3127 | UINT64 RetVal;
|
| 3128 |
|
| 3129 | if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
|
| 3130 | return ((UINTN)RetVal);
|
| 3131 | }
|
| 3132 |
|
| 3133 | return ((UINTN)(-1));
|
| 3134 | }
|
| 3135 |
|
| 3136 | /**
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 3137 | Function to determine whether a string is decimal or hex representation of a number
|
Jaben Carsey | d59fc24 | 2013-11-14 23:52:43 +0000 | [diff] [blame] | 3138 | and return the number converted from the string. Spaces are always skipped.
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3139 |
|
| 3140 | @param[in] String String representation of a number
|
| 3141 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3142 | @return the number
|
| 3143 | @retval (UINTN)(-1) An error ocurred.
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3144 | **/
|
| 3145 | UINTN
|
| 3146 | EFIAPI
|
| 3147 | ShellStrToUintn(
|
| 3148 | IN CONST CHAR16 *String
|
| 3149 | )
|
| 3150 | {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3151 | UINT64 RetVal;
|
| 3152 | BOOLEAN Hex;
|
| 3153 |
|
| 3154 | Hex = FALSE;
|
| 3155 |
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3156 | if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3157 | Hex = TRUE;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3158 | }
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3159 |
|
| 3160 | if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
|
| 3161 | return ((UINTN)RetVal);
|
| 3162 | }
|
| 3163 | return ((UINTN)(-1));
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3164 | }
|
| 3165 |
|
| 3166 | /**
|
| 3167 | Safely append with automatic string resizing given length of Destination and
|
| 3168 | desired length of copy from Source.
|
| 3169 |
|
| 3170 | append the first D characters of Source to the end of Destination, where D is
|
| 3171 | the lesser of Count and the StrLen() of Source. If appending those D characters
|
| 3172 | will fit within Destination (whose Size is given as CurrentSize) and
|
jcarsey | 1e6e84c | 2010-01-25 20:05:08 +0000 | [diff] [blame] | 3173 | still leave room for a NULL terminator, then those characters are appended,
|
| 3174 | starting at the original terminating NULL of Destination, and a new terminating
|
| 3175 | NULL is appended.
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3176 |
|
| 3177 | If appending D characters onto Destination will result in a overflow of the size
|
| 3178 | given in CurrentSize the string will be grown such that the copy can be performed
|
| 3179 | and CurrentSize will be updated to the new size.
|
| 3180 |
|
| 3181 | If Source is NULL, there is nothing to append, just return the current buffer in
|
| 3182 | Destination.
|
| 3183 |
|
| 3184 | if Destination is NULL, then ASSERT()
|
| 3185 | if Destination's current length (including NULL terminator) is already more then
|
| 3186 | CurrentSize, then ASSERT()
|
| 3187 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 3188 | @param[in, out] Destination The String to append onto
|
| 3189 | @param[in, out] CurrentSize on call the number of bytes in Destination. On
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3190 | return possibly the new size (still in bytes). if NULL
|
| 3191 | then allocate whatever is needed.
|
| 3192 | @param[in] Source The String to append from
|
| 3193 | @param[in] Count Maximum number of characters to append. if 0 then
|
| 3194 | all are appended.
|
| 3195 |
|
| 3196 | @return Destination return the resultant string.
|
| 3197 | **/
|
| 3198 | CHAR16*
|
| 3199 | EFIAPI
|
| 3200 | StrnCatGrow (
|
| 3201 | IN OUT CHAR16 **Destination,
|
| 3202 | IN OUT UINTN *CurrentSize,
|
| 3203 | IN CONST CHAR16 *Source,
|
| 3204 | IN UINTN Count
|
| 3205 | )
|
| 3206 | {
|
| 3207 | UINTN DestinationStartSize;
|
| 3208 | UINTN NewSize;
|
| 3209 |
|
| 3210 | //
|
| 3211 | // ASSERTs
|
| 3212 | //
|
| 3213 | ASSERT(Destination != NULL);
|
| 3214 |
|
| 3215 | //
|
| 3216 | // If there's nothing to do then just return Destination
|
| 3217 | //
|
| 3218 | if (Source == NULL) {
|
| 3219 | return (*Destination);
|
| 3220 | }
|
| 3221 |
|
| 3222 | //
|
| 3223 | // allow for un-initialized pointers, based on size being 0
|
| 3224 | //
|
| 3225 | if (CurrentSize != NULL && *CurrentSize == 0) {
|
| 3226 | *Destination = NULL;
|
| 3227 | }
|
| 3228 |
|
| 3229 | //
|
| 3230 | // allow for NULL pointers address as Destination
|
| 3231 | //
|
| 3232 | if (*Destination != NULL) {
|
| 3233 | ASSERT(CurrentSize != 0);
|
| 3234 | DestinationStartSize = StrSize(*Destination);
|
| 3235 | ASSERT(DestinationStartSize <= *CurrentSize);
|
| 3236 | } else {
|
| 3237 | DestinationStartSize = 0;
|
| 3238 | // ASSERT(*CurrentSize == 0);
|
| 3239 | }
|
| 3240 |
|
| 3241 | //
|
| 3242 | // Append all of Source?
|
| 3243 | //
|
| 3244 | if (Count == 0) {
|
| 3245 | Count = StrLen(Source);
|
| 3246 | }
|
| 3247 |
|
| 3248 | //
|
| 3249 | // Test and grow if required
|
| 3250 | //
|
| 3251 | if (CurrentSize != NULL) {
|
| 3252 | NewSize = *CurrentSize;
|
ydong10 | f480fdc | 2012-09-07 01:55:33 +0000 | [diff] [blame] | 3253 | if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
|
| 3254 | while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
|
| 3255 | NewSize += 2 * Count * sizeof(CHAR16);
|
| 3256 | }
|
| 3257 | *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
|
| 3258 | *CurrentSize = NewSize;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3259 | }
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3260 | } else {
|
Qiu Shumin | c587fd3 | 2015-07-02 01:12:03 +0000 | [diff] [blame] | 3261 | NewSize = (Count+1)*sizeof(CHAR16);
|
| 3262 | *Destination = AllocateZeroPool(NewSize);
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3263 | }
|
| 3264 |
|
| 3265 | //
|
| 3266 | // Now use standard StrnCat on a big enough buffer
|
| 3267 | //
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3268 | if (*Destination == NULL) {
|
| 3269 | return (NULL);
|
| 3270 | }
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 3271 |
|
Qiu Shumin | c587fd3 | 2015-07-02 01:12:03 +0000 | [diff] [blame] | 3272 | StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
|
Qiu Shumin | e75390f | 2015-06-30 03:18:31 +0000 | [diff] [blame] | 3273 | return *Destination;
|
jcarsey | 125c2cf | 2009-11-18 21:36:50 +0000 | [diff] [blame] | 3274 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3275 |
|
| 3276 | /**
|
| 3277 | Prompt the user and return the resultant answer to the requestor.
|
| 3278 |
|
| 3279 | This function will display the requested question on the shell prompt and then
|
| 3280 | wait for an apropriate answer to be input from the console.
|
| 3281 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3282 | if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3283 | or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
|
| 3284 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3285 | if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3286 | CHAR16*.
|
| 3287 |
|
| 3288 | In either case *Response must be callee freed if Response was not NULL;
|
| 3289 |
|
| 3290 | @param Type What type of question is asked. This is used to filter the input
|
| 3291 | to prevent invalid answers to question.
|
| 3292 | @param Prompt Pointer to string prompt to use to request input.
|
| 3293 | @param Response Pointer to Response which will be populated upon return.
|
| 3294 |
|
| 3295 | @retval EFI_SUCCESS The operation was sucessful.
|
| 3296 | @retval EFI_UNSUPPORTED The operation is not supported as requested.
|
| 3297 | @retval EFI_INVALID_PARAMETER A parameter was invalid.
|
| 3298 | @return other The operation failed.
|
| 3299 | **/
|
| 3300 | EFI_STATUS
|
| 3301 | EFIAPI
|
| 3302 | ShellPromptForResponse (
|
| 3303 | IN SHELL_PROMPT_REQUEST_TYPE Type,
|
| 3304 | IN CHAR16 *Prompt OPTIONAL,
|
| 3305 | IN OUT VOID **Response OPTIONAL
|
| 3306 | )
|
| 3307 | {
|
| 3308 | EFI_STATUS Status;
|
| 3309 | EFI_INPUT_KEY Key;
|
| 3310 | UINTN EventIndex;
|
| 3311 | SHELL_PROMPT_RESPONSE *Resp;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3312 | UINTN Size;
|
| 3313 | CHAR16 *Buffer;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3314 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3315 | Status = EFI_UNSUPPORTED;
|
| 3316 | Resp = NULL;
|
| 3317 | Buffer = NULL;
|
| 3318 | Size = 0;
|
| 3319 | if (Type != ShellPromptResponseTypeFreeform) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3320 | Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
|
jcarsey | 3e082d5 | 2010-10-04 16:44:57 +0000 | [diff] [blame] | 3321 | if (Resp == NULL) {
|
| 3322 | return (EFI_OUT_OF_RESOURCES);
|
| 3323 | }
|
xdu2 | 90bfa22 | 2010-07-19 05:21:27 +0000 | [diff] [blame] | 3324 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3325 |
|
| 3326 | switch(Type) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3327 | case ShellPromptResponseTypeQuitContinue:
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3328 | if (Prompt != NULL) {
|
| 3329 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3330 | }
|
| 3331 | //
|
| 3332 | // wait for valid response
|
| 3333 | //
|
| 3334 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
| 3335 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3336 | if (EFI_ERROR(Status)) {
|
| 3337 | break;
|
| 3338 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3339 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3340 | if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3341 | *Resp = ShellPromptResponseQuit;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3342 | } else {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3343 | *Resp = ShellPromptResponseContinue;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3344 | }
|
| 3345 | break;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3346 | case ShellPromptResponseTypeYesNoCancel:
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3347 | if (Prompt != NULL) {
|
| 3348 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3349 | }
|
| 3350 | //
|
| 3351 | // wait for valid response
|
| 3352 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3353 | *Resp = ShellPromptResponseMax;
|
| 3354 | while (*Resp == ShellPromptResponseMax) {
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3355 | if (ShellGetExecutionBreakFlag()) {
|
| 3356 | Status = EFI_ABORTED;
|
| 3357 | break;
|
| 3358 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3359 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
| 3360 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3361 | if (EFI_ERROR(Status)) {
|
| 3362 | break;
|
| 3363 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3364 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3365 | switch (Key.UnicodeChar) {
|
| 3366 | case L'Y':
|
| 3367 | case L'y':
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3368 | *Resp = ShellPromptResponseYes;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3369 | break;
|
| 3370 | case L'N':
|
| 3371 | case L'n':
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3372 | *Resp = ShellPromptResponseNo;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3373 | break;
|
| 3374 | case L'C':
|
| 3375 | case L'c':
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3376 | *Resp = ShellPromptResponseCancel;
|
| 3377 | break;
|
| 3378 | }
|
| 3379 | }
|
| 3380 | break; case ShellPromptResponseTypeYesNoAllCancel:
|
| 3381 | if (Prompt != NULL) {
|
| 3382 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3383 | }
|
| 3384 | //
|
| 3385 | // wait for valid response
|
| 3386 | //
|
| 3387 | *Resp = ShellPromptResponseMax;
|
| 3388 | while (*Resp == ShellPromptResponseMax) {
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3389 | if (ShellGetExecutionBreakFlag()) {
|
| 3390 | Status = EFI_ABORTED;
|
| 3391 | break;
|
| 3392 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3393 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
| 3394 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3395 | if (EFI_ERROR(Status)) {
|
| 3396 | break;
|
| 3397 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3398 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3399 | switch (Key.UnicodeChar) {
|
| 3400 | case L'Y':
|
| 3401 | case L'y':
|
| 3402 | *Resp = ShellPromptResponseYes;
|
| 3403 | break;
|
| 3404 | case L'N':
|
| 3405 | case L'n':
|
| 3406 | *Resp = ShellPromptResponseNo;
|
| 3407 | break;
|
| 3408 | case L'A':
|
| 3409 | case L'a':
|
| 3410 | *Resp = ShellPromptResponseAll;
|
| 3411 | break;
|
| 3412 | case L'C':
|
| 3413 | case L'c':
|
| 3414 | *Resp = ShellPromptResponseCancel;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3415 | break;
|
| 3416 | }
|
| 3417 | }
|
| 3418 | break;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3419 | case ShellPromptResponseTypeEnterContinue:
|
| 3420 | case ShellPromptResponseTypeAnyKeyContinue:
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3421 | if (Prompt != NULL) {
|
| 3422 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3423 | }
|
| 3424 | //
|
| 3425 | // wait for valid response
|
| 3426 | //
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3427 | *Resp = ShellPromptResponseMax;
|
| 3428 | while (*Resp == ShellPromptResponseMax) {
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3429 | if (ShellGetExecutionBreakFlag()) {
|
| 3430 | Status = EFI_ABORTED;
|
| 3431 | break;
|
| 3432 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3433 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3434 | if (Type == ShellPromptResponseTypeEnterContinue) {
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3435 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3436 | if (EFI_ERROR(Status)) {
|
| 3437 | break;
|
| 3438 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3439 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3440 | if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3441 | *Resp = ShellPromptResponseContinue;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3442 | break;
|
| 3443 | }
|
| 3444 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3445 | if (Type == ShellPromptResponseTypeAnyKeyContinue) {
|
| 3446 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
| 3447 | ASSERT_EFI_ERROR(Status);
|
| 3448 | *Resp = ShellPromptResponseContinue;
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3449 | break;
|
| 3450 | }
|
| 3451 | }
|
| 3452 | break;
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3453 | case ShellPromptResponseTypeYesNo:
|
| 3454 | if (Prompt != NULL) {
|
| 3455 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3456 | }
|
| 3457 | //
|
| 3458 | // wait for valid response
|
| 3459 | //
|
| 3460 | *Resp = ShellPromptResponseMax;
|
| 3461 | while (*Resp == ShellPromptResponseMax) {
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3462 | if (ShellGetExecutionBreakFlag()) {
|
| 3463 | Status = EFI_ABORTED;
|
| 3464 | break;
|
| 3465 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3466 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
| 3467 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3468 | if (EFI_ERROR(Status)) {
|
| 3469 | break;
|
| 3470 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3471 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3472 | switch (Key.UnicodeChar) {
|
| 3473 | case L'Y':
|
| 3474 | case L'y':
|
| 3475 | *Resp = ShellPromptResponseYes;
|
| 3476 | break;
|
| 3477 | case L'N':
|
| 3478 | case L'n':
|
| 3479 | *Resp = ShellPromptResponseNo;
|
| 3480 | break;
|
| 3481 | }
|
| 3482 | }
|
| 3483 | break;
|
| 3484 | case ShellPromptResponseTypeFreeform:
|
| 3485 | if (Prompt != NULL) {
|
| 3486 | ShellPrintEx(-1, -1, L"%s", Prompt);
|
| 3487 | }
|
| 3488 | while(1) {
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3489 | if (ShellGetExecutionBreakFlag()) {
|
| 3490 | Status = EFI_ABORTED;
|
| 3491 | break;
|
| 3492 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3493 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
|
| 3494 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
|
Jaben Carsey | 31b018a | 2014-01-16 16:52:39 +0000 | [diff] [blame] | 3495 | if (EFI_ERROR(Status)) {
|
| 3496 | break;
|
| 3497 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3498 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
|
| 3499 | if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
|
| 3500 | break;
|
| 3501 | }
|
| 3502 | ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
|
| 3503 | StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
|
| 3504 | }
|
| 3505 | break;
|
| 3506 | //
|
| 3507 | // This is the location to add new prompt types.
|
Jaben Carsey | 194ae48 | 2013-12-09 22:55:13 +0000 | [diff] [blame] | 3508 | // If your new type loops remember to add ExecutionBreak support.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3509 | //
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3510 | default:
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3511 | ASSERT(FALSE);
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3512 | }
|
| 3513 |
|
| 3514 | if (Response != NULL) {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3515 | if (Resp != NULL) {
|
| 3516 | *Response = Resp;
|
| 3517 | } else if (Buffer != NULL) {
|
| 3518 | *Response = Buffer;
|
| 3519 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3520 | } else {
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3521 | if (Resp != NULL) {
|
| 3522 | FreePool(Resp);
|
| 3523 | }
|
| 3524 | if (Buffer != NULL) {
|
| 3525 | FreePool(Buffer);
|
| 3526 | }
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3527 | }
|
| 3528 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3529 | ShellPrintEx(-1, -1, L"\r\n");
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3530 | return (Status);
|
| 3531 | }
|
| 3532 |
|
| 3533 | /**
|
| 3534 | Prompt the user and return the resultant answer to the requestor.
|
| 3535 |
|
| 3536 | This function is the same as ShellPromptForResponse, except that the prompt is
|
| 3537 | automatically pulled from HII.
|
| 3538 |
|
| 3539 | @param Type What type of question is asked. This is used to filter the input
|
| 3540 | to prevent invalid answers to question.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3541 | @param[in] HiiFormatStringId The format string Id for getting from Hii.
|
| 3542 | @param[in] HiiFormatHandle The format string Handle for getting from Hii.
|
| 3543 | @param Response Pointer to Response which will be populated upon return.
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3544 |
|
| 3545 | @retval EFI_SUCCESS the operation was sucessful.
|
| 3546 | @return other the operation failed.
|
| 3547 |
|
| 3548 | @sa ShellPromptForResponse
|
| 3549 | **/
|
| 3550 | EFI_STATUS
|
| 3551 | EFIAPI
|
| 3552 | ShellPromptForResponseHii (
|
| 3553 | IN SHELL_PROMPT_REQUEST_TYPE Type,
|
| 3554 | IN CONST EFI_STRING_ID HiiFormatStringId,
|
| 3555 | IN CONST EFI_HANDLE HiiFormatHandle,
|
| 3556 | IN OUT VOID **Response
|
| 3557 | )
|
| 3558 | {
|
| 3559 | CHAR16 *Prompt;
|
| 3560 | EFI_STATUS Status;
|
| 3561 |
|
| 3562 | Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
|
| 3563 | Status = ShellPromptForResponse(Type, Prompt, Response);
|
| 3564 | FreePool(Prompt);
|
| 3565 | return (Status);
|
| 3566 | }
|
| 3567 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3568 | /**
|
| 3569 | Function to determin if an entire string is a valid number.
|
jcarsey | c9d92df | 2010-02-03 15:37:54 +0000 | [diff] [blame] | 3570 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3571 | If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
|
| 3572 |
|
| 3573 | @param[in] String The string to evaluate.
|
| 3574 | @param[in] ForceHex TRUE - always assume hex.
|
| 3575 | @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3576 | @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3577 |
|
| 3578 | @retval TRUE It is all numeric (dec/hex) characters.
|
| 3579 | @retval FALSE There is a non-numeric character.
|
| 3580 | **/
|
| 3581 | BOOLEAN
|
| 3582 | EFIAPI
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3583 | InternalShellIsHexOrDecimalNumber (
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3584 | IN CONST CHAR16 *String,
|
| 3585 | IN CONST BOOLEAN ForceHex,
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3586 | IN CONST BOOLEAN StopAtSpace,
|
| 3587 | IN CONST BOOLEAN TimeNumbers
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3588 | )
|
| 3589 | {
|
| 3590 | BOOLEAN Hex;
|
| 3591 |
|
| 3592 | //
|
| 3593 | // chop off a single negative sign
|
| 3594 | //
|
| 3595 | if (String != NULL && *String == L'-') {
|
| 3596 | String++;
|
| 3597 | }
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3598 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3599 | if (String == NULL) {
|
| 3600 | return (FALSE);
|
| 3601 | }
|
| 3602 |
|
| 3603 | //
|
| 3604 | // chop leading zeroes
|
| 3605 | //
|
| 3606 | while(String != NULL && *String == L'0'){
|
| 3607 | String++;
|
| 3608 | }
|
| 3609 | //
|
| 3610 | // allow '0x' or '0X', but not 'x' or 'X'
|
| 3611 | //
|
| 3612 | if (String != NULL && (*String == L'x' || *String == L'X')) {
|
| 3613 | if (*(String-1) != L'0') {
|
| 3614 | //
|
| 3615 | // we got an x without a preceeding 0
|
| 3616 | //
|
| 3617 | return (FALSE);
|
| 3618 | }
|
| 3619 | String++;
|
| 3620 | Hex = TRUE;
|
| 3621 | } else if (ForceHex) {
|
| 3622 | Hex = TRUE;
|
| 3623 | } else {
|
| 3624 | Hex = FALSE;
|
| 3625 | }
|
| 3626 |
|
| 3627 | //
|
| 3628 | // loop through the remaining characters and use the lib function
|
| 3629 | //
|
| 3630 | for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3631 | if (TimeNumbers && (String[0] == L':')) {
|
| 3632 | continue;
|
| 3633 | }
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3634 | if (Hex) {
|
| 3635 | if (!ShellIsHexaDecimalDigitCharacter(*String)) {
|
| 3636 | return (FALSE);
|
| 3637 | }
|
| 3638 | } else {
|
| 3639 | if (!ShellIsDecimalDigitCharacter(*String)) {
|
| 3640 | return (FALSE);
|
| 3641 | }
|
| 3642 | }
|
| 3643 | }
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3644 |
|
jcarsey | a405b86 | 2010-09-14 05:18:09 +0000 | [diff] [blame] | 3645 | return (TRUE);
|
| 3646 | }
|
| 3647 |
|
| 3648 | /**
|
| 3649 | Function to determine if a given filename exists.
|
| 3650 |
|
| 3651 | @param[in] Name Path to test.
|
| 3652 |
|
| 3653 | @retval EFI_SUCCESS The Path represents a file.
|
| 3654 | @retval EFI_NOT_FOUND The Path does not represent a file.
|
| 3655 | @retval other The path failed to open.
|
| 3656 | **/
|
| 3657 | EFI_STATUS
|
| 3658 | EFIAPI
|
| 3659 | ShellFileExists(
|
| 3660 | IN CONST CHAR16 *Name
|
| 3661 | )
|
| 3662 | {
|
| 3663 | EFI_STATUS Status;
|
| 3664 | EFI_SHELL_FILE_INFO *List;
|
| 3665 |
|
| 3666 | ASSERT(Name != NULL);
|
| 3667 |
|
| 3668 | List = NULL;
|
| 3669 | Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
|
| 3670 | if (EFI_ERROR(Status)) {
|
| 3671 | return (Status);
|
| 3672 | }
|
| 3673 |
|
| 3674 | ShellCloseFileMetaArg(&List);
|
| 3675 |
|
| 3676 | return (EFI_SUCCESS);
|
| 3677 | }
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3678 |
|
| 3679 | /**
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3680 | Convert a Unicode character to upper case only if
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3681 | it maps to a valid small-case ASCII character.
|
| 3682 |
|
| 3683 | This internal function only deal with Unicode character
|
| 3684 | which maps to a valid small-case ASCII character, i.e.
|
| 3685 | L'a' to L'z'. For other Unicode character, the input character
|
| 3686 | is returned directly.
|
| 3687 |
|
| 3688 | @param Char The character to convert.
|
| 3689 |
|
| 3690 | @retval LowerCharacter If the Char is with range L'a' to L'z'.
|
| 3691 | @retval Unchanged Otherwise.
|
| 3692 |
|
| 3693 | **/
|
| 3694 | CHAR16
|
| 3695 | EFIAPI
|
| 3696 | InternalShellCharToUpper (
|
| 3697 | IN CHAR16 Char
|
| 3698 | )
|
| 3699 | {
|
| 3700 | if (Char >= L'a' && Char <= L'z') {
|
| 3701 | return (CHAR16) (Char - (L'a' - L'A'));
|
| 3702 | }
|
| 3703 |
|
| 3704 | return Char;
|
| 3705 | }
|
| 3706 |
|
| 3707 | /**
|
| 3708 | Convert a Unicode character to numerical value.
|
| 3709 |
|
| 3710 | This internal function only deal with Unicode character
|
| 3711 | which maps to a valid hexadecimal ASII character, i.e.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3712 | L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3713 | Unicode character, the value returned does not make sense.
|
| 3714 |
|
| 3715 | @param Char The character to convert.
|
| 3716 |
|
| 3717 | @return The numerical value converted.
|
| 3718 |
|
| 3719 | **/
|
| 3720 | UINTN
|
| 3721 | EFIAPI
|
| 3722 | InternalShellHexCharToUintn (
|
| 3723 | IN CHAR16 Char
|
| 3724 | )
|
| 3725 | {
|
| 3726 | if (ShellIsDecimalDigitCharacter (Char)) {
|
| 3727 | return Char - L'0';
|
| 3728 | }
|
| 3729 |
|
| 3730 | return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
|
| 3731 | }
|
| 3732 |
|
| 3733 | /**
|
| 3734 | Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
|
| 3735 |
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3736 | This function returns a value of type UINT64 by interpreting the contents
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3737 | of the Unicode string specified by String as a hexadecimal number.
|
| 3738 | The format of the input Unicode string String is:
|
| 3739 |
|
| 3740 | [spaces][zeros][x][hexadecimal digits].
|
| 3741 |
|
| 3742 | The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
|
| 3743 | The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
|
| 3744 | If "x" appears in the input string, it must be prefixed with at least one 0.
|
| 3745 | The function will ignore the pad space, which includes spaces or tab characters,
|
| 3746 | before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
|
| 3747 | [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
|
| 3748 | first valid hexadecimal digit. Then, the function stops at the first character that is
|
| 3749 | a not a valid hexadecimal character or NULL, whichever one comes first.
|
| 3750 |
|
| 3751 | If String has only pad spaces, then zero is returned.
|
| 3752 | If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
|
| 3753 | then zero is returned.
|
| 3754 |
|
| 3755 | @param[in] String A pointer to a Null-terminated Unicode string.
|
| 3756 | @param[out] Value Upon a successful return the value of the conversion.
|
| 3757 | @param[in] StopAtSpace FALSE to skip spaces.
|
| 3758 |
|
| 3759 | @retval EFI_SUCCESS The conversion was successful.
|
| 3760 | @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
|
| 3761 | @retval EFI_DEVICE_ERROR An overflow occured.
|
| 3762 | **/
|
| 3763 | EFI_STATUS
|
| 3764 | EFIAPI
|
| 3765 | InternalShellStrHexToUint64 (
|
| 3766 | IN CONST CHAR16 *String,
|
| 3767 | OUT UINT64 *Value,
|
| 3768 | IN CONST BOOLEAN StopAtSpace
|
| 3769 | )
|
| 3770 | {
|
| 3771 | UINT64 Result;
|
| 3772 |
|
| 3773 | if (String == NULL || StrSize(String) == 0 || Value == NULL) {
|
| 3774 | return (EFI_INVALID_PARAMETER);
|
| 3775 | }
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3776 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3777 | //
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3778 | // Ignore the pad spaces (space or tab)
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3779 | //
|
| 3780 | while ((*String == L' ') || (*String == L'\t')) {
|
| 3781 | String++;
|
| 3782 | }
|
| 3783 |
|
| 3784 | //
|
| 3785 | // Ignore leading Zeros after the spaces
|
| 3786 | //
|
| 3787 | while (*String == L'0') {
|
| 3788 | String++;
|
| 3789 | }
|
| 3790 |
|
| 3791 | if (InternalShellCharToUpper (*String) == L'X') {
|
| 3792 | if (*(String - 1) != L'0') {
|
| 3793 | return 0;
|
| 3794 | }
|
| 3795 | //
|
| 3796 | // Skip the 'X'
|
| 3797 | //
|
| 3798 | String++;
|
| 3799 | }
|
| 3800 |
|
| 3801 | Result = 0;
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3802 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3803 | //
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3804 | // there is a space where there should't be
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3805 | //
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3806 | if (*String == L' ') {
|
| 3807 | return (EFI_INVALID_PARAMETER);
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3808 | }
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3809 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3810 | while (ShellIsHexaDecimalDigitCharacter (*String)) {
|
| 3811 | //
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3812 | // If the Hex Number represented by String overflows according
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3813 | // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3814 | //
|
| 3815 | if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
|
| 3816 | // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
|
| 3817 | return (EFI_DEVICE_ERROR);
|
| 3818 | }
|
| 3819 |
|
| 3820 | Result = (LShiftU64(Result, 4));
|
| 3821 | Result += InternalShellHexCharToUintn (*String);
|
| 3822 | String++;
|
| 3823 |
|
| 3824 | //
|
jcarsey | 49bd498 | 2012-01-27 18:42:43 +0000 | [diff] [blame] | 3825 | // stop at spaces if requested
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3826 | //
|
jcarsey | 49bd498 | 2012-01-27 18:42:43 +0000 | [diff] [blame] | 3827 | if (StopAtSpace && *String == L' ') {
|
| 3828 | break;
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3829 | }
|
| 3830 | }
|
| 3831 |
|
| 3832 | *Value = Result;
|
| 3833 | return (EFI_SUCCESS);
|
| 3834 | }
|
| 3835 |
|
| 3836 | /**
|
| 3837 | Convert a Null-terminated Unicode decimal string to a value of
|
| 3838 | type UINT64.
|
| 3839 |
|
| 3840 | This function returns a value of type UINT64 by interpreting the contents
|
| 3841 | of the Unicode string specified by String as a decimal number. The format
|
| 3842 | of the input Unicode string String is:
|
| 3843 |
|
| 3844 | [spaces] [decimal digits].
|
| 3845 |
|
| 3846 | The valid decimal digit character is in the range [0-9]. The
|
| 3847 | function will ignore the pad space, which includes spaces or
|
| 3848 | tab characters, before [decimal digits]. The running zero in the
|
| 3849 | beginning of [decimal digits] will be ignored. Then, the function
|
| 3850 | stops at the first character that is a not a valid decimal character
|
| 3851 | or a Null-terminator, whichever one comes first.
|
| 3852 |
|
| 3853 | If String has only pad spaces, then 0 is returned.
|
| 3854 | If String has no pad spaces or valid decimal digits,
|
| 3855 | then 0 is returned.
|
| 3856 |
|
| 3857 | @param[in] String A pointer to a Null-terminated Unicode string.
|
| 3858 | @param[out] Value Upon a successful return the value of the conversion.
|
| 3859 | @param[in] StopAtSpace FALSE to skip spaces.
|
| 3860 |
|
| 3861 | @retval EFI_SUCCESS The conversion was successful.
|
| 3862 | @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
|
| 3863 | @retval EFI_DEVICE_ERROR An overflow occured.
|
| 3864 | **/
|
| 3865 | EFI_STATUS
|
| 3866 | EFIAPI
|
| 3867 | InternalShellStrDecimalToUint64 (
|
| 3868 | IN CONST CHAR16 *String,
|
| 3869 | OUT UINT64 *Value,
|
| 3870 | IN CONST BOOLEAN StopAtSpace
|
| 3871 | )
|
| 3872 | {
|
| 3873 | UINT64 Result;
|
| 3874 |
|
| 3875 | if (String == NULL || StrSize (String) == 0 || Value == NULL) {
|
| 3876 | return (EFI_INVALID_PARAMETER);
|
| 3877 | }
|
| 3878 |
|
| 3879 | //
|
| 3880 | // Ignore the pad spaces (space or tab)
|
| 3881 | //
|
| 3882 | while ((*String == L' ') || (*String == L'\t')) {
|
| 3883 | String++;
|
| 3884 | }
|
| 3885 |
|
| 3886 | //
|
| 3887 | // Ignore leading Zeros after the spaces
|
| 3888 | //
|
| 3889 | while (*String == L'0') {
|
| 3890 | String++;
|
| 3891 | }
|
| 3892 |
|
| 3893 | Result = 0;
|
| 3894 |
|
| 3895 | //
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3896 | // Stop upon space if requested
|
| 3897 | // (if the whole value was 0)
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3898 | //
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3899 | if (StopAtSpace && *String == L' ') {
|
| 3900 | *Value = Result;
|
| 3901 | return (EFI_SUCCESS);
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3902 | }
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3903 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3904 | while (ShellIsDecimalDigitCharacter (*String)) {
|
| 3905 | //
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3906 | // If the number represented by String overflows according
|
Jaben Carsey | 80f3e34 | 2015-07-02 17:26:42 +0000 | [diff] [blame] | 3907 | // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3908 | //
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3909 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3910 | if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
|
| 3911 | return (EFI_DEVICE_ERROR);
|
| 3912 | }
|
| 3913 |
|
| 3914 | Result = MultU64x32(Result, 10) + (*String - L'0');
|
| 3915 | String++;
|
| 3916 |
|
| 3917 | //
|
| 3918 | // Stop at spaces if requested
|
| 3919 | //
|
| 3920 | if (StopAtSpace && *String == L' ') {
|
| 3921 | break;
|
| 3922 | }
|
| 3923 | }
|
| 3924 |
|
| 3925 | *Value = Result;
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3926 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3927 | return (EFI_SUCCESS);
|
| 3928 | }
|
| 3929 |
|
| 3930 | /**
|
| 3931 | Function to verify and convert a string to its numerical value.
|
| 3932 |
|
| 3933 | If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
|
| 3934 |
|
| 3935 | @param[in] String The string to evaluate.
|
| 3936 | @param[out] Value Upon a successful return the value of the conversion.
|
| 3937 | @param[in] ForceHex TRUE - always assume hex.
|
| 3938 | @param[in] StopAtSpace FALSE to skip spaces.
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3939 |
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3940 | @retval EFI_SUCCESS The conversion was successful.
|
| 3941 | @retval EFI_INVALID_PARAMETER String contained an invalid character.
|
| 3942 | @retval EFI_NOT_FOUND String was a number, but Value was NULL.
|
| 3943 | **/
|
| 3944 | EFI_STATUS
|
| 3945 | EFIAPI
|
| 3946 | ShellConvertStringToUint64(
|
| 3947 | IN CONST CHAR16 *String,
|
| 3948 | OUT UINT64 *Value,
|
| 3949 | IN CONST BOOLEAN ForceHex,
|
| 3950 | IN CONST BOOLEAN StopAtSpace
|
| 3951 | )
|
| 3952 | {
|
| 3953 | UINT64 RetVal;
|
| 3954 | CONST CHAR16 *Walker;
|
| 3955 | EFI_STATUS Status;
|
| 3956 | BOOLEAN Hex;
|
| 3957 |
|
| 3958 | Hex = ForceHex;
|
| 3959 |
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3960 | if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3961 | if (!Hex) {
|
| 3962 | Hex = TRUE;
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3963 | if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3964 | return (EFI_INVALID_PARAMETER);
|
| 3965 | }
|
| 3966 | } else {
|
| 3967 | return (EFI_INVALID_PARAMETER);
|
| 3968 | }
|
| 3969 | }
|
| 3970 |
|
| 3971 | //
|
| 3972 | // Chop off leading spaces
|
| 3973 | //
|
| 3974 | for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
|
| 3975 |
|
| 3976 | //
|
| 3977 | // make sure we have something left that is numeric.
|
| 3978 | //
|
jcarsey | 658bf43 | 2014-11-04 22:33:16 +0000 | [diff] [blame] | 3979 | if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3980 | return (EFI_INVALID_PARAMETER);
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 3981 | }
|
jcarsey | 252d945 | 2011-03-25 20:49:53 +0000 | [diff] [blame] | 3982 |
|
| 3983 | //
|
| 3984 | // do the conversion.
|
| 3985 | //
|
| 3986 | if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
|
| 3987 | Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
|
| 3988 | } else {
|
| 3989 | Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
|
| 3990 | }
|
| 3991 |
|
| 3992 | if (Value == NULL && !EFI_ERROR(Status)) {
|
| 3993 | return (EFI_NOT_FOUND);
|
| 3994 | }
|
| 3995 |
|
| 3996 | if (Value != NULL) {
|
| 3997 | *Value = RetVal;
|
| 3998 | }
|
| 3999 |
|
| 4000 | return (Status);
|
| 4001 | }
|
| 4002 |
|
| 4003 | /**
|
| 4004 | Function to determin if an entire string is a valid number.
|
| 4005 |
|
| 4006 | If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
|
| 4007 |
|
| 4008 | @param[in] String The string to evaluate.
|
| 4009 | @param[in] ForceHex TRUE - always assume hex.
|
| 4010 | @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
|
| 4011 |
|
| 4012 | @retval TRUE It is all numeric (dec/hex) characters.
|
| 4013 | @retval FALSE There is a non-numeric character.
|
| 4014 | **/
|
| 4015 | BOOLEAN
|
| 4016 | EFIAPI
|
| 4017 | ShellIsHexOrDecimalNumber (
|
| 4018 | IN CONST CHAR16 *String,
|
| 4019 | IN CONST BOOLEAN ForceHex,
|
| 4020 | IN CONST BOOLEAN StopAtSpace
|
| 4021 | )
|
| 4022 | {
|
| 4023 | if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
|
| 4024 | return (TRUE);
|
| 4025 | }
|
| 4026 | return (FALSE);
|
| 4027 | }
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4028 |
|
| 4029 | /**
|
| 4030 | Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
|
| 4031 | buffer. The returned buffer must be callee freed.
|
| 4032 |
|
| 4033 | If the position upon start is 0, then the Ascii Boolean will be set. This should be
|
| 4034 | maintained and not changed for all operations with the same file.
|
| 4035 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 4036 | @param[in] Handle SHELL_FILE_HANDLE to read from.
|
| 4037 | @param[in, out] Ascii Boolean value for indicating whether the file is
|
| 4038 | Ascii (TRUE) or UCS2 (FALSE).
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4039 |
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 4040 | @return The line of text from the file.
|
| 4041 | @retval NULL There was not enough memory available.
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4042 |
|
| 4043 | @sa ShellFileHandleReadLine
|
| 4044 | **/
|
| 4045 | CHAR16*
|
| 4046 | EFIAPI
|
| 4047 | ShellFileHandleReturnLine(
|
| 4048 | IN SHELL_FILE_HANDLE Handle,
|
| 4049 | IN OUT BOOLEAN *Ascii
|
| 4050 | )
|
| 4051 | {
|
| 4052 | CHAR16 *RetVal;
|
| 4053 | UINTN Size;
|
| 4054 | EFI_STATUS Status;
|
| 4055 |
|
| 4056 | Size = 0;
|
| 4057 | RetVal = NULL;
|
| 4058 |
|
| 4059 | Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
|
| 4060 | if (Status == EFI_BUFFER_TOO_SMALL) {
|
| 4061 | RetVal = AllocateZeroPool(Size);
|
jcarsey | beab0fc | 2011-10-10 17:26:25 +0000 | [diff] [blame] | 4062 | if (RetVal == NULL) {
|
| 4063 | return (NULL);
|
| 4064 | }
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4065 | Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
|
darylm503 | b0934ac | 2011-11-12 00:35:11 +0000 | [diff] [blame] | 4066 |
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4067 | }
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4068 | if (EFI_ERROR(Status) && (RetVal != NULL)) {
|
| 4069 | FreePool(RetVal);
|
| 4070 | RetVal = NULL;
|
| 4071 | }
|
| 4072 | return (RetVal);
|
| 4073 | }
|
| 4074 |
|
| 4075 | /**
|
| 4076 | Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
|
| 4077 |
|
| 4078 | If the position upon start is 0, then the Ascii Boolean will be set. This should be
|
| 4079 | maintained and not changed for all operations with the same file.
|
| 4080 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 4081 | @param[in] Handle SHELL_FILE_HANDLE to read from.
|
| 4082 | @param[in, out] Buffer The pointer to buffer to read into.
|
| 4083 | @param[in, out] Size The pointer to number of bytes in Buffer.
|
| 4084 | @param[in] Truncate If the buffer is large enough, this has no effect.
|
| 4085 | If the buffer is is too small and Truncate is TRUE,
|
| 4086 | the line will be truncated.
|
| 4087 | If the buffer is is too small and Truncate is FALSE,
|
| 4088 | then no read will occur.
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4089 |
|
ydong10 | 4ff7e37 | 2011-09-02 08:05:34 +0000 | [diff] [blame] | 4090 | @param[in, out] Ascii Boolean value for indicating whether the file is
|
| 4091 | Ascii (TRUE) or UCS2 (FALSE).
|
jcarsey | 4d0a4fc | 2011-07-06 22:28:36 +0000 | [diff] [blame] | 4092 |
|
| 4093 | @retval EFI_SUCCESS The operation was successful. The line is stored in
|
| 4094 | Buffer.
|
| 4095 | @retval EFI_INVALID_PARAMETER Handle was NULL.
|
| 4096 | @retval EFI_INVALID_PARAMETER Size was NULL.
|
| 4097 | @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
|
| 4098 | Size was updated to the minimum space required.
|
| 4099 | **/
|
| 4100 | EFI_STATUS
|
| 4101 | EFIAPI
|
| 4102 | ShellFileHandleReadLine(
|
| 4103 | IN SHELL_FILE_HANDLE Handle,
|
| 4104 | IN OUT CHAR16 *Buffer,
|
| 4105 | IN OUT UINTN *Size,
|
| 4106 | IN BOOLEAN Truncate,
|
| 4107 | IN OUT BOOLEAN *Ascii
|
| 4108 | )
|
| 4109 | {
|
| 4110 | EFI_STATUS Status;
|
| 4111 | CHAR16 CharBuffer;
|
| 4112 | UINTN CharSize;
|
| 4113 | UINTN CountSoFar;
|
| 4114 | UINT64 OriginalFilePosition;
|
| 4115 |
|
| 4116 |
|
| 4117 | if (Handle == NULL
|
| 4118 | ||Size == NULL
|
| 4119 | ){
|
| 4120 | return (EFI_INVALID_PARAMETER);
|
| 4121 | }
|
| 4122 | if (Buffer == NULL) {
|
| 4123 | ASSERT(*Size == 0);
|
| 4124 | } else {
|
| 4125 | *Buffer = CHAR_NULL;
|
| 4126 | }
|
| 4127 | gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
|
| 4128 | if (OriginalFilePosition == 0) {
|
| 4129 | CharSize = sizeof(CHAR16);
|
| 4130 | Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
|
| 4131 | ASSERT_EFI_ERROR(Status);
|
| 4132 | if (CharBuffer == gUnicodeFileTag) {
|
| 4133 | *Ascii = FALSE;
|
| 4134 | } else {
|
| 4135 | *Ascii = TRUE;
|
| 4136 | gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
|
| 4137 | }
|
| 4138 | }
|
| 4139 |
|
| 4140 | for (CountSoFar = 0;;CountSoFar++){
|
| 4141 | CharBuffer = 0;
|
| 4142 | if (*Ascii) {
|
| 4143 | CharSize = sizeof(CHAR8);
|
| 4144 | } else {
|
| 4145 | CharSize = sizeof(CHAR16);
|
| 4146 | }
|
| 4147 | Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
|
| 4148 | if ( EFI_ERROR(Status)
|
| 4149 | || CharSize == 0
|
| 4150 | || (CharBuffer == L'\n' && !(*Ascii))
|
| 4151 | || (CharBuffer == '\n' && *Ascii)
|
| 4152 | ){
|
| 4153 | break;
|
| 4154 | }
|
| 4155 | //
|
| 4156 | // if we have space save it...
|
| 4157 | //
|
| 4158 | if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
|
| 4159 | ASSERT(Buffer != NULL);
|
| 4160 | ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
|
| 4161 | ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
|
| 4162 | }
|
| 4163 | }
|
| 4164 |
|
| 4165 | //
|
| 4166 | // if we ran out of space tell when...
|
| 4167 | //
|
| 4168 | if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
|
| 4169 | *Size = (CountSoFar+1)*sizeof(CHAR16);
|
| 4170 | if (!Truncate) {
|
| 4171 | gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
|
| 4172 | } else {
|
| 4173 | DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
|
| 4174 | }
|
| 4175 | return (EFI_BUFFER_TOO_SMALL);
|
| 4176 | }
|
| 4177 | while(Buffer[StrLen(Buffer)-1] == L'\r') {
|
| 4178 | Buffer[StrLen(Buffer)-1] = CHAR_NULL;
|
| 4179 | }
|
| 4180 |
|
| 4181 | return (Status);
|
| 4182 | }
|
jcarsey | fb5278e | 2013-02-20 18:21:14 +0000 | [diff] [blame] | 4183 |
|
| 4184 | /**
|
jcarsey | 365aa98 | 2013-03-04 21:54:02 +0000 | [diff] [blame] | 4185 | Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
|
| 4186 |
|
| 4187 | @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
|
| 4188 | @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
|
| 4189 | @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
|
| 4190 | the help content only.
|
| 4191 | @retval EFI_DEVICE_ERROR The help data format was incorrect.
|
| 4192 | @retval EFI_NOT_FOUND The help data could not be found.
|
| 4193 | @retval EFI_SUCCESS The operation was successful.
|
| 4194 | **/
|
| 4195 | EFI_STATUS
|
| 4196 | EFIAPI
|
| 4197 | ShellPrintHelp (
|
| 4198 | IN CONST CHAR16 *CommandToGetHelpOn,
|
| 4199 | IN CONST CHAR16 *SectionToGetHelpOn,
|
| 4200 | IN BOOLEAN PrintCommandText
|
| 4201 | )
|
| 4202 | {
|
| 4203 | EFI_STATUS Status;
|
| 4204 | CHAR16 *OutText;
|
| 4205 |
|
| 4206 | OutText = NULL;
|
| 4207 |
|
| 4208 | //
|
| 4209 | // Get the string to print based
|
| 4210 | //
|
| 4211 | Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
|
| 4212 |
|
| 4213 | //
|
| 4214 | // make sure we got a valid string
|
| 4215 | //
|
| 4216 | if (EFI_ERROR(Status)){
|
| 4217 | return Status;
|
| 4218 | }
|
| 4219 | if (OutText == NULL || StrLen(OutText) == 0) {
|
| 4220 | return EFI_NOT_FOUND;
|
| 4221 | }
|
| 4222 |
|
| 4223 | //
|
| 4224 | // Chop off trailing stuff we dont need
|
| 4225 | //
|
| 4226 | while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
|
| 4227 | OutText[StrLen(OutText)-1] = CHAR_NULL;
|
| 4228 | }
|
| 4229 |
|
| 4230 | //
|
| 4231 | // Print this out to the console
|
| 4232 | //
|
| 4233 | if (PrintCommandText) {
|
| 4234 | ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
|
| 4235 | } else {
|
| 4236 | ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
|
| 4237 | }
|
| 4238 |
|
| 4239 | SHELL_FREE_NON_NULL(OutText);
|
| 4240 |
|
| 4241 | return EFI_SUCCESS;
|
| 4242 | }
|
| 4243 |
|
| 4244 | /**
|
jcarsey | fb5278e | 2013-02-20 18:21:14 +0000 | [diff] [blame] | 4245 | Function to delete a file by name
|
| 4246 |
|
| 4247 | @param[in] FileName Pointer to file name to delete.
|
| 4248 |
|
| 4249 | @retval EFI_SUCCESS the file was deleted sucessfully
|
| 4250 | @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
|
| 4251 | deleted
|
| 4252 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
| 4253 | @retval EFI_NOT_FOUND The specified file could not be found on the
|
| 4254 | device or the file system could not be found
|
| 4255 | on the device.
|
| 4256 | @retval EFI_NO_MEDIA The device has no medium.
|
| 4257 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
|
| 4258 | medium is no longer supported.
|
| 4259 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 4260 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
| 4261 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
| 4262 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
| 4263 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
|
| 4264 | file.
|
| 4265 | @retval other The file failed to open
|
| 4266 | **/
|
| 4267 | EFI_STATUS
|
| 4268 | EFIAPI
|
| 4269 | ShellDeleteFileByName(
|
| 4270 | IN CONST CHAR16 *FileName
|
| 4271 | )
|
| 4272 | {
|
| 4273 | EFI_STATUS Status;
|
| 4274 | SHELL_FILE_HANDLE FileHandle;
|
| 4275 |
|
| 4276 | Status = ShellFileExists(FileName);
|
| 4277 |
|
| 4278 | if (Status == EFI_SUCCESS){
|
| 4279 | Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
|
| 4280 | if (Status == EFI_SUCCESS){
|
| 4281 | Status = ShellDeleteFile(&FileHandle);
|
| 4282 | }
|
| 4283 | }
|
| 4284 |
|
| 4285 | return(Status);
|
| 4286 |
|
| 4287 | }
|
Qiu Shumin | 0960ba1 | 2014-09-17 07:58:31 +0000 | [diff] [blame] | 4288 |
|
| 4289 | /**
|
| 4290 | Cleans off all the quotes in the string.
|
| 4291 |
|
| 4292 | @param[in] OriginalString pointer to the string to be cleaned.
|
| 4293 | @param[out] CleanString The new string with all quotes removed.
|
| 4294 | Memory allocated in the function and free
|
| 4295 | by caller.
|
| 4296 |
|
| 4297 | @retval EFI_SUCCESS The operation was successful.
|
| 4298 | **/
|
| 4299 | EFI_STATUS
|
| 4300 | EFIAPI
|
| 4301 | InternalShellStripQuotes (
|
| 4302 | IN CONST CHAR16 *OriginalString,
|
| 4303 | OUT CHAR16 **CleanString
|
| 4304 | )
|
| 4305 | {
|
| 4306 | CHAR16 *Walker;
|
| 4307 |
|
| 4308 | if (OriginalString == NULL || CleanString == NULL) {
|
| 4309 | return EFI_INVALID_PARAMETER;
|
| 4310 | }
|
| 4311 |
|
| 4312 | *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
|
| 4313 | if (*CleanString == NULL) {
|
| 4314 | return EFI_OUT_OF_RESOURCES;
|
| 4315 | }
|
| 4316 |
|
| 4317 | for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
|
| 4318 | if (*Walker == L'\"') {
|
| 4319 | CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
|
| 4320 | }
|
| 4321 | }
|
| 4322 |
|
| 4323 | return EFI_SUCCESS;
|
| 4324 | }
|
| 4325 |
|