]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
ShellPkg/Ls: Consider UEFI timezone may not be set
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Load.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 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "UefiShellLevel2CommandsLib.h"
11
12 // This function was from from the BdsLib implementation in
13 // IntelFrameworkModulePkg\Library\GenericBdsLib\BdsConnect.c
14 // function name: BdsLibConnectAllEfi
15 /**
16 This function will connect all current system handles recursively. The
17 connection will finish until every handle's child handle created if it have.
18
19 @retval EFI_SUCCESS All handles and it's child handle have been
20 connected
21 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
22
23 **/
24 EFI_STATUS
25 ConnectAllEfi (
26 VOID
27 )
28 {
29 EFI_STATUS Status;
30 UINTN HandleCount;
31 EFI_HANDLE *HandleBuffer;
32 UINTN Index;
33
34 Status = gBS->LocateHandleBuffer (
35 AllHandles,
36 NULL,
37 NULL,
38 &HandleCount,
39 &HandleBuffer
40 );
41 if (EFI_ERROR (Status)) {
42 return Status;
43 }
44
45 for (Index = 0; Index < HandleCount; Index++) {
46 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
47 }
48
49 if (HandleBuffer != NULL) {
50 FreePool (HandleBuffer);
51 }
52
53 return EFI_SUCCESS;
54 }
55
56 /**
57 function to load a .EFI driver into memory and possible connect the driver.
58
59 if FileName is NULL then ASSERT.
60
61 @param[in] FileName FileName of the driver to load
62 @param[in] Connect Whether to connect or not
63
64 @retval EFI_SUCCESS the driver was loaded and if Connect was
65 true then connect was attempted. Connection may
66 have failed.
67 @retval EFI_OUT_OF_RESOURCES there was insufficient memory
68 **/
69 EFI_STATUS
70 LoadDriver(
71 IN CONST CHAR16 *FileName,
72 IN CONST BOOLEAN Connect
73 )
74 {
75 EFI_HANDLE LoadedDriverHandle;
76 EFI_STATUS Status;
77 EFI_DEVICE_PATH_PROTOCOL *FilePath;
78 EFI_LOADED_IMAGE_PROTOCOL *LoadedDriverImage;
79
80 LoadedDriverImage = NULL;
81 FilePath = NULL;
82 LoadedDriverHandle = NULL;
83 Status = EFI_SUCCESS;
84
85 ASSERT (FileName != NULL);
86
87 //
88 // Fix local copies of the protocol pointers
89 //
90 Status = CommandInit();
91 ASSERT_EFI_ERROR(Status);
92
93 //
94 // Convert to DEVICE_PATH
95 //
96 FilePath = gEfiShellProtocol->GetDevicePathFromFilePath(FileName);
97
98 if (FilePath == NULL) {
99 ASSERT(FALSE);
100 return (EFI_INVALID_PARAMETER);
101 }
102
103 //
104 // Use LoadImage to get it into memory
105 //
106 Status = gBS->LoadImage(
107 FALSE,
108 gImageHandle,
109 FilePath,
110 NULL,
111 0,
112 &LoadedDriverHandle);
113
114 if (EFI_ERROR(Status)) {
115 //
116 // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
117 // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
118 // If the caller doesn't have the option to defer the execution of an image, we should
119 // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
120 //
121 if (Status == EFI_SECURITY_VIOLATION) {
122 gBS->UnloadImage (LoadedDriverHandle);
123 }
124 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_IMAGE), gShellLevel2HiiHandle, FileName, Status);
125 } else {
126 //
127 // Make sure it is a driver image
128 //
129 Status = gBS->HandleProtocol (LoadedDriverHandle, &gEfiLoadedImageProtocolGuid, (VOID *) &LoadedDriverImage);
130
131 ASSERT (LoadedDriverImage != NULL);
132
133 if ( EFI_ERROR(Status)
134 || ( LoadedDriverImage->ImageCodeType != EfiBootServicesCode
135 && LoadedDriverImage->ImageCodeType != EfiRuntimeServicesCode)
136 ){
137 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_DRIVER), gShellLevel2HiiHandle, FileName);
138
139 //
140 // Exit and unload the non-driver image
141 //
142 gBS->Exit(LoadedDriverHandle, EFI_INVALID_PARAMETER, 0, NULL);
143 Status = EFI_INVALID_PARAMETER;
144 }
145 }
146
147 if (!EFI_ERROR(Status)) {
148 //
149 // Start the image
150 //
151 Status = gBS->StartImage(LoadedDriverHandle, NULL, NULL);
152 if (EFI_ERROR(Status)) {
153 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_ERROR), gShellLevel2HiiHandle, FileName, Status);
154 } else {
155 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_LOADED), gShellLevel2HiiHandle, FileName, LoadedDriverImage->ImageBase, Status);
156 }
157 }
158
159 if (!EFI_ERROR(Status) && Connect) {
160 //
161 // Connect it...
162 //
163 Status = ConnectAllEfi();
164 }
165
166 //
167 // clean up memory...
168 //
169 if (FilePath != NULL) {
170 FreePool(FilePath);
171 }
172
173 return (Status);
174 }
175
176 STATIC CONST SHELL_PARAM_ITEM LoadParamList[] = {
177 {L"-nc", TypeFlag},
178 {NULL, TypeMax}
179 };
180
181 /**
182 Function for 'load' command.
183
184 @param[in] ImageHandle Handle to the Image (NULL if Internal).
185 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
186 **/
187 SHELL_STATUS
188 EFIAPI
189 ShellCommandRunLoad (
190 IN EFI_HANDLE ImageHandle,
191 IN EFI_SYSTEM_TABLE *SystemTable
192 )
193 {
194 EFI_STATUS Status;
195 LIST_ENTRY *Package;
196 CHAR16 *ProblemParam;
197 SHELL_STATUS ShellStatus;
198 UINTN ParamCount;
199 EFI_SHELL_FILE_INFO *ListHead;
200 EFI_SHELL_FILE_INFO *Node;
201
202 ListHead = NULL;
203 ProblemParam = NULL;
204 ShellStatus = SHELL_SUCCESS;
205
206 //
207 // initialize the shell lib (we must be in non-auto-init...)
208 //
209 Status = ShellInitialize();
210 ASSERT_EFI_ERROR(Status);
211
212 //
213 // parse the command line
214 //
215 Status = ShellCommandLineParse (LoadParamList, &Package, &ProblemParam, TRUE);
216 if (EFI_ERROR(Status)) {
217 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
218 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"load", ProblemParam);
219 FreePool(ProblemParam);
220 ShellStatus = SHELL_INVALID_PARAMETER;
221 } else {
222 ASSERT(FALSE);
223 }
224 } else {
225 //
226 // check for "-?"
227 //
228 if (ShellCommandLineGetFlag(Package, L"-?")) {
229 ASSERT(FALSE);
230 } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
231 //
232 // we didnt get a single file to load parameter
233 //
234 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"load");
235 ShellStatus = SHELL_INVALID_PARAMETER;
236 } else {
237 for ( ParamCount = 1
238 ; ShellCommandLineGetRawValue(Package, ParamCount) != NULL
239 ; ParamCount++
240 ){
241 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount), EFI_FILE_MODE_READ, &ListHead);
242 if (!EFI_ERROR(Status)) {
243 for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&ListHead->Link)
244 ; !IsNull(&ListHead->Link, &Node->Link)
245 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&ListHead->Link, &Node->Link)
246 ){
247 //
248 // once we have an error preserve that value, but finish the loop.
249 //
250 if (EFI_ERROR(Status)) {
251 LoadDriver(Node->FullName, (BOOLEAN)(ShellCommandLineGetFlag(Package, L"-nc")==FALSE));
252 } else {
253 Status = LoadDriver(Node->FullName, (BOOLEAN)(ShellCommandLineGetFlag(Package, L"-nc")==FALSE));
254 }
255 } // for loop for multi-open
256 if (EFI_ERROR(Status)) {
257 ShellCloseFileMetaArg(&ListHead);
258 } else {
259 Status = ShellCloseFileMetaArg(&ListHead);;
260 }
261 } else {
262 //
263 // no files found.
264 //
265 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"load", (CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount));
266 ShellStatus = SHELL_NOT_FOUND;
267 }
268 } // for loop for params
269 }
270
271 //
272 // free the command line package
273 //
274 ShellCommandLineFreeVarList (Package);
275 }
276
277 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {
278 ShellStatus = SHELL_DEVICE_ERROR;
279 }
280
281 return (ShellStatus);
282 }