blob: acf39e28ce8cf4f53ed8b2c990d9f41441e22e8f [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 */
19} Param;
20
21/* List of parameters, terminated with a param with NULL name */
22const Param sys_param_list[] = {
23 /* Read-only integers */
24 {"devsw_cur", 0, 0, "Developer switch current position"},
25 {"devsw_boot", 0, 0, "Developer switch position at boot"},
26 {"recoverysw_cur", 0, 0, "Recovery switch current position"},
27 {"recoverysw_boot", 0, 0, "Recovery switch position at boot"},
28 {"recoverysw_ec_boot", 0, 0, "Recovery switch position at EC boot"},
29 {"wpsw_cur", 0, 0, "Firmware write protect switch current position"},
30 {"wpsw_boot", 0, 0, "Firmware write protect switch position at boot"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080031 {"recovery_reason", 0, 0, "Recovery mode reason for current boot"},
32 {"savedmem_base", 0, 0, "RAM debug data area physical address"},
33 {"savedmem_size", 0, 0, "RAM debug data area size in bytes"},
Randall Spangler2b59a072011-02-24 11:17:24 -080034 {"fmap_base", 0, 0, "Main firmware flashmap physical address"},
Randall Spangler54218662011-02-07 11:20:20 -080035 /* Read-only strings */
36 {"hwid", 1, 0, "Hardware ID"},
37 {"fwid", 1, 0, "Active firmware ID"},
38 {"ro_fwid", 1, 0, "Read-only firmware ID"},
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080039 {"mainfw_act", 1, 0, "Active main firmware"},
40 {"mainfw_type", 1, 0, "Active main firmware type"},
41 {"ecfw_act", 1, 0, "Active EC firmware"},
Randall Spanglere73302c2011-02-18 14:53:01 -080042 /* Writable integers */
43 {"recovery_request", 0, 1, "Recovery mode request (writable)"},
44 {"dbg_reset", 0, 1, "Debug reset mode request (writable)"},
45 {"fwb_tries", 0, 1, "Try firmware B count (writable)"},
46
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080047 /* TODO: implement the following:
48 * nvram_cleared
49 */
50
Randall Spangler54218662011-02-07 11:20:20 -080051 /* Terminate with null name */
52 {NULL, 0, 0, NULL}
53};
54
55
56/* Print help */
57void PrintHelp(const char *progname) {
58 const Param *p;
59
60 printf("\nUsage:\n"
61 " %s\n"
62 " Prints all parameters with descriptions and current values.\n"
63 " %s [param1 [param2 [...]]]\n"
64 " Prints the current value(s) of the parameter(s).\n"
65 " %s [param1=value1] [param2=value2 [...]]]\n"
66 " Sets the parameter(s) to the specified value(s).\n"
67 "\n"
68 "Valid parameters:\n", progname, progname, progname);
69 for (p = sys_param_list; p->name; p++)
70 printf(" %-22s %s\n", p->name, p->desc);
71}
72
73
74/* Find the parameter in the list.
75 *
76 * Returns the parameter, or NULL if no match. */
77const Param* FindParam(const char* name) {
78 const Param* p;
79 for (p = sys_param_list; p->name; p++) {
80 if (!strcasecmp(p->name, name))
81 return p;
82 }
83 return NULL;
84}
85
86
87/* Set the specified parameter.
88 *
89 * Returns 0 if success, non-zero if error. */
90int SetParam(const Param* p, const char* value) {
91 if (!p->can_write)
92 return 1; /* Parameter is read-only */
93
94 if (p->is_string) {
95 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
96 } else {
97 char* e;
98 int i = strtol(value, &e, 0);
99 if (!*value || (e && *e))
100 return 1;
101 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
102 }
103}
104
105
106/* Print the specified parameter.
107 *
108 * Returns 0 if success, non-zero if error. */
109int PrintParam(const Param* p) {
110 if (p->is_string) {
111 char buf[256];
112 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
113 if (!v)
114 return 1;
115 printf("%s", v);
116 } else {
117 int v = VbGetSystemPropertyInt(p->name);
118 if (v == -1)
119 return 1;
120 printf("%d", v);
121 }
122 return 0;
123}
124
125
126/* Print all parameters with descriptions,
127 *
128 * Returns 0 if success, non-zero if error. */
129int PrintAllParams(void) {
130 const Param* p;
131 int retval = 0;
132 char buf[256];
133 const char* value;
134
135 for (p = sys_param_list; p->name; p++) {
136 if (p->is_string) {
137 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
138 } else {
139 int v = VbGetSystemPropertyInt(p->name);
140 if (v == -1)
141 value = NULL;
142 else {
143 snprintf(buf, sizeof(buf), "%d", v);
144 value = buf;
145 }
146 }
147 printf("%-22s = %-20s # %s\n",
148 p->name, (value ? value : "(error)"), p->desc);
149 }
150 return retval;
151}
152
153
154int main(int argc, char* argv[]) {
155 int retval = 0;
156 int i;
157
158 char* progname = strrchr(argv[0], '/');
159 if (progname)
160 progname++;
161 else
162 progname = argv[0];
163
164 /* If no args specified, print all params */
165 if (argc == 1)
166 return PrintAllParams();
167
168 /* Print help if needed */
169 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
170 PrintHelp(progname);
171 return 0;
172 }
173
174 /* Otherwise, loop through params and get/set them */
175 for (i = 1; i < argc && retval == 0; i++) {
176 char* name = strtok(argv[i], "=");
177 char* value = strtok(NULL, "=");
178 const Param* p = FindParam(name);
179 if (!p) {
180 fprintf(stderr, "Invalid parameter name: %s\n", name);
181 PrintHelp(progname);
182 return 1;
183 }
184
185 if (i > 1)
186 printf(" "); /* Output params space-delimited */
187 if (value)
188 retval = SetParam(p, value);
189 else
190 retval = PrintParam(p);
191 }
192
193 return retval;
194}