2 X86 specific implementation of QemuLoadImageLib library class interface
3 with support for loading mixed mode images and non-EFI stub images
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2020, ARM Ltd. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Guid/QemuKernelLoaderFsMedia.h>
14 #include <Library/DebugLib.h>
15 #include <Library/LoadLinuxLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/PrintLib.h>
18 #include <Library/QemuFwCfgLib.h>
19 #include <Library/QemuLoadImageLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Protocol/DevicePath.h>
22 #include <Protocol/LoadedImage.h>
23 #include <Protocol/OvmfLoadedX86LinuxKernel.h>
27 EFI_DEVICE_PATH_PROTOCOL FilePathHeader
;
28 CHAR16 FilePath
[ARRAY_SIZE (L
"kernel")];
29 } KERNEL_FILE_DEVPATH
;
32 VENDOR_DEVICE_PATH VenMediaNode
;
33 KERNEL_FILE_DEVPATH FileNode
;
34 EFI_DEVICE_PATH_PROTOCOL EndNode
;
35 } KERNEL_VENMEDIA_FILE_DEVPATH
;
38 STATIC CONST KERNEL_VENMEDIA_FILE_DEVPATH mKernelDevicePath
= {
41 MEDIA_DEVICE_PATH
, MEDIA_VENDOR_DP
,
42 { sizeof (VENDOR_DEVICE_PATH
) }
44 QEMU_KERNEL_LOADER_FS_MEDIA_GUID
47 MEDIA_DEVICE_PATH
, MEDIA_FILEPATH_DP
,
48 { sizeof (KERNEL_FILE_DEVPATH
) }
52 END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
,
53 { sizeof (EFI_DEVICE_PATH_PROTOCOL
) }
60 IN OVMF_LOADED_X86_LINUX_KERNEL
*LoadedImage
63 if (LoadedImage
->SetupBuf
!= NULL
) {
64 FreePages (LoadedImage
->SetupBuf
,
65 EFI_SIZE_TO_PAGES (LoadedImage
->SetupSize
));
67 if (LoadedImage
->KernelBuf
!= NULL
) {
68 FreePages (LoadedImage
->KernelBuf
,
69 EFI_SIZE_TO_PAGES (LoadedImage
->KernelInitialSize
));
71 if (LoadedImage
->CommandLine
!= NULL
) {
72 FreePages (LoadedImage
->CommandLine
,
73 EFI_SIZE_TO_PAGES (LoadedImage
->CommandLineSize
));
75 if (LoadedImage
->InitrdData
!= NULL
) {
76 FreePages (LoadedImage
->InitrdData
,
77 EFI_SIZE_TO_PAGES (LoadedImage
->InitrdSize
));
84 OUT EFI_HANDLE
*ImageHandle
90 OVMF_LOADED_X86_LINUX_KERNEL
*LoadedImage
;
92 QemuFwCfgSelectItem (QemuFwCfgItemKernelSize
);
93 KernelSize
= (UINTN
)QemuFwCfgRead32 ();
95 QemuFwCfgSelectItem (QemuFwCfgItemKernelSetupSize
);
96 SetupSize
= (UINTN
)QemuFwCfgRead32 ();
98 if (KernelSize
== 0 || SetupSize
== 0) {
99 DEBUG ((DEBUG_INFO
, "qemu -kernel was not used.\n"));
100 return EFI_NOT_FOUND
;
103 LoadedImage
= AllocateZeroPool (sizeof (*LoadedImage
));
104 if (LoadedImage
== NULL
) {
105 return EFI_OUT_OF_RESOURCES
;
108 LoadedImage
->SetupSize
= SetupSize
;
109 LoadedImage
->SetupBuf
= LoadLinuxAllocateKernelSetupPages (
110 EFI_SIZE_TO_PAGES (LoadedImage
->SetupSize
));
111 if (LoadedImage
->SetupBuf
== NULL
) {
112 DEBUG ((DEBUG_ERROR
, "Unable to allocate memory for kernel setup!\n"));
113 Status
= EFI_OUT_OF_RESOURCES
;
117 DEBUG ((DEBUG_INFO
, "Setup size: 0x%x\n", (UINT32
)LoadedImage
->SetupSize
));
118 DEBUG ((DEBUG_INFO
, "Reading kernel setup image ..."));
119 QemuFwCfgSelectItem (QemuFwCfgItemKernelSetupData
);
120 QemuFwCfgReadBytes (LoadedImage
->SetupSize
, LoadedImage
->SetupBuf
);
121 DEBUG ((DEBUG_INFO
, " [done]\n"));
123 Status
= LoadLinuxCheckKernelSetup (LoadedImage
->SetupBuf
,
124 LoadedImage
->SetupSize
);
125 if (EFI_ERROR (Status
)) {
129 Status
= LoadLinuxInitializeKernelSetup (LoadedImage
->SetupBuf
);
130 if (EFI_ERROR (Status
)) {
134 LoadedImage
->KernelInitialSize
= LoadLinuxGetKernelSize (
135 LoadedImage
->SetupBuf
, KernelSize
);
136 if (LoadedImage
->KernelInitialSize
== 0) {
137 Status
= EFI_UNSUPPORTED
;
141 LoadedImage
->KernelBuf
= LoadLinuxAllocateKernelPages (
142 LoadedImage
->SetupBuf
,
143 EFI_SIZE_TO_PAGES (LoadedImage
->KernelInitialSize
)
145 if (LoadedImage
->KernelBuf
== NULL
) {
146 DEBUG ((DEBUG_ERROR
, "Unable to allocate memory for kernel!\n"));
147 Status
= EFI_OUT_OF_RESOURCES
;
151 DEBUG ((DEBUG_INFO
, "Kernel size: 0x%x\n", (UINT32
)KernelSize
));
152 DEBUG ((DEBUG_INFO
, "Reading kernel image ..."));
153 QemuFwCfgSelectItem (QemuFwCfgItemKernelData
);
154 QemuFwCfgReadBytes (KernelSize
, LoadedImage
->KernelBuf
);
155 DEBUG ((DEBUG_INFO
, " [done]\n"));
157 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineSize
);
158 LoadedImage
->CommandLineSize
= (UINTN
)QemuFwCfgRead32 ();
160 if (LoadedImage
->CommandLineSize
> 0) {
161 LoadedImage
->CommandLine
= LoadLinuxAllocateCommandLinePages (
163 LoadedImage
->CommandLineSize
));
164 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData
);
165 QemuFwCfgReadBytes (LoadedImage
->CommandLineSize
, LoadedImage
->CommandLine
);
168 Status
= LoadLinuxSetCommandLine (LoadedImage
->SetupBuf
,
169 LoadedImage
->CommandLine
);
170 if (EFI_ERROR (Status
)) {
174 QemuFwCfgSelectItem (QemuFwCfgItemInitrdSize
);
175 LoadedImage
->InitrdSize
= (UINTN
)QemuFwCfgRead32 ();
177 if (LoadedImage
->InitrdSize
> 0) {
178 LoadedImage
->InitrdData
= LoadLinuxAllocateInitrdPages (
179 LoadedImage
->SetupBuf
,
180 EFI_SIZE_TO_PAGES (LoadedImage
->InitrdSize
));
181 DEBUG ((DEBUG_INFO
, "Initrd size: 0x%x\n",
182 (UINT32
)LoadedImage
->InitrdSize
));
183 DEBUG ((DEBUG_INFO
, "Reading initrd image ..."));
184 QemuFwCfgSelectItem (QemuFwCfgItemInitrdData
);
185 QemuFwCfgReadBytes (LoadedImage
->InitrdSize
, LoadedImage
->InitrdData
);
186 DEBUG ((DEBUG_INFO
, " [done]\n"));
189 Status
= LoadLinuxSetInitrd (LoadedImage
->SetupBuf
, LoadedImage
->InitrdData
,
190 LoadedImage
->InitrdSize
);
191 if (EFI_ERROR (Status
)) {
196 Status
= gBS
->InstallProtocolInterface (ImageHandle
,
197 &gOvmfLoadedX86LinuxKernelProtocolGuid
, EFI_NATIVE_INTERFACE
,
199 if (EFI_ERROR (Status
)) {
205 FreeLegacyImage (LoadedImage
);
207 FreePool (LoadedImage
);
213 QemuStartLegacyImage (
214 IN EFI_HANDLE ImageHandle
218 OVMF_LOADED_X86_LINUX_KERNEL
*LoadedImage
;
220 Status
= gBS
->OpenProtocol (
222 &gOvmfLoadedX86LinuxKernelProtocolGuid
,
223 (VOID
**)&LoadedImage
,
224 gImageHandle
, // AgentHandle
225 NULL
, // ControllerHandle
226 EFI_OPEN_PROTOCOL_GET_PROTOCOL
228 if (EFI_ERROR (Status
)) {
229 return EFI_INVALID_PARAMETER
;
232 return LoadLinux (LoadedImage
->KernelBuf
, LoadedImage
->SetupBuf
);
237 QemuUnloadLegacyImage (
238 IN EFI_HANDLE ImageHandle
242 OVMF_LOADED_X86_LINUX_KERNEL
*LoadedImage
;
244 Status
= gBS
->OpenProtocol (
246 &gOvmfLoadedX86LinuxKernelProtocolGuid
,
247 (VOID
**)&LoadedImage
,
248 gImageHandle
, // AgentHandle
249 NULL
, // ControllerHandle
250 EFI_OPEN_PROTOCOL_GET_PROTOCOL
252 if (EFI_ERROR (Status
)) {
253 return EFI_INVALID_PARAMETER
;
256 Status
= gBS
->UninstallProtocolInterface (ImageHandle
,
257 &gOvmfLoadedX86LinuxKernelProtocolGuid
, LoadedImage
);
258 ASSERT_EFI_ERROR (Status
);
260 FreeLegacyImage (LoadedImage
);
261 FreePool (LoadedImage
);
266 Download the kernel, the initial ramdisk, and the kernel command line from
267 QEMU's fw_cfg. The kernel will be instructed via its command line to load
268 the initrd from the same Simple FileSystem where the kernel was loaded from.
270 @param[out] ImageHandle The image handle that was allocated for
273 @retval EFI_SUCCESS The image was loaded successfully.
274 @retval EFI_NOT_FOUND Kernel image was not found.
275 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
276 @retval EFI_PROTOCOL_ERROR Unterminated kernel command line.
278 @return Error codes from any of the underlying
283 QemuLoadKernelImage (
284 OUT EFI_HANDLE
*ImageHandle
288 EFI_HANDLE KernelImageHandle
;
289 EFI_LOADED_IMAGE_PROTOCOL
*KernelLoadedImage
;
290 UINTN CommandLineSize
;
295 // Redundant assignment to work around GCC48/GCC49 limitations.
300 // Load the image. This should call back into the QEMU EFI loader file system.
302 Status
= gBS
->LoadImage (
303 FALSE
, // BootPolicy: exact match required
304 gImageHandle
, // ParentImageHandle
305 (EFI_DEVICE_PATH_PROTOCOL
*)&mKernelDevicePath
,
306 NULL
, // SourceBuffer
316 // The image does not exist - no -kernel image was supplied via the
317 // command line so no point in invoking the legacy fallback
319 return EFI_NOT_FOUND
;
321 case EFI_SECURITY_VIOLATION
:
323 // We are running with UEFI secure boot enabled, and the image failed to
324 // authenticate. For compatibility reasons, we fall back to the legacy
325 // loader in this case. Since the image has been loaded, we need to unload
326 // it before proceeding
328 gBS
->UnloadImage (KernelImageHandle
);
332 case EFI_UNSUPPORTED
:
334 // The image is not natively supported or cross-type supported. Let's try
335 // loading it using the loader that parses the bzImage metadata directly.
337 Status
= QemuLoadLegacyImage (&KernelImageHandle
);
338 if (EFI_ERROR (Status
)) {
339 DEBUG ((DEBUG_ERROR
, "%a: QemuLoadLegacyImage(): %r\n", __FUNCTION__
,
343 *ImageHandle
= KernelImageHandle
;
347 DEBUG ((DEBUG_ERROR
, "%a: LoadImage(): %r\n", __FUNCTION__
, Status
));
352 // Construct the kernel command line.
354 Status
= gBS
->OpenProtocol (
356 &gEfiLoadedImageProtocolGuid
,
357 (VOID
**)&KernelLoadedImage
,
358 gImageHandle
, // AgentHandle
359 NULL
, // ControllerHandle
360 EFI_OPEN_PROTOCOL_GET_PROTOCOL
362 ASSERT_EFI_ERROR (Status
);
364 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineSize
);
365 CommandLineSize
= (UINTN
)QemuFwCfgRead32 ();
367 if (CommandLineSize
== 0) {
368 KernelLoadedImage
->LoadOptionsSize
= 0;
370 CommandLine
= AllocatePool (CommandLineSize
);
371 if (CommandLine
== NULL
) {
372 Status
= EFI_OUT_OF_RESOURCES
;
376 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData
);
377 QemuFwCfgReadBytes (CommandLineSize
, CommandLine
);
380 // Verify NUL-termination of the command line.
382 if (CommandLine
[CommandLineSize
- 1] != '\0') {
383 DEBUG ((DEBUG_ERROR
, "%a: kernel command line is not NUL-terminated\n",
385 Status
= EFI_PROTOCOL_ERROR
;
386 goto FreeCommandLine
;
390 // Drop the terminating NUL, convert to UTF-16.
392 KernelLoadedImage
->LoadOptionsSize
= (UINT32
) ((CommandLineSize
- 1) * 2);
395 QemuFwCfgSelectItem (QemuFwCfgItemInitrdSize
);
396 InitrdSize
= (UINTN
)QemuFwCfgRead32 ();
398 if (InitrdSize
> 0) {
400 // Append ' initrd=initrd' in UTF-16.
402 KernelLoadedImage
->LoadOptionsSize
+= sizeof (L
" initrd=initrd") - 2;
405 if (KernelLoadedImage
->LoadOptionsSize
== 0) {
406 KernelLoadedImage
->LoadOptions
= NULL
;
409 // NUL-terminate in UTF-16.
411 KernelLoadedImage
->LoadOptionsSize
+= 2;
413 KernelLoadedImage
->LoadOptions
= AllocatePool (
414 KernelLoadedImage
->LoadOptionsSize
);
415 if (KernelLoadedImage
->LoadOptions
== NULL
) {
416 KernelLoadedImage
->LoadOptionsSize
= 0;
417 Status
= EFI_OUT_OF_RESOURCES
;
418 goto FreeCommandLine
;
421 UnicodeSPrintAsciiFormat (
422 KernelLoadedImage
->LoadOptions
,
423 KernelLoadedImage
->LoadOptionsSize
,
425 (CommandLineSize
== 0) ? "" : CommandLine
,
426 (InitrdSize
== 0) ? "" : " initrd=initrd"
428 DEBUG ((DEBUG_INFO
, "%a: command line: \"%s\"\n", __FUNCTION__
,
429 (CHAR16
*)KernelLoadedImage
->LoadOptions
));
432 *ImageHandle
= KernelImageHandle
;
436 if (CommandLineSize
> 0) {
437 FreePool (CommandLine
);
440 gBS
->UnloadImage (KernelImageHandle
);
446 Transfer control to a kernel image loaded with QemuLoadKernelImage ()
448 @param[in,out] ImageHandle Handle of image to be started. May assume a
449 different value on return if the image was
452 @retval EFI_INVALID_PARAMETER ImageHandle is either an invalid image handle
453 or the image has already been initialized with
455 @retval EFI_SECURITY_VIOLATION The current platform policy specifies that the
456 image should not be started.
458 @return Error codes returned by the started image
462 QemuStartKernelImage (
463 IN OUT EFI_HANDLE
*ImageHandle
467 OVMF_LOADED_X86_LINUX_KERNEL
*LoadedImage
;
469 Status
= gBS
->OpenProtocol (
471 &gOvmfLoadedX86LinuxKernelProtocolGuid
,
472 (VOID
**)&LoadedImage
,
473 gImageHandle
, // AgentHandle
474 NULL
, // ControllerHandle
475 EFI_OPEN_PROTOCOL_GET_PROTOCOL
477 if (!EFI_ERROR (Status
)) {
478 return QemuStartLegacyImage (*ImageHandle
);
481 Status
= gBS
->StartImage (
483 NULL
, // ExitDataSize
487 if (Status
== EFI_UNSUPPORTED
) {
488 EFI_HANDLE KernelImageHandle
;
491 // On IA32, EFI_UNSUPPORTED means that the image's machine type is X64 while
492 // we are expecting a IA32 one, and the StartImage () boot service is unable
493 // to handle it, either because the image does not have the special .compat
494 // PE/COFF section that Linux specifies for mixed mode capable images, or
495 // because we are running without the support code for that. So load the
496 // image again, using the legacy loader, and unload the normally loaded
497 // image before starting the legacy one.
499 Status
= QemuLoadLegacyImage (&KernelImageHandle
);
500 if (EFI_ERROR (Status
)) {
502 // Note: no change to (*ImageHandle), the caller will release it.
507 // Swap in the legacy-loaded image.
509 QemuUnloadKernelImage (*ImageHandle
);
510 *ImageHandle
= KernelImageHandle
;
511 return QemuStartLegacyImage (KernelImageHandle
);
518 Unloads an image loaded with QemuLoadKernelImage ().
520 @param ImageHandle Handle that identifies the image to be
523 @retval EFI_SUCCESS The image has been unloaded.
524 @retval EFI_UNSUPPORTED The image has been started, and does not
526 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
528 @return Exit code from the image's unload function.
532 QemuUnloadKernelImage (
533 IN EFI_HANDLE ImageHandle
536 EFI_LOADED_IMAGE_PROTOCOL
*KernelLoadedImage
;
539 Status
= gBS
->OpenProtocol (
541 &gEfiLoadedImageProtocolGuid
,
542 (VOID
**)&KernelLoadedImage
,
543 gImageHandle
, // AgentHandle
544 NULL
, // ControllerHandle
545 EFI_OPEN_PROTOCOL_GET_PROTOCOL
547 if (Status
== EFI_UNSUPPORTED
) {
549 // The handle exists but does not have an instance of the standard loaded
550 // image protocol installed on it. Attempt to unload it as a legacy image
553 return QemuUnloadLegacyImage (ImageHandle
);
556 if (EFI_ERROR (Status
)) {
557 return EFI_INVALID_PARAMETER
;
561 // We are unloading a normal, non-legacy loaded image, either on behalf of
562 // an external caller, or called from QemuStartKernelImage() on IA32, while
563 // switching from the normal to the legacy method to load and start a X64
566 if (KernelLoadedImage
->LoadOptions
!= NULL
) {
567 FreePool (KernelLoadedImage
->LoadOptions
);
568 KernelLoadedImage
->LoadOptions
= NULL
;
570 KernelLoadedImage
->LoadOptionsSize
= 0;
572 return gBS
->UnloadImage (ImageHandle
);