12118f387SNathan Whitehorn /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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
29e25e006dSNathan Whitehorn #include <sys/types.h>
30e25e006dSNathan Whitehorn #include <sys/sysctl.h>
312118f387SNathan Whitehorn #include <string.h>
322118f387SNathan Whitehorn
332118f387SNathan Whitehorn #include "partedit.h"
342118f387SNathan Whitehorn
35e25e006dSNathan Whitehorn static char platform[255] = "";
36e25e006dSNathan Whitehorn
372118f387SNathan Whitehorn const char *
default_scheme(void)382118f387SNathan Whitehorn default_scheme(void) {
39e25e006dSNathan Whitehorn size_t platlen = sizeof(platform);
40e25e006dSNathan Whitehorn if (strlen(platform) == 0)
41e25e006dSNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1);
42e25e006dSNathan Whitehorn
43e25e006dSNathan Whitehorn if (strcmp(platform, "powermac") == 0)
442118f387SNathan Whitehorn return ("APM");
4560f18ad0SJustin Hibbits if (strcmp(platform, "chrp") == 0 || strcmp(platform, "ps3") == 0 ||
4660f18ad0SJustin Hibbits strcmp(platform, "mpc85xx") == 0)
47e25e006dSNathan Whitehorn return ("MBR");
48e25e006dSNathan Whitehorn
498befcf7bSNathan Whitehorn /* Pick GPT as a generic default */
50e25e006dSNathan Whitehorn return ("GPT");
512118f387SNathan Whitehorn }
522118f387SNathan Whitehorn
532118f387SNathan Whitehorn int
is_scheme_bootable(const char * part_type)542118f387SNathan Whitehorn is_scheme_bootable(const char *part_type) {
55e25e006dSNathan Whitehorn size_t platlen = sizeof(platform);
56e25e006dSNathan Whitehorn if (strlen(platform) == 0)
57e25e006dSNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1);
58e25e006dSNathan Whitehorn
59e25e006dSNathan Whitehorn if (strcmp(platform, "powermac") == 0 && strcmp(part_type, "APM") == 0)
602118f387SNathan Whitehorn return (1);
618befcf7bSNathan Whitehorn if (strcmp(platform, "powernv") == 0 && strcmp(part_type, "GPT") == 0)
62e25e006dSNathan Whitehorn return (1);
638befcf7bSNathan Whitehorn if ((strcmp(platform, "chrp") == 0 || strcmp(platform, "ps3") == 0) &&
641ee0f089SNathan Whitehorn (strcmp(part_type, "MBR") == 0 || strcmp(part_type, "BSD") == 0 ||
651ee0f089SNathan Whitehorn strcmp(part_type, "GPT") == 0))
66e25e006dSNathan Whitehorn return (1);
6760f18ad0SJustin Hibbits if (strcmp(platform, "mpc85xx") == 0 && strcmp(part_type, "MBR") == 0)
6860f18ad0SJustin Hibbits return (1);
69e25e006dSNathan Whitehorn
702118f387SNathan Whitehorn return (0);
712118f387SNathan Whitehorn }
722118f387SNathan Whitehorn
736e15678aSNathan Whitehorn int
is_fs_bootable(const char * part_type,const char * fs)746e15678aSNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs)
756e15678aSNathan Whitehorn {
766e15678aSNathan Whitehorn if (strcmp(fs, "freebsd-ufs") == 0)
776e15678aSNathan Whitehorn return (1);
786e15678aSNathan Whitehorn
796e15678aSNathan Whitehorn return (0);
806e15678aSNathan Whitehorn }
816e15678aSNathan Whitehorn
822118f387SNathan Whitehorn size_t
bootpart_size(const char * part_type)838befcf7bSNathan Whitehorn bootpart_size(const char *part_type)
848befcf7bSNathan Whitehorn {
851ee0f089SNathan Whitehorn size_t platlen = sizeof(platform);
861ee0f089SNathan Whitehorn if (strlen(platform) == 0)
871ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1);
881ee0f089SNathan Whitehorn
898befcf7bSNathan Whitehorn if (strcmp(part_type, "APM") == 0)
902118f387SNathan Whitehorn return (800*1024);
918befcf7bSNathan Whitehorn if (strcmp(part_type, "BSD") == 0) /* Nothing for nested */
928befcf7bSNathan Whitehorn return (0);
938befcf7bSNathan Whitehorn if (strcmp(platform, "chrp") == 0)
941ee0f089SNathan Whitehorn return (800*1024);
95bc4d122dSJustin Hibbits if (strcmp(platform, "ps3") == 0 || strcmp(platform, "powernv") == 0)
968befcf7bSNathan Whitehorn return (512*1024*1024);
97bc4d122dSJustin Hibbits if (strcmp(platform, "mpc85xx") == 0)
98bc4d122dSJustin Hibbits return (16*1024*1024);
992118f387SNathan Whitehorn return (0);
1002118f387SNathan Whitehorn }
1012118f387SNathan Whitehorn
1022118f387SNathan Whitehorn const char *
bootpart_type(const char * scheme,const char ** mountpoint)1038befcf7bSNathan Whitehorn bootpart_type(const char *scheme, const char **mountpoint)
1048befcf7bSNathan Whitehorn {
1051ee0f089SNathan Whitehorn size_t platlen = sizeof(platform);
1061ee0f089SNathan Whitehorn if (strlen(platform) == 0)
1071ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1);
1081ee0f089SNathan Whitehorn
1091ee0f089SNathan Whitehorn if (strcmp(platform, "chrp") == 0)
1101ee0f089SNathan Whitehorn return ("prep-boot");
1111ee0f089SNathan Whitehorn if (strcmp(platform, "powermac") == 0)
1121ee0f089SNathan Whitehorn return ("apple-boot");
113bc4d122dSJustin Hibbits if (strcmp(platform, "powernv") == 0 || strcmp(platform, "ps3") == 0) {
1148befcf7bSNathan Whitehorn *mountpoint = "/boot";
1158befcf7bSNathan Whitehorn if (strcmp(scheme, "GPT") == 0)
1168befcf7bSNathan Whitehorn return ("ms-basic-data");
1178befcf7bSNathan Whitehorn else if (strcmp(scheme, "MBR") == 0)
1188befcf7bSNathan Whitehorn return ("fat32");
1198befcf7bSNathan Whitehorn }
120bc4d122dSJustin Hibbits if (strcmp(platform, "mpc85xx") == 0) {
121bc4d122dSJustin Hibbits *mountpoint = "/boot/uboot";
122bc4d122dSJustin Hibbits return ("fat16");
123bc4d122dSJustin Hibbits }
1241ee0f089SNathan Whitehorn
1256b446ed5SNathan Whitehorn return ("freebsd-boot");
1266b446ed5SNathan Whitehorn }
1276b446ed5SNathan Whitehorn
1286b446ed5SNathan Whitehorn const char *
bootcode_path(const char * part_type)1292118f387SNathan Whitehorn bootcode_path(const char *part_type) {
1302118f387SNathan Whitehorn return (NULL);
1312118f387SNathan Whitehorn }
1322118f387SNathan Whitehorn
1332118f387SNathan Whitehorn const char *
partcode_path(const char * part_type,const char * fs_type)1346e15678aSNathan Whitehorn partcode_path(const char *part_type, const char *fs_type) {
1351ee0f089SNathan Whitehorn size_t platlen = sizeof(platform);
1361ee0f089SNathan Whitehorn if (strlen(platform) == 0)
1371ee0f089SNathan Whitehorn sysctlbyname("hw.platform", platform, &platlen, NULL, -1);
1381ee0f089SNathan Whitehorn
1392118f387SNathan Whitehorn if (strcmp(part_type, "APM") == 0)
1402118f387SNathan Whitehorn return ("/boot/boot1.hfs");
1418befcf7bSNathan Whitehorn if (strcmp(platform, "chrp") == 0)
142090dd246SNathan Whitehorn return ("/boot/boot1.elf");
1432118f387SNathan Whitehorn return (NULL);
1442118f387SNathan Whitehorn }
1452118f387SNathan Whitehorn
146