From 7c1d78579b1a761726b777003252f249923e84e9 Mon Sep 17 00:00:00 2001 From: Visual Ehrmanntraut <30368284+VisualEhrmanntraut@users.noreply.github.com> Date: Sat, 18 Jan 2025 05:54:46 +0200 Subject: [PATCH] feat: windows support fixes partially fixes #22, need companion usb connection to have support for non-unix socket "Quality is hard, that is why they named themselves Microsoft" --- hw/arm/apple-silicon/boot.c | 8 ++++---- hw/arm/apple-silicon/mem.c | 15 -------------- hw/arm/apple-silicon/s8000.c | 12 +++++------ hw/arm/apple-silicon/sep.c | 2 +- hw/arm/apple-silicon/t8030.c | 32 +++++++++++++++-------------- hw/arm/apple-silicon/xnu_pf.c | 9 ++++---- include/hw/arm/apple-silicon/boot.h | 12 +++++------ include/hw/arm/apple-silicon/mem.h | 4 ---- 8 files changed, 38 insertions(+), 56 deletions(-) diff --git a/hw/arm/apple-silicon/boot.c b/hw/arm/apple-silicon/boot.c index 5d5edf61e7..f8aa0ebcfc 100644 --- a/hw/arm/apple-silicon/boot.c +++ b/hw/arm/apple-silicon/boot.c @@ -300,7 +300,7 @@ static void extract_im4p_payload(const char *filename, char *payload_type, uint8_t **secure_monitor) { uint8_t *file_data; - unsigned long fsize; + gsize fsize; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; asn1_node img4_definitions = NULL; asn1_node img4; @@ -551,7 +551,7 @@ void macho_populate_dtb(DTBNode *root, AppleBootInfo *info) dtb_set_prop(child, "BootArgs", sizeof(memmap), &memmap); dtb_set_prop(child, "DeviceTree", sizeof(memmap), &memmap); - info->device_tree_size = align_16k_high(dtb_get_serialised_node_size(root)); + info->device_tree_size = ROUND_UP(dtb_get_serialised_node_size(root), 0x4000); } static void set_memory_range(DTBNode *root, const char *name, uint64_t addr, @@ -668,7 +668,7 @@ uint8_t *load_trustcache_from_file(const char *filename, uint64_t *size) file_size = (unsigned long)length; - trustcache_size = align_16k_high(file_size + 8); + trustcache_size = ROUND_UP(file_size + 8, 0x4000); trustcache_data = (uint32_t *)g_malloc(trustcache_size); trustcache_data[0] = 1; // #trustcaches trustcache_data[1] = 8; // offset @@ -749,7 +749,7 @@ void macho_load_raw_file(const char *filename, AddressSpace *as, uint64_t *size) { uint8_t *file_data = NULL; - unsigned long sizef; + gsize sizef; if (g_file_get_contents(filename, (char **)&file_data, &sizef, NULL)) { *size = sizef; diff --git a/hw/arm/apple-silicon/mem.c b/hw/arm/apple-silicon/mem.c index 082164b429..d0b4ea29c7 100644 --- a/hw/arm/apple-silicon/mem.c +++ b/hw/arm/apple-silicon/mem.c @@ -65,21 +65,6 @@ uint8_t get_highest_different_bit_index(hwaddr addr1, hwaddr addr2) return 64 - __builtin_clzll(addr1 ^ addr2); } -hwaddr align_16k_low(hwaddr addr) -{ - return addr & ~0x3FFFull; -} - -hwaddr align_16k_high(hwaddr addr) -{ - return align_up(addr, 0x4000); -} - -hwaddr align_up(hwaddr addr, hwaddr alignment) -{ - return (addr + (alignment - 1)) & ~(alignment - 1); -} - uint8_t get_lowest_non_zero_bit_index(hwaddr addr) { g_assert_cmphex(addr, !=, 0); diff --git a/hw/arm/apple-silicon/s8000.c b/hw/arm/apple-silicon/s8000.c index e1443f8b43..2c0253e830 100644 --- a/hw/arm/apple-silicon/s8000.c +++ b/hw/arm/apple-silicon/s8000.c @@ -252,19 +252,19 @@ static void s8000_load_classic_kc(S8000MachineState *s8000_machine, info_report("Kernel entry point: 0x" TARGET_FMT_lx, info->kern_entry); virt_end += g_virt_slide; - phys_ptr = vtop_static(align_16k_high(virt_end)); + phys_ptr = vtop_static(ROUND_UP(virt_end, 0x4000)); // Device tree info->device_tree_addr = phys_ptr; dtb_va = ptov_static(info->device_tree_addr); - phys_ptr += align_16k_high(info->device_tree_size); + phys_ptr += info->device_tree_size; // RAM disk if (machine->initrd_filename) { info->ramdisk_addr = phys_ptr; macho_load_ramdisk(machine->initrd_filename, nsas, sysmem, info->ramdisk_addr, &info->ramdisk_size); - info->ramdisk_size = align_16k_high(info->ramdisk_size); + info->ramdisk_size = ROUND_UP(info->ramdisk_size, 0x4000); phys_ptr += info->ramdisk_size; } @@ -273,18 +273,18 @@ static void s8000_load_classic_kc(S8000MachineState *s8000_machine, macho_load_raw_file(s8000_machine->sep_fw_filename, nsas, sysmem, "sepfw", info->sep_fw_addr, &info->sep_fw_size); } - info->sep_fw_size = align_16k_high(8 * MiB); + info->sep_fw_size = ROUND_UP(8 * MiB, 0x4000); phys_ptr += info->sep_fw_size; // Kernel boot args info->kern_boot_args_addr = phys_ptr; info->kern_boot_args_size = 0x4000; - phys_ptr += align_16k_high(0x4000); + phys_ptr += info->kern_boot_args_size; macho_load_dtb(s8000_machine->device_tree, nsas, sysmem, "DeviceTree", info); - top_of_kernel_data_pa = (align_16k_high(phys_ptr) + 0x3000ull) & ~0x3FFFull; + top_of_kernel_data_pa = (ROUND_UP(phys_ptr, 0x4000) + 0x3000ull) & ~0x3FFFull; info_report("Boot args: [%s]", cmdline); macho_setup_bootargs("BootArgs", nsas, sysmem, info->kern_boot_args_addr, diff --git a/hw/arm/apple-silicon/sep.c b/hw/arm/apple-silicon/sep.c index 64d5a9d04d..9acb3b96ad 100644 --- a/hw/arm/apple-silicon/sep.c +++ b/hw/arm/apple-silicon/sep.c @@ -3471,7 +3471,7 @@ static void map_sepfw(AppleSEPState *s) } AddressSpace *nsas = &address_space_memory; // Apparently needed because of a bug occurring on XNU - address_space_set(nsas, 0x4000ULL, 0, align_16k_high(8 * MiB), + address_space_set(nsas, 0x4000ULL, 0, ROUND_UP(8 * MiB, 0x4000), MEMTXATTRS_UNSPECIFIED); address_space_rw(nsas, 0x4000ULL, MEMTXATTRS_UNSPECIFIED, (uint8_t *)s->sepfw_data, s->sep_fw_size, true); diff --git a/hw/arm/apple-silicon/t8030.c b/hw/arm/apple-silicon/t8030.c index 83bbf32593..f648c3ee62 100644 --- a/hw/arm/apple-silicon/t8030.c +++ b/hw/arm/apple-silicon/t8030.c @@ -338,7 +338,7 @@ static void t8030_load_classic_kc(T8030MachineState *t8030_machine, info_report("Kernel entry point: 0x" TARGET_FMT_lx, info->kern_entry); virt_end += g_virt_slide; - phys_ptr = vtop_static(align_16k_high(virt_end)); + phys_ptr = vtop_static(ROUND_UP(virt_end, 0x4000)); amcc_lower = info->trustcache_addr; amcc_upper = vtop_slid(last_base) + last_seg->vmsize - 1; @@ -354,7 +354,7 @@ static void t8030_load_classic_kc(T8030MachineState *t8030_machine, info->ramdisk_addr = phys_ptr; macho_load_ramdisk(machine->initrd_filename, nsas, sysmem, info->ramdisk_addr, &info->ramdisk_size); - info->ramdisk_size = align_16k_high(info->ramdisk_size); + info->ramdisk_size = ROUND_UP(info->ramdisk_size, 0x4000); phys_ptr += info->ramdisk_size; } @@ -370,18 +370,18 @@ static void t8030_load_classic_kc(T8030MachineState *t8030_machine, g_file_get_contents(t8030_machine->sep_fw_filename, &sep->sepfw_data, NULL, NULL); } - info->sep_fw_size = align_16k_high(8 * MiB); + info->sep_fw_size = ROUND_UP(8 * MiB, 0x4000); phys_ptr += info->sep_fw_size; // Kernel boot args info->kern_boot_args_addr = phys_ptr; info->kern_boot_args_size = 0x4000; - phys_ptr += align_16k_high(0x4000); + phys_ptr += info->kern_boot_args_size; // Device tree info->device_tree_addr = phys_ptr; dtb_va = ptov_static(info->device_tree_addr); - phys_ptr += align_16k_high(info->device_tree_size); + phys_ptr += info->device_tree_size; info_report("Device tree physical base: 0x" TARGET_FMT_lx, info->device_tree_addr); info_report("Device tree virtual base: 0x" TARGET_FMT_lx, dtb_va); @@ -395,7 +395,8 @@ static void t8030_load_classic_kc(T8030MachineState *t8030_machine, macho_load_dtb(t8030_machine->device_tree, nsas, sysmem, "DeviceTree", info); - top_of_kernel_data_pa = (align_16k_high(phys_ptr) + 0x3000ull) & ~0x3FFFull; + top_of_kernel_data_pa = + (ROUND_UP(phys_ptr, 0x4000) + 0x3000ull) & ~0x3FFFull; info_report("Boot args: [%s]", cmdline); macho_setup_bootargs("BootArgs", nsas, sysmem, info->kern_boot_args_addr, @@ -435,7 +436,7 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, prelink_info_seg = macho_get_segment(hdr, "__PRELINK_INFO"); extradata_size = - align_16k_high(info->device_tree_size + info->trustcache_size); + ROUND_UP(info->device_tree_size + info->trustcache_size, 0x4000); g_assert_cmpuint(extradata_size, <, L2_GRANULE); get_kaslr_slides(t8030_machine, &g_phys_slide, &g_virt_slide); @@ -443,7 +444,7 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, l2_remaining = (virt_low + g_virt_slide) & L2_GRANULE_MASK; if (extradata_size >= l2_remaining) { - uint64_t grown_slide = align_16k_high(extradata_size - l2_remaining); + uint64_t grown_slide = ROUND_UP(extradata_size - l2_remaining, 0x4000); g_phys_slide += grown_slide; g_virt_slide += grown_slide; } @@ -461,7 +462,7 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, info->trustcache_addr = phys_ptr; macho_load_trustcache(t8030_machine->trustcache, info->trustcache_size, nsas, sysmem, info->trustcache_addr); - phys_ptr += align_16k_high(info->trustcache_size); + phys_ptr += ROUND_UP(info->trustcache_size, 0x4000); g_virt_base += g_virt_slide; g_virt_base -= phys_ptr - g_phys_base; @@ -474,7 +475,7 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, info_report("Kernel entry point: 0x" TARGET_FMT_lx, info->kern_entry); virt_end += g_virt_slide; - phys_ptr = vtop_static(align_16k_high(virt_end)); + phys_ptr = vtop_static(ROUND_UP(virt_end, 0x4000)); amcc_lower = info->device_tree_addr; amcc_upper = @@ -492,18 +493,18 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, info->ramdisk_addr = phys_ptr; macho_load_ramdisk(machine->initrd_filename, nsas, sysmem, info->ramdisk_addr, &info->ramdisk_size); - info->ramdisk_size = align_16k_high(info->ramdisk_size); + info->ramdisk_size = ROUND_UP(info->ramdisk_size, 0x400); phys_ptr += info->ramdisk_size; } // SEPFW info->sep_fw_addr = phys_ptr; - info->sep_fw_size = align_16k_high(8 * MiB); + info->sep_fw_size = ROUND_UP(8 * MiB, 0x4000); phys_ptr += info->sep_fw_size; info->kern_boot_args_addr = phys_ptr; info->kern_boot_args_size = 0x4000; - phys_ptr += align_16k_high(0x4000); + phys_ptr += info->kern_boot_args_size; mem_size = machine->maxram_size - @@ -512,7 +513,8 @@ static void t8030_load_fileset_kc(T8030MachineState *t8030_machine, macho_load_dtb(t8030_machine->device_tree, nsas, sysmem, "DeviceTree", info); - top_of_kernel_data_pa = (align_16k_high(phys_ptr) + 0x3000ull) & ~0x3fffull; + top_of_kernel_data_pa = + (ROUND_UP(phys_ptr, 0x4000) + 0x3000ull) & ~0x3fffull; info_report("Boot args: [%s]", cmdline); macho_setup_bootargs("BootArgs", nsas, sysmem, info->kern_boot_args_addr, @@ -532,7 +534,7 @@ static void t8030_memory_setup(T8030MachineState *t8030_machine) AddressSpace *nsas; char *cmdline; char *seprom; - unsigned long fsize; + gsize fsize; machine = MACHINE(t8030_machine); info = &t8030_machine->bootinfo; diff --git a/hw/arm/apple-silicon/xnu_pf.c b/hw/arm/apple-silicon/xnu_pf.c index 46d1ce99e7..3900eddb89 100644 --- a/hw/arm/apple-silicon/xnu_pf.c +++ b/hw/arm/apple-silicon/xnu_pf.c @@ -120,8 +120,8 @@ MachoHeader64 *xnu_pf_get_kext_header(MachoHeader64 *kheader, end_dict = strstr(end_dict + 1, ""); } - ident = memmem(last_dict, end_dict - last_dict, - "CFBundleIdentifier", strlen("CFBundleIdentifier")); + ident = g_strstr_len(last_dict, end_dict - last_dict, + "CFBundleIdentifier"); if (ident) { const char *value = strstr(ident, ""); if (value) { @@ -134,9 +134,8 @@ MachoHeader64 *xnu_pf_get_kext_header(MachoHeader64 *kheader, kname[value_end - value] = 0; if (strcmp(kname, kext_bundle_id) == 0) { const char *addr = - memmem(last_dict, end_dict - last_dict, - "_PrelinkExecutableLoadAddr", - strlen("_PrelinkExecutableLoadAddr")); + g_strstr_len(last_dict, end_dict - last_dict, + "_PrelinkExecutableLoadAddr"); if (addr) { const char *avalue = strstr(addr, "