]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/PlatformBootManagerLib/QemuKernel.c
ArmVirtPkg: duplicate PlatformIntelBdsLib to PlatformBootManagerLib
[mirror_edk2.git] / ArmVirtPkg / Library / PlatformBootManagerLib / QemuKernel.c
CommitLineData
8d620322
LE
1/** @file\r
2 Try to load an EFI-stubbed ARM Linux kernel from QEMU's fw_cfg.\r
3\r
4 This implementation differs from OvmfPkg/Library/LoadLinuxLib. An EFI\r
5 stub in the subject kernel is a hard requirement here.\r
6\r
7 Copyright (C) 2014-2016, Red Hat, Inc.\r
8\r
9 This program and the accompanying materials are licensed and made available\r
10 under the terms and conditions of the BSD License which accompanies this\r
11 distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16**/\r
17\r
18#include <Guid/FileInfo.h>\r
19#include <Guid/FileSystemInfo.h>\r
20#include <Guid/FileSystemVolumeLabelInfo.h>\r
21#include <Library/PrintLib.h>\r
22#include <Library/QemuFwCfgLib.h>\r
23#include <Protocol/DevicePath.h>\r
24#include <Protocol/LoadedImage.h>\r
25#include <Protocol/SimpleFileSystem.h>\r
26\r
27#include "PlatformBm.h"\r
28\r
29//\r
30// Static data that hosts the fw_cfg blobs and serves file requests.\r
31//\r
32typedef enum {\r
33 KernelBlobTypeKernel,\r
34 KernelBlobTypeInitrd,\r
35 KernelBlobTypeCommandLine,\r
36 KernelBlobTypeMax\r
37} KERNEL_BLOB_TYPE;\r
38\r
39typedef struct {\r
40 FIRMWARE_CONFIG_ITEM CONST SizeKey;\r
41 FIRMWARE_CONFIG_ITEM CONST DataKey;\r
42 CONST CHAR16 * CONST Name;\r
43 UINT32 Size;\r
44 UINT8 *Data;\r
45} KERNEL_BLOB;\r
46\r
47STATIC KERNEL_BLOB mKernelBlob[KernelBlobTypeMax] = {\r
48 { QemuFwCfgItemKernelSize, QemuFwCfgItemKernelData, L"kernel" },\r
49 { QemuFwCfgItemInitrdSize, QemuFwCfgItemInitrdData, L"initrd" },\r
50 { QemuFwCfgItemCommandLineSize, QemuFwCfgItemCommandLineData, L"cmdline" }\r
51};\r
52\r
53STATIC UINT64 mTotalBlobBytes;\r
54\r
55//\r
56// Device path for the handle that incorporates our "EFI stub filesystem". The\r
57// GUID is arbitrary and need not be standardized or advertized.\r
58//\r
59#pragma pack(1)\r
60typedef struct {\r
61 VENDOR_DEVICE_PATH VenHwNode;\r
62 EFI_DEVICE_PATH_PROTOCOL EndNode;\r
63} SINGLE_VENHW_NODE_DEVPATH;\r
64#pragma pack()\r
65\r
66STATIC CONST SINGLE_VENHW_NODE_DEVPATH mFileSystemDevicePath = {\r
67 {\r
68 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH) } },\r
69 {\r
70 0xb0fae7e7, 0x6b07, 0x49d0,\r
71 { 0x9e, 0x5b, 0x3b, 0xde, 0xc8, 0x3b, 0x03, 0x9d }\r
72 }\r
73 },\r
74\r
75 {\r
76 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
77 { sizeof (EFI_DEVICE_PATH_PROTOCOL) }\r
78 }\r
79};\r
80\r
81//\r
82// The "file in the EFI stub filesystem" abstraction.\r
83//\r
84STATIC EFI_TIME mInitTime;\r
85\r
86#define STUB_FILE_SIG SIGNATURE_64 ('S', 'T', 'U', 'B', 'F', 'I', 'L', 'E')\r
87\r
88typedef struct {\r
89 UINT64 Signature; // Carries STUB_FILE_SIG.\r
90\r
91 KERNEL_BLOB_TYPE BlobType; // Index into mKernelBlob. KernelBlobTypeMax\r
92 // denotes the root directory of the filesystem.\r
93\r
94 UINT64 Position; // Byte position for regular files;\r
95 // next directory entry to return for the root\r
96 // directory.\r
97\r
98 EFI_FILE_PROTOCOL File; // Standard protocol interface.\r
99} STUB_FILE;\r
100\r
101#define STUB_FILE_FROM_FILE(FilePointer) \\r
102 CR (FilePointer, STUB_FILE, File, STUB_FILE_SIG)\r
103\r
104//\r
105// Tentative definition of the file protocol template. The initializer\r
106// (external definition) will be provided later.\r
107//\r
108STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate;\r
109\r
110\r
111//\r
112// Protocol member functions for File.\r
113//\r
114\r
115/**\r
116 Opens a new file relative to the source file's location.\r
117\r
118 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is\r
119 the file handle to the source location. This would\r
120 typically be an open handle to a directory.\r
121\r
122 @param[out] NewHandle A pointer to the location to return the opened handle\r
123 for the new file.\r
124\r
125 @param[in] FileName The Null-terminated string of the name of the file to\r
126 be opened. The file name may contain the following\r
127 path modifiers: "\", ".", and "..".\r
128\r
129 @param[in] OpenMode The mode to open the file. The only valid\r
130 combinations that the file may be opened with are:\r
131 Read, Read/Write, or Create/Read/Write.\r
132\r
133 @param[in] Attributes Only valid for EFI_FILE_MODE_CREATE, in which case\r
134 these are the attribute bits for the newly created\r
135 file.\r
136\r
137 @retval EFI_SUCCESS The file was opened.\r
138 @retval EFI_NOT_FOUND The specified file could not be found on the\r
139 device.\r
140 @retval EFI_NO_MEDIA The device has no medium.\r
141 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
142 medium is no longer supported.\r
143 @retval EFI_DEVICE_ERROR The device reported an error.\r
144 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
145 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a\r
146 file for write when the media is\r
147 write-protected.\r
148 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
149 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
150 file.\r
151 @retval EFI_VOLUME_FULL The volume is full.\r
152**/\r
153STATIC\r
154EFI_STATUS\r
155EFIAPI\r
156StubFileOpen (\r
157 IN EFI_FILE_PROTOCOL *This,\r
158 OUT EFI_FILE_PROTOCOL **NewHandle,\r
159 IN CHAR16 *FileName,\r
160 IN UINT64 OpenMode,\r
161 IN UINT64 Attributes\r
162 )\r
163{\r
164 CONST STUB_FILE *StubFile;\r
165 UINTN BlobType;\r
166 STUB_FILE *NewStubFile;\r
167\r
168 //\r
169 // We're read-only.\r
170 //\r
171 switch (OpenMode) {\r
172 case EFI_FILE_MODE_READ:\r
173 break;\r
174\r
175 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
176 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:\r
177 return EFI_WRITE_PROTECTED;\r
178\r
179 default:\r
180 return EFI_INVALID_PARAMETER;\r
181 }\r
182\r
183 //\r
184 // Only the root directory supports opening files in it.\r
185 //\r
186 StubFile = STUB_FILE_FROM_FILE (This);\r
187 if (StubFile->BlobType != KernelBlobTypeMax) {\r
188 return EFI_UNSUPPORTED;\r
189 }\r
190\r
191 //\r
192 // Locate the file.\r
193 //\r
194 for (BlobType = 0; BlobType < KernelBlobTypeMax; ++BlobType) {\r
195 if (StrCmp (FileName, mKernelBlob[BlobType].Name) == 0) {\r
196 break;\r
197 }\r
198 }\r
199 if (BlobType == KernelBlobTypeMax) {\r
200 return EFI_NOT_FOUND;\r
201 }\r
202\r
203 //\r
204 // Found it.\r
205 //\r
206 NewStubFile = AllocatePool (sizeof *NewStubFile);\r
207 if (NewStubFile == NULL) {\r
208 return EFI_OUT_OF_RESOURCES;\r
209 }\r
210\r
211 NewStubFile->Signature = STUB_FILE_SIG;\r
212 NewStubFile->BlobType = (KERNEL_BLOB_TYPE)BlobType;\r
213 NewStubFile->Position = 0;\r
214 CopyMem (&NewStubFile->File, &mEfiFileProtocolTemplate,\r
215 sizeof mEfiFileProtocolTemplate);\r
216 *NewHandle = &NewStubFile->File;\r
217\r
218 return EFI_SUCCESS;\r
219}\r
220\r
221\r
222/**\r
223 Closes a specified file handle.\r
224\r
225 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
226 handle to close.\r
227\r
228 @retval EFI_SUCCESS The file was closed.\r
229**/\r
230STATIC\r
231EFI_STATUS\r
232EFIAPI\r
233StubFileClose (\r
234 IN EFI_FILE_PROTOCOL *This\r
235 )\r
236{\r
237 FreePool (STUB_FILE_FROM_FILE (This));\r
238 return EFI_SUCCESS;\r
239}\r
240\r
241\r
242/**\r
243 Close and delete the file handle.\r
244\r
245 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
246 handle to the file to delete.\r
247\r
248 @retval EFI_SUCCESS The file was closed and deleted, and the\r
249 handle was closed.\r
250 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not\r
251 deleted.\r
252\r
253**/\r
254STATIC\r
255EFI_STATUS\r
256EFIAPI\r
257StubFileDelete (\r
258 IN EFI_FILE_PROTOCOL *This\r
259 )\r
260{\r
261 FreePool (STUB_FILE_FROM_FILE (This));\r
262 return EFI_WARN_DELETE_FAILURE;\r
263}\r
264\r
265\r
266/**\r
267 Helper function that formats an EFI_FILE_INFO structure into the\r
268 user-allocated buffer, for any valid KERNEL_BLOB_TYPE value (including\r
269 KernelBlobTypeMax, which stands for the root directory).\r
270\r
271 The interface follows the EFI_FILE_GET_INFO -- and for directories, the\r
272 EFI_FILE_READ -- interfaces.\r
273\r
274 @param[in] BlobType The KERNEL_BLOB_TYPE value identifying the fw_cfg\r
275 blob backing the STUB_FILE that information is\r
276 being requested about. If BlobType equals\r
277 KernelBlobTypeMax, then information will be\r
278 provided about the root directory of the\r
279 filesystem.\r
280\r
281 @param[in,out] BufferSize On input, the size of Buffer. On output, the\r
282 amount of data returned in Buffer. In both cases,\r
283 the size is measured in bytes.\r
284\r
285 @param[out] Buffer A pointer to the data buffer to return. The\r
286 buffer's type is EFI_FILE_INFO.\r
287\r
288 @retval EFI_SUCCESS The information was returned.\r
289 @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to store the\r
290 EFI_FILE_INFO structure. BufferSize has been\r
291 updated with the size needed to complete the\r
292 request.\r
293**/\r
294STATIC\r
295EFI_STATUS\r
296ConvertKernelBlobTypeToFileInfo (\r
297 IN KERNEL_BLOB_TYPE BlobType,\r
298 IN OUT UINTN *BufferSize,\r
299 OUT VOID *Buffer\r
300 )\r
301{\r
302 CONST CHAR16 *Name;\r
303 UINT64 FileSize;\r
304 UINT64 Attribute;\r
305\r
306 UINTN NameSize;\r
307 UINTN FileInfoSize;\r
308 EFI_FILE_INFO *FileInfo;\r
309 UINTN OriginalBufferSize;\r
310\r
311 if (BlobType == KernelBlobTypeMax) {\r
312 //\r
313 // getting file info about the root directory\r
314 //\r
315 Name = L"\\";\r
316 FileSize = KernelBlobTypeMax;\r
317 Attribute = EFI_FILE_READ_ONLY | EFI_FILE_DIRECTORY;\r
318 } else {\r
319 CONST KERNEL_BLOB *Blob;\r
320\r
321 Blob = &mKernelBlob[BlobType];\r
322 Name = Blob->Name;\r
323 FileSize = Blob->Size;\r
324 Attribute = EFI_FILE_READ_ONLY;\r
325 }\r
326\r
327 NameSize = (StrLen(Name) + 1) * 2;\r
328 FileInfoSize = OFFSET_OF (EFI_FILE_INFO, FileName) + NameSize;\r
329 ASSERT (FileInfoSize >= sizeof *FileInfo);\r
330\r
331 OriginalBufferSize = *BufferSize;\r
332 *BufferSize = FileInfoSize;\r
333 if (OriginalBufferSize < *BufferSize) {\r
334 return EFI_BUFFER_TOO_SMALL;\r
335 }\r
336\r
337 FileInfo = (EFI_FILE_INFO *)Buffer;\r
338 FileInfo->Size = FileInfoSize;\r
339 FileInfo->FileSize = FileSize;\r
340 FileInfo->PhysicalSize = FileSize;\r
341 FileInfo->Attribute = Attribute;\r
342\r
343 CopyMem (&FileInfo->CreateTime, &mInitTime, sizeof mInitTime);\r
344 CopyMem (&FileInfo->LastAccessTime, &mInitTime, sizeof mInitTime);\r
345 CopyMem (&FileInfo->ModificationTime, &mInitTime, sizeof mInitTime);\r
346 CopyMem (FileInfo->FileName, Name, NameSize);\r
347\r
348 return EFI_SUCCESS;\r
349}\r
350\r
351\r
352/**\r
353 Reads data from a file, or continues scanning a directory.\r
354\r
355 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that\r
356 is the file handle to read data from.\r
357\r
358 @param[in,out] BufferSize On input, the size of the Buffer. On output, the\r
359 amount of data returned in Buffer. In both cases,\r
360 the size is measured in bytes. If the read goes\r
361 beyond the end of the file, the read length is\r
362 truncated to the end of the file.\r
363\r
364 If This is a directory, the function reads the\r
365 directory entry at the current position and\r
366 returns the entry (as EFI_FILE_INFO) in Buffer. If\r
367 there are no more directory entries, the\r
368 BufferSize is set to zero on output.\r
369\r
370 @param[out] Buffer The buffer into which the data is read.\r
371\r
372 @retval EFI_SUCCESS Data was read.\r
373 @retval EFI_NO_MEDIA The device has no medium.\r
374 @retval EFI_DEVICE_ERROR The device reported an error.\r
375 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted\r
376 file.\r
377 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond\r
378 the end of the file.\r
379 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
380 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the\r
381 current directory entry as a EFI_FILE_INFO\r
382 structure. BufferSize has been updated with the\r
383 size needed to complete the request, and the\r
384 directory position has not been advanced.\r
385**/\r
386STATIC\r
387EFI_STATUS\r
388EFIAPI\r
389StubFileRead (\r
390 IN EFI_FILE_PROTOCOL *This,\r
391 IN OUT UINTN *BufferSize,\r
392 OUT VOID *Buffer\r
393 )\r
394{\r
395 STUB_FILE *StubFile;\r
396 CONST KERNEL_BLOB *Blob;\r
397 UINT64 Left;\r
398\r
399 StubFile = STUB_FILE_FROM_FILE (This);\r
400\r
401 //\r
402 // Scanning the root directory?\r
403 //\r
404 if (StubFile->BlobType == KernelBlobTypeMax) {\r
405 EFI_STATUS Status;\r
406\r
407 if (StubFile->Position == KernelBlobTypeMax) {\r
408 //\r
409 // Scanning complete.\r
410 //\r
411 *BufferSize = 0;\r
412 return EFI_SUCCESS;\r
413 }\r
414\r
415 Status = ConvertKernelBlobTypeToFileInfo (\r
416 (KERNEL_BLOB_TYPE)StubFile->Position,\r
417 BufferSize,\r
418 Buffer);\r
419 if (EFI_ERROR (Status)) {\r
420 return Status;\r
421 }\r
422\r
423 ++StubFile->Position;\r
424 return EFI_SUCCESS;\r
425 }\r
426\r
427 //\r
428 // Reading a file.\r
429 //\r
430 Blob = &mKernelBlob[StubFile->BlobType];\r
431 if (StubFile->Position > Blob->Size) {\r
432 return EFI_DEVICE_ERROR;\r
433 }\r
434\r
435 Left = Blob->Size - StubFile->Position;\r
436 if (*BufferSize > Left) {\r
437 *BufferSize = (UINTN)Left;\r
438 }\r
439 if (Blob->Data != NULL) {\r
440 CopyMem (Buffer, Blob->Data + StubFile->Position, *BufferSize);\r
441 }\r
442 StubFile->Position += *BufferSize;\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446\r
447/**\r
448 Writes data to a file.\r
449\r
450 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that\r
451 is the file handle to write data to.\r
452\r
453 @param[in,out] BufferSize On input, the size of the Buffer. On output, the\r
454 amount of data actually written. In both cases,\r
455 the size is measured in bytes.\r
456\r
457 @param[in] Buffer The buffer of data to write.\r
458\r
459 @retval EFI_SUCCESS Data was written.\r
460 @retval EFI_UNSUPPORTED Writes to open directory files are not\r
461 supported.\r
462 @retval EFI_NO_MEDIA The device has no medium.\r
463 @retval EFI_DEVICE_ERROR The device reported an error.\r
464 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.\r
465 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
466 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
467 @retval EFI_ACCESS_DENIED The file was opened read only.\r
468 @retval EFI_VOLUME_FULL The volume is full.\r
469**/\r
470STATIC\r
471EFI_STATUS\r
472EFIAPI\r
473StubFileWrite (\r
474 IN EFI_FILE_PROTOCOL *This,\r
475 IN OUT UINTN *BufferSize,\r
476 IN VOID *Buffer\r
477 )\r
478{\r
479 STUB_FILE *StubFile;\r
480\r
481 StubFile = STUB_FILE_FROM_FILE (This);\r
482 return (StubFile->BlobType == KernelBlobTypeMax) ?\r
483 EFI_UNSUPPORTED :\r
484 EFI_WRITE_PROTECTED;\r
485}\r
486\r
487\r
488/**\r
489 Returns a file's current position.\r
490\r
491 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
492 file handle to get the current position on.\r
493\r
494 @param[out] Position The address to return the file's current position\r
495 value.\r
496\r
497 @retval EFI_SUCCESS The position was returned.\r
498 @retval EFI_UNSUPPORTED The request is not valid on open directories.\r
499 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a\r
500 deleted file.\r
501**/\r
502STATIC\r
503EFI_STATUS\r
504EFIAPI\r
505StubFileGetPosition (\r
506 IN EFI_FILE_PROTOCOL *This,\r
507 OUT UINT64 *Position\r
508 )\r
509{\r
510 STUB_FILE *StubFile;\r
511\r
512 StubFile = STUB_FILE_FROM_FILE (This);\r
513 if (StubFile->BlobType == KernelBlobTypeMax) {\r
514 return EFI_UNSUPPORTED;\r
515 }\r
516\r
517 *Position = StubFile->Position;\r
518 return EFI_SUCCESS;\r
519}\r
520\r
521\r
522/**\r
523 Sets a file's current position.\r
524\r
525 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
526 file handle to set the requested position on.\r
527\r
528 @param[in] Position The byte position from the start of the file to set. For\r
529 regular files, MAX_UINT64 means "seek to end". For\r
530 directories, zero means "rewind directory scan".\r
531\r
532 @retval EFI_SUCCESS The position was set.\r
533 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open\r
534 directories.\r
535 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a\r
536 deleted file.\r
537**/\r
538STATIC\r
539EFI_STATUS\r
540EFIAPI\r
541StubFileSetPosition (\r
542 IN EFI_FILE_PROTOCOL *This,\r
543 IN UINT64 Position\r
544 )\r
545{\r
546 STUB_FILE *StubFile;\r
547 KERNEL_BLOB *Blob;\r
548\r
549 StubFile = STUB_FILE_FROM_FILE (This);\r
550\r
551 if (StubFile->BlobType == KernelBlobTypeMax) {\r
552 if (Position == 0) {\r
553 //\r
554 // rewinding a directory scan is allowed\r
555 //\r
556 StubFile->Position = 0;\r
557 return EFI_SUCCESS;\r
558 }\r
559 return EFI_UNSUPPORTED;\r
560 }\r
561\r
562 //\r
563 // regular file seek\r
564 //\r
565 Blob = &mKernelBlob[StubFile->BlobType];\r
566 if (Position == MAX_UINT64) {\r
567 //\r
568 // seek to end\r
569 //\r
570 StubFile->Position = Blob->Size;\r
571 } else {\r
572 //\r
573 // absolute seek from beginning -- seeking past the end is allowed\r
574 //\r
575 StubFile->Position = Position;\r
576 }\r
577 return EFI_SUCCESS;\r
578}\r
579\r
580\r
581/**\r
582 Returns information about a file.\r
583\r
584 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance\r
585 that is the file handle the requested\r
586 information is for.\r
587\r
588 @param[in] InformationType The type identifier GUID for the information\r
589 being requested. The following information\r
590 types are supported, storing the\r
591 corresponding structures in Buffer:\r
592\r
593 - gEfiFileInfoGuid: EFI_FILE_INFO\r
594\r
595 - gEfiFileSystemInfoGuid:\r
596 EFI_FILE_SYSTEM_INFO\r
597\r
598 - gEfiFileSystemVolumeLabelInfoIdGuid:\r
599 EFI_FILE_SYSTEM_VOLUME_LABEL\r
600\r
601 @param[in,out] BufferSize On input, the size of Buffer. On output, the\r
602 amount of data returned in Buffer. In both\r
603 cases, the size is measured in bytes.\r
604\r
605 @param[out] Buffer A pointer to the data buffer to return. The\r
606 buffer's type is indicated by\r
607 InformationType.\r
608\r
609 @retval EFI_SUCCESS The information was returned.\r
610 @retval EFI_UNSUPPORTED The InformationType is not known.\r
611 @retval EFI_NO_MEDIA The device has no medium.\r
612 @retval EFI_DEVICE_ERROR The device reported an error.\r
613 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
614 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to store the\r
615 information structure requested by\r
616 InformationType. BufferSize has been updated\r
617 with the size needed to complete the request.\r
618**/\r
619STATIC\r
620EFI_STATUS\r
621EFIAPI\r
622StubFileGetInfo (\r
623 IN EFI_FILE_PROTOCOL *This,\r
624 IN EFI_GUID *InformationType,\r
625 IN OUT UINTN *BufferSize,\r
626 OUT VOID *Buffer\r
627 )\r
628{\r
629 CONST STUB_FILE *StubFile;\r
630 UINTN OriginalBufferSize;\r
631\r
632 StubFile = STUB_FILE_FROM_FILE (This);\r
633\r
634 if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
635 return ConvertKernelBlobTypeToFileInfo (StubFile->BlobType, BufferSize,\r
636 Buffer);\r
637 }\r
638\r
639 OriginalBufferSize = *BufferSize;\r
640\r
641 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
642 EFI_FILE_SYSTEM_INFO *FileSystemInfo;\r
643\r
644 *BufferSize = sizeof *FileSystemInfo;\r
645 if (OriginalBufferSize < *BufferSize) {\r
646 return EFI_BUFFER_TOO_SMALL;\r
647 }\r
648\r
649 FileSystemInfo = (EFI_FILE_SYSTEM_INFO *)Buffer;\r
650 FileSystemInfo->Size = sizeof *FileSystemInfo;\r
651 FileSystemInfo->ReadOnly = TRUE;\r
652 FileSystemInfo->VolumeSize = mTotalBlobBytes;\r
653 FileSystemInfo->FreeSpace = 0;\r
654 FileSystemInfo->BlockSize = 1;\r
655 FileSystemInfo->VolumeLabel[0] = L'\0';\r
656\r
657 return EFI_SUCCESS;\r
658 }\r
659\r
660 if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
661 EFI_FILE_SYSTEM_VOLUME_LABEL *FileSystemVolumeLabel;\r
662\r
663 *BufferSize = sizeof *FileSystemVolumeLabel;\r
664 if (OriginalBufferSize < *BufferSize) {\r
665 return EFI_BUFFER_TOO_SMALL;\r
666 }\r
667\r
668 FileSystemVolumeLabel = (EFI_FILE_SYSTEM_VOLUME_LABEL *)Buffer;\r
669 FileSystemVolumeLabel->VolumeLabel[0] = L'\0';\r
670\r
671 return EFI_SUCCESS;\r
672 }\r
673\r
674 return EFI_UNSUPPORTED;\r
675}\r
676\r
677\r
678/**\r
679 Sets information about a file.\r
680\r
681 @param[in] File A pointer to the EFI_FILE_PROTOCOL instance that\r
682 is the file handle the information is for.\r
683\r
684 @param[in] InformationType The type identifier for the information being\r
685 set.\r
686\r
687 @param[in] BufferSize The size, in bytes, of Buffer.\r
688\r
689 @param[in] Buffer A pointer to the data buffer to write. The\r
690 buffer's type is indicated by InformationType.\r
691\r
692 @retval EFI_SUCCESS The information was set.\r
693 @retval EFI_UNSUPPORTED The InformationType is not known.\r
694 @retval EFI_NO_MEDIA The device has no medium.\r
695 @retval EFI_DEVICE_ERROR The device reported an error.\r
696 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
697 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the\r
698 media is read-only.\r
699 @retval EFI_WRITE_PROTECTED InformationType is\r
700 EFI_FILE_PROTOCOL_SYSTEM_INFO_ID and the media\r
701 is read only.\r
702 @retval EFI_WRITE_PROTECTED InformationType is\r
703 EFI_FILE_SYSTEM_VOLUME_LABEL_ID and the media\r
704 is read-only.\r
705 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file\r
706 to a file that is already present.\r
707 @retval EFI_ACCESS_DENIED An attempt is being made to change the\r
708 EFI_FILE_DIRECTORY Attribute.\r
709 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of\r
710 a directory.\r
711 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the\r
712 file was opened read-only and an attempt is\r
713 being made to modify a field other than\r
714 Attribute.\r
715 @retval EFI_VOLUME_FULL The volume is full.\r
716 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type\r
717 indicated by InformationType.\r
718**/\r
719STATIC\r
720EFI_STATUS\r
721EFIAPI\r
722StubFileSetInfo (\r
723 IN EFI_FILE_PROTOCOL *This,\r
724 IN EFI_GUID *InformationType,\r
725 IN UINTN BufferSize,\r
726 IN VOID *Buffer\r
727 )\r
728{\r
729 return EFI_WRITE_PROTECTED;\r
730}\r
731\r
732\r
733/**\r
734 Flushes all modified data associated with a file to a device.\r
735\r
736 @param [in] This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
737 file handle to flush.\r
738\r
739 @retval EFI_SUCCESS The data was flushed.\r
740 @retval EFI_NO_MEDIA The device has no medium.\r
741 @retval EFI_DEVICE_ERROR The device reported an error.\r
742 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
743 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
744 @retval EFI_ACCESS_DENIED The file was opened read-only.\r
745 @retval EFI_VOLUME_FULL The volume is full.\r
746**/\r
747STATIC\r
748EFI_STATUS\r
749EFIAPI\r
750StubFileFlush (\r
751 IN EFI_FILE_PROTOCOL *This\r
752 )\r
753{\r
754 return EFI_WRITE_PROTECTED;\r
755}\r
756\r
757//\r
758// External definition of the file protocol template.\r
759//\r
760STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate = {\r
761 EFI_FILE_PROTOCOL_REVISION, // revision 1\r
762 StubFileOpen,\r
763 StubFileClose,\r
764 StubFileDelete,\r
765 StubFileRead,\r
766 StubFileWrite,\r
767 StubFileGetPosition,\r
768 StubFileSetPosition,\r
769 StubFileGetInfo,\r
770 StubFileSetInfo,\r
771 StubFileFlush,\r
772 NULL, // OpenEx, revision 2\r
773 NULL, // ReadEx, revision 2\r
774 NULL, // WriteEx, revision 2\r
775 NULL // FlushEx, revision 2\r
776};\r
777\r
778\r
779//\r
780// Protocol member functions for SimpleFileSystem.\r
781//\r
782\r
783/**\r
784 Open the root directory on a volume.\r
785\r
786 @param[in] This A pointer to the volume to open the root directory on.\r
787\r
788 @param[out] Root A pointer to the location to return the opened file handle\r
789 for the root directory in.\r
790\r
791 @retval EFI_SUCCESS The device was opened.\r
792 @retval EFI_UNSUPPORTED This volume does not support the requested file\r
793 system type.\r
794 @retval EFI_NO_MEDIA The device has no medium.\r
795 @retval EFI_DEVICE_ERROR The device reported an error.\r
796 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
797 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
798 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of\r
799 resources.\r
800 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
801 medium is no longer supported. Any existing\r
802 file handles for this volume are no longer\r
803 valid. To access the files on the new medium,\r
804 the volume must be reopened with OpenVolume().\r
805**/\r
806STATIC\r
807EFI_STATUS\r
808EFIAPI\r
809StubFileSystemOpenVolume (\r
810 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,\r
811 OUT EFI_FILE_PROTOCOL **Root\r
812 )\r
813{\r
814 STUB_FILE *StubFile;\r
815\r
816 StubFile = AllocatePool (sizeof *StubFile);\r
817 if (StubFile == NULL) {\r
818 return EFI_OUT_OF_RESOURCES;\r
819 }\r
820\r
821 StubFile->Signature = STUB_FILE_SIG;\r
822 StubFile->BlobType = KernelBlobTypeMax;\r
823 StubFile->Position = 0;\r
824 CopyMem (&StubFile->File, &mEfiFileProtocolTemplate,\r
825 sizeof mEfiFileProtocolTemplate);\r
826 *Root = &StubFile->File;\r
827\r
828 return EFI_SUCCESS;\r
829}\r
830\r
831STATIC CONST EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mFileSystem = {\r
832 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION,\r
833 StubFileSystemOpenVolume\r
834};\r
835\r
836\r
837//\r
838// Utility functions.\r
839//\r
840\r
841/**\r
842 Populate a blob in mKernelBlob.\r
843\r
844 param[in,out] Blob Pointer to the KERNEL_BLOB element in mKernelBlob that is\r
845 to be filled from fw_cfg.\r
846\r
847 @retval EFI_SUCCESS Blob has been populated. If fw_cfg reported a\r
848 size of zero for the blob, then Blob->Data has\r
849 been left unchanged.\r
850\r
851 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Blob->Data.\r
852**/\r
853STATIC\r
854EFI_STATUS\r
855FetchBlob (\r
856 IN OUT KERNEL_BLOB *Blob\r
857 )\r
858{\r
859 UINT32 Left;\r
860\r
861 //\r
862 // Read blob size.\r
863 //\r
864 QemuFwCfgSelectItem (Blob->SizeKey);\r
865 Blob->Size = QemuFwCfgRead32 ();\r
866 if (Blob->Size == 0) {\r
867 return EFI_SUCCESS;\r
868 }\r
869\r
870 //\r
871 // Read blob.\r
872 //\r
873 Blob->Data = AllocatePool (Blob->Size);\r
874 if (Blob->Data == NULL) {\r
875 DEBUG ((EFI_D_ERROR, "%a: failed to allocate %Ld bytes for \"%s\"\n",\r
876 __FUNCTION__, (INT64)Blob->Size, Blob->Name));\r
877 return EFI_OUT_OF_RESOURCES;\r
878 }\r
879\r
880 DEBUG ((EFI_D_INFO, "%a: loading %Ld bytes for \"%s\"\n", __FUNCTION__,\r
881 (INT64)Blob->Size, Blob->Name));\r
882 QemuFwCfgSelectItem (Blob->DataKey);\r
883\r
884 Left = Blob->Size;\r
885 do {\r
886 UINT32 Chunk;\r
887\r
888 Chunk = (Left < SIZE_1MB) ? Left : SIZE_1MB;\r
889 QemuFwCfgReadBytes (Chunk, Blob->Data + (Blob->Size - Left));\r
890 Left -= Chunk;\r
891 DEBUG ((EFI_D_VERBOSE, "%a: %Ld bytes remaining for \"%s\"\n",\r
892 __FUNCTION__, (INT64)Left, Blob->Name));\r
893 } while (Left > 0);\r
894 return EFI_SUCCESS;\r
895}\r
896\r
897\r
898//\r
899// The entry point of the feature.\r
900//\r
901\r
902/**\r
903 Download the kernel, the initial ramdisk, and the kernel command line from\r
904 QEMU's fw_cfg. Construct a minimal SimpleFileSystem that contains the two\r
905 image files, and load and start the kernel from it.\r
906\r
907 The kernel will be instructed via its command line to load the initrd from\r
908 the same Simple FileSystem.\r
909\r
910 @retval EFI_NOT_FOUND Kernel image was not found.\r
911 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
912 @retval EFI_PROTOCOL_ERROR Unterminated kernel command line.\r
913\r
914 @return Error codes from any of the underlying\r
915 functions. On success, the function doesn't\r
916 return.\r
917**/\r
918EFI_STATUS\r
919EFIAPI\r
920TryRunningQemuKernel (\r
921 VOID\r
922 )\r
923{\r
924 UINTN BlobType;\r
925 KERNEL_BLOB *CurrentBlob;\r
926 KERNEL_BLOB *KernelBlob, *InitrdBlob, *CommandLineBlob;\r
927 EFI_STATUS Status;\r
928 EFI_HANDLE FileSystemHandle;\r
929 EFI_DEVICE_PATH_PROTOCOL *KernelDevicePath;\r
930 EFI_HANDLE KernelImageHandle;\r
931 EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage;\r
932\r
933 Status = gRT->GetTime (&mInitTime, NULL /* Capabilities */);\r
934 if (EFI_ERROR (Status)) {\r
935 DEBUG ((EFI_D_ERROR, "%a: GetTime(): %r\n", __FUNCTION__, Status));\r
936 return Status;\r
937 }\r
938\r
939 //\r
940 // Fetch all blobs.\r
941 //\r
942 for (BlobType = 0; BlobType < KernelBlobTypeMax; ++BlobType) {\r
943 CurrentBlob = &mKernelBlob[BlobType];\r
944 Status = FetchBlob (CurrentBlob);\r
945 if (EFI_ERROR (Status)) {\r
946 goto FreeBlobs;\r
947 }\r
948 mTotalBlobBytes += CurrentBlob->Size;\r
949 }\r
950 KernelBlob = &mKernelBlob[KernelBlobTypeKernel];\r
951 InitrdBlob = &mKernelBlob[KernelBlobTypeInitrd];\r
952 CommandLineBlob = &mKernelBlob[KernelBlobTypeCommandLine];\r
953\r
954 if (KernelBlob->Data == NULL) {\r
955 Status = EFI_NOT_FOUND;\r
956 goto FreeBlobs;\r
957 }\r
958\r
959 //\r
960 // Create a new handle with a single VenHw() node device path protocol on it,\r
961 // plus a custom SimpleFileSystem protocol on it.\r
962 //\r
963 FileSystemHandle = NULL;\r
964 Status = gBS->InstallMultipleProtocolInterfaces (&FileSystemHandle,\r
965 &gEfiDevicePathProtocolGuid, &mFileSystemDevicePath,\r
966 &gEfiSimpleFileSystemProtocolGuid, &mFileSystem,\r
967 NULL);\r
968 if (EFI_ERROR (Status)) {\r
969 DEBUG ((EFI_D_ERROR, "%a: InstallMultipleProtocolInterfaces(): %r\n",\r
970 __FUNCTION__, Status));\r
971 goto FreeBlobs;\r
972 }\r
973\r
974 //\r
975 // Create a device path for the kernel image to be loaded from that will call\r
976 // back into our file system.\r
977 //\r
978 KernelDevicePath = FileDevicePath (FileSystemHandle, KernelBlob->Name);\r
979 if (KernelDevicePath == NULL) {\r
980 DEBUG ((EFI_D_ERROR, "%a: failed to allocate kernel device path\n",\r
981 __FUNCTION__));\r
982 Status = EFI_OUT_OF_RESOURCES;\r
983 goto UninstallProtocols;\r
984 }\r
985\r
986 //\r
987 // Load the image. This should call back into our file system.\r
988 //\r
989 Status = gBS->LoadImage (\r
990 FALSE, // BootPolicy: exact match required\r
991 gImageHandle, // ParentImageHandle\r
992 KernelDevicePath,\r
993 NULL, // SourceBuffer\r
994 0, // SourceSize\r
995 &KernelImageHandle\r
996 );\r
997 if (EFI_ERROR (Status)) {\r
998 DEBUG ((EFI_D_ERROR, "%a: LoadImage(): %r\n", __FUNCTION__, Status));\r
999 goto FreeKernelDevicePath;\r
1000 }\r
1001\r
1002 //\r
1003 // Construct the kernel command line.\r
1004 //\r
1005 Status = gBS->OpenProtocol (\r
1006 KernelImageHandle,\r
1007 &gEfiLoadedImageProtocolGuid,\r
1008 (VOID **)&KernelLoadedImage,\r
1009 gImageHandle, // AgentHandle\r
1010 NULL, // ControllerHandle\r
1011 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1012 );\r
1013 ASSERT_EFI_ERROR (Status);\r
1014\r
1015 if (CommandLineBlob->Data == NULL) {\r
1016 KernelLoadedImage->LoadOptionsSize = 0;\r
1017 } else {\r
1018 //\r
1019 // Verify NUL-termination of the command line.\r
1020 //\r
1021 if (CommandLineBlob->Data[CommandLineBlob->Size - 1] != '\0') {\r
1022 DEBUG ((EFI_D_ERROR, "%a: kernel command line is not NUL-terminated\n",\r
1023 __FUNCTION__));\r
1024 Status = EFI_PROTOCOL_ERROR;\r
1025 goto UnloadKernelImage;\r
1026 }\r
1027\r
1028 //\r
1029 // Drop the terminating NUL, convert to UTF-16.\r
1030 //\r
1031 KernelLoadedImage->LoadOptionsSize = (CommandLineBlob->Size - 1) * 2;\r
1032 }\r
1033\r
1034 if (InitrdBlob->Data != NULL) {\r
1035 //\r
1036 // Append ' initrd=<name>' in UTF-16.\r
1037 //\r
1038 KernelLoadedImage->LoadOptionsSize +=\r
1039 (8 + StrLen(InitrdBlob->Name)) * 2;\r
1040 }\r
1041\r
1042 if (KernelLoadedImage->LoadOptionsSize == 0) {\r
1043 KernelLoadedImage->LoadOptions = NULL;\r
1044 } else {\r
1045 //\r
1046 // NUL-terminate in UTF-16.\r
1047 //\r
1048 KernelLoadedImage->LoadOptionsSize += 2;\r
1049\r
1050 KernelLoadedImage->LoadOptions = AllocatePool (\r
1051 KernelLoadedImage->LoadOptionsSize);\r
1052 if (KernelLoadedImage->LoadOptions == NULL) {\r
1053 KernelLoadedImage->LoadOptionsSize = 0;\r
1054 Status = EFI_OUT_OF_RESOURCES;\r
1055 goto UnloadKernelImage;\r
1056 }\r
1057\r
1058 UnicodeSPrintAsciiFormat (\r
1059 KernelLoadedImage->LoadOptions,\r
1060 KernelLoadedImage->LoadOptionsSize,\r
1061 "%a%a%s",\r
1062 (CommandLineBlob->Data == NULL) ? "" : (CHAR8 *)CommandLineBlob->Data,\r
1063 (InitrdBlob->Data == NULL) ? "" : " initrd=",\r
1064 (InitrdBlob->Data == NULL) ? L"" : InitrdBlob->Name\r
1065 );\r
1066 DEBUG ((EFI_D_INFO, "%a: command line: \"%s\"\n", __FUNCTION__,\r
1067 (CHAR16 *)KernelLoadedImage->LoadOptions));\r
1068 }\r
1069\r
1070 //\r
1071 // Signal the EFI_EVENT_GROUP_READY_TO_BOOT event.\r
1072 //\r
1073 EfiSignalEventReadyToBoot();\r
1074\r
1075 //\r
1076 // Start the image.\r
1077 //\r
1078 Status = gBS->StartImage (\r
1079 KernelImageHandle,\r
1080 NULL, // ExitDataSize\r
1081 NULL // ExitData\r
1082 );\r
1083 if (EFI_ERROR (Status)) {\r
1084 DEBUG ((EFI_D_ERROR, "%a: StartImage(): %r\n", __FUNCTION__, Status));\r
1085 }\r
1086\r
1087 if (KernelLoadedImage->LoadOptions != NULL) {\r
1088 FreePool (KernelLoadedImage->LoadOptions);\r
1089 }\r
1090 KernelLoadedImage->LoadOptionsSize = 0;\r
1091\r
1092UnloadKernelImage:\r
1093 gBS->UnloadImage (KernelImageHandle);\r
1094\r
1095FreeKernelDevicePath:\r
1096 FreePool (KernelDevicePath);\r
1097\r
1098UninstallProtocols:\r
1099 gBS->UninstallMultipleProtocolInterfaces (FileSystemHandle,\r
1100 &gEfiSimpleFileSystemProtocolGuid, &mFileSystem,\r
1101 &gEfiDevicePathProtocolGuid, &mFileSystemDevicePath,\r
1102 NULL);\r
1103\r
1104FreeBlobs:\r
1105 while (BlobType > 0) {\r
1106 CurrentBlob = &mKernelBlob[--BlobType];\r
1107 if (CurrentBlob->Data != NULL) {\r
1108 FreePool (CurrentBlob->Data);\r
1109 CurrentBlob->Size = 0;\r
1110 CurrentBlob->Data = NULL;\r
1111 }\r
1112 }\r
1113\r
1114 return Status;\r
1115}\r