blob: 7289ad1de85c81e8f488b527b53e8d58cccf4692 [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 Spangler4115b892011-04-08 14:11:48 -070065 {"fwupdate_tries", CAN_WRITE,
66 "Times to try OS firmware update (writable, inside kern_nv)"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070067 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
68 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spangler71415712011-03-21 11:04:50 -070069 /* Fields not shown in a print-all list */
70 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
71 "LoadFirmware() debug data (not in print-all)"},
72 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
73 "LoadKernel() debug data (not in print-all)"},
Randall Spangler54218662011-02-07 11:20:20 -080074 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070075 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080076};
77
78
79/* Print help */
80void PrintHelp(const char *progname) {
81 const Param *p;
82
83 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070084 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080085 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070086 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -080087 " %s [param1 [param2 [...]]]\n"
88 " Prints the current value(s) of the parameter(s).\n"
89 " %s [param1=value1] [param2=value2 [...]]]\n"
90 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080091 " %s [param1?value1] [param2?value2 [...]]]\n"
92 " Checks if the parameter(s) all contain the specified value(s).\n"
93 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080094 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080095 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080096 for (p = sys_param_list; p->name; p++)
97 printf(" %-22s %s\n", p->name, p->desc);
98}
99
100
101/* Find the parameter in the list.
102 *
103 * Returns the parameter, or NULL if no match. */
104const Param* FindParam(const char* name) {
105 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700106 if (!name)
107 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800108 for (p = sys_param_list; p->name; p++) {
109 if (!strcasecmp(p->name, name))
110 return p;
111 }
112 return NULL;
113}
114
115
116/* Set the specified parameter.
117 *
118 * Returns 0 if success, non-zero if error. */
119int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700120 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800121 return 1; /* Parameter is read-only */
122
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700123 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800124 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
125 } else {
126 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800127 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800128 if (!*value || (e && *e))
129 return 1;
130 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
131 }
132}
133
134
Randall Spangler227f7922011-03-11 13:34:56 -0800135/* Compares the parameter with the expected value.
136 *
137 * Returns 0 if success (match), non-zero if error (mismatch). */
138int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700139 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700140 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800141 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
142 if (!v || 0 != strcmp(v, expect))
143 return 1;
144 } else {
145 char* e;
146 int i = (int)strtol(expect, &e, 0);
147 int v = VbGetSystemPropertyInt(p->name);
148 if (!*expect || (e && *e))
149 return 1;
150 if (v == -1 || i != v)
151 return 1;
152 }
153 return 0;
154}
155
156
Randall Spangler54218662011-02-07 11:20:20 -0800157/* Print the specified parameter.
158 *
159 * Returns 0 if success, non-zero if error. */
160int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700161 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700162 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800163 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
164 if (!v)
165 return 1;
166 printf("%s", v);
167 } else {
168 int v = VbGetSystemPropertyInt(p->name);
169 if (v == -1)
170 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800171 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800172 }
173 return 0;
174}
175
176
Randall Spangler55af5b52011-04-08 14:26:46 -0700177/* Print all parameters with descriptions. If force_all!=0, prints even
178 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800179 *
180 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700181int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800182 const Param* p;
183 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700184 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800185 const char* value;
186
187 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700188 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700189 continue;
190 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800191 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
192 } else {
193 int v = VbGetSystemPropertyInt(p->name);
194 if (v == -1)
195 value = NULL;
196 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800197 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800198 value = buf;
199 }
200 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700201 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800202 p->name, (value ? value : "(error)"), p->desc);
203 }
204 return retval;
205}
206
207
208int main(int argc, char* argv[]) {
209 int retval = 0;
210 int i;
211
212 char* progname = strrchr(argv[0], '/');
213 if (progname)
214 progname++;
215 else
216 progname = argv[0];
217
218 /* If no args specified, print all params */
219 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700220 return PrintAllParams(0);
221 /* --all or -a prints all params including normally hidden ones */
222 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
223 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800224
225 /* Print help if needed */
226 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
227 PrintHelp(progname);
228 return 0;
229 }
230
231 /* Otherwise, loop through params and get/set them */
232 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700233 char* has_set = strchr(argv[i], '=');
234 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800235 char* name = strtok(argv[i], "=?");
236 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700237 const Param* p;
238
239 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
240 if (!name || has_set == argv[i] || has_expect == argv[i]) {
241 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800242 PrintHelp(progname);
243 return 1;
244 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700245 if (!value)
246 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800247 if (has_set && has_expect) {
248 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
249 PrintHelp(progname);
250 return 1;
251 }
Randall Spangler54218662011-02-07 11:20:20 -0800252
Randall Spanglerf27583f2011-03-21 16:58:54 -0700253 /* Find the parameter */
254 p = FindParam(name);
255 if (!p) {
256 fprintf(stderr, "Invalid parameter name: %s\n", name);
257 PrintHelp(progname);
258 return 1;
259 }
260
Randall Spangler54218662011-02-07 11:20:20 -0800261 if (i > 1)
262 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800263 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800264 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800265 else if (has_expect)
266 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800267 else
268 retval = PrintParam(p);
269 }
270
271 return retval;
272}