blob: 2161583fbc17989120aea1c77aff07ae60f57896 [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)"},
Bill Richardson35d07332012-05-25 12:55:58 -070040 {"disable_dev_request", CAN_WRITE, "Disable virtual dev-mode on next boot"},
Randall Spanglerdaa807c2011-07-11 10:55:18 -070041 {"dev_boot_usb", CAN_WRITE,
Randall Spangler43127702011-07-20 11:02:38 -070042 "Enable developer mode boot from USB/SD (writable)"},
Bill Richardson7272a692011-11-17 10:48:59 -080043 {"dev_boot_signed_only", CAN_WRITE,
44 "Enable developer mode boot only from official kernels (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070045 {"devsw_boot", 0, "Developer switch position at boot"},
46 {"devsw_cur", 0, "Developer switch current position"},
47 {"ecfw_act", IS_STRING, "Active EC firmware"},
48 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070049 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070050 {"fwid", IS_STRING, "Active firmware ID"},
Randall Spangler4115b892011-04-08 14:11:48 -070051 {"fwupdate_tries", CAN_WRITE,
52 "Times to try OS firmware update (writable, inside kern_nv)"},
Randall Spangler207825b2011-04-20 10:54:40 -070053 {"hwid", IS_STRING, "Hardware ID"},
Randall Spanglerff3f0002011-07-26 10:43:53 -070054 {"kern_nv", 0, "Non-volatile field for kernel use", "0x%08x"},
Randall Spangler207825b2011-04-20 10:54:40 -070055 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
56 {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"},
57 {"mainfw_act", IS_STRING, "Active main firmware"},
58 {"mainfw_type", IS_STRING, "Active main firmware type"},
59 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
Bernie Thompson0b5789f2011-12-28 07:40:48 -080060 {"platform_family", IS_STRING, "Platform family type"},
Randall Spangler207825b2011-04-20 10:54:40 -070061 {"recovery_reason", 0, "Recovery mode reason for current boot"},
62 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
63 {"recoverysw_boot", 0, "Recovery switch position at boot"},
64 {"recoverysw_cur", 0, "Recovery switch current position"},
65 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
66 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
67 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
68 {"savedmem_size", 0, "RAM debug data area size in bytes"},
69 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
70 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
71 {"tried_fwb", 0, "Tried firmware B before A this boot"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070072 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
73 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070074 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler71415712011-03-21 11:04:50 -070075 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
76 "LoadFirmware() debug data (not in print-all)"},
77 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
78 "LoadKernel() debug data (not in print-all)"},
Randall Spangler207825b2011-04-20 10:54:40 -070079 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
80 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
81 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
Randall Spangler54218662011-02-07 11:20:20 -080082 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070083 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080084};
85
86
87/* Print help */
88void PrintHelp(const char *progname) {
89 const Param *p;
90
91 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070092 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080093 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070094 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -080095 " %s [param1 [param2 [...]]]\n"
96 " Prints the current value(s) of the parameter(s).\n"
97 " %s [param1=value1] [param2=value2 [...]]]\n"
98 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080099 " %s [param1?value1] [param2?value2 [...]]]\n"
100 " Checks if the parameter(s) all contain the specified value(s).\n"
101 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -0800102 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800103 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -0800104 for (p = sys_param_list; p->name; p++)
105 printf(" %-22s %s\n", p->name, p->desc);
106}
107
108
109/* Find the parameter in the list.
110 *
111 * Returns the parameter, or NULL if no match. */
112const Param* FindParam(const char* name) {
113 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700114 if (!name)
115 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800116 for (p = sys_param_list; p->name; p++) {
117 if (!strcasecmp(p->name, name))
118 return p;
119 }
120 return NULL;
121}
122
123
124/* Set the specified parameter.
125 *
126 * Returns 0 if success, non-zero if error. */
127int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700128 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800129 return 1; /* Parameter is read-only */
130
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700131 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800132 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
133 } else {
134 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800135 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800136 if (!*value || (e && *e))
137 return 1;
138 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
139 }
140}
141
142
Randall Spangler227f7922011-03-11 13:34:56 -0800143/* Compares the parameter with the expected value.
144 *
145 * Returns 0 if success (match), non-zero if error (mismatch). */
146int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700147 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700148 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800149 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
150 if (!v || 0 != strcmp(v, expect))
151 return 1;
152 } else {
153 char* e;
154 int i = (int)strtol(expect, &e, 0);
155 int v = VbGetSystemPropertyInt(p->name);
156 if (!*expect || (e && *e))
157 return 1;
158 if (v == -1 || i != v)
159 return 1;
160 }
161 return 0;
162}
163
164
Randall Spangler54218662011-02-07 11:20:20 -0800165/* Print the specified parameter.
166 *
167 * Returns 0 if success, non-zero if error. */
168int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700169 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700170 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800171 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
172 if (!v)
173 return 1;
174 printf("%s", v);
175 } else {
176 int v = VbGetSystemPropertyInt(p->name);
177 if (v == -1)
178 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800179 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800180 }
181 return 0;
182}
183
184
Randall Spangler55af5b52011-04-08 14:26:46 -0700185/* Print all parameters with descriptions. If force_all!=0, prints even
186 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800187 *
188 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700189int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800190 const Param* p;
191 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700192 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800193 const char* value;
194
195 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700196 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700197 continue;
198 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800199 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
200 } else {
201 int v = VbGetSystemPropertyInt(p->name);
202 if (v == -1)
203 value = NULL;
204 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800205 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800206 value = buf;
207 }
208 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700209 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800210 p->name, (value ? value : "(error)"), p->desc);
211 }
212 return retval;
213}
214
215
216int main(int argc, char* argv[]) {
217 int retval = 0;
218 int i;
219
220 char* progname = strrchr(argv[0], '/');
221 if (progname)
222 progname++;
223 else
224 progname = argv[0];
225
Vadim Bendeburyc3574082011-05-02 16:23:30 -0700226 if (VbArchInit()) {
227 fprintf(stderr, "Failed to initialize\n");
228 return -1;
229 }
230
Randall Spangler54218662011-02-07 11:20:20 -0800231 /* If no args specified, print all params */
232 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700233 return PrintAllParams(0);
234 /* --all or -a prints all params including normally hidden ones */
235 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
236 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800237
238 /* Print help if needed */
239 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
240 PrintHelp(progname);
241 return 0;
242 }
243
244 /* Otherwise, loop through params and get/set them */
245 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700246 char* has_set = strchr(argv[i], '=');
247 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800248 char* name = strtok(argv[i], "=?");
249 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700250 const Param* p;
251
252 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
253 if (!name || has_set == argv[i] || has_expect == argv[i]) {
254 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800255 PrintHelp(progname);
256 return 1;
257 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700258 if (!value)
259 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800260 if (has_set && has_expect) {
261 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
262 PrintHelp(progname);
263 return 1;
264 }
Randall Spangler54218662011-02-07 11:20:20 -0800265
Randall Spanglerf27583f2011-03-21 16:58:54 -0700266 /* Find the parameter */
267 p = FindParam(name);
268 if (!p) {
269 fprintf(stderr, "Invalid parameter name: %s\n", name);
270 PrintHelp(progname);
271 return 1;
272 }
273
Randall Spangler54218662011-02-07 11:20:20 -0800274 if (i > 1)
275 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800276 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800277 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800278 else if (has_expect)
279 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800280 else
281 retval = PrintParam(p);
282 }
283
284 return retval;
285}