blob: 90ad718bdf62ee879e9eb91c609baaa596d9f716 [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"},
30 {"wpsw_cur", 0, 0, "Firmware write protect switch current position"},
31 {"wpsw_boot", 0, 0, "Firmware write protect switch position at boot"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080032 {"recovery_reason", 0, 0, "Recovery mode reason for current boot"},
Randall Spangler0ca76fc2011-02-24 14:33:20 -080033 {"savedmem_base", 0, 0, "RAM debug data area physical address", "0x%08x"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080034 {"savedmem_size", 0, 0, "RAM debug data area size in bytes"},
Randall Spangler0ca76fc2011-02-24 14:33:20 -080035 {"fmap_base", 0, 0, "Main firmware flashmap physical address", "0x%08x"},
Randall Spangler17260282011-02-25 12:06:26 -080036 {"tried_fwb", 0, 0, "Tried firmware B before A this boot"},
Randall Spangler196e1772011-03-10 11:31:06 -080037 {"cros_debug", 0, 0, "OS should allow debug features"},
Randall Spangler54218662011-02-07 11:20:20 -080038 /* Read-only strings */
39 {"hwid", 1, 0, "Hardware ID"},
40 {"fwid", 1, 0, "Active firmware ID"},
41 {"ro_fwid", 1, 0, "Read-only firmware ID"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080042 {"mainfw_act", 1, 0, "Active main firmware"},
43 {"mainfw_type", 1, 0, "Active main firmware type"},
44 {"ecfw_act", 1, 0, "Active EC firmware"},
Randall Spangler17260282011-02-25 12:06:26 -080045 {"kernkey_vfy", 1, 0, "Type of verification done on kernel key block"},
Randall Spanglere73302c2011-02-18 14:53:01 -080046 /* Writable integers */
Randall Spanglerb4167142011-03-01 13:04:22 -080047 {"nvram_cleared", 0, 1, "Have NV settings been lost? Write 0 to clear"},
48 {"kern_nv", 0, 1, "Non-volatile field for kernel use", "0x%08x"},
Randall Spanglere73302c2011-02-18 14:53:01 -080049 {"recovery_request", 0, 1, "Recovery mode request (writable)"},
50 {"dbg_reset", 0, 1, "Debug reset mode request (writable)"},
51 {"fwb_tries", 0, 1, "Try firmware B count (writable)"},
52
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080053 /* TODO: implement the following:
54 * nvram_cleared
55 */
56
Randall Spangler54218662011-02-07 11:20:20 -080057 /* Terminate with null name */
58 {NULL, 0, 0, NULL}
59};
60
61
62/* Print help */
63void PrintHelp(const char *progname) {
64 const Param *p;
65
66 printf("\nUsage:\n"
67 " %s\n"
68 " Prints all parameters with descriptions and current values.\n"
69 " %s [param1 [param2 [...]]]\n"
70 " Prints the current value(s) of the parameter(s).\n"
71 " %s [param1=value1] [param2=value2 [...]]]\n"
72 " Sets the parameter(s) to the specified value(s).\n"
Randall Spangler227f7922011-03-11 13:34:56 -080073 " %s [param1?value1] [param2?value2 [...]]]\n"
74 " Checks if the parameter(s) all contain the specified value(s).\n"
75 "Stops at the first error."
Randall Spangler54218662011-02-07 11:20:20 -080076 "\n"
Randall Spangler227f7922011-03-11 13:34:56 -080077 "Valid parameters:\n", progname, progname, progname, progname);
Randall Spangler54218662011-02-07 11:20:20 -080078 for (p = sys_param_list; p->name; p++)
79 printf(" %-22s %s\n", p->name, p->desc);
80}
81
82
83/* Find the parameter in the list.
84 *
85 * Returns the parameter, or NULL if no match. */
86const Param* FindParam(const char* name) {
87 const Param* p;
88 for (p = sys_param_list; p->name; p++) {
89 if (!strcasecmp(p->name, name))
90 return p;
91 }
92 return NULL;
93}
94
95
96/* Set the specified parameter.
97 *
98 * Returns 0 if success, non-zero if error. */
99int SetParam(const Param* p, const char* value) {
100 if (!p->can_write)
101 return 1; /* Parameter is read-only */
102
103 if (p->is_string) {
104 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
105 } else {
106 char* e;
Randall Spangler227f7922011-03-11 13:34:56 -0800107 int i = (int)strtol(value, &e, 0);
Randall Spangler54218662011-02-07 11:20:20 -0800108 if (!*value || (e && *e))
109 return 1;
110 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
111 }
112}
113
114
Randall Spangler227f7922011-03-11 13:34:56 -0800115/* Compares the parameter with the expected value.
116 *
117 * Returns 0 if success (match), non-zero if error (mismatch). */
118int CheckParam(const Param* p, char* expect) {
119 if (p->is_string) {
120 char buf[256];
121 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
122 if (!v || 0 != strcmp(v, expect))
123 return 1;
124 } else {
125 char* e;
126 int i = (int)strtol(expect, &e, 0);
127 int v = VbGetSystemPropertyInt(p->name);
128 if (!*expect || (e && *e))
129 return 1;
130 if (v == -1 || i != v)
131 return 1;
132 }
133 return 0;
134}
135
136
Randall Spangler54218662011-02-07 11:20:20 -0800137/* Print the specified parameter.
138 *
139 * Returns 0 if success, non-zero if error. */
140int PrintParam(const Param* p) {
141 if (p->is_string) {
142 char buf[256];
143 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
144 if (!v)
145 return 1;
146 printf("%s", v);
147 } else {
148 int v = VbGetSystemPropertyInt(p->name);
149 if (v == -1)
150 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800151 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800152 }
153 return 0;
154}
155
156
157/* Print all parameters with descriptions,
158 *
159 * Returns 0 if success, non-zero if error. */
160int PrintAllParams(void) {
161 const Param* p;
162 int retval = 0;
163 char buf[256];
164 const char* value;
165
166 for (p = sys_param_list; p->name; p++) {
167 if (p->is_string) {
168 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
169 } else {
170 int v = VbGetSystemPropertyInt(p->name);
171 if (v == -1)
172 value = NULL;
173 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800174 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800175 value = buf;
176 }
177 }
178 printf("%-22s = %-20s # %s\n",
179 p->name, (value ? value : "(error)"), p->desc);
180 }
181 return retval;
182}
183
184
185int main(int argc, char* argv[]) {
186 int retval = 0;
187 int i;
188
189 char* progname = strrchr(argv[0], '/');
190 if (progname)
191 progname++;
192 else
193 progname = argv[0];
194
195 /* If no args specified, print all params */
196 if (argc == 1)
197 return PrintAllParams();
198
199 /* Print help if needed */
200 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
201 PrintHelp(progname);
202 return 0;
203 }
204
205 /* Otherwise, loop through params and get/set them */
206 for (i = 1; i < argc && retval == 0; i++) {
Randall Spangler227f7922011-03-11 13:34:56 -0800207 int has_set = (NULL != strchr(argv[i], '='));
208 int has_expect = (NULL != strchr(argv[i], '?'));
209 char* name = strtok(argv[i], "=?");
210 char* value = strtok(NULL, "=?");
Randall Spangler54218662011-02-07 11:20:20 -0800211 const Param* p = FindParam(name);
212 if (!p) {
213 fprintf(stderr, "Invalid parameter name: %s\n", name);
214 PrintHelp(progname);
215 return 1;
216 }
Randall Spangler227f7922011-03-11 13:34:56 -0800217 if (has_set && has_expect) {
218 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
219 PrintHelp(progname);
220 return 1;
221 }
Randall Spangler54218662011-02-07 11:20:20 -0800222
223 if (i > 1)
224 printf(" "); /* Output params space-delimited */
Randall Spangler227f7922011-03-11 13:34:56 -0800225 if (has_set)
Randall Spangler54218662011-02-07 11:20:20 -0800226 retval = SetParam(p, value);
Randall Spangler227f7922011-03-11 13:34:56 -0800227 else if (has_expect)
228 retval = CheckParam(p, value);
Randall Spangler54218662011-02-07 11:20:20 -0800229 else
230 retval = PrintParam(p);
231 }
232
233 return retval;
234}