blob: b9c540e5030e898896c5cb8eecbd3c5ce9b6f7e6 [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 Spanglerdaa807c2011-07-11 10:55:18 -070040 {"dev_boot_usb", CAN_WRITE,
Randall Spangler43127702011-07-20 11:02:38 -070041 "Enable developer mode boot from USB/SD (writable)"},
Bill Richardson7272a692011-11-17 10:48:59 -080042 {"dev_boot_signed_only", CAN_WRITE,
43 "Enable developer mode boot only from official kernels (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070044 {"devsw_boot", 0, "Developer switch position at boot"},
45 {"devsw_cur", 0, "Developer switch current position"},
46 {"ecfw_act", IS_STRING, "Active EC firmware"},
47 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070048 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070049 {"fwid", IS_STRING, "Active firmware ID"},
Randall Spangler4115b892011-04-08 14:11:48 -070050 {"fwupdate_tries", CAN_WRITE,
51 "Times to try OS firmware update (writable, inside kern_nv)"},
Randall Spangler207825b2011-04-20 10:54:40 -070052 {"hwid", IS_STRING, "Hardware ID"},
Randall Spanglerff3f0002011-07-26 10:43:53 -070053 {"kern_nv", 0, "Non-volatile field for kernel use", "0x%08x"},
Randall Spangler207825b2011-04-20 10:54:40 -070054 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
55 {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"},
56 {"mainfw_act", IS_STRING, "Active main firmware"},
57 {"mainfw_type", IS_STRING, "Active main firmware type"},
58 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
59 {"recovery_reason", 0, "Recovery mode reason for current boot"},
60 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
61 {"recoverysw_boot", 0, "Recovery switch position at boot"},
62 {"recoverysw_cur", 0, "Recovery switch current position"},
63 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
64 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
65 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
66 {"savedmem_size", 0, "RAM debug data area size in bytes"},
67 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
68 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
69 {"tried_fwb", 0, "Tried firmware B before A this boot"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070070 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
71 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070072 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler71415712011-03-21 11:04:50 -070073 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
74 "LoadFirmware() debug data (not in print-all)"},
75 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
76 "LoadKernel() debug data (not in print-all)"},
Randall Spangler207825b2011-04-20 10:54:40 -070077 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
78 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
79 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
Randall Spangler54218662011-02-07 11:20:20 -080080 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070081 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080082};
83
84
85/* Print help */
86void PrintHelp(const char *progname) {
87 const Param *p;
88
89 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070090 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080091 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070092 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -080093 " %s [param1 [param2 [...]]]\n"
94 " Prints the current value(s) of the parameter(s).\n"
95 " %s [param1=value1] [param2=value2 [...]]]\n"
96 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080097 " %s [param1?value1] [param2?value2 [...]]]\n"
98 " Checks if the parameter(s) all contain the specified value(s).\n"
99 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -0800100 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800101 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -0800102 for (p = sys_param_list; p->name; p++)
103 printf(" %-22s %s\n", p->name, p->desc);
104}
105
106
107/* Find the parameter in the list.
108 *
109 * Returns the parameter, or NULL if no match. */
110const Param* FindParam(const char* name) {
111 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700112 if (!name)
113 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800114 for (p = sys_param_list; p->name; p++) {
115 if (!strcasecmp(p->name, name))
116 return p;
117 }
118 return NULL;
119}
120
121
122/* Set the specified parameter.
123 *
124 * Returns 0 if success, non-zero if error. */
125int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700126 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800127 return 1; /* Parameter is read-only */
128
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700129 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800130 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
131 } else {
132 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800133 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800134 if (!*value || (e && *e))
135 return 1;
136 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
137 }
138}
139
140
Randall Spangler227f7922011-03-11 13:34:56 -0800141/* Compares the parameter with the expected value.
142 *
143 * Returns 0 if success (match), non-zero if error (mismatch). */
144int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700145 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700146 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800147 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
148 if (!v || 0 != strcmp(v, expect))
149 return 1;
150 } else {
151 char* e;
152 int i = (int)strtol(expect, &e, 0);
153 int v = VbGetSystemPropertyInt(p->name);
154 if (!*expect || (e && *e))
155 return 1;
156 if (v == -1 || i != v)
157 return 1;
158 }
159 return 0;
160}
161
162
Randall Spangler54218662011-02-07 11:20:20 -0800163/* Print the specified parameter.
164 *
165 * Returns 0 if success, non-zero if error. */
166int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700167 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700168 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800169 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
170 if (!v)
171 return 1;
172 printf("%s", v);
173 } else {
174 int v = VbGetSystemPropertyInt(p->name);
175 if (v == -1)
176 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800177 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800178 }
179 return 0;
180}
181
182
Randall Spangler55af5b52011-04-08 14:26:46 -0700183/* Print all parameters with descriptions. If force_all!=0, prints even
184 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800185 *
186 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700187int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800188 const Param* p;
189 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700190 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800191 const char* value;
192
193 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700194 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700195 continue;
196 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800197 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
198 } else {
199 int v = VbGetSystemPropertyInt(p->name);
200 if (v == -1)
201 value = NULL;
202 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800203 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800204 value = buf;
205 }
206 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700207 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800208 p->name, (value ? value : "(error)"), p->desc);
209 }
210 return retval;
211}
212
213
214int main(int argc, char* argv[]) {
215 int retval = 0;
216 int i;
217
218 char* progname = strrchr(argv[0], '/');
219 if (progname)
220 progname++;
221 else
222 progname = argv[0];
223
Vadim Bendeburyc3574082011-05-02 16:23:30 -0700224 if (VbArchInit()) {
225 fprintf(stderr, "Failed to initialize\n");
226 return -1;
227 }
228
Randall Spangler54218662011-02-07 11:20:20 -0800229 /* If no args specified, print all params */
230 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700231 return PrintAllParams(0);
232 /* --all or -a prints all params including normally hidden ones */
233 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
234 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800235
236 /* Print help if needed */
237 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
238 PrintHelp(progname);
239 return 0;
240 }
241
242 /* Otherwise, loop through params and get/set them */
243 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700244 char* has_set = strchr(argv[i], '=');
245 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800246 char* name = strtok(argv[i], "=?");
247 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700248 const Param* p;
249
250 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
251 if (!name || has_set == argv[i] || has_expect == argv[i]) {
252 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800253 PrintHelp(progname);
254 return 1;
255 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700256 if (!value)
257 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800258 if (has_set && has_expect) {
259 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
260 PrintHelp(progname);
261 return 1;
262 }
Randall Spangler54218662011-02-07 11:20:20 -0800263
Randall Spanglerf27583f2011-03-21 16:58:54 -0700264 /* Find the parameter */
265 p = FindParam(name);
266 if (!p) {
267 fprintf(stderr, "Invalid parameter name: %s\n", name);
268 PrintHelp(progname);
269 return 1;
270 }
271
Randall Spangler54218662011-02-07 11:20:20 -0800272 if (i > 1)
273 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800274 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800275 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800276 else if (has_expect)
277 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800278 else
279 retval = PrintParam(p);
280 }
281
282 return retval;
283}