12118f387SNathan Whitehorn /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 31de7b4b8SPedro F. Giffuni * 42118f387SNathan Whitehorn * Copyright (c) 2011 Nathan Whitehorn 52118f387SNathan Whitehorn * All rights reserved. 62118f387SNathan Whitehorn * 72118f387SNathan Whitehorn * Redistribution and use in source and binary forms, with or without 82118f387SNathan Whitehorn * modification, are permitted provided that the following conditions 92118f387SNathan Whitehorn * are met: 102118f387SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 112118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer. 122118f387SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 132118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 142118f387SNathan Whitehorn * documentation and/or other materials provided with the distribution. 152118f387SNathan Whitehorn * 162118f387SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 172118f387SNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 182118f387SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 192118f387SNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 202118f387SNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 212118f387SNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 222118f387SNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 232118f387SNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 242118f387SNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 252118f387SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 262118f387SNathan Whitehorn * SUCH DAMAGE. 272118f387SNathan Whitehorn * 282118f387SNathan Whitehorn * $FreeBSD$ 292118f387SNathan Whitehorn */ 302118f387SNathan Whitehorn 31e25e006dSNathan Whitehorn #include <sys/types.h> 32e25e006dSNathan Whitehorn #include <sys/sysctl.h> 332118f387SNathan Whitehorn #include <string.h> 342118f387SNathan Whitehorn 352118f387SNathan Whitehorn #include "partedit.h" 362118f387SNathan Whitehorn 37e25e006dSNathan Whitehorn static char platform[255] = ""; 38e25e006dSNathan Whitehorn 392118f387SNathan Whitehorn const char * 402118f387SNathan Whitehorn default_scheme(void) { 41e25e006dSNathan Whitehorn size_t platlen = sizeof(platform); 42e25e006dSNathan Whitehorn if (strlen(platform) == 0) 43e25e006dSNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1); 44e25e006dSNathan Whitehorn 45e25e006dSNathan Whitehorn if (strcmp(platform, "powermac") == 0) 462118f387SNathan Whitehorn return ("APM"); 4760f18ad0SJustin Hibbits if (strcmp(platform, "chrp") == 0 || strcmp(platform, "ps3") == 0 || 4860f18ad0SJustin Hibbits strcmp(platform, "mpc85xx") == 0) 49e25e006dSNathan Whitehorn return ("MBR"); 50e25e006dSNathan Whitehorn 518befcf7bSNathan Whitehorn /* Pick GPT as a generic default */ 52e25e006dSNathan Whitehorn return ("GPT"); 532118f387SNathan Whitehorn } 542118f387SNathan Whitehorn 552118f387SNathan Whitehorn int 562118f387SNathan Whitehorn is_scheme_bootable(const char *part_type) { 57e25e006dSNathan Whitehorn size_t platlen = sizeof(platform); 58e25e006dSNathan Whitehorn if (strlen(platform) == 0) 59e25e006dSNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1); 60e25e006dSNathan Whitehorn 61e25e006dSNathan Whitehorn if (strcmp(platform, "powermac") == 0 && strcmp(part_type, "APM") == 0) 622118f387SNathan Whitehorn return (1); 638befcf7bSNathan Whitehorn if (strcmp(platform, "powernv") == 0 && strcmp(part_type, "GPT") == 0) 64e25e006dSNathan Whitehorn return (1); 658befcf7bSNathan Whitehorn if ((strcmp(platform, "chrp") == 0 || strcmp(platform, "ps3") == 0) && 661ee0f089SNathan Whitehorn (strcmp(part_type, "MBR") == 0 || strcmp(part_type, "BSD") == 0 || 671ee0f089SNathan Whitehorn strcmp(part_type, "GPT") == 0)) 68e25e006dSNathan Whitehorn return (1); 6960f18ad0SJustin Hibbits if (strcmp(platform, "mpc85xx") == 0 && strcmp(part_type, "MBR") == 0) 7060f18ad0SJustin Hibbits return (1); 71e25e006dSNathan Whitehorn 722118f387SNathan Whitehorn return (0); 732118f387SNathan Whitehorn } 742118f387SNathan Whitehorn 756e15678aSNathan Whitehorn int 766e15678aSNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs) 776e15678aSNathan Whitehorn { 786e15678aSNathan Whitehorn if (strcmp(fs, "freebsd-ufs") == 0) 796e15678aSNathan Whitehorn return (1); 806e15678aSNathan Whitehorn 816e15678aSNathan Whitehorn return (0); 826e15678aSNathan Whitehorn } 836e15678aSNathan Whitehorn 842118f387SNathan Whitehorn size_t 858befcf7bSNathan Whitehorn bootpart_size(const char *part_type) 868befcf7bSNathan Whitehorn { 871ee0f089SNathan Whitehorn size_t platlen = sizeof(platform); 881ee0f089SNathan Whitehorn if (strlen(platform) == 0) 891ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1); 901ee0f089SNathan Whitehorn 918befcf7bSNathan Whitehorn if (strcmp(part_type, "APM") == 0) 922118f387SNathan Whitehorn return (800*1024); 938befcf7bSNathan Whitehorn if (strcmp(part_type, "BSD") == 0) /* Nothing for nested */ 948befcf7bSNathan Whitehorn return (0); 958befcf7bSNathan Whitehorn if (strcmp(platform, "chrp") == 0) 961ee0f089SNathan Whitehorn return (800*1024); 97*bc4d122dSJustin Hibbits if (strcmp(platform, "ps3") == 0 || strcmp(platform, "powernv") == 0) 988befcf7bSNathan Whitehorn return (512*1024*1024); 99*bc4d122dSJustin Hibbits if (strcmp(platform, "mpc85xx") == 0) 100*bc4d122dSJustin Hibbits return (16*1024*1024); 1012118f387SNathan Whitehorn return (0); 1022118f387SNathan Whitehorn } 1032118f387SNathan Whitehorn 1042118f387SNathan Whitehorn const char * 1058befcf7bSNathan Whitehorn bootpart_type(const char *scheme, const char **mountpoint) 1068befcf7bSNathan Whitehorn { 1071ee0f089SNathan Whitehorn size_t platlen = sizeof(platform); 1081ee0f089SNathan Whitehorn if (strlen(platform) == 0) 1091ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1); 1101ee0f089SNathan Whitehorn 1111ee0f089SNathan Whitehorn if (strcmp(platform, "chrp") == 0) 1121ee0f089SNathan Whitehorn return ("prep-boot"); 1131ee0f089SNathan Whitehorn if (strcmp(platform, "powermac") == 0) 1141ee0f089SNathan Whitehorn return ("apple-boot"); 115*bc4d122dSJustin Hibbits if (strcmp(platform, "powernv") == 0 || strcmp(platform, "ps3") == 0) { 1168befcf7bSNathan Whitehorn *mountpoint = "/boot"; 1178befcf7bSNathan Whitehorn if (strcmp(scheme, "GPT") == 0) 1188befcf7bSNathan Whitehorn return ("ms-basic-data"); 1198befcf7bSNathan Whitehorn else if (strcmp(scheme, "MBR") == 0) 1208befcf7bSNathan Whitehorn return ("fat32"); 1218befcf7bSNathan Whitehorn } 122*bc4d122dSJustin Hibbits if (strcmp(platform, "mpc85xx") == 0) { 123*bc4d122dSJustin Hibbits *mountpoint = "/boot/uboot"; 124*bc4d122dSJustin Hibbits return ("fat16"); 125*bc4d122dSJustin Hibbits } 1261ee0f089SNathan Whitehorn 1276b446ed5SNathan Whitehorn return ("freebsd-boot"); 1286b446ed5SNathan Whitehorn } 1296b446ed5SNathan Whitehorn 1306b446ed5SNathan Whitehorn const char * 1312118f387SNathan Whitehorn bootcode_path(const char *part_type) { 1322118f387SNathan Whitehorn return (NULL); 1332118f387SNathan Whitehorn } 1342118f387SNathan Whitehorn 1352118f387SNathan Whitehorn const char * 1366e15678aSNathan Whitehorn partcode_path(const char *part_type, const char *fs_type) { 1371ee0f089SNathan Whitehorn size_t platlen = sizeof(platform); 1381ee0f089SNathan Whitehorn if (strlen(platform) == 0) 1391ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1); 1401ee0f089SNathan Whitehorn 1412118f387SNathan Whitehorn if (strcmp(part_type, "APM") == 0) 1422118f387SNathan Whitehorn return ("/boot/boot1.hfs"); 1438befcf7bSNathan Whitehorn if (strcmp(platform, "chrp") == 0) 144090dd246SNathan Whitehorn return ("/boot/boot1.elf"); 1452118f387SNathan Whitehorn return (NULL); 1462118f387SNathan Whitehorn } 1472118f387SNathan Whitehorn 148