TianoCore EDK2 master
Loading...
Searching...
No Matches
Vol.c
Go to the documentation of this file.
1
11#include <Guid/FileSystemInfo.h>
13
25 IN CONST CHAR16 *Path,
26 IN CONST BOOLEAN Delete,
27 IN CONST CHAR16 *Name OPTIONAL
28 )
29{
30 EFI_STATUS Status;
31 SHELL_STATUS ShellStatus;
32 EFI_FILE_SYSTEM_INFO *SysInfo;
33 UINTN SysInfoSize;
34 SHELL_FILE_HANDLE ShellFileHandle;
35 EFI_FILE_PROTOCOL *EfiFpHandle;
36 UINTN Size1;
37 UINTN Size2;
38
39 ShellStatus = SHELL_SUCCESS;
40
41 if (
42 (Name != NULL) && (
43 (StrStr (Name, L"%") != NULL) ||
44 (StrStr (Name, L"^") != NULL) ||
45 (StrStr (Name, L"*") != NULL) ||
46 (StrStr (Name, L"+") != NULL) ||
47 (StrStr (Name, L"=") != NULL) ||
48 (StrStr (Name, L"[") != NULL) ||
49 (StrStr (Name, L"]") != NULL) ||
50 (StrStr (Name, L"|") != NULL) ||
51 (StrStr (Name, L":") != NULL) ||
52 (StrStr (Name, L";") != NULL) ||
53 (StrStr (Name, L"\"") != NULL) ||
54 (StrStr (Name, L"<") != NULL) ||
55 (StrStr (Name, L">") != NULL) ||
56 (StrStr (Name, L"?") != NULL) ||
57 (StrStr (Name, L"/") != NULL) ||
58 (StrStr (Name, L" ") != NULL))
59 )
60 {
61 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"vol", Name);
63 }
64
65 Status = gEfiShellProtocol->OpenFileByName (
66 Path,
67 &ShellFileHandle,
68 Name != NULL ? EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE : EFI_FILE_MODE_READ
69 );
70
71 if (EFI_ERROR (Status) || (ShellFileHandle == NULL)) {
72 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel2HiiHandle, L"vol", Path);
73 return (SHELL_ACCESS_DENIED);
74 }
75
76 //
77 // Get the Volume Info from ShellFileHandle
78 //
79 SysInfo = NULL;
80 SysInfoSize = 0;
81 EfiFpHandle = ConvertShellHandleToEfiFileProtocol (ShellFileHandle);
82 Status = EfiFpHandle->GetInfo (
83 EfiFpHandle,
84 &gEfiFileSystemInfoGuid,
85 &SysInfoSize,
86 SysInfo
87 );
88
89 if (Status == EFI_BUFFER_TOO_SMALL) {
90 SysInfo = AllocateZeroPool (SysInfoSize);
91 if (SysInfo == NULL) {
92 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"vol");
93 ASSERT (FALSE);
95 }
96
97 Status = EfiFpHandle->GetInfo (
98 EfiFpHandle,
99 &gEfiFileSystemInfoGuid,
100 &SysInfoSize,
101 SysInfo
102 );
103 }
104
105 if (Delete) {
106 *((CHAR16 *)SysInfo->VolumeLabel) = CHAR_NULL;
108 Status = EfiFpHandle->SetInfo (
109 EfiFpHandle,
110 &gEfiFileSystemInfoGuid,
111 (UINTN)SysInfo->Size,
112 SysInfo
113 );
114 } else if (Name != NULL) {
115 Size1 = StrSize (Name);
116 Size2 = StrSize (SysInfo->VolumeLabel);
117 if (Size1 > Size2) {
118 SysInfo = ReallocatePool ((UINTN)SysInfo->Size, (UINTN)SysInfo->Size + Size1 - Size2, SysInfo);
119 if (SysInfo == NULL) {
120 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"vol");
121 ShellStatus = SHELL_OUT_OF_RESOURCES;
122 }
123 }
124
125 if (SysInfo != NULL) {
126 StrCpyS (
127 (CHAR16 *)SysInfo->VolumeLabel,
128 (Size1 > Size2 ? Size1/sizeof (CHAR16) : Size2/sizeof (CHAR16)),
129 Name
130 );
131 SysInfo->Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + Size1;
132 Status = EfiFpHandle->SetInfo (
133 EfiFpHandle,
134 &gEfiFileSystemInfoGuid,
135 (UINTN)SysInfo->Size,
136 SysInfo
137 );
138 }
139 }
140
141 FreePool (SysInfo);
142
143 if (Delete || (Name != NULL)) {
144 if (EFI_ERROR (Status)) {
145 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_AD), gShellLevel2HiiHandle, L"vol", Path);
146 ShellStatus = SHELL_ACCESS_DENIED;
147 }
148 }
149
150 SysInfoSize = 0;
151 SysInfo = NULL;
152
153 Status = EfiFpHandle->GetInfo (
154 EfiFpHandle,
155 &gEfiFileSystemInfoGuid,
156 &SysInfoSize,
157 SysInfo
158 );
159
160 if (Status == EFI_BUFFER_TOO_SMALL) {
161 SysInfo = AllocateZeroPool (SysInfoSize);
162 if (SysInfo == NULL) {
163 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"vol");
165 }
166
167 Status = EfiFpHandle->GetInfo (
168 EfiFpHandle,
169 &gEfiFileSystemInfoGuid,
170 &SysInfoSize,
171 SysInfo
172 );
173 }
174
175 gEfiShellProtocol->CloseFile (ShellFileHandle);
176
177 ASSERT (SysInfo != NULL);
178
179 if (SysInfo != NULL) {
180 //
181 // print VolumeInfo table
182 //
184 0,
186 NULL,
187 STRING_TOKEN (STR_VOL_VOLINFO),
188 gShellLevel2HiiHandle,
189 SysInfo->VolumeLabel,
190 SysInfo->ReadOnly ? L"r" : L"rw",
191 SysInfo->VolumeSize,
192 SysInfo->FreeSpace,
193 SysInfo->BlockSize
194 );
195 SHELL_FREE_NON_NULL (SysInfo);
196 }
197
198 return (ShellStatus);
199}
200
201STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
202 { L"-d", TypeFlag },
203 { L"-n", TypeValue },
204 { NULL, TypeMax }
205};
206
214EFIAPI
216 IN EFI_HANDLE ImageHandle,
217 IN EFI_SYSTEM_TABLE *SystemTable
218 )
219{
220 EFI_STATUS Status;
221 LIST_ENTRY *Package;
222 CHAR16 *ProblemParam;
223 SHELL_STATUS ShellStatus;
224 CONST CHAR16 *PathName;
225 CONST CHAR16 *CurDir;
226 BOOLEAN DeleteMode;
227 CHAR16 *FullPath;
228 CHAR16 *TempSpot;
229 UINTN Length;
230 CONST CHAR16 *NewName;
231
232 Length = 0;
233 ProblemParam = NULL;
234 ShellStatus = SHELL_SUCCESS;
235 PathName = NULL;
236 CurDir = NULL;
237 FullPath = NULL;
238
239 //
240 // initialize the shell lib (we must be in non-auto-init...)
241 //
242 Status = ShellInitialize ();
243 ASSERT_EFI_ERROR (Status);
244
245 //
246 // Fix local copies of the protocol pointers
247 //
248 Status = CommandInit ();
249 ASSERT_EFI_ERROR (Status);
250
251 //
252 // parse the command line
253 //
254 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
255 if (EFI_ERROR (Status)) {
256 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
257 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"vol", ProblemParam);
258 FreePool (ProblemParam);
259 ShellStatus = SHELL_INVALID_PARAMETER;
260 } else {
261 ASSERT (FALSE);
262 }
263 } else {
264 //
265 // check for "-?"
266 //
267 if (ShellCommandLineGetFlag (Package, L"-?")) {
268 ASSERT (FALSE);
269 }
270
271 if (ShellCommandLineGetCount (Package) > 2) {
272 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"vol");
273 ShellStatus = SHELL_INVALID_PARAMETER;
274 } else {
275 PathName = ShellCommandLineGetRawValue (Package, 1);
276 if (PathName == NULL) {
277 CurDir = gEfiShellProtocol->GetCurDir (NULL);
278 if (CurDir == NULL) {
279 ShellStatus = SHELL_NOT_FOUND;
280 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"vol");
281 } else {
282 PathName = CurDir;
283 }
284 }
285
286 if (PathName != NULL) {
287 TempSpot = StrStr (PathName, L":");
288 if (TempSpot != NULL) {
289 *TempSpot = CHAR_NULL;
290 }
291
292 TempSpot = StrStr (PathName, L"\\");
293 if (TempSpot != NULL) {
294 *TempSpot = CHAR_NULL;
295 }
296
297 StrnCatGrow (&FullPath, &Length, PathName, 0);
298 StrnCatGrow (&FullPath, &Length, L":\\", 0);
299 DeleteMode = ShellCommandLineGetFlag (Package, L"-d");
300 NewName = ShellCommandLineGetValue (Package, L"-n");
301 if (DeleteMode && ShellCommandLineGetFlag (Package, L"-n")) {
302 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellLevel2HiiHandle, L"vol", L"-d", L"-n");
303 ShellStatus = SHELL_INVALID_PARAMETER;
304 } else if (ShellCommandLineGetFlag (Package, L"-n") && (NewName == NULL)) {
305 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"vol", L"-n");
306 ShellStatus = SHELL_INVALID_PARAMETER;
307 } else if ((NewName != NULL) && (StrLen (NewName) > 11)) {
308 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"vol", NewName, L"-n");
309 ShellStatus = SHELL_INVALID_PARAMETER;
310 } else if (ShellStatus == SHELL_SUCCESS) {
311 ShellStatus = HandleVol (
312 FullPath,
313 DeleteMode,
314 NewName
315 );
316 }
317 }
318 }
319 }
320
321 SHELL_FREE_NON_NULL (FullPath);
322
323 //
324 // free the command line package
325 //
327
328 return (ShellStatus);
329}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
RETURN_STATUS EFIAPI StrCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:226
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
CHAR16 *EFIAPI StrStr(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString)
Definition: String.c:224
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define SIZE_OF_EFI_FILE_SYSTEM_INFO
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_STATUS EFIAPI CommandInit(VOID)
EFI_FILE_PROTOCOL *EFIAPI ConvertShellHandleToEfiFileProtocol(IN CONST SHELL_FILE_HANDLE Handle)
CONST CHAR16 *EFIAPI ShellCommandLineGetValue(IN CONST LIST_ENTRY *CheckPackage, IN CHAR16 *KeyString)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeValue
A flag that has some data following it with a space (IE "-a 1").
Definition: ShellLib.h:700
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
CHAR16 *EFIAPI StrnCatGrow(IN OUT CHAR16 **Destination, IN OUT UINTN *CurrentSize, IN CONST CHAR16 *Source, IN UINTN Count)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_SYSTEM_TABLE * gST
#define STRING_TOKEN(t)
SHELL_STATUS HandleVol(IN CONST CHAR16 *Path, IN CONST BOOLEAN Delete, IN CONST CHAR16 *Name OPTIONAL)
Definition: Vol.c:24
SHELL_STATUS EFIAPI ShellCommandRunVol(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Vol.c:215
EFI_SIMPLE_TEXT_OUTPUT_MODE * Mode
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
Definition: UefiSpec.h:2064