VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c@ 80924

Last change on this file since 80924 was 80721, checked in by vboxsync, 6 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
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 - 2018, 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**/
24EFI_STATUS
25ConnectAllEfi (
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**/
69EFI_STATUS
70LoadDriver(
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 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_IMAGE), gShellLevel2HiiHandle, FileName, Status);
116 } else {
117 //
118 // Make sure it is a driver image
119 //
120 Status = gBS->HandleProtocol (LoadedDriverHandle, &gEfiLoadedImageProtocolGuid, (VOID *) &LoadedDriverImage);
121
122 ASSERT (LoadedDriverImage != NULL);
123
124 if ( EFI_ERROR(Status)
125 || ( LoadedDriverImage->ImageCodeType != EfiBootServicesCode
126 && LoadedDriverImage->ImageCodeType != EfiRuntimeServicesCode)
127 ){
128 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_DRIVER), gShellLevel2HiiHandle, FileName);
129
130 //
131 // Exit and unload the non-driver image
132 //
133 gBS->Exit(LoadedDriverHandle, EFI_INVALID_PARAMETER, 0, NULL);
134 Status = EFI_INVALID_PARAMETER;
135 }
136 }
137
138 if (!EFI_ERROR(Status)) {
139 //
140 // Start the image
141 //
142 Status = gBS->StartImage(LoadedDriverHandle, NULL, NULL);
143 if (EFI_ERROR(Status)) {
144 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_ERROR), gShellLevel2HiiHandle, FileName, Status);
145 } else {
146 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_LOADED), gShellLevel2HiiHandle, FileName, LoadedDriverImage->ImageBase, Status);
147 }
148 }
149
150 if (!EFI_ERROR(Status) && Connect) {
151 //
152 // Connect it...
153 //
154 Status = ConnectAllEfi();
155 }
156
157 //
158 // clean up memory...
159 //
160 if (FilePath != NULL) {
161 FreePool(FilePath);
162 }
163
164 return (Status);
165}
166
167STATIC CONST SHELL_PARAM_ITEM LoadParamList[] = {
168 {L"-nc", TypeFlag},
169 {NULL, TypeMax}
170 };
171
172/**
173 Function for 'load' command.
174
175 @param[in] ImageHandle Handle to the Image (NULL if Internal).
176 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
177**/
178SHELL_STATUS
179EFIAPI
180ShellCommandRunLoad (
181 IN EFI_HANDLE ImageHandle,
182 IN EFI_SYSTEM_TABLE *SystemTable
183 )
184{
185 EFI_STATUS Status;
186 LIST_ENTRY *Package;
187 CHAR16 *ProblemParam;
188 SHELL_STATUS ShellStatus;
189 UINTN ParamCount;
190 EFI_SHELL_FILE_INFO *ListHead;
191 EFI_SHELL_FILE_INFO *Node;
192
193 ListHead = NULL;
194 ProblemParam = NULL;
195 ShellStatus = SHELL_SUCCESS;
196
197 //
198 // initialize the shell lib (we must be in non-auto-init...)
199 //
200 Status = ShellInitialize();
201 ASSERT_EFI_ERROR(Status);
202
203 //
204 // parse the command line
205 //
206 Status = ShellCommandLineParse (LoadParamList, &Package, &ProblemParam, TRUE);
207 if (EFI_ERROR(Status)) {
208 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"load", ProblemParam);
210 FreePool(ProblemParam);
211 ShellStatus = SHELL_INVALID_PARAMETER;
212 } else {
213 ASSERT(FALSE);
214 }
215 } else {
216 //
217 // check for "-?"
218 //
219 if (ShellCommandLineGetFlag(Package, L"-?")) {
220 ASSERT(FALSE);
221 } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
222 //
223 // we didnt get a single file to load parameter
224 //
225 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"load");
226 ShellStatus = SHELL_INVALID_PARAMETER;
227 } else {
228 for ( ParamCount = 1
229 ; ShellCommandLineGetRawValue(Package, ParamCount) != NULL
230 ; ParamCount++
231 ){
232 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount), EFI_FILE_MODE_READ, &ListHead);
233 if (!EFI_ERROR(Status)) {
234 for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&ListHead->Link)
235 ; !IsNull(&ListHead->Link, &Node->Link)
236 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&ListHead->Link, &Node->Link)
237 ){
238 //
239 // once we have an error preserve that value, but finish the loop.
240 //
241 if (EFI_ERROR(Status)) {
242 LoadDriver(Node->FullName, (BOOLEAN)(ShellCommandLineGetFlag(Package, L"-nc")==FALSE));
243 } else {
244 Status = LoadDriver(Node->FullName, (BOOLEAN)(ShellCommandLineGetFlag(Package, L"-nc")==FALSE));
245 }
246 } // for loop for multi-open
247 if (EFI_ERROR(Status)) {
248 ShellCloseFileMetaArg(&ListHead);
249 } else {
250 Status = ShellCloseFileMetaArg(&ListHead);;
251 }
252 } else {
253 //
254 // no files found.
255 //
256 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"load", (CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount));
257 ShellStatus = SHELL_NOT_FOUND;
258 }
259 } // for loop for params
260 }
261
262 //
263 // free the command line package
264 //
265 ShellCommandLineFreeVarList (Package);
266 }
267
268 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {
269 ShellStatus = SHELL_DEVICE_ERROR;
270 }
271
272 return (ShellStatus);
273}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette