blob: 0d44b99ab5a18d6e2673d3800787eaa7539a1136 [file] [log] [blame]
Randall Spangler54218662011-02-07 11:20:20 -08001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Chrome OS firmware/system interface utility
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "crossystem.h"
13
Randall Spangler71415712011-03-21 11:04:50 -070014/* Max length of a string parameter */
15#define MAX_STRING 8192
16
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070017/* Flags for Param */
18#define IS_STRING 0x01 /* String (not present = integer) */
19#define CAN_WRITE 0x02 /* Writable (not present = read-only */
20#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
21 * doing a print-all */
22
Randall Spangler54218662011-02-07 11:20:20 -080023typedef struct Param {
24 const char* name; /* Parameter name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070025 int flags; /* Flags (see above) */
Randall Spangler54218662011-02-07 11:20:20 -080026 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080027 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080028} Param;
29
30/* List of parameters, terminated with a param with NULL name */
31const Param sys_param_list[] = {
32 /* Read-only integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070033 {"devsw_cur", 0, "Developer switch current position"},
34 {"devsw_boot", 0, "Developer switch position at boot"},
35 {"recoverysw_cur", 0, "Recovery switch current position"},
36 {"recoverysw_boot", 0, "Recovery switch position at boot"},
37 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
38 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
39 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
40 {"recovery_reason", 0, "Recovery mode reason for current boot"},
41 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
42 {"savedmem_size", 0, "RAM debug data area size in bytes"},
43 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
44 {"tried_fwb", 0, "Tried firmware B before A this boot"},
45 {"cros_debug", 0, "OS should allow debug features"},
46 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler5ac39bf2011-03-17 17:58:56 -070047 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
48 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
Randall Spangler54218662011-02-07 11:20:20 -080049 /* Read-only strings */
Randall Spangler824906b2011-04-08 13:34:44 -070050 {"arch", IS_STRING, "Platform architecture"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070051 {"hwid", IS_STRING, "Hardware ID"},
52 {"fwid", IS_STRING, "Active firmware ID"},
53 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
54 {"mainfw_act", IS_STRING, "Active main firmware"},
55 {"mainfw_type", IS_STRING, "Active main firmware type"},
56 {"ecfw_act", IS_STRING, "Active EC firmware"},
57 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
58 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
Randall Spanglere73302c2011-02-18 14:53:01 -080059 /* Writable integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070060 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
61 {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
62 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
63 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
64 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
Randall Spanglerd7728232011-04-08 14:04:21 -070065 {"fwupdate_tries", CAN_WRITE, "Times to try OS firmware update (writable)"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070066 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
67 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spangler71415712011-03-21 11:04:50 -070068 /* Fields not shown in a print-all list */
69 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
70 "LoadFirmware() debug data (not in print-all)"},
71 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
72 "LoadKernel() debug data (not in print-all)"},
Randall Spangler54218662011-02-07 11:20:20 -080073 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070074 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080075};
76
77
78/* Print help */
79void PrintHelp(const char *progname) {
80 const Param *p;
81
82 printf("\nUsage:\n"
83 " %s\n"
84 " Prints all parameters with descriptions and current values.\n"
85 " %s [param1 [param2 [...]]]\n"
86 " Prints the current value(s) of the parameter(s).\n"
87 " %s [param1=value1] [param2=value2 [...]]]\n"
88 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080089 " %s [param1?value1] [param2?value2 [...]]]\n"
90 " Checks if the parameter(s) all contain the specified value(s).\n"
91 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080092 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080093 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080094 for (p = sys_param_list; p->name; p++)
95 printf(" %-22s %s\n", p->name, p->desc);
96}
97
98
99/* Find the parameter in the list.
100 *
101 * Returns the parameter, or NULL if no match. */
102const Param* FindParam(const char* name) {
103 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700104 if (!name)
105 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800106 for (p = sys_param_list; p->name; p++) {
107 if (!strcasecmp(p->name, name))
108 return p;
109 }
110 return NULL;
111}
112
113
114/* Set the specified parameter.
115 *
116 * Returns 0 if success, non-zero if error. */
117int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700118 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800119 return 1; /* Parameter is read-only */
120
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700121 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800122 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
123 } else {
124 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800125 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800126 if (!*value || (e && *e))
127 return 1;
128 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
129 }
130}
131
132
Randall Spangler227f7922011-03-11 13:34:56 -0800133/* Compares the parameter with the expected value.
134 *
135 * Returns 0 if success (match), non-zero if error (mismatch). */
136int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700137 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700138 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800139 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
140 if (!v || 0 != strcmp(v, expect))
141 return 1;
142 } else {
143 char* e;
144 int i = (int)strtol(expect, &e, 0);
145 int v = VbGetSystemPropertyInt(p->name);
146 if (!*expect || (e && *e))
147 return 1;
148 if (v == -1 || i != v)
149 return 1;
150 }
151 return 0;
152}
153
154
Randall Spangler54218662011-02-07 11:20:20 -0800155/* Print the specified parameter.
156 *
157 * Returns 0 if success, non-zero if error. */
158int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700159 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700160 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800161 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
162 if (!v)
163 return 1;
164 printf("%s", v);
165 } else {
166 int v = VbGetSystemPropertyInt(p->name);
167 if (v == -1)
168 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800169 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800170 }
171 return 0;
172}
173
174
175/* Print all parameters with descriptions,
176 *
177 * Returns 0 if success, non-zero if error. */
178int PrintAllParams(void) {
179 const Param* p;
180 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700181 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800182 const char* value;
183
184 for (p = sys_param_list; p->name; p++) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700185 if (p->flags & NO_PRINT_ALL)
186 continue;
187 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800188 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
189 } else {
190 int v = VbGetSystemPropertyInt(p->name);
191 if (v == -1)
192 value = NULL;
193 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800194 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800195 value = buf;
196 }
197 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700198 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800199 p->name, (value ? value : "(error)"), p->desc);
200 }
201 return retval;
202}
203
204
205int main(int argc, char* argv[]) {
206 int retval = 0;
207 int i;
208
209 char* progname = strrchr(argv[0], '/');
210 if (progname)
211 progname++;
212 else
213 progname = argv[0];
214
215 /* If no args specified, print all params */
216 if (argc == 1)
217 return PrintAllParams();
218
219 /* Print help if needed */
220 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
221 PrintHelp(progname);
222 return 0;
223 }
224
225 /* Otherwise, loop through params and get/set them */
226 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700227 char* has_set = strchr(argv[i], '=');
228 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800229 char* name = strtok(argv[i], "=?");
230 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700231 const Param* p;
232
233 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
234 if (!name || has_set == argv[i] || has_expect == argv[i]) {
235 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800236 PrintHelp(progname);
237 return 1;
238 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700239 if (!value)
240 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800241 if (has_set && has_expect) {
242 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
243 PrintHelp(progname);
244 return 1;
245 }
Randall Spangler54218662011-02-07 11:20:20 -0800246
Randall Spanglerf27583f2011-03-21 16:58:54 -0700247 /* Find the parameter */
248 p = FindParam(name);
249 if (!p) {
250 fprintf(stderr, "Invalid parameter name: %s\n", name);
251 PrintHelp(progname);
252 return 1;
253 }
254
Randall Spangler54218662011-02-07 11:20:20 -0800255 if (i > 1)
256 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800257 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800258 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800259 else if (has_expect)
260 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800261 else
262 retval = PrintParam(p);
263 }
264
265 return retval;
266}