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