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