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