hand.c 14 KB

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