10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55648Ssetje * Common Development and Distribution License (the "License").
65648Ssetje * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6423Sgw25295 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23785Seota * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/cpr.h>
300Sstevel@tonic-gate #include <sys/pte.h>
310Sstevel@tonic-gate #include <sys/promimpl.h>
320Sstevel@tonic-gate #include <sys/prom_plat.h>
335648Ssetje #include "cprboot.h"
340Sstevel@tonic-gate
350Sstevel@tonic-gate extern void prom_unmap(caddr_t, uint_t);
360Sstevel@tonic-gate
370Sstevel@tonic-gate extern int cpr_debug;
380Sstevel@tonic-gate static int cpr_show_props = 0;
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * Read the config file and pass back the file path, filesystem
420Sstevel@tonic-gate * device path.
430Sstevel@tonic-gate */
440Sstevel@tonic-gate int
cpr_read_cprinfo(int fd,char * file_path,char * fs_path)450Sstevel@tonic-gate cpr_read_cprinfo(int fd, char *file_path, char *fs_path)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate struct cprconfig cf;
480Sstevel@tonic-gate
495648Ssetje if (cpr_fs_read(fd, (char *)&cf, sizeof (cf)) != sizeof (cf) ||
500Sstevel@tonic-gate cf.cf_magic != CPR_CONFIG_MAGIC)
510Sstevel@tonic-gate return (-1);
520Sstevel@tonic-gate
530Sstevel@tonic-gate (void) prom_strcpy(file_path, cf.cf_path);
540Sstevel@tonic-gate (void) prom_strcpy(fs_path, cf.cf_dev_prom);
55*6423Sgw25295 if (cf.cf_type == CFT_ZVOL)
56*6423Sgw25295 volname = cf.cf_fs;
570Sstevel@tonic-gate return (0);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * Read the location of the state file from the root filesystem.
630Sstevel@tonic-gate * Pass back to the caller the full device path of the filesystem
640Sstevel@tonic-gate * and the filename relative to that fs.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate int
cpr_locate_statefile(char * file_path,char * fs_path)670Sstevel@tonic-gate cpr_locate_statefile(char *file_path, char *fs_path)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate int fd;
700Sstevel@tonic-gate int rc;
710Sstevel@tonic-gate
725648Ssetje if ((fd = cpr_fs_open(CPR_CONFIG)) != -1) {
730Sstevel@tonic-gate rc = cpr_read_cprinfo(fd, file_path, fs_path);
745648Ssetje (void) cpr_fs_close(fd);
750Sstevel@tonic-gate } else
760Sstevel@tonic-gate rc = -1;
770Sstevel@tonic-gate
780Sstevel@tonic-gate return (rc);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * Open the "defaults" file in the root fs and read the values of the
840Sstevel@tonic-gate * properties saved during the checkpoint. Restore the values to nvram.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * Note: an invalid magic number in the "defaults" file means that the
870Sstevel@tonic-gate * state file is bad or obsolete so our caller should not proceed with
880Sstevel@tonic-gate * the resume.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate int
cpr_reset_properties(void)910Sstevel@tonic-gate cpr_reset_properties(void)
920Sstevel@tonic-gate {
935648Ssetje char *str, *default_path;
940Sstevel@tonic-gate int fd, len, rc, prop_errors;
950Sstevel@tonic-gate cprop_t *prop, *tail;
960Sstevel@tonic-gate cdef_t cdef;
97789Sahrens pnode_t node;
980Sstevel@tonic-gate
990Sstevel@tonic-gate str = "cpr_reset_properties";
1000Sstevel@tonic-gate default_path = CPR_DEFAULT;
1010Sstevel@tonic-gate
1025648Ssetje if ((fd = cpr_fs_open(default_path)) == -1) {
1035648Ssetje prom_printf("%s: unable to open %s\n",
1045648Ssetje str, default_path);
1050Sstevel@tonic-gate return (-1);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate rc = 0;
1095648Ssetje len = cpr_fs_read(fd, (char *)&cdef, sizeof (cdef));
1100Sstevel@tonic-gate if (len != sizeof (cdef)) {
1110Sstevel@tonic-gate prom_printf("%s: error reading %s\n", str, default_path);
1120Sstevel@tonic-gate rc = -1;
1130Sstevel@tonic-gate } else if (cdef.mini.magic != CPR_DEFAULT_MAGIC) {
1140Sstevel@tonic-gate prom_printf("%s: bad magic number in %s\n", str, default_path);
1150Sstevel@tonic-gate rc = -1;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1185648Ssetje (void) cpr_fs_close(fd);
1190Sstevel@tonic-gate if (rc)
1200Sstevel@tonic-gate return (rc);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate node = prom_optionsnode();
1230Sstevel@tonic-gate if (node == OBP_NONODE || node == OBP_BADNODE) {
124785Seota prom_printf("%s: cannot find \"options\" node\n", str);
1250Sstevel@tonic-gate return (-1);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * reset nvram to the original property values
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate if (cpr_show_props)
1320Sstevel@tonic-gate prom_printf("\n\ncpr_show_props:\n");
1330Sstevel@tonic-gate for (prop_errors = 0, prop = cdef.props, tail = prop + CPR_MAXPROP;
1340Sstevel@tonic-gate prop < tail; prop++) {
1350Sstevel@tonic-gate if (cpr_show_props) {
1360Sstevel@tonic-gate prom_printf("mod=%c, name=\"%s\",\tvalue=\"%s\"\n",
1370Sstevel@tonic-gate prop->mod, prop->name, prop->value);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate if (prop->mod != PROP_MOD)
1400Sstevel@tonic-gate continue;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate len = prom_strlen(prop->value);
1430Sstevel@tonic-gate if (prom_setprop(node, prop->name, prop->value, len + 1) < 0 ||
1440Sstevel@tonic-gate prom_getproplen(node, prop->name) != len) {
1450Sstevel@tonic-gate prom_printf("%s: error setting \"%s\" to \"%s\"\n",
1460Sstevel@tonic-gate str, prop->name, prop->value);
1470Sstevel@tonic-gate prop_errors++;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate return (prop_errors ? -1 : 0);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * Read and verify cpr dump descriptor
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate int
cpr_read_cdump(int fd,cdd_t * cdp,ushort_t mach_type)1590Sstevel@tonic-gate cpr_read_cdump(int fd, cdd_t *cdp, ushort_t mach_type)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate char *str;
1620Sstevel@tonic-gate int nread;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate str = "\ncpr_read_cdump:";
1650Sstevel@tonic-gate nread = cpr_read(fd, (caddr_t)cdp, sizeof (*cdp));
1660Sstevel@tonic-gate if (nread != sizeof (*cdp)) {
1670Sstevel@tonic-gate prom_printf("%s Error reading cpr dump descriptor\n", str);
1680Sstevel@tonic-gate return (-1);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (cdp->cdd_magic != CPR_DUMP_MAGIC) {
1720Sstevel@tonic-gate prom_printf("%s bad dump magic 0x%x, expected 0x%x\n",
1730Sstevel@tonic-gate str, cdp->cdd_magic, CPR_DUMP_MAGIC);
1740Sstevel@tonic-gate return (-1);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (cdp->cdd_version != CPR_VERSION) {
1780Sstevel@tonic-gate prom_printf("%s bad cpr version %d, expected %d\n",
1790Sstevel@tonic-gate str, cdp->cdd_version, CPR_VERSION);
1800Sstevel@tonic-gate return (-1);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (cdp->cdd_machine != mach_type) {
1840Sstevel@tonic-gate prom_printf("%s bad machine type 0x%x, expected 0x%x\n",
1850Sstevel@tonic-gate str, cdp->cdd_machine, mach_type);
1860Sstevel@tonic-gate return (-1);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (cdp->cdd_bitmaprec <= 0) {
1900Sstevel@tonic-gate prom_printf("%s bad bitmap %d\n", str, cdp->cdd_bitmaprec);
1910Sstevel@tonic-gate return (-1);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (cdp->cdd_dumppgsize <= 0) {
1950Sstevel@tonic-gate prom_printf("%s Bad pg tot %d\n", str, cdp->cdd_dumppgsize);
1960Sstevel@tonic-gate return (-1);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate cpr_debug = cdp->cdd_debug;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate return (0);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * update cpr dump terminator
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate void
cpr_update_terminator(ctrm_t * file_term,caddr_t mapva)2090Sstevel@tonic-gate cpr_update_terminator(ctrm_t *file_term, caddr_t mapva)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate ctrm_t *mem_term;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate * Add the offset to reach the terminator in the kernel so that we
2150Sstevel@tonic-gate * can directly change the restored kernel image.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate mem_term = (ctrm_t *)(mapva + (file_term->va & MMU_PAGEOFFSET));
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate mem_term->real_statef_size = file_term->real_statef_size;
2200Sstevel@tonic-gate mem_term->tm_shutdown = file_term->tm_shutdown;
2210Sstevel@tonic-gate mem_term->tm_cprboot_start.tv_sec = file_term->tm_cprboot_start.tv_sec;
2220Sstevel@tonic-gate mem_term->tm_cprboot_end.tv_sec = prom_gettime() / 1000;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * simple bcopy for cprboot
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate void
bcopy(const void * s,void * d,size_t count)2300Sstevel@tonic-gate bcopy(const void *s, void *d, size_t count)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate const char *src = s;
2330Sstevel@tonic-gate char *dst = d;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate while (count--)
2360Sstevel@tonic-gate *dst++ = *src++;
2370Sstevel@tonic-gate }
238