/* macho.c - load Mach-O files. */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ /* This Mach-O loader is incomplete and can load only non-relocatable segments. This is however enough to boot xnu (otool -l and Mach-O specs for more info). */ #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); VasEBoot_err_t VasEBoot_macho_close (VasEBoot_macho_t macho) { VasEBoot_file_t file = macho->file; if (!macho->uncompressed32) VasEBoot_free (macho->cmds32); VasEBoot_free (macho->uncompressed32); if (!macho->uncompressed64) VasEBoot_free (macho->cmds64); VasEBoot_free (macho->uncompressed64); VasEBoot_free (macho); if (file) VasEBoot_file_close (file); return VasEBoot_errno; } VasEBoot_macho_t VasEBoot_macho_file (VasEBoot_file_t file, const char *filename, int is_64bit) { VasEBoot_macho_t macho; union VasEBoot_macho_filestart filestart; macho = VasEBoot_malloc (sizeof (*macho)); if (! macho) return 0; macho->file = file; macho->offset32 = -1; macho->offset64 = -1; macho->end32 = -1; macho->end64 = -1; macho->cmds32 = 0; macho->cmds64 = 0; macho->uncompressed32 = 0; macho->uncompressed64 = 0; macho->compressed32 = 0; macho->compressed64 = 0; if (VasEBoot_file_seek (macho->file, 0) == (VasEBoot_off_t) -1) goto fail; if (VasEBoot_file_read (macho->file, &filestart, sizeof (filestart)) != sizeof (filestart)) { if (!VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), filename); goto fail; } /* Is it a fat file? */ if (filestart.fat.magic == VasEBoot_cpu_to_be32_compile_time (VAS_EBOOT_MACHO_FAT_MAGIC)) { struct VasEBoot_macho_fat_arch *archs; int i, narchs; /* Load architecture description. */ narchs = VasEBoot_be_to_cpu32 (filestart.fat.nfat_arch); if (VasEBoot_file_seek (macho->file, sizeof (struct VasEBoot_macho_fat_header)) == (VasEBoot_off_t) -1) goto fail; archs = VasEBoot_calloc (narchs, sizeof (struct VasEBoot_macho_fat_arch)); if (!archs) goto fail; if (VasEBoot_file_read (macho->file, archs, sizeof (struct VasEBoot_macho_fat_arch) * narchs) != (VasEBoot_ssize_t) sizeof(struct VasEBoot_macho_fat_arch) * narchs) { VasEBoot_free (archs); if (!VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), filename); goto fail; } for (i = 0; i < narchs; i++) { if ((archs[i].cputype == VasEBoot_cpu_to_be32_compile_time (VAS_EBOOT_MACHO_CPUTYPE_IA32)) && !is_64bit) { macho->offset32 = VasEBoot_be_to_cpu32 (archs[i].offset); macho->end32 = VasEBoot_be_to_cpu32 (archs[i].offset) + VasEBoot_be_to_cpu32 (archs[i].size); } if ((archs[i].cputype == VasEBoot_cpu_to_be32_compile_time (VAS_EBOOT_MACHO_CPUTYPE_AMD64)) && is_64bit) { macho->offset64 = VasEBoot_be_to_cpu32 (archs[i].offset); macho->end64 = VasEBoot_be_to_cpu32 (archs[i].offset) + VasEBoot_be_to_cpu32 (archs[i].size); } } VasEBoot_free (archs); } /* Is it a thin 32-bit file? */ if (filestart.thin32.magic == VAS_EBOOT_MACHO_MAGIC32 && !is_64bit) { macho->offset32 = 0; macho->end32 = VasEBoot_file_size (file); } /* Is it a thin 64-bit file? */ if (filestart.thin64.magic == VAS_EBOOT_MACHO_MAGIC64 && is_64bit) { macho->offset64 = 0; macho->end64 = VasEBoot_file_size (file); } if (VasEBoot_memcmp (filestart.lzss.magic, VAS_EBOOT_MACHO_LZSS_MAGIC, sizeof (filestart.lzss.magic)) == 0 && !is_64bit) { macho->offset32 = 0; macho->end32 = VasEBoot_file_size (file); } /* Is it a thin 64-bit file? */ if (VasEBoot_memcmp (filestart.lzss.magic, VAS_EBOOT_MACHO_LZSS_MAGIC, sizeof (filestart.lzss.magic)) == 0 && is_64bit) { macho->offset64 = 0; macho->end64 = VasEBoot_file_size (file); } VasEBoot_macho_parse32 (macho, filename); VasEBoot_macho_parse64 (macho, filename); if (macho->offset32 == -1 && !is_64bit) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "Mach-O doesn't contain suitable 32-bit architecture"); goto fail; } if (macho->offset64 == -1 && is_64bit) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "Mach-O doesn't contain suitable 64-bit architecture"); goto fail; } return macho; fail: macho->file = 0; VasEBoot_macho_close (macho); return 0; } VasEBoot_macho_t VasEBoot_macho_open (const char *name, enum VasEBoot_file_type type, int is_64bit) { VasEBoot_file_t file; VasEBoot_macho_t macho; file = VasEBoot_file_open (name, type); if (! file) return 0; macho = VasEBoot_macho_file (file, name, is_64bit); if (! macho) VasEBoot_file_close (file); return macho; }