12118f387SNathan Whitehorn /*- 22118f387SNathan Whitehorn * Copyright (c) 2011 Nathan Whitehorn 32118f387SNathan Whitehorn * All rights reserved. 42118f387SNathan Whitehorn * 52118f387SNathan Whitehorn * Redistribution and use in source and binary forms, with or without 62118f387SNathan Whitehorn * modification, are permitted provided that the following conditions 72118f387SNathan Whitehorn * are met: 82118f387SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 92118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer. 102118f387SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 112118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 122118f387SNathan Whitehorn * documentation and/or other materials provided with the distribution. 132118f387SNathan Whitehorn * 142118f387SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152118f387SNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 162118f387SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 172118f387SNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182118f387SNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192118f387SNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202118f387SNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212118f387SNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 222118f387SNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232118f387SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242118f387SNathan Whitehorn * SUCH DAMAGE. 252118f387SNathan Whitehorn * 262118f387SNathan Whitehorn * $FreeBSD$ 272118f387SNathan Whitehorn */ 282118f387SNathan Whitehorn 296b446ed5SNathan Whitehorn #include <sys/types.h> 306b446ed5SNathan Whitehorn #include <sys/sysctl.h> 312118f387SNathan Whitehorn #include <string.h> 322118f387SNathan Whitehorn 332118f387SNathan Whitehorn #include "partedit.h" 342118f387SNathan Whitehorn 35be01d6e9SNathan Whitehorn static const char * 36be01d6e9SNathan Whitehorn x86_bootmethod(void) 37be01d6e9SNathan Whitehorn { 38be01d6e9SNathan Whitehorn static char fw[255] = ""; 39be01d6e9SNathan Whitehorn size_t len = sizeof(fw); 40be01d6e9SNathan Whitehorn int error; 41be01d6e9SNathan Whitehorn 42be01d6e9SNathan Whitehorn if (strlen(fw) == 0) { 43be01d6e9SNathan Whitehorn error = sysctlbyname("machdep.bootmethod", fw, &len, NULL, -1); 44be01d6e9SNathan Whitehorn if (error != 0) 45be01d6e9SNathan Whitehorn return ("BIOS"); 46be01d6e9SNathan Whitehorn } 47be01d6e9SNathan Whitehorn 48be01d6e9SNathan Whitehorn return (fw); 49be01d6e9SNathan Whitehorn } 506b446ed5SNathan Whitehorn 512118f387SNathan Whitehorn const char * 52403a3c8cSNathan Whitehorn default_scheme(void) 53403a3c8cSNathan Whitehorn { 54*04e98a37SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 552118f387SNathan Whitehorn return ("GPT"); 56*04e98a37SNathan Whitehorn else 57*04e98a37SNathan Whitehorn return ("MBR"); 582118f387SNathan Whitehorn } 592118f387SNathan Whitehorn 602118f387SNathan Whitehorn int 61be01d6e9SNathan Whitehorn is_scheme_bootable(const char *part_type) 62be01d6e9SNathan Whitehorn { 636b446ed5SNathan Whitehorn 642118f387SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) 652118f387SNathan Whitehorn return (1); 66be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "BIOS") == 0) { 676b446ed5SNathan Whitehorn if (strcmp(part_type, "BSD") == 0) 686b446ed5SNathan Whitehorn return (1); 692118f387SNathan Whitehorn if (strcmp(part_type, "MBR") == 0) 702118f387SNathan Whitehorn return (1); 716b446ed5SNathan Whitehorn } 722118f387SNathan Whitehorn 732118f387SNathan Whitehorn return (0); 742118f387SNathan Whitehorn } 752118f387SNathan Whitehorn 766e15678aSNathan Whitehorn int 77be01d6e9SNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs) 78be01d6e9SNathan Whitehorn { 796e15678aSNathan Whitehorn 806e15678aSNathan Whitehorn if (strcmp(fs, "freebsd-ufs") == 0) 816e15678aSNathan Whitehorn return (1); 826e15678aSNathan Whitehorn 83be01d6e9SNathan Whitehorn if (strcmp(fs, "freebsd-zfs") == 0 && 84403a3c8cSNathan Whitehorn strcmp(part_type, "GPT") == 0 && 85be01d6e9SNathan Whitehorn strcmp(x86_bootmethod(), "BIOS") == 0) 866e15678aSNathan Whitehorn return (1); 876e15678aSNathan Whitehorn 886e15678aSNathan Whitehorn return (0); 896e15678aSNathan Whitehorn } 906e15678aSNathan Whitehorn 912118f387SNathan Whitehorn size_t 92be01d6e9SNathan Whitehorn bootpart_size(const char *scheme) 93be01d6e9SNathan Whitehorn { 942118f387SNathan Whitehorn 952118f387SNathan Whitehorn /* No partcode except for GPT */ 966b446ed5SNathan Whitehorn if (strcmp(scheme, "GPT") != 0) 976b446ed5SNathan Whitehorn return (0); 986b446ed5SNathan Whitehorn 99be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "BIOS") == 0) 100963ae465SNathan Whitehorn return (512*1024); 1016b446ed5SNathan Whitehorn else 1026b446ed5SNathan Whitehorn return (800*1024); 1036b446ed5SNathan Whitehorn 1042118f387SNathan Whitehorn return (0); 1052118f387SNathan Whitehorn } 1062118f387SNathan Whitehorn 1072118f387SNathan Whitehorn const char * 108be01d6e9SNathan Whitehorn bootpart_type(const char *scheme) 109be01d6e9SNathan Whitehorn { 1106b446ed5SNathan Whitehorn 111be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1126b446ed5SNathan Whitehorn return ("efi"); 1136b446ed5SNathan Whitehorn 1146b446ed5SNathan Whitehorn return ("freebsd-boot"); 1156b446ed5SNathan Whitehorn } 1166b446ed5SNathan Whitehorn 1176b446ed5SNathan Whitehorn const char * 118be01d6e9SNathan Whitehorn bootcode_path(const char *part_type) 119be01d6e9SNathan Whitehorn { 120be01d6e9SNathan Whitehorn 121be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1226b446ed5SNathan Whitehorn return (NULL); 1236b446ed5SNathan Whitehorn 1242118f387SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) 1252118f387SNathan Whitehorn return ("/boot/pmbr"); 1262118f387SNathan Whitehorn if (strcmp(part_type, "MBR") == 0) 1272118f387SNathan Whitehorn return ("/boot/mbr"); 1282118f387SNathan Whitehorn if (strcmp(part_type, "BSD") == 0) 1292118f387SNathan Whitehorn return ("/boot/boot"); 1302118f387SNathan Whitehorn 1312118f387SNathan Whitehorn return (NULL); 1322118f387SNathan Whitehorn } 1332118f387SNathan Whitehorn 1342118f387SNathan Whitehorn const char * 135be01d6e9SNathan Whitehorn partcode_path(const char *part_type, const char *fs_type) 136be01d6e9SNathan Whitehorn { 1376b446ed5SNathan Whitehorn 1386b446ed5SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) { 139be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1406b446ed5SNathan Whitehorn return ("/boot/boot1.efifat"); 1416e15678aSNathan Whitehorn else if (strcmp(fs_type, "zfs") == 0) 1426e15678aSNathan Whitehorn return ("/boot/gptzfsboot"); 1436b446ed5SNathan Whitehorn else 1442118f387SNathan Whitehorn return ("/boot/gptboot"); 1456b446ed5SNathan Whitehorn } 1462118f387SNathan Whitehorn 1472118f387SNathan Whitehorn /* No partcode except for GPT */ 1482118f387SNathan Whitehorn return (NULL); 1492118f387SNathan Whitehorn } 1502118f387SNathan Whitehorn 151