From 47a15937a1c745ba0790e58ff7f3028ddd3b7ac1 Mon Sep 17 00:00:00 2001 From: Subash Lakkimsetti Date: Mon, 11 May 2020 14:26:41 -0700 Subject: [PATCH] String function Support library Add string functionality in Osloader to a common library Signed-off-by: Subash Lakkimsetti --- .../Include/Library/StringSupportLib.h | 109 ++++++++++ .../StringSupportLib/StringSupportLib.c | 195 ++++++++++++++++++ .../StringSupportLib/StringSupportLib.inf | 27 +++ BootloaderCorePkg/BootloaderCorePkg.dsc | 2 + PayloadPkg/OsLoader/BootConfig.c | 158 -------------- PayloadPkg/OsLoader/OsLoader.h | 2 + PayloadPkg/OsLoader/OsLoader.inf | 1 + 7 files changed, 336 insertions(+), 158 deletions(-) create mode 100644 BootloaderCommonPkg/Include/Library/StringSupportLib.h create mode 100644 BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.c create mode 100644 BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.inf diff --git a/BootloaderCommonPkg/Include/Library/StringSupportLib.h b/BootloaderCommonPkg/Include/Library/StringSupportLib.h new file mode 100644 index 00000000..8adcfec7 --- /dev/null +++ b/BootloaderCommonPkg/Include/Library/StringSupportLib.h @@ -0,0 +1,109 @@ +/** @file +Header file for string function support library + +Copyright (c) 2020, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _STRING_SUPPORT_LIB_H_ +#define _STRING_SUPPORT_LIB_H_ + +#include + +/** + Get next line start pointer. + + @param[in] Start The pointer for current line. + @param[out] Length The UINT32 pointer to receive the length of current line. + + @retval The pointer for next line. + NULL if no more line is available. +**/ + +CHAR8 * +EFIAPI +GetNextLine ( + IN CHAR8 *Start, + OUT UINT32 *Length + ); + +/** + Trim leading white space for a line. + + @param[in] Line The pointer to the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +TrimLeft ( + IN CHAR8 *Line + ); + +/** + Get next space for the start of a line. + + @param[in] LineStart The pointer to the start of the line buffer. + @param[in] LineEnd The pointer to the end of the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +GetNextSpace ( + IN CHAR8 *LineStart, + IN CHAR8 *LineEnd + ); + +/** + Trim trailing white space for a line. + + @param[in] Line The pointer to the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +TrimRight ( + IN CHAR8 *Line + ); + +/** + Check if a line starts with a known keyword. + + @param[in] Line The pointer to the line buffer. + @param[in] Keyword The pointer to the keyword. + + @retval Length of the matched keyword. + 0 if there is no match. + +**/ +UINT32 +EFIAPI +MatchKeyWord ( + IN CHAR8 *Line, + IN CHAR8 *Keyword + ); + +/** + Check if a line matches varaible assignment syntax. + + @param[in] Line The pointer to the line buffer. + @param[in] Variable The pointer to the variable name. + + @retval Length of the matched variable name. + 0 if there is no match. + +**/ +UINT32 +EFIAPI +MatchAssignment ( + IN CHAR8 *Line, + IN CHAR8 *Variable + ); + +#endif diff --git a/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.c b/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.c new file mode 100644 index 00000000..414783fd --- /dev/null +++ b/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.c @@ -0,0 +1,195 @@ +/** @file + String support library implementation. + + Copyright (c) 2020, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +/** + Get next line start pointer. + + @param[in] Start The pointer for current line. + @param[out] Length The UINT32 pointer to receive the length of current line. + + @retval The pointer for next line. + NULL if no more line is available. +**/ +CHAR8 * +EFIAPI +GetNextLine ( + IN CHAR8 *Start, + OUT UINT32 *Length + ) +{ + CHAR8 *Ptr; + + if (Start == NULL) { + return NULL; + } + + Ptr = Start; + while ((Ptr[0] != 0) && (Ptr[0] != '\n')) { + Ptr++; + } + + if (Length != NULL){ + *Length = (UINT32)(Ptr - Start); + } + + if (Ptr[0] == 0) { + return NULL; + } else { + return Ptr + 1; + } +} + + +/** + Trim leading white space for a line. + + @param[in] Line The pointer to the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +TrimLeft ( + IN CHAR8 *Line + ) +{ + if (Line == NULL) { + return NULL; + } + + while ((Line[0] == ' ') || (Line[0] == '\t')) { + Line++; + } + return Line; +} + + +/** + Get next space for the start of a line. + + @param[in] LineStart The pointer to the start of the line buffer. + @param[in] LineEnd The pointer to the end of the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +GetNextSpace ( + IN CHAR8 *LineStart, + IN CHAR8 *LineEnd + ) +{ + CHAR8 *Line; + + if ((LineStart == NULL) || (LineEnd == NULL)) { + return NULL; + } + + Line = LineStart; + while ((Line[0] != 0) && (Line[0] != ' ') && (Line[0] != '\t') && (Line < LineEnd)) { + Line++; + } + return Line; +} + + +/** + Trim trailing white space for a line. + + @param[in] Line The pointer to the line buffer. + + @retval The pointer to the trimmed line. + +**/ +CHAR8 * +EFIAPI +TrimRight ( + IN CHAR8 *Line + ) +{ + if (Line == NULL) { + return NULL; + } + + while ((Line[0] == ' ') || (Line[0] == '\t') || (Line[0] == '\r') || (Line[0] == '\n')) { + Line--; + } + return Line; +} + + +/** + Check if a line starts with a known keyword. + + @param[in] Line The pointer to the line buffer. + @param[in] Keyword The pointer to the keyword. + + @retval Length of the matched keyword. + 0 if there is no match. + +**/ +UINT32 +EFIAPI +MatchKeyWord ( + IN CHAR8 *Line, + IN CHAR8 *Keyword + ) +{ + UINT32 KeywordLen; + + if ((Line == NULL) || (Keyword == NULL)) { + return 0; + } + + KeywordLen = (UINT32)AsciiStrLen (Keyword); + if (AsciiStrnCmp (Line, Keyword, KeywordLen) == 0) { + // A separator is required to follow the keyword + if (((Line[KeywordLen] == 0)) || (Line[KeywordLen] == ' ') || (Line[KeywordLen] == '\t')) { + return KeywordLen; + } + } + return 0; +} + + +/** + Check if a line matches varaible assignment syntax. + + @param[in] Line The pointer to the line buffer. + @param[in] Variable The pointer to the variable name. + + @retval Length of the matched variable name. + 0 if there is no match. + +**/ +UINT32 +EFIAPI +MatchAssignment ( + IN CHAR8 *Line, + IN CHAR8 *Variable + ) +{ + UINT32 VarLen; + + if ((Line == NULL) || (Variable == NULL)) { + return 0; + } + + VarLen = (UINT32)AsciiStrLen (Variable); + if (AsciiStrnCmp (Line, Variable, VarLen) == 0) { + if (Line[VarLen] == '=') { + return VarLen; + } + } + return 0; +} + diff --git a/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.inf b/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.inf new file mode 100644 index 00000000..bbcab794 --- /dev/null +++ b/BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.inf @@ -0,0 +1,27 @@ +## @file +# String Support Library Instance. +# +# Copyright (c) 2020, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = StringSupportLib + FILE_GUID = BB1506A8-93C9-11EA-BB37-0242AC130002 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = StringSupportLib + +[Sources] + StringSupportLib.c + +[Packages] + MdePkg/MdePkg.dec + BootloaderCommonPkg/BootloaderCommonPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + diff --git a/BootloaderCorePkg/BootloaderCorePkg.dsc b/BootloaderCorePkg/BootloaderCorePkg.dsc index b4fdd04b..f0e2d46e 100644 --- a/BootloaderCorePkg/BootloaderCorePkg.dsc +++ b/BootloaderCorePkg/BootloaderCorePkg.dsc @@ -118,6 +118,8 @@ SortLib|BootloaderCommonPkg/Library/SortLib/SortLib.inf IoMmuLib|BootloaderCommonPkg/Library/IoMmuLib/IoMmuLib.inf MtrrLib|BootloaderCommonPkg/Library/MtrrLib/MtrrLib.inf + StringSupportLib|BootloaderCommonPkg/Library/StringSupportLib/StringSupportLib.inf + !if $(ENABLE_SOURCE_DEBUG) DebugAgentLib|BootloaderCommonPkg/Library/DebugAgentLib/DebugAgentLib.inf diff --git a/PayloadPkg/OsLoader/BootConfig.c b/PayloadPkg/OsLoader/BootConfig.c index e691e069..2cf0b8b4 100644 --- a/PayloadPkg/OsLoader/BootConfig.c +++ b/PayloadPkg/OsLoader/BootConfig.c @@ -7,164 +7,6 @@ #include "OsLoader.h" -/** - Get next line start pointer. - - @param[in] Start The pointer for current line. - @param[out] Length The UINT32 pointer to receive the length of current line. - - @retval The pointer for next line. - NULL if no more line is available. -**/ -STATIC -CHAR8 * -GetNextLine ( - IN CHAR8 *Start, - OUT UINT32 *Length - ) -{ - CHAR8 *Ptr; - - Ptr = Start; - while ((Ptr[0] != 0) && (Ptr[0] != '\n')) { - Ptr++; - } - - *Length = (UINT32)(Ptr - Start); - if (Ptr[0] == 0) { - return NULL; - } else { - return Ptr + 1; - } -} - - -/** - Trim leading white space for a line. - - @param[in] Line The pointer to the line buffer. - - @retval The pointer to the trimmed line. - -**/ -STATIC -CHAR8 * -TrimLeft ( - IN CHAR8 *Line - ) -{ - while ((Line[0] == ' ') || (Line[0] == '\t')) { - Line++; - } - return Line; -} - - -/** - Get next space for the start of a line. - - @param[in] LineStart The pointer to the start of the line buffer. - @param[in] LineEnd The pointer to the end of the line buffer. - - @retval The pointer to the trimmed line. - -**/ -STATIC -CHAR8 * -GetNextSpace ( - IN CHAR8 *LineStart, - IN CHAR8 *LineEnd - ) -{ - CHAR8 *Line; - - Line = LineStart; - while ((Line[0] != 0) && (Line[0] != ' ') && (Line[0] != '\t') && (Line < LineEnd)) { - Line++; - } - return Line; -} - - -/** - Trim trailing white space for a line. - - @param[in] Line The pointer to the line buffer. - - @retval The pointer to the trimmed line. - -**/ -STATIC -CHAR8 * -TrimRight ( - IN CHAR8 *Line - ) -{ - while ((Line[0] == ' ') || (Line[0] == '\t') || (Line[0] == '\r') || (Line[0] == '\n')) { - Line--; - } - return Line; -} - - -/** - Check if a line starts with a known keyword. - - @param[in] Line The pointer to the line buffer. - @param[in] Keyword The pointer to the keyword. - - @retval Length of the matched keyword. - 0 if there is no match. - -**/ -STATIC -UINT32 -MatchKeyWord ( - IN CHAR8 *Line, - IN CHAR8 *Keyword - ) -{ - UINT32 KeywordLen; - - KeywordLen = (UINT32)AsciiStrLen (Keyword); - if (AsciiStrnCmp (Line, Keyword, KeywordLen) == 0) { - // A separator is required to follow the keyword - if (((Line[KeywordLen] == 0)) || (Line[KeywordLen] == ' ') || (Line[KeywordLen] == '\t')) { - return KeywordLen; - } - } - return 0; -} - - -/** - Check if a line matches varaible assignment syntax. - - @param[in] Line The pointer to the line buffer. - @param[in] Variable The pointer to the variable name. - - @retval Length of the matched variable name. - 0 if there is no match. - -**/ -STATIC -UINT32 -MatchAssignment ( - IN CHAR8 *Line, - IN CHAR8 *Variable - ) -{ - UINT32 VarLen; - - VarLen = (UINT32)AsciiStrLen (Variable); - if (AsciiStrnCmp (Line, Variable, VarLen) == 0) { - if (Line[VarLen] == '=') { - return VarLen; - } - } - return 0; -} - /** Parse the Linux boot configuration file. diff --git a/PayloadPkg/OsLoader/OsLoader.h b/PayloadPkg/OsLoader/OsLoader.h index d8bc6633..3c81c983 100644 --- a/PayloadPkg/OsLoader/OsLoader.h +++ b/PayloadPkg/OsLoader/OsLoader.h @@ -62,6 +62,8 @@ #include #include #include "PreOsChecker.h" +#include + #define MKHI_BOOTLOADER_SEED_LEN 64 diff --git a/PayloadPkg/OsLoader/OsLoader.inf b/PayloadPkg/OsLoader/OsLoader.inf index d83ab94b..be9c8d1d 100644 --- a/PayloadPkg/OsLoader/OsLoader.inf +++ b/PayloadPkg/OsLoader/OsLoader.inf @@ -80,6 +80,7 @@ LiteFvLib LinuxLib ContainerLib + StringSupportLib [Guids] gOsConfigDataGuid