]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
pointer verification (not NULL) and buffer overrun fixes.
[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
40\r
41 ProblemParam = NULL;\r
42 ShellStatus = SHELL_SUCCESS;\r
43 Drive = NULL;\r
44 DriveSize = 0;\r
45\r
46 Status = CommandInit();\r
47 ASSERT_EFI_ERROR(Status);\r
48\r
49 //\r
50 // initialize the shell lib (we must be in non-auto-init...)\r
51 //\r
52 Status = ShellInitialize();\r
53 ASSERT_EFI_ERROR(Status);\r
54\r
55 //\r
56 // parse the command line\r
57 //\r
58 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
59 if (EFI_ERROR(Status)) {\r
60 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
61 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
62 FreePool(ProblemParam);\r
63 ShellStatus = SHELL_INVALID_PARAMETER;\r
64 } else {\r
65 ASSERT(FALSE);\r
66 }\r
67 }\r
68\r
69 //\r
70 // check for "-?"\r
71 //\r
72 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
73 ASSERT(FALSE);\r
74 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r
75 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);\r
76 ShellStatus = SHELL_INVALID_PARAMETER;\r
77 } else {\r
78 //\r
79 // remember that param 0 is the command name\r
80 // If there are 0 value parameters, then print the current directory\r
81 // else If there are 2 value parameters, then print the error message\r
82 // else If there is 1 value paramerer , then change the directory\r
83 //\r
84 Param1 = ShellCommandLineGetRawValue(Package, 1);\r
85 if (Param1 == NULL) {\r
86 //\r
87 // display the current directory\r
88 //\r
89 Directory = ShellGetCurrentDir(NULL);\r
90 if (Directory != NULL) {\r
91 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory);\r
92 } else {\r
93 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
94 ShellStatus = SHELL_NOT_FOUND;\r
95 }\r
96 } else {\r
97 if (StrCmp(Param1, L".") == 0) {\r
98 //\r
99 // nothing to do... change to current directory\r
100 //\r
101 } else if (StrCmp(Param1, L"..") == 0) {\r
102 //\r
103 // Change up one directory...\r
104 //\r
105 Directory = ShellGetCurrentDir(NULL);\r
106 if (Directory == NULL) {\r
107 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
108 ShellStatus = SHELL_NOT_FOUND;\r
109 } else {\r
110 Drive = GetFullyQualifiedPath(Directory);\r
111 ChopLastSlash(Drive);\r
112 }\r
113 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
114 //\r
115 // change directory on current drive letter\r
116 //\r
117 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
118 if (Status == EFI_NOT_FOUND) {\r
119 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
120 ShellStatus = SHELL_NOT_FOUND;\r
121 }\r
122 }\r
123 } else if (StrCmp(Param1, L"\\") == 0) {\r
124 //\r
125 // Move to root of current drive\r
126 //\r
127 Directory = ShellGetCurrentDir(NULL);\r
128 if (Directory == NULL) {\r
129 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
130 ShellStatus = SHELL_NOT_FOUND;\r
131 } else {\r
132 Drive = GetFullyQualifiedPath(Directory);\r
133 while (ChopLastSlash(Drive)) ;\r
134 }\r
135 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
136 //\r
137 // change directory on current drive letter\r
138 //\r
139 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
140 if (Status == EFI_NOT_FOUND) {\r
141 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
142 ShellStatus = SHELL_NOT_FOUND;\r
143 }\r
144 }\r
145 } else if (StrStr(Param1, L":") == NULL) {\r
146 if (ShellGetCurrentDir(NULL) == NULL) {\r
147 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
148 ShellStatus = SHELL_NOT_FOUND;\r
149 } else {\r
150 ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
151 Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
152 if (*Param1 == L'\\') {\r
153 while (ChopLastSlash(Drive)) ;\r
154 Drive = StrnCatGrow(&Drive, &DriveSize, Param1+1, 0);\r
155 } else {\r
156 Drive = StrnCatGrow(&Drive, &DriveSize, Param1, 0);\r
157 }\r
158 //\r
159 // Verify that this is a valid directory\r
160 //\r
161 Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
162 if (EFI_ERROR(Status)) {\r
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
164 ShellStatus = SHELL_NOT_FOUND;\r
165 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
167 ShellStatus = SHELL_NOT_FOUND;\r
168 }\r
169 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
170 //\r
171 // change directory on current drive letter\r
172 //\r
173 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
174 if (Status == EFI_NOT_FOUND) {\r
175 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
176 ShellStatus = SHELL_NOT_FOUND;\r
177 }\r
178 }\r
179 if (Handle != NULL) {\r
180 gEfiShellProtocol->CloseFile(Handle);\r
181 DEBUG_CODE(Handle = NULL;);\r
182 }\r
183 }\r
184 } else {\r
185 //\r
186 // change directory on other drive letter\r
187 //\r
188 Drive = AllocateZeroPool(StrSize(Param1));\r
9ea69f8a 189 if (Drive == NULL) {\r
190 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
191 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
192 } else {\r
193 Drive = StrCpy(Drive, Param1);\r
194 Path = StrStr(Drive, L":");\r
b54fd049 195 ASSERT(Path != NULL);\r
196 if (*(Path+1) == CHAR_NULL) {\r
9ea69f8a 197 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
198 ShellStatus = SHELL_NOT_FOUND;\r
199 } else {\r
b54fd049 200 *(Path+1) = CHAR_NULL;\r
201 if (Path == Drive + StrLen(Drive)) {\r
202 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
203 ShellStatus = SHELL_NOT_FOUND;\r
204 } else {\r
205 Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
206 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
207 }\r
9ea69f8a 208 }\r
9ea69f8a 209 if (Status == EFI_NOT_FOUND) {\r
210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
211 Status = SHELL_NOT_FOUND;\r
212 } else if (EFI_ERROR(Status)) {\r
213 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1);\r
214 Status = SHELL_NOT_FOUND;\r
215 }\r
a405b86d 216 }\r
217 }\r
218 }\r
219 }\r
220\r
221 if (Drive != NULL) {\r
222 FreePool(Drive);\r
223 }\r
224 //\r
225 // free the command line package\r
226 //\r
227 ShellCommandLineFreeVarList (Package);\r
228\r
229 //\r
230 // return the status\r
231 //\r
232 return (ShellStatus);\r
233}\r
234\r