devicepathfromtext.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 2,229 行 · 第 1/4 页
C
2,229 行
/*++
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
DevicePathFromText.c
Abstract:
DevicePathFromText protocol as defined in the UEFI 2.0 specification.
--*/
#include "DevicePathDriver.h"
CHAR16 *
StrDuplicate (
IN CONST CHAR16 *Src
)
/*++
Routine Description:
Duplicate a string
Arguments:
Src - Source string
Returns:
Duplicated string
--*/
{
UINTN Length;
CHAR16 *ReturnStr;
Length = EfiStrLen ((CHAR16 *) Src);
ReturnStr = EfiLibAllocateCopyPool ((Length + 1) * sizeof (CHAR16), (VOID *) Src);
return ReturnStr;
}
CHAR16 *
GetParamByNodeName (
IN CHAR16 *Str,
IN CHAR16 *NodeName
)
/*++
Routine Description:
Get parameter in a pair of parentheses follow the given node name.
For example, given the "Pci(0,1)" and NodeName "Pci", it returns "0,1".
Arguments:
Str - Device Path Text
NodeName - Name of the node
Returns:
Parameter text for the node
--*/
{
CHAR16 *ParamStr;
CHAR16 *StrPointer;
UINTN NodeNameLength;
UINTN ParameterLength;
//
// Check whether the node name matchs
//
NodeNameLength = EfiStrLen (NodeName);
if (EfiCompareMem (Str, NodeName, NodeNameLength * sizeof (CHAR16)) != 0) {
return NULL;
}
ParamStr = Str + NodeNameLength;
if (!IS_LEFT_PARENTH (*ParamStr)) {
return NULL;
}
//
// Skip the found '(' and find first occurrence of ')'
//
ParamStr++;
ParameterLength = 0;
StrPointer = ParamStr;
while (!IS_NULL (*StrPointer)) {
if (IS_RIGHT_PARENTH (*StrPointer)) {
break;
}
StrPointer++;
ParameterLength++;
}
if (IS_NULL (*StrPointer)) {
//
// ')' not found
//
return NULL;
}
ParamStr = EfiLibAllocateCopyPool ((ParameterLength + 1) * sizeof (CHAR16), ParamStr);
if (ParamStr == NULL) {
return NULL;
}
//
// Terminate the parameter string
//
ParamStr[ParameterLength] = L'\0';
return ParamStr;
}
CHAR16 *
SplitStr (
IN OUT CHAR16 **List,
IN CHAR16 Separator
)
/*++
Routine Description:
Get current sub-string from a string list, before return
the list header is moved to next sub-string. The sub-string is separated
by the specified character. For example, the separator is ',', the string
list is "2,0,3", it returns "2", the remain list move to "2,3"
Arguments:
List - A string list separated by the specified separator
Separator - The separator character
Returns:
pointer - The current sub-string
--*/
{
CHAR16 *Str;
CHAR16 *ReturnStr;
Str = *List;
ReturnStr = Str;
if (IS_NULL (*Str)) {
return ReturnStr;
}
//
// Find first occurrence of the separator
//
while (!IS_NULL (*Str)) {
if (*Str == Separator) {
break;
}
Str++;
}
if (*Str == Separator) {
//
// Find a sub-string, terminate it
//
*Str = L'\0';
Str++;
}
//
// Move to next sub-string
//
*List = Str;
return ReturnStr;
}
CHAR16 *
GetNextParamStr (
IN OUT CHAR16 **List
)
{
//
// The separator is comma
//
return SplitStr (List, L',');
}
CHAR16 *
GetNextDeviceNodeStr (
IN OUT CHAR16 **DevicePath,
OUT BOOLEAN *IsInstanceEnd
)
/*++
Routine Description:
Get one device node from entire device path text.
Arguments:
Str - The entire device path text string
IsInstanceEnd - This node is the end of a device path instance
Returns:
a pointer - A device node text
NULL - No more device node available
--*/
{
CHAR16 *Str;
CHAR16 *ReturnStr;
UINTN ParenthesesStack;
Str = *DevicePath;
if (IS_NULL (*Str)) {
return NULL;
}
//
// Skip the leading '/', '(', ')' and ','
//
while (!IS_NULL (*Str)) {
if (!IS_SLASH (*Str) &&
!IS_COMMA (*Str) &&
!IS_LEFT_PARENTH (*Str) &&
!IS_RIGHT_PARENTH (*Str)) {
break;
}
Str++;
}
ReturnStr = Str;
//
// Scan for the separator of this device node, '/' or ','
//
ParenthesesStack = 0;
while (!IS_NULL (*Str)) {
if ((IS_COMMA (*Str) || IS_SLASH (*Str)) && (ParenthesesStack == 0)) {
break;
}
if (IS_LEFT_PARENTH (*Str)) {
ParenthesesStack++;
} else if (IS_RIGHT_PARENTH (*Str)) {
ParenthesesStack--;
}
Str++;
}
if (ParenthesesStack != 0) {
//
// The '(' doesn't pair with ')', invalid device path text
//
return NULL;
}
if (IS_COMMA (*Str)) {
*IsInstanceEnd = TRUE;
*Str = L'\0';
Str++;
} else {
*IsInstanceEnd = FALSE;
if (!IS_NULL (*Str)) {
*Str = L'\0';
Str++;
}
}
*DevicePath = Str;
return ReturnStr;
}
CHAR16 *
TrimHexStr (
IN CHAR16 *Str
)
/*++
Routine Description:
Skip the leading white space and '0x' or '0X' of a hex string
Arguments:
Str - The hex string
Returns:
--*/
{
//
// skip preceeding white space
//
while (*Str && *Str == ' ') {
Str += 1;
}
//
// skip preceeding zeros
//
while (*Str && *Str == '0') {
Str += 1;
}
//
// skip preceeding character 'x'
//
if (*Str && (*Str == 'x' || *Str == 'X')) {
Str += 1;
}
return Str;
}
UINTN
Xtoi (
IN CHAR16 *Str
)
/*++
Routine Description:
Convert hex string to uint
Arguments:
Str - The string
Returns:
--*/
{
UINTN u;
UINTN Length;
ASSERT (Str != NULL);
//
// convert hex digits
//
u = 0;
Length = sizeof (UINTN);
HexStringToBuf ((UINT8 *) &u, &Length, TrimHexStr (Str), NULL);
return u;
}
VOID
Xtoi64 (
IN CHAR16 *Str,
IN UINT64 *Data
)
/*++
Routine Description:
Convert hex string to 64 bit data.
Arguments:
Str - The string
Returns:
--*/
{
UINTN Length;
*Data = 0;
Length = sizeof (UINT64);
HexStringToBuf ((UINT8 *) Data, &Length, TrimHexStr (Str), NULL);
}
UINTN
Atoi (
IN CHAR16 *str
)
/*++
Routine Description:
Convert decimal string to uint
Arguments:
Str - The string
Returns:
--*/
{
UINTN u;
CHAR16 c;
UINTN m;
UINTN n;
ASSERT (str != NULL);
m = (UINTN) -1 / 10;
n = (UINTN) -1 % 10;
//
// skip preceeding white space
//
while (*str && *str == ' ') {
str += 1;
}
//
// convert digits
//
u = 0;
c = *(str++);
while (c) {
if (c >= '0' && c <= '9') {
if (u > m || u == m && c - '0' > (INTN) n) {
return (UINTN) -1;
}
u = (u * 10) + c - '0';
} else {
break;
}
c = *(str++);
}
return u;
}
EFI_STATUS
StrToBuf (
OUT UINT8 *Buf,
IN UINTN BufferLength,
IN CHAR16 *Str
)
{
UINTN Index;
UINTN StrLength;
UINT8 Digit;
UINT8 Byte;
//
// Two hex char make up one byte
//
StrLength = BufferLength * sizeof (CHAR16);
for(Index = 0; Index < StrLength; Index++, Str++) {
IsHexDigit (&Digit, *Str);
//
// For odd charaters, write the upper nibble for each buffer byte,
// and for even characters, the lower nibble.
//
if ((Index & 1) == 0) {
Byte = Digit << 4;
} else {
Byte = Buf[Index / 2];
Byte &= 0xF0;
Byte |= Digit;
}
Buf[Index / 2] = Byte;
}
return EFI_SUCCESS;
}
EFI_STATUS
StrToGuid (
IN CHAR16 *Str,
OUT EFI_GUID *Guid
)
{
UINTN BufferLength;
UINTN ConvertedStrLen;
EFI_STATUS Status;
BufferLength = sizeof (Guid->Data1);
Status = HexStringToBuf ((UINT8 *) &Guid->Data1, &BufferLength, Str, &ConvertedStrLen);
if (EFI_ERROR (Status)) {
return Status;
}
Str += ConvertedStrLen;
if (IS_HYPHEN (*Str)) {
Str++;
} else {
return EFI_UNSUPPORTED;
}
BufferLength = sizeof (Guid->Data2);
Status = HexStringToBuf ((UINT8 *) &Guid->Data2, &BufferLength, Str, &ConvertedStrLen);
if (EFI_ERROR (Status)) {
return Status;
}
Str += ConvertedStrLen;
if (IS_HYPHEN (*Str)) {
Str++;
} else {
return EFI_UNSUPPORTED;
}
BufferLength = sizeof (Guid->Data3);
Status = HexStringToBuf ((UINT8 *) &Guid->Data3, &BufferLength, Str, &ConvertedStrLen);
if (EFI_ERROR (Status)) {
return Status;
}
Str += ConvertedStrLen;
if (IS_HYPHEN (*Str)) {
Str++;
} else {
return EFI_UNSUPPORTED;
}
StrToBuf (&Guid->Data4[0], 2, Str);
//
// Skip 2 byte hex chars
//
Str += 2 * 2;
if (IS_HYPHEN (*Str)) {
Str++;
} else {
return EFI_UNSUPPORTED;
}
StrToBuf (&Guid->Data4[2], 6, Str);
return EFI_SUCCESS;
}
VOID
StrToIPv4Addr (
IN OUT CHAR16 **Str,
OUT EFI_IPv4_ADDRESS *IPv4Addr
)
{
UINTN Index;
for (Index = 0; Index < 4; Index++) {
IPv4Addr->Addr[Index] = (UINT8) Atoi (SplitStr (Str, L'.'));
}
}
VOID
StrToIPv6Addr (
IN OUT CHAR16 **Str,
OUT EFI_IPv6_ADDRESS *IPv6Addr
)
{
UINTN Index;
UINT16 Data;
for (Index = 0; Index < 8; Index++) {
Data = (UINT16) Xtoi (SplitStr (Str, L':'));
IPv6Addr->Addr[Index * 2] = (UINT8) (Data >> 8);
IPv6Addr->Addr[Index * 2 + 1] = (UINT8) (Data & 0xff);
}
}
VOID
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?