blob: 53166e48e0774bd342d1ed6cf16190124ffb3a05 [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
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070017/* Flags for Param */
18#define IS_STRING 0x01 /* String (not present = integer) */
19#define CAN_WRITE 0x02 /* Writable (not present = read-only */
20#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
21 * doing a print-all */
22
Randall Spangler54218662011-02-07 11:20:20 -080023typedef struct Param {
24 const char* name; /* Parameter name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070025 int flags; /* Flags (see above) */
Randall Spangler54218662011-02-07 11:20:20 -080026 const char* desc; /* Human-readable description */
Randall Spangler0ca76fc2011-02-24 14:33:20 -080027 const char* format; /* Format string, if non-NULL and 0==is_string*/
Randall Spangler54218662011-02-07 11:20:20 -080028} Param;
29
30/* List of parameters, terminated with a param with NULL name */
31const Param sys_param_list[] = {
32 /* Read-only integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070033 {"devsw_cur", 0, "Developer switch current position"},
34 {"devsw_boot", 0, "Developer switch position at boot"},
35 {"recoverysw_cur", 0, "Recovery switch current position"},
36 {"recoverysw_boot", 0, "Recovery switch position at boot"},
37 {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
38 {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
39 {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
40 {"recovery_reason", 0, "Recovery mode reason for current boot"},
41 {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
42 {"savedmem_size", 0, "RAM debug data area size in bytes"},
43 {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
44 {"tried_fwb", 0, "Tried firmware B before A this boot"},
45 {"cros_debug", 0, "OS should allow debug features"},
46 {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
Randall Spangler5ac39bf2011-03-17 17:58:56 -070047 {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
48 {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
Randall Spangler54218662011-02-07 11:20:20 -080049 /* Read-only strings */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070050 {"hwid", IS_STRING, "Hardware ID"},
51 {"fwid", IS_STRING, "Active firmware ID"},
52 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
53 {"mainfw_act", IS_STRING, "Active main firmware"},
54 {"mainfw_type", IS_STRING, "Active main firmware type"},
55 {"ecfw_act", IS_STRING, "Active EC firmware"},
56 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
57 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
Randall Spanglere73302c2011-02-18 14:53:01 -080058 /* Writable integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070059 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
60 {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
61 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
62 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
63 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
64 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
65 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spangler71415712011-03-21 11:04:50 -070066 /* Fields not shown in a print-all list */
67 {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
68 "LoadFirmware() debug data (not in print-all)"},
69 {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
70 "LoadKernel() debug data (not in print-all)"},
Randall Spangler54218662011-02-07 11:20:20 -080071 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070072 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080073};
74
75
76/* Print help */
77void PrintHelp(const char *progname) {
78 const Param *p;
79
80 printf("\nUsage:\n"
81 " %s\n"
82 " Prints all parameters with descriptions and current values.\n"
83 " %s [param1 [param2 [...]]]\n"
84 " Prints the current value(s) of the parameter(s).\n"
85 " %s [param1=value1] [param2=value2 [...]]]\n"
86 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080087 " %s [param1?value1] [param2?value2 [...]]]\n"
88 " Checks if the parameter(s) all contain the specified value(s).\n"
89 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080090 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080091 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080092 for (p = sys_param_list; p->name; p++)
93 printf(" %-22s %s\n", p->name, p->desc);
94}
95
96
97/* Find the parameter in the list.
98 *
99 * Returns the parameter, or NULL if no match. */
100const Param* FindParam(const char* name) {
101 const Param* p;
102 for (p = sys_param_list; p->name; p++) {
103 if (!strcasecmp(p->name, name))
104 return p;
105 }
106 return NULL;
107}
108
109
110/* Set the specified parameter.
111 *
112 * Returns 0 if success, non-zero if error. */
113int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700114 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800115 return 1; /* Parameter is read-only */
116
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700117 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800118 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
119 } else {
120 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800121 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800122 if (!*value || (e && *e))
123 return 1;
124 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
125 }
126}
127
128
Randall Spangler227f7922011-03-11 13:34:56 -0800129/* Compares the parameter with the expected value.
130 *
131 * Returns 0 if success (match), non-zero if error (mismatch). */
132int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700133 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700134 char buf[MAX_STRING];
Randall Spangler227f7922011-03-11 13:34:56 -0800135 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
136 if (!v || 0 != strcmp(v, expect))
137 return 1;
138 } else {
139 char* e;
140 int i = (int)strtol(expect, &e, 0);
141 int v = VbGetSystemPropertyInt(p->name);
142 if (!*expect || (e && *e))
143 return 1;
144 if (v == -1 || i != v)
145 return 1;
146 }
147 return 0;
148}
149
150
Randall Spangler54218662011-02-07 11:20:20 -0800151/* Print the specified parameter.
152 *
153 * Returns 0 if success, non-zero if error. */
154int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700155 if (p->flags & IS_STRING) {
Randall Spangler71415712011-03-21 11:04:50 -0700156 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800157 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
158 if (!v)
159 return 1;
160 printf("%s", v);
161 } else {
162 int v = VbGetSystemPropertyInt(p->name);
163 if (v == -1)
164 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800165 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800166 }
167 return 0;
168}
169
170
171/* Print all parameters with descriptions,
172 *
173 * Returns 0 if success, non-zero if error. */
174int PrintAllParams(void) {
175 const Param* p;
176 int retval = 0;
Randall Spangler71415712011-03-21 11:04:50 -0700177 char buf[MAX_STRING];
Randall Spangler54218662011-02-07 11:20:20 -0800178 const char* value;
179
180 for (p = sys_param_list; p->name; p++) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700181 if (p->flags & NO_PRINT_ALL)
182 continue;
183 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800184 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
185 } else {
186 int v = VbGetSystemPropertyInt(p->name);
187 if (v == -1)
188 value = NULL;
189 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800190 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800191 value = buf;
192 }
193 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700194 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800195 p->name, (value ? value : "(error)"), p->desc);
196 }
197 return retval;
198}
199
200
201int main(int argc, char* argv[]) {
202 int retval = 0;
203 int i;
204
205 char* progname = strrchr(argv[0], '/');
206 if (progname)
207 progname++;
208 else
209 progname = argv[0];
210
211 /* If no args specified, print all params */
212 if (argc == 1)
213 return PrintAllParams();
214
215 /* Print help if needed */
216 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
217 PrintHelp(progname);
218 return 0;
219 }
220
221 /* Otherwise, loop through params and get/set them */
222 for (i = 1; i < argc && retval == 0; i++) {
Randall Spangler227f7922011-03-11 13:34:56 -0800223 int has_set = (NULL != strchr(argv[i], '='));
224 int has_expect = (NULL != strchr(argv[i], '?'));
225 char* name = strtok(argv[i], "=?");
226 char* value = strtok(NULL, "=?");
Randall Spangler54218662011-02-07 11:20:20 -0800227 const Param* p = FindParam(name);
228 if (!p) {
229 fprintf(stderr, "Invalid parameter name: %s\n", name);
230 PrintHelp(progname);
231 return 1;
232 }
Randall Spangler227f7922011-03-11 13:34:56 -0800233 if (has_set && has_expect) {
234 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
235 PrintHelp(progname);
236 return 1;
237 }
Randall Spangler54218662011-02-07 11:20:20 -0800238
239 if (i > 1)
240 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800241 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800242 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800243 else if (has_expect)
244 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800245 else
246 retval = PrintParam(p);
247 }
248
249 return retval;
250}