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