X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FInclude%2FLibrary%2FBaseLib.h;h=123ae19dc263dc98e73174e877b37473b7d28a2a;hp=d65b01adf65b62309b4819595483f66979c8f0f0;hb=d7634dc0c5a83360b3b6c155df29c078ad9c77ce;hpb=c058d59f40b4a95c218cd171ff976bee1ff487e2 diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h index d65b01adf6..123ae19dc2 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -1,8 +1,8 @@ /** @file Provides string functions, linked list functions, math functions, synchronization - functions, and CPU architecture-specific functions. + functions, file path functions, and CPU architecture-specific functions. -Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -187,6 +187,8 @@ typedef struct { /** Returns the length of a Null-terminated Unicode string. + This function is similar as strlen_s defined in C11. + If String is not aligned on a 16-bit boundary, then ASSERT(). @param String A pointer to a Null-terminated Unicode string. @@ -205,12 +207,45 @@ StrnLenS ( IN UINTN MaxSize ); +/** + Returns the size of a Null-terminated Unicode string in bytes, including the + Null terminator. + + This function returns the size of the Null-terminated Unicode string + specified by String in bytes, including the Null terminator. + + If String is not aligned on a 16-bit boundary, then ASSERT(). + + @param String A pointer to a Null-terminated Unicode string. + @param MaxSize The maximum number of Destination Unicode + char, including the Null terminator. + + @retval 0 If String is NULL. + @retval (sizeof (CHAR16) * (MaxSize + 1)) + If there is no Null terminator in the first MaxSize characters of + String. + @return The size of the Null-terminated Unicode string in bytes, including + the Null terminator. + +**/ +UINTN +EFIAPI +StrnSizeS ( + IN CONST CHAR16 *String, + IN UINTN MaxSize + ); + /** Copies the string pointed to by Source (including the terminating null char) to the array pointed to by Destination. + This function is similar as strcpy_s defined in C11. + If Destination is not aligned on a 16-bit boundary, then ASSERT(). If Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. @param Destination A pointer to a Null-terminated Unicode string. @param DestMax The maximum number of Destination Unicode @@ -222,7 +257,7 @@ StrnLenS ( @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumUnicodeStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumUnicodeStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -240,8 +275,13 @@ StrCpyS ( Source to the array pointed to by Destination. If no null char is copied from Source, then Destination[Length] is always set to null. + This function is similar as strncpy_s defined in C11. + If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT(). If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. @param Destination A pointer to a Null-terminated Unicode string. @param DestMax The maximum number of Destination Unicode @@ -250,12 +290,12 @@ StrCpyS ( @param Length The maximum number of Unicode characters to copy. @retval RETURN_SUCCESS String is copied. - @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than MIN(StrLen(Source), Length). @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumUnicodeStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumUnicodeStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -273,8 +313,13 @@ StrnCpyS ( Appends a copy of the string pointed to by Source (including the terminating null char) to the end of the string pointed to by Destination. + This function is similar as strcat_s defined in C11. + If Destination is not aligned on a 16-bit boundary, then ASSERT(). If Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. @param Destination A pointer to a Null-terminated Unicode string. @param DestMax The maximum number of Destination Unicode @@ -282,14 +327,14 @@ StrnCpyS ( @param Source A pointer to a Null-terminated Unicode string. @retval RETURN_SUCCESS String is appended. - @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than + @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than StrLen(Destination). @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT greater than StrLen(Source). @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumUnicodeStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumUnicodeStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -308,8 +353,13 @@ StrCatS ( copied from Source, then Destination[StrLen(Destination) + Length] is always set to null. + This function is similar as strncat_s defined in C11. + If Destination is not aligned on a 16-bit boundary, then ASSERT(). - If and Source is not aligned on a 16-bit boundary, then ASSERT(). + If Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. @param Destination A pointer to a Null-terminated Unicode string. @param DestMax The maximum number of Destination Unicode @@ -325,7 +375,7 @@ StrCatS ( @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumUnicodeStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumUnicodeStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -339,9 +389,245 @@ StrnCatS ( IN UINTN Length ); +/** + Convert a Null-terminated Unicode decimal string to a value of type UINTN. + + This function outputs a value of type UINTN by interpreting the contents of + the Unicode string specified by String as a decimal number. The format of the + input Unicode string String is: + + [spaces] [decimal digits]. + + The valid decimal digit character is in the range [0-9]. The function will + ignore the pad space, which includes spaces or tab characters, before + [decimal digits]. The running zero in the beginning of [decimal digits] will + be ignored. Then, the function stops at the first character that is a not a + valid decimal character or a Null-terminator, whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If String is not aligned in a 16-bit boundary, then ASSERT(). + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid decimal digits in the above format, then 0 is stored + at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINTN, then + MAX_UINTN is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + decimal digits right after the optional pad spaces, the value of String is + stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINTN. + +**/ +RETURN_STATUS +EFIAPI +StrDecimalToUintnS ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT UINTN *Data + ); + +/** + Convert a Null-terminated Unicode decimal string to a value of type UINT64. + + This function outputs a value of type UINT64 by interpreting the contents of + the Unicode string specified by String as a decimal number. The format of the + input Unicode string String is: + + [spaces] [decimal digits]. + + The valid decimal digit character is in the range [0-9]. The function will + ignore the pad space, which includes spaces or tab characters, before + [decimal digits]. The running zero in the beginning of [decimal digits] will + be ignored. Then, the function stops at the first character that is a not a + valid decimal character or a Null-terminator, whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If String is not aligned in a 16-bit boundary, then ASSERT(). + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid decimal digits in the above format, then 0 is stored + at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINT64, then + MAX_UINT64 is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + decimal digits right after the optional pad spaces, the value of String is + stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINT64. + +**/ +RETURN_STATUS +EFIAPI +StrDecimalToUint64S ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT UINT64 *Data + ); + +/** + Convert a Null-terminated Unicode hexadecimal string to a value of type + UINTN. + + This function outputs a value of type UINTN by interpreting the contents of + the Unicode string specified by String as a hexadecimal number. The format of + the input Unicode string String is: + + [spaces][zeros][x][hexadecimal digits]. + + The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. + The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. + If "x" appears in the input string, it must be prefixed with at least one 0. + The function will ignore the pad space, which includes spaces or tab + characters, before [zeros], [x] or [hexadecimal digit]. The running zero + before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts + after [x] or the first valid hexadecimal digit. Then, the function stops at + the first character that is a not a valid hexadecimal character or NULL, + whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If String is not aligned in a 16-bit boundary, then ASSERT(). + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid hexadecimal digits in the above format, then 0 is + stored at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINTN, then + MAX_UINTN is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + hexadecimal digits right after the optional pad spaces, the value of String + is stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINTN. + +**/ +RETURN_STATUS +EFIAPI +StrHexToUintnS ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT UINTN *Data + ); + +/** + Convert a Null-terminated Unicode hexadecimal string to a value of type + UINT64. + + This function outputs a value of type UINT64 by interpreting the contents of + the Unicode string specified by String as a hexadecimal number. The format of + the input Unicode string String is: + + [spaces][zeros][x][hexadecimal digits]. + + The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. + The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. + If "x" appears in the input string, it must be prefixed with at least one 0. + The function will ignore the pad space, which includes spaces or tab + characters, before [zeros], [x] or [hexadecimal digit]. The running zero + before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts + after [x] or the first valid hexadecimal digit. Then, the function stops at + the first character that is a not a valid hexadecimal character or NULL, + whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If String is not aligned in a 16-bit boundary, then ASSERT(). + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid hexadecimal digits in the above format, then 0 is + stored at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINT64, then + MAX_UINT64 is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + hexadecimal digits right after the optional pad spaces, the value of String + is stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINT64. + +**/ +RETURN_STATUS +EFIAPI +StrHexToUint64S ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT UINT64 *Data + ); + /** Returns the length of a Null-terminated Ascii string. + This function is similar as strlen_s defined in C11. + @param String A pointer to a Null-terminated Ascii string. @param MaxSize The maximum number of Destination Ascii char, including terminating null char. @@ -358,10 +644,42 @@ AsciiStrnLenS ( IN UINTN MaxSize ); +/** + Returns the size of a Null-terminated Ascii string in bytes, including the + Null terminator. + + This function returns the size of the Null-terminated Ascii string specified + by String in bytes, including the Null terminator. + + @param String A pointer to a Null-terminated Ascii string. + @param MaxSize The maximum number of Destination Ascii + char, including the Null terminator. + + @retval 0 If String is NULL. + @retval (sizeof (CHAR8) * (MaxSize + 1)) + If there is no Null terminator in the first MaxSize characters of + String. + @return The size of the Null-terminated Ascii string in bytes, including the + Null terminator. + +**/ +UINTN +EFIAPI +AsciiStrnSizeS ( + IN CONST CHAR8 *String, + IN UINTN MaxSize + ); + /** Copies the string pointed to by Source (including the terminating null char) to the array pointed to by Destination. + This function is similar as strcpy_s defined in C11. + + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + @param Destination A pointer to a Null-terminated Ascii string. @param DestMax The maximum number of Destination Ascii char, including terminating null char. @@ -372,7 +690,7 @@ AsciiStrnLenS ( @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumAsciiStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumAsciiStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -390,6 +708,12 @@ AsciiStrCpyS ( Source to the array pointed to by Destination. If no null char is copied from Source, then Destination[Length] is always set to null. + This function is similar as strncpy_s defined in C11. + + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + @param Destination A pointer to a Null-terminated Ascii string. @param DestMax The maximum number of Destination Ascii char, including terminating null char. @@ -397,12 +721,12 @@ AsciiStrCpyS ( @param Length The maximum number of Ascii characters to copy. @retval RETURN_SUCCESS String is copied. - @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than MIN(StrLen(Source), Length). @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumAsciiStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumAsciiStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -420,20 +744,26 @@ AsciiStrnCpyS ( Appends a copy of the string pointed to by Source (including the terminating null char) to the end of the string pointed to by Destination. + This function is similar as strcat_s defined in C11. + + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + @param Destination A pointer to a Null-terminated Ascii string. @param DestMax The maximum number of Destination Ascii char, including terminating null char. @param Source A pointer to a Null-terminated Ascii string. @retval RETURN_SUCCESS String is appended. - @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than + @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than StrLen(Destination). @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT greater than StrLen(Source). @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumAsciiStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumAsciiStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -452,6 +782,12 @@ AsciiStrCatS ( copied from Source, then Destination[StrLen(Destination) + Length] is always set to null. + This function is similar as strncat_s defined in C11. + + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + @param Destination A pointer to a Null-terminated Ascii string. @param DestMax The maximum number of Destination Ascii char, including terminating null char. @@ -466,7 +802,7 @@ AsciiStrCatS ( @retval RETURN_INVALID_PARAMETER If Destination is NULL. If Source is NULL. If PcdMaximumAsciiStringLength is not zero, - and DestMax is greater than + and DestMax is greater than PcdMaximumAsciiStringLength. If DestMax is 0. @retval RETURN_ACCESS_DENIED If Source and Destination overlap. @@ -480,8 +816,240 @@ AsciiStrnCatS ( IN UINTN Length ); +/** + Convert a Null-terminated Ascii decimal string to a value of type UINTN. + + This function outputs a value of type UINTN by interpreting the contents of + the Ascii string specified by String as a decimal number. The format of the + input Ascii string String is: + + [spaces] [decimal digits]. + + The valid decimal digit character is in the range [0-9]. The function will + ignore the pad space, which includes spaces or tab characters, before + [decimal digits]. The running zero in the beginning of [decimal digits] will + be ignored. Then, the function stops at the first character that is a not a + valid decimal character or a Null-terminator, whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If PcdMaximumAsciiStringLength is not zero, and String contains more than + PcdMaximumAsciiStringLength Ascii characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid decimal digits in the above format, then 0 is stored + at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINTN, then + MAX_UINTN is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + decimal digits right after the optional pad spaces, the value of String is + stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Ascii string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumAsciiStringLength is not zero, + and String contains more than + PcdMaximumAsciiStringLength Ascii + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINTN. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrDecimalToUintnS ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT UINTN *Data + ); + +/** + Convert a Null-terminated Ascii decimal string to a value of type UINT64. + + This function outputs a value of type UINT64 by interpreting the contents of + the Ascii string specified by String as a decimal number. The format of the + input Ascii string String is: + + [spaces] [decimal digits]. + + The valid decimal digit character is in the range [0-9]. The function will + ignore the pad space, which includes spaces or tab characters, before + [decimal digits]. The running zero in the beginning of [decimal digits] will + be ignored. Then, the function stops at the first character that is a not a + valid decimal character or a Null-terminator, whichever one comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If PcdMaximumAsciiStringLength is not zero, and String contains more than + PcdMaximumAsciiStringLength Ascii characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid decimal digits in the above format, then 0 is stored + at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINT64, then + MAX_UINT64 is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + decimal digits right after the optional pad spaces, the value of String is + stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Ascii string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumAsciiStringLength is not zero, + and String contains more than + PcdMaximumAsciiStringLength Ascii + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINT64. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrDecimalToUint64S ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT UINT64 *Data + ); + +/** + Convert a Null-terminated Ascii hexadecimal string to a value of type UINTN. + + This function outputs a value of type UINTN by interpreting the contents of + the Ascii string specified by String as a hexadecimal number. The format of + the input Ascii string String is: + + [spaces][zeros][x][hexadecimal digits]. + + The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. + The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If + "x" appears in the input string, it must be prefixed with at least one 0. The + function will ignore the pad space, which includes spaces or tab characters, + before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or + [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or + the first valid hexadecimal digit. Then, the function stops at the first + character that is a not a valid hexadecimal character or Null-terminator, + whichever on comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If PcdMaximumAsciiStringLength is not zero, and String contains more than + PcdMaximumAsciiStringLength Ascii characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid hexadecimal digits in the above format, then 0 is + stored at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINTN, then + MAX_UINTN is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + hexadecimal digits right after the optional pad spaces, the value of String + is stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Ascii string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumAsciiStringLength is not zero, + and String contains more than + PcdMaximumAsciiStringLength Ascii + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINTN. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrHexToUintnS ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT UINTN *Data + ); + +/** + Convert a Null-terminated Ascii hexadecimal string to a value of type UINT64. + + This function outputs a value of type UINT64 by interpreting the contents of + the Ascii string specified by String as a hexadecimal number. The format of + the input Ascii string String is: + + [spaces][zeros][x][hexadecimal digits]. + + The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. + The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If + "x" appears in the input string, it must be prefixed with at least one 0. The + function will ignore the pad space, which includes spaces or tab characters, + before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or + [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or + the first valid hexadecimal digit. Then, the function stops at the first + character that is a not a valid hexadecimal character or Null-terminator, + whichever on comes first. + + If String is NULL, then ASSERT(). + If Data is NULL, then ASSERT(). + If PcdMaximumAsciiStringLength is not zero, and String contains more than + PcdMaximumAsciiStringLength Ascii characters, not including the + Null-terminator, then ASSERT(). + + If String has no valid hexadecimal digits in the above format, then 0 is + stored at the location pointed to by Data. + If the number represented by String exceeds the range defined by UINT64, then + MAX_UINT64 is stored at the location pointed to by Data. + + If EndPointer is not NULL, a pointer to the character that stopped the scan + is stored at the location pointed to by EndPointer. If String has no valid + hexadecimal digits right after the optional pad spaces, the value of String + is stored at the location pointed to by EndPointer. + + @param String Pointer to a Null-terminated Ascii string. + @param EndPointer Pointer to character that stops scan. + @param Data Pointer to the converted value. + + @retval RETURN_SUCCESS Value is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If PcdMaximumAsciiStringLength is not zero, + and String contains more than + PcdMaximumAsciiStringLength Ascii + characters, not including the + Null-terminator. + @retval RETURN_UNSUPPORTED If the number represented by String exceeds + the range defined by UINT64. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrHexToUint64S ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT UINT64 *Data + ); + + +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES /** + [ATTENTION] This function is deprecated for security reason. + Copies one Null-terminated Unicode string to another Null-terminated Unicode string and returns the new Unicode string. @@ -513,7 +1081,9 @@ StrCpy ( /** - Copies up to a specified length from one Null-terminated Unicode string to + [ATTENTION] This function is deprecated for security reason. + + Copies up to a specified length from one Null-terminated Unicode string to another Null-terminated Unicode string and returns the new Unicode string. This function copies the contents of the Unicode string Source to the Unicode @@ -529,7 +1099,7 @@ StrCpy ( If Length > 0 and Source is NULL, then ASSERT(). If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT(). If Source and Destination overlap, then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and Length is greater than + If PcdMaximumUnicodeStringLength is not zero, and Length is greater than PcdMaximumUnicodeStringLength, then ASSERT(). If PcdMaximumUnicodeStringLength is not zero, and Source contains more than PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator, @@ -549,7 +1119,7 @@ StrnCpy ( IN CONST CHAR16 *Source, IN UINTN Length ); - +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) /** Returns the length of a Null-terminated Unicode string. @@ -579,7 +1149,7 @@ StrLen ( Returns the size of a Null-terminated Unicode string in bytes, including the Null terminator. - This function returns the size, in bytes, of the Null-terminated Unicode string + This function returns the size, in bytes, of the Null-terminated Unicode string specified by String. If String is NULL, then ASSERT(). @@ -639,7 +1209,7 @@ StrCmp ( /** Compares up to a specified length the contents of two Null-terminated Unicode strings, and returns the difference between the first mismatched Unicode characters. - + This function compares the Null-terminated Unicode string FirstString to the Null-terminated Unicode string SecondString. At most, Length Unicode characters will be compared. If Length is 0, then 0 is returned. If @@ -677,7 +1247,11 @@ StrnCmp ( ); +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES + /** + [ATTENTION] This function is deprecated for security reason. + Concatenates one Null-terminated Unicode string to another Null-terminated Unicode string, and returns the concatenated Unicode string. @@ -718,8 +1292,10 @@ StrCat ( /** - Concatenates up to a specified length one Null-terminated Unicode to the end - of another Null-terminated Unicode string, and returns the concatenated + [ATTENTION] This function is deprecated for security reason. + + Concatenates up to a specified length one Null-terminated Unicode to the end + of another Null-terminated Unicode string, and returns the concatenated Unicode string. This function concatenates two Null-terminated Unicode strings. The contents @@ -735,7 +1311,7 @@ StrCat ( If Length > 0 and Source is NULL, then ASSERT(). If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT(). If Source and Destination overlap, then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and Length is greater than + If PcdMaximumUnicodeStringLength is not zero, and Length is greater than PcdMaximumUnicodeStringLength, then ASSERT(). If PcdMaximumUnicodeStringLength is not zero, and Destination contains more than PcdMaximumUnicodeStringLength Unicode characters, not including the @@ -762,6 +1338,7 @@ StrnCat ( IN CONST CHAR16 *Source, IN UINTN Length ); +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) /** Returns the first occurrence of a Null-terminated Unicode sub-string @@ -818,7 +1395,7 @@ StrStr ( If String has no pad spaces or valid decimal digits, then 0 is returned. If the number represented by String overflows according - to the range defined by UINTN, then ASSERT(). + to the range defined by UINTN, then MAX_UINTN is returned. If PcdMaximumUnicodeStringLength is not zero, and String contains more than PcdMaximumUnicodeStringLength Unicode characters not including @@ -858,7 +1435,7 @@ StrDecimalToUintn ( If String has no pad spaces or valid decimal digits, then 0 is returned. If the number represented by String overflows according - to the range defined by UINT64, then ASSERT(). + to the range defined by UINT64, then MAX_UINT64 is returned. If PcdMaximumUnicodeStringLength is not zero, and String contains more than PcdMaximumUnicodeStringLength Unicode characters not including @@ -874,7 +1451,7 @@ EFIAPI StrDecimalToUint64 ( IN CONST CHAR16 *String ); - + /** Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN. @@ -891,7 +1468,7 @@ StrDecimalToUint64 ( The function will ignore the pad space, which includes spaces or tab characters, before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the - first valid hexadecimal digit. Then, the function stops at the first character + first valid hexadecimal digit. Then, the function stops at the first character that is a not a valid hexadecimal character or NULL, whichever one comes first. If String is NULL, then ASSERT(). @@ -900,7 +1477,7 @@ StrDecimalToUint64 ( If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then zero is returned. If the number represented by String overflows according to the range defined by - UINTN, then ASSERT(). + UINTN, then MAX_UINTN is returned. If PcdMaximumUnicodeStringLength is not zero, and String contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, @@ -942,7 +1519,7 @@ StrHexToUintn ( If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then zero is returned. If the number represented by String overflows according to the range defined by - UINT64, then ASSERT(). + UINT64, then MAX_UINT64 is returned. If PcdMaximumUnicodeStringLength is not zero, and String contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, @@ -960,6 +1537,241 @@ StrHexToUint64 ( ); /** + Convert a Null-terminated Unicode string to IPv6 address and prefix length. + + This function outputs a value of type IPv6_ADDRESS and may output a value + of type UINT8 by interpreting the contents of the Unicode string specified + by String. The format of the input Unicode string String is as follows: + + X:X:X:X:X:X:X:X[/P] + + X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and + [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low + memory address and high byte is stored in high memory address. P contains decimal + digit characters in the range [0-9]. The running zero in the beginning of P will + be ignored. /P is optional. + + When /P is not in the String, the function stops at the first character that is + not a valid hexadecimal digit character after eight X's are converted. + + When /P is in the String, the function stops at the first character that is not + a valid decimal digit character after P is converted. + + "::" can be used to compress one or more groups of X when X contains only 0. + The "::" can only appear once in the String. + + If String is NULL, then ASSERT(). + + If Address is NULL, then ASSERT(). + + If String is not aligned in a 16-bit boundary, then ASSERT(). + + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If EndPointer is not NULL and Address is translated from String, a pointer + to the character that stopped the scan is stored at the location pointed to + by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Address Pointer to the converted IPv6 address. + @param PrefixLength Pointer to the converted IPv6 address prefix + length. MAX_UINT8 is returned when /P is + not in the String. + + @retval RETURN_SUCCESS Address is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If X contains more than four hexadecimal + digit characters. + If String contains "::" and number of X + is not less than 8. + If P starts with character that is not a + valid decimal digit character. + If the decimal number converted from P + exceeds 128. + +**/ +RETURN_STATUS +EFIAPI +StrToIpv6Address ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT IPv6_ADDRESS *Address, + OUT UINT8 *PrefixLength OPTIONAL + ); + +/** + Convert a Null-terminated Unicode string to IPv4 address and prefix length. + + This function outputs a value of type IPv4_ADDRESS and may output a value + of type UINT8 by interpreting the contents of the Unicode string specified + by String. The format of the input Unicode string String is as follows: + + D.D.D.D[/P] + + D and P are decimal digit characters in the range [0-9]. The running zero in + the beginning of D and P will be ignored. /P is optional. + + When /P is not in the String, the function stops at the first character that is + not a valid decimal digit character after four D's are converted. + + When /P is in the String, the function stops at the first character that is not + a valid decimal digit character after P is converted. + + If String is NULL, then ASSERT(). + + If Address is NULL, then ASSERT(). + + If String is not aligned in a 16-bit boundary, then ASSERT(). + + If PcdMaximumUnicodeStringLength is not zero, and String contains more than + PcdMaximumUnicodeStringLength Unicode characters, not including the + Null-terminator, then ASSERT(). + + If EndPointer is not NULL and Address is translated from String, a pointer + to the character that stopped the scan is stored at the location pointed to + by EndPointer. + + @param String Pointer to a Null-terminated Unicode string. + @param EndPointer Pointer to character that stops scan. + @param Address Pointer to the converted IPv4 address. + @param PrefixLength Pointer to the converted IPv4 address prefix + length. MAX_UINT8 is returned when /P is + not in the String. + + @retval RETURN_SUCCESS Address is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If String is not in the correct format. + If any decimal number converted from D + exceeds 255. + If the decimal number converted from P + exceeds 32. + +**/ +RETURN_STATUS +EFIAPI +StrToIpv4Address ( + IN CONST CHAR16 *String, + OUT CHAR16 **EndPointer, OPTIONAL + OUT IPv4_ADDRESS *Address, + OUT UINT8 *PrefixLength OPTIONAL + ); + +#define GUID_STRING_LENGTH 36 + +/** + Convert a Null-terminated Unicode GUID string to a value of type + EFI_GUID. + + This function outputs a GUID value by interpreting the contents of + the Unicode string specified by String. The format of the input + Unicode string String consists of 36 characters, as follows: + + aabbccdd-eeff-gghh-iijj-kkllmmnnoopp + + The pairs aa - pp are two characters in the range [0-9], [a-f] and + [A-F], with each pair representing a single byte hexadecimal value. + + The mapping between String and the EFI_GUID structure is as follows: + aa Data1[24:31] + bb Data1[16:23] + cc Data1[8:15] + dd Data1[0:7] + ee Data2[8:15] + ff Data2[0:7] + gg Data3[8:15] + hh Data3[0:7] + ii Data4[0:7] + jj Data4[8:15] + kk Data4[16:23] + ll Data4[24:31] + mm Data4[32:39] + nn Data4[40:47] + oo Data4[48:55] + pp Data4[56:63] + + If String is NULL, then ASSERT(). + If Guid is NULL, then ASSERT(). + If String is not aligned in a 16-bit boundary, then ASSERT(). + + @param String Pointer to a Null-terminated Unicode string. + @param Guid Pointer to the converted GUID. + + @retval RETURN_SUCCESS Guid is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If String is not as the above format. + +**/ +RETURN_STATUS +EFIAPI +StrToGuid ( + IN CONST CHAR16 *String, + OUT GUID *Guid + ); + +/** + Convert a Null-terminated Unicode hexadecimal string to a byte array. + + This function outputs a byte array by interpreting the contents of + the Unicode string specified by String in hexadecimal format. The format of + the input Unicode string String is: + + [XX]* + + X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F]. + The function decodes every two hexadecimal digit characters as one byte. The + decoding stops after Length of characters and outputs Buffer containing + (Length / 2) bytes. + + If String is not aligned in a 16-bit boundary, then ASSERT(). + + If String is NULL, then ASSERT(). + + If Buffer is NULL, then ASSERT(). + + If Length is not multiple of 2, then ASSERT(). + + If PcdMaximumUnicodeStringLength is not zero and Length is greater than + PcdMaximumUnicodeStringLength, then ASSERT(). + + If MaxBufferSize is less than (Length / 2), then ASSERT(). + + @param String Pointer to a Null-terminated Unicode string. + @param Length The number of Unicode characters to decode. + @param Buffer Pointer to the converted bytes array. + @param MaxBufferSize The maximum size of Buffer. + + @retval RETURN_SUCCESS Buffer is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If Length is not multiple of 2. + If PcdMaximumUnicodeStringLength is not zero, + and Length is greater than + PcdMaximumUnicodeStringLength. + @retval RETURN_UNSUPPORTED If Length of characters from String contain + a character that is not valid hexadecimal + digit characters, or a Null-terminator. + @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2). +**/ +RETURN_STATUS +EFIAPI +StrHexToBytes ( + IN CONST CHAR16 *String, + IN UINTN Length, + OUT UINT8 *Buffer, + IN UINTN MaxBufferSize + ); + +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES + +/** + [ATTENTION] This function is deprecated for security reason. + Convert a Null-terminated Unicode string to a Null-terminated ASCII string and returns the ASCII string. @@ -999,8 +1811,116 @@ UnicodeStrToAsciiStr ( OUT CHAR8 *Destination ); +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) + +/** + Convert a Null-terminated Unicode string to a Null-terminated + ASCII string. + + This function is similar to AsciiStrCpyS. + + This function converts the content of the Unicode string Source + to the ASCII string Destination by copying the lower 8 bits of + each Unicode character. The function terminates the ASCII string + Destination by appending a Null-terminator character at the end. + + The caller is responsible to make sure Destination points to a buffer with size + equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes. + + If any Unicode characters in Source contain non-zero value in + the upper 8 bits, then ASSERT(). + + If Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + + @param Source The pointer to a Null-terminated Unicode string. + @param Destination The pointer to a Null-terminated ASCII string. + @param DestMax The maximum number of Destination Ascii + char, including terminating null char. + + @retval RETURN_SUCCESS String is converted. + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source). + @retval RETURN_INVALID_PARAMETER If Destination is NULL. + If Source is NULL. + If PcdMaximumAsciiStringLength is not zero, + and DestMax is greater than + PcdMaximumAsciiStringLength. + If PcdMaximumUnicodeStringLength is not zero, + and DestMax is greater than + PcdMaximumUnicodeStringLength. + If DestMax is 0. + @retval RETURN_ACCESS_DENIED If Source and Destination overlap. + +**/ +RETURN_STATUS +EFIAPI +UnicodeStrToAsciiStrS ( + IN CONST CHAR16 *Source, + OUT CHAR8 *Destination, + IN UINTN DestMax + ); + +/** + Convert not more than Length successive characters from a Null-terminated + Unicode string to a Null-terminated Ascii string. If no null char is copied + from Source, then Destination[Length] is always set to null. + + This function converts not more than Length successive characters from the + Unicode string Source to the Ascii string Destination by copying the lower 8 + bits of each Unicode character. The function terminates the Ascii string + Destination by appending a Null-terminator character at the end. + + The caller is responsible to make sure Destination points to a buffer with size + equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes. + + If any Unicode characters in Source contain non-zero value in the upper 8 + bits, then ASSERT(). + If Source is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. + + @param Source The pointer to a Null-terminated Unicode string. + @param Length The maximum number of Unicode characters to + convert. + @param Destination The pointer to a Null-terminated Ascii string. + @param DestMax The maximum number of Destination Ascii + char, including terminating null char. + @param DestinationLength The number of Unicode characters converted. + + @retval RETURN_SUCCESS String is converted. + @retval RETURN_INVALID_PARAMETER If Destination is NULL. + If Source is NULL. + If DestinationLength is NULL. + If PcdMaximumAsciiStringLength is not zero, + and Length or DestMax is greater than + PcdMaximumAsciiStringLength. + If PcdMaximumUnicodeStringLength is not + zero, and Length or DestMax is greater than + PcdMaximumUnicodeStringLength. + If DestMax is 0. + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than + MIN(StrLen(Source), Length). + @retval RETURN_ACCESS_DENIED If Source and Destination overlap. + +**/ +RETURN_STATUS +EFIAPI +UnicodeStrnToAsciiStrS ( + IN CONST CHAR16 *Source, + IN UINTN Length, + OUT CHAR8 *Destination, + IN UINTN DestMax, + OUT UINTN *DestinationLength + ); + +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES /** + [ATTENTION] This function is deprecated for security reason. + Copies one Null-terminated ASCII string to another Null-terminated ASCII string and returns the new ASCII string. @@ -1030,7 +1950,9 @@ AsciiStrCpy ( /** - Copies up to a specified length one Null-terminated ASCII string to another + [ATTENTION] This function is deprecated for security reason. + + Copies up to a specified length one Null-terminated ASCII string to another Null-terminated ASCII string and returns the new ASCII string. This function copies the contents of the ASCII string Source to the ASCII @@ -1043,7 +1965,7 @@ AsciiStrCpy ( If Destination is NULL, then ASSERT(). If Source is NULL, then ASSERT(). If Source and Destination overlap, then ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and Length is greater than + If PcdMaximumAsciiStringLength is not zero, and Length is greater than PcdMaximumAsciiStringLength, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and Source contains more than PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator, @@ -1063,7 +1985,7 @@ AsciiStrnCpy ( IN CONST CHAR8 *Source, IN UINTN Length ); - +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) /** Returns the length of a Null-terminated ASCII string. @@ -1197,7 +2119,7 @@ AsciiStriCmp ( If Length > 0 and FirstString is NULL, then ASSERT(). If Length > 0 and SecondString is NULL, then ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and Length is greater than + If PcdMaximumAsciiStringLength is not zero, and Length is greater than PcdMaximumAsciiStringLength, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator, @@ -1209,7 +2131,7 @@ AsciiStriCmp ( @param FirstString The pointer to a Null-terminated ASCII string. @param SecondString The pointer to a Null-terminated ASCII string. @param Length The maximum number of ASCII characters for compare. - + @retval ==0 FirstString is identical to SecondString. @retval !=0 FirstString is not identical to SecondString. @@ -1223,7 +2145,11 @@ AsciiStrnCmp ( ); +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES + /** + [ATTENTION] This function is deprecated for security reason. + Concatenates one Null-terminated ASCII string to another Null-terminated ASCII string, and returns the concatenated ASCII string. @@ -1259,8 +2185,10 @@ AsciiStrCat ( /** - Concatenates up to a specified length one Null-terminated ASCII string to - the end of another Null-terminated ASCII string, and returns the + [ATTENTION] This function is deprecated for security reason. + + Concatenates up to a specified length one Null-terminated ASCII string to + the end of another Null-terminated ASCII string, and returns the concatenated ASCII string. This function concatenates two Null-terminated ASCII strings. The contents @@ -1301,7 +2229,7 @@ AsciiStrnCat ( IN CONST CHAR8 *Source, IN UINTN Length ); - +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) /** Returns the first occurrence of a Null-terminated ASCII sub-string @@ -1354,7 +2282,7 @@ AsciiStrStr ( If String has only pad spaces, then 0 is returned. If String has no pad spaces or valid decimal digits, then 0 is returned. If the number represented by String overflows according to the range defined by - UINTN, then ASSERT(). + UINTN, then MAX_UINTN is returned. If String is NULL, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and String contains more than PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, @@ -1391,7 +2319,7 @@ AsciiStrDecimalToUintn ( If String has only pad spaces, then 0 is returned. If String has no pad spaces or valid decimal digits, then 0 is returned. If the number represented by String overflows according to the range defined by - UINT64, then ASSERT(). + UINT64, then MAX_UINT64 is returned. If String is NULL, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and String contains more than PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, @@ -1432,7 +2360,7 @@ AsciiStrDecimalToUint64 ( 0 is returned. If the number represented by String overflows according to the range defined by UINTN, - then ASSERT(). + then MAX_UINTN is returned. If String is NULL, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and String contains more than PcdMaximumAsciiStringLength ASCII characters not including @@ -1473,7 +2401,7 @@ AsciiStrHexToUintn ( 0 is returned. If the number represented by String overflows according to the range defined by UINT64, - then ASSERT(). + then MAX_UINT64 is returned. If String is NULL, then ASSERT(). If PcdMaximumAsciiStringLength is not zero, and String contains more than PcdMaximumAsciiStringLength ASCII characters not including @@ -1490,41 +2418,357 @@ AsciiStrHexToUint64 ( IN CONST CHAR8 *String ); +/** + Convert a Null-terminated ASCII string to IPv6 address and prefix length. + + This function outputs a value of type IPv6_ADDRESS and may output a value + of type UINT8 by interpreting the contents of the ASCII string specified + by String. The format of the input ASCII string String is as follows: + + X:X:X:X:X:X:X:X[/P] + + X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and + [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low + memory address and high byte is stored in high memory address. P contains decimal + digit characters in the range [0-9]. The running zero in the beginning of P will + be ignored. /P is optional. + + When /P is not in the String, the function stops at the first character that is + not a valid hexadecimal digit character after eight X's are converted. + + When /P is in the String, the function stops at the first character that is not + a valid decimal digit character after P is converted. + + "::" can be used to compress one or more groups of X when X contains only 0. + The "::" can only appear once in the String. + + If String is NULL, then ASSERT(). + + If Address is NULL, then ASSERT(). + + If EndPointer is not NULL and Address is translated from String, a pointer + to the character that stopped the scan is stored at the location pointed to + by EndPointer. + + @param String Pointer to a Null-terminated ASCII string. + @param EndPointer Pointer to character that stops scan. + @param Address Pointer to the converted IPv6 address. + @param PrefixLength Pointer to the converted IPv6 address prefix + length. MAX_UINT8 is returned when /P is + not in the String. + + @retval RETURN_SUCCESS Address is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If X contains more than four hexadecimal + digit characters. + If String contains "::" and number of X + is not less than 8. + If P starts with character that is not a + valid decimal digit character. + If the decimal number converted from P + exceeds 128. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrToIpv6Address ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT IPv6_ADDRESS *Address, + OUT UINT8 *PrefixLength OPTIONAL + ); + +/** + Convert a Null-terminated ASCII string to IPv4 address and prefix length. + + This function outputs a value of type IPv4_ADDRESS and may output a value + of type UINT8 by interpreting the contents of the ASCII string specified + by String. The format of the input ASCII string String is as follows: + + D.D.D.D[/P] + + D and P are decimal digit characters in the range [0-9]. The running zero in + the beginning of D and P will be ignored. /P is optional. + + When /P is not in the String, the function stops at the first character that is + not a valid decimal digit character after four D's are converted. + + When /P is in the String, the function stops at the first character that is not + a valid decimal digit character after P is converted. + + If String is NULL, then ASSERT(). + + If Address is NULL, then ASSERT(). + + If EndPointer is not NULL and Address is translated from String, a pointer + to the character that stopped the scan is stored at the location pointed to + by EndPointer. + + @param String Pointer to a Null-terminated ASCII string. + @param EndPointer Pointer to character that stops scan. + @param Address Pointer to the converted IPv4 address. + @param PrefixLength Pointer to the converted IPv4 address prefix + length. MAX_UINT8 is returned when /P is + not in the String. + + @retval RETURN_SUCCESS Address is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If String is not in the correct format. + If any decimal number converted from D + exceeds 255. + If the decimal number converted from P + exceeds 32. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrToIpv4Address ( + IN CONST CHAR8 *String, + OUT CHAR8 **EndPointer, OPTIONAL + OUT IPv4_ADDRESS *Address, + OUT UINT8 *PrefixLength OPTIONAL + ); + +/** + Convert a Null-terminated ASCII GUID string to a value of type + EFI_GUID. + + This function outputs a GUID value by interpreting the contents of + the ASCII string specified by String. The format of the input + ASCII string String consists of 36 characters, as follows: + + aabbccdd-eeff-gghh-iijj-kkllmmnnoopp + + The pairs aa - pp are two characters in the range [0-9], [a-f] and + [A-F], with each pair representing a single byte hexadecimal value. + + The mapping between String and the EFI_GUID structure is as follows: + aa Data1[24:31] + bb Data1[16:23] + cc Data1[8:15] + dd Data1[0:7] + ee Data2[8:15] + ff Data2[0:7] + gg Data3[8:15] + hh Data3[0:7] + ii Data4[0:7] + jj Data4[8:15] + kk Data4[16:23] + ll Data4[24:31] + mm Data4[32:39] + nn Data4[40:47] + oo Data4[48:55] + pp Data4[56:63] + + If String is NULL, then ASSERT(). + If Guid is NULL, then ASSERT(). + + @param String Pointer to a Null-terminated ASCII string. + @param Guid Pointer to the converted GUID. + + @retval RETURN_SUCCESS Guid is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + @retval RETURN_UNSUPPORTED If String is not as the above format. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrToGuid ( + IN CONST CHAR8 *String, + OUT GUID *Guid + ); + +/** + Convert a Null-terminated ASCII hexadecimal string to a byte array. + + This function outputs a byte array by interpreting the contents of + the ASCII string specified by String in hexadecimal format. The format of + the input ASCII string String is: + + [XX]* + + X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F]. + The function decodes every two hexadecimal digit characters as one byte. The + decoding stops after Length of characters and outputs Buffer containing + (Length / 2) bytes. + + If String is NULL, then ASSERT(). + + If Buffer is NULL, then ASSERT(). + + If Length is not multiple of 2, then ASSERT(). + + If PcdMaximumAsciiStringLength is not zero and Length is greater than + PcdMaximumAsciiStringLength, then ASSERT(). + + If MaxBufferSize is less than (Length / 2), then ASSERT(). + + @param String Pointer to a Null-terminated ASCII string. + @param Length The number of ASCII characters to decode. + @param Buffer Pointer to the converted bytes array. + @param MaxBufferSize The maximum size of Buffer. + + @retval RETURN_SUCCESS Buffer is translated from String. + @retval RETURN_INVALID_PARAMETER If String is NULL. + If Data is NULL. + If Length is not multiple of 2. + If PcdMaximumAsciiStringLength is not zero, + and Length is greater than + PcdMaximumAsciiStringLength. + @retval RETURN_UNSUPPORTED If Length of characters from String contain + a character that is not valid hexadecimal + digit characters, or a Null-terminator. + @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2). +**/ +RETURN_STATUS +EFIAPI +AsciiStrHexToBytes ( + IN CONST CHAR8 *String, + IN UINTN Length, + OUT UINT8 *Buffer, + IN UINTN MaxBufferSize + ); + +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES /** + [ATTENTION] This function is deprecated for security reason. + Convert one Null-terminated ASCII string to a Null-terminated Unicode string and returns the Unicode string. This function converts the contents of the ASCII string Source to the Unicode - string Destination, and returns Destination. The function terminates the - Unicode string Destination by appending a Null-terminator character at the end. + string Destination, and returns Destination. The function terminates the + Unicode string Destination by appending a Null-terminator character at the end. + The caller is responsible to make sure Destination points to a buffer with size + equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes. + + If Destination is NULL, then ASSERT(). + If Destination is not aligned on a 16-bit boundary, then ASSERT(). + If Source is NULL, then ASSERT(). + If Source and Destination overlap, then ASSERT(). + If PcdMaximumAsciiStringLength is not zero, and Source contains more than + PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, + then ASSERT(). + If PcdMaximumUnicodeStringLength is not zero, and Source contains more than + PcdMaximumUnicodeStringLength ASCII characters not including the + Null-terminator, then ASSERT(). + + @param Source The pointer to a Null-terminated ASCII string. + @param Destination The pointer to a Null-terminated Unicode string. + + @return Destination. + +**/ +CHAR16 * +EFIAPI +AsciiStrToUnicodeStr ( + IN CONST CHAR8 *Source, + OUT CHAR16 *Destination + ); + +#endif // !defined (DISABLE_NEW_DEPRECATED_INTERFACES) + +/** + Convert one Null-terminated ASCII string to a Null-terminated + Unicode string. + + This function is similar to StrCpyS. + + This function converts the contents of the ASCII string Source to the Unicode + string Destination. The function terminates the Unicode string Destination by + appending a Null-terminator character at the end. + The caller is responsible to make sure Destination points to a buffer with size equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes. - If Destination is NULL, then ASSERT(). If Destination is not aligned on a 16-bit boundary, then ASSERT(). - If Source is NULL, then ASSERT(). - If Source and Destination overlap, then ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and Source contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, - then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and Source contains more than - PcdMaximumUnicodeStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then the Destination is unmodified. @param Source The pointer to a Null-terminated ASCII string. @param Destination The pointer to a Null-terminated Unicode string. + @param DestMax The maximum number of Destination Unicode + char, including terminating null char. - @return Destination. + @retval RETURN_SUCCESS String is converted. + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source). + @retval RETURN_INVALID_PARAMETER If Destination is NULL. + If Source is NULL. + If PcdMaximumUnicodeStringLength is not zero, + and DestMax is greater than + PcdMaximumUnicodeStringLength. + If PcdMaximumAsciiStringLength is not zero, + and DestMax is greater than + PcdMaximumAsciiStringLength. + If DestMax is 0. + @retval RETURN_ACCESS_DENIED If Source and Destination overlap. **/ -CHAR16 * +RETURN_STATUS EFIAPI -AsciiStrToUnicodeStr ( +AsciiStrToUnicodeStrS ( IN CONST CHAR8 *Source, - OUT CHAR16 *Destination + OUT CHAR16 *Destination, + IN UINTN DestMax ); +/** + Convert not more than Length successive characters from a Null-terminated + Ascii string to a Null-terminated Unicode string. If no null char is copied + from Source, then Destination[Length] is always set to null. + + This function converts not more than Length successive characters from the + Ascii string Source to the Unicode string Destination. The function + terminates the Unicode string Destination by appending a Null-terminator + character at the end. + + The caller is responsible to make sure Destination points to a buffer with + size not smaller than + ((MIN(AsciiStrLen(Source), Length) + 1) * sizeof (CHAR8)) in bytes. + + If Destination is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + If an error is returned, then Destination and DestinationLength are + unmodified. + + @param Source The pointer to a Null-terminated Ascii string. + @param Length The maximum number of Ascii characters to convert. + @param Destination The pointer to a Null-terminated Unicode string. + @param DestMax The maximum number of Destination Unicode char, + including terminating null char. + @param DestinationLength The number of Ascii characters converted. + + @retval RETURN_SUCCESS String is converted. + @retval RETURN_INVALID_PARAMETER If Destination is NULL. + If Source is NULL. + If DestinationLength is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and Length or DestMax is greater than + PcdMaximumUnicodeStringLength. + If PcdMaximumAsciiStringLength is not zero, + and Length or DestMax is greater than + PcdMaximumAsciiStringLength. + If DestMax is 0. + @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than + MIN(AsciiStrLen(Source), Length). + @retval RETURN_ACCESS_DENIED If Source and Destination overlap. + +**/ +RETURN_STATUS +EFIAPI +AsciiStrnToUnicodeStrS ( + IN CONST CHAR8 *Source, + IN UINTN Length, + OUT CHAR16 *Destination, + IN UINTN DestMax, + OUT UINTN *DestinationLength + ); /** Converts an 8-bit value to an 8-bit BCD value. @@ -1566,6 +2810,42 @@ BcdToDecimal8 ( IN UINT8 Value ); +// +// File Path Manipulation Functions +// + +/** + Removes the last directory or file entry in a path. + + @param[in, out] Path The pointer to the path to modify. + + @retval FALSE Nothing was found to remove. + @retval TRUE A directory or file was removed. +**/ +BOOLEAN +EFIAPI +PathRemoveLastItem( + IN OUT CHAR16 *Path + ); + +/** + Function to clean up paths. + - Single periods in the path are removed. + - Double periods in the path are removed along with a single parent directory. + - Forward slashes L'/' are converted to backward slashes L'\'. + + This will be done inline and the existing buffer may be larger than required + upon completion. + + @param[in] Path The pointer to the string containing the path. + + @return Returns Path, otherwise returns NULL to indicate that an error has occurred. +**/ +CHAR16* +EFIAPI +PathCleanUpDirectories( + IN CHAR16 *Path + ); // // Linked List Functions and Macros @@ -1588,6 +2868,33 @@ BcdToDecimal8 ( #define INITIALIZE_LIST_HEAD_VARIABLE(ListHead) {&(ListHead), &(ListHead)} +/** + Checks whether FirstEntry and SecondEntry are part of the same doubly-linked + list. + + If FirstEntry is NULL, then ASSERT(). + If FirstEntry->ForwardLink is NULL, then ASSERT(). + If FirstEntry->BackLink is NULL, then ASSERT(). + If SecondEntry is NULL, then ASSERT(); + If PcdMaximumLinkedListLength is not zero, and List contains more than + PcdMaximumLinkedListLength nodes, then ASSERT(). + + @param FirstEntry A pointer to a node in a linked list. + @param SecondEntry A pointer to the node to locate. + + @retval TRUE SecondEntry is in the same doubly-linked list as FirstEntry. + @retval FALSE SecondEntry isn't in the same doubly-linked list as FirstEntry, + or FirstEntry is invalid. + +**/ +BOOLEAN +EFIAPI +IsNodeInList ( + IN CONST LIST_ENTRY *FirstEntry, + IN CONST LIST_ENTRY *SecondEntry + ); + + /** Initializes the head node of a doubly linked list, and returns the pointer to the head node of the doubly linked list. @@ -1650,7 +2957,7 @@ InsertHeadList ( If ListHead is NULL, then ASSERT(). If Entry is NULL, then ASSERT(). - If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and prior to insertion the number of nodes in ListHead, including the ListHead node, is greater than or @@ -1674,12 +2981,12 @@ InsertTailList ( /** Retrieves the first node of a doubly linked list. - Returns the first node of a doubly linked list. List must have been + Returns the first node of a doubly linked list. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. If List is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to @@ -1701,13 +3008,13 @@ GetFirstNode ( /** Retrieves the next node of a doubly linked list. - Returns the node of a doubly linked list that follows Node. + Returns the node of a doubly linked list that follows Node. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). @@ -1726,27 +3033,27 @@ GetNextNode ( IN CONST LIST_ENTRY *Node ); - + /** Retrieves the previous node of a doubly linked list. - - Returns the node of a doubly linked list that precedes Node. + + Returns the node of a doubly linked list that precedes Node. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. - + If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT(). - + @param List A pointer to the head node of a doubly linked list. @param Node A pointer to a node in the doubly linked list. - + @return The pointer to the previous node if one exists. Otherwise List is returned. - + **/ LIST_ENTRY * EFIAPI @@ -1755,7 +3062,7 @@ GetPreviousNode ( IN CONST LIST_ENTRY *Node ); - + /** Checks to see if a doubly linked list is empty or not. @@ -1763,7 +3070,7 @@ GetPreviousNode ( zero nodes, this function returns TRUE. Otherwise, it returns FALSE. If ListHead is NULL, then ASSERT(). - If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to @@ -1793,12 +3100,12 @@ IsListEmpty ( If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to PcdMaximumLinkedListLength, then ASSERT(). - If PcdVerifyNodeInList is TRUE and Node is not a node in List the and Node is not equal + If PcdVerifyNodeInList is TRUE and Node is not a node in List the and Node is not equal to List, then ASSERT(). @param List A pointer to the head node of a doubly linked list. @@ -1855,12 +3162,12 @@ IsNodeAtEnd ( Otherwise, the location of the FirstEntry node is swapped with the location of the SecondEntry node in a doubly linked list. SecondEntry must be in the same double linked list as FirstEntry and that double linked list must have - been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). + been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). SecondEntry is returned after the nodes are swapped. If FirstEntry is NULL, then ASSERT(). If SecondEntry is NULL, then ASSERT(). - If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the + If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the same linked list, then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in the linked list containing the FirstEntry and SecondEntry nodes, including @@ -1869,7 +3176,7 @@ IsNodeAtEnd ( @param FirstEntry A pointer to a node in a linked list. @param SecondEntry A pointer to another node in the same linked list. - + @return SecondEntry. **/ @@ -2437,7 +3744,7 @@ DivU64x64Remainder ( function returns the 64-bit signed quotient. It is the caller's responsibility to not call this function with a Divisor of 0. - If Divisor is 0, then the quotient and remainder should be assumed to be + If Divisor is 0, then the quotient and remainder should be assumed to be the largest negative integer. If Divisor is 0, then ASSERT(). @@ -2769,7 +4076,7 @@ BitFieldAnd8 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise + in Operand and the value specified by AndData, followed by a bitwise OR with value specified by OrData. All other bits in Operand are preserved. The new 8-bit value is returned. @@ -2936,7 +4243,7 @@ BitFieldAnd16 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise + in Operand and the value specified by AndData, followed by a bitwise OR with value specified by OrData. All other bits in Operand are preserved. The new 16-bit value is returned. @@ -3103,7 +4410,7 @@ BitFieldAnd32 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise + in Operand and the value specified by AndData, followed by a bitwise OR with value specified by OrData. All other bits in Operand are preserved. The new 32-bit value is returned. @@ -3270,7 +4577,7 @@ BitFieldAnd64 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise + in Operand and the value specified by AndData, followed by a bitwise OR with value specified by OrData. All other bits in Operand are preserved. The new 64-bit value is returned. @@ -3302,6 +4609,62 @@ BitFieldAndThenOr64 ( IN UINT64 OrData ); +/** + Reads a bit field from a 32-bit value, counts and returns + the number of set bits. + + Counts the number of set bits in the bit field specified by + StartBit and EndBit in Operand. The count is returned. + + If StartBit is greater than 31, then ASSERT(). + If EndBit is greater than 31, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). + + @param Operand Operand on which to perform the bitfield operation. + @param StartBit The ordinal of the least significant bit in the bit field. + Range 0..31. + @param EndBit The ordinal of the most significant bit in the bit field. + Range 0..31. + + @return The number of bits set between StartBit and EndBit. + +**/ +UINT8 +EFIAPI +BitFieldCountOnes32 ( + IN UINT32 Operand, + IN UINTN StartBit, + IN UINTN EndBit + ); + +/** + Reads a bit field from a 64-bit value, counts and returns + the number of set bits. + + Counts the number of set bits in the bit field specified by + StartBit and EndBit in Operand. The count is returned. + + If StartBit is greater than 63, then ASSERT(). + If EndBit is greater than 63, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). + + @param Operand Operand on which to perform the bitfield operation. + @param StartBit The ordinal of the least significant bit in the bit field. + Range 0..63. + @param EndBit The ordinal of the most significant bit in the bit field. + Range 0..63. + + @return The number of bits set between StartBit and EndBit. + +**/ +UINT8 +EFIAPI +BitFieldCountOnes64 ( + IN UINT64 Operand, + IN UINTN StartBit, + IN UINTN EndBit + ); + // // Base Library Checksum Functions // @@ -3522,6 +4885,25 @@ CalculateCheckSum64 ( IN UINTN Length ); +/** + Computes and returns a 32-bit CRC for a data buffer. + CRC32 value bases on ITU-T V.42. + + If Buffer is NULL, then ASSERT(). + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). + + @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed. + @param[in] Length The number of bytes in the buffer Data. + + @retval Crc32 The 32-bit CRC was computed for the data buffer. + +**/ +UINT32 +EFIAPI +CalculateCrc32( + IN VOID *Buffer, + IN UINTN Length + ); // // Base Library CPU Functions @@ -3566,17 +4948,18 @@ MemoryFence ( If JumpBuffer is NULL, then ASSERT(). For Itanium processors, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT(). - + NOTE: The structure BASE_LIBRARY_JUMP_BUFFER is CPU architecture specific. The same structure must never be used for more than one CPU architecture context. - For example, a BASE_LIBRARY_JUMP_BUFFER allocated by an IA-32 module must never be used from an x64 module. - SetJump()/LongJump() is not currently supported for the EBC processor type. + For example, a BASE_LIBRARY_JUMP_BUFFER allocated by an IA-32 module must never be used from an x64 module. + SetJump()/LongJump() is not currently supported for the EBC processor type. @param JumpBuffer A pointer to CPU context buffer. @retval 0 Indicates a return from SetJump(). **/ +RETURNS_TWICE UINTN EFIAPI SetJump ( @@ -3731,9 +5114,9 @@ CpuPause ( function. @param NewStack A pointer to the new stack to use for the EntryPoint function. - @param ... This variable argument list is ignored for IA-32, x64, and - EBC architectures. For Itanium processors, this variable - argument list is expected to contain a single parameter of + @param ... This variable argument list is ignored for IA-32, x64, and + EBC architectures. For Itanium processors, this variable + argument list is expected to contain a single parameter of type VOID * that specifies the new backing store pointer. @@ -3777,19 +5160,19 @@ EFIAPI CpuDeadLoop ( VOID ); - + #if defined (MDE_CPU_IPF) /** Flush a range of cache lines in the cache coherency domain of the calling CPU. - Flushes the cache lines specified by Address and Length. If Address is not aligned - on a cache line boundary, then entire cache line containing Address is flushed. - If Address + Length is not aligned on a cache line boundary, then the entire cache - line containing Address + Length - 1 is flushed. This function may choose to flush - the entire cache if that is more efficient than flushing the specified range. If - Length is 0, the no cache lines are flushed. Address is returned. + Flushes the cache lines specified by Address and Length. If Address is not aligned + on a cache line boundary, then entire cache line containing Address is flushed. + If Address + Length is not aligned on a cache line boundary, then the entire cache + line containing Address + Length - 1 is flushed. This function may choose to flush + the entire cache if that is more efficient than flushing the specified range. If + Length is 0, the no cache lines are flushed. Address is returned. This function is only available on Itanium processors. If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT(). @@ -3850,8 +5233,8 @@ AsmFci ( /** Reads the current value of a Processor Identifier Register (CPUID). - - Reads and returns the current value of Processor Identifier Register specified by Index. + + Reads and returns the current value of Processor Identifier Register specified by Index. The Index of largest implemented CPUID (One less than the number of implemented CPUID registers) is determined by CPUID [3] bits {7:0}. No parameter checking is performed on Index. If the Index value is beyond the @@ -3907,8 +5290,8 @@ AsmWritePsr ( /** Reads the current value of 64-bit Kernel Register #0 (KR0). - - Reads and returns the current value of KR0. + + Reads and returns the current value of KR0. This function is only available on Itanium processors. @return The current value of KR0. @@ -3924,7 +5307,7 @@ AsmReadKr0 ( /** Reads the current value of 64-bit Kernel Register #1 (KR1). - Reads and returns the current value of KR1. + Reads and returns the current value of KR1. This function is only available on Itanium processors. @return The current value of KR1. @@ -3940,7 +5323,7 @@ AsmReadKr1 ( /** Reads the current value of 64-bit Kernel Register #2 (KR2). - Reads and returns the current value of KR2. + Reads and returns the current value of KR2. This function is only available on Itanium processors. @return The current value of KR2. @@ -3956,7 +5339,7 @@ AsmReadKr2 ( /** Reads the current value of 64-bit Kernel Register #3 (KR3). - Reads and returns the current value of KR3. + Reads and returns the current value of KR3. This function is only available on Itanium processors. @return The current value of KR3. @@ -3972,9 +5355,9 @@ AsmReadKr3 ( /** Reads the current value of 64-bit Kernel Register #4 (KR4). - Reads and returns the current value of KR4. + Reads and returns the current value of KR4. This function is only available on Itanium processors. - + @return The current value of KR4. **/ @@ -3988,7 +5371,7 @@ AsmReadKr4 ( /** Reads the current value of 64-bit Kernel Register #5 (KR5). - Reads and returns the current value of KR5. + Reads and returns the current value of KR5. This function is only available on Itanium processors. @return The current value of KR5. @@ -4004,7 +5387,7 @@ AsmReadKr5 ( /** Reads the current value of 64-bit Kernel Register #6 (KR6). - Reads and returns the current value of KR6. + Reads and returns the current value of KR6. This function is only available on Itanium processors. @return The current value of KR6. @@ -4020,7 +5403,7 @@ AsmReadKr6 ( /** Reads the current value of 64-bit Kernel Register #7 (KR7). - Reads and returns the current value of KR7. + Reads and returns the current value of KR7. This function is only available on Itanium processors. @return The current value of KR7. @@ -4035,8 +5418,8 @@ AsmReadKr7 ( /** Write the current value of 64-bit Kernel Register #0 (KR0). - - Writes the current value of KR0. The 64-bit value written to + + Writes the current value of KR0. The 64-bit value written to the KR0 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR0. @@ -4054,7 +5437,7 @@ AsmWriteKr0 ( /** Write the current value of 64-bit Kernel Register #1 (KR1). - Writes the current value of KR1. The 64-bit value written to + Writes the current value of KR1. The 64-bit value written to the KR1 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR1. @@ -4072,7 +5455,7 @@ AsmWriteKr1 ( /** Write the current value of 64-bit Kernel Register #2 (KR2). - Writes the current value of KR2. The 64-bit value written to + Writes the current value of KR2. The 64-bit value written to the KR2 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR2. @@ -4090,7 +5473,7 @@ AsmWriteKr2 ( /** Write the current value of 64-bit Kernel Register #3 (KR3). - Writes the current value of KR3. The 64-bit value written to + Writes the current value of KR3. The 64-bit value written to the KR3 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR3. @@ -4108,7 +5491,7 @@ AsmWriteKr3 ( /** Write the current value of 64-bit Kernel Register #4 (KR4). - Writes the current value of KR4. The 64-bit value written to + Writes the current value of KR4. The 64-bit value written to the KR4 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR4. @@ -4126,7 +5509,7 @@ AsmWriteKr4 ( /** Write the current value of 64-bit Kernel Register #5 (KR5). - Writes the current value of KR5. The 64-bit value written to + Writes the current value of KR5. The 64-bit value written to the KR5 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR5. @@ -4144,7 +5527,7 @@ AsmWriteKr5 ( /** Write the current value of 64-bit Kernel Register #6 (KR6). - Writes the current value of KR6. The 64-bit value written to + Writes the current value of KR6. The 64-bit value written to the KR6 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR6. @@ -4162,7 +5545,7 @@ AsmWriteKr6 ( /** Write the current value of 64-bit Kernel Register #7 (KR7). - Writes the current value of KR7. The 64-bit value written to + Writes the current value of KR7. The 64-bit value written to the KR7 is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to KR7. @@ -4179,7 +5562,7 @@ AsmWriteKr7 ( /** Reads the current value of Interval Timer Counter Register (ITC). - + Reads and returns the current value of ITC. This function is only available on Itanium processors. @@ -4195,8 +5578,8 @@ AsmReadItc ( /** Reads the current value of Interval Timer Vector Register (ITV). - - Reads and returns the current value of ITV. + + Reads and returns the current value of ITV. This function is only available on Itanium processors. @return The current value of ITV. @@ -4211,7 +5594,7 @@ AsmReadItv ( /** Reads the current value of Interval Timer Match Register (ITM). - + Reads and returns the current value of ITM. This function is only available on Itanium processors. @@ -4226,8 +5609,8 @@ AsmReadItm ( /** Writes the current value of 64-bit Interval Timer Counter Register (ITC). - - Writes the current value of ITC. The 64-bit value written to the ITC is returned. + + Writes the current value of ITC. The 64-bit value written to the ITC is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to ITC. @@ -4244,8 +5627,8 @@ AsmWriteItc ( /** Writes the current value of 64-bit Interval Timer Match Register (ITM). - - Writes the current value of ITM. The 64-bit value written to the ITM is returned. + + Writes the current value of ITM. The 64-bit value written to the ITM is returned. This function is only available on Itanium processors. @param Value The 64-bit value to write to ITM. @@ -4262,8 +5645,8 @@ AsmWriteItm ( /** Writes the current value of 64-bit Interval Timer Vector Register (ITV). - - Writes the current value of ITV. The 64-bit value written to the ITV is returned. + + Writes the current value of ITV. The 64-bit value written to the ITV is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of ITV must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4284,7 +5667,7 @@ AsmWriteItv ( /** Reads the current value of Default Control Register (DCR). - + Reads and returns the current value of DCR. This function is only available on Itanium processors. @return The current value of DCR. @@ -4299,7 +5682,7 @@ AsmReadDcr ( /** Reads the current value of Interruption Vector Address Register (IVA). - + Reads and returns the current value of IVA. This function is only available on Itanium processors. @return The current value of IVA. @@ -4313,7 +5696,7 @@ AsmReadIva ( /** Reads the current value of Page Table Address Register (PTA). - + Reads and returns the current value of PTA. This function is only available on Itanium processors. @return The current value of PTA. @@ -4328,8 +5711,8 @@ AsmReadPta ( /** Writes the current value of 64-bit Default Control Register (DCR). - - Writes the current value of DCR. The 64-bit value written to the DCR is returned. + + Writes the current value of DCR. The 64-bit value written to the DCR is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of DCR must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4350,8 +5733,8 @@ AsmWriteDcr ( /** Writes the current value of 64-bit Interruption Vector Address Register (IVA). - - Writes the current value of IVA. The 64-bit value written to the IVA is returned. + + Writes the current value of IVA. The 64-bit value written to the IVA is returned. The size of vector table is 32 K bytes and is 32 K bytes aligned the low 15 bits of Value is ignored when written. This function is only available on Itanium processors. @@ -4370,8 +5753,8 @@ AsmWriteIva ( /** Writes the current value of 64-bit Page Table Address Register (PTA). - - Writes the current value of PTA. The 64-bit value written to the PTA is returned. + + Writes the current value of PTA. The 64-bit value written to the PTA is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of DCR must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4391,7 +5774,7 @@ AsmWritePta ( /** Reads the current value of Local Interrupt ID Register (LID). - + Reads and returns the current value of LID. This function is only available on Itanium processors. @return The current value of LID. @@ -4406,8 +5789,8 @@ AsmReadLid ( /** Reads the current value of External Interrupt Vector Register (IVR). - - Reads and returns the current value of IVR. This function is only available on Itanium processors. + + Reads and returns the current value of IVR. This function is only available on Itanium processors. @return The current value of IVR. @@ -4421,8 +5804,8 @@ AsmReadIvr ( /** Reads the current value of Task Priority Register (TPR). - - Reads and returns the current value of TPR. This function is only available on Itanium processors. + + Reads and returns the current value of TPR. This function is only available on Itanium processors. @return The current value of TPR. @@ -4436,8 +5819,8 @@ AsmReadTpr ( /** Reads the current value of External Interrupt Request Register #0 (IRR0). - - Reads and returns the current value of IRR0. This function is only available on Itanium processors. + + Reads and returns the current value of IRR0. This function is only available on Itanium processors. @return The current value of IRR0. @@ -4451,8 +5834,8 @@ AsmReadIrr0 ( /** Reads the current value of External Interrupt Request Register #1 (IRR1). - - Reads and returns the current value of IRR1. This function is only available on Itanium processors. + + Reads and returns the current value of IRR1. This function is only available on Itanium processors. @return The current value of IRR1. @@ -4466,7 +5849,7 @@ AsmReadIrr1 ( /** Reads the current value of External Interrupt Request Register #2 (IRR2). - + Reads and returns the current value of IRR2. This function is only available on Itanium processors. @return The current value of IRR2. @@ -4481,8 +5864,8 @@ AsmReadIrr2 ( /** Reads the current value of External Interrupt Request Register #3 (IRR3). - - Reads and returns the current value of IRR3. This function is only available on Itanium processors. + + Reads and returns the current value of IRR3. This function is only available on Itanium processors. @return The current value of IRR3. @@ -4496,8 +5879,8 @@ AsmReadIrr3 ( /** Reads the current value of Performance Monitor Vector Register (PMV). - - Reads and returns the current value of PMV. This function is only available on Itanium processors. + + Reads and returns the current value of PMV. This function is only available on Itanium processors. @return The current value of PMV. @@ -4511,7 +5894,7 @@ AsmReadPmv ( /** Reads the current value of Corrected Machine Check Vector Register (CMCV). - + Reads and returns the current value of CMCV. This function is only available on Itanium processors. @return The current value of CMCV. @@ -4526,8 +5909,8 @@ AsmReadCmcv ( /** Reads the current value of Local Redirection Register #0 (LRR0). - - Reads and returns the current value of LRR0. This function is only available on Itanium processors. + + Reads and returns the current value of LRR0. This function is only available on Itanium processors. @return The current value of LRR0. @@ -4541,7 +5924,7 @@ AsmReadLrr0 ( /** Reads the current value of Local Redirection Register #1 (LRR1). - + Reads and returns the current value of LRR1. This function is only available on Itanium processors. @return The current value of LRR1. @@ -4556,8 +5939,8 @@ AsmReadLrr1 ( /** Writes the current value of 64-bit Page Local Interrupt ID Register (LID). - - Writes the current value of LID. The 64-bit value written to the LID is returned. + + Writes the current value of LID. The 64-bit value written to the LID is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of LID must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4578,8 +5961,8 @@ AsmWriteLid ( /** Writes the current value of 64-bit Task Priority Register (TPR). - - Writes the current value of TPR. The 64-bit value written to the TPR is returned. + + Writes the current value of TPR. The 64-bit value written to the TPR is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of TPR must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4600,7 +5983,7 @@ AsmWriteTpr ( /** Performs a write operation on End OF External Interrupt Register (EOI). - + Writes a value of 0 to the EOI Register. This function is only available on Itanium processors. **/ @@ -4613,8 +5996,8 @@ AsmWriteEoi ( /** Writes the current value of 64-bit Performance Monitor Vector Register (PMV). - - Writes the current value of PMV. The 64-bit value written to the PMV is returned. + + Writes the current value of PMV. The 64-bit value written to the PMV is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of PMV must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4635,8 +6018,8 @@ AsmWritePmv ( /** Writes the current value of 64-bit Corrected Machine Check Vector Register (CMCV). - - Writes the current value of CMCV. The 64-bit value written to the CMCV is returned. + + Writes the current value of CMCV. The 64-bit value written to the CMCV is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of CMCV must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4657,8 +6040,8 @@ AsmWriteCmcv ( /** Writes the current value of 64-bit Local Redirection Register #0 (LRR0). - - Writes the current value of LRR0. The 64-bit value written to the LRR0 is returned. + + Writes the current value of LRR0. The 64-bit value written to the LRR0 is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of LRR0 must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must set up @@ -4679,8 +6062,8 @@ AsmWriteLrr0 ( /** Writes the current value of 64-bit Local Redirection Register #1 (LRR1). - - Writes the current value of LRR1. The 64-bit value written to the LRR1 is returned. + + Writes the current value of LRR1. The 64-bit value written to the LRR1 is returned. No parameter checking is performed on Value. All bits of Value corresponding to reserved fields of LRR1 must be 0 or a Reserved Register/Field fault may occur. The caller must either guarantee that Value is valid, or the caller must @@ -4701,7 +6084,7 @@ AsmWriteLrr1 ( /** Reads the current value of Instruction Breakpoint Register (IBR). - + The Instruction Breakpoint Registers are used in pairs. The even numbered registers contain breakpoint addresses, and the odd numbered registers contain breakpoint mask conditions. At least four instruction registers pairs are implemented @@ -4994,9 +6377,9 @@ AsmReadSp ( /** Reads a 64-bit control register. - Reads and returns the control register specified by Index. The valid Index valued + Reads and returns the control register specified by Index. The valid Index valued are defined above in "Related Definitions". - If Index is invalid then 0xFFFFFFFFFFFFFFFF is returned. This function is only + If Index is invalid then 0xFFFFFFFFFFFFFFFF is returned. This function is only available on Itanium processors. @param Index The index of the control register to read. @@ -5045,9 +6428,9 @@ AsmReadControlRegister ( /** Reads a 64-bit application register. - Reads and returns the application register specified by Index. The valid Index + Reads and returns the application register specified by Index. The valid Index valued are defined above in "Related Definitions". - If Index is invalid then 0xFFFFFFFFFFFFFFFF is returned. This function is only + If Index is invalid then 0xFFFFFFFFFFFFFFFF is returned. This function is only available on Itanium processors. @param Index The index of the application register to read. @@ -5073,13 +6456,13 @@ AsmReadApplicationRegister ( @param Index The 8-bit Machine Specific Register index to read. - @return The current value of the Machine Specific Register specified by Index. + @return The current value of the Machine Specific Register specified by Index. **/ UINT64 EFIAPI AsmReadMsr ( - IN UINT8 Index + IN UINT8 Index ); @@ -5095,14 +6478,14 @@ AsmReadMsr ( @param Index The 8-bit Machine Specific Register index to write. @param Value The 64-bit value to write to the Machine Specific Register. - @return The 64-bit value to write to the Machine Specific Register. + @return The 64-bit value to write to the Machine Specific Register. **/ UINT64 EFIAPI AsmWriteMsr ( - IN UINT8 Index, - IN UINT64 Value + IN UINT8 Index, + IN UINT64 Value ); @@ -5168,7 +6551,7 @@ AsmPalCall ( IN UINT64 Arg3, IN UINT64 Arg4 ); -#endif +#endif // defined (MDE_CPU_IPF) #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) /// @@ -5321,6 +6704,8 @@ typedef struct { #define IA32_IDT_GATE_TYPE_INTERRUPT_32 0x8E #define IA32_IDT_GATE_TYPE_TRAP_32 0x8F +#define IA32_GDT_TYPE_TSS 0x9 +#define IA32_GDT_ALIGNMENT 8 #if defined (MDE_CPU_IA32) /// @@ -5337,7 +6722,71 @@ typedef union { UINT64 Uint64; } IA32_IDT_GATE_DESCRIPTOR; -#endif +#pragma pack (1) +// +// IA32 Task-State Segment Definition +// +typedef struct { + UINT16 PreviousTaskLink; + UINT16 Reserved_2; + UINT32 ESP0; + UINT16 SS0; + UINT16 Reserved_10; + UINT32 ESP1; + UINT16 SS1; + UINT16 Reserved_18; + UINT32 ESP2; + UINT16 SS2; + UINT16 Reserved_26; + UINT32 CR3; + UINT32 EIP; + UINT32 EFLAGS; + UINT32 EAX; + UINT32 ECX; + UINT32 EDX; + UINT32 EBX; + UINT32 ESP; + UINT32 EBP; + UINT32 ESI; + UINT32 EDI; + UINT16 ES; + UINT16 Reserved_74; + UINT16 CS; + UINT16 Reserved_78; + UINT16 SS; + UINT16 Reserved_82; + UINT16 DS; + UINT16 Reserved_86; + UINT16 FS; + UINT16 Reserved_90; + UINT16 GS; + UINT16 Reserved_94; + UINT16 LDTSegmentSelector; + UINT16 Reserved_98; + UINT16 T; + UINT16 IOMapBaseAddress; +} IA32_TASK_STATE_SEGMENT; + +typedef union { + struct { + UINT32 LimitLow:16; ///< Segment Limit 15..00 + UINT32 BaseLow:16; ///< Base Address 15..00 + UINT32 BaseMid:8; ///< Base Address 23..16 + UINT32 Type:4; ///< Type (1 0 B 1) + UINT32 Reserved_43:1; ///< 0 + UINT32 DPL:2; ///< Descriptor Privilege Level + UINT32 P:1; ///< Segment Present + UINT32 LimitHigh:4; ///< Segment Limit 19..16 + UINT32 AVL:1; ///< Available for use by system software + UINT32 Reserved_52:2; ///< 0 0 + UINT32 G:1; ///< Granularity + UINT32 BaseHigh:8; ///< Base Address 31..24 + } Bits; + UINT64 Uint64; +} IA32_TSS_DESCRIPTOR; +#pragma pack () + +#endif // defined (MDE_CPU_IA32) #if defined (MDE_CPU_X64) /// @@ -5356,10 +6805,50 @@ typedef union { struct { UINT64 Uint64; UINT64 Uint64_1; - } Uint128; + } Uint128; } IA32_IDT_GATE_DESCRIPTOR; -#endif +#pragma pack (1) +// +// IA32 Task-State Segment Definition +// +typedef struct { + UINT32 Reserved_0; + UINT64 RSP0; + UINT64 RSP1; + UINT64 RSP2; + UINT64 Reserved_28; + UINT64 IST[7]; + UINT64 Reserved_92; + UINT16 Reserved_100; + UINT16 IOMapBaseAddress; +} IA32_TASK_STATE_SEGMENT; + +typedef union { + struct { + UINT32 LimitLow:16; ///< Segment Limit 15..00 + UINT32 BaseLow:16; ///< Base Address 15..00 + UINT32 BaseMidl:8; ///< Base Address 23..16 + UINT32 Type:4; ///< Type (1 0 B 1) + UINT32 Reserved_43:1; ///< 0 + UINT32 DPL:2; ///< Descriptor Privilege Level + UINT32 P:1; ///< Segment Present + UINT32 LimitHigh:4; ///< Segment Limit 19..16 + UINT32 AVL:1; ///< Available for use by system software + UINT32 Reserved_52:2; ///< 0 0 + UINT32 G:1; ///< Granularity + UINT32 BaseMidh:8; ///< Base Address 31..24 + UINT32 BaseHigh:32; ///< Base Address 63..32 + UINT32 Reserved_96:32; ///< Reserved + } Bits; + struct { + UINT64 Uint64; + UINT64 Uint64_1; + } Uint128; +} IA32_TSS_DESCRIPTOR; +#pragma pack () + +#endif // defined (MDE_CPU_X64) /// /// Byte packed structure for an FP/SSE/SSE2 context. @@ -5448,6 +6937,20 @@ typedef struct { #define THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 0x00000002 #define THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL 0x00000004 +/// +/// Type definition for representing labels in NASM source code that allow for +/// the patching of immediate operands of IA32 and X64 instructions. +/// +/// While the type is technically defined as a function type (note: not a +/// pointer-to-function type), such labels in NASM source code never stand for +/// actual functions, and identifiers declared with this function type should +/// never be called. This is also why the EFIAPI calling convention specifier +/// is missing from the typedef, and why the typedef does not follow the usual +/// edk2 coding style for function (or pointer-to-function) typedefs. The VOID +/// return type and the VOID argument list are merely artifacts. +/// +typedef VOID (X86_ASSEMBLY_PATCH_LABEL) (VOID); + /** Retrieves CPUID information. @@ -5724,8 +7227,8 @@ AsmMsrBitFieldRead32 ( Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit field is specified by the StartBit and the EndBit. All other bits in the destination MSR are preserved. The lower 32-bits of the MSR written is - returned. The caller must either guarantee that Index and the data written - is valid, or the caller must set up exception handlers to catch the exceptions. + returned. The caller must either guarantee that Index and the data written + is valid, or the caller must set up exception handlers to catch the exceptions. This function is only available on IA-32 and x64. If StartBit is greater than 31, then ASSERT(). @@ -5968,7 +7471,7 @@ AsmMsrAnd64 ( /** - Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise + Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise OR, and writes the result back to the 64-bit MSR. Reads the 64-bit MSR specified by Index, performs a bitwise AND between read @@ -6033,8 +7536,8 @@ AsmMsrBitFieldRead64 ( Writes Value to a bit field in a 64-bit MSR. The bit field is specified by the StartBit and the EndBit. All other bits in the destination MSR are - preserved. The MSR written is returned. The caller must either guarantee - that Index and the data written is valid, or the caller must set up exception + preserved. The MSR written is returned. The caller must either guarantee + that Index and the data written is valid, or the caller must set up exception handlers to catch the exceptions. This function is only available on IA-32 and x64. If StartBit is greater than 63, then ASSERT(). @@ -7446,7 +8949,7 @@ AsmDisablePaging64 ( in ExtraStackSize. If parameters are passed to the 16-bit real mode code, then the actual minimum stack size is ExtraStackSize plus the maximum number of bytes that need to be passed to the 16-bit real mode code. - + If RealModeBufferSize is NULL, then ASSERT(). If ExtraStackSize is NULL, then ASSERT(). @@ -7470,7 +8973,7 @@ AsmGetThunk16Properties ( Prepares all structures a code required to use AsmThunk16(). Prepares all structures and code required to use AsmThunk16(). - + This interface is limited to be used in either physical mode or virtual modes with paging enabled where the virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1. @@ -7494,43 +8997,43 @@ AsmPrepareThunk16 ( AsmPrepareThunk16() must be called with ThunkContext before this function is used. This function must be called with interrupts disabled. - The register state from the RealModeState field of ThunkContext is restored just prior - to calling the 16-bit real mode entry point. This includes the EFLAGS field of RealModeState, + The register state from the RealModeState field of ThunkContext is restored just prior + to calling the 16-bit real mode entry point. This includes the EFLAGS field of RealModeState, which is used to set the interrupt state when a 16-bit real mode entry point is called. Control is transferred to the 16-bit real mode entry point specified by the CS and Eip fields of RealModeState. - The stack is initialized to the SS and ESP fields of RealModeState. Any parameters passed to - the 16-bit real mode code must be populated by the caller at SS:ESP prior to calling this function. + The stack is initialized to the SS and ESP fields of RealModeState. Any parameters passed to + the 16-bit real mode code must be populated by the caller at SS:ESP prior to calling this function. The 16-bit real mode entry point is invoked with a 16-bit CALL FAR instruction, - so when accessing stack contents, the 16-bit real mode code must account for the 16-bit segment - and 16-bit offset of the return address that were pushed onto the stack. The 16-bit real mode entry - point must exit with a RETF instruction. The register state is captured into RealModeState immediately + so when accessing stack contents, the 16-bit real mode code must account for the 16-bit segment + and 16-bit offset of the return address that were pushed onto the stack. The 16-bit real mode entry + point must exit with a RETF instruction. The register state is captured into RealModeState immediately after the RETF instruction is executed. - - If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts, - or any of the 16-bit real mode code makes a SW interrupt, then the caller is responsible for making sure - the IDT at address 0 is initialized to handle any HW or SW interrupts that may occur while in 16-bit real mode. - - If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts, - then the caller is responsible for making sure the 8259 PIC is in a state compatible with 16-bit real mode. + + If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts, + or any of the 16-bit real mode code makes a SW interrupt, then the caller is responsible for making sure + the IDT at address 0 is initialized to handle any HW or SW interrupts that may occur while in 16-bit real mode. + + If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts, + then the caller is responsible for making sure the 8259 PIC is in a state compatible with 16-bit real mode. This includes the base vectors, the interrupt masks, and the edge/level trigger mode. - - If THUNK_ATTRIBUTE_BIG_REAL_MODE is set in the ThunkAttributes field of ThunkContext, then the user code + + If THUNK_ATTRIBUTE_BIG_REAL_MODE is set in the ThunkAttributes field of ThunkContext, then the user code is invoked in big real mode. Otherwise, the user code is invoked in 16-bit real mode with 64KB segment limits. - - If neither THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 nor THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in - ThunkAttributes, then it is assumed that the user code did not enable the A20 mask, and no attempt is made to + + If neither THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 nor THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in + ThunkAttributes, then it is assumed that the user code did not enable the A20 mask, and no attempt is made to disable the A20 mask. - - If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is set and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is clear in - ThunkAttributes, then attempt to use the INT 15 service to disable the A20 mask. If this INT 15 call fails, + + If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is set and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is clear in + ThunkAttributes, then attempt to use the INT 15 service to disable the A20 mask. If this INT 15 call fails, then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports. - - If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is clear and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is set in + + If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is clear and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is set in ThunkAttributes, then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports. - + If ThunkContext is NULL, then ASSERT(). If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT(). - If both THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in + If both THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in ThunkAttributes, then ASSERT(). This interface is limited to be used in either physical mode or virtual modes with paging enabled where the @@ -7573,7 +9076,109 @@ AsmPrepareAndThunk16 ( IN OUT THUNK_CONTEXT *ThunkContext ); -#endif -#endif +/** + Generates a 16-bit random number through RDRAND instruction. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the random result. + + @retval TRUE RDRAND call was successful. + @retval FALSE Failed attempts to call RDRAND. + + **/ +BOOLEAN +EFIAPI +AsmRdRand16 ( + OUT UINT16 *Rand + ); + +/** + Generates a 32-bit random number through RDRAND instruction. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the random result. + + @retval TRUE RDRAND call was successful. + @retval FALSE Failed attempts to call RDRAND. + +**/ +BOOLEAN +EFIAPI +AsmRdRand32 ( + OUT UINT32 *Rand + ); + +/** + Generates a 64-bit random number through RDRAND instruction. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the random result. + + @retval TRUE RDRAND call was successful. + @retval FALSE Failed attempts to call RDRAND. + +**/ +BOOLEAN +EFIAPI +AsmRdRand64 ( + OUT UINT64 *Rand + ); + +/** + Load given selector into TR register. + + @param[in] Selector Task segment selector +**/ +VOID +EFIAPI +AsmWriteTr ( + IN UINT16 Selector + ); + +/** + Patch the immediate operand of an IA32 or X64 instruction such that the byte, + word, dword or qword operand is encoded at the end of the instruction's + binary representation. + + This function should be used to update object code that was compiled with + NASM from assembly source code. Example: + + NASM source code: + + mov eax, strict dword 0 ; the imm32 zero operand will be patched + ASM_PFX(gPatchCr3): + mov cr3, eax + C source code: + + X86_ASSEMBLY_PATCH_LABEL gPatchCr3; + PatchInstructionX86 (gPatchCr3, AsmReadCr3 (), 4); + + @param[out] InstructionEnd Pointer right past the instruction to patch. The + immediate operand to patch is expected to + comprise the trailing bytes of the instruction. + If InstructionEnd is closer to address 0 than + ValueSize permits, then ASSERT(). + + @param[in] PatchValue The constant to write to the immediate operand. + The caller is responsible for ensuring that + PatchValue can be represented in the byte, word, + dword or qword operand (as indicated through + ValueSize); otherwise ASSERT(). + + @param[in] ValueSize The size of the operand in bytes; must be 1, 2, + 4, or 8. ASSERT() otherwise. +**/ +VOID +EFIAPI +PatchInstructionX86 ( + OUT X86_ASSEMBLY_PATCH_LABEL *InstructionEnd, + IN UINT64 PatchValue, + IN UINTN ValueSize + ); +#endif // defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) +#endif // !defined (__BASE_LIB__)