TianoCore EDK2 master
Loading...
Searching...
No Matches
Cd.c
Go to the documentation of this file.
1
13
30 IN OUT CHAR16 **FullPath,
31 IN CONST CHAR16 *Cwd
32 )
33{
34 CHAR16 *Splitter;
35 CHAR16 *TempBuffer;
36 UINTN TotalSize;
37
38 Splitter = NULL;
39 TempBuffer = NULL;
40 TotalSize = 0;
41
42 if ((FullPath == NULL) || (*FullPath == NULL)) {
43 return EFI_SUCCESS;
44 }
45
46 Splitter = StrStr (*FullPath, L":");
47 ASSERT (Splitter != *FullPath);
48
49 if ((Splitter != NULL) && (*(Splitter + 1) != L'\\') && (*(Splitter + 1) != L'/')) {
50 TotalSize = StrSize (Cwd) + StrSize (Splitter + 1);
51 TempBuffer = AllocateZeroPool (TotalSize);
52 if (TempBuffer == NULL) {
53 return EFI_OUT_OF_RESOURCES;
54 }
55
56 StrCpyS (TempBuffer, TotalSize / sizeof (CHAR16), Cwd);
57 StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), L"\\");
58 StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), Splitter + 1);
59
60 FreePool (*FullPath);
61 *FullPath = TempBuffer;
62 }
63
64 return EFI_SUCCESS;
65}
66
76BOOLEAN
78 IN CONST CHAR16 *FullPath,
79 IN CONST CHAR16 *Cwd
80 )
81{
82 CHAR16 *Splitter1;
83 CHAR16 *Splitter2;
84
85 Splitter1 = NULL;
86 Splitter2 = NULL;
87
88 ASSERT (FullPath != NULL);
89
90 Splitter1 = StrStr (FullPath, L":");
91 if (Splitter1 == NULL) {
92 return TRUE;
93 }
94
95 Splitter2 = StrStr (Cwd, L":");
96
97 if (((UINTN)Splitter1 - (UINTN)FullPath) != ((UINTN)Splitter2 - (UINTN)Cwd)) {
98 return FALSE;
99 } else {
100 if (StrniCmp (FullPath, Cwd, ((UINTN)Splitter1 - (UINTN)FullPath) / sizeof (CHAR16)) == 0) {
101 return TRUE;
102 } else {
103 return FALSE;
104 }
105 }
106}
107
122 IN CONST CHAR16 *FullPath,
123 OUT CHAR16 **Drive,
124 OUT CHAR16 **Path
125 )
126{
127 CHAR16 *Splitter;
128
129 ASSERT (FullPath != NULL);
130
131 Splitter = StrStr (FullPath, L":");
132
133 if (Splitter == NULL) {
134 *Drive = NULL;
135 *Path = AllocateCopyPool (StrSize (FullPath), FullPath);
136 if (*Path == NULL) {
137 return EFI_OUT_OF_RESOURCES;
138 }
139 } else {
140 if (*(Splitter + 1) == CHAR_NULL) {
141 *Drive = AllocateCopyPool (StrSize (FullPath), FullPath);
142 *Path = NULL;
143 if (*Drive == NULL) {
144 return EFI_OUT_OF_RESOURCES;
145 }
146 } else {
147 *Drive = AllocateCopyPool ((Splitter - FullPath + 2) * sizeof (CHAR16), FullPath);
148 if (*Drive == NULL) {
149 return EFI_OUT_OF_RESOURCES;
150 }
151
152 (*Drive)[Splitter - FullPath + 1] = CHAR_NULL;
153
154 *Path = AllocateCopyPool (StrSize (Splitter + 1), Splitter + 1);
155 if (*Path == NULL) {
156 FreePool (*Drive);
157 return EFI_OUT_OF_RESOURCES;
158 }
159 }
160 }
161
162 return EFI_SUCCESS;
163}
164
172EFIAPI
174 IN EFI_HANDLE ImageHandle,
175 IN EFI_SYSTEM_TABLE *SystemTable
176 )
177{
178 EFI_STATUS Status;
179 LIST_ENTRY *Package;
180 CONST CHAR16 *Cwd;
181 CHAR16 *Path;
182 CHAR16 *Drive;
183 CHAR16 *ProblemParam;
184 SHELL_STATUS ShellStatus;
185 CONST CHAR16 *Param1;
186 CHAR16 *Param1Copy;
187 CHAR16 *Walker;
188 CHAR16 *Splitter;
189 CHAR16 *TempBuffer;
190 UINTN TotalSize;
191
192 ProblemParam = NULL;
193 ShellStatus = SHELL_SUCCESS;
194 Cwd = NULL;
195 Path = NULL;
196 Drive = NULL;
197 Splitter = NULL;
198 TempBuffer = NULL;
199 TotalSize = 0;
200
201 Status = CommandInit ();
202 ASSERT_EFI_ERROR (Status);
203
204 //
205 // initialize the shell lib (we must be in non-auto-init...)
206 //
207 Status = ShellInitialize ();
208 ASSERT_EFI_ERROR (Status);
209
210 //
211 // parse the command line
212 //
213 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
214 if (EFI_ERROR (Status)) {
215 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
216 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cd", ProblemParam);
217 FreePool (ProblemParam);
218 ShellStatus = SHELL_INVALID_PARAMETER;
219 } else {
220 ASSERT (FALSE);
221 }
222 }
223
224 //
225 // check for "-?"
226 //
227 if (ShellCommandLineGetFlag (Package, L"-?")) {
228 ASSERT (FALSE);
229 } else if (ShellCommandLineGetRawValue (Package, 2) != NULL) {
230 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"cd");
231 ShellStatus = SHELL_INVALID_PARAMETER;
232 } else {
233 //
234 // remember that param 0 is the command name
235 // If there are 0 value parameters, then print the current directory
236 // else If there are 2 value parameters, then print the error message
237 // else If there is 1 value paramerer , then change the directory
238 //
239 Cwd = ShellGetCurrentDir (NULL);
240 if (Cwd == NULL) {
241 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd");
242 ShellStatus = SHELL_NOT_FOUND;
243 } else {
244 Param1 = ShellCommandLineGetRawValue (Package, 1);
245 if (Param1 == NULL) {
246 //
247 // display the current directory
248 //
249 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Cwd);
250 } else {
251 Param1Copy = CatSPrint (NULL, L"%s", Param1, NULL);
252 for (Walker = Param1Copy; Walker != NULL && *Walker != CHAR_NULL; Walker++) {
253 if (*Walker == L'\"') {
254 CopyMem (Walker, Walker + 1, StrSize (Walker) - sizeof (Walker[0]));
255 }
256 }
257
258 if ((Param1Copy != NULL) && IsCurrentFileSystem (Param1Copy, Cwd)) {
259 Status = ReplaceDriveWithCwd (&Param1Copy, Cwd);
260 } else {
261 //
262 // Can't use cd command to change filesystem.
263 //
264 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd");
265 Status = EFI_NOT_FOUND;
266 }
267
268 if (!EFI_ERROR (Status) && (Param1Copy != NULL)) {
269 Splitter = StrStr (Cwd, L":");
270 if (Param1Copy[0] == L'\\') {
271 //
272 // Absolute Path on current drive letter.
273 //
274 TotalSize = ((Splitter - Cwd + 1) * sizeof (CHAR16)) + StrSize (Param1Copy);
275 TempBuffer = AllocateZeroPool (TotalSize);
276 if (TempBuffer == NULL) {
277 Status = EFI_OUT_OF_RESOURCES;
278 } else {
279 StrnCpyS (TempBuffer, TotalSize / sizeof (CHAR16), Cwd, (Splitter - Cwd + 1));
280 StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), Param1Copy);
281
282 FreePool (Param1Copy);
283 Param1Copy = TempBuffer;
284 TempBuffer = NULL;
285 }
286 } else {
287 if (StrStr (Param1Copy, L":") == NULL) {
288 TotalSize = StrSize (Cwd) + StrSize (Param1Copy);
289 TempBuffer = AllocateZeroPool (TotalSize);
290 if (TempBuffer == NULL) {
291 Status = EFI_OUT_OF_RESOURCES;
292 } else {
293 StrCpyS (TempBuffer, TotalSize / sizeof (CHAR16), Cwd);
294 StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), L"\\");
295 StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), Param1Copy);
296
297 FreePool (Param1Copy);
298 Param1Copy = TempBuffer;
299 TempBuffer = NULL;
300 }
301 }
302 }
303 }
304
305 if (!EFI_ERROR (Status)) {
306 Param1Copy = PathCleanUpDirectories (Param1Copy);
307 if (Param1Copy == NULL) {
308 Status = EFI_NOT_FOUND;
309 ShellStatus = SHELL_INVALID_PARAMETER;
310 } else {
311 Status = ExtractDriveAndPath (Param1Copy, &Drive, &Path);
312 }
313 }
314
315 if (!EFI_ERROR (Status) && (Drive != NULL) && (Path != NULL)) {
316 if (EFI_ERROR (ShellIsDirectory (Param1Copy))) {
317 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Param1Copy);
318 ShellStatus = SHELL_NOT_FOUND;
319 } else {
320 Status = gEfiShellProtocol->SetCurDir (Drive, Path + 1);
321 if (EFI_ERROR (Status)) {
322 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Param1Copy);
323 ShellStatus = SHELL_NOT_FOUND;
324 }
325 }
326 }
327
328 if (Drive != NULL) {
329 FreePool (Drive);
330 }
331
332 if (Path != NULL) {
333 FreePool (Path);
334 }
335
336 FreePool (Param1Copy);
337 }
338 }
339 }
340
341 //
342 // free the command line package
343 //
345
346 //
347 // return the status
348 //
349 return (ShellStatus);
350}
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
RETURN_STATUS EFIAPI StrCatS(IN OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:405
CHAR16 *EFIAPI PathCleanUpDirectories(IN CHAR16 *Path)
Definition: FilePaths.c:68
RETURN_STATUS EFIAPI StrnCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source, IN UINTN Length)
Definition: SafeString.c:310
CHAR16 *EFIAPI StrStr(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString)
Definition: String.c:224
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
EFI_STATUS ExtractDriveAndPath(IN CONST CHAR16 *FullPath, OUT CHAR16 **Drive, OUT CHAR16 **Path)
Definition: Cd.c:121
BOOLEAN IsCurrentFileSystem(IN CONST CHAR16 *FullPath, IN CONST CHAR16 *Cwd)
Definition: Cd.c:77
EFI_STATUS ReplaceDriveWithCwd(IN OUT CHAR16 **FullPath, IN CONST CHAR16 *Cwd)
Definition: Cd.c:29
SHELL_STATUS EFIAPI ShellCommandRunCd(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Cd.c:173
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ 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)
CONST CHAR16 *EFIAPI ShellGetCurrentDir(IN CHAR16 *CONST DeviceName OPTIONAL)
#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)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellIsDirectory(IN CONST CHAR16 *DirName)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
SHELL_PARAM_ITEM EmptyParamList[]
Helper structure for no parameters (besides -? and -b)
Definition: UefiShellLib.c:19
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
#define STRING_TOKEN(t)
CHAR16 *EFIAPI CatSPrint(IN CHAR16 *String OPTIONAL, IN CONST CHAR16 *FormatString,...)
Definition: UefiLibPrint.c:827
INTN StrniCmp(IN CONST CHAR16 *Source, IN CONST CHAR16 *Target, IN CONST UINTN Count)