3 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/LoadLinuxLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/QemuFwCfgLib.h>
15 #include <Library/ReportStatusCodeLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiLib.h>
21 TryRunningQemuKernel (
27 UINTN KernelInitialSize
;
31 UINTN CommandLineSize
;
39 KernelInitialSize
= 0;
45 if (!QemuFwCfgIsAvailable ()) {
49 QemuFwCfgSelectItem (QemuFwCfgItemKernelSize
);
50 KernelSize
= (UINTN
) QemuFwCfgRead64 ();
52 QemuFwCfgSelectItem (QemuFwCfgItemKernelSetupSize
);
53 SetupSize
= (UINTN
) QemuFwCfgRead64 ();
55 if (KernelSize
== 0 || SetupSize
== 0) {
56 DEBUG ((EFI_D_INFO
, "qemu -kernel was not used.\n"));
60 SetupBuf
= LoadLinuxAllocateKernelSetupPages (EFI_SIZE_TO_PAGES (SetupSize
));
61 if (SetupBuf
== NULL
) {
62 DEBUG ((EFI_D_ERROR
, "Unable to allocate memory for kernel setup!\n"));
63 return EFI_OUT_OF_RESOURCES
;
66 DEBUG ((EFI_D_INFO
, "Setup size: 0x%x\n", (UINT32
) SetupSize
));
67 DEBUG ((EFI_D_INFO
, "Reading kernel setup image ..."));
68 QemuFwCfgSelectItem (QemuFwCfgItemKernelSetupData
);
69 QemuFwCfgReadBytes (SetupSize
, SetupBuf
);
70 DEBUG ((EFI_D_INFO
, " [done]\n"));
72 Status
= LoadLinuxCheckKernelSetup (SetupBuf
, SetupSize
);
73 if (EFI_ERROR (Status
)) {
77 Status
= LoadLinuxInitializeKernelSetup (SetupBuf
);
78 if (EFI_ERROR (Status
)) {
82 KernelInitialSize
= LoadLinuxGetKernelSize (SetupBuf
, KernelSize
);
83 if (KernelInitialSize
== 0) {
84 Status
= EFI_UNSUPPORTED
;
88 KernelBuf
= LoadLinuxAllocateKernelPages (
90 EFI_SIZE_TO_PAGES (KernelInitialSize
));
91 if (KernelBuf
== NULL
) {
92 DEBUG ((EFI_D_ERROR
, "Unable to allocate memory for kernel!\n"));
93 Status
= EFI_OUT_OF_RESOURCES
;
97 DEBUG ((EFI_D_INFO
, "Kernel size: 0x%x\n", (UINT32
) KernelSize
));
98 DEBUG ((EFI_D_INFO
, "Reading kernel image ..."));
99 QemuFwCfgSelectItem (QemuFwCfgItemKernelData
);
100 QemuFwCfgReadBytes (KernelSize
, KernelBuf
);
101 DEBUG ((EFI_D_INFO
, " [done]\n"));
103 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineSize
);
104 CommandLineSize
= (UINTN
) QemuFwCfgRead64 ();
106 if (CommandLineSize
> 0) {
107 CommandLine
= LoadLinuxAllocateCommandLinePages (
108 EFI_SIZE_TO_PAGES (CommandLineSize
));
109 QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData
);
110 QemuFwCfgReadBytes (CommandLineSize
, CommandLine
);
115 Status
= LoadLinuxSetCommandLine (SetupBuf
, CommandLine
);
116 if (EFI_ERROR (Status
)) {
120 QemuFwCfgSelectItem (QemuFwCfgItemInitrdSize
);
121 InitrdSize
= (UINTN
) QemuFwCfgRead64 ();
123 if (InitrdSize
> 0) {
124 InitrdData
= LoadLinuxAllocateInitrdPages (
126 EFI_SIZE_TO_PAGES (InitrdSize
)
128 DEBUG ((EFI_D_INFO
, "Initrd size: 0x%x\n", (UINT32
) InitrdSize
));
129 DEBUG ((EFI_D_INFO
, "Reading initrd image ..."));
130 QemuFwCfgSelectItem (QemuFwCfgItemInitrdData
);
131 QemuFwCfgReadBytes (InitrdSize
, InitrdData
);
132 DEBUG ((EFI_D_INFO
, " [done]\n"));
137 Status
= LoadLinuxSetInitrd (SetupBuf
, InitrdData
, InitrdSize
);
138 if (EFI_ERROR (Status
)) {
143 // Signal the EVT_SIGNAL_READY_TO_BOOT event
145 EfiSignalEventReadyToBoot();
147 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
,
148 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
150 Status
= LoadLinux (KernelBuf
, SetupBuf
);
153 if (SetupBuf
!= NULL
) {
154 FreePages (SetupBuf
, EFI_SIZE_TO_PAGES (SetupSize
));
156 if (KernelBuf
!= NULL
) {
157 FreePages (KernelBuf
, EFI_SIZE_TO_PAGES (KernelInitialSize
));
159 if (CommandLine
!= NULL
) {
160 FreePages (CommandLine
, EFI_SIZE_TO_PAGES (CommandLineSize
));
162 if (InitrdData
!= NULL
) {
163 FreePages (InitrdData
, EFI_SIZE_TO_PAGES (InitrdSize
));