EmuSimpleFileSystem.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*++ @file
  2. Produce Simple File System abstractions for directories on your PC using Posix APIs.
  3. The configuration of what devices to mount or emulate comes from UNIX
  4. environment variables. The variables must be visible to the Microsoft*
  5. Developer Studio for them to work.
  6. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  7. Portions copyright (c) 2011, Apple Inc. All rights reserved.
  8. SPDX-License-Identifier: BSD-2-Clause-Patent
  9. **/
  10. #include "EmuSimpleFileSystem.h"
  11. /**
  12. Opens a new file relative to the source file's location.
  13. @param This The protocol instance pointer.
  14. @param NewHandle Returns File Handle for FileName.
  15. @param FileName Null terminated string. "\", ".", and ".." are supported.
  16. @param OpenMode Open mode for file.
  17. @param Attributes Only used for EFI_FILE_MODE_CREATE.
  18. @retval EFI_SUCCESS The device was opened.
  19. @retval EFI_NOT_FOUND The specified file could not be found on the device.
  20. @retval EFI_NO_MEDIA The device has no media.
  21. @retval EFI_MEDIA_CHANGED The media has changed.
  22. @retval EFI_DEVICE_ERROR The device reported an error.
  23. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  24. @retval EFI_ACCESS_DENIED The service denied access to the file.
  25. @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
  26. @retval EFI_VOLUME_FULL The volume is full.
  27. **/
  28. EFI_STATUS
  29. EFIAPI
  30. EmuSimpleFileSystemOpen (
  31. IN EFI_FILE_PROTOCOL *This,
  32. OUT EFI_FILE_PROTOCOL **NewHandle,
  33. IN CHAR16 *FileName,
  34. IN UINT64 OpenMode,
  35. IN UINT64 Attributes
  36. )
  37. {
  38. EFI_STATUS Status;
  39. EFI_TPL OldTpl;
  40. EMU_EFI_FILE_PRIVATE *PrivateFile;
  41. EMU_EFI_FILE_PRIVATE *NewPrivateFile;
  42. //
  43. // Check for obvious invalid parameters.
  44. //
  45. if ((This == NULL) || (NewHandle == NULL) || (FileName == NULL)) {
  46. return EFI_INVALID_PARAMETER;
  47. }
  48. switch (OpenMode) {
  49. case EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
  50. if (Attributes &~EFI_FILE_VALID_ATTR) {
  51. return EFI_INVALID_PARAMETER;
  52. }
  53. if (Attributes & EFI_FILE_READ_ONLY) {
  54. return EFI_INVALID_PARAMETER;
  55. }
  56. //
  57. // fall through
  58. //
  59. case EFI_FILE_MODE_READ:
  60. case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
  61. break;
  62. default:
  63. return EFI_INVALID_PARAMETER;
  64. }
  65. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  66. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  67. NewPrivateFile = AllocateCopyPool (sizeof (EMU_EFI_FILE_PRIVATE), PrivateFile);
  68. if (NewPrivateFile == NULL) {
  69. Status = EFI_OUT_OF_RESOURCES;
  70. goto Done;
  71. }
  72. Status = PrivateFile->Io->Open (PrivateFile->Io, &NewPrivateFile->Io, FileName, OpenMode, Attributes);
  73. if (!EFI_ERROR (Status)) {
  74. *NewHandle = &NewPrivateFile->EfiFile;
  75. } else {
  76. *NewHandle = NULL;
  77. FreePool (NewPrivateFile);
  78. }
  79. Done:
  80. gBS->RestoreTPL (OldTpl);
  81. return Status;
  82. }
  83. /**
  84. Close the file handle
  85. @param This Protocol instance pointer.
  86. @retval EFI_SUCCESS The file was closed.
  87. **/
  88. EFI_STATUS
  89. EFIAPI
  90. EmuSimpleFileSystemClose (
  91. IN EFI_FILE_PROTOCOL *This
  92. )
  93. {
  94. EFI_STATUS Status;
  95. EMU_EFI_FILE_PRIVATE *PrivateFile;
  96. EFI_TPL OldTpl;
  97. if (This == NULL) {
  98. return EFI_INVALID_PARAMETER;
  99. }
  100. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  101. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  102. Status = PrivateFile->Io->Close (PrivateFile->Io);
  103. if (!EFI_ERROR (Status)) {
  104. gBS->FreePool (PrivateFile);
  105. }
  106. gBS->RestoreTPL (OldTpl);
  107. return Status;
  108. }
  109. /**
  110. Close and delete the file handle.
  111. @param This Protocol instance pointer.
  112. @retval EFI_SUCCESS The file was closed and deleted.
  113. @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
  114. **/
  115. EFI_STATUS
  116. EFIAPI
  117. EmuSimpleFileSystemDelete (
  118. IN EFI_FILE_PROTOCOL *This
  119. )
  120. {
  121. EFI_STATUS Status;
  122. EMU_EFI_FILE_PRIVATE *PrivateFile;
  123. EFI_TPL OldTpl;
  124. if (This == NULL) {
  125. return EFI_INVALID_PARAMETER;
  126. }
  127. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  128. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  129. Status = PrivateFile->Io->Delete (PrivateFile->Io);
  130. if (!EFI_ERROR (Status)) {
  131. gBS->FreePool (PrivateFile);
  132. }
  133. gBS->RestoreTPL (OldTpl);
  134. return Status;
  135. }
  136. /**
  137. Read data from the file.
  138. @param This Protocol instance pointer.
  139. @param BufferSize On input size of buffer, on output amount of data in buffer.
  140. @param Buffer The buffer in which data is read.
  141. @retval EFI_SUCCESS Data was read.
  142. @retval EFI_NO_MEDIA The device has no media.
  143. @retval EFI_DEVICE_ERROR The device reported an error.
  144. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  145. @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.
  146. **/
  147. EFI_STATUS
  148. EFIAPI
  149. EmuSimpleFileSystemRead (
  150. IN EFI_FILE_PROTOCOL *This,
  151. IN OUT UINTN *BufferSize,
  152. OUT VOID *Buffer
  153. )
  154. {
  155. EFI_STATUS Status;
  156. EMU_EFI_FILE_PRIVATE *PrivateFile;
  157. EFI_TPL OldTpl;
  158. if ((This == NULL) || (BufferSize == NULL)) {
  159. return EFI_INVALID_PARAMETER;
  160. }
  161. if ((*BufferSize != 0) && (Buffer == NULL)) {
  162. // Buffer can be NULL if *BufferSize is zero
  163. return EFI_INVALID_PARAMETER;
  164. }
  165. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  166. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  167. Status = PrivateFile->Io->Read (PrivateFile->Io, BufferSize, Buffer);
  168. gBS->RestoreTPL (OldTpl);
  169. return Status;
  170. }
  171. /**
  172. Write data to a file.
  173. @param This Protocol instance pointer.
  174. @param BufferSize On input size of buffer, on output amount of data in buffer.
  175. @param Buffer The buffer in which data to write.
  176. @retval EFI_SUCCESS Data was written.
  177. @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
  178. @retval EFI_NO_MEDIA The device has no media.
  179. @retval EFI_DEVICE_ERROR The device reported an error.
  180. @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
  181. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  182. @retval EFI_WRITE_PROTECTED The device is write protected.
  183. @retval EFI_ACCESS_DENIED The file was open for read only.
  184. @retval EFI_VOLUME_FULL The volume is full.
  185. **/
  186. EFI_STATUS
  187. EFIAPI
  188. EmuSimpleFileSystemWrite (
  189. IN EFI_FILE_PROTOCOL *This,
  190. IN OUT UINTN *BufferSize,
  191. IN VOID *Buffer
  192. )
  193. {
  194. EFI_STATUS Status;
  195. EMU_EFI_FILE_PRIVATE *PrivateFile;
  196. EFI_TPL OldTpl;
  197. if ((This == NULL) || (BufferSize == NULL) || (Buffer == NULL)) {
  198. return EFI_INVALID_PARAMETER;
  199. }
  200. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  201. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  202. Status = PrivateFile->Io->Write (PrivateFile->Io, BufferSize, Buffer);
  203. gBS->RestoreTPL (OldTpl);
  204. return Status;
  205. }
  206. /**
  207. Get a file's current position
  208. @param This Protocol instance pointer.
  209. @param Position Byte position from the start of the file.
  210. @retval EFI_SUCCESS Position was updated.
  211. @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.
  212. **/
  213. EFI_STATUS
  214. EFIAPI
  215. EmuSimpleFileSystemGetPosition (
  216. IN EFI_FILE_PROTOCOL *This,
  217. OUT UINT64 *Position
  218. )
  219. {
  220. EFI_STATUS Status;
  221. EMU_EFI_FILE_PRIVATE *PrivateFile;
  222. EFI_TPL OldTpl;
  223. if ((This == NULL) || (Position == NULL)) {
  224. return EFI_INVALID_PARAMETER;
  225. }
  226. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  227. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  228. Status = PrivateFile->Io->GetPosition (PrivateFile->Io, Position);
  229. gBS->RestoreTPL (OldTpl);
  230. return Status;
  231. }
  232. /**
  233. Set file's current position
  234. @param This Protocol instance pointer.
  235. @param Position Byte position from the start of the file.
  236. @retval EFI_SUCCESS Position was updated.
  237. @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..
  238. **/
  239. EFI_STATUS
  240. EFIAPI
  241. EmuSimpleFileSystemSetPosition (
  242. IN EFI_FILE_PROTOCOL *This,
  243. IN UINT64 Position
  244. )
  245. {
  246. EFI_STATUS Status;
  247. EMU_EFI_FILE_PRIVATE *PrivateFile;
  248. EFI_TPL OldTpl;
  249. if (This == NULL) {
  250. return EFI_INVALID_PARAMETER;
  251. }
  252. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  253. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  254. Status = PrivateFile->Io->SetPosition (PrivateFile->Io, Position);
  255. gBS->RestoreTPL (OldTpl);
  256. return Status;
  257. }
  258. /**
  259. Get information about a file.
  260. @param This Protocol instance pointer.
  261. @param InformationType Type of information to return in Buffer.
  262. @param BufferSize On input size of buffer, on output amount of data in buffer.
  263. @param Buffer The buffer to return data.
  264. @retval EFI_SUCCESS Data was returned.
  265. @retval EFI_UNSUPPORTED InformationType is not supported.
  266. @retval EFI_NO_MEDIA The device has no media.
  267. @retval EFI_DEVICE_ERROR The device reported an error.
  268. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  269. @retval EFI_WRITE_PROTECTED The device is write protected.
  270. @retval EFI_ACCESS_DENIED The file was open for read only.
  271. @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.
  272. **/
  273. EFI_STATUS
  274. EFIAPI
  275. EmuSimpleFileSystemGetInfo (
  276. IN EFI_FILE_PROTOCOL *This,
  277. IN EFI_GUID *InformationType,
  278. IN OUT UINTN *BufferSize,
  279. OUT VOID *Buffer
  280. )
  281. {
  282. EFI_STATUS Status;
  283. EMU_EFI_FILE_PRIVATE *PrivateFile;
  284. EFI_TPL OldTpl;
  285. if ((This == NULL) || (InformationType == NULL) || (BufferSize == NULL)) {
  286. return EFI_INVALID_PARAMETER;
  287. }
  288. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  289. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  290. Status = PrivateFile->Io->GetInfo (PrivateFile->Io, InformationType, BufferSize, Buffer);
  291. gBS->RestoreTPL (OldTpl);
  292. return Status;
  293. }
  294. /**
  295. Set information about a file
  296. @param File Protocol instance pointer.
  297. @param InformationType Type of information in Buffer.
  298. @param BufferSize Size of buffer.
  299. @param Buffer The data to write.
  300. @retval EFI_SUCCESS Data was set.
  301. @retval EFI_UNSUPPORTED InformationType is not supported.
  302. @retval EFI_NO_MEDIA The device has no media.
  303. @retval EFI_DEVICE_ERROR The device reported an error.
  304. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  305. @retval EFI_WRITE_PROTECTED The device is write protected.
  306. @retval EFI_ACCESS_DENIED The file was open for read only.
  307. **/
  308. EFI_STATUS
  309. EFIAPI
  310. EmuSimpleFileSystemSetInfo (
  311. IN EFI_FILE_PROTOCOL *This,
  312. IN EFI_GUID *InformationType,
  313. IN UINTN BufferSize,
  314. IN VOID *Buffer
  315. )
  316. {
  317. EFI_STATUS Status;
  318. EMU_EFI_FILE_PRIVATE *PrivateFile;
  319. EFI_TPL OldTpl;
  320. //
  321. // Check for invalid parameters.
  322. //
  323. if ((This == NULL) || (InformationType == NULL) || (BufferSize == 0) || (Buffer == NULL)) {
  324. return EFI_INVALID_PARAMETER;
  325. }
  326. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  327. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  328. Status = PrivateFile->Io->SetInfo (PrivateFile->Io, InformationType, BufferSize, Buffer);
  329. gBS->RestoreTPL (OldTpl);
  330. return Status;
  331. }
  332. /**
  333. Flush data back for the file handle.
  334. @param This Protocol instance pointer.
  335. @retval EFI_SUCCESS Data was flushed.
  336. @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
  337. @retval EFI_NO_MEDIA The device has no media.
  338. @retval EFI_DEVICE_ERROR The device reported an error.
  339. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  340. @retval EFI_WRITE_PROTECTED The device is write protected.
  341. @retval EFI_ACCESS_DENIED The file was open for read only.
  342. @retval EFI_VOLUME_FULL The volume is full.
  343. **/
  344. EFI_STATUS
  345. EFIAPI
  346. EmuSimpleFileSystemFlush (
  347. IN EFI_FILE_PROTOCOL *This
  348. )
  349. {
  350. EFI_STATUS Status;
  351. EMU_EFI_FILE_PRIVATE *PrivateFile;
  352. EFI_TPL OldTpl;
  353. if (This == NULL) {
  354. return EFI_INVALID_PARAMETER;
  355. }
  356. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  357. PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
  358. Status = PrivateFile->Io->Flush (PrivateFile->Io);
  359. gBS->RestoreTPL (OldTpl);
  360. return Status;
  361. }
  362. /**
  363. Open the root directory on a volume.
  364. @param This Protocol instance pointer.
  365. @param Root Returns an Open file handle for the root directory
  366. @retval EFI_SUCCESS The device was opened.
  367. @retval EFI_UNSUPPORTED This volume does not support the file system.
  368. @retval EFI_NO_MEDIA The device has no media.
  369. @retval EFI_DEVICE_ERROR The device reported an error.
  370. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  371. @retval EFI_ACCESS_DENIED The service denied access to the file.
  372. @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
  373. **/
  374. EFI_STATUS
  375. EFIAPI
  376. EmuSimpleFileSystemOpenVolume (
  377. IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
  378. OUT EFI_FILE_PROTOCOL **Root
  379. )
  380. {
  381. EFI_STATUS Status;
  382. EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
  383. EMU_EFI_FILE_PRIVATE *PrivateFile;
  384. EFI_TPL OldTpl;
  385. Status = EFI_UNSUPPORTED;
  386. if ((This == NULL) || (Root == NULL)) {
  387. return EFI_INVALID_PARAMETER;
  388. }
  389. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  390. Private = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This);
  391. PrivateFile = AllocatePool (sizeof (EMU_EFI_FILE_PRIVATE));
  392. if (PrivateFile == NULL) {
  393. Status = EFI_OUT_OF_RESOURCES;
  394. goto Done;
  395. }
  396. PrivateFile->Signature = EMU_EFI_FILE_PRIVATE_SIGNATURE;
  397. PrivateFile->IoThunk = Private->IoThunk;
  398. PrivateFile->SimpleFileSystem = This;
  399. ZeroMem (&PrivateFile->EfiFile, sizeof (PrivateFile->EfiFile));
  400. PrivateFile->EfiFile.Revision = EFI_FILE_PROTOCOL_REVISION;
  401. PrivateFile->EfiFile.Open = EmuSimpleFileSystemOpen;
  402. PrivateFile->EfiFile.Close = EmuSimpleFileSystemClose;
  403. PrivateFile->EfiFile.Delete = EmuSimpleFileSystemDelete;
  404. PrivateFile->EfiFile.Read = EmuSimpleFileSystemRead;
  405. PrivateFile->EfiFile.Write = EmuSimpleFileSystemWrite;
  406. PrivateFile->EfiFile.GetPosition = EmuSimpleFileSystemGetPosition;
  407. PrivateFile->EfiFile.SetPosition = EmuSimpleFileSystemSetPosition;
  408. PrivateFile->EfiFile.GetInfo = EmuSimpleFileSystemGetInfo;
  409. PrivateFile->EfiFile.SetInfo = EmuSimpleFileSystemSetInfo;
  410. PrivateFile->EfiFile.Flush = EmuSimpleFileSystemFlush;
  411. *Root = &PrivateFile->EfiFile;
  412. Status = Private->Io->OpenVolume (Private->Io, &PrivateFile->Io);
  413. if (EFI_ERROR (Status)) {
  414. goto Done;
  415. }
  416. AddUnicodeString2 (
  417. "eng",
  418. gEmuSimpleFileSystemComponentName.SupportedLanguages,
  419. &Private->ControllerNameTable,
  420. Private->IoThunk->ConfigString,
  421. TRUE
  422. );
  423. AddUnicodeString2 (
  424. "en",
  425. gEmuSimpleFileSystemComponentName.SupportedLanguages,
  426. &Private->ControllerNameTable,
  427. Private->IoThunk->ConfigString,
  428. FALSE
  429. );
  430. Done:
  431. if (EFI_ERROR (Status)) {
  432. if (PrivateFile) {
  433. gBS->FreePool (PrivateFile);
  434. }
  435. *Root = NULL;
  436. }
  437. gBS->RestoreTPL (OldTpl);
  438. return Status;
  439. }
  440. /**
  441. Tests to see if this driver supports a given controller. If a child device is provided,
  442. it further tests to see if this driver supports creating a handle for the specified child device.
  443. This function checks to see if the driver specified by This supports the device specified by
  444. ControllerHandle. Drivers will typically use the device path attached to
  445. ControllerHandle and/or the services from the bus I/O abstraction attached to
  446. ControllerHandle to determine if the driver supports ControllerHandle. This function
  447. may be called many times during platform initialization. In order to reduce boot times, the tests
  448. performed by this function must be very small, and take as little time as possible to execute. This
  449. function must not change the state of any hardware devices, and this function must be aware that the
  450. device specified by ControllerHandle may already be managed by the same driver or a
  451. different driver. This function must match its calls to AllocatePages() with FreePages(),
  452. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  453. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  454. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  455. to guarantee the state of ControllerHandle is not modified by this function.
  456. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  457. @param[in] ControllerHandle The handle of the controller to test. This handle
  458. must support a protocol interface that supplies
  459. an I/O abstraction to the driver.
  460. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  461. parameter is ignored by device drivers, and is optional for bus
  462. drivers. For bus drivers, if this parameter is not NULL, then
  463. the bus driver must determine if the bus controller specified
  464. by ControllerHandle and the child controller specified
  465. by RemainingDevicePath are both supported by this
  466. bus driver.
  467. @retval EFI_SUCCESS The device specified by ControllerHandle and
  468. RemainingDevicePath is supported by the driver specified by This.
  469. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  470. RemainingDevicePath is already being managed by the driver
  471. specified by This.
  472. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  473. RemainingDevicePath is already being managed by a different
  474. driver or an application that requires exclusive access.
  475. Currently not implemented.
  476. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  477. RemainingDevicePath is not supported by the driver specified by This.
  478. **/
  479. EFI_STATUS
  480. EFIAPI
  481. EmuSimpleFileSystemDriverBindingSupported (
  482. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  483. IN EFI_HANDLE ControllerHandle,
  484. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  485. )
  486. {
  487. EFI_STATUS Status;
  488. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  489. //
  490. // Open the IO Abstraction(s) needed to perform the supported test
  491. //
  492. Status = gBS->OpenProtocol (
  493. ControllerHandle,
  494. &gEmuIoThunkProtocolGuid,
  495. (VOID **)&EmuIoThunk,
  496. This->DriverBindingHandle,
  497. ControllerHandle,
  498. EFI_OPEN_PROTOCOL_BY_DRIVER
  499. );
  500. if (EFI_ERROR (Status)) {
  501. return Status;
  502. }
  503. //
  504. // Make sure GUID is for a File System handle.
  505. //
  506. Status = EFI_UNSUPPORTED;
  507. if (CompareGuid (EmuIoThunk->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
  508. Status = EFI_SUCCESS;
  509. }
  510. //
  511. // Close the I/O Abstraction(s) used to perform the supported test
  512. //
  513. gBS->CloseProtocol (
  514. ControllerHandle,
  515. &gEmuIoThunkProtocolGuid,
  516. This->DriverBindingHandle,
  517. ControllerHandle
  518. );
  519. return Status;
  520. }
  521. /**
  522. Starts a device controller or a bus controller.
  523. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  524. As a result, much of the error checking on the parameters to Start() has been moved into this
  525. common boot service. It is legal to call Start() from other locations,
  526. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  527. 1. ControllerHandle must be a valid EFI_HANDLE.
  528. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  529. EFI_DEVICE_PATH_PROTOCOL.
  530. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  531. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  532. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  533. @param[in] ControllerHandle The handle of the controller to start. This handle
  534. must support a protocol interface that supplies
  535. an I/O abstraction to the driver.
  536. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  537. parameter is ignored by device drivers, and is optional for bus
  538. drivers. For a bus driver, if this parameter is NULL, then handles
  539. for all the children of Controller are created by this driver.
  540. If this parameter is not NULL and the first Device Path Node is
  541. not the End of Device Path Node, then only the handle for the
  542. child device specified by the first Device Path Node of
  543. RemainingDevicePath is created by this driver.
  544. If the first Device Path Node of RemainingDevicePath is
  545. the End of Device Path Node, no child handle is created by this
  546. driver.
  547. @retval EFI_SUCCESS The device was started.
  548. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  549. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  550. @retval Others The driver failded to start the device.
  551. **/
  552. EFI_STATUS
  553. EFIAPI
  554. EmuSimpleFileSystemDriverBindingStart (
  555. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  556. IN EFI_HANDLE ControllerHandle,
  557. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  558. )
  559. {
  560. EFI_STATUS Status;
  561. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  562. EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
  563. Private = NULL;
  564. //
  565. // Open the IO Abstraction(s) needed
  566. //
  567. Status = gBS->OpenProtocol (
  568. ControllerHandle,
  569. &gEmuIoThunkProtocolGuid,
  570. (VOID **)&EmuIoThunk,
  571. This->DriverBindingHandle,
  572. ControllerHandle,
  573. EFI_OPEN_PROTOCOL_BY_DRIVER
  574. );
  575. if (EFI_ERROR (Status)) {
  576. return Status;
  577. }
  578. //
  579. // Validate GUID
  580. //
  581. if (!CompareGuid (EmuIoThunk->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
  582. Status = EFI_UNSUPPORTED;
  583. goto Done;
  584. }
  585. Private = AllocateZeroPool (sizeof (EMU_SIMPLE_FILE_SYSTEM_PRIVATE));
  586. if (Private == NULL) {
  587. Status = EFI_OUT_OF_RESOURCES;
  588. goto Done;
  589. }
  590. Status = EmuIoThunk->Open (EmuIoThunk);
  591. if (EFI_ERROR (Status)) {
  592. goto Done;
  593. }
  594. Private->Signature = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE;
  595. Private->IoThunk = EmuIoThunk;
  596. Private->Io = EmuIoThunk->Interface;
  597. Private->SimpleFileSystem.Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  598. Private->SimpleFileSystem.OpenVolume = EmuSimpleFileSystemOpenVolume;
  599. Private->ControllerNameTable = NULL;
  600. AddUnicodeString2 (
  601. "eng",
  602. gEmuSimpleFileSystemComponentName.SupportedLanguages,
  603. &Private->ControllerNameTable,
  604. EmuIoThunk->ConfigString,
  605. TRUE
  606. );
  607. AddUnicodeString2 (
  608. "en",
  609. gEmuSimpleFileSystemComponentName2.SupportedLanguages,
  610. &Private->ControllerNameTable,
  611. EmuIoThunk->ConfigString,
  612. FALSE
  613. );
  614. Status = gBS->InstallMultipleProtocolInterfaces (
  615. &ControllerHandle,
  616. &gEfiSimpleFileSystemProtocolGuid,
  617. &Private->SimpleFileSystem,
  618. NULL
  619. );
  620. Done:
  621. if (EFI_ERROR (Status)) {
  622. if (Private != NULL) {
  623. if (Private->ControllerNameTable != NULL) {
  624. FreeUnicodeStringTable (Private->ControllerNameTable);
  625. }
  626. gBS->FreePool (Private);
  627. }
  628. gBS->CloseProtocol (
  629. ControllerHandle,
  630. &gEmuIoThunkProtocolGuid,
  631. This->DriverBindingHandle,
  632. ControllerHandle
  633. );
  634. }
  635. return Status;
  636. }
  637. /**
  638. Stops a device controller or a bus controller.
  639. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  640. As a result, much of the error checking on the parameters to Stop() has been moved
  641. into this common boot service. It is legal to call Stop() from other locations,
  642. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  643. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  644. same driver's Start() function.
  645. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  646. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  647. Start() function, and the Start() function must have called OpenProtocol() on
  648. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  649. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  650. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  651. support a bus specific I/O protocol for the driver
  652. to use to stop the device.
  653. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  654. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  655. if NumberOfChildren is 0.
  656. @retval EFI_SUCCESS The device was stopped.
  657. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  658. **/
  659. EFI_STATUS
  660. EFIAPI
  661. EmuSimpleFileSystemDriverBindingStop (
  662. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  663. IN EFI_HANDLE ControllerHandle,
  664. IN UINTN NumberOfChildren,
  665. IN EFI_HANDLE *ChildHandleBuffer
  666. )
  667. {
  668. EFI_STATUS Status;
  669. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
  670. EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
  671. //
  672. // Get our context back
  673. //
  674. Status = gBS->OpenProtocol (
  675. ControllerHandle,
  676. &gEfiSimpleFileSystemProtocolGuid,
  677. (VOID **)&SimpleFileSystem,
  678. This->DriverBindingHandle,
  679. ControllerHandle,
  680. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  681. );
  682. if (EFI_ERROR (Status)) {
  683. return EFI_UNSUPPORTED;
  684. }
  685. Private = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (SimpleFileSystem);
  686. //
  687. // Uninstall the Simple File System Protocol from ControllerHandle
  688. //
  689. Status = gBS->UninstallMultipleProtocolInterfaces (
  690. ControllerHandle,
  691. &gEfiSimpleFileSystemProtocolGuid,
  692. &Private->SimpleFileSystem,
  693. NULL
  694. );
  695. if (!EFI_ERROR (Status)) {
  696. Status = gBS->CloseProtocol (
  697. ControllerHandle,
  698. &gEmuIoThunkProtocolGuid,
  699. This->DriverBindingHandle,
  700. ControllerHandle
  701. );
  702. ASSERT_EFI_ERROR (Status);
  703. //
  704. // Destroy the IO interface.
  705. //
  706. Status = Private->IoThunk->Close (Private->IoThunk);
  707. ASSERT_EFI_ERROR (Status);
  708. //
  709. // Free our instance data
  710. //
  711. FreeUnicodeStringTable (Private->ControllerNameTable);
  712. gBS->FreePool (Private);
  713. }
  714. return Status;
  715. }
  716. EFI_DRIVER_BINDING_PROTOCOL gEmuSimpleFileSystemDriverBinding = {
  717. EmuSimpleFileSystemDriverBindingSupported,
  718. EmuSimpleFileSystemDriverBindingStart,
  719. EmuSimpleFileSystemDriverBindingStop,
  720. 0xa,
  721. NULL,
  722. NULL
  723. };
  724. /**
  725. The user Entry Point for module EmuSimpleFileSystem. The user code starts with this function.
  726. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  727. @param[in] SystemTable A pointer to the EFI System Table.
  728. @retval EFI_SUCCESS The entry point is executed successfully.
  729. @retval other Some error occurs when executing this entry point.
  730. **/
  731. EFI_STATUS
  732. EFIAPI
  733. InitializeEmuSimpleFileSystem (
  734. IN EFI_HANDLE ImageHandle,
  735. IN EFI_SYSTEM_TABLE *SystemTable
  736. )
  737. {
  738. EFI_STATUS Status;
  739. Status = EfiLibInstallDriverBindingComponentName2 (
  740. ImageHandle,
  741. SystemTable,
  742. &gEmuSimpleFileSystemDriverBinding,
  743. ImageHandle,
  744. &gEmuSimpleFileSystemComponentName,
  745. &gEmuSimpleFileSystemComponentName2
  746. );
  747. ASSERT_EFI_ERROR (Status);
  748. return Status;
  749. }