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