| /* |
| * Copyright 2015-2025 Rivoreo |
| * All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in the |
| * documentation and/or other materials provided with the distribution. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| * SUCH DAMAGE. |
| */ |
| |
| #include "mfiutil.h" |
| #include <err.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <string.h> |
| |
| static int set_boolean_prop(const char *prop_name, uint8_t *value_p, const char *value_s) { |
| if(strcasecmp(value_s, "off") == 0) *value_p = 0; |
| else if(strcasecmp(value_s, "on") == 0) *value_p = 1; |
| else { |
| warnx("Invalid value '%s' for '%s'", value_s, prop_name); |
| return -1; |
| } |
| return 0; |
| } |
| |
| static int set_boot_props(int ac, char **av) { |
| if(ac < 2) { |
| warnx("boot: at least one property is required"); |
| return EINVAL; |
| } |
| int fd = mfi_open(mfi_unit, O_RDWR); |
| if(fd == -1) { |
| int e = errno; |
| warn("mfi_open"); |
| return e; |
| } |
| struct mfi_bios_data data; |
| if(mfi_ctrl_get_bios_data(fd, &data, NULL) < 0) { |
| int e = errno; |
| warn("Failed to get controller BIOS data"); |
| close(fd); |
| return e; |
| } |
| struct mfi_bios_data orig_data = data; |
| int i = 1; |
| do { |
| if(strcmp(av[i], "enable") == 0) data.do_not_int_13 = 0; |
| else if(strcmp(av[i], "disable") == 0) data.do_not_int_13 = 1; |
| else if(strcmp(av[i], "drive") == 0) { |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing drive or 'none'"); |
| return EINVAL; |
| } |
| if(strcmp(av[i], "none") == 0) { |
| data.boot_target_id = 65535; |
| data.boot_syspd = 0; |
| } else { |
| int e = mfi_lookup_drive(fd, av[i], &data.boot_target_id); |
| if(e) { |
| close(fd); |
| return e; |
| } |
| data.boot_syspd = 1; |
| } |
| } else if(strcmp(av[i], "volume") == 0) { |
| uint8_t target_id; |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing volume"); |
| return EINVAL; |
| } |
| if(mfi_lookup_volume(fd, av[i], &target_id) < 0) { |
| int e = errno; |
| warn("Invalid volume %s specified", av[i]); |
| close(fd); |
| return e; |
| } |
| data.boot_target_id = target_id; |
| data.boot_syspd = 0; |
| } else if(strcmp(av[i], "on-error") == 0) { |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing value"); |
| return EINVAL; |
| } |
| if(strcasecmp(av[i], "stop") == 0) data.continue_on_error = 0; |
| else if(strcasecmp(av[i], "warn") == 0) data.continue_on_error = 1; |
| else if(strcasecmp(av[i], "ignore") == 0) data.continue_on_error = 2; |
| else { |
| close(fd); |
| warnx("Invalid value '%s' for 'on-error'", av[i]); |
| return EINVAL; |
| } |
| } else if(strcmp(av[i], "verbose") == 0) { |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing value"); |
| return EINVAL; |
| } |
| if(set_boolean_prop(av[i - 1], &data.verbose, av[i]) < 0) { |
| close(fd); |
| return EINVAL; |
| } |
| } else if(strcmp(av[i], "expose-all-drives") == 0) { |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing value"); |
| return EINVAL; |
| } |
| if(set_boolean_prop(av[i - 1], &data.expose_all_drives, av[i]) < 0) { |
| close(fd); |
| return EINVAL; |
| } |
| } else if(strcmp(av[i], "autoselect-boot-volume") == 0) { |
| if(++i >= ac) { |
| close(fd); |
| warnx("Missing value"); |
| return EINVAL; |
| } |
| if(set_boolean_prop(av[i - 1], &data.auto_select_boot_ld, av[i]) < 0) { |
| close(fd); |
| return EINVAL; |
| } |
| } else { |
| close(fd); |
| warnx("Invalid operand %s", av[i]); |
| return EINVAL; |
| } |
| } while(++i < ac); |
| if(memcmp(&data, &orig_data, sizeof(struct mfi_bios_data)) == 0) { |
| close(fd); |
| warnx("No changes to boot configuration"); |
| return 0; |
| } |
| if(mfi_ctrl_set_bios_data(fd, &data, NULL) < 0) { |
| int e = errno; |
| warn("Failed to set controller BIOS data"); |
| close(fd); |
| return e; |
| } |
| close(fd); |
| return 0; |
| } |
| MFI_COMMAND(top, boot, set_boot_props); |