diff --git a/Library/OcXmlLib/OcXmlLib.c b/Library/OcXmlLib/OcXmlLib.c index 9d755a91..55082dcd 100755 --- a/Library/OcXmlLib/OcXmlLib.c +++ b/Library/OcXmlLib/OcXmlLib.c @@ -64,6 +64,7 @@ typedef struct XML_PARSER_ XML_PARSER; // struct XML_NODE_ { CONST CHAR8 *Name; + CONST CHAR8 *Attributes; CONST CHAR8 *Content; XML_NODE_LIST *Children; }; @@ -130,6 +131,7 @@ STATIC XML_NODE * XmlNodeCreate ( CONST CHAR8 *Name, + CONST CHAR8 *Attributes, CONST CHAR8 *Content, XML_NODE_LIST *Children ) @@ -139,9 +141,10 @@ XmlNodeCreate ( Node = AllocatePool (sizeof (XML_NODE)); if (Node != NULL) { - Node->Name = Name; - Node->Content = Content; - Node->Children = Children; + Node->Name = Name; + Node->Attributes = Attributes; + Node->Content = Content; + Node->Children = Children; } return Node; @@ -409,11 +412,13 @@ STATIC CONST CHAR8 * XmlParseTagEnd ( XML_PARSER *Parser, - BOOLEAN *SelfClosing + BOOLEAN *SelfClosing, + CONST CHAR8 **Attributes ) { CHAR8 Current; UINT32 Start; + UINT32 AttributeStart; UINT32 Length = 0; UINT32 NameLength = 0; @@ -446,9 +451,22 @@ XmlParseTagEnd ( } // - // No attributes besides name. + // Handle attributes. // - if (NameLength == 0) { + if (NameLength != 0) { + if (Attributes != NULL) { + *Attributes = &Parser->Buffer[Start + NameLength]; + AttributeStart = NameLength; + while (IsAsciiSpace (**Attributes) && AttributeStart < Length) { + (*Attributes)++; + AttributeStart++; + } + Parser->Buffer[Start + Length] = '\0'; + } + } else { + // + // No attributes besides name. + // NameLength = Length; } @@ -491,7 +509,8 @@ STATIC CONST CHAR8 * XmlParseTagOpen ( XML_PARSER *Parser, - BOOLEAN *SelfClosing + BOOLEAN *SelfClosing, + CONST CHAR8 **Attributes ) { CHAR8 Current; @@ -539,7 +558,7 @@ XmlParseTagOpen ( // // Consume tag name. // - return XmlParseTagEnd (Parser, SelfClosing); + return XmlParseTagEnd (Parser, SelfClosing, Attributes); } // @@ -589,7 +608,7 @@ XmlParseTagClose ( // // Consume tag name. // - return XmlParseTagEnd(Parser, NULL); + return XmlParseTagEnd(Parser, NULL, NULL); } // @@ -721,6 +740,11 @@ XmlNodeExportRecursive ( XmlBufferAppend (Buffer, AllocSize, CurrentSize, "<", L_STR_LEN ("<")); XmlBufferAppend (Buffer, AllocSize, CurrentSize, Node->Name, NameLength); + if (Node->Attributes != NULL) { + XmlBufferAppend (Buffer, AllocSize, CurrentSize, " ", L_STR_LEN (" ")); + XmlBufferAppend (Buffer, AllocSize, CurrentSize, Node->Attributes, AsciiStrLen (Node->Attributes)); + } + if (Node->Children != NULL || Node->Content != NULL) { XmlBufferAppend (Buffer, AllocSize, CurrentSize, ">", L_STR_LEN (">")); @@ -763,17 +787,22 @@ XmlParseNode ( { CONST CHAR8 *TagOpen; CONST CHAR8 *TagClose; + CONST CHAR8 *Attributes; XML_NODE *Node; XML_NODE *Child; - BOOLEAN SelfClosing = FALSE; - BOOLEAN Unprefixed = FALSE; + BOOLEAN SelfClosing; + BOOLEAN Unprefixed; XML_PARSER_INFO (Parser, "node"); + Attributes = NULL; + SelfClosing = FALSE; + Unprefixed = FALSE; + // // Parse open tag. // - TagOpen = XmlParseTagOpen (Parser, &SelfClosing); + TagOpen = XmlParseTagOpen (Parser, &SelfClosing, &Attributes); if (TagOpen == NULL) { if ('/' != XmlParserPeek (Parser, CURRENT_CHARACTER)) { XML_PARSER_ERROR (Parser, NO_CHARACTER, "XmlParseNode::tag_open"); @@ -783,7 +812,7 @@ XmlParseNode ( XmlSkipWhitespace (Parser); - Node = XmlNodeCreate (TagOpen, NULL, NULL); + Node = XmlNodeCreate (TagOpen, Attributes, NULL, NULL); if (Node == NULL) { XML_PARSER_ERROR (Parser, NO_CHARACTER, "XmlParseNode::node alloc fail"); return NULL; diff --git a/Tests/KernelTest/KernelTest.c b/Tests/KernelTest/KernelTest.c index 40db045e..98b23b95 100644 --- a/Tests/KernelTest/KernelTest.c +++ b/Tests/KernelTest/KernelTest.c @@ -31,6 +31,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include +#include #include #include @@ -46,6 +48,64 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include +STATIC +INT32 +CheckPrelinked ( + IN OUT UINT8 *Kernel, + IN UINT32 KernelSize + ) +{ + OC_MACHO_CONTEXT Context; + MACH_HEADER_64 *Hdr; + MACH_SEGMENT_COMMAND_64 *InfoSegment; + MACH_SECTION_64 *InfoSection; + XML_DOCUMENT *InfoDocument; + CHAR8 *NewInfo; + UINT32 NewInfoSize; + + if (!MachoInitializeContext (&Context, Kernel, KernelSize)) { + return -1; + } + + Hdr = MachoGetMachHeader64 (&Context); + if (Hdr == NULL) { + return -2; + } + + InfoSegment = MachoGetSegmentByName64(&Context, "__PRELINK_INFO"); + if (InfoSegment == NULL) { + return -3; + } + + InfoSection = MachoGetSectionByName64 (&Context, InfoSegment, "__info"); + if (InfoSection == NULL) { + return -4; + } + + InfoDocument = XmlDocumentParse ((CHAR8 *) &Kernel[InfoSection->Offset], InfoSection->Size); + if (InfoDocument == NULL) { + return -5; + } + + NewInfo = XmlDocumentExport(InfoDocument, &NewInfoSize); + + XmlDocumentFree (InfoDocument); + + if (NewInfo == NULL) { + return -6; + } + + if (InfoSection->Size < NewInfoSize) { + FreePool (NewInfo); + return -7; + } + + CopyMem (&Kernel[InfoSection->Offset], NewInfo, NewInfoSize); + ZeroMem (&Kernel[InfoSection->Offset + NewInfoSize], InfoSection->Size - NewInfoSize); + + return 0; +} + STATIC EFI_STATUS EFIAPI @@ -63,6 +123,7 @@ TestFileOpen ( UINT32 AllocatedSize; CHAR16 *FileNameCopy; EFI_FILE_PROTOCOL *VirtualFileHandle; + INT32 PrelinkedCode; Status = This->Open (This, NewHandle, FileName, OpenMode, Attributes); @@ -94,6 +155,10 @@ TestFileOpen ( // TODO: patches, dropping, and injection here. // + PrelinkedCode = CheckPrelinked (Kernel, KernelSize); + + DEBUG ((DEBUG_WARN, "Prelinked code is %u\n", PrelinkedCode)); + ApplyPatch ( (UINT8 *) "Darwin Kernel Version", NULL, diff --git a/Tests/KernelTest/KernelTestApp.inf b/Tests/KernelTest/KernelTestApp.inf index 96ef0d0f..181131e6 100644 --- a/Tests/KernelTest/KernelTestApp.inf +++ b/Tests/KernelTest/KernelTestApp.inf @@ -52,7 +52,6 @@ UefiLib PcdLib IoLib - # PrintLib OcGuardLib DevicePathLib OcDevicePropertyLib @@ -64,3 +63,5 @@ OcAppleKernelLib OcCompressionLib OcVirtualFsLib + OcXmlLib +