blob: abd41865070367b773eafb2decaadb58dfe76763 [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
14typedef struct Param {
15 const char* name; /* Parameter name */
16 int is_string; /* 0 if integer, 1 if string */
17 int can_write; /* 0 if read-only, 1 if writable */
18 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080019 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080020} Param;
21
22/* List of parameters, terminated with a param with NULL name */
23const Param sys_param_list[] = {
24 /* Read-only integers */
25 {"devsw_cur", 0, 0, "Developer switch current position"},
26 {"devsw_boot", 0, 0, "Developer switch position at boot"},
27 {"recoverysw_cur", 0, 0, "Recovery switch current position"},
28 {"recoverysw_boot", 0, 0, "Recovery switch position at boot"},
29 {"recoverysw_ec_boot", 0, 0, "Recovery switch position at EC boot"},
Randall Spanglerb17e8d32011-03-15 09:50:38 -070030 {"wpsw_cur", 0, 0,
31 "Firmware write protect hardware switch current position"},
32 {"wpsw_boot", 0, 0,
33 "Firmware write protect hardware switch position at boot"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080034 {"recovery_reason", 0, 0, "Recovery mode reason for current boot"},
Randall Spangler0ca76fc2011-02-24 14:33:20 -080035 {"savedmem_base", 0, 0, "RAM debug data area physical address", "0x%08x"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080036 {"savedmem_size", 0, 0, "RAM debug data area size in bytes"},
Randall Spangler0ca76fc2011-02-24 14:33:20 -080037 {"fmap_base", 0, 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spangler17260282011-02-25 12:06:26 -080038 {"tried_fwb", 0, 0, "Tried firmware B before A this boot"},
Randall Spangler196e1772011-03-10 11:31:06 -080039 {"cros_debug", 0, 0, "OS should allow debug features"},
Randall Spangler54218662011-02-07 11:20:20 -080040 /* Read-only strings */
41 {"hwid", 1, 0, "Hardware ID"},
42 {"fwid", 1, 0, "Active firmware ID"},
43 {"ro_fwid", 1, 0, "Read-only firmware ID"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080044 {"mainfw_act", 1, 0, "Active main firmware"},
45 {"mainfw_type", 1, 0, "Active main firmware type"},
46 {"ecfw_act", 1, 0, "Active EC firmware"},
Randall Spangler17260282011-02-25 12:06:26 -080047 {"kernkey_vfy", 1, 0, "Type of verification done on kernel key block"},
Randall Spanglere73302c2011-02-18 14:53:01 -080048 /* Writable integers */
Randall Spanglerb4167142011-03-01 13:04:22 -080049 {"nvram_cleared", 0, 1, "Have NV settings been lost? Write 0 to clear"},
50 {"kern_nv", 0, 1, "Non-volatile field for kernel use", "0x%08x"},
Randall Spanglere73302c2011-02-18 14:53:01 -080051 {"recovery_request", 0, 1, "Recovery mode request (writable)"},
52 {"dbg_reset", 0, 1, "Debug reset mode request (writable)"},
53 {"fwb_tries", 0, 1, "Try firmware B count (writable)"},
Randall Spanglerb17e8d32011-03-15 09:50:38 -070054 {"vbtest_errfunc", 0, 1, "Verified boot test error function (writable)"},
55 {"vbtest_errno", 0, 1, "Verified boot test error number (writable)"},
Vadim Bendebury20084232011-03-15 09:29:48 -070056 {"vdat", 1, 0, "Raw VDAT contents."},
Randall Spanglere73302c2011-02-18 14:53:01 -080057
Randall Spangler54218662011-02-07 11:20:20 -080058 /* Terminate with null name */
59 {NULL, 0, 0, NULL}
60};
61
62
63/* Print help */
64void PrintHelp(const char *progname) {
65 const Param *p;
66
67 printf("\nUsage:\n"
68 " %s\n"
69 " Prints all parameters with descriptions and current values.\n"
70 " %s [param1 [param2 [...]]]\n"
71 " Prints the current value(s) of the parameter(s).\n"
72 " %s [param1=value1] [param2=value2 [...]]]\n"
73 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080074 " %s [param1?value1] [param2?value2 [...]]]\n"
75 " Checks if the parameter(s) all contain the specified value(s).\n"
76 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080077 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080078 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080079 for (p = sys_param_list; p->name; p++)
80 printf(" %-22s %s\n", p->name, p->desc);
81}
82
83
84/* Find the parameter in the list.
85 *
86 * Returns the parameter, or NULL if no match. */
87const Param* FindParam(const char* name) {
88 const Param* p;
89 for (p = sys_param_list; p->name; p++) {
90 if (!strcasecmp(p->name, name))
91 return p;
92 }
93 return NULL;
94}
95
96
97/* Set the specified parameter.
98 *
99 * Returns 0 if success, non-zero if error. */
100int SetParam(const Param* p, const char* value) {
101 if (!p->can_write)
102 return 1; /* Parameter is read-only */
103
104 if (p->is_string) {
105 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
106 } else {
107 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800108 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800109 if (!*value || (e && *e))
110 return 1;
111 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
112 }
113}
114
115
Randall Spangler227f7922011-03-11 13:34:56 -0800116/* Compares the parameter with the expected value.
117 *
118 * Returns 0 if success (match), non-zero if error (mismatch). */
119int CheckParam(const Param* p, char* expect) {
120 if (p->is_string) {
121 char buf[256];
122 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
123 if (!v || 0 != strcmp(v, expect))
124 return 1;
125 } else {
126 char* e;
127 int i = (int)strtol(expect, &e, 0);
128 int v = VbGetSystemPropertyInt(p->name);
129 if (!*expect || (e && *e))
130 return 1;
131 if (v == -1 || i != v)
132 return 1;
133 }
134 return 0;
135}
136
137
Randall Spangler54218662011-02-07 11:20:20 -0800138/* Print the specified parameter.
139 *
140 * Returns 0 if success, non-zero if error. */
141int PrintParam(const Param* p) {
142 if (p->is_string) {
143 char buf[256];
144 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
145 if (!v)
146 return 1;
147 printf("%s", v);
148 } else {
149 int v = VbGetSystemPropertyInt(p->name);
150 if (v == -1)
151 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800152 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800153 }
154 return 0;
155}
156
157
158/* Print all parameters with descriptions,
159 *
160 * Returns 0 if success, non-zero if error. */
161int PrintAllParams(void) {
162 const Param* p;
163 int retval = 0;
164 char buf[256];
165 const char* value;
166
167 for (p = sys_param_list; p->name; p++) {
168 if (p->is_string) {
169 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
170 } else {
171 int v = VbGetSystemPropertyInt(p->name);
172 if (v == -1)
173 value = NULL;
174 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800175 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800176 value = buf;
177 }
178 }
179 printf("%-22s = %-20s # %s\n",
180 p->name, (value ? value : "(error)"), p->desc);
181 }
182 return retval;
183}
184
185
186int main(int argc, char* argv[]) {
187 int retval = 0;
188 int i;
189
190 char* progname = strrchr(argv[0], '/');
191 if (progname)
192 progname++;
193 else
194 progname = argv[0];
195
196 /* If no args specified, print all params */
197 if (argc == 1)
198 return PrintAllParams();
199
200 /* Print help if needed */
201 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
202 PrintHelp(progname);
203 return 0;
204 }
205
206 /* Otherwise, loop through params and get/set them */
207 for (i = 1; i < argc && retval == 0; i++) {
Randall Spangler227f7922011-03-11 13:34:56 -0800208 int has_set = (NULL != strchr(argv[i], '='));
209 int has_expect = (NULL != strchr(argv[i], '?'));
210 char* name = strtok(argv[i], "=?");
211 char* value = strtok(NULL, "=?");
Randall Spangler54218662011-02-07 11:20:20 -0800212 const Param* p = FindParam(name);
213 if (!p) {
214 fprintf(stderr, "Invalid parameter name: %s\n", name);
215 PrintHelp(progname);
216 return 1;
217 }
Randall Spangler227f7922011-03-11 13:34:56 -0800218 if (has_set && has_expect) {
219 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
220 PrintHelp(progname);
221 return 1;
222 }
Randall Spangler54218662011-02-07 11:20:20 -0800223
224 if (i > 1)
225 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800226 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800227 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800228 else if (has_expect)
229 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800230 else
231 retval = PrintParam(p);
232 }
233
234 return retval;
235}