xref: /titanic_50/usr/src/psm/stand/cpr/common/support.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5986fd29aSsetje  * Common Development and Distribution License (the "License").
6986fd29aSsetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e7cbe64fSgw25295  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2353391bafSeota  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/cpr.h>
307c478bd9Sstevel@tonic-gate #include <sys/pte.h>
317c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
327c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
33986fd29aSsetje #include "cprboot.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate extern void	prom_unmap(caddr_t, uint_t);
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate extern int cpr_debug;
387c478bd9Sstevel@tonic-gate static int cpr_show_props = 0;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Read the config file and pass back the file path, filesystem
427c478bd9Sstevel@tonic-gate  * device path.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate int
cpr_read_cprinfo(int fd,char * file_path,char * fs_path)457c478bd9Sstevel@tonic-gate cpr_read_cprinfo(int fd, char *file_path, char *fs_path)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	struct cprconfig cf;
487c478bd9Sstevel@tonic-gate 
49986fd29aSsetje 	if (cpr_fs_read(fd, (char *)&cf, sizeof (cf)) != sizeof (cf) ||
507c478bd9Sstevel@tonic-gate 	    cf.cf_magic != CPR_CONFIG_MAGIC)
517c478bd9Sstevel@tonic-gate 		return (-1);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	(void) prom_strcpy(file_path, cf.cf_path);
547c478bd9Sstevel@tonic-gate 	(void) prom_strcpy(fs_path, cf.cf_dev_prom);
55*e7cbe64fSgw25295 	if (cf.cf_type == CFT_ZVOL)
56*e7cbe64fSgw25295 		volname = cf.cf_fs;
577c478bd9Sstevel@tonic-gate 	return (0);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Read the location of the state file from the root filesystem.
637c478bd9Sstevel@tonic-gate  * Pass back to the caller the full device path of the filesystem
647c478bd9Sstevel@tonic-gate  * and the filename relative to that fs.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate int
cpr_locate_statefile(char * file_path,char * fs_path)677c478bd9Sstevel@tonic-gate cpr_locate_statefile(char *file_path, char *fs_path)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int fd;
707c478bd9Sstevel@tonic-gate 	int rc;
717c478bd9Sstevel@tonic-gate 
72986fd29aSsetje 	if ((fd = cpr_fs_open(CPR_CONFIG)) != -1) {
737c478bd9Sstevel@tonic-gate 		rc = cpr_read_cprinfo(fd, file_path, fs_path);
74986fd29aSsetje 		(void) cpr_fs_close(fd);
757c478bd9Sstevel@tonic-gate 	} else
767c478bd9Sstevel@tonic-gate 		rc = -1;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (rc);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Open the "defaults" file in the root fs and read the values of the
847c478bd9Sstevel@tonic-gate  * properties saved during the checkpoint.  Restore the values to nvram.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Note: an invalid magic number in the "defaults" file means that the
877c478bd9Sstevel@tonic-gate  * state file is bad or obsolete so our caller should not proceed with
887c478bd9Sstevel@tonic-gate  * the resume.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate int
cpr_reset_properties(void)917c478bd9Sstevel@tonic-gate cpr_reset_properties(void)
927c478bd9Sstevel@tonic-gate {
93986fd29aSsetje 	char *str, *default_path;
947c478bd9Sstevel@tonic-gate 	int fd, len, rc, prop_errors;
957c478bd9Sstevel@tonic-gate 	cprop_t *prop, *tail;
967c478bd9Sstevel@tonic-gate 	cdef_t cdef;
97fa9e4066Sahrens 	pnode_t node;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	str = "cpr_reset_properties";
1007c478bd9Sstevel@tonic-gate 	default_path = CPR_DEFAULT;
1017c478bd9Sstevel@tonic-gate 
102986fd29aSsetje 	if ((fd = cpr_fs_open(default_path)) == -1) {
103986fd29aSsetje 		prom_printf("%s: unable to open %s\n",
104986fd29aSsetje 		    str, default_path);
1057c478bd9Sstevel@tonic-gate 		return (-1);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	rc = 0;
109986fd29aSsetje 	len = cpr_fs_read(fd, (char *)&cdef, sizeof (cdef));
1107c478bd9Sstevel@tonic-gate 	if (len != sizeof (cdef)) {
1117c478bd9Sstevel@tonic-gate 		prom_printf("%s: error reading %s\n", str, default_path);
1127c478bd9Sstevel@tonic-gate 		rc = -1;
1137c478bd9Sstevel@tonic-gate 	} else if (cdef.mini.magic != CPR_DEFAULT_MAGIC) {
1147c478bd9Sstevel@tonic-gate 		prom_printf("%s: bad magic number in %s\n", str, default_path);
1157c478bd9Sstevel@tonic-gate 		rc = -1;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
118986fd29aSsetje 	(void) cpr_fs_close(fd);
1197c478bd9Sstevel@tonic-gate 	if (rc)
1207c478bd9Sstevel@tonic-gate 		return (rc);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	node = prom_optionsnode();
1237c478bd9Sstevel@tonic-gate 	if (node == OBP_NONODE || node == OBP_BADNODE) {
12453391bafSeota 		prom_printf("%s: cannot find \"options\" node\n", str);
1257c478bd9Sstevel@tonic-gate 		return (-1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * reset nvram to the original property values
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 	if (cpr_show_props)
1327c478bd9Sstevel@tonic-gate 		prom_printf("\n\ncpr_show_props:\n");
1337c478bd9Sstevel@tonic-gate 	for (prop_errors = 0, prop = cdef.props, tail = prop + CPR_MAXPROP;
1347c478bd9Sstevel@tonic-gate 	    prop < tail; prop++) {
1357c478bd9Sstevel@tonic-gate 		if (cpr_show_props) {
1367c478bd9Sstevel@tonic-gate 			prom_printf("mod=%c, name=\"%s\",\tvalue=\"%s\"\n",
1377c478bd9Sstevel@tonic-gate 			    prop->mod, prop->name, prop->value);
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		if (prop->mod != PROP_MOD)
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		len = prom_strlen(prop->value);
1437c478bd9Sstevel@tonic-gate 		if (prom_setprop(node, prop->name, prop->value, len + 1) < 0 ||
1447c478bd9Sstevel@tonic-gate 		    prom_getproplen(node, prop->name) != len) {
1457c478bd9Sstevel@tonic-gate 			prom_printf("%s: error setting \"%s\" to \"%s\"\n",
1467c478bd9Sstevel@tonic-gate 			    str, prop->name, prop->value);
1477c478bd9Sstevel@tonic-gate 			prop_errors++;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (prop_errors ? -1 : 0);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Read and verify cpr dump descriptor
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate int
cpr_read_cdump(int fd,cdd_t * cdp,ushort_t mach_type)1597c478bd9Sstevel@tonic-gate cpr_read_cdump(int fd, cdd_t *cdp, ushort_t mach_type)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	char *str;
1627c478bd9Sstevel@tonic-gate 	int nread;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	str = "\ncpr_read_cdump:";
1657c478bd9Sstevel@tonic-gate 	nread = cpr_read(fd, (caddr_t)cdp, sizeof (*cdp));
1667c478bd9Sstevel@tonic-gate 	if (nread != sizeof (*cdp)) {
1677c478bd9Sstevel@tonic-gate 		prom_printf("%s Error reading cpr dump descriptor\n", str);
1687c478bd9Sstevel@tonic-gate 		return (-1);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (cdp->cdd_magic != CPR_DUMP_MAGIC) {
1727c478bd9Sstevel@tonic-gate 		prom_printf("%s bad dump magic 0x%x, expected 0x%x\n",
1737c478bd9Sstevel@tonic-gate 		    str, cdp->cdd_magic, CPR_DUMP_MAGIC);
1747c478bd9Sstevel@tonic-gate 		return (-1);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (cdp->cdd_version != CPR_VERSION) {
1787c478bd9Sstevel@tonic-gate 		prom_printf("%s bad cpr version %d, expected %d\n",
1797c478bd9Sstevel@tonic-gate 		    str, cdp->cdd_version, CPR_VERSION);
1807c478bd9Sstevel@tonic-gate 		return (-1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (cdp->cdd_machine != mach_type) {
1847c478bd9Sstevel@tonic-gate 		prom_printf("%s bad machine type 0x%x, expected 0x%x\n",
1857c478bd9Sstevel@tonic-gate 		    str, cdp->cdd_machine, mach_type);
1867c478bd9Sstevel@tonic-gate 		return (-1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (cdp->cdd_bitmaprec <= 0) {
1907c478bd9Sstevel@tonic-gate 		prom_printf("%s bad bitmap %d\n", str, cdp->cdd_bitmaprec);
1917c478bd9Sstevel@tonic-gate 		return (-1);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (cdp->cdd_dumppgsize <= 0) {
1957c478bd9Sstevel@tonic-gate 		prom_printf("%s Bad pg tot %d\n", str, cdp->cdd_dumppgsize);
1967c478bd9Sstevel@tonic-gate 		return (-1);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	cpr_debug = cdp->cdd_debug;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (0);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * update cpr dump terminator
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate void
cpr_update_terminator(ctrm_t * file_term,caddr_t mapva)2097c478bd9Sstevel@tonic-gate cpr_update_terminator(ctrm_t *file_term, caddr_t mapva)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	ctrm_t *mem_term;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Add the offset to reach the terminator in the kernel so that we
2157c478bd9Sstevel@tonic-gate 	 * can directly change the restored kernel image.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	mem_term = (ctrm_t *)(mapva + (file_term->va & MMU_PAGEOFFSET));
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	mem_term->real_statef_size = file_term->real_statef_size;
2207c478bd9Sstevel@tonic-gate 	mem_term->tm_shutdown = file_term->tm_shutdown;
2217c478bd9Sstevel@tonic-gate 	mem_term->tm_cprboot_start.tv_sec = file_term->tm_cprboot_start.tv_sec;
2227c478bd9Sstevel@tonic-gate 	mem_term->tm_cprboot_end.tv_sec = prom_gettime() / 1000;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * simple bcopy for cprboot
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate void
bcopy(const void * s,void * d,size_t count)2307c478bd9Sstevel@tonic-gate bcopy(const void *s, void *d, size_t count)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	const char *src = s;
2337c478bd9Sstevel@tonic-gate 	char *dst = d;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	while (count--)
2367c478bd9Sstevel@tonic-gate 		*dst++ = *src++;
2377c478bd9Sstevel@tonic-gate }
238