VirtualBox

Ignore:
Timestamp:
Aug 14, 2024 1:16:30 PM (4 months ago)
Author:
vboxsync
Message:

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

Location:
trunk/src/VBox/Devices/EFI/FirmwareNew
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/FirmwareNew

  • trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c

    r99404 r105670  
    1010
    1111#include "Dhcp6Impl.h"
     12
     13//
     14// Verifies the packet cursor is within the packet
     15// otherwise it is invalid
     16//
     17#define IS_INVALID_PACKET_CURSOR(PacketCursor, Packet) \
     18  (((*PacketCursor) < (Packet)->Dhcp6.Option) || \
     19   ((*PacketCursor) >= (Packet)->Dhcp6.Option + ((Packet)->Size - sizeof(EFI_DHCP6_HEADER))) \
     20  )                                                                            \
     21
    1222
    1323/**
     
    578588
    579589/**
    580   Append the option to Buf, and move Buf to the end.
    581 
    582   @param[in, out] Buf           The pointer to the buffer.
    583   @param[in]      OptType       The option type.
    584   @param[in]      OptLen        The length of option contents.
    585   @param[in]      Data          The pointer to the option content.
    586 
    587   @return         Buf           The position to append the next option.
    588 
    589 **/
    590 UINT8 *
     590  Append the option to Buf, update the length of packet, and move Buf to the end.
     591
     592  @param[in, out] Packet         A pointer to the packet, on success Packet->Length
     593                                 will be updated.
     594  @param[in, out] PacketCursor   The pointer in the packet, on success PacketCursor
     595                                 will be moved to the end of the option.
     596  @param[in]      OptType        The option type.
     597  @param[in]      OptLen         The length of option contents.
     598  @param[in]      Data           The pointer to the option content.
     599
     600  @retval   EFI_INVALID_PARAMETER An argument provided to the function was invalid
     601  @retval   EFI_BUFFER_TOO_SMALL  The buffer is too small to append the option.
     602  @retval   EFI_SUCCESS           The option is appended successfully.
     603
     604**/
     605EFI_STATUS
    591606Dhcp6AppendOption (
    592   IN OUT UINT8   *Buf,
    593   IN     UINT16  OptType,
    594   IN     UINT16  OptLen,
    595   IN     UINT8   *Data
    596   )
    597 {
     607  IN OUT EFI_DHCP6_PACKET  *Packet,
     608  IN OUT UINT8             **PacketCursor,
     609  IN     UINT16            OptType,
     610  IN     UINT16            OptLen,
     611  IN     UINT8             *Data
     612  )
     613{
     614  UINT32  Length;
     615  UINT32  BytesNeeded;
     616
    598617  //
    599618  //  The format of Dhcp6 option:
     
    608627  //
    609628
    610   ASSERT (OptLen != 0);
    611 
    612   WriteUnaligned16 ((UINT16 *)Buf, OptType);
    613   Buf += 2;
    614   WriteUnaligned16 ((UINT16 *)Buf, OptLen);
    615   Buf += 2;
    616   CopyMem (Buf, Data, NTOHS (OptLen));
    617   Buf += NTOHS (OptLen);
    618 
    619   return Buf;
     629  //
     630  // Verify the arguments are valid
     631  //
     632  if (Packet == NULL) {
     633    return EFI_INVALID_PARAMETER;
     634  }
     635
     636  if ((PacketCursor == NULL) || (*PacketCursor == NULL)) {
     637    return EFI_INVALID_PARAMETER;
     638  }
     639
     640  if (Data == NULL) {
     641    return EFI_INVALID_PARAMETER;
     642  }
     643
     644  if (OptLen == 0) {
     645    return EFI_INVALID_PARAMETER;
     646  }
     647
     648  //
     649  // Verify the PacketCursor is within the packet
     650  //
     651  if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) {
     652    return EFI_INVALID_PARAMETER;
     653  }
     654
     655  //
     656  // Calculate the bytes needed for the option
     657  //
     658  BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN + NTOHS (OptLen);
     659
     660  //
     661  // Space remaining in the packet
     662  //
     663  Length = Packet->Size - Packet->Length;
     664  if (Length < BytesNeeded) {
     665    return EFI_BUFFER_TOO_SMALL;
     666  }
     667
     668  WriteUnaligned16 ((UINT16 *)*PacketCursor, OptType);
     669  *PacketCursor += DHCP6_SIZE_OF_OPT_CODE;
     670  WriteUnaligned16 ((UINT16 *)*PacketCursor, OptLen);
     671  *PacketCursor += DHCP6_SIZE_OF_OPT_LEN;
     672  CopyMem (*PacketCursor, Data, NTOHS (OptLen));
     673  *PacketCursor += NTOHS (OptLen);
     674
     675  // Update the packet length by the length of the option + 4 bytes
     676  Packet->Length += BytesNeeded;
     677
     678  return EFI_SUCCESS;
    620679}
    621680
     
    623682  Append the appointed IA Address option to Buf, and move Buf to the end.
    624683
    625   @param[in, out] Buf           The pointer to the position to append.
     684  @param[in, out] Packet        A pointer to the packet, on success Packet->Length
     685                                will be updated.
     686  @param[in, out] PacketCursor  The pointer in the packet, on success PacketCursor
     687                                will be moved to the end of the option.
    626688  @param[in]      IaAddr        The pointer to the IA Address.
    627689  @param[in]      MessageType   Message type of DHCP6 package.
    628690
    629   @return         Buf           The position to append the next option.
    630 
    631 **/
    632 UINT8 *
     691  @retval   EFI_INVALID_PARAMETER An argument provided to the function was invalid
     692  @retval   EFI_BUFFER_TOO_SMALL  The buffer is too small to append the option.
     693  @retval   EFI_SUCCESS           The option is appended successfully.
     694
     695**/
     696EFI_STATUS
    633697Dhcp6AppendIaAddrOption (
    634   IN OUT UINT8                 *Buf,
     698  IN OUT EFI_DHCP6_PACKET      *Packet,
     699  IN OUT UINT8                 **PacketCursor,
    635700  IN     EFI_DHCP6_IA_ADDRESS  *IaAddr,
    636701  IN     UINT32                MessageType
    637702  )
    638703{
     704  UINT32  BytesNeeded;
     705  UINT32  Length;
     706
    639707  //  The format of the IA Address option is:
    640708  //
     
    659727
    660728  //
     729  // Verify the arguments are valid
     730  //
     731  if (Packet == NULL) {
     732    return EFI_INVALID_PARAMETER;
     733  }
     734
     735  if ((PacketCursor == NULL) || (*PacketCursor == NULL)) {
     736    return EFI_INVALID_PARAMETER;
     737  }
     738
     739  if (IaAddr == NULL) {
     740    return EFI_INVALID_PARAMETER;
     741  }
     742
     743  //
     744  // Verify the PacketCursor is within the packet
     745  //
     746  if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) {
     747    return EFI_INVALID_PARAMETER;
     748  }
     749
     750  BytesNeeded  = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN;
     751  BytesNeeded += sizeof (EFI_IPv6_ADDRESS);
     752  //
     753  // Even if the preferred-lifetime is 0, it still needs to store it.
     754  //
     755  BytesNeeded += sizeof (IaAddr->PreferredLifetime);
     756  //
     757  // Even if the valid-lifetime is 0, it still needs to store it.
     758  //
     759  BytesNeeded += sizeof (IaAddr->ValidLifetime);
     760
     761  //
     762  // Space remaining in the packet
     763  //
     764  Length = Packet->Size - Packet->Length;
     765  if (Length < BytesNeeded) {
     766    return EFI_BUFFER_TOO_SMALL;
     767  }
     768
     769  //
    661770  // Fill the value of Ia Address option type
    662771  //
    663   WriteUnaligned16 ((UINT16 *)Buf, HTONS (Dhcp6OptIaAddr));
    664   Buf += 2;
    665 
    666   WriteUnaligned16 ((UINT16 *)Buf, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS)));
    667   Buf += 2;
    668 
    669   CopyMem (Buf, &IaAddr->IpAddress, sizeof (EFI_IPv6_ADDRESS));
    670   Buf += sizeof (EFI_IPv6_ADDRESS);
     772  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Dhcp6OptIaAddr));
     773  *PacketCursor += DHCP6_SIZE_OF_OPT_CODE;
     774
     775  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS)));
     776  *PacketCursor += DHCP6_SIZE_OF_OPT_LEN;
     777
     778  CopyMem (*PacketCursor, &IaAddr->IpAddress, sizeof (EFI_IPv6_ADDRESS));
     779  *PacketCursor += sizeof (EFI_IPv6_ADDRESS);
    671780
    672781  //
     
    676785  //
    677786  if (MessageType != Dhcp6MsgConfirm) {
    678     WriteUnaligned32 ((UINT32 *)Buf, HTONL (IaAddr->PreferredLifetime));
    679   }
    680 
    681   Buf += 4;
     787    WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (IaAddr->PreferredLifetime));
     788  }
     789
     790  *PacketCursor += sizeof (IaAddr->PreferredLifetime);
    682791
    683792  if (MessageType != Dhcp6MsgConfirm) {
    684     WriteUnaligned32 ((UINT32 *)Buf, HTONL (IaAddr->ValidLifetime));
    685   }
    686 
    687   Buf += 4;
    688 
    689   return Buf;
     793    WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (IaAddr->ValidLifetime));
     794  }
     795
     796  *PacketCursor += sizeof (IaAddr->ValidLifetime);
     797
     798  //
     799  // Update the packet length
     800  //
     801  Packet->Length += BytesNeeded;
     802
     803  return EFI_SUCCESS;
    690804}
    691805
     
    693807  Append the appointed Ia option to Buf, and move Buf to the end.
    694808
    695   @param[in, out] Buf           The pointer to the position to append.
     809  @param[in, out] Packet        A pointer to the packet, on success Packet->Length
     810                                will be updated.
     811  @param[in, out] PacketCursor  The pointer in the packet, on success PacketCursor
     812                                will be moved to the end of the option.
    696813  @param[in]      Ia            The pointer to the Ia.
    697814  @param[in]      T1            The time of T1.
     
    699816  @param[in]      MessageType   Message type of DHCP6 package.
    700817
    701   @return         Buf           The position to append the next Ia option.
    702 
    703 **/
    704 UINT8 *
     818  @retval   EFI_INVALID_PARAMETER An argument provided to the function was invalid
     819  @retval   EFI_BUFFER_TOO_SMALL  The buffer is too small to append the option.
     820  @retval   EFI_SUCCESS           The option is appended successfully.
     821
     822**/
     823EFI_STATUS
    705824Dhcp6AppendIaOption (
    706   IN OUT UINT8         *Buf,
    707   IN     EFI_DHCP6_IA  *Ia,
    708   IN     UINT32        T1,
    709   IN     UINT32        T2,
    710   IN     UINT32        MessageType
    711   )
    712 {
    713   UINT8   *AddrOpt;
    714   UINT16  *Len;
    715   UINTN   Index;
     825  IN OUT EFI_DHCP6_PACKET  *Packet,
     826  IN OUT UINT8             **PacketCursor,
     827  IN     EFI_DHCP6_IA      *Ia,
     828  IN     UINT32            T1,
     829  IN     UINT32            T2,
     830  IN     UINT32            MessageType
     831  )
     832{
     833  UINT8       *AddrOpt;
     834  UINT16      *Len;
     835  UINTN       Index;
     836  UINT32      BytesNeeded;
     837  UINT32      Length;
     838  EFI_STATUS  Status;
    716839
    717840  //
     
    735858
    736859  //
     860  // Verify the arguments are valid
     861  //
     862  if (Packet == NULL) {
     863    return EFI_INVALID_PARAMETER;
     864  }
     865
     866  if ((PacketCursor == NULL) || (*PacketCursor == NULL)) {
     867    return EFI_INVALID_PARAMETER;
     868  }
     869
     870  if (Ia == NULL) {
     871    return EFI_INVALID_PARAMETER;
     872  }
     873
     874  //
     875  // Verify the PacketCursor is within the packet
     876  //
     877  if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) {
     878    return EFI_INVALID_PARAMETER;
     879  }
     880
     881  BytesNeeded  = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN;
     882  BytesNeeded += sizeof (Ia->Descriptor.IaId);
     883  //
     884  // + N for the IA_NA-options/IA_TA-options
     885  // Dhcp6AppendIaAddrOption will need to check the length for each address
     886  //
     887  if (Ia->Descriptor.Type == Dhcp6OptIana) {
     888    BytesNeeded += sizeof (T1) + sizeof (T2);
     889  }
     890
     891  //
     892  // Space remaining in the packet
     893  //
     894  Length = (UINT16)(Packet->Size - Packet->Length);
     895  if (Length < BytesNeeded) {
     896    return EFI_BUFFER_TOO_SMALL;
     897  }
     898
     899  //
    737900  // Fill the value of Ia option type
    738901  //
    739   WriteUnaligned16 ((UINT16 *)Buf, HTONS (Ia->Descriptor.Type));
    740   Buf += 2;
     902  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Ia->Descriptor.Type));
     903  *PacketCursor += DHCP6_SIZE_OF_OPT_CODE;
    741904
    742905  //
    743906  // Fill the len of Ia option later, keep the pointer first
    744907  //
    745   Len  = (UINT16 *)Buf;
    746   Buf += 2;
     908  Len            = (UINT16 *)*PacketCursor;
     909  *PacketCursor += DHCP6_SIZE_OF_OPT_LEN;
    747910
    748911  //
    749912  // Fill the value of iaid
    750913  //
    751   WriteUnaligned32 ((UINT32 *)Buf, HTONL (Ia->Descriptor.IaId));
    752   Buf += 4;
     914  WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (Ia->Descriptor.IaId));
     915  *PacketCursor += sizeof (Ia->Descriptor.IaId);
    753916
    754917  //
     
    756919  //
    757920  if (Ia->Descriptor.Type == Dhcp6OptIana) {
    758     WriteUnaligned32 ((UINT32 *)Buf, HTONL ((T1 != 0) ? T1 : 0xffffffff));
    759     Buf += 4;
    760     WriteUnaligned32 ((UINT32 *)Buf, HTONL ((T2 != 0) ? T2 : 0xffffffff));
    761     Buf += 4;
    762   }
     921    WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL ((T1 != 0) ? T1 : 0xffffffff));
     922    *PacketCursor += sizeof (T1);
     923    WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL ((T2 != 0) ? T2 : 0xffffffff));
     924    *PacketCursor += sizeof (T2);
     925  }
     926
     927  //
     928  // Update the packet length
     929  //
     930  Packet->Length += BytesNeeded;
    763931
    764932  //
     
    767935  for (Index = 0; Index < Ia->IaAddressCount; Index++) {
    768936    AddrOpt = (UINT8 *)Ia->IaAddress + Index * sizeof (EFI_DHCP6_IA_ADDRESS);
    769     Buf     = Dhcp6AppendIaAddrOption (Buf, (EFI_DHCP6_IA_ADDRESS *)AddrOpt, MessageType);
     937    Status  = Dhcp6AppendIaAddrOption (Packet, PacketCursor, (EFI_DHCP6_IA_ADDRESS *)AddrOpt, MessageType);
     938    if (EFI_ERROR (Status)) {
     939      return Status;
     940    }
    770941  }
    771942
     
    773944  // Fill the value of Ia option length
    774945  //
    775   *Len = HTONS ((UINT16)(Buf - (UINT8 *)Len - 2));
    776 
    777   return Buf;
     946  *Len = HTONS ((UINT16)(*PacketCursor - (UINT8 *)Len - 2));
     947
     948  return EFI_SUCCESS;
    778949}
    779950
     
    781952  Append the appointed Elapsed time option to Buf, and move Buf to the end.
    782953
    783   @param[in, out] Buf           The pointer to the position to append.
     954  @param[in, out] Packet        A pointer to the packet, on success Packet->Length
     955                                will be updated.
     956  @param[in, out] PacketCursor  The pointer in the packet, on success PacketCursor
     957                                will be moved to the end of the option.
    784958  @param[in]      Instance      The pointer to the Dhcp6 instance.
    785959  @param[out]     Elapsed       The pointer to the elapsed time value in
    786                                   the generated packet.
    787 
    788   @return         Buf           The position to append the next Ia option.
    789 
    790 **/
    791 UINT8 *
     960                                the generated packet.
     961
     962  @retval   EFI_INVALID_PARAMETER An argument provided to the function was invalid
     963  @retval   EFI_BUFFER_TOO_SMALL  The buffer is too small to append the option.
     964  @retval   EFI_SUCCESS           The option is appended successfully.
     965
     966**/
     967EFI_STATUS
    792968Dhcp6AppendETOption (
    793   IN OUT UINT8           *Buf,
    794   IN     DHCP6_INSTANCE  *Instance,
    795   OUT    UINT16          **Elapsed
    796   )
    797 {
     969  IN OUT EFI_DHCP6_PACKET  *Packet,
     970  IN OUT UINT8             **PacketCursor,
     971  IN     DHCP6_INSTANCE    *Instance,
     972  OUT    UINT16            **Elapsed
     973  )
     974{
     975  UINT32  BytesNeeded;
     976  UINT32  Length;
     977
    798978  //
    799979  //  The format of elapsed time option:
     
    808988
    809989  //
     990  // Verify the arguments are valid
     991  //
     992  if (Packet == NULL) {
     993    return EFI_INVALID_PARAMETER;
     994  }
     995
     996  if ((PacketCursor == NULL) || (*PacketCursor == NULL)) {
     997    return EFI_INVALID_PARAMETER;
     998  }
     999
     1000  if (Instance == NULL) {
     1001    return EFI_INVALID_PARAMETER;
     1002  }
     1003
     1004  if ((Elapsed == NULL)) {
     1005    return EFI_INVALID_PARAMETER;
     1006  }
     1007
     1008  //
     1009  // Verify the PacketCursor is within the packet
     1010  //
     1011  if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) {
     1012    return EFI_INVALID_PARAMETER;
     1013  }
     1014
     1015  BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN;
     1016  //
     1017  // + 2 for elapsed-time
     1018  //
     1019  BytesNeeded += sizeof (UINT16);
     1020  //
     1021  // Space remaining in the packet
     1022  //
     1023  Length = Packet->Size - Packet->Length;
     1024  if (Length < BytesNeeded) {
     1025    return EFI_BUFFER_TOO_SMALL;
     1026  }
     1027
     1028  //
    8101029  // Fill the value of elapsed-time option type.
    8111030  //
    812   WriteUnaligned16 ((UINT16 *)Buf, HTONS (Dhcp6OptElapsedTime));
    813   Buf += 2;
     1031  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Dhcp6OptElapsedTime));
     1032  *PacketCursor += DHCP6_SIZE_OF_OPT_CODE;
    8141033
    8151034  //
    8161035  // Fill the len of elapsed-time option, which is fixed.
    8171036  //
    818   WriteUnaligned16 ((UINT16 *)Buf, HTONS (2));
    819   Buf += 2;
     1037  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (2));
     1038  *PacketCursor += DHCP6_SIZE_OF_OPT_LEN;
    8201039
    8211040  //
     
    8231042  // filled in later just before the packet is transmitted.
    8241043  //
    825   WriteUnaligned16 ((UINT16 *)Buf, HTONS (0));
    826   *Elapsed = (UINT16 *)Buf;
    827   Buf     += 2;
    828 
    829   return Buf;
     1044  WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (0));
     1045  *Elapsed       = (UINT16 *)*PacketCursor;
     1046  *PacketCursor += sizeof (UINT16);
     1047
     1048  Packet->Length += BytesNeeded;
     1049
     1050  return EFI_SUCCESS;
    8301051}
    8311052
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette