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 <Protocol/DevicePath.h>
24 #include <Protocol/LoadedImage.h>
25 #include <Protocol/SimpleFileSystem.h>
27 #include "PlatformBm.h"
30 // Static data that hosts the fw_cfg blobs and serves file requests.
35 KernelBlobTypeCommandLine
,
40 FIRMWARE_CONFIG_ITEM CONST SizeKey
;
41 FIRMWARE_CONFIG_ITEM CONST DataKey
;
42 CONST CHAR16
* CONST Name
;
47 STATIC KERNEL_BLOB mKernelBlob
[KernelBlobTypeMax
] = {
48 { QemuFwCfgItemKernelSize
, QemuFwCfgItemKernelData
, L
"kernel" },
49 { QemuFwCfgItemInitrdSize
, QemuFwCfgItemInitrdData
, L
"initrd" },
50 { QemuFwCfgItemCommandLineSize
, QemuFwCfgItemCommandLineData
, L
"cmdline" }
53 STATIC UINT64 mTotalBlobBytes
;
56 // Device path for the handle that incorporates our "EFI stub filesystem". The
57 // GUID is arbitrary and need not be standardized or advertized.
61 VENDOR_DEVICE_PATH VenHwNode
;
62 EFI_DEVICE_PATH_PROTOCOL EndNode
;
63 } SINGLE_VENHW_NODE_DEVPATH
;
66 STATIC CONST SINGLE_VENHW_NODE_DEVPATH mFileSystemDevicePath
= {
68 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
) } },
70 0xb0fae7e7, 0x6b07, 0x49d0,
71 { 0x9e, 0x5b, 0x3b, 0xde, 0xc8, 0x3b, 0x03, 0x9d }
76 END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
,
77 { sizeof (EFI_DEVICE_PATH_PROTOCOL
) }
82 // The "file in the EFI stub filesystem" abstraction.
84 STATIC EFI_TIME mInitTime
;
86 #define STUB_FILE_SIG SIGNATURE_64 ('S', 'T', 'U', 'B', 'F', 'I', 'L', 'E')
89 UINT64 Signature
; // Carries STUB_FILE_SIG.
91 KERNEL_BLOB_TYPE BlobType
; // Index into mKernelBlob. KernelBlobTypeMax
92 // denotes the root directory of the filesystem.
94 UINT64 Position
; // Byte position for regular files;
95 // next directory entry to return for the root
98 EFI_FILE_PROTOCOL File
; // Standard protocol interface.
101 #define STUB_FILE_FROM_FILE(FilePointer) \
102 CR (FilePointer, STUB_FILE, File, STUB_FILE_SIG)
105 // Tentative definition of the file protocol template. The initializer
106 // (external definition) will be provided later.
108 STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate
;
112 // Protocol member functions for File.
116 Opens a new file relative to the source file's location.
118 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
119 the file handle to the source location. This would
120 typically be an open handle to a directory.
122 @param[out] NewHandle A pointer to the location to return the opened handle
125 @param[in] FileName The Null-terminated string of the name of the file to
126 be opened. The file name may contain the following
127 path modifiers: "\", ".", and "..".
129 @param[in] OpenMode The mode to open the file. The only valid
130 combinations that the file may be opened with are:
131 Read, Read/Write, or Create/Read/Write.
133 @param[in] Attributes Only valid for EFI_FILE_MODE_CREATE, in which case
134 these are the attribute bits for the newly created
137 @retval EFI_SUCCESS The file was opened.
138 @retval EFI_NOT_FOUND The specified file could not be found on the
140 @retval EFI_NO_MEDIA The device has no medium.
141 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
142 medium is no longer supported.
143 @retval EFI_DEVICE_ERROR The device reported an error.
144 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
145 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a
146 file for write when the media is
148 @retval EFI_ACCESS_DENIED The service denied access to the file.
149 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
151 @retval EFI_VOLUME_FULL The volume is full.
157 IN EFI_FILE_PROTOCOL
*This
,
158 OUT EFI_FILE_PROTOCOL
**NewHandle
,
164 CONST STUB_FILE
*StubFile
;
166 STUB_FILE
*NewStubFile
;
172 case EFI_FILE_MODE_READ
:
175 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
176 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
:
177 return EFI_WRITE_PROTECTED
;
180 return EFI_INVALID_PARAMETER
;
184 // Only the root directory supports opening files in it.
186 StubFile
= STUB_FILE_FROM_FILE (This
);
187 if (StubFile
->BlobType
!= KernelBlobTypeMax
) {
188 return EFI_UNSUPPORTED
;
194 for (BlobType
= 0; BlobType
< KernelBlobTypeMax
; ++BlobType
) {
195 if (StrCmp (FileName
, mKernelBlob
[BlobType
].Name
) == 0) {
199 if (BlobType
== KernelBlobTypeMax
) {
200 return EFI_NOT_FOUND
;
206 NewStubFile
= AllocatePool (sizeof *NewStubFile
);
207 if (NewStubFile
== NULL
) {
208 return EFI_OUT_OF_RESOURCES
;
211 NewStubFile
->Signature
= STUB_FILE_SIG
;
212 NewStubFile
->BlobType
= (KERNEL_BLOB_TYPE
)BlobType
;
213 NewStubFile
->Position
= 0;
214 CopyMem (&NewStubFile
->File
, &mEfiFileProtocolTemplate
,
215 sizeof mEfiFileProtocolTemplate
);
216 *NewHandle
= &NewStubFile
->File
;
223 Closes a specified file handle.
225 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file
228 @retval EFI_SUCCESS The file was closed.
234 IN EFI_FILE_PROTOCOL
*This
237 FreePool (STUB_FILE_FROM_FILE (This
));
243 Close and delete the file handle.
245 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
246 handle to the file to delete.
248 @retval EFI_SUCCESS The file was closed and deleted, and the
250 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not
258 IN EFI_FILE_PROTOCOL
*This
261 FreePool (STUB_FILE_FROM_FILE (This
));
262 return EFI_WARN_DELETE_FAILURE
;
267 Helper function that formats an EFI_FILE_INFO structure into the
268 user-allocated buffer, for any valid KERNEL_BLOB_TYPE value (including
269 KernelBlobTypeMax, which stands for the root directory).
271 The interface follows the EFI_FILE_GET_INFO -- and for directories, the
272 EFI_FILE_READ -- interfaces.
274 @param[in] BlobType The KERNEL_BLOB_TYPE value identifying the fw_cfg
275 blob backing the STUB_FILE that information is
276 being requested about. If BlobType equals
277 KernelBlobTypeMax, then information will be
278 provided about the root directory of the
281 @param[in,out] BufferSize On input, the size of Buffer. On output, the
282 amount of data returned in Buffer. In both cases,
283 the size is measured in bytes.
285 @param[out] Buffer A pointer to the data buffer to return. The
286 buffer's type is EFI_FILE_INFO.
288 @retval EFI_SUCCESS The information was returned.
289 @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to store the
290 EFI_FILE_INFO structure. BufferSize has been
291 updated with the size needed to complete the
296 ConvertKernelBlobTypeToFileInfo (
297 IN KERNEL_BLOB_TYPE BlobType
,
298 IN OUT UINTN
*BufferSize
,
308 EFI_FILE_INFO
*FileInfo
;
309 UINTN OriginalBufferSize
;
311 if (BlobType
== KernelBlobTypeMax
) {
313 // getting file info about the root directory
316 FileSize
= KernelBlobTypeMax
;
317 Attribute
= EFI_FILE_READ_ONLY
| EFI_FILE_DIRECTORY
;
319 CONST KERNEL_BLOB
*Blob
;
321 Blob
= &mKernelBlob
[BlobType
];
323 FileSize
= Blob
->Size
;
324 Attribute
= EFI_FILE_READ_ONLY
;
327 NameSize
= (StrLen(Name
) + 1) * 2;
328 FileInfoSize
= OFFSET_OF (EFI_FILE_INFO
, FileName
) + NameSize
;
329 ASSERT (FileInfoSize
>= sizeof *FileInfo
);
331 OriginalBufferSize
= *BufferSize
;
332 *BufferSize
= FileInfoSize
;
333 if (OriginalBufferSize
< *BufferSize
) {
334 return EFI_BUFFER_TOO_SMALL
;
337 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
338 FileInfo
->Size
= FileInfoSize
;
339 FileInfo
->FileSize
= FileSize
;
340 FileInfo
->PhysicalSize
= FileSize
;
341 FileInfo
->Attribute
= Attribute
;
343 CopyMem (&FileInfo
->CreateTime
, &mInitTime
, sizeof mInitTime
);
344 CopyMem (&FileInfo
->LastAccessTime
, &mInitTime
, sizeof mInitTime
);
345 CopyMem (&FileInfo
->ModificationTime
, &mInitTime
, sizeof mInitTime
);
346 CopyMem (FileInfo
->FileName
, Name
, NameSize
);
353 Reads data from a file, or continues scanning a directory.
355 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
356 is the file handle to read data from.
358 @param[in,out] BufferSize On input, the size of the Buffer. On output, the
359 amount of data returned in Buffer. In both cases,
360 the size is measured in bytes. If the read goes
361 beyond the end of the file, the read length is
362 truncated to the end of the file.
364 If This is a directory, the function reads the
365 directory entry at the current position and
366 returns the entry (as EFI_FILE_INFO) in Buffer. If
367 there are no more directory entries, the
368 BufferSize is set to zero on output.
370 @param[out] Buffer The buffer into which the data is read.
372 @retval EFI_SUCCESS Data was read.
373 @retval EFI_NO_MEDIA The device has no medium.
374 @retval EFI_DEVICE_ERROR The device reported an error.
375 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted
377 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond
379 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
380 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the
381 current directory entry as a EFI_FILE_INFO
382 structure. BufferSize has been updated with the
383 size needed to complete the request, and the
384 directory position has not been advanced.
390 IN EFI_FILE_PROTOCOL
*This
,
391 IN OUT UINTN
*BufferSize
,
396 CONST KERNEL_BLOB
*Blob
;
399 StubFile
= STUB_FILE_FROM_FILE (This
);
402 // Scanning the root directory?
404 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
407 if (StubFile
->Position
== KernelBlobTypeMax
) {
409 // Scanning complete.
415 Status
= ConvertKernelBlobTypeToFileInfo (
416 (KERNEL_BLOB_TYPE
)StubFile
->Position
,
419 if (EFI_ERROR (Status
)) {
423 ++StubFile
->Position
;
430 Blob
= &mKernelBlob
[StubFile
->BlobType
];
431 if (StubFile
->Position
> Blob
->Size
) {
432 return EFI_DEVICE_ERROR
;
435 Left
= Blob
->Size
- StubFile
->Position
;
436 if (*BufferSize
> Left
) {
437 *BufferSize
= (UINTN
)Left
;
439 if (Blob
->Data
!= NULL
) {
440 CopyMem (Buffer
, Blob
->Data
+ StubFile
->Position
, *BufferSize
);
442 StubFile
->Position
+= *BufferSize
;
448 Writes data to a file.
450 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
451 is the file handle to write data to.
453 @param[in,out] BufferSize On input, the size of the Buffer. On output, the
454 amount of data actually written. In both cases,
455 the size is measured in bytes.
457 @param[in] Buffer The buffer of data to write.
459 @retval EFI_SUCCESS Data was written.
460 @retval EFI_UNSUPPORTED Writes to open directory files are not
462 @retval EFI_NO_MEDIA The device has no medium.
463 @retval EFI_DEVICE_ERROR The device reported an error.
464 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
465 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
466 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.
467 @retval EFI_ACCESS_DENIED The file was opened read only.
468 @retval EFI_VOLUME_FULL The volume is full.
474 IN EFI_FILE_PROTOCOL
*This
,
475 IN OUT UINTN
*BufferSize
,
481 StubFile
= STUB_FILE_FROM_FILE (This
);
482 return (StubFile
->BlobType
== KernelBlobTypeMax
) ?
489 Returns a file's current position.
491 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
492 file handle to get the current position on.
494 @param[out] Position The address to return the file's current position
497 @retval EFI_SUCCESS The position was returned.
498 @retval EFI_UNSUPPORTED The request is not valid on open directories.
499 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a
505 StubFileGetPosition (
506 IN EFI_FILE_PROTOCOL
*This
,
512 StubFile
= STUB_FILE_FROM_FILE (This
);
513 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
514 return EFI_UNSUPPORTED
;
517 *Position
= StubFile
->Position
;
523 Sets a file's current position.
525 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
526 file handle to set the requested position on.
528 @param[in] Position The byte position from the start of the file to set. For
529 regular files, MAX_UINT64 means "seek to end". For
530 directories, zero means "rewind directory scan".
532 @retval EFI_SUCCESS The position was set.
533 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open
535 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a
541 StubFileSetPosition (
542 IN EFI_FILE_PROTOCOL
*This
,
549 StubFile
= STUB_FILE_FROM_FILE (This
);
551 if (StubFile
->BlobType
== KernelBlobTypeMax
) {
554 // rewinding a directory scan is allowed
556 StubFile
->Position
= 0;
559 return EFI_UNSUPPORTED
;
565 Blob
= &mKernelBlob
[StubFile
->BlobType
];
566 if (Position
== MAX_UINT64
) {
570 StubFile
->Position
= Blob
->Size
;
573 // absolute seek from beginning -- seeking past the end is allowed
575 StubFile
->Position
= Position
;
582 Returns information about a file.
584 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance
585 that is the file handle the requested
588 @param[in] InformationType The type identifier GUID for the information
589 being requested. The following information
590 types are supported, storing the
591 corresponding structures in Buffer:
593 - gEfiFileInfoGuid: EFI_FILE_INFO
595 - gEfiFileSystemInfoGuid:
598 - gEfiFileSystemVolumeLabelInfoIdGuid:
599 EFI_FILE_SYSTEM_VOLUME_LABEL
601 @param[in,out] BufferSize On input, the size of Buffer. On output, the
602 amount of data returned in Buffer. In both
603 cases, the size is measured in bytes.
605 @param[out] Buffer A pointer to the data buffer to return. The
606 buffer's type is indicated by
609 @retval EFI_SUCCESS The information was returned.
610 @retval EFI_UNSUPPORTED The InformationType is not known.
611 @retval EFI_NO_MEDIA The device has no medium.
612 @retval EFI_DEVICE_ERROR The device reported an error.
613 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
614 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the
615 information structure requested by
616 InformationType. BufferSize has been updated
617 with the size needed to complete the request.
623 IN EFI_FILE_PROTOCOL
*This
,
624 IN EFI_GUID
*InformationType
,
625 IN OUT UINTN
*BufferSize
,
629 CONST STUB_FILE
*StubFile
;
630 UINTN OriginalBufferSize
;
632 StubFile
= STUB_FILE_FROM_FILE (This
);
634 if (CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
635 return ConvertKernelBlobTypeToFileInfo (StubFile
->BlobType
, BufferSize
,
639 OriginalBufferSize
= *BufferSize
;
641 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
642 EFI_FILE_SYSTEM_INFO
*FileSystemInfo
;
644 *BufferSize
= sizeof *FileSystemInfo
;
645 if (OriginalBufferSize
< *BufferSize
) {
646 return EFI_BUFFER_TOO_SMALL
;
649 FileSystemInfo
= (EFI_FILE_SYSTEM_INFO
*)Buffer
;
650 FileSystemInfo
->Size
= sizeof *FileSystemInfo
;
651 FileSystemInfo
->ReadOnly
= TRUE
;
652 FileSystemInfo
->VolumeSize
= mTotalBlobBytes
;
653 FileSystemInfo
->FreeSpace
= 0;
654 FileSystemInfo
->BlockSize
= 1;
655 FileSystemInfo
->VolumeLabel
[0] = L
'\0';
660 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
661 EFI_FILE_SYSTEM_VOLUME_LABEL
*FileSystemVolumeLabel
;
663 *BufferSize
= sizeof *FileSystemVolumeLabel
;
664 if (OriginalBufferSize
< *BufferSize
) {
665 return EFI_BUFFER_TOO_SMALL
;
668 FileSystemVolumeLabel
= (EFI_FILE_SYSTEM_VOLUME_LABEL
*)Buffer
;
669 FileSystemVolumeLabel
->VolumeLabel
[0] = L
'\0';
674 return EFI_UNSUPPORTED
;
679 Sets information about a file.
681 @param[in] File A pointer to the EFI_FILE_PROTOCOL instance that
682 is the file handle the information is for.
684 @param[in] InformationType The type identifier for the information being
687 @param[in] BufferSize The size, in bytes, of Buffer.
689 @param[in] Buffer A pointer to the data buffer to write. The
690 buffer's type is indicated by InformationType.
692 @retval EFI_SUCCESS The information was set.
693 @retval EFI_UNSUPPORTED The InformationType is not known.
694 @retval EFI_NO_MEDIA The device has no medium.
695 @retval EFI_DEVICE_ERROR The device reported an error.
696 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
697 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the
699 @retval EFI_WRITE_PROTECTED InformationType is
700 EFI_FILE_PROTOCOL_SYSTEM_INFO_ID and the media
702 @retval EFI_WRITE_PROTECTED InformationType is
703 EFI_FILE_SYSTEM_VOLUME_LABEL_ID and the media
705 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file
706 to a file that is already present.
707 @retval EFI_ACCESS_DENIED An attempt is being made to change the
708 EFI_FILE_DIRECTORY Attribute.
709 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of
711 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the
712 file was opened read-only and an attempt is
713 being made to modify a field other than
715 @retval EFI_VOLUME_FULL The volume is full.
716 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type
717 indicated by InformationType.
723 IN EFI_FILE_PROTOCOL
*This
,
724 IN EFI_GUID
*InformationType
,
729 return EFI_WRITE_PROTECTED
;
734 Flushes all modified data associated with a file to a device.
736 @param [in] This A pointer to the EFI_FILE_PROTOCOL instance that is the
737 file handle to flush.
739 @retval EFI_SUCCESS The data was flushed.
740 @retval EFI_NO_MEDIA The device has no medium.
741 @retval EFI_DEVICE_ERROR The device reported an error.
742 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
743 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.
744 @retval EFI_ACCESS_DENIED The file was opened read-only.
745 @retval EFI_VOLUME_FULL The volume is full.
751 IN EFI_FILE_PROTOCOL
*This
754 return EFI_WRITE_PROTECTED
;
758 // External definition of the file protocol template.
760 STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate
= {
761 EFI_FILE_PROTOCOL_REVISION
, // revision 1
772 NULL
, // OpenEx, revision 2
773 NULL
, // ReadEx, revision 2
774 NULL
, // WriteEx, revision 2
775 NULL
// FlushEx, revision 2
780 // Protocol member functions for SimpleFileSystem.
784 Open the root directory on a volume.
786 @param[in] This A pointer to the volume to open the root directory on.
788 @param[out] Root A pointer to the location to return the opened file handle
789 for the root directory in.
791 @retval EFI_SUCCESS The device was opened.
792 @retval EFI_UNSUPPORTED This volume does not support the requested file
794 @retval EFI_NO_MEDIA The device has no medium.
795 @retval EFI_DEVICE_ERROR The device reported an error.
796 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
797 @retval EFI_ACCESS_DENIED The service denied access to the file.
798 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of
800 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
801 medium is no longer supported. Any existing
802 file handles for this volume are no longer
803 valid. To access the files on the new medium,
804 the volume must be reopened with OpenVolume().
809 StubFileSystemOpenVolume (
810 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
811 OUT EFI_FILE_PROTOCOL
**Root
816 StubFile
= AllocatePool (sizeof *StubFile
);
817 if (StubFile
== NULL
) {
818 return EFI_OUT_OF_RESOURCES
;
821 StubFile
->Signature
= STUB_FILE_SIG
;
822 StubFile
->BlobType
= KernelBlobTypeMax
;
823 StubFile
->Position
= 0;
824 CopyMem (&StubFile
->File
, &mEfiFileProtocolTemplate
,
825 sizeof mEfiFileProtocolTemplate
);
826 *Root
= &StubFile
->File
;
831 STATIC CONST EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mFileSystem
= {
832 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
,
833 StubFileSystemOpenVolume
838 // Utility functions.
842 Populate a blob in mKernelBlob.
844 param[in,out] Blob Pointer to the KERNEL_BLOB element in mKernelBlob that is
845 to be filled from fw_cfg.
847 @retval EFI_SUCCESS Blob has been populated. If fw_cfg reported a
848 size of zero for the blob, then Blob->Data has
851 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Blob->Data.
856 IN OUT KERNEL_BLOB
*Blob
864 QemuFwCfgSelectItem (Blob
->SizeKey
);
865 Blob
->Size
= QemuFwCfgRead32 ();
866 if (Blob
->Size
== 0) {
873 Blob
->Data
= AllocatePool (Blob
->Size
);
874 if (Blob
->Data
== NULL
) {
875 DEBUG ((EFI_D_ERROR
, "%a: failed to allocate %Ld bytes for \"%s\"\n",
876 __FUNCTION__
, (INT64
)Blob
->Size
, Blob
->Name
));
877 return EFI_OUT_OF_RESOURCES
;
880 DEBUG ((EFI_D_INFO
, "%a: loading %Ld bytes for \"%s\"\n", __FUNCTION__
,
881 (INT64
)Blob
->Size
, Blob
->Name
));
882 QemuFwCfgSelectItem (Blob
->DataKey
);
888 Chunk
= (Left
< SIZE_1MB
) ? Left
: SIZE_1MB
;
889 QemuFwCfgReadBytes (Chunk
, Blob
->Data
+ (Blob
->Size
- Left
));
891 DEBUG ((EFI_D_VERBOSE
, "%a: %Ld bytes remaining for \"%s\"\n",
892 __FUNCTION__
, (INT64
)Left
, Blob
->Name
));
899 // The entry point of the feature.
903 Download the kernel, the initial ramdisk, and the kernel command line from
904 QEMU's fw_cfg. Construct a minimal SimpleFileSystem that contains the two
905 image files, and load and start the kernel from it.
907 The kernel will be instructed via its command line to load the initrd from
908 the same Simple FileSystem.
910 @retval EFI_NOT_FOUND Kernel image was not found.
911 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
912 @retval EFI_PROTOCOL_ERROR Unterminated kernel command line.
914 @return Error codes from any of the underlying
915 functions. On success, the function doesn't
920 TryRunningQemuKernel (
925 KERNEL_BLOB
*CurrentBlob
;
926 KERNEL_BLOB
*KernelBlob
, *InitrdBlob
, *CommandLineBlob
;
928 EFI_HANDLE FileSystemHandle
;
929 EFI_DEVICE_PATH_PROTOCOL
*KernelDevicePath
;
930 EFI_HANDLE KernelImageHandle
;
931 EFI_LOADED_IMAGE_PROTOCOL
*KernelLoadedImage
;
933 Status
= gRT
->GetTime (&mInitTime
, NULL
/* Capabilities */);
934 if (EFI_ERROR (Status
)) {
935 DEBUG ((EFI_D_ERROR
, "%a: GetTime(): %r\n", __FUNCTION__
, Status
));
942 for (BlobType
= 0; BlobType
< KernelBlobTypeMax
; ++BlobType
) {
943 CurrentBlob
= &mKernelBlob
[BlobType
];
944 Status
= FetchBlob (CurrentBlob
);
945 if (EFI_ERROR (Status
)) {
948 mTotalBlobBytes
+= CurrentBlob
->Size
;
950 KernelBlob
= &mKernelBlob
[KernelBlobTypeKernel
];
951 InitrdBlob
= &mKernelBlob
[KernelBlobTypeInitrd
];
952 CommandLineBlob
= &mKernelBlob
[KernelBlobTypeCommandLine
];
954 if (KernelBlob
->Data
== NULL
) {
955 Status
= EFI_NOT_FOUND
;
960 // Create a new handle with a single VenHw() node device path protocol on it,
961 // plus a custom SimpleFileSystem protocol on it.
963 FileSystemHandle
= NULL
;
964 Status
= gBS
->InstallMultipleProtocolInterfaces (&FileSystemHandle
,
965 &gEfiDevicePathProtocolGuid
, &mFileSystemDevicePath
,
966 &gEfiSimpleFileSystemProtocolGuid
, &mFileSystem
,
968 if (EFI_ERROR (Status
)) {
969 DEBUG ((EFI_D_ERROR
, "%a: InstallMultipleProtocolInterfaces(): %r\n",
970 __FUNCTION__
, Status
));
975 // Create a device path for the kernel image to be loaded from that will call
976 // back into our file system.
978 KernelDevicePath
= FileDevicePath (FileSystemHandle
, KernelBlob
->Name
);
979 if (KernelDevicePath
== NULL
) {
980 DEBUG ((EFI_D_ERROR
, "%a: failed to allocate kernel device path\n",
982 Status
= EFI_OUT_OF_RESOURCES
;
983 goto UninstallProtocols
;
987 // Load the image. This should call back into our file system.
989 Status
= gBS
->LoadImage (
990 FALSE
, // BootPolicy: exact match required
991 gImageHandle
, // ParentImageHandle
993 NULL
, // SourceBuffer
997 if (EFI_ERROR (Status
)) {
998 DEBUG ((EFI_D_ERROR
, "%a: LoadImage(): %r\n", __FUNCTION__
, Status
));
999 goto FreeKernelDevicePath
;
1003 // Construct the kernel command line.
1005 Status
= gBS
->OpenProtocol (
1007 &gEfiLoadedImageProtocolGuid
,
1008 (VOID
**)&KernelLoadedImage
,
1009 gImageHandle
, // AgentHandle
1010 NULL
, // ControllerHandle
1011 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1013 ASSERT_EFI_ERROR (Status
);
1015 if (CommandLineBlob
->Data
== NULL
) {
1016 KernelLoadedImage
->LoadOptionsSize
= 0;
1019 // Verify NUL-termination of the command line.
1021 if (CommandLineBlob
->Data
[CommandLineBlob
->Size
- 1] != '\0') {
1022 DEBUG ((EFI_D_ERROR
, "%a: kernel command line is not NUL-terminated\n",
1024 Status
= EFI_PROTOCOL_ERROR
;
1025 goto UnloadKernelImage
;
1029 // Drop the terminating NUL, convert to UTF-16.
1031 KernelLoadedImage
->LoadOptionsSize
= (CommandLineBlob
->Size
- 1) * 2;
1034 if (InitrdBlob
->Data
!= NULL
) {
1036 // Append ' initrd=<name>' in UTF-16.
1038 KernelLoadedImage
->LoadOptionsSize
+=
1039 (8 + StrLen(InitrdBlob
->Name
)) * 2;
1042 if (KernelLoadedImage
->LoadOptionsSize
== 0) {
1043 KernelLoadedImage
->LoadOptions
= NULL
;
1046 // NUL-terminate in UTF-16.
1048 KernelLoadedImage
->LoadOptionsSize
+= 2;
1050 KernelLoadedImage
->LoadOptions
= AllocatePool (
1051 KernelLoadedImage
->LoadOptionsSize
);
1052 if (KernelLoadedImage
->LoadOptions
== NULL
) {
1053 KernelLoadedImage
->LoadOptionsSize
= 0;
1054 Status
= EFI_OUT_OF_RESOURCES
;
1055 goto UnloadKernelImage
;
1058 UnicodeSPrintAsciiFormat (
1059 KernelLoadedImage
->LoadOptions
,
1060 KernelLoadedImage
->LoadOptionsSize
,
1062 (CommandLineBlob
->Data
== NULL
) ? "" : (CHAR8
*)CommandLineBlob
->Data
,
1063 (InitrdBlob
->Data
== NULL
) ? "" : " initrd=",
1064 (InitrdBlob
->Data
== NULL
) ? L
"" : InitrdBlob
->Name
1066 DEBUG ((EFI_D_INFO
, "%a: command line: \"%s\"\n", __FUNCTION__
,
1067 (CHAR16
*)KernelLoadedImage
->LoadOptions
));
1071 // Signal the EFI_EVENT_GROUP_READY_TO_BOOT event.
1073 EfiSignalEventReadyToBoot();
1078 Status
= gBS
->StartImage (
1080 NULL
, // ExitDataSize
1083 if (EFI_ERROR (Status
)) {
1084 DEBUG ((EFI_D_ERROR
, "%a: StartImage(): %r\n", __FUNCTION__
, Status
));
1087 if (KernelLoadedImage
->LoadOptions
!= NULL
) {
1088 FreePool (KernelLoadedImage
->LoadOptions
);
1090 KernelLoadedImage
->LoadOptionsSize
= 0;
1093 gBS
->UnloadImage (KernelImageHandle
);
1095 FreeKernelDevicePath
:
1096 FreePool (KernelDevicePath
);
1099 gBS
->UninstallMultipleProtocolInterfaces (FileSystemHandle
,
1100 &gEfiSimpleFileSystemProtocolGuid
, &mFileSystem
,
1101 &gEfiDevicePathProtocolGuid
, &mFileSystemDevicePath
,
1105 while (BlobType
> 0) {
1106 CurrentBlob
= &mKernelBlob
[--BlobType
];
1107 if (CurrentBlob
->Data
!= NULL
) {
1108 FreePool (CurrentBlob
->Data
);
1109 CurrentBlob
->Size
= 0;
1110 CurrentBlob
->Data
= NULL
;