xref: /freebsd/usr.sbin/bsdinstall/partedit/partedit_x86.c (revision 963ae465d5039f71a7a494d7ca0f789bd7a16971)
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 
35235591a5SNathan Whitehorn static char platform[255] = "";
36235591a5SNathan Whitehorn static const char *platform_sysctl = "machdep.bootmethod";
376b446ed5SNathan 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) {
456b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
466b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
476b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
486b446ed5SNathan Whitehorn 
492118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
502118f387SNathan Whitehorn 		return (1);
516b446ed5SNathan Whitehorn 	if (strcmp(platform, "BIOS") == 0) {
526b446ed5SNathan Whitehorn 		if (strcmp(part_type, "BSD") == 0)
536b446ed5SNathan Whitehorn 			return (1);
542118f387SNathan Whitehorn 		if (strcmp(part_type, "MBR") == 0)
552118f387SNathan Whitehorn 			return (1);
566b446ed5SNathan Whitehorn 	}
572118f387SNathan Whitehorn 
582118f387SNathan Whitehorn 	return (0);
592118f387SNathan Whitehorn }
602118f387SNathan Whitehorn 
612118f387SNathan Whitehorn size_t
626b446ed5SNathan Whitehorn bootpart_size(const char *scheme) {
636b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
646b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
656b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
662118f387SNathan Whitehorn 
672118f387SNathan Whitehorn 	/* No partcode except for GPT */
686b446ed5SNathan Whitehorn 	if (strcmp(scheme, "GPT") != 0)
696b446ed5SNathan Whitehorn 		return (0);
706b446ed5SNathan Whitehorn 
716b446ed5SNathan Whitehorn 	if (strcmp(platform, "BIOS") == 0)
72*963ae465SNathan Whitehorn 		return (512*1024);
736b446ed5SNathan Whitehorn 	else
746b446ed5SNathan Whitehorn 		return (800*1024);
756b446ed5SNathan Whitehorn 
762118f387SNathan Whitehorn 	return (0);
772118f387SNathan Whitehorn }
782118f387SNathan Whitehorn 
792118f387SNathan Whitehorn const char *
806b446ed5SNathan Whitehorn bootpart_type(const char *scheme) {
816b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
826b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
836b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
846b446ed5SNathan Whitehorn 
85235591a5SNathan Whitehorn 	if (strcmp(platform, "UEFI") == 0)
866b446ed5SNathan Whitehorn 		return ("efi");
876b446ed5SNathan Whitehorn 
886b446ed5SNathan Whitehorn 	return ("freebsd-boot");
896b446ed5SNathan Whitehorn }
906b446ed5SNathan Whitehorn 
916b446ed5SNathan Whitehorn const char *
922118f387SNathan Whitehorn bootcode_path(const char *part_type) {
936b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
946b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
956b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
96235591a5SNathan Whitehorn 	if (strcmp(platform, "UEFI") == 0)
976b446ed5SNathan Whitehorn 		return (NULL);
986b446ed5SNathan 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) {
1116b446ed5SNathan Whitehorn 	size_t platlen = sizeof(platform);
1126b446ed5SNathan Whitehorn 	if (strlen(platform) == 0)
1136b446ed5SNathan Whitehorn 		sysctlbyname(platform_sysctl, platform, &platlen, NULL, -1);
1146b446ed5SNathan Whitehorn 
1156b446ed5SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0) {
116235591a5SNathan Whitehorn 		if (strcmp(platform, "UEFI") == 0)
1176b446ed5SNathan Whitehorn 			return ("/boot/boot1.efifat");
1186b446ed5SNathan Whitehorn 		else
1192118f387SNathan Whitehorn 			return ("/boot/gptboot");
1206b446ed5SNathan Whitehorn 	}
1212118f387SNathan Whitehorn 
1222118f387SNathan Whitehorn 	/* No partcode except for GPT */
1232118f387SNathan Whitehorn 	return (NULL);
1242118f387SNathan Whitehorn }
1252118f387SNathan Whitehorn 
126