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