xref: /freebsd/usr.sbin/bsdinstall/partedit/partedit_x86.c (revision 6b446ed5ab83d741d3d20d896b0fb67bb952f647)
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 
29*6b446ed5SNathan Whitehorn #include <sys/types.h>
30*6b446ed5SNathan Whitehorn #include <sys/sysctl.h>
312118f387SNathan Whitehorn #include <string.h>
322118f387SNathan Whitehorn 
332118f387SNathan Whitehorn #include "partedit.h"
342118f387SNathan Whitehorn 
35*6b446ed5SNathan Whitehorn static char platform[255] = "BIOS"; /* XXX once sysctl exists, make this an empty string */
36*6b446ed5SNathan Whitehorn static const char *platform_sysctl = "hw.platform";
37*6b446ed5SNathan Whitehorn 
382118f387SNathan Whitehorn const char *
392118f387SNathan Whitehorn default_scheme(void) {
402118f387SNathan Whitehorn 	return ("GPT");
412118f387SNathan Whitehorn }
422118f387SNathan Whitehorn 
432118f387SNathan Whitehorn int
442118f387SNathan Whitehorn is_scheme_bootable(const char *part_type) {
45*6b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
46*6b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
47*6b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
48*6b446ed5SNathan Whitehorn 
492118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
502118f387SNathan Whitehorn 		return (1);
51*6b446ed5SNathan Whitehorn 	if (strcmp(platform, "BIOS") == 0) {
52*6b446ed5SNathan Whitehorn 		if (strcmp(part_type, "BSD") == 0)
53*6b446ed5SNathan Whitehorn 			return (1);
542118f387SNathan Whitehorn 		if (strcmp(part_type, "MBR") == 0)
552118f387SNathan Whitehorn 			return (1);
56*6b446ed5SNathan Whitehorn 	}
572118f387SNathan Whitehorn 
582118f387SNathan Whitehorn 	return (0);
592118f387SNathan Whitehorn }
602118f387SNathan Whitehorn 
612118f387SNathan Whitehorn size_t
62*6b446ed5SNathan Whitehorn bootpart_size(const char *scheme) {
63*6b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
64*6b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
65*6b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
662118f387SNathan Whitehorn 
672118f387SNathan Whitehorn 	/* No partcode except for GPT */
68*6b446ed5SNathan Whitehorn 	if (strcmp(scheme, "GPT") != 0)
69*6b446ed5SNathan Whitehorn 		return (0);
70*6b446ed5SNathan Whitehorn 
71*6b446ed5SNathan Whitehorn 	if (strcmp(platform, "BIOS") == 0)
72*6b446ed5SNathan Whitehorn 		return (64*1024);
73*6b446ed5SNathan Whitehorn 	else
74*6b446ed5SNathan Whitehorn 		return (800*1024);
75*6b446ed5SNathan Whitehorn 
762118f387SNathan Whitehorn 	return (0);
772118f387SNathan Whitehorn }
782118f387SNathan Whitehorn 
792118f387SNathan Whitehorn const char *
80*6b446ed5SNathan Whitehorn bootpart_type(const char *scheme) {
81*6b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
82*6b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
83*6b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
84*6b446ed5SNathan Whitehorn 
85*6b446ed5SNathan Whitehorn 	if (strcmp(platform, "EFI") == 0)
86*6b446ed5SNathan Whitehorn 		return ("efi");
87*6b446ed5SNathan Whitehorn 
88*6b446ed5SNathan Whitehorn 	return ("freebsd-boot");
89*6b446ed5SNathan Whitehorn }
90*6b446ed5SNathan Whitehorn 
91*6b446ed5SNathan Whitehorn const char *
922118f387SNathan Whitehorn bootcode_path(const char *part_type) {
93*6b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
94*6b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
95*6b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
96*6b446ed5SNathan Whitehorn 	if (strcmp(platform, "EFI") == 0)
97*6b446ed5SNathan Whitehorn 		return (NULL);
98*6b446ed5SNathan Whitehorn 
992118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
1002118f387SNathan Whitehorn 		return ("/boot/pmbr");
1012118f387SNathan Whitehorn 	if (strcmp(part_type, "MBR") == 0)
1022118f387SNathan Whitehorn 		return ("/boot/mbr");
1032118f387SNathan Whitehorn 	if (strcmp(part_type, "BSD") == 0)
1042118f387SNathan Whitehorn 		return ("/boot/boot");
1052118f387SNathan Whitehorn 
1062118f387SNathan Whitehorn 	return (NULL);
1072118f387SNathan Whitehorn }
1082118f387SNathan Whitehorn 
1092118f387SNathan Whitehorn const char *
1102118f387SNathan Whitehorn partcode_path(const char *part_type) {
111*6b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
112*6b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
113*6b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
114*6b446ed5SNathan Whitehorn 
115*6b446ed5SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0) {
116*6b446ed5SNathan Whitehorn 		if (strcmp(platform, "EFI") == 0)
117*6b446ed5SNathan Whitehorn 			return ("/boot/boot1.efifat");
118*6b446ed5SNathan Whitehorn 		else
1192118f387SNathan Whitehorn 			return ("/boot/gptboot");
120*6b446ed5SNathan Whitehorn 	}
1212118f387SNathan Whitehorn 
1222118f387SNathan Whitehorn 	/* No partcode except for GPT */
1232118f387SNathan Whitehorn 	return (NULL);
1242118f387SNathan Whitehorn }
1252118f387SNathan Whitehorn 
126