Path: blob/main/usr.sbin/bsdinstall/partedit/partedit_x86.c
105643 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 Nathan Whitehorn4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>29#include <sys/sysctl.h>30#include <string.h>3132#include "partedit.h"3334/* EFI partition size in bytes */35#define EFI_BOOTPART_SIZE (260 * 1024 * 1024)3637static const char *38x86_bootmethod(void)39{40static char fw[255] = "";41size_t len = sizeof(fw);42int error;4344if (strlen(fw) == 0) {45error = sysctlbyname("machdep.bootmethod", fw, &len, NULL, -1);46if (error != 0)47return ("BIOS");48}4950return (fw);51}5253const char *54default_scheme(void)55{56if (strcmp(x86_bootmethod(), "UEFI") == 0)57return ("GPT");58else59return ("MBR");60}6162int63is_scheme_bootable(const char *part_type)64{6566if (strcmp(part_type, "GPT") == 0)67return (1);68if (strcmp(x86_bootmethod(), "BIOS") == 0) {69if (strcmp(part_type, "BSD") == 0)70return (1);71if (strcmp(part_type, "MBR") == 0)72return (1);73}7475return (0);76}7778int79is_fs_bootable(const char *part_type, const char *fs)80{8182if (strcmp(fs, "freebsd-ufs") == 0)83return (1);8485if (strcmp(fs, "freebsd-zfs") == 0 &&86strcmp(part_type, "GPT") == 0 &&87strcmp(x86_bootmethod(), "BIOS") == 0)88return (1);8990return (0);91}9293size_t94bootpart_size(const char *scheme)95{9697/* No partcode except for GPT */98if (strcmp(scheme, "GPT") != 0)99return (0);100101if (strcmp(x86_bootmethod(), "BIOS") == 0)102return (512*1024);103else104return (EFI_BOOTPART_SIZE);105106return (0);107}108109const char *110bootpart_type(const char *scheme, const char **mountpoint)111{112113if (strcmp(x86_bootmethod(), "UEFI") == 0) {114*mountpoint = "/boot/efi";115return ("efi");116}117118return ("freebsd-boot");119}120121const char *122bootcode_path(const char *part_type)123{124125if (strcmp(x86_bootmethod(), "UEFI") == 0)126return (NULL);127128if (strcmp(part_type, "GPT") == 0)129return ("/boot/pmbr");130if (strcmp(part_type, "MBR") == 0)131return ("/boot/mbr");132if (strcmp(part_type, "BSD") == 0)133return ("/boot/boot");134135return (NULL);136}137138const char *139partcode_path(const char *part_type, const char *fs_type)140{141142if (strcmp(part_type, "GPT") == 0 && strcmp(x86_bootmethod(), "UEFI") != 0) {143if (strcmp(fs_type, "zfs") == 0)144return ("/boot/gptzfsboot");145else146return ("/boot/gptboot");147}148149/* No partcode except for non-UEFI GPT */150return (NULL);151}152153154155