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