xref: /titanic_50/usr/src/psm/stand/cpr/common/support.c (revision 33f2fefd46350ca5992567761c46a5b70f864340)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/cpr.h>
30 #include <sys/pte.h>
31 #include <sys/promimpl.h>
32 #include <sys/prom_plat.h>
33 #include "cprboot.h"
34 
35 extern void	prom_unmap(caddr_t, uint_t);
36 
37 extern int cpr_debug;
38 static int cpr_show_props = 0;
39 
40 /*
41  * Read the config file and pass back the file path, filesystem
42  * device path.
43  */
44 int
45 cpr_read_cprinfo(int fd, char *file_path, char *fs_path)
46 {
47 	struct cprconfig cf;
48 
49 	if (cpr_fs_read(fd, (char *)&cf, sizeof (cf)) != sizeof (cf) ||
50 	    cf.cf_magic != CPR_CONFIG_MAGIC)
51 		return (-1);
52 
53 	(void) prom_strcpy(file_path, cf.cf_path);
54 	(void) prom_strcpy(fs_path, cf.cf_dev_prom);
55 	if (cf.cf_type == CFT_ZVOL)
56 		volname = cf.cf_fs;
57 	return (0);
58 }
59 
60 
61 /*
62  * Read the location of the state file from the root filesystem.
63  * Pass back to the caller the full device path of the filesystem
64  * and the filename relative to that fs.
65  */
66 int
67 cpr_locate_statefile(char *file_path, char *fs_path)
68 {
69 	int fd;
70 	int rc;
71 
72 	if ((fd = cpr_fs_open(CPR_CONFIG)) != -1) {
73 		rc = cpr_read_cprinfo(fd, file_path, fs_path);
74 		(void) cpr_fs_close(fd);
75 	} else
76 		rc = -1;
77 
78 	return (rc);
79 }
80 
81 
82 /*
83  * Open the "defaults" file in the root fs and read the values of the
84  * properties saved during the checkpoint.  Restore the values to nvram.
85  *
86  * Note: an invalid magic number in the "defaults" file means that the
87  * state file is bad or obsolete so our caller should not proceed with
88  * the resume.
89  */
90 int
91 cpr_reset_properties(void)
92 {
93 	char *str, *default_path;
94 	int fd, len, rc, prop_errors;
95 	cprop_t *prop, *tail;
96 	cdef_t cdef;
97 	pnode_t node;
98 
99 	str = "cpr_reset_properties";
100 	default_path = CPR_DEFAULT;
101 
102 	if ((fd = cpr_fs_open(default_path)) == -1) {
103 		prom_printf("%s: unable to open %s\n",
104 		    str, default_path);
105 		return (-1);
106 	}
107 
108 	rc = 0;
109 	len = cpr_fs_read(fd, (char *)&cdef, sizeof (cdef));
110 	if (len != sizeof (cdef)) {
111 		prom_printf("%s: error reading %s\n", str, default_path);
112 		rc = -1;
113 	} else if (cdef.mini.magic != CPR_DEFAULT_MAGIC) {
114 		prom_printf("%s: bad magic number in %s\n", str, default_path);
115 		rc = -1;
116 	}
117 
118 	(void) cpr_fs_close(fd);
119 	if (rc)
120 		return (rc);
121 
122 	node = prom_optionsnode();
123 	if (node == OBP_NONODE || node == OBP_BADNODE) {
124 		prom_printf("%s: cannot find \"options\" node\n", str);
125 		return (-1);
126 	}
127 
128 	/*
129 	 * reset nvram to the original property values
130 	 */
131 	if (cpr_show_props)
132 		prom_printf("\n\ncpr_show_props:\n");
133 	for (prop_errors = 0, prop = cdef.props, tail = prop + CPR_MAXPROP;
134 	    prop < tail; prop++) {
135 		if (cpr_show_props) {
136 			prom_printf("mod=%c, name=\"%s\",\tvalue=\"%s\"\n",
137 			    prop->mod, prop->name, prop->value);
138 		}
139 		if (prop->mod != PROP_MOD)
140 			continue;
141 
142 		len = prom_strlen(prop->value);
143 		if (prom_setprop(node, prop->name, prop->value, len + 1) < 0 ||
144 		    prom_getproplen(node, prop->name) != len) {
145 			prom_printf("%s: error setting \"%s\" to \"%s\"\n",
146 			    str, prop->name, prop->value);
147 			prop_errors++;
148 		}
149 	}
150 
151 	return (prop_errors ? -1 : 0);
152 }
153 
154 
155 /*
156  * Read and verify cpr dump descriptor
157  */
158 int
159 cpr_read_cdump(int fd, cdd_t *cdp, ushort_t mach_type)
160 {
161 	char *str;
162 	int nread;
163 
164 	str = "\ncpr_read_cdump:";
165 	nread = cpr_read(fd, (caddr_t)cdp, sizeof (*cdp));
166 	if (nread != sizeof (*cdp)) {
167 		prom_printf("%s Error reading cpr dump descriptor\n", str);
168 		return (-1);
169 	}
170 
171 	if (cdp->cdd_magic != CPR_DUMP_MAGIC) {
172 		prom_printf("%s bad dump magic 0x%x, expected 0x%x\n",
173 		    str, cdp->cdd_magic, CPR_DUMP_MAGIC);
174 		return (-1);
175 	}
176 
177 	if (cdp->cdd_version != CPR_VERSION) {
178 		prom_printf("%s bad cpr version %d, expected %d\n",
179 		    str, cdp->cdd_version, CPR_VERSION);
180 		return (-1);
181 	}
182 
183 	if (cdp->cdd_machine != mach_type) {
184 		prom_printf("%s bad machine type 0x%x, expected 0x%x\n",
185 		    str, cdp->cdd_machine, mach_type);
186 		return (-1);
187 	}
188 
189 	if (cdp->cdd_bitmaprec <= 0) {
190 		prom_printf("%s bad bitmap %d\n", str, cdp->cdd_bitmaprec);
191 		return (-1);
192 	}
193 
194 	if (cdp->cdd_dumppgsize <= 0) {
195 		prom_printf("%s Bad pg tot %d\n", str, cdp->cdd_dumppgsize);
196 		return (-1);
197 	}
198 
199 	cpr_debug = cdp->cdd_debug;
200 
201 	return (0);
202 }
203 
204 
205 /*
206  * update cpr dump terminator
207  */
208 void
209 cpr_update_terminator(ctrm_t *file_term, caddr_t mapva)
210 {
211 	ctrm_t *mem_term;
212 
213 	/*
214 	 * Add the offset to reach the terminator in the kernel so that we
215 	 * can directly change the restored kernel image.
216 	 */
217 	mem_term = (ctrm_t *)(mapva + (file_term->va & MMU_PAGEOFFSET));
218 
219 	mem_term->real_statef_size = file_term->real_statef_size;
220 	mem_term->tm_shutdown = file_term->tm_shutdown;
221 	mem_term->tm_cprboot_start.tv_sec = file_term->tm_cprboot_start.tv_sec;
222 	mem_term->tm_cprboot_end.tv_sec = prom_gettime() / 1000;
223 }
224 
225 
226 /*
227  * simple bcopy for cprboot
228  */
229 void
230 bcopy(const void *s, void *d, size_t count)
231 {
232 	const char *src = s;
233 	char *dst = d;
234 
235 	while (count--)
236 		*dst++ = *src++;
237 }
238