DevicePathUtilities.c 28 KB

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