]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
InstallOpROM in CSM driver could be enhanced to start video when RomImage is passed...
[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
99 Param1Copy = PathCleanUpDirectories(Param1Copy);\r
100 if (StrCmp(Param1Copy, L".") == 0) {\r
a405b86d 101 //\r
102 // nothing to do... change to current directory\r
103 //\r
1fc3749d 104 } else if (StrCmp(Param1Copy, L"..") == 0) {\r
a405b86d 105 //\r
106 // Change up one directory...\r
107 //\r
108 Directory = ShellGetCurrentDir(NULL);\r
109 if (Directory == NULL) {\r
110 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
111 ShellStatus = SHELL_NOT_FOUND;\r
112 } else {\r
113 Drive = GetFullyQualifiedPath(Directory);\r
ab94587a 114 PathRemoveLastItem(Drive);\r
a405b86d 115 }\r
116 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
117 //\r
118 // change directory on current drive letter\r
119 //\r
120 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
121 if (Status == EFI_NOT_FOUND) {\r
122 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
123 ShellStatus = SHELL_NOT_FOUND;\r
124 }\r
125 }\r
1fc3749d 126 } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
a405b86d 127 //\r
128 // Move to root of current drive\r
129 //\r
130 Directory = ShellGetCurrentDir(NULL);\r
131 if (Directory == NULL) {\r
132 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
133 ShellStatus = SHELL_NOT_FOUND;\r
134 } else {\r
135 Drive = GetFullyQualifiedPath(Directory);\r
ab94587a 136 while (PathRemoveLastItem(Drive)) ;\r
a405b86d 137 }\r
138 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
139 //\r
140 // change directory on current drive letter\r
141 //\r
142 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
143 if (Status == EFI_NOT_FOUND) {\r
144 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
145 ShellStatus = SHELL_NOT_FOUND;\r
146 }\r
147 }\r
1fc3749d 148 } else if (StrStr(Param1Copy, L":") == NULL) {\r
a405b86d 149 if (ShellGetCurrentDir(NULL) == NULL) {\r
150 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
151 ShellStatus = SHELL_NOT_FOUND;\r
152 } else {\r
153 ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
154 Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
1fc3749d 155 if (*Param1Copy == L'\\') {\r
ab94587a 156 while (PathRemoveLastItem(Drive)) ;\r
1fc3749d 157 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
a405b86d 158 } else {\r
1fc3749d 159 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
a405b86d 160 }\r
161 //\r
162 // Verify that this is a valid directory\r
163 //\r
164 Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
165 if (EFI_ERROR(Status)) {\r
166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
167 ShellStatus = SHELL_NOT_FOUND;\r
168 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
169 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
170 ShellStatus = SHELL_NOT_FOUND;\r
171 }\r
172 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
173 //\r
174 // change directory on current drive letter\r
175 //\r
176 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
177 if (Status == EFI_NOT_FOUND) {\r
178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
179 ShellStatus = SHELL_NOT_FOUND;\r
180 }\r
181 }\r
182 if (Handle != NULL) {\r
183 gEfiShellProtocol->CloseFile(Handle);\r
184 DEBUG_CODE(Handle = NULL;);\r
185 }\r
186 }\r
187 } else {\r
188 //\r
189 // change directory on other drive letter\r
190 //\r
1fc3749d 191 Drive = AllocateZeroPool(StrSize(Param1Copy));\r
9ea69f8a 192 if (Drive == NULL) {\r
193 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
194 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
195 } else {\r
1fc3749d 196 Drive = StrCpy(Drive, Param1Copy);\r
9ea69f8a 197 Path = StrStr(Drive, L":");\r
b54fd049 198 ASSERT(Path != NULL);\r
199 if (*(Path+1) == CHAR_NULL) {\r
9ea69f8a 200 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
201 ShellStatus = SHELL_NOT_FOUND;\r
202 } else {\r
b54fd049 203 *(Path+1) = CHAR_NULL;\r
204 if (Path == Drive + StrLen(Drive)) {\r
205 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
206 ShellStatus = SHELL_NOT_FOUND;\r
207 } else {\r
208 Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
210 }\r
9ea69f8a 211 }\r
9ea69f8a 212 if (Status == EFI_NOT_FOUND) {\r
213 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
214 Status = SHELL_NOT_FOUND;\r
215 } else if (EFI_ERROR(Status)) {\r
1fc3749d 216 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1Copy);\r
9ea69f8a 217 Status = SHELL_NOT_FOUND;\r
218 }\r
a405b86d 219 }\r
220 }\r
1fc3749d 221 FreePool(Param1Copy);\r
a405b86d 222 }\r
223 }\r
224\r
225 if (Drive != NULL) {\r
226 FreePool(Drive);\r
227 }\r
228 //\r
229 // free the command line package\r
230 //\r
231 ShellCommandLineFreeVarList (Package);\r
232\r
233 //\r
234 // return the status\r
235 //\r
236 return (ShellStatus);\r
237}\r
238\r