blob: 6b249e002717b8863d0e5510916a2b0f90481503 [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
Vadim Bendeburyc3574082011-05-02 16:23:30 -070017/*
18 * Call arch specific init, if provided, otherwise use the 'weak' stub.
19 */
20int __VbArchInit(void) { return 0; }
21int VbArchInit(void) __attribute__((weak, alias("__VbArchInit")));
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070022/* Flags for Param */
23#define IS_STRING 0x01 /* String (not present = integer) */
24#define CAN_WRITE 0x02 /* Writable (not present = read-only */
25#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
26 * doing a print-all */
27
Randall Spangler54218662011-02-07 11:20:20 -080028typedef struct Param {
29 const char* name; /* Parameter name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070030 int flags; /* Flags (see above) */
Randall Spangler54218662011-02-07 11:20:20 -080031 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080032 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080033} Param;
34
35/* List of parameters, terminated with a param with NULL name */
36const Param sys_param_list[] = {
Randall Spangler824906b2011-04-08 13:34:44 -070037 {"arch", IS_STRING, "Platform architecture"},
Randall Spangler207825b2011-04-20 10:54:40 -070038 {"cros_debug", 0, "OS should allow debug features"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070039 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070040 {"devsw_boot", 0, "Developer switch position at boot"},
41 {"devsw_cur", 0, "Developer switch current position"},
42 {"ecfw_act", IS_STRING, "Active EC firmware"},
43 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070044 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070045 {"fwid", IS_STRING, "Active firmware ID"},
Randall Spangler4115b892011-04-08 14:11:48 -070046 {"fwupdate_tries", CAN_WRITE,
47 "Times to try OS firmware update (writable, inside kern_nv)"},
Randall Spangler207825b2011-04-20 10:54:40 -070048 {"hwid", IS_STRING, "Hardware ID"},
49 {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
50 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
51 {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"},
52 {"mainfw_act", IS_STRING, "Active main firmware"},
53 {"mainfw_type", IS_STRING, "Active main firmware type"},
54 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
55 {"recovery_reason", 0, "Recovery mode reason for current boot"},
56 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
57 {"recoverysw_boot", 0, "Recovery switch position at boot"},
58 {"recoverysw_cur", 0, "Recovery switch current position"},
59 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
60 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
61 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
62 {"savedmem_size", 0, "RAM debug data area size in bytes"},
63 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
64 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
65 {"tried_fwb", 0, "Tried firmware B before A this boot"},
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 Spangler207825b2011-04-20 10:54:40 -070068 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler71415712011-03-21 11:04:50 -070069 {"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 Spangler207825b2011-04-20 10:54:40 -070073 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
74 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
75 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
Randall Spangler54218662011-02-07 11:20:20 -080076 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070077 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080078};
79
80
81/* Print help */
82void PrintHelp(const char *progname) {
83 const Param *p;
84
85 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070086 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080087 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070088 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -080089 " %s [param1 [param2 [...]]]\n"
90 " Prints the current value(s) of the parameter(s).\n"
91 " %s [param1=value1] [param2=value2 [...]]]\n"
92 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080093 " %s [param1?value1] [param2?value2 [...]]]\n"
94 " Checks if the parameter(s) all contain the specified value(s).\n"
95 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080096 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080097 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080098 for (p = sys_param_list; p->name; p++)
99 printf(" %-22s %s\n", p->name, p->desc);
100}
101
102
103/* Find the parameter in the list.
104 *
105 * Returns the parameter, or NULL if no match. */
106const Param* FindParam(const char* name) {
107 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700108 if (!name)
109 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800110 for (p = sys_param_list; p->name; p++) {
111 if (!strcasecmp(p->name, name))
112 return p;
113 }
114 return NULL;
115}
116
117
118/* Set the specified parameter.
119 *
120 * Returns 0 if success, non-zero if error. */
121int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700122 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800123 return 1; /* Parameter is read-only */
124
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700125 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800126 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
127 } else {
128 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800129 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800130 if (!*value || (e && *e))
131 return 1;
132 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
133 }
134}
135
136
Randall Spangler227f7922011-03-11 13:34:56 -0800137/* Compares the parameter with the expected value.
138 *
139 * Returns 0 if success (match), non-zero if error (mismatch). */
140int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700141 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700142 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800143 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
144 if (!v || 0 != strcmp(v, expect))
145 return 1;
146 } else {
147 char* e;
148 int i = (int)strtol(expect, &e, 0);
149 int v = VbGetSystemPropertyInt(p->name);
150 if (!*expect || (e && *e))
151 return 1;
152 if (v == -1 || i != v)
153 return 1;
154 }
155 return 0;
156}
157
158
Randall Spangler54218662011-02-07 11:20:20 -0800159/* Print the specified parameter.
160 *
161 * Returns 0 if success, non-zero if error. */
162int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700163 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700164 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800165 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
166 if (!v)
167 return 1;
168 printf("%s", v);
169 } else {
170 int v = VbGetSystemPropertyInt(p->name);
171 if (v == -1)
172 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800173 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800174 }
175 return 0;
176}
177
178
Randall Spangler55af5b52011-04-08 14:26:46 -0700179/* Print all parameters with descriptions. If force_all!=0, prints even
180 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800181 *
182 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700183int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800184 const Param* p;
185 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700186 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800187 const char* value;
188
189 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700190 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700191 continue;
192 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800193 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
194 } else {
195 int v = VbGetSystemPropertyInt(p->name);
196 if (v == -1)
197 value = NULL;
198 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800199 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800200 value = buf;
201 }
202 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700203 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800204 p->name, (value ? value : "(error)"), p->desc);
205 }
206 return retval;
207}
208
209
210int main(int argc, char* argv[]) {
211 int retval = 0;
212 int i;
213
214 char* progname = strrchr(argv[0], '/');
215 if (progname)
216 progname++;
217 else
218 progname = argv[0];
219
Vadim Bendeburyc3574082011-05-02 16:23:30 -0700220 if (VbArchInit()) {
221 fprintf(stderr, "Failed to initialize\n");
222 return -1;
223 }
224
Randall Spangler54218662011-02-07 11:20:20 -0800225 /* If no args specified, print all params */
226 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700227 return PrintAllParams(0);
228 /* --all or -a prints all params including normally hidden ones */
229 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
230 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800231
232 /* Print help if needed */
233 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
234 PrintHelp(progname);
235 return 0;
236 }
237
238 /* Otherwise, loop through params and get/set them */
239 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700240 char* has_set = strchr(argv[i], '=');
241 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800242 char* name = strtok(argv[i], "=?");
243 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700244 const Param* p;
245
246 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
247 if (!name || has_set == argv[i] || has_expect == argv[i]) {
248 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800249 PrintHelp(progname);
250 return 1;
251 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700252 if (!value)
253 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800254 if (has_set && has_expect) {
255 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
256 PrintHelp(progname);
257 return 1;
258 }
Randall Spangler54218662011-02-07 11:20:20 -0800259
Randall Spanglerf27583f2011-03-21 16:58:54 -0700260 /* Find the parameter */
261 p = FindParam(name);
262 if (!p) {
263 fprintf(stderr, "Invalid parameter name: %s\n", name);
264 PrintHelp(progname);
265 return 1;
266 }
267
Randall Spangler54218662011-02-07 11:20:20 -0800268 if (i > 1)
269 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800270 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800271 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800272 else if (has_expect)
273 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800274 else
275 retval = PrintParam(p);
276 }
277
278 return retval;
279}