blob: 584de594106e03bf2497ac900a621e517effdf02 [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"
73 "\n"
74 "Valid parameters:\n", progname, progname, progname);
75 for (p = sys_param_list; p->name; p++)
76 printf(" %-22s %s\n", p->name, p->desc);
77}
78
79
80/* Find the parameter in the list.
81 *
82 * Returns the parameter, or NULL if no match. */
83const Param* FindParam(const char* name) {
84 const Param* p;
85 for (p = sys_param_list; p->name; p++) {
86 if (!strcasecmp(p->name, name))
87 return p;
88 }
89 return NULL;
90}
91
92
93/* Set the specified parameter.
94 *
95 * Returns 0 if success, non-zero if error. */
96int SetParam(const Param* p, const char* value) {
97 if (!p->can_write)
98 return 1; /* Parameter is read-only */
99
100 if (p->is_string) {
101 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
102 } else {
103 char* e;
104 int i = strtol(value, &e, 0);
105 if (!*value || (e && *e))
106 return 1;
107 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
108 }
109}
110
111
112/* Print the specified parameter.
113 *
114 * Returns 0 if success, non-zero if error. */
115int PrintParam(const Param* p) {
116 if (p->is_string) {
117 char buf[256];
118 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
119 if (!v)
120 return 1;
121 printf("%s", v);
122 } else {
123 int v = VbGetSystemPropertyInt(p->name);
124 if (v == -1)
125 return 1;
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800126 printf(p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800127 }
128 return 0;
129}
130
131
132/* Print all parameters with descriptions,
133 *
134 * Returns 0 if success, non-zero if error. */
135int PrintAllParams(void) {
136 const Param* p;
137 int retval = 0;
138 char buf[256];
139 const char* value;
140
141 for (p = sys_param_list; p->name; p++) {
142 if (p->is_string) {
143 value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
144 } else {
145 int v = VbGetSystemPropertyInt(p->name);
146 if (v == -1)
147 value = NULL;
148 else {
Randall Spangler0ca76fc2011-02-24 14:33:20 -0800149 snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
Randall Spangler54218662011-02-07 11:20:20 -0800150 value = buf;
151 }
152 }
153 printf("%-22s = %-20s # %s\n",
154 p->name, (value ? value : "(error)"), p->desc);
155 }
156 return retval;
157}
158
159
160int main(int argc, char* argv[]) {
161 int retval = 0;
162 int i;
163
164 char* progname = strrchr(argv[0], '/');
165 if (progname)
166 progname++;
167 else
168 progname = argv[0];
169
170 /* If no args specified, print all params */
171 if (argc == 1)
172 return PrintAllParams();
173
174 /* Print help if needed */
175 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
176 PrintHelp(progname);
177 return 0;
178 }
179
180 /* Otherwise, loop through params and get/set them */
181 for (i = 1; i < argc && retval == 0; i++) {
182 char* name = strtok(argv[i], "=");
183 char* value = strtok(NULL, "=");
184 const Param* p = FindParam(name);
185 if (!p) {
186 fprintf(stderr, "Invalid parameter name: %s\n", name);
187 PrintHelp(progname);
188 return 1;
189 }
190
191 if (i > 1)
192 printf(" "); /* Output params space-delimited */
193 if (value)
194 retval = SetParam(p, value);
195 else
196 retval = PrintParam(p);
197 }
198
199 return retval;
200}