]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg: Verify memory allocations without ASSERT.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cd.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for attrib shell level 2 function.\r
3\r
b54fd049 4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
a405b86d 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellLevel2CommandsLib.h"\r
16\r
17/**\r
18 Function for 'cd' command.\r
19\r
20 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
21 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
22**/\r
23SHELL_STATUS\r
24EFIAPI\r
25ShellCommandRunCd (\r
26 IN EFI_HANDLE ImageHandle,\r
27 IN EFI_SYSTEM_TABLE *SystemTable\r
28 )\r
29{\r
30 EFI_STATUS Status;\r
31 LIST_ENTRY *Package;\r
32 CONST CHAR16 *Directory;\r
33 CHAR16 *Path;\r
34 CHAR16 *Drive;\r
35 UINTN DriveSize;\r
36 CHAR16 *ProblemParam;\r
37 SHELL_STATUS ShellStatus;\r
38 SHELL_FILE_HANDLE Handle;\r
39 CONST CHAR16 *Param1;\r
1fc3749d 40 CHAR16 *Param1Copy;\r
a405b86d 41\r
42 ProblemParam = NULL;\r
43 ShellStatus = SHELL_SUCCESS;\r
44 Drive = NULL;\r
45 DriveSize = 0;\r
46\r
47 Status = CommandInit();\r
48 ASSERT_EFI_ERROR(Status);\r
49\r
50 //\r
51 // initialize the shell lib (we must be in non-auto-init...)\r
52 //\r
53 Status = ShellInitialize();\r
54 ASSERT_EFI_ERROR(Status);\r
55\r
56 //\r
57 // parse the command line\r
58 //\r
59 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
60 if (EFI_ERROR(Status)) {\r
61 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
62 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
63 FreePool(ProblemParam);\r
64 ShellStatus = SHELL_INVALID_PARAMETER;\r
65 } else {\r
66 ASSERT(FALSE);\r
67 }\r
68 }\r
69\r
70 //\r
71 // check for "-?"\r
72 //\r
73 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
74 ASSERT(FALSE);\r
75 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r
76 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);\r
77 ShellStatus = SHELL_INVALID_PARAMETER;\r
78 } else {\r
79 //\r
80 // remember that param 0 is the command name\r
81 // If there are 0 value parameters, then print the current directory\r
82 // else If there are 2 value parameters, then print the error message\r
83 // else If there is 1 value paramerer , then change the directory\r
84 //\r
85 Param1 = ShellCommandLineGetRawValue(Package, 1);\r
86 if (Param1 == NULL) {\r
87 //\r
88 // display the current directory\r
89 //\r
90 Directory = ShellGetCurrentDir(NULL);\r
91 if (Directory != NULL) {\r
92 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory);\r
93 } else {\r
94 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
95 ShellStatus = SHELL_NOT_FOUND;\r
96 }\r
97 } else {\r
1fc3749d 98 Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
beab0fc5 99 if (Param1Copy != NULL) {\r
100 Param1Copy = PathCleanUpDirectories(Param1Copy);\r
101 }\r
102 if (Param1Copy != NULL) {\r
103 if (StrCmp(Param1Copy, L".") == 0) {\r
a405b86d 104 //\r
beab0fc5 105 // nothing to do... change to current directory\r
a405b86d 106 //\r
beab0fc5 107 } else if (StrCmp(Param1Copy, L"..") == 0) {\r
a405b86d 108 //\r
beab0fc5 109 // Change up one directory...\r
a405b86d 110 //\r
beab0fc5 111 Directory = ShellGetCurrentDir(NULL);\r
112 if (Directory == NULL) {\r
113 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
a405b86d 114 ShellStatus = SHELL_NOT_FOUND;\r
a405b86d 115 } else {\r
beab0fc5 116 Drive = GetFullyQualifiedPath(Directory);\r
117 PathRemoveLastItem(Drive);\r
a405b86d 118 }\r
beab0fc5 119 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
120 //\r
121 // change directory on current drive letter\r
122 //\r
123 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
124 if (Status == EFI_NOT_FOUND) {\r
125 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
126 ShellStatus = SHELL_NOT_FOUND;\r
127 }\r
128 }\r
129 } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
a405b86d 130 //\r
beab0fc5 131 // Move to root of current drive\r
a405b86d 132 //\r
beab0fc5 133 Directory = ShellGetCurrentDir(NULL);\r
134 if (Directory == NULL) {\r
135 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
a405b86d 136 ShellStatus = SHELL_NOT_FOUND;\r
beab0fc5 137 } else {\r
138 Drive = GetFullyQualifiedPath(Directory);\r
139 while (PathRemoveLastItem(Drive)) ;\r
a405b86d 140 }\r
141 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
142 //\r
143 // change directory on current drive letter\r
144 //\r
145 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
146 if (Status == EFI_NOT_FOUND) {\r
147 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
148 ShellStatus = SHELL_NOT_FOUND;\r
149 }\r
150 }\r
beab0fc5 151 } else if (StrStr(Param1Copy, L":") == NULL) {\r
152 if (ShellGetCurrentDir(NULL) == NULL) {\r
153 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
154 ShellStatus = SHELL_NOT_FOUND;\r
155 } else {\r
156 ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
157 Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
158 if (*Param1Copy == L'\\') {\r
159 while (PathRemoveLastItem(Drive)) ;\r
160 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
161 } else {\r
162 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
163 }\r
164 //\r
165 // Verify that this is a valid directory\r
166 //\r
167 Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
168 if (EFI_ERROR(Status)) {\r
169 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
170 ShellStatus = SHELL_NOT_FOUND;\r
171 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
172 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
173 ShellStatus = SHELL_NOT_FOUND;\r
174 }\r
175 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
176 //\r
177 // change directory on current drive letter\r
178 //\r
179 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
180 if (Status == EFI_NOT_FOUND) {\r
181 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
182 ShellStatus = SHELL_NOT_FOUND;\r
183 }\r
184 }\r
185 if (Handle != NULL) {\r
186 gEfiShellProtocol->CloseFile(Handle);\r
187 DEBUG_CODE(Handle = NULL;);\r
188 }\r
a405b86d 189 }\r
9ea69f8a 190 } else {\r
beab0fc5 191 //\r
192 // change directory on other drive letter\r
193 //\r
194 Drive = AllocateZeroPool(StrSize(Param1Copy));\r
195 if (Drive == NULL) {\r
196 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
197 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
9ea69f8a 198 } else {\r
beab0fc5 199 Drive = StrCpy(Drive, Param1Copy);\r
200 Path = StrStr(Drive, L":");\r
201 ASSERT(Path != NULL);\r
202 if (*(Path+1) == CHAR_NULL) {\r
b54fd049 203 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
204 ShellStatus = SHELL_NOT_FOUND;\r
205 } else {\r
beab0fc5 206 *(Path+1) = CHAR_NULL;\r
207 if (Path == Drive + StrLen(Drive)) {\r
208 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
209 ShellStatus = SHELL_NOT_FOUND;\r
210 } else {\r
211 Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
212 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
213 }\r
214 }\r
215 if (Status == EFI_NOT_FOUND) {\r
216 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
217 Status = SHELL_NOT_FOUND;\r
218 } else if (EFI_ERROR(Status)) {\r
219 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1Copy);\r
220 Status = SHELL_NOT_FOUND;\r
b54fd049 221 }\r
9ea69f8a 222 }\r
a405b86d 223 }\r
224 }\r
1fc3749d 225 FreePool(Param1Copy);\r
a405b86d 226 }\r
227 }\r
228\r
229 if (Drive != NULL) {\r
230 FreePool(Drive);\r
231 }\r
232 //\r
233 // free the command line package\r
234 //\r
235 ShellCommandLineFreeVarList (Package);\r
236\r
237 //\r
238 // return the status\r
239 //\r
240 return (ShellStatus);\r
241}\r
242\r