2 Main file for NULL named library for level 2 shell command functions.
21 * functions are non-interactive only
24 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
25 This program and the accompanying materials
26 are licensed and made available under the terms and conditions of the BSD License
27 which accompanies this distribution. The full text of the license may be found at
28 http://opensource.org/licenses/bsd-license.php
30 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
31 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
34 #include "UefiShellLevel2CommandsLib.h"
36 CONST CHAR16 mFileName
[] = L
"ShellCommands";
37 EFI_HANDLE gShellLevel2HiiHandle
= NULL
;
38 CONST EFI_GUID gShellLevel2HiiGuid
= \
40 0xf95a7ccc, 0x4c55, 0x4426, { 0xa7, 0xb4, 0xdc, 0x89, 0x61, 0x95, 0xb, 0xae } \
45 ShellCommandGetManFileNameLevel2 (
53 Constructor for the Shell Level 2 Commands library.
55 Install the handlers for level 2 UEFI Shell 2.0 commands.
57 @param ImageHandle the image handle of the process
58 @param SystemTable the EFI System Table pointer
60 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
61 @retval EFI_UNSUPPORTED the shell level required was not found.
65 ShellLevel2CommandsLibConstructor (
66 IN EFI_HANDLE ImageHandle
,
67 IN EFI_SYSTEM_TABLE
*SystemTable
71 // if shell level is less than 2 do nothing
73 if (PcdGet8(PcdShellSupportLevel
) < 2) {
74 return (EFI_UNSUPPORTED
);
77 gShellLevel2HiiHandle
= HiiAddPackages (&gShellLevel2HiiGuid
, gImageHandle
, UefiShellLevel2CommandsLibStrings
, NULL
);
78 if (gShellLevel2HiiHandle
== NULL
) {
79 return (EFI_DEVICE_ERROR
);
83 // install our shell command handlers that are always installed
85 ShellCommandRegisterCommandName(L
"attrib", ShellCommandRunAttrib
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_ATTRIB
) );
86 ShellCommandRegisterCommandName(L
"cd", ShellCommandRunCd
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_CD
) );
87 ShellCommandRegisterCommandName(L
"cp", ShellCommandRunCp
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_CP
) );
88 ShellCommandRegisterCommandName(L
"load", ShellCommandRunLoad
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_LOAD
) );
89 ShellCommandRegisterCommandName(L
"map", ShellCommandRunMap
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_MAP
) );
90 ShellCommandRegisterCommandName(L
"mkdir", ShellCommandRunMkDir
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_MKDIR
) );
91 ShellCommandRegisterCommandName(L
"mv", ShellCommandRunMv
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_MV
) );
92 ShellCommandRegisterCommandName(L
"parse", ShellCommandRunParse
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_PARSE
) );
93 ShellCommandRegisterCommandName(L
"reset", ShellCommandRunReset
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_RESET
) );
94 ShellCommandRegisterCommandName(L
"set", ShellCommandRunSet
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_SET
) );
95 ShellCommandRegisterCommandName(L
"ls", ShellCommandRunLs
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_LS
) );
96 ShellCommandRegisterCommandName(L
"rm", ShellCommandRunRm
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_RM
) );
99 // support for permenant (built in) aliases
101 ShellCommandRegisterAlias(L
"rm", L
"del");
102 ShellCommandRegisterAlias(L
"ls", L
"dir");
103 ShellCommandRegisterAlias(L
"cp", L
"copy");
104 ShellCommandRegisterAlias(L
"mkdir", L
"md");
105 ShellCommandRegisterAlias(L
"cd ..", L
"cd..");
106 ShellCommandRegisterAlias(L
"cd \\", L
"cd\\");
108 // These are installed in level 2 or 3...
110 if (PcdGet8(PcdShellSupportLevel
) == 2 || PcdGet8(PcdShellSupportLevel
) == 3) {
111 ShellCommandRegisterCommandName(L
"date", ShellCommandRunDate
, ShellCommandGetManFileNameLevel2
, PcdGet8(PcdShellSupportLevel
), L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_DATE
) );
112 ShellCommandRegisterCommandName(L
"time", ShellCommandRunTime
, ShellCommandGetManFileNameLevel2
, PcdGet8(PcdShellSupportLevel
), L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_TIME
) );
113 ShellCommandRegisterCommandName(L
"timezone", ShellCommandRunTimeZone
, ShellCommandGetManFileNameLevel2
, PcdGet8(PcdShellSupportLevel
), L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_TIMEZONE
));
117 // we want to be able to test these so install them under a different name in debug mode...
119 ShellCommandRegisterCommandName(L
"l2date", ShellCommandRunDate
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_DATE
) );
120 ShellCommandRegisterCommandName(L
"l2time", ShellCommandRunTime
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_TIME
) );
121 ShellCommandRegisterCommandName(L
"l2timezone", ShellCommandRunTimeZone
, ShellCommandGetManFileNameLevel2
, 2, L
"", TRUE
, gShellLevel2HiiHandle
, STRING_TOKEN(STR_GET_HELP_TIMEZONE
));
125 return (EFI_SUCCESS
);
129 Destructor for the library. free any resources.
131 @param ImageHandle The image handle of the process.
132 @param SystemTable The EFI System Table pointer.
134 @retval EFI_SUCCESS Always returned.
138 ShellLevel2CommandsLibDestructor (
139 IN EFI_HANDLE ImageHandle
,
140 IN EFI_SYSTEM_TABLE
*SystemTable
143 if (gShellLevel2HiiHandle
!= NULL
) {
144 HiiRemovePackages(gShellLevel2HiiHandle
);
146 return (EFI_SUCCESS
);
150 Function to clean up paths. Removes the following items:
151 single periods in the path (no need for the current directory tag)
152 double periods in the path and removes a single parent directory.
154 This will be done inline and the resultant string may be be 'too big'.
156 @param[in] PathToReturn The pointer to the string containing the path.
158 @return PathToReturn is always returned.
163 IN CHAR16
*PathToReturn
168 if (PathToReturn
==NULL
) {
172 // Fix up the directory name
174 while ((TempString
= StrStr(PathToReturn
, L
"\\..\\")) != NULL
) {
175 *TempString
= CHAR_NULL
;
177 ChopLastSlash(PathToReturn
);
178 TempSize
= StrSize(TempString
);
179 CopyMem(PathToReturn
+StrLen(PathToReturn
), TempString
, TempSize
);
181 if ((TempString
= StrStr(PathToReturn
, L
"\\..")) != NULL
&& *(TempString
+ 3) == CHAR_NULL
) {
182 *TempString
= CHAR_NULL
;
183 ChopLastSlash(PathToReturn
);
185 while ((TempString
= StrStr(PathToReturn
, L
"\\.\\")) != NULL
) {
186 *TempString
= CHAR_NULL
;
188 TempSize
= StrSize(TempString
);
189 CopyMem(PathToReturn
+StrLen(PathToReturn
), TempString
, TempSize
);
191 if ((TempString
= StrStr(PathToReturn
, L
"\\.")) != NULL
&& *(TempString
+ 2) == CHAR_NULL
) {
192 *TempString
= CHAR_NULL
;
194 return (PathToReturn
);
198 returns a fully qualified directory (contains a map drive at the begining)
199 path from a unknown directory path.
201 If Path is already fully qualified this will return a duplicat otherwise this
202 will use get the current directory and use that to build the fully qualified
205 if the return value is not NULL it must be caller freed.
207 @param[in] Path The unknown Path Value
209 @retval NULL A memory allocation failed
210 @retval NULL a fully qualified path could not be discovered.
211 @retval other pointer to a fuly qualified path.
215 GetFullyQualifiedPath(
216 IN CONST CHAR16
* Path
219 CHAR16
*PathToReturn
;
221 CONST CHAR16
*CurDir
;
226 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
228 // convert a local path to an absolute path
230 if (StrStr(Path
, L
":") == NULL
) {
231 CurDir
= gEfiShellProtocol
->GetCurDir(NULL
);
232 StrnCatGrow(&PathToReturn
, &Size
, CurDir
, 0);
233 if (*Path
== L
'\\') {
237 StrnCatGrow(&PathToReturn
, &Size
, Path
, 0);
239 CleanPath(PathToReturn
);
241 while (PathToReturn
[StrLen(PathToReturn
)-1] == L
'*') {
242 PathToReturn
[StrLen(PathToReturn
)-1] = CHAR_NULL
;
245 return (PathToReturn
);
249 Function to verify all intermediate directories in the path.
251 @param[in] Path The pointer to the path to fix.
253 @retval EFI_SUCCESS The operation was successful.
257 VerifyIntermediateDirectories (
258 IN CONST CHAR16
*Path
264 SHELL_FILE_HANDLE FileHandle
;
266 ASSERT(Path
!= NULL
);
268 Status
= EFI_SUCCESS
;
270 PathCopy
= StrnCatGrow(&PathCopy
, NULL
, Path
, 0);
273 for (TempSpot
= &PathCopy
[StrLen(PathCopy
)-1] ; *TempSpot
!= CHAR_NULL
&& *TempSpot
!= L
'\\' ; TempSpot
= &PathCopy
[StrLen(PathCopy
)-1]){
274 *TempSpot
= CHAR_NULL
;
276 if (*TempSpot
== L
'\\') {
277 *TempSpot
= CHAR_NULL
;
280 if (PathCopy
!= NULL
&& *PathCopy
!= CHAR_NULL
) {
281 Status
= VerifyIntermediateDirectories(PathCopy
);
283 if (PathCopy
[StrLen(PathCopy
)-1] != L
':') {
284 if (!EFI_ERROR(Status
)) {
285 Status
= ShellOpenFileByName(PathCopy
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
286 if (FileHandle
!= NULL
) {
287 ShellCloseFile(&FileHandle
);
293 SHELL_FREE_NON_NULL(PathCopy
);
298 // be lazy and borrow from baselib.
301 InternalCharToUpper (
308 IN CONST CHAR16
*Source
,
309 IN CONST CHAR16
*Target
,
317 ASSERT(Source
!= NULL
);
318 ASSERT(Target
!= NULL
);
320 for (LoopCount
= 0 ; LoopCount
< Count
; LoopCount
++) {
321 Char1
= InternalCharToUpper(Source
[LoopCount
]);
322 Char2
= InternalCharToUpper(Target
[LoopCount
]);
323 if (Char1
!= Char2
) {
324 return (&Source
[LoopCount
]);