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