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 * 52*403a3c8cSNathan Whitehorn default_scheme(void) 53*403a3c8cSNathan Whitehorn { 542118f387SNathan Whitehorn return ("GPT"); 552118f387SNathan Whitehorn } 562118f387SNathan Whitehorn 572118f387SNathan Whitehorn int 58be01d6e9SNathan Whitehorn is_scheme_bootable(const char *part_type) 59be01d6e9SNathan Whitehorn { 606b446ed5SNathan Whitehorn 612118f387SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) 622118f387SNathan Whitehorn return (1); 63be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "BIOS") == 0) { 646b446ed5SNathan Whitehorn if (strcmp(part_type, "BSD") == 0) 656b446ed5SNathan Whitehorn return (1); 662118f387SNathan Whitehorn if (strcmp(part_type, "MBR") == 0) 672118f387SNathan Whitehorn return (1); 686b446ed5SNathan Whitehorn } 692118f387SNathan Whitehorn 702118f387SNathan Whitehorn return (0); 712118f387SNathan Whitehorn } 722118f387SNathan Whitehorn 736e15678aSNathan Whitehorn int 74be01d6e9SNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs) 75be01d6e9SNathan Whitehorn { 766e15678aSNathan Whitehorn 776e15678aSNathan Whitehorn if (strcmp(fs, "freebsd-ufs") == 0) 786e15678aSNathan Whitehorn return (1); 796e15678aSNathan Whitehorn 80be01d6e9SNathan Whitehorn if (strcmp(fs, "freebsd-zfs") == 0 && 81*403a3c8cSNathan Whitehorn strcmp(part_type, "GPT") == 0 && 82be01d6e9SNathan Whitehorn strcmp(x86_bootmethod(), "BIOS") == 0) 836e15678aSNathan Whitehorn return (1); 846e15678aSNathan Whitehorn 856e15678aSNathan Whitehorn return (0); 866e15678aSNathan Whitehorn } 876e15678aSNathan Whitehorn 882118f387SNathan Whitehorn size_t 89be01d6e9SNathan Whitehorn bootpart_size(const char *scheme) 90be01d6e9SNathan Whitehorn { 912118f387SNathan Whitehorn 922118f387SNathan Whitehorn /* No partcode except for GPT */ 936b446ed5SNathan Whitehorn if (strcmp(scheme, "GPT") != 0) 946b446ed5SNathan Whitehorn return (0); 956b446ed5SNathan Whitehorn 96be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "BIOS") == 0) 97963ae465SNathan Whitehorn return (512*1024); 986b446ed5SNathan Whitehorn else 996b446ed5SNathan Whitehorn return (800*1024); 1006b446ed5SNathan Whitehorn 1012118f387SNathan Whitehorn return (0); 1022118f387SNathan Whitehorn } 1032118f387SNathan Whitehorn 1042118f387SNathan Whitehorn const char * 105be01d6e9SNathan Whitehorn bootpart_type(const char *scheme) 106be01d6e9SNathan Whitehorn { 1076b446ed5SNathan Whitehorn 108be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1096b446ed5SNathan Whitehorn return ("efi"); 1106b446ed5SNathan Whitehorn 1116b446ed5SNathan Whitehorn return ("freebsd-boot"); 1126b446ed5SNathan Whitehorn } 1136b446ed5SNathan Whitehorn 1146b446ed5SNathan Whitehorn const char * 115be01d6e9SNathan Whitehorn bootcode_path(const char *part_type) 116be01d6e9SNathan Whitehorn { 117be01d6e9SNathan Whitehorn 118be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1196b446ed5SNathan Whitehorn return (NULL); 1206b446ed5SNathan Whitehorn 1212118f387SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) 1222118f387SNathan Whitehorn return ("/boot/pmbr"); 1232118f387SNathan Whitehorn if (strcmp(part_type, "MBR") == 0) 1242118f387SNathan Whitehorn return ("/boot/mbr"); 1252118f387SNathan Whitehorn if (strcmp(part_type, "BSD") == 0) 1262118f387SNathan Whitehorn return ("/boot/boot"); 1272118f387SNathan Whitehorn 1282118f387SNathan Whitehorn return (NULL); 1292118f387SNathan Whitehorn } 1302118f387SNathan Whitehorn 1312118f387SNathan Whitehorn const char * 132be01d6e9SNathan Whitehorn partcode_path(const char *part_type, const char *fs_type) 133be01d6e9SNathan Whitehorn { 1346b446ed5SNathan Whitehorn 1356b446ed5SNathan Whitehorn if (strcmp(part_type, "GPT") == 0) { 136be01d6e9SNathan Whitehorn if (strcmp(x86_bootmethod(), "UEFI") == 0) 1376b446ed5SNathan Whitehorn return ("/boot/boot1.efifat"); 1386e15678aSNathan Whitehorn else if (strcmp(fs_type, "zfs") == 0) 1396e15678aSNathan Whitehorn return ("/boot/gptzfsboot"); 1406b446ed5SNathan Whitehorn else 1412118f387SNathan Whitehorn return ("/boot/gptboot"); 1426b446ed5SNathan Whitehorn } 1432118f387SNathan Whitehorn 1442118f387SNathan Whitehorn /* No partcode except for GPT */ 1452118f387SNathan Whitehorn return (NULL); 1462118f387SNathan Whitehorn } 1472118f387SNathan Whitehorn 148