Coverage Report

Created: 2024-10-17 06:29

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