2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * This file contains high level functions used by multiple utilities.
2N/A */
2N/A
2N/A#include "libscf_impl.h"
2N/A
2N/A#include <assert.h>
2N/A#include <libuutil.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <sys/uadmin.h>
2N/A#include <sys/utsname.h>
2N/A
2N/A#ifdef __x86
2N/A#include <smbios.h>
2N/A
2N/A/*
2N/A * Check whether the platform is on the fastreboot_blacklist.
2N/A * Return 1 if the platform has been blacklisted, 0 otherwise.
2N/A */
2N/Astatic int
2N/Ascf_is_fb_blacklisted(void)
2N/A{
2N/A smbios_hdl_t *shp;
2N/A smbios_system_t sys;
2N/A smbios_info_t info;
2N/A
2N/A id_t id;
2N/A int err;
2N/A int i;
2N/A
2N/A scf_simple_prop_t *prop = NULL;
2N/A ssize_t numvals;
2N/A char *platform_name;
2N/A
2N/A int blacklisted = 0;
2N/A
2N/A /*
2N/A * If there's no SMBIOS, assume it's blacklisted.
2N/A */
2N/A if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL)
2N/A return (1);
2N/A
2N/A /*
2N/A * If we can't read system info, assume it's blacklisted.
2N/A */
2N/A if ((id = smbios_info_system(shp, &sys)) == SMB_ERR ||
2N/A smbios_info_common(shp, id, &info) == SMB_ERR) {
2N/A blacklisted = 1;
2N/A goto fb_out;
2N/A }
2N/A
2N/A /*
2N/A * If we can't read the "platforms" property from property group
2N/A * BOOT_CONFIG_PG_FBBLACKLIST, assume no platforms have
2N/A * been blacklisted.
2N/A */
2N/A if ((prop = scf_simple_prop_get(NULL, FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_FBBLACKLIST, "platforms")) == NULL)
2N/A goto fb_out;
2N/A
2N/A numvals = scf_simple_prop_numvalues(prop);
2N/A
2N/A for (i = 0; i < numvals; i++) {
2N/A platform_name = scf_simple_prop_next_astring(prop);
2N/A if (platform_name == NULL)
2N/A break;
2N/A if (strcmp(platform_name, info.smbi_product) == 0) {
2N/A blacklisted = 1;
2N/A break;
2N/A }
2N/A }
2N/A
2N/Afb_out:
2N/A smbios_close(shp);
2N/A scf_simple_prop_free(prop);
2N/A
2N/A return (blacklisted);
2N/A}
2N/A
2N/A/*
2N/A * Add or get a property group given an FMRI.
2N/A * Return SCF_SUCCESS on success, SCF_FAILED on failure.
2N/A */
2N/Astatic int
2N/Ascf_fmri_pg_get_or_add(const char *fmri, const char *pgname,
2N/A const char *pgtype, uint32_t pgflags, int add)
2N/A{
2N/A scf_handle_t *handle = NULL;
2N/A scf_instance_t *inst = NULL;
2N/A int rc = SCF_FAILED;
2N/A int error;
2N/A
2N/A if ((handle = scf_handle_create(SCF_VERSION)) == NULL ||
2N/A scf_handle_bind(handle) != 0 ||
2N/A (inst = scf_instance_create(handle)) == NULL ||
2N/A scf_handle_decode_fmri(handle, fmri, NULL, NULL,
2N/A inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS)
2N/A goto scferror;
2N/A
2N/A if (add) {
2N/A rc = scf_instance_add_pg(inst, pgname, pgtype, pgflags, NULL);
2N/A /*
2N/A * If the property group already exists, return SCF_SUCCESS.
2N/A */
2N/A if (rc != SCF_SUCCESS && scf_error() == SCF_ERROR_EXISTS)
2N/A rc = SCF_SUCCESS;
2N/A } else {
2N/A rc = scf_instance_get_pg(inst, pgname, NULL);
2N/A }
2N/A
2N/Ascferror:
2N/A if (rc != SCF_SUCCESS)
2N/A error = scf_error();
2N/A
2N/A scf_instance_destroy(inst);
2N/A if (handle)
2N/A (void) scf_handle_unbind(handle);
2N/A scf_handle_destroy(handle);
2N/A
2N/A if (rc != SCF_SUCCESS)
2N/A (void) scf_set_error(error);
2N/A
2N/A return (rc);
2N/A}
2N/A#endif /* __x86 */
2N/A
2N/A/*
2N/A * Get config properties from svc:/system/boot-config:default.
2N/A * It prints errors with uu_warn().
2N/A */
2N/Avoid
2N/Ascf_get_boot_config(uint8_t *boot_config)
2N/A{
2N/A assert(boot_config);
2N/A *boot_config = 0;
2N/A
2N/A {
2N/A /*
2N/A * Property vector for BOOT_CONFIG_PG_PARAMS property group.
2N/A */
2N/A scf_propvec_t ua_boot_config[] = {
2N/A { FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, B_FALSE,
2N/A NULL, UA_FASTREBOOT_DEFAULT },
2N/A { FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, B_FALSE,
2N/A NULL, UA_FASTREBOOT_ONPANIC },
2N/A { NULL }
2N/A };
2N/A scf_propvec_t *prop;
2N/A uint64_t prop64_val = 0;
2N/A
2N/A for (prop = ua_boot_config; prop->pv_prop != NULL; prop++)
2N/A prop->pv_ptr = &prop64_val;
2N/A
2N/A prop = NULL;
2N/A if (scf_read_propvec(FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_PARAMS,
2N/A B_TRUE, ua_boot_config, &prop) != SCF_FAILED) {
2N/A
2N/A assert((prop64_val & ~0xFFULL) == 0);
2N/A *boot_config = (uint8_t)prop64_val;
2N/A#ifdef __x86
2N/A /*
2N/A * Unset both flags if the platform has been
2N/A * blacklisted.
2N/A */
2N/A if (scf_is_fb_blacklisted())
2N/A *boot_config &= ~(UA_FASTREBOOT_DEFAULT |
2N/A UA_FASTREBOOT_ONPANIC);
2N/A#endif /* __x86 */
2N/A return;
2N/A }
2N/A
2N/A assert((prop64_val & ~0xFFULL) == 0);
2N/A *boot_config = (uint8_t)prop64_val;
2N/A#if defined(FASTREBOOT_DEBUG)
2N/A if (prop != NULL) {
2N/A (void) uu_warn("Service %s property '%s/%s' "
2N/A "not found.\n", FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_PARAMS, prop->pv_prop);
2N/A } else {
2N/A (void) uu_warn("Unable to read service %s "
2N/A "property '%s': %s\n", FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_PARAMS, scf_strerror(scf_error()));
2N/A }
2N/A#endif /* FASTREBOOT_DEBUG */
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Get or set properties in non-persistent "config_ovr" property group
2N/A * in svc:/system/boot-config:default.
2N/A * It prints errors with uu_warn().
2N/A */
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Ascf_getset_boot_config_ovr(int set, uint8_t *boot_config_ovr)
2N/A{
2N/A int rc = SCF_SUCCESS;
2N/A
2N/A assert(boot_config_ovr);
2N/A
2N/A#ifndef __x86
2N/A return (rc);
2N/A#else
2N/A {
2N/A /*
2N/A * Property vector for BOOT_CONFIG_PG_OVR property group.
2N/A */
2N/A scf_propvec_t ua_boot_config_ovr[] = {
2N/A { FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, B_FALSE,
2N/A NULL, UA_FASTREBOOT_DEFAULT },
2N/A { FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, B_FALSE,
2N/A NULL, UA_FASTREBOOT_ONPANIC },
2N/A { NULL }
2N/A };
2N/A scf_propvec_t *prop;
2N/A uint64_t prop64_val = *boot_config_ovr;
2N/A
2N/A rc = scf_fmri_pg_get_or_add(FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_OVR, SCF_GROUP_APPLICATION,
2N/A SCF_PG_FLAG_NONPERSISTENT, set);
2N/A
2N/A if (rc != SCF_SUCCESS) {
2N/A#if defined(FASTREBOOT_DEBUG)
2N/A if (set)
2N/A (void) uu_warn("Unable to add service %s "
2N/A "property group '%s'\n",
2N/A FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR);
2N/A#endif /* FASTREBOOT_DEBUG */
2N/A return (rc);
2N/A }
2N/A
2N/A for (prop = ua_boot_config_ovr; prop->pv_prop != NULL; prop++)
2N/A prop->pv_ptr = &prop64_val;
2N/A prop = NULL;
2N/A
2N/A if (set)
2N/A rc = scf_write_propvec(FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_OVR, ua_boot_config_ovr, &prop);
2N/A else
2N/A rc = scf_read_propvec(FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_OVR, B_FALSE, ua_boot_config_ovr,
2N/A &prop);
2N/A
2N/A assert((prop64_val & ~0xFFULL) == 0);
2N/A *boot_config_ovr = (uint8_t)prop64_val;
2N/A
2N/A#if defined(FASTREBOOT_DEBUG)
2N/A if (rc != SCF_SUCCESS) {
2N/A if (prop != NULL) {
2N/A (void) uu_warn("Service %s property '%s/%s' "
2N/A "not found.\n", FMRI_BOOT_CONFIG,
2N/A BOOT_CONFIG_PG_OVR, prop->pv_prop);
2N/A } else {
2N/A (void) uu_warn("Unable to %s service %s "
2N/A "property '%s': %s\n", set ? "set" : "get",
2N/A FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR,
2N/A scf_strerror(scf_error()));
2N/A }
2N/A }
2N/A#endif /* FASTREBOOT_DEBUG */
2N/A
2N/A if (set)
2N/A (void) smf_refresh_instance(FMRI_BOOT_CONFIG);
2N/A
2N/A return (rc);
2N/A
2N/A }
2N/A#endif /* __x86 */
2N/A}
2N/A
2N/A/*
2N/A * Get values of properties in non-persistent "config_ovr" property group.
2N/A */
2N/Avoid
2N/Ascf_get_boot_config_ovr(uint8_t *boot_config_ovr)
2N/A{
2N/A (void) scf_getset_boot_config_ovr(B_FALSE, boot_config_ovr);
2N/A}
2N/A
2N/A/*
2N/A * Set value of "config_ovr/fastreboot_default".
2N/A */
2N/Aint
2N/Ascf_fastreboot_default_set_transient(boolean_t value)
2N/A{
2N/A uint8_t boot_config_ovr = 0;
2N/A
2N/A if (value == B_TRUE)
2N/A boot_config_ovr = UA_FASTREBOOT_DEFAULT | UA_FASTREBOOT_ONPANIC;
2N/A
2N/A return (scf_getset_boot_config_ovr(B_TRUE, &boot_config_ovr));
2N/A}
2N/A
2N/A/*
2N/A * Check whether Fast Reboot is the default operating mode.
2N/A * Return 0 if
2N/A * 1. the platform is xVM
2N/A * or
2N/A * 2. svc:/system/boot-config:default service doesn't exist,
2N/A * or
2N/A * 3. property "config/fastreboot_default" doesn't exist,
2N/A * or
2N/A * 4. value of property "config/fastreboot_default" is set to "false"
2N/A * and "config_ovr/fastreboot_default" is not set to "true",
2N/A * or
2N/A * 5. the platform has been blacklisted.
2N/A * or
2N/A * 6. value of property "config_ovr/fastreboot_default" is set to "false".
2N/A * Return non-zero otherwise.
2N/A */
2N/Aint
2N/Ascf_is_fastboot_default(void)
2N/A{
2N/A uint8_t boot_config = 0, boot_config_ovr;
2N/A char procbuf[SYS_NMLN];
2N/A
2N/A /*
2N/A * If we are on xVM, do not fast reboot by default.
2N/A */
2N/A if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1 ||
2N/A strcmp(procbuf, "i86xpv") == 0)
2N/A return (0);
2N/A
2N/A /*
2N/A * Get property values from "config" property group
2N/A */
2N/A scf_get_boot_config(&boot_config);
2N/A
2N/A /*
2N/A * Get property values from non-persistent "config_ovr" property group
2N/A */
2N/A boot_config_ovr = boot_config;
2N/A scf_get_boot_config_ovr(&boot_config_ovr);
2N/A
2N/A return (boot_config_ovr & UA_FASTREBOOT_DEFAULT);
2N/A}