blob: 520f7fdf10c2b64dbb11fcecb9674277e528c7c1 [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
Vadim Bendeburyc3574082011-05-02 16:23:30 -070014/*
15 * Call arch specific init, if provided, otherwise use the 'weak' stub.
16 */
17int __VbArchInit(void) { return 0; }
18int VbArchInit(void) __attribute__((weak, alias("__VbArchInit")));
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070019/* Flags for Param */
20#define IS_STRING 0x01 /* String (not present = integer) */
21#define CAN_WRITE 0x02 /* Writable (not present = read-only */
22#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
23 * doing a print-all */
24
Randall Spangler54218662011-02-07 11:20:20 -080025typedef struct Param {
26 const char* name; /* Parameter name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070027 int flags; /* Flags (see above) */
Randall Spangler54218662011-02-07 11:20:20 -080028 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080029 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080030} Param;
31
32/* List of parameters, terminated with a param with NULL name */
33const Param sys_param_list[] = {
Randall Spangler824906b2011-04-08 13:34:44 -070034 {"arch", IS_STRING, "Platform architecture"},
Randall Spangler29e88072012-06-19 10:03:53 -070035 {"clear_tpm_owner_request", CAN_WRITE, "Clear TPM owner on next boot"},
36 {"clear_tpm_owner_done", CAN_WRITE, "Clear TPM owner done"},
Randall Spangler207825b2011-04-20 10:54:40 -070037 {"cros_debug", 0, "OS should allow debug features"},
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070038 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +080039 {"ddr_type", IS_STRING, "Type of DDR RAM"},
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -070040 {"debug_build", 0, "OS image built for debug features"},
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)"},
Stefan Reinauera2326ee2012-08-23 15:06:45 -070043 {"dev_boot_legacy", CAN_WRITE,
44 "Enable developer mode boot Legacy OSes (writable)"},
Bill Richardson7272a692011-11-17 10:48:59 -080045 {"dev_boot_signed_only", CAN_WRITE,
46 "Enable developer mode boot only from official kernels (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070047 {"devsw_boot", 0, "Developer switch position at boot"},
48 {"devsw_cur", 0, "Developer switch current position"},
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -070049 {"disable_dev_request", CAN_WRITE, "Disable virtual dev-mode on next boot"},
Randall Spangler207825b2011-04-20 10:54:40 -070050 {"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)"},
Bill Richardson699ebf32012-12-17 14:35:22 -080067 {"recovery_subcode", CAN_WRITE, "Recovery reason subcode (writable)"},
Randall Spangler207825b2011-04-20 10:54:40 -070068 {"recoverysw_boot", 0, "Recovery switch position at boot"},
69 {"recoverysw_cur", 0, "Recovery switch current position"},
70 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
71 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
72 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
73 {"savedmem_size", 0, "RAM debug data area size in bytes"},
Bill Richardson9dc62172012-08-28 15:00:51 -070074 {"sw_wpsw_boot", 0,
75 "Firmware write protect software setting enabled at boot"},
Randall Spangler207825b2011-04-20 10:54:40 -070076 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
77 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
78 {"tried_fwb", 0, "Tried firmware B before A this boot"},
Randall Spangler207825b2011-04-20 10:54:40 -070079 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler71415712011-03-21 11:04:50 -070080 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
81 "LoadFirmware() debug data (not in print-all)"},
82 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
83 "LoadKernel() debug data (not in print-all)"},
Randall Spangler207825b2011-04-20 10:54:40 -070084 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
85 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
86 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
Randall Spangler54218662011-02-07 11:20:20 -080087 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070088 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080089};
90
91
92/* Print help */
93void PrintHelp(const char *progname) {
94 const Param *p;
95
96 printf("\nUsage:\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070097 " %s [--all]\n"
Randall Spangler54218662011-02-07 11:20:20 -080098 " Prints all parameters with descriptions and current values.\n"
Randall Spangler55af5b52011-04-08 14:26:46 -070099 " If --all is specified, prints even normally hidden fields.\n"
Randall Spangler54218662011-02-07 11:20:20 -0800100 " %s [param1 [param2 [...]]]\n"
101 " Prints the current value(s) of the parameter(s).\n"
102 " %s [param1=value1] [param2=value2 [...]]]\n"
103 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800104 " %s [param1?value1] [param2?value2 [...]]]\n"
105 " Checks if the parameter(s) all contain the specified value(s).\n"
106 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -0800107 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -0800108 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -0800109 for (p = sys_param_list; p->name; p++)
110 printf(" %-22s %s\n", p->name, p->desc);
111}
112
113
114/* Find the parameter in the list.
115 *
116 * Returns the parameter, or NULL if no match. */
117const Param* FindParam(const char* name) {
118 const Param* p;
Randall Spanglerf27583f2011-03-21 16:58:54 -0700119 if (!name)
120 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800121 for (p = sys_param_list; p->name; p++) {
122 if (!strcasecmp(p->name, name))
123 return p;
124 }
125 return NULL;
126}
127
128
129/* Set the specified parameter.
130 *
131 * Returns 0 if success, non-zero if error. */
132int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700133 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800134 return 1; /* Parameter is read-only */
135
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700136 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800137 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
138 } else {
139 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800140 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800141 if (!*value || (e && *e))
142 return 1;
143 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
144 }
145}
146
147
Randall Spangler227f7922011-03-11 13:34:56 -0800148/* Compares the parameter with the expected value.
149 *
150 * Returns 0 if success (match), non-zero if error (mismatch). */
151int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700152 if (p->flags & IS_STRING) {
J. Richard Barnettea3d70a32013-10-30 11:36:45 -0700153 char buf[VB_MAX_STRING_PROPERTY];
Randall Spangler227f7922011-03-11 13:34:56 -0800154 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
155 if (!v || 0 != strcmp(v, expect))
156 return 1;
157 } else {
158 char* e;
159 int i = (int)strtol(expect, &e, 0);
160 int v = VbGetSystemPropertyInt(p->name);
161 if (!*expect || (e && *e))
162 return 1;
163 if (v == -1 || i != v)
164 return 1;
165 }
166 return 0;
167}
168
169
Randall Spangler54218662011-02-07 11:20:20 -0800170/* Print the specified parameter.
171 *
172 * Returns 0 if success, non-zero if error. */
173int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700174 if (p->flags & IS_STRING) {
J. Richard Barnettea3d70a32013-10-30 11:36:45 -0700175 char buf[VB_MAX_STRING_PROPERTY];
Randall Spangler54218662011-02-07 11:20:20 -0800176 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
177 if (!v)
178 return 1;
179 printf("%s", v);
180 } else {
181 int v = VbGetSystemPropertyInt(p->name);
182 if (v == -1)
183 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800184 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800185 }
186 return 0;
187}
188
189
Randall Spangler55af5b52011-04-08 14:26:46 -0700190/* Print all parameters with descriptions. If force_all!=0, prints even
191 * parameters that specify the NO_PRINT_ALL flag.
Randall Spangler54218662011-02-07 11:20:20 -0800192 *
193 * Returns 0 if success, non-zero if error. */
Randall Spangler55af5b52011-04-08 14:26:46 -0700194int PrintAllParams(int force_all) {
Randall Spangler54218662011-02-07 11:20:20 -0800195 const Param* p;
196 int retval = 0;
J. Richard Barnettea3d70a32013-10-30 11:36:45 -0700197 char buf[VB_MAX_STRING_PROPERTY];
Randall Spangler54218662011-02-07 11:20:20 -0800198 const char* value;
199
200 for (p = sys_param_list; p->name; p++) {
Randall Spangler55af5b52011-04-08 14:26:46 -0700201 if (0 == force_all && (p->flags & NO_PRINT_ALL))
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700202 continue;
203 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800204 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
205 } else {
206 int v = VbGetSystemPropertyInt(p->name);
207 if (v == -1)
208 value = NULL;
209 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800210 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800211 value = buf;
212 }
213 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700214 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800215 p->name, (value ? value : "(error)"), p->desc);
216 }
217 return retval;
218}
219
220
221int main(int argc, char* argv[]) {
222 int retval = 0;
223 int i;
224
225 char* progname = strrchr(argv[0], '/');
226 if (progname)
227 progname++;
228 else
229 progname = argv[0];
230
Vadim Bendeburyc3574082011-05-02 16:23:30 -0700231 if (VbArchInit()) {
232 fprintf(stderr, "Failed to initialize\n");
233 return -1;
234 }
235
Randall Spangler54218662011-02-07 11:20:20 -0800236 /* If no args specified, print all params */
237 if (argc == 1)
Randall Spangler55af5b52011-04-08 14:26:46 -0700238 return PrintAllParams(0);
239 /* --all or -a prints all params including normally hidden ones */
240 if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
241 return PrintAllParams(1);
Randall Spangler54218662011-02-07 11:20:20 -0800242
243 /* Print help if needed */
244 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
245 PrintHelp(progname);
246 return 0;
247 }
248
249 /* Otherwise, loop through params and get/set them */
250 for (i = 1; i < argc && retval == 0; i++) {
Randall Spanglerf27583f2011-03-21 16:58:54 -0700251 char* has_set = strchr(argv[i], '=');
252 char* has_expect = strchr(argv[i], '?');
Randall Spangler227f7922011-03-11 13:34:56 -0800253 char* name = strtok(argv[i], "=?");
254 char* value = strtok(NULL, "=?");
Randall Spanglerf27583f2011-03-21 16:58:54 -0700255 const Param* p;
256
257 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
258 if (!name || has_set == argv[i] || has_expect == argv[i]) {
259 fprintf(stderr, "Poorly formed parameter\n");
Randall Spangler54218662011-02-07 11:20:20 -0800260 PrintHelp(progname);
261 return 1;
262 }
Randall Spanglerf27583f2011-03-21 16:58:54 -0700263 if (!value)
264 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
Randall Spangler227f7922011-03-11 13:34:56 -0800265 if (has_set && has_expect) {
266 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
267 PrintHelp(progname);
268 return 1;
269 }
Randall Spangler54218662011-02-07 11:20:20 -0800270
Randall Spanglerf27583f2011-03-21 16:58:54 -0700271 /* Find the parameter */
272 p = FindParam(name);
273 if (!p) {
274 fprintf(stderr, "Invalid parameter name: %s\n", name);
275 PrintHelp(progname);
276 return 1;
277 }
278
Randall Spangler54218662011-02-07 11:20:20 -0800279 if (i > 1)
280 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800281 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800282 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800283 else if (has_expect)
284 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800285 else
286 retval = PrintParam(p);
287 }
288
289 return retval;
290}