19#include <Library/BaseLib.h>
20#include <Library/BaseMemoryLib.h>
21#include <Library/DebugLib.h>
22#include <Library/MemoryAllocationLib.h>
40 CONST CHAR8 *KextPlistKey;
54 for (KextIndex = 1; KextIndex <= KextCount; ++KextIndex) {
56 if (LastKext == NULL) {
64 for (FieldIndex = 0; FieldIndex < FieldCount; ++FieldIndex) {
66 if (KextPlistKey == NULL) {
71 if (!
PlistIntegerValue (KextPlistValue, &LoadAddress,
sizeof (LoadAddress), TRUE)) {
80 if ((LoadSize != 0) && (LoadAddress != 0)) {
85 if (BaseOverflowAddU64 (LoadAddress, LoadSize, &LoadAddress)) {
89 if (LoadAddress != 0) {
109 if (*PrelinkedInfoSegment == NULL) {
110 return EFI_NOT_FOUND;
113 if ((MachoContext->Is32Bit ? (*PrelinkedInfoSegment)->Segment32.FileOffset : (*PrelinkedInfoSegment)->Segment64.FileOffset) > MAX_UINT32) {
114 return EFI_UNSUPPORTED;
119 *PrelinkedInfoSegment,
122 if (*PrelinkedInfoSection == NULL) {
123 return EFI_NOT_FOUND;
126 if ((MachoContext->Is32Bit ? (*PrelinkedInfoSection)->Section32.Size : (*PrelinkedInfoSection)->Section64.Size) > MAX_UINT32) {
127 return EFI_UNSUPPORTED;
138 IN UINT32 BufferSize,
139 OUT BOOLEAN *KernelCollection OPTIONAL
144 BOOLEAN IsKernelCollection;
150 IsKernelCollection = (Context->Is32Bit ?
153 if (KernelCollection != NULL) {
154 *KernelCollection = IsKernelCollection;
160 if (IsKernelCollection) {
168 "OCAK: KC symtab failed locating inner %Lx %Lx (%d)\n",
173 return EFI_INVALID_PARAMETER;
186 "OCAK: KC symtab failed initialising inner %Lx %x\n",
190 return EFI_INVALID_PARAMETER;
196 "OCAK: KC symtab failed getting symtab from inner %Lx %x\n",
200 return EFI_INVALID_PARAMETER;
210 IN OUT UINT8 *Prelinked,
211 IN UINT32 PrelinkedSize,
212 IN UINT32 PrelinkedAllocSize,
219 CONST CHAR8 *PrelinkedInfoRootKey;
220 UINT64 SegmentEndOffset;
221 UINT32 PrelinkedInfoRootIndex;
222 UINT32 PrelinkedInfoRootCount;
226 ASSERT (Prelinked != NULL);
227 ASSERT (PrelinkedSize > 0);
228 ASSERT (PrelinkedAllocSize >= PrelinkedSize);
230 ZeroMem (Context,
sizeof (*Context));
232 Context->Prelinked = Prelinked;
233 Context->PrelinkedSize =
MACHO_ALIGN (PrelinkedSize);
234 Context->PrelinkedAllocSize = PrelinkedAllocSize;
235 Context->Is32Bit = Is32Bit;
240 if (Context->PrelinkedSize != PrelinkedSize) {
241 if (Context->PrelinkedSize > PrelinkedAllocSize) {
242 return EFI_BUFFER_TOO_SMALL;
245 ZeroMem (&Prelinked[PrelinkedSize], Context->PrelinkedSize - PrelinkedSize);
251 if (!
MachoInitializeContext (&Context->PrelinkedMachContext, Prelinked, PrelinkedSize, 0, PrelinkedSize, Context->Is32Bit)) {
252 return EFI_INVALID_PARAMETER;
256 &Context->PrelinkedMachContext,
257 &Context->InnerMachContext,
259 Context->PrelinkedSize,
260 &Context->IsKernelCollection
262 if (EFI_ERROR (Status)) {
269 InitializeListHead (&Context->PrelinkedKexts);
270 InitializeListHead (&Context->InjectedKexts);
272 if (PrelinkedKext == NULL) {
273 return EFI_INVALID_PARAMETER;
279 if (Context->PrelinkedLastAddress == 0) {
280 return EFI_INVALID_PARAMETER;
287 &Context->PrelinkedMachContext,
288 &Context->PrelinkedInfoSegment,
289 &Context->PrelinkedInfoSection
291 if (EFI_ERROR (Status)) {
295 if (Context->IsKernelCollection) {
300 &Context->InnerMachContext,
304 if (EFI_ERROR (Status)) {
309 &Context->PrelinkedMachContext,
312 if (Context->RegionSegment == NULL) {
313 return EFI_NOT_FOUND;
317 &Context->PrelinkedMachContext,
320 if (Context->LinkEditSegment == NULL) {
321 return EFI_NOT_FOUND;
325 &Context->PrelinkedMachContext,
328 if (Context->PrelinkedTextSegment == NULL) {
329 return EFI_NOT_FOUND;
333 &Context->PrelinkedMachContext,
334 Context->PrelinkedTextSegment,
337 if (Context->PrelinkedTextSection == NULL) {
338 return EFI_NOT_FOUND;
342 Context->PrelinkedInfo = Context->Is32Bit ?
344 Context->PrelinkedInfoSection->Section32.Size,
345 &Context->Prelinked[Context->PrelinkedInfoSection->Section32.Offset]
348 (UINTN)Context->PrelinkedInfoSection->Section64.Size,
349 &Context->Prelinked[Context->PrelinkedInfoSection->Section64.Offset]
351 if (Context->PrelinkedInfo == NULL) {
353 return EFI_OUT_OF_RESOURCES;
357 Context->PrelinkedInfo,
358 (UINT32)(Context->Is32Bit ?
359 Context->PrelinkedInfoSection->Section32.Size : Context->PrelinkedInfoSection->Section64.Size),
362 if (Context->PrelinkedInfoDocument == NULL) {
364 return EFI_INVALID_PARAMETER;
371 if (Context->IsKernelCollection) {
378 if (PrelinkedInfoRoot == NULL) {
380 return EFI_INVALID_PARAMETER;
383 if (Context->IsKernelCollection) {
387 SegmentEndOffset = Context->LinkEditSegment->Segment64.FileOffset + Context->LinkEditSegment->Segment64.FileSize;
389 if (
MACHO_ALIGN (SegmentEndOffset) != Context->PrelinkedSize) {
391 return EFI_INVALID_PARAMETER;
394 Context->PrelinkedLastLoadAddress = Context->LinkEditSegment->Segment64.VirtualAddress + Context->LinkEditSegment->Segment64.Size;
401 for (PrelinkedInfoRootIndex = 0; PrelinkedInfoRootIndex < PrelinkedInfoRootCount; ++PrelinkedInfoRootIndex) {
403 if (PrelinkedInfoRootKey == NULL) {
409 if (Context->PrelinkedLastLoadAddress == 0) {
413 if (Context->PrelinkedLastLoadAddress != 0) {
423 return EFI_INVALID_PARAMETER;
435 if (Context->PrelinkedInfoDocument != NULL) {
437 Context->PrelinkedInfoDocument = NULL;
440 if (Context->KextScratchBuffer != NULL) {
441 FreePool (Context->KextScratchBuffer);
442 Context->KextScratchBuffer = NULL;
445 if (Context->PrelinkedInfo != NULL) {
446 FreePool (Context->PrelinkedInfo);
447 Context->PrelinkedInfo = NULL;
450 if (Context->PooledBuffers != NULL) {
451 for (Index = 0; Index < Context->PooledBuffersCount; ++Index) {
452 FreePool (Context->PooledBuffers[Index]);
455 FreePool (Context->PooledBuffers);
456 Context->PooledBuffers = NULL;
459 if (Context->LinkBuffer != NULL) {
460 ZeroMem (Context->LinkBuffer, Context->LinkBufferSize);
461 FreePool (Context->LinkBuffer);
462 Context->LinkBuffer = NULL;
465 if (Context->PrelinkedStateKernel != NULL) {
466 FreePool (Context->PrelinkedStateKernel);
467 Context->PrelinkedStateKernel = NULL;
470 if (Context->PrelinkedStateKexts != NULL) {
471 FreePool (Context->PrelinkedStateKexts);
472 Context->PrelinkedStateKexts = NULL;
475 while (!IsListEmpty (&Context->PrelinkedKexts)) {
476 Link = GetFirstNode (&Context->PrelinkedKexts);
478 RemoveEntryList (Link);
482 ZeroMem (&Context->PrelinkedKexts, sizeof (Context->PrelinkedKexts));
487 ZeroMem (&Context->InjectedKexts, sizeof (Context->InjectedKexts));
496 VOID **NewPooledBuffers;
498 if (Context->PooledBuffersCount == Context->PooledBuffersAllocCount) {
499 NewPooledBuffers = AllocatePool (
500 2 * (Context->PooledBuffersAllocCount + 1) * sizeof (NewPooledBuffers[0])
502 if (NewPooledBuffers == NULL) {
503 return EFI_OUT_OF_RESOURCES;
506 if (Context->PooledBuffers != NULL) {
508 &NewPooledBuffers[0],
509 &Context->PooledBuffers[0],
510 Context->PooledBuffersCount * sizeof (NewPooledBuffers[0])
512 FreePool (Context->PooledBuffers);
515 Context->PooledBuffers = NewPooledBuffers;
516 Context->PooledBuffersAllocCount = 2 * (Context->PooledBuffersAllocCount + 1);
519 Context->PooledBuffers[Context->PooledBuffersCount] =
Buffer;
520 Context->PooledBuffersCount++;
528 IN UINT32 LinkedExpansion,
529 IN UINT32 ReservedExeSize
533 UINT64 SegmentEndOffset;
534 UINT32 AlignedExpansion;
536 if (Context->IsKernelCollection) {
542 if ( (AlignedExpansion < LinkedExpansion)
543 || (Context->PrelinkedAllocSize < AlignedExpansion)
544 || (Context->PrelinkedAllocSize - AlignedExpansion < Context->PrelinkedSize))
546 return EFI_OUT_OF_RESOURCES;
549 ASSERT (Context->PrelinkedLastAddress == Context->PrelinkedLastLoadAddress);
556 "KextsFixupChains may be unaligned"
559 Context->KextsFixupChains = (VOID *)(Context->Prelinked + Context->PrelinkedSize);
565 Context->Prelinked + Context->PrelinkedSize,
569 Context->PrelinkedSize += AlignedExpansion;
570 Context->PrelinkedLastAddress += AlignedExpansion;
571 Context->PrelinkedLastLoadAddress += AlignedExpansion;
572 Context->LinkEditSegment->Segment64.Size += AlignedExpansion;
573 Context->LinkEditSegment->Segment64.FileSize += AlignedExpansion;
614 SegmentEndOffset = Context->Is32Bit ?
615 Context->PrelinkedInfoSegment->Segment32.FileOffset + Context->PrelinkedInfoSegment->Segment32.FileSize :
616 Context->PrelinkedInfoSegment->Segment64.FileOffset + Context->PrelinkedInfoSegment->Segment64.FileSize;
618 if (
MACHO_ALIGN (SegmentEndOffset) == Context->PrelinkedSize) {
621 "OCAK: Reducing %a-bit prelink size from %X to %X via plist\n",
622 Context->Is32Bit ?
"32" :
"64",
623 Context->PrelinkedSize,
626 Context->PrelinkedInfoSegment->Segment32.FileOffset : Context->PrelinkedInfoSegment->Segment64.FileOffset
631 Context->PrelinkedInfoSegment->Segment32.FileOffset : Context->PrelinkedInfoSegment->Segment64.FileOffset
636 "OCAK: Leaving unchanged %a-bit prelink size %X due to %LX plist\n",
637 Context->Is32Bit ?
"32" :
"64",
638 Context->PrelinkedSize,
643 if (Context->PrelinkedStateSegment != NULL) {
644 SegmentEndOffset = Context->Is32Bit ?
645 Context->PrelinkedStateSegment->Segment32.FileOffset + Context->PrelinkedStateSegment->Segment32.FileSize :
646 Context->PrelinkedStateSegment->Segment64.FileOffset + Context->PrelinkedStateSegment->Segment64.FileSize;
648 if (
MACHO_ALIGN (SegmentEndOffset) == Context->PrelinkedSize) {
651 "OCAK: Reducing %a-bit prelink size from %X to %X via state\n",
652 Context->Is32Bit ?
"32" :
"64",
653 Context->PrelinkedSize,
656 Context->PrelinkedStateSegment->Segment32.FileOffset : Context->PrelinkedStateSegment->Segment64.FileOffset
661 Context->PrelinkedStateSegment->Segment32.FileOffset : Context->PrelinkedStateSegment->Segment64.FileOffset
668 if (Context->Is32Bit) {
669 Context->PrelinkedStateSegment->Segment32.VirtualAddress = 0;
670 Context->PrelinkedStateSegment->Segment32.Size = 0;
671 Context->PrelinkedStateSegment->Segment32.FileOffset = 0;
672 Context->PrelinkedStateSegment->Segment32.FileSize = 0;
673 Context->PrelinkedStateSectionKernel->Section32.Address = 0;
674 Context->PrelinkedStateSectionKernel->Section32.Size = 0;
675 Context->PrelinkedStateSectionKernel->Section32.Offset = 0;
676 Context->PrelinkedStateSectionKexts->Section32.Address = 0;
677 Context->PrelinkedStateSectionKexts->Section32.Size = 0;
678 Context->PrelinkedStateSectionKexts->Section32.Offset = 0;
680 Context->PrelinkedStateSegment->Segment64.VirtualAddress = 0;
681 Context->PrelinkedStateSegment->Segment64.Size = 0;
682 Context->PrelinkedStateSegment->Segment64.FileOffset = 0;
683 Context->PrelinkedStateSegment->Segment64.FileSize = 0;
684 Context->PrelinkedStateSectionKernel->Section64.Address = 0;
685 Context->PrelinkedStateSectionKernel->Section64.Size = 0;
686 Context->PrelinkedStateSectionKernel->Section64.Offset = 0;
687 Context->PrelinkedStateSectionKexts->Section64.Address = 0;
688 Context->PrelinkedStateSectionKexts->Section64.Size = 0;
689 Context->PrelinkedStateSectionKexts->Section64.Offset = 0;
694 "OCAK: Leaving unchanged %a-bit prelink size %X due to %LX state\n",
695 Context->Is32Bit ?
"32" :
"64",
696 Context->PrelinkedSize,
702 if (Context->Is32Bit) {
703 Context->PrelinkedInfoSegment->Segment32.VirtualAddress = 0;
704 Context->PrelinkedInfoSegment->Segment32.Size = 0;
705 Context->PrelinkedInfoSegment->Segment32.FileOffset = 0;
706 Context->PrelinkedInfoSegment->Segment32.FileSize = 0;
707 Context->PrelinkedInfoSection->Section32.Address = 0;
708 Context->PrelinkedInfoSection->Section32.Size = 0;
709 Context->PrelinkedInfoSection->Section32.Offset = 0;
711 Context->PrelinkedInfoSegment->Segment64.VirtualAddress = 0;
712 Context->PrelinkedInfoSegment->Segment64.Size = 0;
713 Context->PrelinkedInfoSegment->Segment64.FileOffset = 0;
714 Context->PrelinkedInfoSegment->Segment64.FileSize = 0;
715 Context->PrelinkedInfoSection->Section64.Address = 0;
716 Context->PrelinkedInfoSection->Section64.Size = 0;
717 Context->PrelinkedInfoSection->Section64.Offset = 0;
721 if (Context->PrelinkedLastAddress == 0) {
722 return EFI_INVALID_PARAMETER;
728 SegmentEndOffset = Context->Is32Bit ?
729 Context->PrelinkedTextSegment->Segment32.FileOffset + Context->PrelinkedTextSegment->Segment32.FileSize :
730 Context->PrelinkedTextSegment->Segment64.FileOffset + Context->PrelinkedTextSegment->Segment64.FileSize;
732 if (
MACHO_ALIGN (SegmentEndOffset) != Context->PrelinkedSize) {
737 return EFI_UNSUPPORTED;
744 Context->KextsFileOffset = Context->PrelinkedSize;
745 Context->KextsVmAddress = Context->PrelinkedLastAddress;
747 if (Context->IsKernelCollection) {
749 if (EFI_ERROR (Status)) {
764 UINT32 ExportedInfoSize;
769 if (Context->IsKernelCollection) {
774 KextsSize = Context->PrelinkedSize - Context->KextsFileOffset;
778 ASSERT (ChainSize <= Context->KextsFixupChains->Size);
780 Context->KextsFixupChains->Size = ChainSize;
781 Context->KextsFixupChains->PageCount = (UINT16)(KextsSize /
MACHO_PAGE_SIZE);
784 if (EFI_ERROR (Status)) {
787 }
else if ((Context->PrelinkedStateSegment != NULL) && ((Context->Is32Bit ?
788 Context->PrelinkedStateSegment->Segment32.VirtualAddress : Context->PrelinkedStateSegment->Segment64.VirtualAddress)
792 if (EFI_ERROR (Status)) {
797 ExportedInfo =
XmlDocumentExport (Context->PrelinkedInfoDocument, &ExportedInfoSize, 0, FALSE);
798 if (ExportedInfo == NULL) {
799 return EFI_OUT_OF_RESOURCES;
807 if ( BaseOverflowAddU32 (Context->PrelinkedSize,
MACHO_ALIGN (ExportedInfoSize), &NewSize)
808 || (NewSize > Context->PrelinkedAllocSize))
810 FreePool (ExportedInfo);
811 return EFI_BUFFER_TOO_SMALL;
819 if (Context->IsKernelCollection && (
MACHO_ALIGN (ExportedInfoSize) <= Context->PrelinkedInfoSegment->Size)) {
821 &Context->Prelinked[Context->PrelinkedInfoSegment->FileOffset],
827 &Context->Prelinked[Context->PrelinkedInfoSegment->FileOffset + ExportedInfoSize],
828 Context->PrelinkedInfoSegment->FileSize - ExportedInfoSize
831 FreePool (ExportedInfo);
838 if (Context->Is32Bit) {
839 Context->PrelinkedInfoSegment->Segment32.VirtualAddress = (UINT32)Context->PrelinkedLastAddress;
840 Context->PrelinkedInfoSegment->Segment32.Size =
MACHO_ALIGN (ExportedInfoSize);
841 Context->PrelinkedInfoSegment->Segment32.FileOffset = Context->PrelinkedSize;
842 Context->PrelinkedInfoSegment->Segment32.FileSize =
MACHO_ALIGN (ExportedInfoSize);
843 Context->PrelinkedInfoSection->Section32.Address = (UINT32)Context->PrelinkedLastAddress;
844 Context->PrelinkedInfoSection->Section32.Size =
MACHO_ALIGN (ExportedInfoSize);
845 Context->PrelinkedInfoSection->Section32.Offset = Context->PrelinkedSize;
847 Context->PrelinkedInfoSegment->Segment64.VirtualAddress = Context->PrelinkedLastAddress;
848 Context->PrelinkedInfoSegment->Segment64.Size =
MACHO_ALIGN (ExportedInfoSize);
849 Context->PrelinkedInfoSegment->Segment64.FileOffset = Context->PrelinkedSize;
850 Context->PrelinkedInfoSegment->Segment64.FileSize =
MACHO_ALIGN (ExportedInfoSize);
851 Context->PrelinkedInfoSection->Section64.Address = Context->PrelinkedLastAddress;
852 Context->PrelinkedInfoSection->Section64.Size =
MACHO_ALIGN (ExportedInfoSize);
853 Context->PrelinkedInfoSection->Section64.Offset = Context->PrelinkedSize;
856 if (Context->IsKernelCollection) {
861 Context->InnerInfoSegment->VirtualAddress = Context->PrelinkedLastAddress;
862 Context->InnerInfoSegment->Size =
MACHO_ALIGN (ExportedInfoSize);
863 Context->InnerInfoSegment->FileOffset = Context->PrelinkedSize;
864 Context->InnerInfoSegment->FileSize =
MACHO_ALIGN (ExportedInfoSize);
865 Context->InnerInfoSection->Address = Context->PrelinkedLastAddress;
866 Context->InnerInfoSection->Size =
MACHO_ALIGN (ExportedInfoSize);
867 Context->InnerInfoSection->Offset = Context->PrelinkedSize;
871 &Context->Prelinked[Context->PrelinkedSize],
877 &Context->Prelinked[Context->PrelinkedSize + ExportedInfoSize],
881 Context->PrelinkedLastAddress +=
MACHO_ALIGN (ExportedInfoSize);
882 Context->PrelinkedSize +=
MACHO_ALIGN (ExportedInfoSize);
884 Context->PrelinkedMachContext.FileSize = Context->PrelinkedSize;
886 if (Context->IsKernelCollection) {
893 &Context->PrelinkedMachContext,
898 FreePool (ExportedInfo);
905 IN OUT UINT32 *ReservedInfoSize,
906 IN OUT UINT32 *ReservedExeSize,
907 IN UINT32 InfoPlistSize,
908 IN UINT8 *Executable OPTIONAL,
909 IN UINT32 ExecutableSize OPTIONAL,
919 return EFI_INVALID_PARAMETER;
924 if (Executable != NULL) {
925 ASSERT (ExecutableSize > 0);
927 return EFI_INVALID_PARAMETER;
931 if (ExecutableSize == 0) {
932 return EFI_INVALID_PARAMETER;
936 if ( BaseOverflowAddU32 (*ReservedInfoSize, InfoPlistSize, &InfoPlistSize)
937 || BaseOverflowAddU32 (*ReservedExeSize, ExecutableSize, &ExecutableSize))
939 return EFI_INVALID_PARAMETER;
942 *ReservedInfoSize = InfoPlistSize;
943 *ReservedExeSize = ExecutableSize;
951 IN CONST CHAR8 *Identifier OPTIONAL,
952 IN CONST CHAR8 *BundlePath,
953 IN CONST CHAR8 *InfoPlist,
954 IN UINT32 InfoPlistSize,
955 IN CONST CHAR8 *ExecutablePath OPTIONAL,
956 IN CONST UINT8 *Executable OPTIONAL,
957 IN UINT32 ExecutableSize OPTIONAL,
970 CONST CHAR8 *TmpKeyValue;
973 UINT32 NewInfoPlistSize;
974 UINT32 NewPrelinkedSize;
975 UINT32 AlignedExecutableSize;
979 CHAR8 ExecutableSourceAddrStr[24];
980 CHAR8 ExecutableSizeStr[24];
981 CHAR8 ExecutableLoadAddrStr[24];
982 CHAR8 KmodInfoStr[24];
985 UINT64 LoadAddressOffset;
986 CONST CHAR8 *BundleVerStr;
988 PrelinkedKext = NULL;
991 ASSERT (BundlePath != NULL);
992 ASSERT (InfoPlist != NULL);
993 ASSERT (InfoPlistSize > 0);
996 AlignedExecutableSize = 0;
1002 if (Identifier != NULL) {
1004 DEBUG ((DEBUG_INFO,
"OCAK: Bundle %a is already present in prelinked\n", Identifier));
1005 return EFI_ALREADY_STARTED;
1012 if (Executable != NULL) {
1013 ASSERT (ExecutableSize > 0);
1015 DEBUG ((DEBUG_INFO,
"OCAK: Injected kext %a/%a is not a supported executable\n", BundlePath, ExecutablePath));
1016 return EFI_INVALID_PARAMETER;
1022 KextOffset = Context->PrelinkedSize;
1026 &Context->Prelinked[KextOffset],
1027 Context->PrelinkedAllocSize - KextOffset,
1032 AlignedExecutableSize =
MACHO_ALIGN (ExecutableSize);
1034 if ( BaseOverflowAddU32 (KextOffset, AlignedExecutableSize, &NewPrelinkedSize)
1035 || (NewPrelinkedSize > Context->PrelinkedAllocSize)
1036 || (ExecutableSize == 0))
1038 return EFI_BUFFER_TOO_SMALL;
1042 &Context->Prelinked[KextOffset + ExecutableSize],
1043 AlignedExecutableSize - ExecutableSize
1047 || BaseOverflowAddU64 (Context->PrelinkedLastLoadAddress, FileOffset, &LoadAddressOffset))
1049 return EFI_INVALID_PARAMETER;
1052 Result =
KextFindKmodAddress (&ExecutableContext, LoadAddressOffset, ExecutableSize, &KmodAddress);
1054 return EFI_INVALID_PARAMETER;
1061 TmpInfoPlist = AllocateCopyPool (InfoPlistSize, InfoPlist);
1062 if (TmpInfoPlist == NULL) {
1063 return EFI_OUT_OF_RESOURCES;
1067 if (InfoPlistDocument == NULL) {
1068 FreePool (TmpInfoPlist);
1069 return EFI_INVALID_PARAMETER;
1073 if (InfoPlistRoot == NULL) {
1075 FreePool (TmpInfoPlist);
1076 return EFI_INVALID_PARAMETER;
1084 DEBUG_CODE_BEGIN ();
1087 if (BundleVersion != NULL) {
1088 for (FieldIndex = 0; FieldIndex < FieldCount; ++FieldIndex) {
1090 if (TmpKeyValue == NULL) {
1109 if (Executable == NULL) {
1110 for (FieldIndex = 0; FieldIndex < FieldCount; ++FieldIndex) {
1112 if (TmpKeyValue == NULL) {
1128 Failed |=
XmlNodeAppend (InfoPlistRoot,
"string", NULL, BundlePath) == NULL;
1129 if (Executable != NULL) {
1131 Failed |=
XmlNodeAppend (InfoPlistRoot,
"string", NULL, ExecutablePath) == NULL;
1132 Failed |= !
AsciiUint64ToLowerHex (ExecutableSourceAddrStr,
sizeof (ExecutableSourceAddrStr), Context->PrelinkedLastAddress);
1135 Failed |= !
AsciiUint64ToLowerHex (ExecutableLoadAddrStr,
sizeof (ExecutableLoadAddrStr), Context->PrelinkedLastLoadAddress);
1138 Failed |= !
AsciiUint64ToLowerHex (ExecutableSizeStr,
sizeof (ExecutableSizeStr), AlignedExecutableSize);
1148 FreePool (TmpInfoPlist);
1149 return EFI_OUT_OF_RESOURCES;
1152 if (Executable != NULL) {
1157 Context->PrelinkedLastLoadAddress,
1162 if (PrelinkedKext == NULL) {
1164 FreePool (TmpInfoPlist);
1165 return EFI_INVALID_PARAMETER;
1172 Context->PrelinkedSize += AlignedExecutableSize;
1173 Context->PrelinkedLastAddress += AlignedExecutableSize;
1174 Context->PrelinkedLastLoadAddress += AlignedExecutableSize;
1176 if (Context->IsKernelCollection) {
1185 if (EFI_ERROR (Status)) {
1188 "Failed to rebase injected kext %a/%a by %u\n",
1199 if (Context->Is32Bit) {
1200 Context->PrelinkedTextSegment->Segment32.Size += AlignedExecutableSize;
1201 Context->PrelinkedTextSegment->Segment32.FileSize += AlignedExecutableSize;
1202 Context->PrelinkedTextSection->Section32.Size += AlignedExecutableSize;
1204 Context->PrelinkedTextSegment->Segment64.Size += AlignedExecutableSize;
1205 Context->PrelinkedTextSegment->Segment64.FileSize += AlignedExecutableSize;
1206 Context->PrelinkedTextSection->Section64.Size += AlignedExecutableSize;
1214 NewInfoPlist =
XmlDocumentExport (InfoPlistDocument, &NewInfoPlistSize, 2, FALSE);
1217 FreePool (TmpInfoPlist);
1219 if (NewInfoPlist == NULL) {
1220 if (PrelinkedKext != NULL) {
1224 return EFI_OUT_OF_RESOURCES;
1228 if (EFI_ERROR (Status)) {
1229 FreePool (NewInfoPlist);
1230 if (PrelinkedKext != NULL) {
1237 if (
XmlNodeAppend (Context->KextList,
"dict", NULL, NewInfoPlist) == NULL) {
1238 if (PrelinkedKext != NULL) {
1242 return EFI_OUT_OF_RESOURCES;
1248 if (PrelinkedKext != NULL) {
1249 InsertTailList (&Context->PrelinkedKexts, &PrelinkedKext->
Link);
1254 InsertTailList (&Context->InjectedKexts, &PrelinkedKext->
InjectedLink);
1263 IN CONST CHAR8 *Identifier,
1270 ASSERT (Context != NULL);
1271 ASSERT (Identifier != NULL);
1275 if (EFI_ERROR (Status)) {
1276 DEBUG ((DEBUG_INFO,
"OCAK: Failed to pk find %a - %r\n", Identifier, Status));
1294 ASSERT (Context != NULL);
1300 if (!EFI_ERROR (Status)) {
1307 DEBUG ((DEBUG_INFO,
"OCAK: Failed to pk find %a - %r\n", KernelQuirk->
Identifier, Status));
1314 IN CONST CHAR8 *Identifier,
1321 ASSERT (Context != NULL);
1322 ASSERT (Identifier != NULL);
1325 if (EFI_ERROR (Status)) {
1326 DEBUG ((DEBUG_INFO,
"OCAK: Failed to pk find %a - %r\n", Identifier, Status));
VENDOR_DEVICE_PATH Header
@ MachHeaderFileTypeFileSet
KERNEL_QUIRK gKernelQuirks[]
STATIC UINT32 KernelVersion
EFI_STATUS InternalKxldStateRebuild(IN OUT PRELINKED_CONTEXT *Context)
#define PRELINK_INFO_SECTION
EFI_STATUS KcRebuildMachHeader(IN OUT PRELINKED_CONTEXT *Context)
EFI_STATUS PatcherBlockKext(IN OUT PATCHER_CONTEXT *Context)
#define PRELINK_INFO_DICTIONARY_KEY
EFI_STATUS PatcherExcludePrelinkedKext(IN CONST CHAR8 *Identifier, IN OUT PATCHER_CONTEXT *PatcherContext, IN OUT PRELINKED_CONTEXT *PrelinkedContext)
#define PRELINK_INFO_EXECUTABLE_LOAD_ADDR_KEY
#define PRELINK_TEXT_SECTION
#define PRELINK_INFO_BUNDLE_PATH_KEY
#define MAX_INFO_BUNDLE_VERSION_KEY_SIZE
#define PRELINK_INFO_INTEGER_ATTRIBUTES
VOID KcKextIndexFixups(IN OUT PRELINKED_CONTEXT *Context, IN OC_MACHO_CONTEXT *MachContext)
#define PRELINK_INFO_EXECUTABLE_RELATIVE_PATH_KEY
#define INFO_BUNDLE_EXECUTABLE_KEY
#define KC_LINKEDIT_SEGMENT
#define PRELINK_INFO_KMOD_INFO_KEY
#define PRELINK_INFO_EXECUTABLE_SOURCE_ADDR_KEY
EFI_STATUS PatcherApplyGenericPatch(IN OUT PATCHER_CONTEXT *Context, IN PATCHER_GENERIC_PATCH *Patch)
#define PLIST_EXPANSION_SIZE
EFI_STATUS KcInitKextFixupChains(IN OUT PRELINKED_CONTEXT *Context, IN UINT32 SegChainSize, IN UINT32 ReservedSize)
#define PRELINK_INFO_EXECUTABLE_SIZE_KEY
#define INFO_BUNDLE_VERSION_KEY
#define PRELINK_TEXT_SEGMENT
#define PRELINK_INFO_SEGMENT
EFI_STATUS KcKextApplyFileDelta(IN PRELINKED_CONTEXT *PrelinkedContext, IN OUT OC_MACHO_CONTEXT *Context, IN UINT32 Delta)
EFI_STATUS PatcherInitContextFromPrelinked(IN OUT PATCHER_CONTEXT *Context, IN OUT PRELINKED_CONTEXT *Prelinked, IN CONST CHAR8 *Name)
#define KC_REGION0_SEGMENT
BOOLEAN KextFindKmodAddress(IN OC_MACHO_CONTEXT *ExecutableContext, IN UINT64 LoadAddress, IN UINT32 Size, OUT UINT64 *Kmod)
UINT32 KcGetSegmentFixupChainsSize(IN UINT32 SegmentSize)
STATIC_ASSERT(BYTES_PER_PIXEL==sizeof(UINT32), "Non 4-byte pixels are unsupported!")
BOOLEAN MachoInitializeContext64(OUT OC_MACHO_CONTEXT *Context, IN VOID *FileData, IN UINT32 FileSize, IN UINT32 HeaderOffset, IN UINT32 InnerSize)
MACH_SECTION_ANY * MachoGetSectionByName(IN OUT OC_MACHO_CONTEXT *Context, IN MACH_SEGMENT_COMMAND_ANY *Segment, IN CONST CHAR8 *SectionName)
UINT64 MachoGetLastAddress(IN OUT OC_MACHO_CONTEXT *Context)
BOOLEAN MachoInitialiseSymtabsExternal(IN OUT OC_MACHO_CONTEXT *Context, IN OC_MACHO_CONTEXT *SymsContext)
UINT32 MachoExpandImage(IN OC_MACHO_CONTEXT *Context, OUT UINT8 *Destination, IN UINT32 DestinationSize, IN BOOLEAN Strip, OUT UINT64 *FileOffset OPTIONAL)
MACH_HEADER_ANY * MachoGetMachHeader(IN OUT OC_MACHO_CONTEXT *Context)
BOOLEAN MachoInitializeContext(OUT OC_MACHO_CONTEXT *Context, IN VOID *FileData, IN UINT32 FileSize, IN UINT32 HeaderOffset, IN UINT32 InnerSize, IN BOOLEAN Is32Bit)
UINT32 MachoGetExpandedImageSize(IN OC_MACHO_CONTEXT *Context)
MACH_SEGMENT_COMMAND_64 * MachoGetSegmentByName64(IN OUT OC_MACHO_CONTEXT *Context, IN CONST CHAR8 *SegmentName)
MACH_SEGMENT_COMMAND_ANY * MachoGetSegmentByName(IN OUT OC_MACHO_CONTEXT *Context, IN CONST CHAR8 *SegmentName)
BOOLEAN AsciiUint64ToLowerHex(OUT CHAR8 *Buffer, IN UINT32 BufferSize, IN UINT64 Value)
OC_TYPING_BUFFER_ENTRY Buffer[OC_TYPING_BUFFER_SIZE]
XML_NODE * XmlDocumentRoot(IN CONST XML_DOCUMENT *Document)
XML_DOCUMENT * XmlDocumentParse(IN OUT CHAR8 *Buffer, IN UINT32 Length, IN BOOLEAN WithRefs)
CONST CHAR8 * PlistKeyValue(IN XML_NODE *Node OPTIONAL)
UINT32 XmlNodeChildren(IN CONST XML_NODE *Node)
CHAR8 * XmlDocumentExport(IN CONST XML_DOCUMENT *Document, OUT UINT32 *Length OPTIONAL, IN UINT32 Skip, IN BOOLEAN PrependPlistInfo)
CONST CHAR8 * XmlNodeContent(IN CONST XML_NODE *Node)
UINT32 PlistDictChildren(IN CONST XML_NODE *Node)
BOOLEAN PlistIntegerValue(IN XML_NODE *Node OPTIONAL, OUT VOID *Value, IN UINT32 Size, IN BOOLEAN Hex)
XML_NODE * PlistNodeCast(IN XML_NODE *Node OPTIONAL, IN PLIST_NODE_TYPE Type)
XML_NODE * XmlNodeAppend(IN OUT XML_NODE *Node, IN CONST CHAR8 *Name, IN CONST CHAR8 *Attributes OPTIONAL, IN CONST CHAR8 *Content OPTIONAL)
XML_NODE * PlistDocumentRoot(IN CONST XML_DOCUMENT *Document)
XML_NODE * XmlNodeChild(IN CONST XML_NODE *Node, IN UINT32 Child)
VOID XmlDocumentFree(IN OUT XML_DOCUMENT *Document)
XML_NODE * PlistDictChild(IN CONST XML_NODE *Node, IN UINT32 Child, OUT XML_NODE **Value OPTIONAL)
EFI_STATUS PrelinkedContextApplyPatch(IN OUT PRELINKED_CONTEXT *Context, IN CONST CHAR8 *Identifier, IN PATCHER_GENERIC_PATCH *Patch)
VOID PrelinkedContextFree(IN OUT PRELINKED_CONTEXT *Context)
EFI_STATUS PrelinkedDependencyInsert(IN OUT PRELINKED_CONTEXT *Context, IN VOID *Buffer)
STATIC EFI_STATUS PrelinkedGetSegmentsFromMacho(IN OC_MACHO_CONTEXT *MachoContext, OUT MACH_SEGMENT_COMMAND_ANY **PrelinkedInfoSegment, OUT MACH_SECTION_ANY **PrelinkedInfoSection)
EFI_STATUS PrelinkedContextInit(IN OUT PRELINKED_CONTEXT *Context, IN OUT UINT8 *Prelinked, IN UINT32 PrelinkedSize, IN UINT32 PrelinkedAllocSize, IN BOOLEAN Is32Bit)
EFI_STATUS PrelinkedInjectPrepare(IN OUT PRELINKED_CONTEXT *Context, IN UINT32 LinkedExpansion, IN UINT32 ReservedExeSize)
EFI_STATUS PrelinkedContextBlock(IN OUT PRELINKED_CONTEXT *Context, IN CONST CHAR8 *Identifier, IN BOOLEAN Exclude)
EFI_STATUS PrelinkedContextApplyQuirk(IN OUT PRELINKED_CONTEXT *Context, IN KERNEL_QUIRK_NAME Quirk, IN UINT32 KernelVersion)
EFI_STATUS PrelinkedReserveKextSize(IN OUT UINT32 *ReservedInfoSize, IN OUT UINT32 *ReservedExeSize, IN UINT32 InfoPlistSize, IN UINT8 *Executable OPTIONAL, IN UINT32 ExecutableSize OPTIONAL, IN BOOLEAN Is32Bit)
EFI_STATUS InternalConnectExternalSymtab(IN OUT OC_MACHO_CONTEXT *Context, OUT OC_MACHO_CONTEXT *InnerContext, IN UINT8 *Buffer, IN UINT32 BufferSize, OUT BOOLEAN *KernelCollection OPTIONAL)
EFI_STATUS PrelinkedInjectKext(IN OUT PRELINKED_CONTEXT *Context, IN CONST CHAR8 *Identifier OPTIONAL, IN CONST CHAR8 *BundlePath, IN CONST CHAR8 *InfoPlist, IN UINT32 InfoPlistSize, IN CONST CHAR8 *ExecutablePath OPTIONAL, IN CONST UINT8 *Executable OPTIONAL, IN UINT32 ExecutableSize OPTIONAL, OUT CHAR8 BundleVersion[MAX_INFO_BUNDLE_VERSION_KEY_SIZE] OPTIONAL)
EFI_STATUS PrelinkedInjectComplete(IN OUT PRELINKED_CONTEXT *Context)
STATIC UINT64 PrelinkedFindLastLoadAddress(IN XML_NODE *KextList)
VOID InternalFreePrelinkedKext(IN PRELINKED_KEXT *Kext)
PRELINKED_KEXT * InternalCachedPrelinkedKext(IN OUT PRELINKED_CONTEXT *Prelinked, IN CONST CHAR8 *Identifier)
#define GET_PRELINKED_KEXT_FROM_LINK(This)
PRELINKED_KEXT * InternalCachedPrelinkedKernel(IN OUT PRELINKED_CONTEXT *Prelinked)
PRELINKED_KEXT * InternalLinkPrelinkedKext(IN OUT PRELINKED_CONTEXT *Context, IN OUT OC_MACHO_CONTEXT *Executable, IN XML_NODE *PlistRoot, IN UINT64 LoadAddress, IN UINT64 KmodAddress, IN UINT64 FileOffset)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
KERNEL_QUIRK_PATCH_FUNCTION * PatchFunction
UINT64 FileOffset
file offset of this segment
UINT64 VirtualAddress
memory address of this segment
OC_MACHO_CONTEXT MachContext