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