blob: 1b926655be27b318563bc7f9e30b69a30923edea [file] [log] [blame]
Randall Spangler29e88072012-06-19 10:03:53 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Randall Spangler54218662011-02-07 11:20:20 -08002 * 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 Spangler29e88072012-06-19 10:03:53 -070038 {"clear_tpm_owner_request", CAN_WRITE, "Clear TPM owner on next boot"},
39 {"clear_tpm_owner_done", CAN_WRITE, "Clear TPM owner done"},
Randall Spangler207825b2011-04-20 10:54:40 -070040 {"cros_debug", 0, "OS should allow debug features"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070041 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +080042 {"ddr_type", IS_STRING, "Type of DDR RAM"},
Bill Richardson35d07332012-05-25 12:55:58 -070043 {"disable_dev_request", CAN_WRITE, "Disable virtual dev-mode on next boot"},
Randall Spanglerdaa807c2011-07-11 10:55:18 -070044 {"dev_boot_usb", CAN_WRITE,
Randall Spangler43127702011-07-20 11:02:38 -070045 "Enable developer mode boot from USB/SD (writable)"},
Bill Richardson7272a692011-11-17 10:48:59 -080046 {"dev_boot_signed_only", CAN_WRITE,
47 "Enable developer mode boot only from official kernels (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070048 {"devsw_boot", 0, "Developer switch position at boot"},
49 {"devsw_cur", 0, "Developer switch current position"},
50 {"ecfw_act", IS_STRING, "Active EC firmware"},
51 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070052 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070053 {"fwid", IS_STRING, "Active firmware ID"},
Randall Spangler4115b892011-04-08 14:11:48 -070054 {"fwupdate_tries", CAN_WRITE,
55 "Times to try OS firmware update (writable, inside kern_nv)"},
Randall Spangler207825b2011-04-20 10:54:40 -070056 {"hwid", IS_STRING, "Hardware ID"},
Randall Spanglerff3f0002011-07-26 10:43:53 -070057 {"kern_nv", 0, "Non-volatile field for kernel use", "0x%08x"},
Randall Spangler207825b2011-04-20 10:54:40 -070058 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
59 {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"},
60 {"mainfw_act", IS_STRING, "Active main firmware"},
61 {"mainfw_type", IS_STRING, "Active main firmware type"},
62 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
Bill Richardson17b82242012-06-26 16:33:56 -070063 {"oprom_needed", CAN_WRITE, "Should we load the VGA Option ROM at boot?"},
Bernie Thompson0b5789f2011-12-28 07:40:48 -080064 {"platform_family", IS_STRING, "Platform family type"},
Randall Spangler207825b2011-04-20 10:54:40 -070065 {"recovery_reason", 0, "Recovery mode reason for current boot"},
66 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
67 {"recoverysw_boot", 0, "Recovery switch position at boot"},
68 {"recoverysw_cur", 0, "Recovery switch current position"},
69 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
70 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
71 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
72 {"savedmem_size", 0, "RAM debug data area size in bytes"},
73 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
74 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
75 {"tried_fwb", 0, "Tried firmware B before A this boot"},
Randall Spangler207825b2011-04-20 10:54:40 -070076 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler71415712011-03-21 11:04:50 -070077 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
78 "LoadFirmware() debug data (not in print-all)"},
79 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
80 "LoadKernel() debug data (not in print-all)"},
Randall Spangler207825b2011-04-20 10:54:40 -070081 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
82 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
83 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
Randall Spangler54218662011-02-07 11:20:20 -080084 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070085 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080086};
87
88
89/* Print help */
90void PrintHelp(const char *progname) {
91 const Param *p;
92
93 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070094 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080095 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070096 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -080097 " %s [param1 [param2 [...]]]\n"
98 " Prints the current value(s) of the parameter(s).\n"
99 " %s [param1=value1] [param2=value2 [...]]]\n"
100 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800101 " %s [param1?value1] [param2?value2 [...]]]\n"
102 " Checks if the parameter(s) all contain the specified value(s).\n"
103 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -0800104 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800105 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -0800106 for (p = sys_param_list; p->name; p++)
107 printf(" %-22s %s\n", p->name, p->desc);
108}
109
110
111/* Find the parameter in the list.
112 *
113 * Returns the parameter, or NULL if no match. */
114const Param* FindParam(const char* name) {
115 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700116 if (!name)
117 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800118 for (p = sys_param_list; p->name; p++) {
119 if (!strcasecmp(p->name, name))
120 return p;
121 }
122 return NULL;
123}
124
125
126/* Set the specified parameter.
127 *
128 * Returns 0 if success, non-zero if error. */
129int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700130 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800131 return 1; /* Parameter is read-only */
132
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700133 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800134 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
135 } else {
136 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800137 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800138 if (!*value || (e && *e))
139 return 1;
140 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
141 }
142}
143
144
Randall Spangler227f7922011-03-11 13:34:56 -0800145/* Compares the parameter with the expected value.
146 *
147 * Returns 0 if success (match), non-zero if error (mismatch). */
148int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700149 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700150 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800151 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
152 if (!v || 0 != strcmp(v, expect))
153 return 1;
154 } else {
155 char* e;
156 int i = (int)strtol(expect, &e, 0);
157 int v = VbGetSystemPropertyInt(p->name);
158 if (!*expect || (e && *e))
159 return 1;
160 if (v == -1 || i != v)
161 return 1;
162 }
163 return 0;
164}
165
166
Randall Spangler54218662011-02-07 11:20:20 -0800167/* Print the specified parameter.
168 *
169 * Returns 0 if success, non-zero if error. */
170int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700171 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700172 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800173 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
174 if (!v)
175 return 1;
176 printf("%s", v);
177 } else {
178 int v = VbGetSystemPropertyInt(p->name);
179 if (v == -1)
180 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800181 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800182 }
183 return 0;
184}
185
186
Randall Spangler55af5b52011-04-08 14:26:46 -0700187/* Print all parameters with descriptions. If force_all!=0, prints even
188 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800189 *
190 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700191int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800192 const Param* p;
193 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700194 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800195 const char* value;
196
197 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700198 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700199 continue;
200 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800201 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
202 } else {
203 int v = VbGetSystemPropertyInt(p->name);
204 if (v == -1)
205 value = NULL;
206 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800207 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800208 value = buf;
209 }
210 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700211 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800212 p->name, (value ? value : "(error)"), p->desc);
213 }
214 return retval;
215}
216
217
218int main(int argc, char* argv[]) {
219 int retval = 0;
220 int i;
221
222 char* progname = strrchr(argv[0], '/');
223 if (progname)
224 progname++;
225 else
226 progname = argv[0];
227
Vadim Bendeburyc3574082011-05-02 16:23:30 -0700228 if (VbArchInit()) {
229 fprintf(stderr, "Failed to initialize\n");
230 return -1;
231 }
232
Randall Spangler54218662011-02-07 11:20:20 -0800233 /* If no args specified, print all params */
234 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700235 return PrintAllParams(0);
236 /* --all or -a prints all params including normally hidden ones */
237 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
238 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800239
240 /* Print help if needed */
241 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
242 PrintHelp(progname);
243 return 0;
244 }
245
246 /* Otherwise, loop through params and get/set them */
247 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700248 char* has_set = strchr(argv[i], '=');
249 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800250 char* name = strtok(argv[i], "=?");
251 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700252 const Param* p;
253
254 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
255 if (!name || has_set == argv[i] || has_expect == argv[i]) {
256 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800257 PrintHelp(progname);
258 return 1;
259 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700260 if (!value)
261 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800262 if (has_set && has_expect) {
263 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
264 PrintHelp(progname);
265 return 1;
266 }
Randall Spangler54218662011-02-07 11:20:20 -0800267
Randall Spanglerf27583f2011-03-21 16:58:54 -0700268 /* Find the parameter */
269 p = FindParam(name);
270 if (!p) {
271 fprintf(stderr, "Invalid parameter name: %s\n", name);
272 PrintHelp(progname);
273 return 1;
274 }
275
Randall Spangler54218662011-02-07 11:20:20 -0800276 if (i > 1)
277 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800278 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800279 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800280 else if (has_expect)
281 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800282 else
283 retval = PrintParam(p);
284 }
285
286 return retval;
287}