QemuKernel.c 35 KB

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