TianoCore EDK2 master
Loading...
Searching...
No Matches
GenericQemuLoadImageLib.c
Go to the documentation of this file.
1
9#include <Uefi.h>
10
11#include <Base.h>
13#include <Library/DebugLib.h>
16#include <Library/PrintLib.h>
19#include <Protocol/DevicePath.h>
22
23#pragma pack (1)
24typedef struct {
25 EFI_DEVICE_PATH_PROTOCOL FilePathHeader;
26 CHAR16 FilePath[ARRAY_SIZE (L"kernel")];
28
29typedef struct {
30 VENDOR_DEVICE_PATH VenMediaNode;
31 KERNEL_FILE_DEVPATH FileNode;
34
35typedef struct {
36 VENDOR_DEVICE_PATH VenMediaNode;
39#pragma pack ()
40
41STATIC CONST KERNEL_VENMEDIA_FILE_DEVPATH mKernelDevicePath = {
42 {
43 {
44 MEDIA_DEVICE_PATH, MEDIA_VENDOR_DP,
45 { sizeof (VENDOR_DEVICE_PATH) }
46 },
47 QEMU_KERNEL_LOADER_FS_MEDIA_GUID
48 }, {
49 {
50 MEDIA_DEVICE_PATH, MEDIA_FILEPATH_DP,
51 { sizeof (KERNEL_FILE_DEVPATH) }
52 },
53 L"kernel",
54 }, {
55 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
56 { sizeof (EFI_DEVICE_PATH_PROTOCOL) }
57 }
58};
59
60STATIC CONST SINGLE_VENMEDIA_NODE_DEVPATH mQemuKernelLoaderFsDevicePath = {
61 {
62 {
63 MEDIA_DEVICE_PATH, MEDIA_VENDOR_DP,
64 { sizeof (VENDOR_DEVICE_PATH) }
65 },
66 QEMU_KERNEL_LOADER_FS_MEDIA_GUID
67 }, {
68 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
69 { sizeof (EFI_DEVICE_PATH_PROTOCOL) }
70 }
71};
72
75GetQemuKernelLoaderBlobSize (
77 IN CHAR16 *FileName,
78 OUT UINTN *Size
79 )
80{
81 EFI_STATUS Status;
82 EFI_FILE_HANDLE FileHandle;
83 UINT64 FileSize;
84
85 Status = Root->Open (Root, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
86 if (EFI_ERROR (Status)) {
87 return Status;
88 }
89
90 Status = FileHandleGetSize (FileHandle, &FileSize);
91 if (EFI_ERROR (Status)) {
92 goto CloseFile;
93 }
94
95 if (FileSize > MAX_UINTN) {
96 Status = EFI_UNSUPPORTED;
97 goto CloseFile;
98 }
99
100 *Size = (UINTN)FileSize;
101 Status = EFI_SUCCESS;
103 FileHandle->Close (FileHandle);
104 return Status;
105}
106
107STATIC
109ReadWholeQemuKernelLoaderBlob (
110 IN EFI_FILE_HANDLE Root,
111 IN CHAR16 *FileName,
112 IN UINTN Size,
113 OUT VOID *Buffer
114 )
115{
116 EFI_STATUS Status;
117 EFI_FILE_HANDLE FileHandle;
118 UINTN ReadSize;
119
120 Status = Root->Open (Root, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
121 if (EFI_ERROR (Status)) {
122 return Status;
123 }
124
125 ReadSize = Size;
126 Status = FileHandle->Read (FileHandle, &ReadSize, Buffer);
127 if (EFI_ERROR (Status)) {
128 goto CloseFile;
129 }
130
131 if (ReadSize != Size) {
132 Status = EFI_PROTOCOL_ERROR;
133 goto CloseFile;
134 }
135
136 Status = EFI_SUCCESS;
138 FileHandle->Close (FileHandle);
139 return Status;
140}
141
162EFIAPI
164 OUT EFI_HANDLE *ImageHandle
165 )
166{
167 EFI_STATUS Status;
168 EFI_HANDLE KernelImageHandle;
169 EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage;
170 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
171 EFI_HANDLE FsVolumeHandle;
173 EFI_FILE_HANDLE Root;
174 UINTN CommandLineSize;
175 CHAR8 *CommandLine;
176 UINTN InitrdSize;
177
178 //
179 // Load the image. This should call back into the QEMU EFI loader file system.
180 //
181 Status = gBS->LoadImage (
182 FALSE, // BootPolicy: exact match required
183 gImageHandle, // ParentImageHandle
184 (EFI_DEVICE_PATH_PROTOCOL *)&mKernelDevicePath,
185 NULL, // SourceBuffer
186 0, // SourceSize
187 &KernelImageHandle
188 );
189 switch (Status) {
190 case EFI_SUCCESS:
191 break;
192
193 case EFI_SECURITY_VIOLATION:
194 //
195 // In this case, the image was loaded but failed to authenticate.
196 //
197 Status = EFI_ACCESS_DENIED;
198 goto UnloadImage;
199
200 default:
201 DEBUG ((
202 Status == EFI_NOT_FOUND ? DEBUG_INFO : DEBUG_ERROR,
203 "%a: LoadImage(): %r\n",
204 __func__,
205 Status
206 ));
207 return Status;
208 }
209
210 //
211 // Construct the kernel command line.
212 //
213 Status = gBS->OpenProtocol (
214 KernelImageHandle,
215 &gEfiLoadedImageProtocolGuid,
216 (VOID **)&KernelLoadedImage,
217 gImageHandle, // AgentHandle
218 NULL, // ControllerHandle
219 EFI_OPEN_PROTOCOL_GET_PROTOCOL
220 );
221 ASSERT_EFI_ERROR (Status);
222
223 //
224 // Open the Qemu Kernel Loader abstract filesystem (volume) which will be
225 // used to query the "initrd" and to read the "cmdline" synthetic files.
226 //
227 DevicePathNode = (EFI_DEVICE_PATH_PROTOCOL *)&mQemuKernelLoaderFsDevicePath;
228 Status = gBS->LocateDevicePath (
229 &gEfiSimpleFileSystemProtocolGuid,
230 &DevicePathNode,
231 &FsVolumeHandle
232 );
233 if (EFI_ERROR (Status)) {
234 goto UnloadImage;
235 }
236
237 Status = gBS->HandleProtocol (
238 FsVolumeHandle,
239 &gEfiSimpleFileSystemProtocolGuid,
240 (VOID **)&FsProtocol
241 );
242 if (EFI_ERROR (Status)) {
243 goto UnloadImage;
244 }
245
246 Status = FsProtocol->OpenVolume (FsVolumeHandle, &Root);
247 if (EFI_ERROR (Status)) {
248 goto UnloadImage;
249 }
250
251 Status = GetQemuKernelLoaderBlobSize (Root, L"cmdline", &CommandLineSize);
252 if (EFI_ERROR (Status)) {
253 goto CloseRoot;
254 }
255
256 if (CommandLineSize == 0) {
257 KernelLoadedImage->LoadOptionsSize = 0;
258 } else {
259 CommandLine = AllocatePool (CommandLineSize);
260 if (CommandLine == NULL) {
261 Status = EFI_OUT_OF_RESOURCES;
262 goto CloseRoot;
263 }
264
265 Status = ReadWholeQemuKernelLoaderBlob (
266 Root,
267 L"cmdline",
268 CommandLineSize,
269 CommandLine
270 );
271 if (EFI_ERROR (Status)) {
272 goto FreeCommandLine;
273 }
274
275 //
276 // Verify NUL-termination of the command line.
277 //
278 if (CommandLine[CommandLineSize - 1] != '\0') {
279 DEBUG ((
280 DEBUG_ERROR,
281 "%a: kernel command line is not NUL-terminated\n",
282 __func__
283 ));
284 Status = EFI_PROTOCOL_ERROR;
285 goto FreeCommandLine;
286 }
287
288 //
289 // Drop the terminating NUL, convert to UTF-16.
290 //
291 KernelLoadedImage->LoadOptionsSize = (UINT32)((CommandLineSize - 1) * 2);
292 }
293
294 Status = GetQemuKernelLoaderBlobSize (Root, L"initrd", &InitrdSize);
295 if (EFI_ERROR (Status)) {
296 goto FreeCommandLine;
297 }
298
299 if (InitrdSize > 0) {
300 //
301 // Append ' initrd=initrd' in UTF-16.
302 //
303 KernelLoadedImage->LoadOptionsSize += sizeof (L" initrd=initrd") - 2;
304 }
305
306 if (KernelLoadedImage->LoadOptionsSize == 0) {
307 KernelLoadedImage->LoadOptions = NULL;
308 } else {
309 //
310 // NUL-terminate in UTF-16.
311 //
312 KernelLoadedImage->LoadOptionsSize += 2;
313
314 KernelLoadedImage->LoadOptions = AllocatePool (
315 KernelLoadedImage->LoadOptionsSize
316 );
317 if (KernelLoadedImage->LoadOptions == NULL) {
318 KernelLoadedImage->LoadOptionsSize = 0;
319 Status = EFI_OUT_OF_RESOURCES;
320 goto FreeCommandLine;
321 }
322
324 KernelLoadedImage->LoadOptions,
325 KernelLoadedImage->LoadOptionsSize,
326 "%a%a",
327 (CommandLineSize == 0) ? "" : CommandLine,
328 (InitrdSize == 0) ? "" : " initrd=initrd"
329 );
330 DEBUG ((
331 DEBUG_INFO,
332 "%a: command line: \"%s\"\n",
333 __func__,
334 (CHAR16 *)KernelLoadedImage->LoadOptions
335 ));
336 }
337
338 *ImageHandle = KernelImageHandle;
339 Status = EFI_SUCCESS;
340
341FreeCommandLine:
342 if (CommandLineSize > 0) {
343 FreePool (CommandLine);
344 }
345
346CloseRoot:
347 Root->Close (Root);
348UnloadImage:
349 if (EFI_ERROR (Status)) {
350 gBS->UnloadImage (KernelImageHandle);
351 }
352
353 return Status;
354}
355
372EFIAPI
374 IN OUT EFI_HANDLE *ImageHandle
375 )
376{
377 return gBS->StartImage (
378 *ImageHandle,
379 NULL, // ExitDataSize
380 NULL // ExitData
381 );
382}
383
398EFIAPI
400 IN EFI_HANDLE ImageHandle
401 )
402{
403 EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage;
404 EFI_STATUS Status;
405
406 Status = gBS->OpenProtocol (
407 ImageHandle,
408 &gEfiLoadedImageProtocolGuid,
409 (VOID **)&KernelLoadedImage,
410 gImageHandle, // AgentHandle
411 NULL, // ControllerHandle
412 EFI_OPEN_PROTOCOL_GET_PROTOCOL
413 );
414 if (EFI_ERROR (Status)) {
415 return EFI_INVALID_PARAMETER;
416 }
417
418 if (KernelLoadedImage->LoadOptions != NULL) {
419 FreePool (KernelLoadedImage->LoadOptions);
420 KernelLoadedImage->LoadOptions = NULL;
421 }
422
423 KernelLoadedImage->LoadOptionsSize = 0;
424
425 return gBS->UnloadImage (ImageHandle);
426}
UINT64 UINTN
#define MEDIA_FILEPATH_DP
Definition: DevicePath.h:1098
#define MEDIA_VENDOR_DP
Media vendor device path subtype.
Definition: DevicePath.h:1093
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI FileHandleGetSize(IN EFI_FILE_HANDLE FileHandle, OUT UINT64 *Size)
EFI_STATUS EFIAPI QemuLoadKernelImage(OUT EFI_HANDLE *ImageHandle)
EFI_STATUS EFIAPI QemuStartKernelImage(IN OUT EFI_HANDLE *ImageHandle)
EFI_STATUS EFIAPI QemuUnloadKernelImage(IN EFI_HANDLE ImageHandle)
UINTN EFIAPI UnicodeSPrintAsciiFormat(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:583
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
VOID CloseFile(IN EFI_FILE_HANDLE FileHandle)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
VOID * LoadOptions
A pointer to the image's binary load options.
Definition: LoadedImage.h:62
UINT32 LoadOptionsSize
The size in bytes of LoadOptions.
Definition: LoadedImage.h:61