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