+/**\r
+ Comparator function for two opaque pointers, ordering on (unsigned) pointer\r
+ value itself.\r
+ Can be used as both Key and UserStruct comparator.\r
+\r
+ @param[in] Pointer1 First pointer.\r
+\r
+ @param[in] Pointer2 Second pointer.\r
+\r
+ @retval <0 If Pointer1 compares less than Pointer2.\r
+\r
+ @retval 0 If Pointer1 compares equal to Pointer2.\r
+\r
+ @retval >0 If Pointer1 compares greater than Pointer2.\r
+**/\r
+STATIC\r
+INTN\r
+EFIAPI\r
+PointerCompare (\r
+ IN CONST VOID *Pointer1,\r
+ IN CONST VOID *Pointer2\r
+ )\r
+{\r
+ if (Pointer1 == Pointer2) {\r
+ return 0;\r
+ }\r
+ if ((UINTN)Pointer1 < (UINTN)Pointer2) {\r
+ return -1;\r
+ }\r
+ return 1;\r
+}\r
+\r
+\r
+/**\r
+ Comparator function for two ASCII strings. Can be used as both Key and\r
+ UserStruct comparator.\r
+\r
+ This function exists solely so we can avoid casting &AsciiStrCmp to\r
+ ORDERED_COLLECTION_USER_COMPARE and ORDERED_COLLECTION_KEY_COMPARE.\r
+\r
+ @param[in] AsciiString1 Pointer to the first ASCII string.\r
+\r
+ @param[in] AsciiString2 Pointer to the second ASCII string.\r
+\r
+ @return The return value of AsciiStrCmp (AsciiString1, AsciiString2).\r
+**/\r
+STATIC\r
+INTN\r
+EFIAPI\r
+AsciiStringCompare (\r
+ IN CONST VOID *AsciiString1,\r
+ IN CONST VOID *AsciiString2\r
+ )\r
+{\r
+ return AsciiStrCmp (AsciiString1, AsciiString2);\r
+}\r
+\r
+\r
+/**\r
+ Release the ORDERED_COLLECTION structure populated by\r
+ CollectAllocationsRestrictedTo32Bit() (below).\r
+\r
+ This function may be called by CollectAllocationsRestrictedTo32Bit() itself,\r
+ on the error path.\r
+\r
+ @param[in] AllocationsRestrictedTo32Bit The ORDERED_COLLECTION structure to\r
+ release.\r
+**/\r
+STATIC\r
+VOID\r
+ReleaseAllocationsRestrictedTo32Bit (\r
+ IN ORDERED_COLLECTION *AllocationsRestrictedTo32Bit\r
+)\r
+{\r
+ ORDERED_COLLECTION_ENTRY *Entry, *Entry2;\r
+\r
+ for (Entry = OrderedCollectionMin (AllocationsRestrictedTo32Bit);\r
+ Entry != NULL;\r
+ Entry = Entry2) {\r
+ Entry2 = OrderedCollectionNext (Entry);\r
+ OrderedCollectionDelete (AllocationsRestrictedTo32Bit, Entry, NULL);\r
+ }\r
+ OrderedCollectionUninit (AllocationsRestrictedTo32Bit);\r
+}\r
+\r
+\r
+/**\r
+ Iterate over the linker/loader script, and collect the names of the fw_cfg\r
+ blobs that are referenced by QEMU_LOADER_ADD_POINTER.PointeeFile fields, such\r
+ that QEMU_LOADER_ADD_POINTER.PointerSize is less than 8. This means that the\r
+ pointee blob's address will have to be patched into a narrower-than-8 byte\r
+ pointer field, hence the pointee blob must not be allocated from 64-bit\r
+ address space.\r
+\r
+ @param[out] AllocationsRestrictedTo32Bit The ORDERED_COLLECTION structure\r
+ linking (not copying / owning) such\r
+ QEMU_LOADER_ADD_POINTER.PointeeFile\r
+ fields that name the blobs\r
+ restricted from 64-bit allocation.\r
+\r
+ @param[in] LoaderStart Points to the first entry in the\r
+ linker/loader script.\r
+\r
+ @param[in] LoaderEnd Points one past the last entry in\r
+ the linker/loader script.\r
+\r
+ @retval EFI_SUCCESS AllocationsRestrictedTo32Bit has been\r
+ populated.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
+\r
+ @retval EFI_PROTOCOL_ERROR Invalid linker/loader script contents.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+CollectAllocationsRestrictedTo32Bit (\r
+ OUT ORDERED_COLLECTION **AllocationsRestrictedTo32Bit,\r
+ IN CONST QEMU_LOADER_ENTRY *LoaderStart,\r
+ IN CONST QEMU_LOADER_ENTRY *LoaderEnd\r
+)\r
+{\r
+ ORDERED_COLLECTION *Collection;\r
+ CONST QEMU_LOADER_ENTRY *LoaderEntry;\r
+ EFI_STATUS Status;\r
+\r
+ Collection = OrderedCollectionInit (AsciiStringCompare, AsciiStringCompare);\r
+ if (Collection == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ for (LoaderEntry = LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry) {\r
+ CONST QEMU_LOADER_ADD_POINTER *AddPointer;\r
+\r
+ if (LoaderEntry->Type != QemuLoaderCmdAddPointer) {\r
+ continue;\r
+ }\r
+ AddPointer = &LoaderEntry->Command.AddPointer;\r
+\r
+ if (AddPointer->PointerSize >= 8) {\r
+ continue;\r
+ }\r
+\r
+ if (AddPointer->PointeeFile[QEMU_LOADER_FNAME_SIZE - 1] != '\0') {\r
+ DEBUG ((DEBUG_ERROR, "%a: malformed file name\n", __FUNCTION__));\r
+ Status = EFI_PROTOCOL_ERROR;\r
+ goto RollBack;\r
+ }\r
+\r
+ Status = OrderedCollectionInsert (\r
+ Collection,\r
+ NULL, // Entry\r
+ (VOID *)AddPointer->PointeeFile\r
+ );\r
+ switch (Status) {\r
+ case EFI_SUCCESS:\r
+ DEBUG ((\r
+ DEBUG_VERBOSE,\r
+ "%a: restricting blob \"%a\" from 64-bit allocation\n",\r
+ __FUNCTION__,\r
+ AddPointer->PointeeFile\r
+ ));\r
+ break;\r
+ case EFI_ALREADY_STARTED:\r
+ //\r
+ // The restriction has been recorded already.\r
+ //\r
+ break;\r
+ case EFI_OUT_OF_RESOURCES:\r
+ goto RollBack;\r
+ default:\r
+ ASSERT (FALSE);\r
+ }\r
+ }\r
+\r
+ *AllocationsRestrictedTo32Bit = Collection;\r
+ return EFI_SUCCESS;\r
+\r
+RollBack:\r
+ ReleaseAllocationsRestrictedTo32Bit (Collection);\r
+ return Status;\r
+}\r
+\r
+\r