xref: /titanic_52/usr/src/lib/libscf/common/highlevel.c (revision 4196e26398ab7019943a8f276006fa66937b4425)
1753a6d45SSherry Moore /*
2753a6d45SSherry Moore  * CDDL HEADER START
3753a6d45SSherry Moore  *
4753a6d45SSherry Moore  * The contents of this file are subject to the terms of the
5753a6d45SSherry Moore  * Common Development and Distribution License (the "License").
6753a6d45SSherry Moore  * You may not use this file except in compliance with the License.
7753a6d45SSherry Moore  *
8753a6d45SSherry Moore  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9753a6d45SSherry Moore  * or http://www.opensolaris.org/os/licensing.
10753a6d45SSherry Moore  * See the License for the specific language governing permissions
11753a6d45SSherry Moore  * and limitations under the License.
12753a6d45SSherry Moore  *
13753a6d45SSherry Moore  * When distributing Covered Code, include this CDDL HEADER in each
14753a6d45SSherry Moore  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15753a6d45SSherry Moore  * If applicable, add the following below this CDDL HEADER, with the
16753a6d45SSherry Moore  * fields enclosed by brackets "[]" replaced with your own identifying
17753a6d45SSherry Moore  * information: Portions Copyright [yyyy] [name of copyright owner]
18753a6d45SSherry Moore  *
19753a6d45SSherry Moore  * CDDL HEADER END
20753a6d45SSherry Moore  */
21753a6d45SSherry Moore 
22753a6d45SSherry Moore /*
23753a6d45SSherry Moore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24753a6d45SSherry Moore  * Use is subject to license terms.
25753a6d45SSherry Moore  */
26753a6d45SSherry Moore 
27753a6d45SSherry Moore /*
28753a6d45SSherry Moore  * This file contains high level functions used by multiple utilities.
29753a6d45SSherry Moore  */
30753a6d45SSherry Moore 
31753a6d45SSherry Moore #include "libscf_impl.h"
32753a6d45SSherry Moore 
33753a6d45SSherry Moore #include <assert.h>
34753a6d45SSherry Moore #include <libuutil.h>
35753a6d45SSherry Moore #include <string.h>
36753a6d45SSherry Moore #include <stdlib.h>
37753a6d45SSherry Moore #include <sys/systeminfo.h>
38753a6d45SSherry Moore #include <sys/uadmin.h>
39753a6d45SSherry Moore #include <sys/utsname.h>
40753a6d45SSherry Moore 
41753a6d45SSherry Moore #ifdef	__x86
42753a6d45SSherry Moore #include <smbios.h>
43753a6d45SSherry Moore 
44753a6d45SSherry Moore /*
45753a6d45SSherry Moore  * Check whether the platform is on the fastreboot_blacklist.
46753a6d45SSherry Moore  * Return 1 if the platform has been blacklisted, 0 otherwise.
47753a6d45SSherry Moore  */
48753a6d45SSherry Moore static int
49753a6d45SSherry Moore scf_is_fb_blacklisted(void)
50753a6d45SSherry Moore {
51753a6d45SSherry Moore 	smbios_hdl_t *shp;
52753a6d45SSherry Moore 	smbios_system_t sys;
53753a6d45SSherry Moore 	smbios_info_t info;
54753a6d45SSherry Moore 
55753a6d45SSherry Moore 	id_t id;
56753a6d45SSherry Moore 	int err;
57753a6d45SSherry Moore 	int i;
58753a6d45SSherry Moore 
59753a6d45SSherry Moore 	scf_simple_prop_t *prop = NULL;
60753a6d45SSherry Moore 	ssize_t numvals;
61753a6d45SSherry Moore 	char *platform_name;
62753a6d45SSherry Moore 
63753a6d45SSherry Moore 	int blacklisted = 0;
64753a6d45SSherry Moore 
65753a6d45SSherry Moore 	/*
66753a6d45SSherry Moore 	 * If there's no SMBIOS, assume it's blacklisted.
67753a6d45SSherry Moore 	 */
68753a6d45SSherry Moore 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL)
69753a6d45SSherry Moore 		return (1);
70753a6d45SSherry Moore 
71753a6d45SSherry Moore 	/*
72753a6d45SSherry Moore 	 * If we can't read system info, assume it's blacklisted.
73753a6d45SSherry Moore 	 */
74753a6d45SSherry Moore 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR ||
75753a6d45SSherry Moore 	    smbios_info_common(shp, id, &info) == SMB_ERR) {
76753a6d45SSherry Moore 		blacklisted = 1;
77753a6d45SSherry Moore 		goto fb_out;
78753a6d45SSherry Moore 	}
79753a6d45SSherry Moore 
80753a6d45SSherry Moore 	/*
81753a6d45SSherry Moore 	 * If we can't read the "platforms" property from property group
82753a6d45SSherry Moore 	 * BOOT_CONFIG_PG_FBBLACKLIST, assume no platforms have
83753a6d45SSherry Moore 	 * been blacklisted.
84753a6d45SSherry Moore 	 */
85753a6d45SSherry Moore 	if ((prop = scf_simple_prop_get(NULL, FMRI_BOOT_CONFIG,
86753a6d45SSherry Moore 	    BOOT_CONFIG_PG_FBBLACKLIST, "platforms")) == NULL)
87753a6d45SSherry Moore 		goto fb_out;
88753a6d45SSherry Moore 
89753a6d45SSherry Moore 	numvals = scf_simple_prop_numvalues(prop);
90753a6d45SSherry Moore 
91753a6d45SSherry Moore 	for (i = 0; i < numvals; i++) {
92753a6d45SSherry Moore 		platform_name = scf_simple_prop_next_astring(prop);
93753a6d45SSherry Moore 		if (platform_name == NULL)
94753a6d45SSherry Moore 			break;
95753a6d45SSherry Moore 		if (strcmp(platform_name, info.smbi_product) == 0) {
96753a6d45SSherry Moore 			blacklisted = 1;
97753a6d45SSherry Moore 			break;
98753a6d45SSherry Moore 		}
99753a6d45SSherry Moore 	}
100753a6d45SSherry Moore 
101753a6d45SSherry Moore fb_out:
102753a6d45SSherry Moore 	smbios_close(shp);
103753a6d45SSherry Moore 	scf_simple_prop_free(prop);
104753a6d45SSherry Moore 
105753a6d45SSherry Moore 	return (blacklisted);
106753a6d45SSherry Moore }
107*4196e263SSherry Moore 
108*4196e263SSherry Moore /*
109*4196e263SSherry Moore  * Add or get a property group given an FMRI.
110*4196e263SSherry Moore  * Return SCF_SUCCESS on success, SCF_FAILED on failure.
111*4196e263SSherry Moore  */
112*4196e263SSherry Moore static int
113*4196e263SSherry Moore scf_fmri_pg_get_or_add(const char *fmri, const char *pgname,
114*4196e263SSherry Moore     const char *pgtype, uint32_t pgflags, int add)
115*4196e263SSherry Moore {
116*4196e263SSherry Moore 	scf_handle_t	*handle = NULL;
117*4196e263SSherry Moore 	scf_instance_t	*inst = NULL;
118*4196e263SSherry Moore 	int		rc = SCF_FAILED;
119*4196e263SSherry Moore 	int		error = SCF_ERROR_NONE;
120*4196e263SSherry Moore 
121*4196e263SSherry Moore 	if ((handle = scf_handle_create(SCF_VERSION)) == NULL ||
122*4196e263SSherry Moore 	    scf_handle_bind(handle) != 0 ||
123*4196e263SSherry Moore 	    (inst = scf_instance_create(handle)) == NULL ||
124*4196e263SSherry Moore 	    scf_handle_decode_fmri(handle, fmri, NULL, NULL,
125*4196e263SSherry Moore 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS)
126*4196e263SSherry Moore 		goto scferror;
127*4196e263SSherry Moore 
128*4196e263SSherry Moore 	if (add) {
129*4196e263SSherry Moore 		rc = scf_instance_add_pg(inst, pgname, pgtype, pgflags, NULL);
130*4196e263SSherry Moore 		/*
131*4196e263SSherry Moore 		 * If the property group already exists, return SCF_SUCCESS.
132*4196e263SSherry Moore 		 */
133*4196e263SSherry Moore 		if (rc != SCF_SUCCESS && scf_error() == SCF_ERROR_EXISTS) {
134*4196e263SSherry Moore 			(void) scf_set_error(SCF_ERROR_NONE);
135*4196e263SSherry Moore 			rc = SCF_SUCCESS;
136*4196e263SSherry Moore 		}
137*4196e263SSherry Moore 	} else {
138*4196e263SSherry Moore 		rc = scf_instance_get_pg(inst, pgname, NULL);
139*4196e263SSherry Moore 	}
140*4196e263SSherry Moore 
141*4196e263SSherry Moore scferror:
142*4196e263SSherry Moore 	error = scf_error();
143*4196e263SSherry Moore 
144*4196e263SSherry Moore 	scf_instance_destroy(inst);
145*4196e263SSherry Moore 	if (handle)
146*4196e263SSherry Moore 		(void) scf_handle_unbind(handle);
147*4196e263SSherry Moore 	scf_handle_destroy(handle);
148*4196e263SSherry Moore 
149*4196e263SSherry Moore 	if (error != SCF_ERROR_NONE) {
150*4196e263SSherry Moore 		(void) scf_set_error(error);
151*4196e263SSherry Moore 		rc = SCF_FAILED;
152*4196e263SSherry Moore 	}
153*4196e263SSherry Moore 	return (rc);
154*4196e263SSherry Moore }
155753a6d45SSherry Moore #endif	/* __x86 */
156753a6d45SSherry Moore 
157753a6d45SSherry Moore /*
158753a6d45SSherry Moore  * Get config properties from svc:/system/boot-config:default.
159753a6d45SSherry Moore  * It prints errors with uu_warn().
160753a6d45SSherry Moore  */
161753a6d45SSherry Moore void
162753a6d45SSherry Moore scf_get_boot_config(uint8_t *boot_config)
163753a6d45SSherry Moore {
164753a6d45SSherry Moore 	assert(boot_config);
165753a6d45SSherry Moore 	*boot_config = 0;
166753a6d45SSherry Moore 
167753a6d45SSherry Moore #ifndef	__x86
168753a6d45SSherry Moore 	return;
169753a6d45SSherry Moore #else
170753a6d45SSherry Moore 	{
171753a6d45SSherry Moore 		/*
172753a6d45SSherry Moore 		 * Property vector for BOOT_CONFIG_PG_PARAMS property group.
173753a6d45SSherry Moore 		 */
174753a6d45SSherry Moore 		scf_propvec_t ua_boot_config[] = {
175*4196e263SSherry Moore 			{ FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, NULL,
176753a6d45SSherry Moore 			    UA_FASTREBOOT_DEFAULT },
177753a6d45SSherry Moore 			{ FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, NULL,
178753a6d45SSherry Moore 			    UA_FASTREBOOT_ONPANIC },
179753a6d45SSherry Moore 			{ NULL }
180753a6d45SSherry Moore 		};
181753a6d45SSherry Moore 		scf_propvec_t	*prop;
182753a6d45SSherry Moore 
183753a6d45SSherry Moore 		for (prop = ua_boot_config; prop->pv_prop != NULL; prop++)
184753a6d45SSherry Moore 			prop->pv_ptr = boot_config;
185753a6d45SSherry Moore 		prop = NULL;
186753a6d45SSherry Moore 		if (scf_read_propvec(FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_PARAMS,
187753a6d45SSherry Moore 		    B_TRUE, ua_boot_config, &prop) != SCF_FAILED) {
188753a6d45SSherry Moore 			/*
189753a6d45SSherry Moore 			 * Unset both flags if the platform has been
190753a6d45SSherry Moore 			 * blacklisted.
191753a6d45SSherry Moore 			 */
192753a6d45SSherry Moore 			if (scf_is_fb_blacklisted())
193753a6d45SSherry Moore 				*boot_config &= ~(UA_FASTREBOOT_DEFAULT |
194753a6d45SSherry Moore 				    UA_FASTREBOOT_ONPANIC);
195753a6d45SSherry Moore 			return;
196753a6d45SSherry Moore 		}
197753a6d45SSherry Moore #if defined(FASTREBOOT_DEBUG)
198753a6d45SSherry Moore 		if (prop != NULL) {
199753a6d45SSherry Moore 			(void) uu_warn("Service %s property '%s/%s' "
200753a6d45SSherry Moore 			    "not found.\n", FMRI_BOOT_CONFIG,
201753a6d45SSherry Moore 			    BOOT_CONFIG_PG_PARAMS, prop->pv_prop);
202753a6d45SSherry Moore 		} else {
203753a6d45SSherry Moore 			(void) uu_warn("Unable to read service %s "
204753a6d45SSherry Moore 			    "property '%s': %s\n", FMRI_BOOT_CONFIG,
205753a6d45SSherry Moore 			    BOOT_CONFIG_PG_PARAMS, scf_strerror(scf_error()));
206753a6d45SSherry Moore 		}
207753a6d45SSherry Moore #endif	/* FASTREBOOT_DEBUG */
208753a6d45SSherry Moore 	}
209753a6d45SSherry Moore #endif	/* __x86 */
210753a6d45SSherry Moore }
211753a6d45SSherry Moore 
212753a6d45SSherry Moore /*
213*4196e263SSherry Moore  * Get or set properties in non-persistent "config_ovr" property group
214*4196e263SSherry Moore  * in svc:/system/boot-config:default.
215*4196e263SSherry Moore  * It prints errors with uu_warn().
216*4196e263SSherry Moore  */
217*4196e263SSherry Moore /*ARGSUSED*/
218*4196e263SSherry Moore static int
219*4196e263SSherry Moore scf_getset_boot_config_ovr(int set, uint8_t *boot_config_ovr)
220*4196e263SSherry Moore {
221*4196e263SSherry Moore 	int rc = SCF_SUCCESS;
222*4196e263SSherry Moore 
223*4196e263SSherry Moore 	assert(boot_config_ovr);
224*4196e263SSherry Moore 
225*4196e263SSherry Moore #ifndef	__x86
226*4196e263SSherry Moore 	return (rc);
227*4196e263SSherry Moore #else
228*4196e263SSherry Moore 	{
229*4196e263SSherry Moore 		/*
230*4196e263SSherry Moore 		 * Property vector for BOOT_CONFIG_PG_OVR property group.
231*4196e263SSherry Moore 		 */
232*4196e263SSherry Moore 		scf_propvec_t ua_boot_config_ovr[] = {
233*4196e263SSherry Moore 			{ FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, NULL,
234*4196e263SSherry Moore 			    UA_FASTREBOOT_DEFAULT },
235*4196e263SSherry Moore 			{ NULL }
236*4196e263SSherry Moore 		};
237*4196e263SSherry Moore 		scf_propvec_t	*prop;
238*4196e263SSherry Moore 
239*4196e263SSherry Moore 		rc = scf_fmri_pg_get_or_add(FMRI_BOOT_CONFIG,
240*4196e263SSherry Moore 		    BOOT_CONFIG_PG_OVR, SCF_GROUP_APPLICATION,
241*4196e263SSherry Moore 		    SCF_PG_FLAG_NONPERSISTENT, set);
242*4196e263SSherry Moore 
243*4196e263SSherry Moore 		if (rc != SCF_SUCCESS) {
244*4196e263SSherry Moore #if defined(FASTREBOOT_DEBUG)
245*4196e263SSherry Moore 			if (set)
246*4196e263SSherry Moore 				(void) uu_warn("Unable to add service %s "
247*4196e263SSherry Moore 				    "property group '%s'\n",
248*4196e263SSherry Moore 				    FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR);
249*4196e263SSherry Moore #endif	/* FASTREBOOT_DEBUG */
250*4196e263SSherry Moore 			return (rc);
251*4196e263SSherry Moore 		}
252*4196e263SSherry Moore 
253*4196e263SSherry Moore 		for (prop = ua_boot_config_ovr; prop->pv_prop != NULL; prop++)
254*4196e263SSherry Moore 			prop->pv_ptr = boot_config_ovr;
255*4196e263SSherry Moore 		prop = NULL;
256*4196e263SSherry Moore 
257*4196e263SSherry Moore 		if (set)
258*4196e263SSherry Moore 			rc = scf_write_propvec(FMRI_BOOT_CONFIG,
259*4196e263SSherry Moore 			    BOOT_CONFIG_PG_OVR, ua_boot_config_ovr, &prop);
260*4196e263SSherry Moore 		else
261*4196e263SSherry Moore 			rc = scf_read_propvec(FMRI_BOOT_CONFIG,
262*4196e263SSherry Moore 			    BOOT_CONFIG_PG_OVR, B_FALSE, ua_boot_config_ovr,
263*4196e263SSherry Moore 			    &prop);
264*4196e263SSherry Moore 
265*4196e263SSherry Moore #if defined(FASTREBOOT_DEBUG)
266*4196e263SSherry Moore 		if (rc != SCF_SUCCESS) {
267*4196e263SSherry Moore 			if (prop != NULL) {
268*4196e263SSherry Moore 				(void) uu_warn("Service %s property '%s/%s' "
269*4196e263SSherry Moore 				    "not found.\n", FMRI_BOOT_CONFIG,
270*4196e263SSherry Moore 				    BOOT_CONFIG_PG_OVR, prop->pv_prop);
271*4196e263SSherry Moore 			} else {
272*4196e263SSherry Moore 				(void) uu_warn("Unable to %s service %s "
273*4196e263SSherry Moore 				    "property '%s': %s\n", set ? "set" : "get",
274*4196e263SSherry Moore 				    FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR,
275*4196e263SSherry Moore 				    scf_strerror(scf_error()));
276*4196e263SSherry Moore 			}
277*4196e263SSherry Moore 		}
278*4196e263SSherry Moore #endif	/* FASTREBOOT_DEBUG */
279*4196e263SSherry Moore 		return (rc);
280*4196e263SSherry Moore 
281*4196e263SSherry Moore 	}
282*4196e263SSherry Moore #endif	/* __x86 */
283*4196e263SSherry Moore }
284*4196e263SSherry Moore 
285*4196e263SSherry Moore /*
286*4196e263SSherry Moore  * Get values of properties in non-persistent "config_ovr" property group.
287*4196e263SSherry Moore  */
288*4196e263SSherry Moore static void
289*4196e263SSherry Moore scf_get_boot_config_ovr(uint8_t *boot_config_ovr)
290*4196e263SSherry Moore {
291*4196e263SSherry Moore 	(void) scf_getset_boot_config_ovr(B_FALSE, boot_config_ovr);
292*4196e263SSherry Moore }
293*4196e263SSherry Moore 
294*4196e263SSherry Moore /*
295*4196e263SSherry Moore  * Set value of "config_ovr/fastreboot_default".
296*4196e263SSherry Moore  */
297*4196e263SSherry Moore int
298*4196e263SSherry Moore scf_fastreboot_default_set_transient(boolean_t value)
299*4196e263SSherry Moore {
300*4196e263SSherry Moore 	uint8_t	boot_config_ovr = (value & UA_FASTREBOOT_DEFAULT);
301*4196e263SSherry Moore 
302*4196e263SSherry Moore 	return (scf_getset_boot_config_ovr(B_TRUE, &boot_config_ovr));
303*4196e263SSherry Moore }
304*4196e263SSherry Moore 
305*4196e263SSherry Moore /*
306753a6d45SSherry Moore  * Check whether Fast Reboot is the default operating mode.
307753a6d45SSherry Moore  * Return 0 if
308753a6d45SSherry Moore  *   1. the platform is xVM
309753a6d45SSherry Moore  * or
310753a6d45SSherry Moore  *   2. svc:/system/boot-config:default service doesn't exist,
311753a6d45SSherry Moore  * or
312*4196e263SSherry Moore  *   3. property "config/fastreboot_default" doesn't exist,
313753a6d45SSherry Moore  * or
314*4196e263SSherry Moore  *   4. value of property "config/fastreboot_default" is set to "false"
315*4196e263SSherry Moore  *      and "config_ovr/fastreboot_default" is not set to "true",
316753a6d45SSherry Moore  * or
317753a6d45SSherry Moore  *   5. the platform has been blacklisted.
318*4196e263SSherry Moore  * or
319*4196e263SSherry Moore  *   6. value of property "config_ovr/fastreboot_default" is set to "false".
320753a6d45SSherry Moore  * Return non-zero otherwise.
321753a6d45SSherry Moore  */
322753a6d45SSherry Moore int
323753a6d45SSherry Moore scf_is_fastboot_default(void)
324753a6d45SSherry Moore {
325*4196e263SSherry Moore 	uint8_t	boot_config = 0, boot_config_ovr;
326753a6d45SSherry Moore 	char procbuf[SYS_NMLN];
327753a6d45SSherry Moore 
328753a6d45SSherry Moore 	/*
329753a6d45SSherry Moore 	 * If we are on xVM, do not fast reboot by default.
330753a6d45SSherry Moore 	 */
331753a6d45SSherry Moore 	if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1 ||
332753a6d45SSherry Moore 	    strcmp(procbuf, "i86xpv") == 0)
333753a6d45SSherry Moore 		return (0);
334753a6d45SSherry Moore 
335*4196e263SSherry Moore 	/*
336*4196e263SSherry Moore 	 * Get property values from "config" property group
337*4196e263SSherry Moore 	 */
338753a6d45SSherry Moore 	scf_get_boot_config(&boot_config);
339*4196e263SSherry Moore 
340*4196e263SSherry Moore 	/*
341*4196e263SSherry Moore 	 * Get property values from non-persistent "config_ovr" property group
342*4196e263SSherry Moore 	 */
343*4196e263SSherry Moore 	boot_config_ovr = boot_config;
344*4196e263SSherry Moore 	scf_get_boot_config_ovr(&boot_config_ovr);
345*4196e263SSherry Moore 
346*4196e263SSherry Moore 	return (boot_config & boot_config_ovr & UA_FASTREBOOT_DEFAULT);
347753a6d45SSherry Moore }
348