blob: 6f352edecec3c121adea7c9dc849f971e09c2344 [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 Spanglerf4ba19d2011-03-17 16:10:21 -070014/* Flags for Param */
15#define IS_STRING 0x01 /* String (not present = integer) */
16#define CAN_WRITE 0x02 /* Writable (not present = read-only */
17#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
18 * doing a print-all */
19
Randall Spangler54218662011-02-07 11:20:20 -080020typedef struct Param {
21 const char* name; /* Parameter name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070022 int flags; /* Flags (see above) */
Randall Spangler54218662011-02-07 11:20:20 -080023 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080024 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080025} Param;
26
27/* List of parameters, terminated with a param with NULL name */
28const Param sys_param_list[] = {
29 /* Read-only integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070030 {"devsw_cur", 0, "Developer switch current position"},
31 {"devsw_boot", 0, "Developer switch position at boot"},
32 {"recoverysw_cur", 0, "Recovery switch current position"},
33 {"recoverysw_boot", 0, "Recovery switch position at boot"},
34 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
35 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
36 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
37 {"recovery_reason", 0, "Recovery mode reason for current boot"},
38 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
39 {"savedmem_size", 0, "RAM debug data area size in bytes"},
40 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
41 {"tried_fwb", 0, "Tried firmware B before A this boot"},
42 {"cros_debug", 0, "OS should allow debug features"},
43 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler5ac39bf2011-03-17 17:58:56 -070044 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
45 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
Randall Spangler54218662011-02-07 11:20:20 -080046 /* Read-only strings */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070047 {"hwid", IS_STRING, "Hardware ID"},
48 {"fwid", IS_STRING, "Active firmware ID"},
49 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
50 {"mainfw_act", IS_STRING, "Active main firmware"},
51 {"mainfw_type", IS_STRING, "Active main firmware type"},
52 {"ecfw_act", IS_STRING, "Active EC firmware"},
53 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
54 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
55 {"vdat_lfdebug", IS_STRING, "LoadFirmware() debug data VbSharedData"},
Randall Spanglere73302c2011-02-18 14:53:01 -080056 /* Writable integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070057 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
58 {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
59 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
60 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
61 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
62 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
63 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spanglere73302c2011-02-18 14:53:01 -080064
Randall Spangler54218662011-02-07 11:20:20 -080065 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070066 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080067};
68
69
70/* Print help */
71void PrintHelp(const char *progname) {
72 const Param *p;
73
74 printf("\nUsage:\n"
75 " %s\n"
76 " Prints all parameters with descriptions and current values.\n"
77 " %s [param1 [param2 [...]]]\n"
78 " Prints the current value(s) of the parameter(s).\n"
79 " %s [param1=value1] [param2=value2 [...]]]\n"
80 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080081 " %s [param1?value1] [param2?value2 [...]]]\n"
82 " Checks if the parameter(s) all contain the specified value(s).\n"
83 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080084 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080085 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080086 for (p = sys_param_list; p->name; p++)
87 printf(" %-22s %s\n", p->name, p->desc);
88}
89
90
91/* Find the parameter in the list.
92 *
93 * Returns the parameter, or NULL if no match. */
94const Param* FindParam(const char* name) {
95 const Param* p;
96 for (p = sys_param_list; p->name; p++) {
97 if (!strcasecmp(p->name, name))
98 return p;
99 }
100 return NULL;
101}
102
103
104/* Set the specified parameter.
105 *
106 * Returns 0 if success, non-zero if error. */
107int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700108 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800109 return 1; /* Parameter is read-only */
110
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700111 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800112 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
113 } else {
114 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800115 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800116 if (!*value || (e && *e))
117 return 1;
118 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
119 }
120}
121
122
Randall Spangler227f7922011-03-11 13:34:56 -0800123/* Compares the parameter with the expected value.
124 *
125 * Returns 0 if success (match), non-zero if error (mismatch). */
126int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700127 if (p->flags & IS_STRING) {
Randall Spangler227f7922011-03-11 13:34:56 -0800128 char buf[256];
129 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
130 if (!v || 0 != strcmp(v, expect))
131 return 1;
132 } else {
133 char* e;
134 int i = (int)strtol(expect, &e, 0);
135 int v = VbGetSystemPropertyInt(p->name);
136 if (!*expect || (e && *e))
137 return 1;
138 if (v == -1 || i != v)
139 return 1;
140 }
141 return 0;
142}
143
144
Randall Spangler54218662011-02-07 11:20:20 -0800145/* Print the specified parameter.
146 *
147 * Returns 0 if success, non-zero if error. */
148int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700149 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800150 char buf[256];
151 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
152 if (!v)
153 return 1;
154 printf("%s", v);
155 } else {
156 int v = VbGetSystemPropertyInt(p->name);
157 if (v == -1)
158 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800159 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800160 }
161 return 0;
162}
163
164
165/* Print all parameters with descriptions,
166 *
167 * Returns 0 if success, non-zero if error. */
168int PrintAllParams(void) {
169 const Param* p;
170 int retval = 0;
171 char buf[256];
172 const char* value;
173
174 for (p = sys_param_list; p->name; p++) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700175 if (p->flags & NO_PRINT_ALL)
176 continue;
177 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800178 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
179 } else {
180 int v = VbGetSystemPropertyInt(p->name);
181 if (v == -1)
182 value = NULL;
183 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800184 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800185 value = buf;
186 }
187 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700188 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800189 p->name, (value ? value : "(error)"), p->desc);
190 }
191 return retval;
192}
193
194
195int main(int argc, char* argv[]) {
196 int retval = 0;
197 int i;
198
199 char* progname = strrchr(argv[0], '/');
200 if (progname)
201 progname++;
202 else
203 progname = argv[0];
204
205 /* If no args specified, print all params */
206 if (argc == 1)
207 return PrintAllParams();
208
209 /* Print help if needed */
210 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
211 PrintHelp(progname);
212 return 0;
213 }
214
215 /* Otherwise, loop through params and get/set them */
216 for (i = 1; i < argc && retval == 0; i++) {
Randall Spangler227f7922011-03-11 13:34:56 -0800217 int has_set = (NULL != strchr(argv[i], '='));
218 int has_expect = (NULL != strchr(argv[i], '?'));
219 char* name = strtok(argv[i], "=?");
220 char* value = strtok(NULL, "=?");
Randall Spangler54218662011-02-07 11:20:20 -0800221 const Param* p = FindParam(name);
222 if (!p) {
223 fprintf(stderr, "Invalid parameter name: %s\n", name);
224 PrintHelp(progname);
225 return 1;
226 }
Randall Spangler227f7922011-03-11 13:34:56 -0800227 if (has_set && has_expect) {
228 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
229 PrintHelp(progname);
230 return 1;
231 }
Randall Spangler54218662011-02-07 11:20:20 -0800232
233 if (i > 1)
234 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800235 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800236 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800237 else if (has_expect)
238 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800239 else
240 retval = PrintParam(p);
241 }
242
243 return retval;
244}