hand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. hand.c
  5. Abstract:
  6. Revision History
  7. --*/
  8. #include "lib.h"
  9. #include "efistdarg.h" // !!!
  10. EFI_STATUS
  11. LibLocateProtocol (
  12. IN EFI_GUID *ProtocolGuid,
  13. OUT VOID **Interface
  14. )
  15. //
  16. // Find the first instance of this Protocol in the system and return it's interface
  17. //
  18. {
  19. EFI_STATUS Status;
  20. UINTN NumberHandles, Index;
  21. EFI_HANDLE *Handles;
  22. *Interface = NULL;
  23. Status = LibLocateHandle (ByProtocol, ProtocolGuid, NULL, &NumberHandles, &Handles);
  24. if (EFI_ERROR(Status)) {
  25. DEBUG((D_INFO, "LibLocateProtocol: Handle not found\n"));
  26. return Status;
  27. }
  28. for (Index=0; Index < NumberHandles; Index++) {
  29. Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], ProtocolGuid, Interface);
  30. if (!EFI_ERROR(Status)) {
  31. break;
  32. }
  33. }
  34. if (Handles) {
  35. FreePool (Handles);
  36. }
  37. return Status;
  38. }
  39. EFI_STATUS
  40. LibLocateHandle (
  41. IN EFI_LOCATE_SEARCH_TYPE SearchType,
  42. IN EFI_GUID *Protocol OPTIONAL,
  43. IN VOID *SearchKey OPTIONAL,
  44. IN OUT UINTN *NoHandles,
  45. OUT EFI_HANDLE **Buffer
  46. )
  47. {
  48. EFI_STATUS Status;
  49. UINTN BufferSize;
  50. //
  51. // Initialize for GrowBuffer loop
  52. //
  53. Status = EFI_SUCCESS;
  54. *Buffer = NULL;
  55. BufferSize = 50 * sizeof(EFI_HANDLE);
  56. //
  57. // Call the real function
  58. //
  59. while (GrowBuffer (&Status, (VOID **) Buffer, BufferSize)) {
  60. Status = uefi_call_wrapper(
  61. BS->LocateHandle,
  62. 5,
  63. SearchType,
  64. Protocol,
  65. SearchKey,
  66. &BufferSize,
  67. *Buffer
  68. );
  69. }
  70. *NoHandles = BufferSize / sizeof (EFI_HANDLE);
  71. if (EFI_ERROR(Status)) {
  72. *NoHandles = 0;
  73. }
  74. return Status;
  75. }
  76. EFI_STATUS
  77. LibLocateHandleByDiskSignature (
  78. IN UINT8 MBRType,
  79. IN UINT8 SignatureType,
  80. IN VOID *Signature,
  81. IN OUT UINTN *NoHandles,
  82. OUT EFI_HANDLE **Buffer
  83. )
  84. {
  85. EFI_STATUS Status;
  86. UINTN BufferSize;
  87. UINTN NoBlockIoHandles;
  88. EFI_HANDLE *BlockIoBuffer;
  89. EFI_DEVICE_PATH *DevicePath;
  90. UINTN Index;
  91. EFI_DEVICE_PATH *Start, *Next, *DevPath;
  92. HARDDRIVE_DEVICE_PATH *HardDriveDevicePath;
  93. BOOLEAN Match;
  94. BOOLEAN PreviousNodeIsHardDriveDevicePath;
  95. //
  96. // Initialize for GrowBuffer loop
  97. //
  98. Status = EFI_SUCCESS;
  99. BlockIoBuffer = NULL;
  100. BufferSize = 50 * sizeof(EFI_HANDLE);
  101. //
  102. // Call the real function
  103. //
  104. while (GrowBuffer (&Status, (VOID **)&BlockIoBuffer, BufferSize)) {
  105. //
  106. // Get list of device handles that support the BLOCK_IO Protocol.
  107. //
  108. Status = uefi_call_wrapper(
  109. BS->LocateHandle,
  110. 5,
  111. ByProtocol,
  112. &BlockIoProtocol,
  113. NULL,
  114. &BufferSize,
  115. BlockIoBuffer
  116. );
  117. }
  118. NoBlockIoHandles = BufferSize / sizeof (EFI_HANDLE);
  119. if (EFI_ERROR(Status)) {
  120. NoBlockIoHandles = 0;
  121. }
  122. //
  123. // If there was an error or there are no device handles that support
  124. // the BLOCK_IO Protocol, then return.
  125. //
  126. if (NoBlockIoHandles == 0) {
  127. FreePool(BlockIoBuffer);
  128. *NoHandles = 0;
  129. *Buffer = NULL;
  130. return Status;
  131. }
  132. //
  133. // Loop through all the device handles that support the BLOCK_IO Protocol
  134. //
  135. *NoHandles = 0;
  136. for(Index=0;Index<NoBlockIoHandles;Index++) {
  137. Status = uefi_call_wrapper(
  138. BS->HandleProtocol,
  139. 3,
  140. BlockIoBuffer[Index],
  141. &DevicePathProtocol,
  142. (VOID*)&DevicePath
  143. );
  144. //
  145. // Search DevicePath for a Hard Drive Media Device Path node.
  146. // If one is found, then see if it matches the signature that was
  147. // passed in. If it does match, and the next node is the End of the
  148. // device path, and the previous node is not a Hard Drive Media Device
  149. // Path, then we have found a match.
  150. //
  151. Match = FALSE;
  152. if (DevicePath != NULL) {
  153. PreviousNodeIsHardDriveDevicePath = FALSE;
  154. DevPath = DevicePath;
  155. Start = DevPath;
  156. //
  157. // Check for end of device path type
  158. //
  159. for (; ;) {
  160. if ((DevicePathType(DevPath) == MEDIA_DEVICE_PATH) &&
  161. (DevicePathSubType(DevPath) == MEDIA_HARDDRIVE_DP)) {
  162. HardDriveDevicePath = (HARDDRIVE_DEVICE_PATH *)(DevPath);
  163. if (PreviousNodeIsHardDriveDevicePath == FALSE) {
  164. Next = NextDevicePathNode(DevPath);
  165. if (IsDevicePathEndType(Next)) {
  166. if ((HardDriveDevicePath->MBRType == MBRType) &&
  167. (HardDriveDevicePath->SignatureType == SignatureType)) {
  168. switch(SignatureType) {
  169. case SIGNATURE_TYPE_MBR:
  170. if (*((UINT32 *)(Signature)) == *(UINT32 *)(&(HardDriveDevicePath->Signature[0]))) {
  171. Match = TRUE;
  172. }
  173. break;
  174. case SIGNATURE_TYPE_GUID:
  175. if (CompareGuid((EFI_GUID *)Signature,(EFI_GUID *)(&(HardDriveDevicePath->Signature[0]))) == 0) {
  176. Match = TRUE;
  177. }
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. PreviousNodeIsHardDriveDevicePath = TRUE;
  184. } else {
  185. PreviousNodeIsHardDriveDevicePath = FALSE;
  186. }
  187. if (IsDevicePathEnd(DevPath)) {
  188. break;
  189. }
  190. DevPath = NextDevicePathNode(DevPath);
  191. }
  192. }
  193. if (Match == FALSE) {
  194. BlockIoBuffer[Index] = NULL;
  195. } else {
  196. *NoHandles = *NoHandles + 1;
  197. }
  198. }
  199. //
  200. // If there are no matches, then return
  201. //
  202. if (*NoHandles == 0) {
  203. FreePool(BlockIoBuffer);
  204. *NoHandles = 0;
  205. *Buffer = NULL;
  206. return EFI_SUCCESS;
  207. }
  208. //
  209. // Allocate space for the return buffer of device handles.
  210. //
  211. *Buffer = AllocatePool(*NoHandles * sizeof(EFI_HANDLE));
  212. if (*Buffer == NULL) {
  213. FreePool(BlockIoBuffer);
  214. *NoHandles = 0;
  215. *Buffer = NULL;
  216. return EFI_OUT_OF_RESOURCES;
  217. }
  218. //
  219. // Build list of matching device handles.
  220. //
  221. *NoHandles = 0;
  222. for(Index=0;Index<NoBlockIoHandles;Index++) {
  223. if (BlockIoBuffer[Index] != NULL) {
  224. (*Buffer)[*NoHandles] = BlockIoBuffer[Index];
  225. *NoHandles = *NoHandles + 1;
  226. }
  227. }
  228. FreePool(BlockIoBuffer);
  229. return EFI_SUCCESS;
  230. }
  231. EFI_FILE_HANDLE
  232. LibOpenRoot (
  233. IN EFI_HANDLE DeviceHandle
  234. )
  235. {
  236. EFI_STATUS Status;
  237. EFI_FILE_IO_INTERFACE *Volume;
  238. EFI_FILE_HANDLE File;
  239. //
  240. // File the file system interface to the device
  241. //
  242. Status = uefi_call_wrapper(BS->HandleProtocol, 3, DeviceHandle, &FileSystemProtocol, (VOID*)&Volume);
  243. //
  244. // Open the root directory of the volume
  245. //
  246. if (!EFI_ERROR(Status)) {
  247. Status = uefi_call_wrapper(Volume->OpenVolume, 2, Volume, &File);
  248. }
  249. //
  250. // Done
  251. //
  252. return EFI_ERROR(Status) ? NULL : File;
  253. }
  254. EFI_FILE_INFO *
  255. LibFileInfo (
  256. IN EFI_FILE_HANDLE FHand
  257. )
  258. {
  259. EFI_STATUS Status;
  260. EFI_FILE_INFO *Buffer;
  261. UINTN BufferSize;
  262. //
  263. // Initialize for GrowBuffer loop
  264. //
  265. Status = EFI_SUCCESS;
  266. Buffer = NULL;
  267. BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
  268. //
  269. // Call the real function
  270. //
  271. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  272. Status = uefi_call_wrapper(
  273. FHand->GetInfo,
  274. 4,
  275. FHand,
  276. &GenericFileInfo,
  277. &BufferSize,
  278. Buffer
  279. );
  280. }
  281. return Buffer;
  282. }
  283. EFI_FILE_SYSTEM_INFO *
  284. LibFileSystemInfo (
  285. IN EFI_FILE_HANDLE FHand
  286. )
  287. {
  288. EFI_STATUS Status;
  289. EFI_FILE_SYSTEM_INFO *Buffer;
  290. UINTN BufferSize;
  291. //
  292. // Initialize for GrowBuffer loop
  293. //
  294. Status = EFI_SUCCESS;
  295. Buffer = NULL;
  296. BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + 200;
  297. //
  298. // Call the real function
  299. //
  300. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  301. Status = uefi_call_wrapper(
  302. FHand->GetInfo,
  303. 4,
  304. FHand,
  305. &FileSystemInfo,
  306. &BufferSize,
  307. Buffer
  308. );
  309. }
  310. return Buffer;
  311. }
  312. EFI_FILE_SYSTEM_VOLUME_LABEL_INFO *
  313. LibFileSystemVolumeLabelInfo (
  314. IN EFI_FILE_HANDLE FHand
  315. )
  316. {
  317. EFI_STATUS Status;
  318. EFI_FILE_SYSTEM_VOLUME_LABEL_INFO *Buffer;
  319. UINTN BufferSize;
  320. //
  321. // Initialize for GrowBuffer loop
  322. //
  323. Status = EFI_SUCCESS;
  324. Buffer = NULL;
  325. BufferSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO + 200;
  326. //
  327. // Call the real function
  328. //
  329. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  330. Status = uefi_call_wrapper(
  331. FHand->GetInfo,
  332. 4,
  333. FHand,
  334. &FileSystemVolumeLabelInfo,
  335. &BufferSize,
  336. Buffer
  337. );
  338. }
  339. return Buffer;
  340. }
  341. EFI_STATUS
  342. LibInstallProtocolInterfaces (
  343. IN OUT EFI_HANDLE *Handle,
  344. ...
  345. )
  346. {
  347. va_list args;
  348. EFI_STATUS Status;
  349. EFI_GUID *Protocol;
  350. VOID *Interface;
  351. EFI_TPL OldTpl;
  352. UINTN Index;
  353. EFI_HANDLE OldHandle;
  354. //
  355. // Syncronize with notifcations
  356. //
  357. OldTpl = uefi_call_wrapper(BS->RaiseTPL, 1, TPL_NOTIFY);
  358. OldHandle = *Handle;
  359. //
  360. // Install the protocol interfaces
  361. //
  362. Index = 0;
  363. Status = EFI_SUCCESS;
  364. va_start (args, Handle);
  365. while (!EFI_ERROR(Status)) {
  366. //
  367. // If protocol is NULL, then it's the end of the list
  368. //
  369. Protocol = va_arg(args, EFI_GUID *);
  370. if (!Protocol) {
  371. break;
  372. }
  373. Interface = va_arg(args, VOID *);
  374. //
  375. // Install it
  376. //
  377. DEBUG((D_INFO, "LibInstallProtocolInterface: %d %x\n", Protocol, Interface));
  378. Status = uefi_call_wrapper(BS->InstallProtocolInterface, 4, Handle, Protocol, EFI_NATIVE_INTERFACE, Interface);
  379. if (EFI_ERROR(Status)) {
  380. break;
  381. }
  382. Index += 1;
  383. }
  384. //
  385. // If there was an error, remove all the interfaces that were
  386. // installed without any errors
  387. //
  388. if (EFI_ERROR(Status)) {
  389. va_start (args, Handle);
  390. while (Index) {
  391. Protocol = va_arg(args, EFI_GUID *);
  392. Interface = va_arg(args, VOID *);
  393. uefi_call_wrapper(BS->UninstallProtocolInterface, 3, *Handle, Protocol, Interface);
  394. Index -= 1;
  395. }
  396. *Handle = OldHandle;
  397. }
  398. //
  399. // Done
  400. //
  401. uefi_call_wrapper(BS->RestoreTPL, 1, OldTpl);
  402. return Status;
  403. }
  404. VOID
  405. LibUninstallProtocolInterfaces (
  406. IN EFI_HANDLE Handle,
  407. ...
  408. )
  409. {
  410. va_list args;
  411. EFI_STATUS Status;
  412. EFI_GUID *Protocol;
  413. VOID *Interface;
  414. va_start (args, Handle);
  415. for (; ;) {
  416. //
  417. // If protocol is NULL, then it's the end of the list
  418. //
  419. Protocol = va_arg(args, EFI_GUID *);
  420. if (!Protocol) {
  421. break;
  422. }
  423. Interface = va_arg(args, VOID *);
  424. //
  425. // Uninstall it
  426. //
  427. Status = uefi_call_wrapper(BS->UninstallProtocolInterface, 3, Handle, Protocol, Interface);
  428. if (EFI_ERROR(Status)) {
  429. DEBUG((D_ERROR, "LibUninstallProtocolInterfaces: failed %g, %r\n", Protocol, Handle));
  430. }
  431. }
  432. }
  433. EFI_STATUS
  434. LibReinstallProtocolInterfaces (
  435. IN OUT EFI_HANDLE *Handle,
  436. ...
  437. )
  438. {
  439. va_list args;
  440. EFI_STATUS Status;
  441. EFI_GUID *Protocol;
  442. VOID *OldInterface, *NewInterface;
  443. EFI_TPL OldTpl;
  444. UINTN Index;
  445. //
  446. // Syncronize with notifcations
  447. //
  448. OldTpl = uefi_call_wrapper(BS->RaiseTPL, 1, TPL_NOTIFY);
  449. //
  450. // Install the protocol interfaces
  451. //
  452. Index = 0;
  453. Status = EFI_SUCCESS;
  454. va_start (args, Handle);
  455. while (!EFI_ERROR(Status)) {
  456. //
  457. // If protocol is NULL, then it's the end of the list
  458. //
  459. Protocol = va_arg(args, EFI_GUID *);
  460. if (!Protocol) {
  461. break;
  462. }
  463. OldInterface = va_arg(args, VOID *);
  464. NewInterface = va_arg(args, VOID *);
  465. //
  466. // Reinstall it
  467. //
  468. Status = uefi_call_wrapper(BS->ReinstallProtocolInterface, 4, Handle, Protocol, OldInterface, NewInterface);
  469. if (EFI_ERROR(Status)) {
  470. break;
  471. }
  472. Index += 1;
  473. }
  474. //
  475. // If there was an error, undo all the interfaces that were
  476. // reinstalled without any errors
  477. //
  478. if (EFI_ERROR(Status)) {
  479. va_start (args, Handle);
  480. while (Index) {
  481. Protocol = va_arg(args, EFI_GUID *);
  482. OldInterface = va_arg(args, VOID *);
  483. NewInterface = va_arg(args, VOID *);
  484. uefi_call_wrapper(BS->ReinstallProtocolInterface, 4, Handle, Protocol, NewInterface, OldInterface);
  485. Index -= 1;
  486. }
  487. }
  488. //
  489. // Done
  490. //
  491. uefi_call_wrapper(BS->RestoreTPL, 1, OldTpl);
  492. return Status;
  493. }