summaryrefslogtreecommitdiff
path: root/usr/src/psm/stand/cpr/common/support.c
blob: d3cc145ea487ab68dc46a5118bb5146bce3a663a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/cpr.h>
#include <sys/pte.h>
#include <sys/promimpl.h>
#include <sys/prom_plat.h>
#include "cprboot.h"

extern void	prom_unmap(caddr_t, uint_t);

extern int cpr_debug;
static int cpr_show_props = 0;

/*
 * Read the config file and pass back the file path, filesystem
 * device path.
 */
int
cpr_read_cprinfo(int fd, char *file_path, char *fs_path)
{
	struct cprconfig cf;

	if (cpr_fs_read(fd, (char *)&cf, sizeof (cf)) != sizeof (cf) ||
	    cf.cf_magic != CPR_CONFIG_MAGIC)
		return (-1);

	(void) prom_strcpy(file_path, cf.cf_path);
	(void) prom_strcpy(fs_path, cf.cf_dev_prom);
	if (cf.cf_type == CFT_ZVOL)
		volname = cf.cf_fs;
	return (0);
}


/*
 * Read the location of the state file from the root filesystem.
 * Pass back to the caller the full device path of the filesystem
 * and the filename relative to that fs.
 */
int
cpr_locate_statefile(char *file_path, char *fs_path)
{
	int fd;
	int rc;

	if ((fd = cpr_fs_open(CPR_CONFIG)) != -1) {
		rc = cpr_read_cprinfo(fd, file_path, fs_path);
		(void) cpr_fs_close(fd);
	} else
		rc = -1;

	return (rc);
}


/*
 * Open the "defaults" file in the root fs and read the values of the
 * properties saved during the checkpoint.  Restore the values to nvram.
 *
 * Note: an invalid magic number in the "defaults" file means that the
 * state file is bad or obsolete so our caller should not proceed with
 * the resume.
 */
int
cpr_reset_properties(void)
{
	char *str, *default_path;
	int fd, len, rc, prop_errors;
	cprop_t *prop, *tail;
	cdef_t cdef;
	pnode_t node;

	str = "cpr_reset_properties";
	default_path = CPR_DEFAULT;

	if ((fd = cpr_fs_open(default_path)) == -1) {
		prom_printf("%s: unable to open %s\n",
		    str, default_path);
		return (-1);
	}

	rc = 0;
	len = cpr_fs_read(fd, (char *)&cdef, sizeof (cdef));
	if (len != sizeof (cdef)) {
		prom_printf("%s: error reading %s\n", str, default_path);
		rc = -1;
	} else if (cdef.mini.magic != CPR_DEFAULT_MAGIC) {
		prom_printf("%s: bad magic number in %s\n", str, default_path);
		rc = -1;
	}

	(void) cpr_fs_close(fd);
	if (rc)
		return (rc);

	node = prom_optionsnode();
	if (node == OBP_NONODE || node == OBP_BADNODE) {
		prom_printf("%s: cannot find \"options\" node\n", str);
		return (-1);
	}

	/*
	 * reset nvram to the original property values
	 */
	if (cpr_show_props)
		prom_printf("\n\ncpr_show_props:\n");
	for (prop_errors = 0, prop = cdef.props, tail = prop + CPR_MAXPROP;
	    prop < tail; prop++) {
		if (cpr_show_props) {
			prom_printf("mod=%c, name=\"%s\",\tvalue=\"%s\"\n",
			    prop->mod, prop->name, prop->value);
		}
		if (prop->mod != PROP_MOD)
			continue;

		len = prom_strlen(prop->value);
		if (prom_setprop(node, prop->name, prop->value, len + 1) < 0 ||
		    prom_getproplen(node, prop->name) != len) {
			prom_printf("%s: error setting \"%s\" to \"%s\"\n",
			    str, prop->name, prop->value);
			prop_errors++;
		}
	}

	return (prop_errors ? -1 : 0);
}


/*
 * Read and verify cpr dump descriptor
 */
int
cpr_read_cdump(int fd, cdd_t *cdp, ushort_t mach_type)
{
	char *str;
	int nread;

	str = "\ncpr_read_cdump:";
	nread = cpr_read(fd, (caddr_t)cdp, sizeof (*cdp));
	if (nread != sizeof (*cdp)) {
		prom_printf("%s Error reading cpr dump descriptor\n", str);
		return (-1);
	}

	if (cdp->cdd_magic != CPR_DUMP_MAGIC) {
		prom_printf("%s bad dump magic 0x%x, expected 0x%x\n",
		    str, cdp->cdd_magic, CPR_DUMP_MAGIC);
		return (-1);
	}

	if (cdp->cdd_version != CPR_VERSION) {
		prom_printf("%s bad cpr version %d, expected %d\n",
		    str, cdp->cdd_version, CPR_VERSION);
		return (-1);
	}

	if (cdp->cdd_machine != mach_type) {
		prom_printf("%s bad machine type 0x%x, expected 0x%x\n",
		    str, cdp->cdd_machine, mach_type);
		return (-1);
	}

	if (cdp->cdd_bitmaprec <= 0) {
		prom_printf("%s bad bitmap %d\n", str, cdp->cdd_bitmaprec);
		return (-1);
	}

	if (cdp->cdd_dumppgsize <= 0) {
		prom_printf("%s Bad pg tot %d\n", str, cdp->cdd_dumppgsize);
		return (-1);
	}

	cpr_debug = cdp->cdd_debug;

	return (0);
}


/*
 * update cpr dump terminator
 */
void
cpr_update_terminator(ctrm_t *file_term, caddr_t mapva)
{
	ctrm_t *mem_term;

	/*
	 * Add the offset to reach the terminator in the kernel so that we
	 * can directly change the restored kernel image.
	 */
	mem_term = (ctrm_t *)(mapva + (file_term->va & MMU_PAGEOFFSET));

	mem_term->real_statef_size = file_term->real_statef_size;
	mem_term->tm_shutdown = file_term->tm_shutdown;
	mem_term->tm_cprboot_start.tv_sec = file_term->tm_cprboot_start.tv_sec;
	mem_term->tm_cprboot_end.tv_sec = prom_gettime() / 1000;
}


/*
 * simple bcopy for cprboot
 */
void
bcopy(const void *s, void *d, size_t count)
{
	const char *src = s;
	char *dst = d;

	while (count--)
		*dst++ = *src++;
}