xref: /titanic_50/usr/src/lib/libscf/common/highlevel.c (revision d7ddd43c70ebe97a1118be9f663a54d0d1d89fe6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This file contains high level functions used by multiple utilities.
29  */
30 
31 #include "libscf_impl.h"
32 
33 #include <assert.h>
34 #include <libuutil.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/systeminfo.h>
38 #include <sys/uadmin.h>
39 #include <sys/utsname.h>
40 
41 #ifdef	__x86
42 #include <smbios.h>
43 
44 /*
45  * Check whether the platform is on the fastreboot_blacklist.
46  * Return 1 if the platform has been blacklisted, 0 otherwise.
47  */
48 static int
49 scf_is_fb_blacklisted(void)
50 {
51 	smbios_hdl_t *shp;
52 	smbios_system_t sys;
53 	smbios_info_t info;
54 
55 	id_t id;
56 	int err;
57 	int i;
58 
59 	scf_simple_prop_t *prop = NULL;
60 	ssize_t numvals;
61 	char *platform_name;
62 
63 	int blacklisted = 0;
64 
65 	/*
66 	 * If there's no SMBIOS, assume it's blacklisted.
67 	 */
68 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL)
69 		return (1);
70 
71 	/*
72 	 * If we can't read system info, assume it's blacklisted.
73 	 */
74 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR ||
75 	    smbios_info_common(shp, id, &info) == SMB_ERR) {
76 		blacklisted = 1;
77 		goto fb_out;
78 	}
79 
80 	/*
81 	 * If we can't read the "platforms" property from property group
82 	 * BOOT_CONFIG_PG_FBBLACKLIST, assume no platforms have
83 	 * been blacklisted.
84 	 */
85 	if ((prop = scf_simple_prop_get(NULL, FMRI_BOOT_CONFIG,
86 	    BOOT_CONFIG_PG_FBBLACKLIST, "platforms")) == NULL)
87 		goto fb_out;
88 
89 	numvals = scf_simple_prop_numvalues(prop);
90 
91 	for (i = 0; i < numvals; i++) {
92 		platform_name = scf_simple_prop_next_astring(prop);
93 		if (platform_name == NULL)
94 			break;
95 		if (strcmp(platform_name, info.smbi_product) == 0) {
96 			blacklisted = 1;
97 			break;
98 		}
99 	}
100 
101 fb_out:
102 	smbios_close(shp);
103 	scf_simple_prop_free(prop);
104 
105 	return (blacklisted);
106 }
107 #endif	/* __x86 */
108 
109 /*
110  * Get config properties from svc:/system/boot-config:default.
111  * It prints errors with uu_warn().
112  */
113 void
114 scf_get_boot_config(uint8_t *boot_config)
115 {
116 	assert(boot_config);
117 	*boot_config = 0;
118 
119 #ifndef	__x86
120 	return;
121 #else
122 	{
123 		/*
124 		 * Property vector for BOOT_CONFIG_PG_PARAMS property group.
125 		 */
126 		scf_propvec_t ua_boot_config[] = {
127 			{ "fastreboot_default", NULL, SCF_TYPE_BOOLEAN, NULL,
128 			    UA_FASTREBOOT_DEFAULT },
129 			{ FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, NULL,
130 			    UA_FASTREBOOT_ONPANIC },
131 			{ NULL }
132 		};
133 		scf_propvec_t	*prop;
134 
135 		for (prop = ua_boot_config; prop->pv_prop != NULL; prop++)
136 			prop->pv_ptr = boot_config;
137 		prop = NULL;
138 		if (scf_read_propvec(FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_PARAMS,
139 		    B_TRUE, ua_boot_config, &prop) != SCF_FAILED) {
140 			/*
141 			 * Unset both flags if the platform has been
142 			 * blacklisted.
143 			 */
144 			if (scf_is_fb_blacklisted())
145 				*boot_config &= ~(UA_FASTREBOOT_DEFAULT |
146 				    UA_FASTREBOOT_ONPANIC);
147 			return;
148 		}
149 #if defined(FASTREBOOT_DEBUG)
150 		if (prop != NULL) {
151 			(void) uu_warn("Service %s property '%s/%s' "
152 			    "not found.\n", FMRI_BOOT_CONFIG,
153 			    BOOT_CONFIG_PG_PARAMS, prop->pv_prop);
154 		} else {
155 			(void) uu_warn("Unable to read service %s "
156 			    "property '%s': %s\n", FMRI_BOOT_CONFIG,
157 			    BOOT_CONFIG_PG_PARAMS, scf_strerror(scf_error()));
158 		}
159 #endif	/* FASTREBOOT_DEBUG */
160 	}
161 #endif	/* __x86 */
162 }
163 
164 /*
165  * Check whether Fast Reboot is the default operating mode.
166  * Return 0 if
167  *   1. the platform is xVM
168  * or
169  *   2. svc:/system/boot-config:default service doesn't exist,
170  * or
171  *   3. property "fastreboot_default" doesn't exist,
172  * or
173  *   4. value of property "fastreboot_default" is set to 0.
174  * or
175  *   5. the platform has been blacklisted.
176  * Return non-zero otherwise.
177  */
178 int
179 scf_is_fastboot_default(void)
180 {
181 	uint8_t	boot_config = 0;
182 	char procbuf[SYS_NMLN];
183 
184 	/*
185 	 * If we are on xVM, do not fast reboot by default.
186 	 */
187 	if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1 ||
188 	    strcmp(procbuf, "i86xpv") == 0)
189 		return (0);
190 
191 	scf_get_boot_config(&boot_config);
192 	return (boot_config & UA_FASTREBOOT_DEFAULT);
193 }
194