2 Try to load an EFI-stubbed ARM Linux kernel from QEMU's fw_cfg.
4 This implementation differs from OvmfPkg/Library/LoadLinuxLib. An EFI
5 stub in the subject kernel is a hard requirement here.
7 Copyright (C) 2014-2016, Red Hat, Inc.
9 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Guid/FileInfo.h>
19 #include <Guid/FileSystemInfo.h>
20 #include <Guid/FileSystemVolumeLabelInfo.h>
21 #include <Library/PrintLib.h>
22 #include <Library/QemuFwCfgLib.h>
23 #include <Library/ReportStatusCodeLib.h>
24 #include <Protocol/DevicePath.h>
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/SimpleFileSystem.h>
28 #include "PlatformBm.h"
31 // Static data that hosts the fw_cfg blobs and serves file requests.
36 KernelBlobTypeCommandLine
,
41 FIRMWARE_CONFIG_ITEM CONST SizeKey
;
42 FIRMWARE_CONFIG_ITEM CONST DataKey
;
43 CONST CHAR16
* CONST Name
;
48 STATIC KERNEL_BLOB mKernelBlob
[KernelBlobTypeMax
] = {
49 { QemuFwCfgItemKernelSize
, QemuFwCfgItemKernelData
, L
"kernel" },
50 { QemuFwCfgItemInitrdSize
, QemuFwCfgItemInitrdData
, L
"initrd" },
51 { QemuFwCfgItemCommandLineSize
, QemuFwCfgItemCommandLineData
, L
"cmdline" }
54 STATIC UINT64 mTotalBlobBytes
;
57 // Device path for the handle that incorporates our "EFI stub filesystem". The
58 // GUID is arbitrary and need not be standardized or advertized.
62 VENDOR_DEVICE_PATH VenHwNode
;
63 EFI_DEVICE_PATH_PROTOCOL EndNode
;
64 } SINGLE_VENHW_NODE_DEVPATH
;
67 STATIC CONST SINGLE_VENHW_NODE_DEVPATH mFileSystemDevicePath
= {
69 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
) } },
71 0xb0fae7e7, 0x6b07, 0x49d0,
72 { 0x9e, 0x5b, 0x3b, 0xde, 0xc8, 0x3b, 0x03, 0x9d }
77 END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
,
78 { sizeof (EFI_DEVICE_PATH_PROTOCOL
) }
83 // The "file in the EFI stub filesystem" abstraction.
85 STATIC EFI_TIME mInitTime
;
87 #define STUB_FILE_SIG SIGNATURE_64 ('S', 'T', 'U', 'B', 'F', 'I', 'L', 'E')
90 UINT64 Signature
; // Carries STUB_FILE_SIG.
92 KERNEL_BLOB_TYPE BlobType
; // Index into mKernelBlob. KernelBlobTypeMax
93 // denotes the root directory of the filesystem.
95 UINT64 Position
; // Byte position for regular files;
96 // next directory entry to return for the root
99 EFI_FILE_PROTOCOL File
; // Standard protocol interface.
102 #define STUB_FILE_FROM_FILE(FilePointer) \
103 CR (FilePointer, STUB_FILE, File, STUB_FILE_SIG)
106 // Tentative definition of the file protocol template. The initializer
107 // (external definition) will be provided later.
109 STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate
;
113 // Protocol member functions for File.
117 Opens a new file relative to the source file's location.
119 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
120 the file handle to the source location. This would
121 typically be an open handle to a directory.
123 @param[out] NewHandle A pointer to the location to return the opened handle
126 @param[in] FileName The Null-terminated string of the name of the file to
127 be opened. The file name may contain the following
128 path modifiers: "\", ".", and "..".
130 @param[in] OpenMode The mode to open the file. The only valid
131 combinations that the file may be opened with are:
132 Read, Read/Write, or Create/Read/Write.
134 @param[in] Attributes Only valid for EFI_FILE_MODE_CREATE, in which case
135 these are the attribute bits for the newly created
138 @retval EFI_SUCCESS The file was opened.
139 @retval EFI_NOT_FOUND The specified file could not be found on the
141 @retval EFI_NO_MEDIA The device has no medium.
142 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
143 medium is no longer supported.
144 @retval EFI_DEVICE_ERROR The device reported an error.
145 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
146 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a
147 file for write when the media is
149 @retval EFI_ACCESS_DENIED The service denied access to the file.
150 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
152 @retval EFI_VOLUME_FULL The volume is full.
158 IN EFI_FILE_PROTOCOL
*This
,
159 OUT EFI_FILE_PROTOCOL
**NewHandle
,
165 CONST STUB_FILE
*StubFile
;
167 STUB_FILE
*NewStubFile
;
173 case EFI_FILE_MODE_READ
:
176 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
177 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
:
178 return EFI_WRITE_PROTECTED
;
181 return EFI_INVALID_PARAMETER
;
185 // Only the root directory supports opening files in it.
187 StubFile
= STUB_FILE_FROM_FILE (This
);
188 if (StubFile
->BlobType
!= KernelBlobTypeMax
) {
189 return EFI_UNSUPPORTED
;
195 for (BlobType
= 0; BlobType
< KernelBlobTypeMax
; ++BlobType
) {
196 if (StrCmp (FileName
, mKernelBlob
[BlobType
].Name
) == 0) {
200 if (BlobType
== KernelBlobTypeMax
) {
201 return EFI_NOT_FOUND
;
207 NewStubFile
= AllocatePool (sizeof *NewStubFile
);
208 if (NewStubFile
== NULL
) {
209 return EFI_OUT_OF_RESOURCES
;
212 NewStubFile
->Signature
= STUB_FILE_SIG
;
213 NewStubFile
->BlobType
= (KERNEL_BLOB_TYPE
)BlobType
;
214 NewStubFile
->Position
= 0;
215 CopyMem (&NewStubFile
->File
, &mEfiFileProtocolTemplate
,
216 sizeof mEfiFileProtocolTemplate
);
217 *NewHandle
= &NewStubFile
->File
;
224 Closes a specified file handle.
226 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
229 @retval EFI_SUCCESS The file was closed.
235 IN EFI_FILE_PROTOCOL
*This
238 FreePool (STUB_FILE_FROM_FILE (This
));
244 Close and delete the file handle.
246 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
247 handle to the file to delete.
249 @retval EFI_SUCCESS The file was closed and deleted, and the
251 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not
259 IN EFI_FILE_PROTOCOL
*This
262 FreePool (STUB_FILE_FROM_FILE (This
));
263 return EFI_WARN_DELETE_FAILURE
;
268 Helper function that formats an EFI_FILE_INFO structure into the
269 user-allocated buffer, for any valid KERNEL_BLOB_TYPE value (including
270 KernelBlobTypeMax, which stands for the root directory).
272 The interface follows the EFI_FILE_GET_INFO -- and for directories, the
273 EFI_FILE_READ -- interfaces.
275 @param[in] BlobType The KERNEL_BLOB_TYPE value identifying the fw_cfg
276 blob backing the STUB_FILE that information is
277 being requested about. If BlobType equals
278 KernelBlobTypeMax, then information will be
279 provided about the root directory of the
282 @param[in,out] BufferSize On input, the size of Buffer. On output, the
283 amount of data returned in Buffer. In both cases,
284 the size is measured in bytes.
286 @param[out] Buffer A pointer to the data buffer to return. The
287 buffer's type is EFI_FILE_INFO.
289 @retval EFI_SUCCESS The information was returned.
290 @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to store the
291 EFI_FILE_INFO structure. BufferSize has been
292 updated with the size needed to complete the
297 ConvertKernelBlobTypeToFileInfo (
298 IN KERNEL_BLOB_TYPE BlobType
,
299 IN OUT UINTN
*BufferSize
,
309 EFI_FILE_INFO
*FileInfo
;
310 UINTN OriginalBufferSize
;
312 if (BlobType
== KernelBlobTypeMax
) {
314 // getting file info about the root directory
317 FileSize
= KernelBlobTypeMax
;
318 Attribute
= EFI_FILE_READ_ONLY
| EFI_FILE_DIRECTORY
;
320 CONST KERNEL_BLOB
*Blob
;
322 Blob
= &mKernelBlob
[BlobType
];
324 FileSize
= Blob
->Size
;
325 Attribute
= EFI_FILE_READ_ONLY
;
328 NameSize
= (StrLen(Name
) + 1) * 2;
329 FileInfoSize
= OFFSET_OF (EFI_FILE_INFO
, FileName
) + NameSize
;
330 ASSERT (FileInfoSize
>= sizeof *FileInfo
);
332 OriginalBufferSize
= *BufferSize
;
333 *BufferSize
= FileInfoSize
;
334 if (OriginalBufferSize
< *BufferSize
) {
335 return EFI_BUFFER_TOO_SMALL
;
338 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
339 FileInfo
->Size
= FileInfoSize
;
340 FileInfo
->FileSize
= FileSize
;
341 FileInfo
->PhysicalSize
= FileSize
;
342 FileInfo
->Attribute
= Attribute
;
344 CopyMem (&FileInfo
->CreateTime
, &mInitTime
, sizeof mInitTime
);
345 CopyMem (&FileInfo
->LastAccessTime
, &mInitTime
, sizeof mInitTime
);
346 CopyMem (&FileInfo
->ModificationTime
, &mInitTime
, sizeof mInitTime
);
347 CopyMem (FileInfo
->FileName
, Name
, NameSize
);
354 Reads data from a file, or continues scanning a directory.
356 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
357 is the file handle to read data from.
359 @param[in,out] BufferSize On input, the size of the Buffer. On output, the
360 amount of data returned in Buffer. In both cases,
361 the size is measured in bytes. If the read goes
362 beyond the end of the file, the read length is
363 truncated to the end of the file.
365 If This is a directory, the function reads the
366 directory entry at the current position and
367 returns the entry (as EFI_FILE_INFO) in Buffer. If
368 there are no more directory entries, the
369 BufferSize is set to zero on output.
371 @param[out] Buffer The buffer into which the data is read.
373 @retval EFI_SUCCESS Data was read.
374 @retval EFI_NO_MEDIA The device has no medium.
375 @retval EFI_DEVICE_ERROR The device reported an error.
376 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted
378 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond
380 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
381 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the
382 current directory entry as a EFI_FILE_INFO
383 structure. BufferSize has been updated with the
384 size needed to complete the request, and the
385 directory position has not been advanced.
391 IN EFI_FILE_PROTOCOL
*This
,
392 IN OUT UINTN
*BufferSize
,
397 CONST KERNEL_BLOB
*Blob
;
400 StubFile
= STUB_FILE_FROM_FILE (This
);
403 // Scanning the root directory?
405 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
408 if (StubFile
->Position
== KernelBlobTypeMax
) {
410 // Scanning complete.
416 Status
= ConvertKernelBlobTypeToFileInfo (
417 (KERNEL_BLOB_TYPE
)StubFile
->Position
,
420 if (EFI_ERROR (Status
)) {
424 ++StubFile
->Position
;
431 Blob
= &mKernelBlob
[StubFile
->BlobType
];
432 if (StubFile
->Position
> Blob
->Size
) {
433 return EFI_DEVICE_ERROR
;
436 Left
= Blob
->Size
- StubFile
->Position
;
437 if (*BufferSize
> Left
) {
438 *BufferSize
= (UINTN
)Left
;
440 if (Blob
->Data
!= NULL
) {
441 CopyMem (Buffer
, Blob
->Data
+ StubFile
->Position
, *BufferSize
);
443 StubFile
->Position
+= *BufferSize
;
449 Writes data to a file.
451 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
452 is the file handle to write data to.
454 @param[in,out] BufferSize On input, the size of the Buffer. On output, the
455 amount of data actually written. In both cases,
456 the size is measured in bytes.
458 @param[in] Buffer The buffer of data to write.
460 @retval EFI_SUCCESS Data was written.
461 @retval EFI_UNSUPPORTED Writes to open directory files are not
463 @retval EFI_NO_MEDIA The device has no medium.
464 @retval EFI_DEVICE_ERROR The device reported an error.
465 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
466 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
467 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.
468 @retval EFI_ACCESS_DENIED The file was opened read only.
469 @retval EFI_VOLUME_FULL The volume is full.
475 IN EFI_FILE_PROTOCOL
*This
,
476 IN OUT UINTN
*BufferSize
,
482 StubFile
= STUB_FILE_FROM_FILE (This
);
483 return (StubFile
->BlobType
== KernelBlobTypeMax
) ?
490 Returns a file's current position.
492 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
493 file handle to get the current position on.
495 @param[out] Position The address to return the file's current position
498 @retval EFI_SUCCESS The position was returned.
499 @retval EFI_UNSUPPORTED The request is not valid on open directories.
500 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a
506 StubFileGetPosition (
507 IN EFI_FILE_PROTOCOL
*This
,
513 StubFile
= STUB_FILE_FROM_FILE (This
);
514 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
515 return EFI_UNSUPPORTED
;
518 *Position
= StubFile
->Position
;
524 Sets a file's current position.
526 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
527 file handle to set the requested position on.
529 @param[in] Position The byte position from the start of the file to set. For
530 regular files, MAX_UINT64 means "seek to end". For
531 directories, zero means "rewind directory scan".
533 @retval EFI_SUCCESS The position was set.
534 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open
536 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a
542 StubFileSetPosition (
543 IN EFI_FILE_PROTOCOL
*This
,
550 StubFile
= STUB_FILE_FROM_FILE (This
);
552 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
555 // rewinding a directory scan is allowed
557 StubFile
->Position
= 0;
560 return EFI_UNSUPPORTED
;
566 Blob
= &mKernelBlob
[StubFile
->BlobType
];
567 if (Position
== MAX_UINT64
) {
571 StubFile
->Position
= Blob
->Size
;
574 // absolute seek from beginning -- seeking past the end is allowed
576 StubFile
->Position
= Position
;
583 Returns information about a file.
585 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance
586 that is the file handle the requested
589 @param[in] InformationType The type identifier GUID for the information
590 being requested. The following information
591 types are supported, storing the
592 corresponding structures in Buffer:
594 - gEfiFileInfoGuid: EFI_FILE_INFO
596 - gEfiFileSystemInfoGuid:
599 - gEfiFileSystemVolumeLabelInfoIdGuid:
600 EFI_FILE_SYSTEM_VOLUME_LABEL
602 @param[in,out] BufferSize On input, the size of Buffer. On output, the
603 amount of data returned in Buffer. In both
604 cases, the size is measured in bytes.
606 @param[out] Buffer A pointer to the data buffer to return. The
607 buffer's type is indicated by
610 @retval EFI_SUCCESS The information was returned.
611 @retval EFI_UNSUPPORTED The InformationType is not known.
612 @retval EFI_NO_MEDIA The device has no medium.
613 @retval EFI_DEVICE_ERROR The device reported an error.
614 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
615 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the
616 information structure requested by
617 InformationType. BufferSize has been updated
618 with the size needed to complete the request.
624 IN EFI_FILE_PROTOCOL
*This
,
625 IN EFI_GUID
*InformationType
,
626 IN OUT UINTN
*BufferSize
,
630 CONST STUB_FILE
*StubFile
;
631 UINTN OriginalBufferSize
;
633 StubFile
= STUB_FILE_FROM_FILE (This
);
635 if (CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
636 return ConvertKernelBlobTypeToFileInfo (StubFile
->BlobType
, BufferSize
,
640 OriginalBufferSize
= *BufferSize
;
642 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
643 EFI_FILE_SYSTEM_INFO
*FileSystemInfo
;
645 *BufferSize
= sizeof *FileSystemInfo
;
646 if (OriginalBufferSize
< *BufferSize
) {
647 return EFI_BUFFER_TOO_SMALL
;
650 FileSystemInfo
= (EFI_FILE_SYSTEM_INFO
*)Buffer
;
651 FileSystemInfo
->Size
= sizeof *FileSystemInfo
;
652 FileSystemInfo
->ReadOnly
= TRUE
;
653 FileSystemInfo
->VolumeSize
= mTotalBlobBytes
;
654 FileSystemInfo
->FreeSpace
= 0;
655 FileSystemInfo
->BlockSize
= 1;
656 FileSystemInfo
->VolumeLabel
[0] = L
'\0';
661 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
662 EFI_FILE_SYSTEM_VOLUME_LABEL
*FileSystemVolumeLabel
;
664 *BufferSize
= sizeof *FileSystemVolumeLabel
;
665 if (OriginalBufferSize
< *BufferSize
) {
666 return EFI_BUFFER_TOO_SMALL
;
669 FileSystemVolumeLabel
= (EFI_FILE_SYSTEM_VOLUME_LABEL
*)Buffer
;
670 FileSystemVolumeLabel
->VolumeLabel
[0] = L
'\0';
675 return EFI_UNSUPPORTED
;
680 Sets information about a file.
682 @param[in] File A pointer to the EFI_FILE_PROTOCOL instance that
683 is the file handle the information is for.
685 @param[in] InformationType The type identifier for the information being
688 @param[in] BufferSize The size, in bytes, of Buffer.
690 @param[in] Buffer A pointer to the data buffer to write. The
691 buffer's type is indicated by InformationType.
693 @retval EFI_SUCCESS The information was set.
694 @retval EFI_UNSUPPORTED The InformationType is not known.
695 @retval EFI_NO_MEDIA The device has no medium.
696 @retval EFI_DEVICE_ERROR The device reported an error.
697 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
698 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the
700 @retval EFI_WRITE_PROTECTED InformationType is
701 EFI_FILE_PROTOCOL_SYSTEM_INFO_ID and the media
703 @retval EFI_WRITE_PROTECTED InformationType is
704 EFI_FILE_SYSTEM_VOLUME_LABEL_ID and the media
706 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file
707 to a file that is already present.
708 @retval EFI_ACCESS_DENIED An attempt is being made to change the
709 EFI_FILE_DIRECTORY Attribute.
710 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of
712 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the
713 file was opened read-only and an attempt is
714 being made to modify a field other than
716 @retval EFI_VOLUME_FULL The volume is full.
717 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type
718 indicated by InformationType.
724 IN EFI_FILE_PROTOCOL
*This
,
725 IN EFI_GUID
*InformationType
,
730 return EFI_WRITE_PROTECTED
;
735 Flushes all modified data associated with a file to a device.
737 @param [in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
738 file handle to flush.
740 @retval EFI_SUCCESS The data was flushed.
741 @retval EFI_NO_MEDIA The device has no medium.
742 @retval EFI_DEVICE_ERROR The device reported an error.
743 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
744 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.
745 @retval EFI_ACCESS_DENIED The file was opened read-only.
746 @retval EFI_VOLUME_FULL The volume is full.
752 IN EFI_FILE_PROTOCOL
*This
755 return EFI_WRITE_PROTECTED
;
759 // External definition of the file protocol template.
761 STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate
= {
762 EFI_FILE_PROTOCOL_REVISION
, // revision 1
773 NULL
, // OpenEx, revision 2
774 NULL
, // ReadEx, revision 2
775 NULL
, // WriteEx, revision 2
776 NULL
// FlushEx, revision 2
781 // Protocol member functions for SimpleFileSystem.
785 Open the root directory on a volume.
787 @param[in] This A pointer to the volume to open the root directory on.
789 @param[out] Root A pointer to the location to return the opened file handle
790 for the root directory in.
792 @retval EFI_SUCCESS The device was opened.
793 @retval EFI_UNSUPPORTED This volume does not support the requested file
795 @retval EFI_NO_MEDIA The device has no medium.
796 @retval EFI_DEVICE_ERROR The device reported an error.
797 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
798 @retval EFI_ACCESS_DENIED The service denied access to the file.
799 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of
801 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
802 medium is no longer supported. Any existing
803 file handles for this volume are no longer
804 valid. To access the files on the new medium,
805 the volume must be reopened with OpenVolume().
810 StubFileSystemOpenVolume (
811 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
812 OUT EFI_FILE_PROTOCOL
**Root
817 StubFile
= AllocatePool (sizeof *StubFile
);
818 if (StubFile
== NULL
) {
819 return EFI_OUT_OF_RESOURCES
;
822 StubFile
->Signature
= STUB_FILE_SIG
;
823 StubFile
->BlobType
= KernelBlobTypeMax
;
824 StubFile
->Position
= 0;
825 CopyMem (&StubFile
->File
, &mEfiFileProtocolTemplate
,
826 sizeof mEfiFileProtocolTemplate
);
827 *Root
= &StubFile
->File
;
832 STATIC CONST EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mFileSystem
= {
833 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
,
834 StubFileSystemOpenVolume
839 // Utility functions.
843 Populate a blob in mKernelBlob.
845 param[in,out] Blob Pointer to the KERNEL_BLOB element in mKernelBlob that is
846 to be filled from fw_cfg.
848 @retval EFI_SUCCESS Blob has been populated. If fw_cfg reported a
849 size of zero for the blob, then Blob->Data has
852 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Blob->Data.
857 IN OUT KERNEL_BLOB
*Blob
865 QemuFwCfgSelectItem (Blob
->SizeKey
);
866 Blob
->Size
= QemuFwCfgRead32 ();
867 if (Blob
->Size
== 0) {
874 Blob
->Data
= AllocatePool (Blob
->Size
);
875 if (Blob
->Data
== NULL
) {
876 DEBUG ((EFI_D_ERROR
, "%a: failed to allocate %Ld bytes for \"%s\"\n",
877 __FUNCTION__
, (INT64
)Blob
->Size
, Blob
->Name
));
878 return EFI_OUT_OF_RESOURCES
;
881 DEBUG ((EFI_D_INFO
, "%a: loading %Ld bytes for \"%s\"\n", __FUNCTION__
,
882 (INT64
)Blob
->Size
, Blob
->Name
));
883 QemuFwCfgSelectItem (Blob
->DataKey
);
889 Chunk
= (Left
< SIZE_1MB
) ? Left
: SIZE_1MB
;
890 QemuFwCfgReadBytes (Chunk
, Blob
->Data
+ (Blob
->Size
- Left
));
892 DEBUG ((EFI_D_VERBOSE
, "%a: %Ld bytes remaining for \"%s\"\n",
893 __FUNCTION__
, (INT64
)Left
, Blob
->Name
));
900 // The entry point of the feature.
904 Download the kernel, the initial ramdisk, and the kernel command line from
905 QEMU's fw_cfg. Construct a minimal SimpleFileSystem that contains the two
906 image files, and load and start the kernel from it.
908 The kernel will be instructed via its command line to load the initrd from
909 the same Simple FileSystem.
911 @retval EFI_NOT_FOUND Kernel image was not found.
912 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
913 @retval EFI_PROTOCOL_ERROR Unterminated kernel command line.
915 @return Error codes from any of the underlying
916 functions. On success, the function doesn't
921 TryRunningQemuKernel (
926 KERNEL_BLOB
*CurrentBlob
;
927 KERNEL_BLOB
*KernelBlob
, *InitrdBlob
, *CommandLineBlob
;
929 EFI_HANDLE FileSystemHandle
;
930 EFI_DEVICE_PATH_PROTOCOL
*KernelDevicePath
;
931 EFI_HANDLE KernelImageHandle
;
932 EFI_LOADED_IMAGE_PROTOCOL
*KernelLoadedImage
;
934 Status
= gRT
->GetTime (&mInitTime
, NULL
/* Capabilities */);
935 if (EFI_ERROR (Status
)) {
936 DEBUG ((EFI_D_ERROR
, "%a: GetTime(): %r\n", __FUNCTION__
, Status
));
943 for (BlobType
= 0; BlobType
< KernelBlobTypeMax
; ++BlobType
) {
944 CurrentBlob
= &mKernelBlob
[BlobType
];
945 Status
= FetchBlob (CurrentBlob
);
946 if (EFI_ERROR (Status
)) {
949 mTotalBlobBytes
+= CurrentBlob
->Size
;
951 KernelBlob
= &mKernelBlob
[KernelBlobTypeKernel
];
952 InitrdBlob
= &mKernelBlob
[KernelBlobTypeInitrd
];
953 CommandLineBlob
= &mKernelBlob
[KernelBlobTypeCommandLine
];
955 if (KernelBlob
->Data
== NULL
) {
956 Status
= EFI_NOT_FOUND
;
961 // Create a new handle with a single VenHw() node device path protocol on it,
962 // plus a custom SimpleFileSystem protocol on it.
964 FileSystemHandle
= NULL
;
965 Status
= gBS
->InstallMultipleProtocolInterfaces (&FileSystemHandle
,
966 &gEfiDevicePathProtocolGuid
, &mFileSystemDevicePath
,
967 &gEfiSimpleFileSystemProtocolGuid
, &mFileSystem
,
969 if (EFI_ERROR (Status
)) {
970 DEBUG ((EFI_D_ERROR
, "%a: InstallMultipleProtocolInterfaces(): %r\n",
971 __FUNCTION__
, Status
));
976 // Create a device path for the kernel image to be loaded from that will call
977 // back into our file system.
979 KernelDevicePath
= FileDevicePath (FileSystemHandle
, KernelBlob
->Name
);
980 if (KernelDevicePath
== NULL
) {
981 DEBUG ((EFI_D_ERROR
, "%a: failed to allocate kernel device path\n",
983 Status
= EFI_OUT_OF_RESOURCES
;
984 goto UninstallProtocols
;
988 // Load the image. This should call back into our file system.
990 Status
= gBS
->LoadImage (
991 FALSE
, // BootPolicy: exact match required
992 gImageHandle
, // ParentImageHandle
994 NULL
, // SourceBuffer
998 if (EFI_ERROR (Status
)) {
999 DEBUG ((EFI_D_ERROR
, "%a: LoadImage(): %r\n", __FUNCTION__
, Status
));
1000 goto FreeKernelDevicePath
;
1004 // Construct the kernel command line.
1006 Status
= gBS
->OpenProtocol (
1008 &gEfiLoadedImageProtocolGuid
,
1009 (VOID
**)&KernelLoadedImage
,
1010 gImageHandle
, // AgentHandle
1011 NULL
, // ControllerHandle
1012 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1014 ASSERT_EFI_ERROR (Status
);
1016 if (CommandLineBlob
->Data
== NULL
) {
1017 KernelLoadedImage
->LoadOptionsSize
= 0;
1020 // Verify NUL-termination of the command line.
1022 if (CommandLineBlob
->Data
[CommandLineBlob
->Size
- 1] != '\0') {
1023 DEBUG ((EFI_D_ERROR
, "%a: kernel command line is not NUL-terminated\n",
1025 Status
= EFI_PROTOCOL_ERROR
;
1026 goto UnloadKernelImage
;
1030 // Drop the terminating NUL, convert to UTF-16.
1032 KernelLoadedImage
->LoadOptionsSize
= (CommandLineBlob
->Size
- 1) * 2;
1035 if (InitrdBlob
->Data
!= NULL
) {
1037 // Append ' initrd=<name>' in UTF-16.
1039 KernelLoadedImage
->LoadOptionsSize
+=
1040 (8 + StrLen(InitrdBlob
->Name
)) * 2;
1043 if (KernelLoadedImage
->LoadOptionsSize
== 0) {
1044 KernelLoadedImage
->LoadOptions
= NULL
;
1047 // NUL-terminate in UTF-16.
1049 KernelLoadedImage
->LoadOptionsSize
+= 2;
1051 KernelLoadedImage
->LoadOptions
= AllocatePool (
1052 KernelLoadedImage
->LoadOptionsSize
);
1053 if (KernelLoadedImage
->LoadOptions
== NULL
) {
1054 KernelLoadedImage
->LoadOptionsSize
= 0;
1055 Status
= EFI_OUT_OF_RESOURCES
;
1056 goto UnloadKernelImage
;
1059 UnicodeSPrintAsciiFormat (
1060 KernelLoadedImage
->LoadOptions
,
1061 KernelLoadedImage
->LoadOptionsSize
,
1063 (CommandLineBlob
->Data
== NULL
) ? "" : (CHAR8
*)CommandLineBlob
->Data
,
1064 (InitrdBlob
->Data
== NULL
) ? "" : " initrd=",
1065 (InitrdBlob
->Data
== NULL
) ? L
"" : InitrdBlob
->Name
1067 DEBUG ((EFI_D_INFO
, "%a: command line: \"%s\"\n", __FUNCTION__
,
1068 (CHAR16
*)KernelLoadedImage
->LoadOptions
));
1072 // Signal the EFI_EVENT_GROUP_READY_TO_BOOT event.
1074 EfiSignalEventReadyToBoot();
1076 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
,
1077 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
1082 Status
= gBS
->StartImage (
1084 NULL
, // ExitDataSize
1087 if (EFI_ERROR (Status
)) {
1088 DEBUG ((EFI_D_ERROR
, "%a: StartImage(): %r\n", __FUNCTION__
, Status
));
1091 if (KernelLoadedImage
->LoadOptions
!= NULL
) {
1092 FreePool (KernelLoadedImage
->LoadOptions
);
1094 KernelLoadedImage
->LoadOptionsSize
= 0;
1097 gBS
->UnloadImage (KernelImageHandle
);
1099 FreeKernelDevicePath
:
1100 FreePool (KernelDevicePath
);
1103 gBS
->UninstallMultipleProtocolInterfaces (FileSystemHandle
,
1104 &gEfiSimpleFileSystemProtocolGuid
, &mFileSystem
,
1105 &gEfiDevicePathProtocolGuid
, &mFileSystemDevicePath
,
1109 while (BlobType
> 0) {
1110 CurrentBlob
= &mKernelBlob
[--BlobType
];
1111 if (CurrentBlob
->Data
!= NULL
) {
1112 FreePool (CurrentBlob
->Data
);
1113 CurrentBlob
->Size
= 0;
1114 CurrentBlob
->Data
= NULL
;