hand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 *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. //
  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. Status = EFI_SUCCESS;
  265. Buffer = NULL;
  266. BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
  267. //
  268. // Call the real function
  269. //
  270. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  271. Status = uefi_call_wrapper(
  272. FHand->GetInfo,
  273. 4,
  274. FHand,
  275. &GenericFileInfo,
  276. &BufferSize,
  277. Buffer
  278. );
  279. }
  280. return Buffer;
  281. }
  282. EFI_FILE_SYSTEM_INFO *
  283. LibFileSystemInfo (
  284. IN EFI_FILE_HANDLE FHand
  285. )
  286. {
  287. EFI_STATUS Status;
  288. EFI_FILE_SYSTEM_INFO *Buffer;
  289. UINTN BufferSize;
  290. //
  291. // Initialize for GrowBuffer loop
  292. //
  293. Status = EFI_SUCCESS;
  294. Buffer = NULL;
  295. BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + 200;
  296. //
  297. // Call the real function
  298. //
  299. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  300. Status = uefi_call_wrapper(
  301. FHand->GetInfo,
  302. 4,
  303. FHand,
  304. &FileSystemInfo,
  305. &BufferSize,
  306. Buffer
  307. );
  308. }
  309. return Buffer;
  310. }
  311. EFI_FILE_SYSTEM_VOLUME_LABEL_INFO *
  312. LibFileSystemVolumeLabelInfo (
  313. IN EFI_FILE_HANDLE FHand
  314. )
  315. {
  316. EFI_STATUS Status;
  317. EFI_FILE_SYSTEM_VOLUME_LABEL_INFO *Buffer;
  318. UINTN BufferSize;
  319. //
  320. // Initialize for GrowBuffer loop
  321. //
  322. Status = EFI_SUCCESS;
  323. Buffer = NULL;
  324. BufferSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO + 200;
  325. //
  326. // Call the real function
  327. //
  328. while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
  329. Status = uefi_call_wrapper(
  330. FHand->GetInfo,
  331. 4,
  332. FHand,
  333. &FileSystemVolumeLabelInfo,
  334. &BufferSize,
  335. Buffer
  336. );
  337. }
  338. return Buffer;
  339. }
  340. EFI_STATUS
  341. LibInstallProtocolInterfaces (
  342. IN OUT EFI_HANDLE *Handle,
  343. ...
  344. )
  345. {
  346. va_list args;
  347. EFI_STATUS Status;
  348. EFI_GUID *Protocol;
  349. VOID *Interface;
  350. EFI_TPL OldTpl;
  351. UINTN Index;
  352. EFI_HANDLE OldHandle;
  353. //
  354. // Syncronize with notifcations
  355. //
  356. OldTpl = uefi_call_wrapper(BS->RaiseTPL, 1, TPL_NOTIFY);
  357. OldHandle = *Handle;
  358. //
  359. // Install the protocol interfaces
  360. //
  361. Index = 0;
  362. Status = EFI_SUCCESS;
  363. va_start (args, Handle);
  364. while (!EFI_ERROR(Status)) {
  365. //
  366. // If protocol is NULL, then it's the end of the list
  367. //
  368. Protocol = va_arg(args, EFI_GUID *);
  369. if (!Protocol) {
  370. break;
  371. }
  372. Interface = va_arg(args, VOID *);
  373. //
  374. // Install it
  375. //
  376. DEBUG((D_INFO, "LibInstallProtocolInterface: %d %x\n", Protocol, Interface));
  377. Status = uefi_call_wrapper(BS->InstallProtocolInterface, 4, Handle, Protocol, EFI_NATIVE_INTERFACE, Interface);
  378. if (EFI_ERROR(Status)) {
  379. break;
  380. }
  381. Index += 1;
  382. }
  383. va_end (args);
  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. va_end (args);
  398. }
  399. //
  400. // Done
  401. //
  402. uefi_call_wrapper(BS->RestoreTPL, 1, OldTpl);
  403. return Status;
  404. }
  405. VOID
  406. LibUninstallProtocolInterfaces (
  407. IN EFI_HANDLE Handle,
  408. ...
  409. )
  410. {
  411. va_list args;
  412. EFI_STATUS Status;
  413. EFI_GUID *Protocol;
  414. VOID *Interface;
  415. va_start (args, Handle);
  416. for (; ;) {
  417. //
  418. // If protocol is NULL, then it's the end of the list
  419. //
  420. Protocol = va_arg(args, EFI_GUID *);
  421. if (!Protocol) {
  422. break;
  423. }
  424. Interface = va_arg(args, VOID *);
  425. //
  426. // Uninstall it
  427. //
  428. Status = uefi_call_wrapper(BS->UninstallProtocolInterface, 3, Handle, Protocol, Interface);
  429. if (EFI_ERROR(Status)) {
  430. DEBUG((D_ERROR, "LibUninstallProtocolInterfaces: failed %g, %r\n", Protocol, Handle));
  431. }
  432. }
  433. va_end (args);
  434. }
  435. EFI_STATUS
  436. LibReinstallProtocolInterfaces (
  437. IN OUT EFI_HANDLE *Handle,
  438. ...
  439. )
  440. {
  441. va_list args;
  442. EFI_STATUS Status;
  443. EFI_GUID *Protocol;
  444. VOID *OldInterface, *NewInterface;
  445. EFI_TPL OldTpl;
  446. UINTN Index;
  447. //
  448. // Syncronize with notifcations
  449. //
  450. OldTpl = uefi_call_wrapper(BS->RaiseTPL, 1, TPL_NOTIFY);
  451. //
  452. // Install the protocol interfaces
  453. //
  454. Index = 0;
  455. Status = EFI_SUCCESS;
  456. va_start (args, Handle);
  457. while (!EFI_ERROR(Status)) {
  458. //
  459. // If protocol is NULL, then it's the end of the list
  460. //
  461. Protocol = va_arg(args, EFI_GUID *);
  462. if (!Protocol) {
  463. break;
  464. }
  465. OldInterface = va_arg(args, VOID *);
  466. NewInterface = va_arg(args, VOID *);
  467. //
  468. // Reinstall it
  469. //
  470. Status = uefi_call_wrapper(BS->ReinstallProtocolInterface, 4, Handle, Protocol, OldInterface, NewInterface);
  471. if (EFI_ERROR(Status)) {
  472. break;
  473. }
  474. Index += 1;
  475. }
  476. va_end (args);
  477. //
  478. // If there was an error, undo all the interfaces that were
  479. // reinstalled without any errors
  480. //
  481. if (EFI_ERROR(Status)) {
  482. va_start (args, Handle);
  483. while (Index) {
  484. Protocol = va_arg(args, EFI_GUID *);
  485. OldInterface = va_arg(args, VOID *);
  486. NewInterface = va_arg(args, VOID *);
  487. uefi_call_wrapper(BS->ReinstallProtocolInterface, 4, Handle, Protocol, NewInterface, OldInterface);
  488. Index -= 1;
  489. }
  490. va_end (args);
  491. }
  492. //
  493. // Done
  494. //
  495. uefi_call_wrapper(BS->RestoreTPL, 1, OldTpl);
  496. return Status;
  497. }