DevicePathUtilities.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /** @file
  2. Device Path services. The thing to remember is device paths are built out of
  3. nodes. The device path is terminated by an end node that is length
  4. sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
  5. all over this file.
  6. The only place where multi-instance device paths are supported is in
  7. environment variables. Multi-instance device paths should never be placed
  8. on a Handle.
  9. Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
  10. SPDX-License-Identifier: BSD-2-Clause-Patent
  11. **/
  12. #include "UefiDevicePathLib.h"
  13. #include <Protocol/DevicePathUtilities.h>
  14. //
  15. // Template for an end-of-device path node.
  16. //
  17. CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {
  18. END_DEVICE_PATH_TYPE,
  19. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  20. {
  21. END_DEVICE_PATH_LENGTH,
  22. 0
  23. }
  24. };
  25. /**
  26. Determine whether a given device path is valid.
  27. @param DevicePath A pointer to a device path data structure.
  28. @param MaxSize The maximum size of the device path data structure.
  29. @retval TRUE DevicePath is valid.
  30. @retval FALSE DevicePath is NULL.
  31. @retval FALSE Maxsize is less than sizeof(EFI_DEVICE_PATH_PROTOCOL).
  32. @retval FALSE The length of any node node in the DevicePath is less
  33. than sizeof (EFI_DEVICE_PATH_PROTOCOL).
  34. @retval FALSE If MaxSize is not zero, the size of the DevicePath
  35. exceeds MaxSize.
  36. @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
  37. count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
  38. **/
  39. BOOLEAN
  40. IsDevicePathValid (
  41. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  42. UINTN MaxSize
  43. )
  44. {
  45. UINTN Count;
  46. UINTN Size;
  47. UINTN NodeLength;
  48. //
  49. // Validate the input whether exists and its size big enough to touch the first node
  50. //
  51. if (DevicePath == NULL || (MaxSize > 0 && MaxSize < END_DEVICE_PATH_LENGTH)) {
  52. return FALSE;
  53. }
  54. if (MaxSize == 0) {
  55. MaxSize = MAX_UINT32;
  56. }
  57. for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {
  58. NodeLength = DevicePathNodeLength (DevicePath);
  59. if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
  60. return FALSE;
  61. }
  62. if (NodeLength > MAX_UINT32 - Size) {
  63. return FALSE;
  64. }
  65. Size += NodeLength;
  66. //
  67. // Validate next node before touch it.
  68. //
  69. if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {
  70. return FALSE;
  71. }
  72. Count++;
  73. if (Count >= MAX_DEVICE_PATH_NODE_COUNT) {
  74. return FALSE;
  75. }
  76. }
  77. //
  78. // Only return TRUE when the End Device Path node is valid.
  79. //
  80. return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);
  81. }
  82. /**
  83. Returns the Type field of a device path node.
  84. Returns the Type field of the device path node specified by Node.
  85. If Node is NULL, then ASSERT().
  86. @param Node A pointer to a device path node data structure.
  87. @return The Type field of the device path node specified by Node.
  88. **/
  89. UINT8
  90. DevicePathType (
  91. CONST VOID *Node
  92. )
  93. {
  94. ASSERT (Node != NULL);
  95. return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;
  96. }
  97. /**
  98. Returns the SubType field of a device path node.
  99. Returns the SubType field of the device path node specified by Node.
  100. If Node is NULL, then ASSERT().
  101. @param Node A pointer to a device path node data structure.
  102. @return The SubType field of the device path node specified by Node.
  103. **/
  104. UINT8
  105. DevicePathSubType (
  106. CONST VOID *Node
  107. )
  108. {
  109. ASSERT (Node != NULL);
  110. return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;
  111. }
  112. /**
  113. Returns the 16-bit Length field of a device path node.
  114. Returns the 16-bit Length field of the device path node specified by Node.
  115. Node is not required to be aligned on a 16-bit boundary, so it is recommended
  116. that a function such as ReadUnaligned16() be used to extract the contents of
  117. the Length field.
  118. If Node is NULL, then ASSERT().
  119. @param Node A pointer to a device path node data structure.
  120. @return The 16-bit Length field of the device path node specified by Node.
  121. **/
  122. UINTN
  123. DevicePathNodeLength (
  124. CONST VOID *Node
  125. )
  126. {
  127. ASSERT (Node != NULL);
  128. return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);
  129. }
  130. /**
  131. Returns a pointer to the next node in a device path.
  132. Returns a pointer to the device path node that follows the device path node
  133. specified by Node.
  134. If Node is NULL, then ASSERT().
  135. @param Node A pointer to a device path node data structure.
  136. @return a pointer to the device path node that follows the device path node
  137. specified by Node.
  138. **/
  139. EFI_DEVICE_PATH_PROTOCOL *
  140. NextDevicePathNode (
  141. CONST VOID *Node
  142. )
  143. {
  144. ASSERT (Node != NULL);
  145. return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));
  146. }
  147. /**
  148. Determines if a device path node is an end node of a device path.
  149. This includes nodes that are the end of a device path instance and nodes that
  150. are the end of an entire device path.
  151. Determines if the device path node specified by Node is an end node of a device path.
  152. This includes nodes that are the end of a device path instance and nodes that are the
  153. end of an entire device path. If Node represents an end node of a device path,
  154. then TRUE is returned. Otherwise, FALSE is returned.
  155. If Node is NULL, then ASSERT().
  156. @param Node A pointer to a device path node data structure.
  157. @retval TRUE The device path node specified by Node is an end node of a
  158. device path.
  159. @retval FALSE The device path node specified by Node is not an end node of
  160. a device path.
  161. **/
  162. BOOLEAN
  163. IsDevicePathEndType (
  164. CONST VOID *Node
  165. )
  166. {
  167. ASSERT (Node != NULL);
  168. return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);
  169. }
  170. /**
  171. Determines if a device path node is an end node of an entire device path.
  172. Determines if a device path node specified by Node is an end node of an entire
  173. device path. If Node represents the end of an entire device path, then TRUE is
  174. returned. Otherwise, FALSE is returned.
  175. If Node is NULL, then ASSERT().
  176. @param Node A pointer to a device path node data structure.
  177. @retval TRUE The device path node specified by Node is the end of an entire
  178. device path.
  179. @retval FALSE The device path node specified by Node is not the end of an
  180. entire device path.
  181. **/
  182. BOOLEAN
  183. IsDevicePathEnd (
  184. CONST VOID *Node
  185. )
  186. {
  187. ASSERT (Node != NULL);
  188. return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);
  189. }
  190. /**
  191. Determines if a device path node is an end node of a device path instance.
  192. Determines if a device path node specified by Node is an end node of a device
  193. path instance. If Node represents the end of a device path instance, then TRUE
  194. is returned. Otherwise, FALSE is returned.
  195. If Node is NULL, then ASSERT().
  196. @param Node A pointer to a device path node data structure.
  197. @retval TRUE The device path node specified by Node is the end of a device
  198. path instance.
  199. @retval FALSE The device path node specified by Node is not the end of a
  200. device path instance.
  201. **/
  202. BOOLEAN
  203. IsDevicePathEndInstance (
  204. CONST VOID *Node
  205. )
  206. {
  207. ASSERT (Node != NULL);
  208. return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);
  209. }
  210. /**
  211. Sets the length, in bytes, of a device path node.
  212. Sets the length of the device path node specified by Node to the value specified
  213. by NodeLength. NodeLength is returned. Node is not required to be aligned on
  214. a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
  215. be used to set the contents of the Length field.
  216. If Node is NULL, then ASSERT().
  217. If NodeLength >= SIZE_64KB, then ASSERT().
  218. If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
  219. @param Node A pointer to a device path node data structure.
  220. @param Length The length, in bytes, of the device path node.
  221. @return Length
  222. **/
  223. UINT16
  224. SetDevicePathNodeLength (
  225. VOID *Node,
  226. UINTN Length
  227. )
  228. {
  229. ASSERT (Node != NULL);
  230. ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));
  231. return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));
  232. }
  233. /**
  234. Fills in all the fields of a device path node that is the end of an entire device path.
  235. Fills in all the fields of a device path node specified by Node so Node represents
  236. the end of an entire device path. The Type field of Node is set to
  237. END_DEVICE_PATH_TYPE, the SubType field of Node is set to
  238. END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
  239. END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
  240. so it is recommended that a function such as WriteUnaligned16() be used to set
  241. the contents of the Length field.
  242. If Node is NULL, then ASSERT().
  243. @param Node A pointer to a device path node data structure.
  244. **/
  245. VOID
  246. SetDevicePathEndNode (
  247. VOID *Node
  248. )
  249. {
  250. ASSERT (Node != NULL);
  251. memcpy (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));
  252. }
  253. /**
  254. Returns the size of a device path in bytes.
  255. This function returns the size, in bytes, of the device path data structure
  256. specified by DevicePath including the end of device path node.
  257. If DevicePath is NULL or invalid, then 0 is returned.
  258. @param DevicePath A pointer to a device path data structure.
  259. @retval 0 If DevicePath is NULL or invalid.
  260. @retval Others The size of a device path in bytes.
  261. **/
  262. UINTN
  263. UefiDevicePathLibGetDevicePathSize (
  264. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  265. )
  266. {
  267. CONST EFI_DEVICE_PATH_PROTOCOL *Start;
  268. if (DevicePath == NULL) {
  269. return 0;
  270. }
  271. if (!IsDevicePathValid (DevicePath, 0)) {
  272. return 0;
  273. }
  274. //
  275. // Search for the end of the device path structure
  276. //
  277. Start = DevicePath;
  278. while (!IsDevicePathEnd (DevicePath)) {
  279. DevicePath = NextDevicePathNode (DevicePath);
  280. }
  281. //
  282. // Compute the size and add back in the size of the end device path structure
  283. //
  284. return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);
  285. }
  286. /**
  287. Creates a new copy of an existing device path.
  288. This function allocates space for a new copy of the device path specified by DevicePath.
  289. If DevicePath is NULL, then NULL is returned. If the memory is successfully
  290. allocated, then the contents of DevicePath are copied to the newly allocated
  291. buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
  292. The memory for the new device path is allocated from EFI boot services memory.
  293. It is the responsibility of the caller to free the memory allocated.
  294. @param DevicePath A pointer to a device path data structure.
  295. @retval NULL DevicePath is NULL or invalid.
  296. @retval Others A pointer to the duplicated device path.
  297. **/
  298. EFI_DEVICE_PATH_PROTOCOL *
  299. UefiDevicePathLibDuplicateDevicePath (
  300. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  301. )
  302. {
  303. UINTN Size;
  304. //
  305. // Compute the size
  306. //
  307. Size = GetDevicePathSize (DevicePath);
  308. if (Size == 0) {
  309. return NULL;
  310. }
  311. //
  312. // Allocate space for duplicate device path
  313. //
  314. return AllocateCopyPool (Size, DevicePath);
  315. }
  316. /**
  317. Creates a new device path by appending a second device path to a first device path.
  318. This function creates a new device path by appending a copy of SecondDevicePath
  319. to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
  320. device node from SecondDevicePath is retained. The newly created device path is
  321. returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
  322. SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
  323. and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
  324. SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
  325. If there is not enough memory for the newly allocated buffer, then NULL is returned.
  326. The memory for the new device path is allocated from EFI boot services memory.
  327. It is the responsibility of the caller to free the memory allocated.
  328. @param FirstDevicePath A pointer to a device path data structure.
  329. @param SecondDevicePath A pointer to a device path data structure.
  330. @retval NULL If there is not enough memory for the newly allocated buffer.
  331. @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
  332. @retval Others A pointer to the new device path if success.
  333. Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
  334. **/
  335. EFI_DEVICE_PATH_PROTOCOL *
  336. UefiDevicePathLibAppendDevicePath (
  337. CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,
  338. CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath
  339. )
  340. {
  341. UINTN Size;
  342. UINTN Size1;
  343. UINTN Size2;
  344. EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
  345. EFI_DEVICE_PATH_PROTOCOL *DevicePath2;
  346. //
  347. // If there's only 1 path, just duplicate it.
  348. //
  349. if (FirstDevicePath == NULL) {
  350. return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);
  351. }
  352. if (SecondDevicePath == NULL) {
  353. return DuplicateDevicePath (FirstDevicePath);
  354. }
  355. if (!IsDevicePathValid (FirstDevicePath, 0) || !IsDevicePathValid (SecondDevicePath, 0)) {
  356. return NULL;
  357. }
  358. //
  359. // Allocate space for the combined device path. It only has one end node of
  360. // length EFI_DEVICE_PATH_PROTOCOL.
  361. //
  362. Size1 = GetDevicePathSize (FirstDevicePath);
  363. Size2 = GetDevicePathSize (SecondDevicePath);
  364. Size = Size1 + Size2 - END_DEVICE_PATH_LENGTH;
  365. NewDevicePath = AllocatePool (Size);
  366. if (NewDevicePath != NULL) {
  367. NewDevicePath = memcpy (NewDevicePath, FirstDevicePath, Size1);
  368. //
  369. // Over write FirstDevicePath EndNode and do the copy
  370. //
  371. DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +
  372. (Size1 - END_DEVICE_PATH_LENGTH));
  373. memcpy (DevicePath2, SecondDevicePath, Size2);
  374. }
  375. return NewDevicePath;
  376. }
  377. /**
  378. Creates a new path by appending the device node to the device path.
  379. This function creates a new device path by appending a copy of the device node
  380. specified by DevicePathNode to a copy of the device path specified by DevicePath
  381. in an allocated buffer. The end-of-device-path device node is moved after the
  382. end of the appended device node.
  383. If DevicePathNode is NULL then a copy of DevicePath is returned.
  384. If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
  385. path device node is returned.
  386. If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
  387. device node is returned.
  388. If there is not enough memory to allocate space for the new device path, then
  389. NULL is returned.
  390. The memory is allocated from EFI boot services memory. It is the responsibility
  391. of the caller to free the memory allocated.
  392. @param DevicePath A pointer to a device path data structure.
  393. @param DevicePathNode A pointer to a single device path node.
  394. @retval NULL If there is not enough memory for the new device path.
  395. @retval Others A pointer to the new device path if success.
  396. A copy of DevicePathNode followed by an end-of-device-path node
  397. if both FirstDevicePath and SecondDevicePath are NULL.
  398. A copy of an end-of-device-path node if both FirstDevicePath
  399. and SecondDevicePath are NULL.
  400. **/
  401. EFI_DEVICE_PATH_PROTOCOL *
  402. UefiDevicePathLibAppendDevicePathNode (
  403. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  404. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode
  405. )
  406. {
  407. EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
  408. EFI_DEVICE_PATH_PROTOCOL *NextNode;
  409. EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
  410. UINTN NodeLength;
  411. if (DevicePathNode == NULL) {
  412. return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);
  413. }
  414. //
  415. // Build a Node that has a terminator on it
  416. //
  417. NodeLength = DevicePathNodeLength (DevicePathNode);
  418. TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);
  419. if (TempDevicePath == NULL) {
  420. return NULL;
  421. }
  422. TempDevicePath = memcpy (TempDevicePath, DevicePathNode, NodeLength);
  423. //
  424. // Add and end device path node to convert Node to device path
  425. //
  426. NextNode = NextDevicePathNode (TempDevicePath);
  427. SetDevicePathEndNode (NextNode);
  428. //
  429. // Append device paths
  430. //
  431. NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);
  432. free (TempDevicePath);
  433. return NewDevicePath;
  434. }
  435. /**
  436. Creates a new device path by appending the specified device path instance to the specified device
  437. path.
  438. This function creates a new device path by appending a copy of the device path
  439. instance specified by DevicePathInstance to a copy of the device path specified
  440. by DevicePath in a allocated buffer.
  441. The end-of-device-path device node is moved after the end of the appended device
  442. path instance and a new end-of-device-path-instance node is inserted between.
  443. If DevicePath is NULL, then a copy if DevicePathInstance is returned.
  444. If DevicePathInstance is NULL, then NULL is returned.
  445. If DevicePath or DevicePathInstance is invalid, then NULL is returned.
  446. If there is not enough memory to allocate space for the new device path, then
  447. NULL is returned.
  448. The memory is allocated from EFI boot services memory. It is the responsibility
  449. of the caller to free the memory allocated.
  450. @param DevicePath A pointer to a device path data structure.
  451. @param DevicePathInstance A pointer to a device path instance.
  452. @return A pointer to the new device path.
  453. **/
  454. EFI_DEVICE_PATH_PROTOCOL *
  455. UefiDevicePathLibAppendDevicePathInstance (
  456. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  457. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance
  458. )
  459. {
  460. EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
  461. EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
  462. UINTN SrcSize;
  463. UINTN InstanceSize;
  464. if (DevicePath == NULL) {
  465. return DuplicateDevicePath (DevicePathInstance);
  466. }
  467. if (DevicePathInstance == NULL) {
  468. return NULL;
  469. }
  470. if (!IsDevicePathValid (DevicePath, 0) || !IsDevicePathValid (DevicePathInstance, 0)) {
  471. return NULL;
  472. }
  473. SrcSize = GetDevicePathSize (DevicePath);
  474. InstanceSize = GetDevicePathSize (DevicePathInstance);
  475. NewDevicePath = AllocatePool (SrcSize + InstanceSize);
  476. if (NewDevicePath != NULL) {
  477. TempDevicePath = memcpy (NewDevicePath, DevicePath, SrcSize);;
  478. while (!IsDevicePathEnd (TempDevicePath)) {
  479. TempDevicePath = NextDevicePathNode (TempDevicePath);
  480. }
  481. TempDevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;
  482. TempDevicePath = NextDevicePathNode (TempDevicePath);
  483. memcpy (TempDevicePath, DevicePathInstance, InstanceSize);
  484. }
  485. return NewDevicePath;
  486. }
  487. /**
  488. Creates a copy of the current device path instance and returns a pointer to the next device path
  489. instance.
  490. This function creates a copy of the current device path instance. It also updates
  491. DevicePath to point to the next device path instance in the device path (or NULL
  492. if no more) and updates Size to hold the size of the device path instance copy.
  493. If DevicePath is NULL, then NULL is returned.
  494. If DevicePath points to a invalid device path, then NULL is returned.
  495. If there is not enough memory to allocate space for the new device path, then
  496. NULL is returned.
  497. The memory is allocated from EFI boot services memory. It is the responsibility
  498. of the caller to free the memory allocated.
  499. If Size is NULL, then ASSERT().
  500. @param DevicePath On input, this holds the pointer to the current
  501. device path instance. On output, this holds
  502. the pointer to the next device path instance
  503. or NULL if there are no more device path
  504. instances in the device path pointer to a
  505. device path data structure.
  506. @param Size On output, this holds the size of the device
  507. path instance, in bytes or zero, if DevicePath
  508. is NULL.
  509. @return A pointer to the current device path instance.
  510. **/
  511. EFI_DEVICE_PATH_PROTOCOL *
  512. UefiDevicePathLibGetNextDevicePathInstance (
  513. EFI_DEVICE_PATH_PROTOCOL **DevicePath,
  514. UINTN *Size
  515. )
  516. {
  517. EFI_DEVICE_PATH_PROTOCOL *DevPath;
  518. EFI_DEVICE_PATH_PROTOCOL *ReturnValue;
  519. UINT8 Temp;
  520. ASSERT (Size != NULL);
  521. if (DevicePath == NULL || *DevicePath == NULL) {
  522. *Size = 0;
  523. return NULL;
  524. }
  525. if (!IsDevicePathValid (*DevicePath, 0)) {
  526. return NULL;
  527. }
  528. //
  529. // Find the end of the device path instance
  530. //
  531. DevPath = *DevicePath;
  532. while (!IsDevicePathEndType (DevPath)) {
  533. DevPath = NextDevicePathNode (DevPath);
  534. }
  535. //
  536. // Compute the size of the device path instance
  537. //
  538. *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
  539. //
  540. // Make a copy and return the device path instance
  541. //
  542. Temp = DevPath->SubType;
  543. DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  544. ReturnValue = DuplicateDevicePath (*DevicePath);
  545. DevPath->SubType = Temp;
  546. //
  547. // If DevPath is the end of an entire device path, then another instance
  548. // does not follow, so *DevicePath is set to NULL.
  549. //
  550. if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {
  551. *DevicePath = NULL;
  552. } else {
  553. *DevicePath = NextDevicePathNode (DevPath);
  554. }
  555. return ReturnValue;
  556. }
  557. /**
  558. Creates a device node.
  559. This function creates a new device node in a newly allocated buffer of size
  560. NodeLength and initializes the device path node header with NodeType and NodeSubType.
  561. The new device path node is returned.
  562. If NodeLength is smaller than a device path header, then NULL is returned.
  563. If there is not enough memory to allocate space for the new device path, then
  564. NULL is returned.
  565. The memory is allocated from EFI boot services memory. It is the responsibility
  566. of the caller to free the memory allocated.
  567. @param NodeType The device node type for the new device node.
  568. @param NodeSubType The device node sub-type for the new device node.
  569. @param NodeLength The length of the new device node.
  570. @return The new device path.
  571. **/
  572. EFI_DEVICE_PATH_PROTOCOL *
  573. UefiDevicePathLibCreateDeviceNode (
  574. UINT8 NodeType,
  575. UINT8 NodeSubType,
  576. UINT16 NodeLength
  577. )
  578. {
  579. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  580. if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
  581. //
  582. // NodeLength is less than the size of the header.
  583. //
  584. return NULL;
  585. }
  586. DevicePath = AllocateZeroPool (NodeLength);
  587. if (DevicePath != NULL) {
  588. DevicePath->Type = NodeType;
  589. DevicePath->SubType = NodeSubType;
  590. SetDevicePathNodeLength (DevicePath, NodeLength);
  591. }
  592. return DevicePath;
  593. }
  594. /**
  595. Determines if a device path is single or multi-instance.
  596. This function returns TRUE if the device path specified by DevicePath is
  597. multi-instance.
  598. Otherwise, FALSE is returned.
  599. If DevicePath is NULL or invalid, then FALSE is returned.
  600. @param DevicePath A pointer to a device path data structure.
  601. @retval TRUE DevicePath is multi-instance.
  602. @retval FALSE DevicePath is not multi-instance, or DevicePath
  603. is NULL or invalid.
  604. **/
  605. BOOLEAN
  606. UefiDevicePathLibIsDevicePathMultiInstance (
  607. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  608. )
  609. {
  610. CONST EFI_DEVICE_PATH_PROTOCOL *Node;
  611. if (DevicePath == NULL) {
  612. return FALSE;
  613. }
  614. if (!IsDevicePathValid (DevicePath, 0)) {
  615. return FALSE;
  616. }
  617. Node = DevicePath;
  618. while (!IsDevicePathEnd (Node)) {
  619. if (IsDevicePathEndInstance (Node)) {
  620. return TRUE;
  621. }
  622. Node = NextDevicePathNode (Node);
  623. }
  624. return FALSE;
  625. }
  626. /**
  627. Retrieves the device path protocol from a handle.
  628. This function returns the device path protocol from the handle specified by Handle.
  629. If Handle is NULL or Handle does not contain a device path protocol, then NULL
  630. is returned.
  631. @param Handle The handle from which to retrieve the device
  632. path protocol.
  633. @return The device path protocol from the handle specified by Handle.
  634. **/
  635. /*
  636. EFI_DEVICE_PATH_PROTOCOL *
  637. DevicePathFromHandle (
  638. EFI_HANDLE Handle
  639. )
  640. {
  641. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  642. EFI_STATUS Status;
  643. Status = gBS->HandleProtocol (
  644. Handle,
  645. &gEfiDevicePathProtocolGuid,
  646. (VOID *) &DevicePath
  647. );
  648. if (EFI_ERROR (Status)) {
  649. DevicePath = NULL;
  650. }
  651. return DevicePath;
  652. }
  653. */
  654. /**
  655. Allocates a device path for a file and appends it to an existing device path.
  656. If Device is a valid device handle that contains a device path protocol, then a device path for
  657. the file specified by FileName is allocated and appended to the device path associated with the
  658. handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
  659. that does not support the device path protocol, then a device path containing a single device
  660. path node for the file specified by FileName is allocated and returned.
  661. The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
  662. of the caller to free the memory allocated.
  663. If FileName is NULL, then ASSERT().
  664. If FileName is not aligned on a 16-bit boundary, then ASSERT().
  665. @param Device A pointer to a device handle. This parameter
  666. is optional and may be NULL.
  667. @param FileName A pointer to a Null-terminated Unicode string.
  668. @return The allocated device path.
  669. **/
  670. EFI_DEVICE_PATH_PROTOCOL *
  671. FileDevicePath (
  672. EFI_HANDLE Device, OPTIONAL
  673. CONST CHAR16 *FileName
  674. )
  675. {
  676. UINTN Size;
  677. FILEPATH_DEVICE_PATH *FilePath;
  678. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  679. EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;
  680. DevicePath = NULL;
  681. Size = StrSize (FileName);
  682. FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);
  683. if (FileDevicePath != NULL) {
  684. FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;
  685. FilePath->Header.Type = MEDIA_DEVICE_PATH;
  686. FilePath->Header.SubType = MEDIA_FILEPATH_DP;
  687. memcpy (&FilePath->PathName, FileName, Size);
  688. SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);
  689. SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));
  690. //if (Device != NULL) {
  691. // DevicePath = DevicePathFromHandle (Device);
  692. //}
  693. DevicePath = AppendDevicePath (DevicePath, FileDevicePath);
  694. free (FileDevicePath);
  695. }
  696. return DevicePath;
  697. }