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