+load_return_t
+fatfile_getbestarch(
+ vm_offset_t data_ptr,
+ vm_size_t data_size,
+ struct fat_arch *archret)
+{
+ /*
+ * Ignore all architectural bits when determining if an image
+ * in a fat file should be skipped or graded.
+ */
+ return fatfile_getarch(data_ptr, data_size, cpu_type(), CPU_ARCH_MASK, archret);
+}
+
+load_return_t
+fatfile_getbestarch_for_cputype(
+ cpu_type_t cputype,
+ vm_offset_t data_ptr,
+ vm_size_t data_size,
+ struct fat_arch *archret)
+{
+ /*
+ * Scan the fat_arch array for exact matches for this cpu_type_t only
+ */
+ return fatfile_getarch(data_ptr, data_size, cputype, 0, archret);
+}
+
+/**********************************************************************
+ * Routine: fatfile_getarch_with_bits()
+ *
+ * Function: Locate the architecture-dependant contents of a fat
+ * file that match this CPU.
+ *
+ * Args: vp: The vnode for the fat file.
+ * archbits: Architecture specific feature bits
+ * header: A pointer to the fat file header.
+ * archret (out): Pointer to fat_arch structure to hold
+ * the results.
+ *
+ * Returns: KERN_SUCCESS: Valid architecture found.
+ * KERN_FAILURE: No valid architecture found.
+ **********************************************************************/
+load_return_t
+fatfile_getarch_with_bits(
+ integer_t archbits,
+ vm_offset_t data_ptr,
+ vm_size_t data_size,
+ struct fat_arch *archret)
+{
+ /*
+ * Scan the fat_arch array for matches with the requested
+ * architectural bits set, and for the current hardware cpu CPU.
+ */
+ return fatfile_getarch(data_ptr, data_size, (archbits & CPU_ARCH_MASK) | (cpu_type() & ~CPU_ARCH_MASK), 0, archret);
+}
+
+/*
+ * Validate the fat_header and fat_arch array in memory. We check that:
+ *
+ * 1) arch count would not exceed the data buffer
+ * 2) arch list does not contain duplicate cputype/cpusubtype tuples
+ * 3) arch list does not have two overlapping slices. The area
+ * at the front of the file containing the fat headers is implicitly
+ * a range that a slice should also not try to cover
+ */
+load_return_t
+fatfile_validate_fatarches(vm_offset_t data_ptr, vm_size_t data_size)
+{
+ uint32_t magic, nfat_arch;
+ uint32_t max_nfat_arch, i, j;
+ uint32_t fat_header_size;
+
+ struct fat_arch *arches;
+ struct fat_header *header;
+
+ if (sizeof(struct fat_header) > data_size) {
+ return (LOAD_FAILURE);
+ }
+
+ header = (struct fat_header *)data_ptr;
+ magic = OSSwapBigToHostInt32(header->magic);
+ nfat_arch = OSSwapBigToHostInt32(header->nfat_arch);
+
+ if (magic != FAT_MAGIC) {
+ /* must be FAT_MAGIC big endian */
+ return (LOAD_FAILURE);
+ }