blob: fb842837cf4b8445c0e1fa9e9accf2de0bd75a4a [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 Spangler54218662011-02-07 11:20:20 -080044 /* Read-only strings */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070045 {"hwid", IS_STRING, "Hardware ID"},
46 {"fwid", IS_STRING, "Active firmware ID"},
47 {"ro_fwid", IS_STRING, "Read-only firmware ID"},
48 {"mainfw_act", IS_STRING, "Active main firmware"},
49 {"mainfw_type", IS_STRING, "Active main firmware type"},
50 {"ecfw_act", IS_STRING, "Active EC firmware"},
51 {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
52 {"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
53 {"vdat_lfdebug", IS_STRING, "LoadFirmware() debug data VbSharedData"},
Randall Spanglere73302c2011-02-18 14:53:01 -080054 /* Writable integers */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070055 {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
56 {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
57 {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
58 {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
59 {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
60 {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
61 {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
Randall Spanglere73302c2011-02-18 14:53:01 -080062
Randall Spangler54218662011-02-07 11:20:20 -080063 /* Terminate with null name */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070064 {NULL, 0, NULL}
Randall Spangler54218662011-02-07 11:20:20 -080065};
66
67
68/* Print help */
69void PrintHelp(const char *progname) {
70 const Param *p;
71
72 printf("\nUsage:\n"
73 " %s\n"
74 " Prints all parameters with descriptions and current values.\n"
75 " %s [param1 [param2 [...]]]\n"
76 " Prints the current value(s) of the parameter(s).\n"
77 " %s [param1=value1] [param2=value2 [...]]]\n"
78 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080079 " %s [param1?value1] [param2?value2 [...]]]\n"
80 " Checks if the parameter(s) all contain the specified value(s).\n"
81 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080082 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080083 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080084 for (p = sys_param_list; p->name; p++)
85 printf(" %-22s %s\n", p->name, p->desc);
86}
87
88
89/* Find the parameter in the list.
90 *
91 * Returns the parameter, or NULL if no match. */
92const Param* FindParam(const char* name) {
93 const Param* p;
94 for (p = sys_param_list; p->name; p++) {
95 if (!strcasecmp(p->name, name))
96 return p;
97 }
98 return NULL;
99}
100
101
102/* Set the specified parameter.
103 *
104 * Returns 0 if success, non-zero if error. */
105int SetParam(const Param* p, const char* value) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700106 if (!(p->flags & CAN_WRITE))
Randall Spangler54218662011-02-07 11:20:20 -0800107 return 1; /* Parameter is read-only */
108
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700109 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800110 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
111 } else {
112 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800113 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800114 if (!*value || (e && *e))
115 return 1;
116 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
117 }
118}
119
120
Randall Spangler227f7922011-03-11 13:34:56 -0800121/* Compares the parameter with the expected value.
122 *
123 * Returns 0 if success (match), non-zero if error (mismatch). */
124int CheckParam(const Param* p, char* expect) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700125 if (p->flags & IS_STRING) {
Randall Spangler227f7922011-03-11 13:34:56 -0800126 char buf[256];
127 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
128 if (!v || 0 != strcmp(v, expect))
129 return 1;
130 } else {
131 char* e;
132 int i = (int)strtol(expect, &e, 0);
133 int v = VbGetSystemPropertyInt(p->name);
134 if (!*expect || (e && *e))
135 return 1;
136 if (v == -1 || i != v)
137 return 1;
138 }
139 return 0;
140}
141
142
Randall Spangler54218662011-02-07 11:20:20 -0800143/* Print the specified parameter.
144 *
145 * Returns 0 if success, non-zero if error. */
146int PrintParam(const Param* p) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700147 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800148 char buf[256];
149 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
150 if (!v)
151 return 1;
152 printf("%s", v);
153 } else {
154 int v = VbGetSystemPropertyInt(p->name);
155 if (v == -1)
156 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800157 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800158 }
159 return 0;
160}
161
162
163/* Print all parameters with descriptions,
164 *
165 * Returns 0 if success, non-zero if error. */
166int PrintAllParams(void) {
167 const Param* p;
168 int retval = 0;
169 char buf[256];
170 const char* value;
171
172 for (p = sys_param_list; p->name; p++) {
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700173 if (p->flags & NO_PRINT_ALL)
174 continue;
175 if (p->flags & IS_STRING) {
Randall Spangler54218662011-02-07 11:20:20 -0800176 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
177 } else {
178 int v = VbGetSystemPropertyInt(p->name);
179 if (v == -1)
180 value = NULL;
181 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800182 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800183 value = buf;
184 }
185 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700186 printf("%-22s = %-30s # %s\n",
Randall Spangler54218662011-02-07 11:20:20 -0800187 p->name, (value ? value : "(error)"), p->desc);
188 }
189 return retval;
190}
191
192
193int main(int argc, char* argv[]) {
194 int retval = 0;
195 int i;
196
197 char* progname = strrchr(argv[0], '/');
198 if (progname)
199 progname++;
200 else
201 progname = argv[0];
202
203 /* If no args specified, print all params */
204 if (argc == 1)
205 return PrintAllParams();
206
207 /* Print help if needed */
208 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
209 PrintHelp(progname);
210 return 0;
211 }
212
213 /* Otherwise, loop through params and get/set them */
214 for (i = 1; i < argc && retval == 0; i++) {
Randall Spangler227f7922011-03-11 13:34:56 -0800215 int has_set = (NULL != strchr(argv[i], '='));
216 int has_expect = (NULL != strchr(argv[i], '?'));
217 char* name = strtok(argv[i], "=?");
218 char* value = strtok(NULL, "=?");
Randall Spangler54218662011-02-07 11:20:20 -0800219 const Param* p = FindParam(name);
220 if (!p) {
221 fprintf(stderr, "Invalid parameter name: %s\n", name);
222 PrintHelp(progname);
223 return 1;
224 }
Randall Spangler227f7922011-03-11 13:34:56 -0800225 if (has_set && has_expect) {
226 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
227 PrintHelp(progname);
228 return 1;
229 }
Randall Spangler54218662011-02-07 11:20:20 -0800230
231 if (i > 1)
232 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800233 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800234 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800235 else if (has_expect)
236 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800237 else
238 retval = PrintParam(p);
239 }
240
241 return retval;
242}