import less(1)
[unleashed/tickless.git] / usr / src / psm / promif / ieee1275 / sun4 / prom_vercheck.c
blob852f2a6e60258bcbc36c0f256bd3d2d718a81e9d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/promif.h>
27 #include <sys/promimpl.h>
29 #ifdef DPRINTF
30 #define dprintf prom_printf
31 #endif
34 * Check if the prom is 64-bit ready.
38 * Table listing the minimum prom versions supported by this kernel.
39 * The model value is expected to match the model in the flashprom node.
41 static struct obp_rev_table {
42 char *model;
43 char *version;
44 } obp_min_revs[] = {
45 {"SUNW,525-1414", "OBP 3.11.2 1997/12/05 10:25"}, /* pulsar */
46 {"SUNW,525-1672", "OBP 3.7.107 1998/02/19 17:54"}, /* tazmo */
47 {"SUNW,525-1431", "OBP 3.2.16 1998/06/08 16:58"}, /* sunfire */
48 { NULL, NULL}
51 #define NMINS 60
52 #define NHOURS 24
53 #define NDAYS 31
54 #define NMONTHS 12
56 #define YEAR(y) ((y-1) * (NMONTHS * NDAYS * NHOURS * NMINS))
57 #define MONTH(m) ((m-1) * (NDAYS * NHOURS * NMINS))
58 #define DAY(d) ((d-1) * (NHOURS * NMINS))
59 #define HOUR(h) ((h) * (NMINS))
60 #define MINUTE(m) (m)
62 static int
63 strtoi(char *str, char **pos)
65 int c;
66 int val = 0;
68 for (c = *str++; c >= '0' && c <= '9'; c = *str++) {
69 val *= 10;
70 val += c - '0';
72 if (pos)
73 *pos = str;
74 return (val);
78 * obp_timestamp: based on the OBP flashprom version string of the
79 * format "OBP x.y.z YYYY/MM/DD HH:MM" calculate a timestamp based
80 * on the year, month, day, hour and minute by turning that into
81 * a number of minutes.
83 static int
84 obp_timestamp(char *v)
86 char *c;
87 int maj, year, month, day, hour, min;
89 if (v[0] != 'O' || v[1] != 'B' || v[2] != 'P')
90 return (-1);
92 c = v + 3;
94 /* Find first non-space character after OBP */
95 while (*c != '\0' && (*c == ' ' || *c == '\t'))
96 c++;
97 if (prom_strlen(c) < 5) /* need at least "x.y.z" */
98 return (-1);
100 maj = strtoi(c, &c);
101 if (maj < 3)
102 return (-1);
104 #if 0 /* XXX - not used */
105 dot = dotdot = 0;
106 if (*c == '.') {
107 dot = strtoi(c + 1, &c);
109 /* optional? dot-dot release */
110 if (*c == '.')
111 dotdot = strtoi(c + 1, &c);
113 #endif
115 /* Find space at the end of version number */
116 while (*c != '\0' && *c != ' ')
117 c++;
118 if (prom_strlen(c) < 11) /* need at least " xxxx/xx/xx" */
119 return (-1);
121 /* Point to first character of date */
122 c++;
124 /* Validate date format */
125 if (c[4] != '/' || c[7] != '/')
126 return (-1);
128 year = strtoi(c, NULL);
129 month = strtoi(c + 5, NULL);
130 day = strtoi(c + 8, NULL);
132 if (year < 1995 || month == 0 || day == 0)
133 return (-1);
136 * Find space at the end of date number
138 c += 10;
139 while (*c != '\0' && *c != ' ')
140 c++;
141 if (prom_strlen(c) < 6) /* need at least " xx:xx" */
142 return (-1);
144 /* Point to first character of time */
145 c++;
147 if (c[2] != ':')
148 return (-1);
150 hour = strtoi(c, NULL);
151 min = strtoi(c + 3, NULL);
153 return (YEAR(year) + MONTH(month) +
154 DAY(day) + HOUR(hour) + MINUTE(min));
158 * Check the prom against the obp_min_revs table and complain if
159 * the system has an older prom installed. The actual major/minor/
160 * dotdot numbers are not checked, only the date/time stamp.
163 static struct obp_rev_table *flashprom_ortp;
164 static pnode_t flashprom_node;
165 static int flashprom_checked;
166 static int flashprom_return_code;
169 check_timestamp(char *model, int tstamp)
171 int min_tstamp;
172 struct obp_rev_table *ortp;
174 for (ortp = obp_min_revs; ortp->model != NULL; ortp++) {
175 if (prom_strcmp(model, ortp->model) == 0) {
176 min_tstamp = obp_timestamp(ortp->version);
177 if (min_tstamp == -1) {
178 #ifdef DEBUG
179 prom_printf("prom_version_check: "
180 "invalid OBP version string in table "
181 " (entry %d)", (int)(ortp - obp_min_revs));
182 #endif
183 continue;
185 if (tstamp < min_tstamp) {
186 #ifdef DPRINTF
187 dprintf("prom_version_check: "
188 "Down-rev OBP detected. "
189 "Please update to at least:\n\t%s\n\n",
190 ortp->version);
191 #endif
192 flashprom_ortp = ortp;
193 return (1);
196 } /* for each obp_rev_table entry */
198 return (0);
201 static pnode_t
202 visit(pnode_t node)
204 int tstamp, plen, i;
205 char vers[512], model[64];
206 static pnode_t openprom_node;
207 static char version[] = "version";
208 static char model_name[] = "model";
209 static char flashprom[] = "flashprom";
212 * if name isn't 'flashprom', continue.
214 if (prom_getproplen(node, OBP_NAME) != sizeof (flashprom))
215 return ((pnode_t)0);
216 (void) prom_getprop(node, OBP_NAME, model);
217 if (prom_strncmp(model, flashprom, sizeof (flashprom)) != 0)
218 return ((pnode_t)0);
220 plen = prom_getproplen(node, version);
221 if (plen <= 0 || plen > sizeof (vers))
222 return ((pnode_t)0);
223 (void) prom_getprop(node, version, vers);
224 vers[plen] = '\0';
226 /* Make sure it's an OBP flashprom */
227 if (vers[0] != 'O' && vers[1] != 'B' && vers[2] != 'P')
228 return ((pnode_t)0);
230 plen = prom_getproplen(node, model_name);
231 if (plen <= 0 || plen > sizeof (model))
232 return ((pnode_t)0);
233 (void) prom_getprop(node, model_name, model);
234 model[plen] = '\0';
236 tstamp = obp_timestamp(vers);
237 if (tstamp == -1) {
238 prom_printf("prom_version_check: node contains "
239 "improperly formatted version property,\n"
240 "\tnot checking prom version");
241 return ((pnode_t)0);
244 i = check_timestamp(model, tstamp);
246 if (i == 0)
247 return ((pnode_t)0);
250 * We know that "node"'s flashprom image contains downrev firmware,
251 * however, a multi-board server might be running correct firmware.
252 * Check for that case by looking at the "/openprom" node,
253 * which always contains the running version. (We needed the
254 * "model" value to be able to do this, so we can use it as
255 * an index value into the table.)
257 * If it turns out we're running 'current' firmware,
258 * but detect down-rev firmware, use a different return code.
261 flashprom_return_code = PROM_VER64_UPGRADE;
263 openprom_node = prom_finddevice("/openprom");
264 if (openprom_node == OBP_BADNODE)
265 return (node);
267 plen = prom_getproplen(node, version);
268 if (plen <= 0 || plen > sizeof (vers))
269 return (node);
270 (void) prom_getprop(node, version, vers);
271 vers[plen] = '\0';
273 if (vers[0] != 'O' && vers[1] != 'B' && vers[2] != 'P') {
274 prom_printf("prom_version_check: "
275 "unknown <version> string in </openprom>\n");
276 return (node);
279 tstamp = obp_timestamp(vers);
280 if (tstamp == -1) {
281 prom_printf("prom_version_check: "
282 "</openprom> node <version> property: bad tstamp\n");
283 return (node);
286 i = check_timestamp(model, tstamp);
288 * If that returned zero, then the running version is
289 * adequate ... so we can 'suggest' instead of 'require'.
291 if (i == 0)
292 flashprom_return_code = PROM_VER64_SUGGEST;
294 return (node);
298 * visit each node in the device tree, until we get a non-null answer
300 static pnode_t
301 walk(pnode_t node)
303 pnode_t id;
305 if (visit(node))
306 return (node);
308 for (node = prom_childnode(node); node; node = prom_nextnode(node))
309 if ((id = walk(node)) != (pnode_t)0)
310 return (id);
312 return ((pnode_t)0);
316 * Check if the prom is 64-bit ready.
318 * If it's ready (or the test doesn't apply), return PROM_VER64_OK.
319 * If downrev firmware is running, return PROM_VER64_UPGRADE.
320 * If downrev firmware is detected (but not running), return PROM_VER64_SUGGEST.
322 * For PROM_VER64_UPGRADE and PROM_VER64_SUGGEST return code values:
323 * Return the nodeid of the flashprom node in *nodeid.
324 * and a printable message in *buf, buflen.
327 prom_version_check(char *buf, size_t buflen, pnode_t *nodeid)
329 char *p;
330 pnode_t node = flashprom_node;
331 size_t i;
334 * If we already checked, we already know the answer.
336 if (flashprom_checked == 0) {
337 flashprom_node = node = walk(prom_rootnode());
338 flashprom_checked = 1;
341 if (nodeid)
342 *nodeid = node;
344 if (node == (pnode_t)0) {
345 if (buf && buflen)
346 *buf = '\0';
347 return (PROM_VER64_OK);
350 /* bzero the callers buffer */
351 for (i = buflen, p = buf; i != 0; --i, ++p)
352 *p = '\0';
355 * Do a bounded copy of the output string into the callers buffer
357 if (buflen <= 1)
358 return (flashprom_return_code);
360 (void) prom_strncpy(buf, flashprom_ortp->version, buflen - 1);
361 return (flashprom_return_code);