Skip to content

Commit

Permalink
feat: windows support fixes
Browse files Browse the repository at this point in the history
partially fixes #22, need companion usb connection to have support for non-unix socket
"Quality is hard, that is why they named themselves Microsoft"
  • Loading branch information
VisualEhrmanntraut committed Jan 18, 2025
1 parent bed10dd commit 7c1d785
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 56 deletions.
8 changes: 4 additions & 4 deletions hw/arm/apple-silicon/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 0 additions & 15 deletions hw/arm/apple-silicon/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions hw/arm/apple-silicon/s8000.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion hw/arm/apple-silicon/sep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 17 additions & 15 deletions hw/arm/apple-silicon/t8030.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -435,15 +436,15 @@ 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);

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;
}
Expand All @@ -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;
Expand All @@ -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 =
Expand All @@ -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 -
Expand All @@ -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,
Expand All @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions hw/arm/apple-silicon/xnu_pf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ MachoHeader64 *xnu_pf_get_kext_header(MachoHeader64 *kheader,
end_dict = strstr(end_dict + 1, "</dict>");
}

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, "<string>");
if (value) {
Expand All @@ -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, "<integer");
if (avalue) {
Expand Down
12 changes: 6 additions & 6 deletions include/hw/arm/apple-silicon/boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ typedef struct {
#define N_EXT (0x01)

typedef struct {
unsigned long base_addr;
unsigned long display;
unsigned long row_bytes;
unsigned long width;
unsigned long height;
unsigned long depth;
uint64_t base_addr;
uint64_t display;
uint64_t row_bytes;
uint64_t width;
uint64_t height;
uint64_t depth;
} AppleVideoArgs;

typedef struct {
Expand Down
4 changes: 0 additions & 4 deletions include/hw/arm/apple-silicon/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ hwaddr ptov_static(hwaddr pa);
hwaddr vtop_slid(hwaddr va);
hwaddr vtop_mmu(hwaddr va, CPUState *cs);

hwaddr align_16k_low(hwaddr addr);
hwaddr align_16k_high(hwaddr addr);
hwaddr align_up(hwaddr addr, hwaddr alignment);

hwaddr vtop_bases(hwaddr va, hwaddr phys_base, hwaddr virt_base);
hwaddr ptov_bases(hwaddr pa, hwaddr phys_base, hwaddr virt_base);

Expand Down

0 comments on commit 7c1d785

Please sign in to comment.