xref: /aosp_15_r20/external/vboot_reference/utility/crossystem.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2012 The ChromiumOS Authors
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 
14 /* Flags for Param */
15 #define IS_STRING      0x01  /* String (not present = integer) */
16 #define CAN_WRITE      0x02  /* Writable (not present = read-only */
17 #define NO_PRINT_ALL   0x04  /* Don't print contents of parameter when
18                               * doing a print-all */
19 
20 typedef struct Param {
21   const char* name;  /* Parameter name */
22   int flags;         /* Flags (see above) */
23   const char* desc;  /* Human-readable description */
24   const char* format; /* Format string, if non-NULL and 0==is_string*/
25 } Param;
26 
27 /* List of parameters, terminated with a param with NULL name */
28 const Param sys_param_list[] = {
29   {"act_fwver", 0, "Active firmware version", "0x%08x"},
30   {"act_kernver", 0, "Active kernel version", "0x%08x"},
31   {"arch", IS_STRING, "Platform architecture"},
32   {"backup_nvram_request", CAN_WRITE,
33    "Backup the nvram somewhere at the next boot. Cleared on success."},
34   {"battery_cutoff_request", CAN_WRITE,
35    "Cut off battery and shutdown on next boot"},
36   {"block_devmode", CAN_WRITE, "Block all use of developer mode"},
37   {"board_id", 0, "Board hardware revision number"},
38   {"clear_tpm_owner_done", CAN_WRITE, "Clear TPM owner done"},
39   {"clear_tpm_owner_request", CAN_WRITE, "Clear TPM owner on next boot"},
40   {"cros_debug", 0, "OS should allow debug features"},
41   {"dbg_reset", CAN_WRITE, "Debug reset mode request"},
42   {"debug_build", 0, "OS image built for debug features"},
43   {"dev_boot_altfw", CAN_WRITE, "Enable developer mode alternate bootloader"},
44   {"dev_boot_signed_only", CAN_WRITE,
45    "Enable developer mode boot only from official kernels"},
46   {"dev_boot_usb", CAN_WRITE,
47    "Enable developer mode boot from external disk (USB/SD)"},
48   {"dev_default_boot", IS_STRING|CAN_WRITE,
49    "Default boot from disk, altfw or usb"},
50   {"dev_enable_udc", CAN_WRITE, "Enable USB Device Controller"},
51   {"devsw_boot", 0, "Developer switch position at boot"},
52   {"devsw_cur",  0, "Developer switch current position"},
53   {"diagnostic_request", CAN_WRITE, "Request diagnostic rom run on next boot"},
54   {"disable_dev_request", CAN_WRITE, "Disable virtual dev-mode on next boot"},
55   {"ecfw_act", IS_STRING, "Active EC firmware"},
56   {"post_ec_sync_delay", CAN_WRITE,
57    "Short delay after EC software sync (persistent, writable, eve only)"},
58   {"fw_prev_result", IS_STRING, "Firmware result of previous boot"},
59   {"fw_prev_tried", IS_STRING, "Firmware tried on previous boot (A or B)"},
60   {"fw_result", IS_STRING|CAN_WRITE, "Firmware result this boot"},
61   {"fw_tried", IS_STRING, "Firmware tried this boot (A or B)"},
62   {"fw_try_count", CAN_WRITE, "Number of times to try fw_try_next"},
63   {"fw_try_next", IS_STRING|CAN_WRITE, "Firmware to try next (A or B)"},
64   {"fw_vboot2", 0, "1 if firmware was selected by vboot2 or 0 otherwise"},
65   {"fwid", IS_STRING, "Active firmware ID"},
66   {"fwupdate_tries", CAN_WRITE,
67    "Times to try OS firmware update (inside kern_nv)"},
68   {"hwid", IS_STRING, "Hardware ID"},
69   {"inside_vm", 0, "Running in a VM?"},
70   {"kern_nv", 0, "Non-volatile field for kernel use", "0x%04x"},
71   {"kernel_max_rollforward", CAN_WRITE, "Max kernel version to store into TPM",
72    "0x%08x"},
73   {"kernkey_vfy", IS_STRING, "Type of verification done on kernel keyblock"},
74   {"loc_idx", CAN_WRITE, "Localization index for firmware screens"},
75   {"mainfw_act", IS_STRING, "Active main firmware"},
76   {"mainfw_type", IS_STRING, "Active main firmware type"},
77   {"minios_priority", IS_STRING|CAN_WRITE,
78    "miniOS image to try first (A or B)"},
79   {"nvram_cleared", CAN_WRITE, "Have NV settings been lost?  Write 0 to clear"},
80   {"display_request", CAN_WRITE, "Should we initialize the display at boot?"},
81   {"phase_enforcement", 0,
82     "Board should have full security settings applied"},
83   {"recovery_reason", 0, "Recovery mode reason for current boot"},
84   {"recovery_request", CAN_WRITE, "Recovery mode request"},
85   {"recovery_subcode", CAN_WRITE, "Recovery reason subcode"},
86   {"recoverysw_boot", 0, "Recovery switch position at boot"},
87   {"recoverysw_cur", 0, "Recovery switch current position"},
88   {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
89   {"ro_fwid", IS_STRING, "Read-only firmware ID"},
90   {"tpm_attack", CAN_WRITE, "TPM was interrupted since this flag was cleared"},
91   {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
92   {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
93   {"tpm_rebooted", 0, "TPM requesting repeated reboot"},
94   {"try_ro_sync", 0, "try read only software sync"},
95   {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
96   {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
97    "LoadFirmware() debug data (not in print-all)"},
98   {"wipeout_request", CAN_WRITE, "Firmware requested factory reset (wipeout)"},
99   {"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
100   /* Terminate with null name */
101   {NULL, 0, NULL}
102 };
103 
104 /* Longest Param name. */
105 static const int kNameWidth = 23;
106 
107 
108 /* Print help */
PrintHelp(const char * progname)109 static void PrintHelp(const char *progname) {
110   const Param *p;
111 
112   printf("Usage:\n"
113          "  %s [--all]\n"
114          "    Prints all parameters with descriptions and current values.\n"
115          "    If --all is specified, prints even normally hidden fields.\n"
116          "  %s [param1 [param2 [...]]]\n"
117          "    Prints the current value(s) of the parameter(s).\n"
118          "  %s [param1=value1] [param2=value2 [...]]]\n"
119          "    Sets the parameter(s) to the specified value(s).\n"
120          "  %s [param1?value1] [param2?value2 [...]]]\n"
121          "    Checks if the parameter(s) all contain the specified value(s).\n"
122          "    Stops at the first error.\n"
123          "\n"
124          "Valid parameters:\n", progname, progname, progname, progname);
125   for (p = sys_param_list; p->name; p++) {
126     printf("  %-*s  [%s/%s] %s\n", kNameWidth, p->name,
127            (p->flags & CAN_WRITE) ? "RW" : "RO",
128            (p->flags & IS_STRING) ? "str" : "int",
129            p->desc);
130   }
131   printf("\n"
132          "For more information, please see:\n"
133          "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/"
134          "os_config.md#crossystem\n");
135 }
136 
137 
138 /* Find the parameter in the list.
139  *
140  * Returns the parameter, or NULL if no match. */
FindParam(const char * name)141 static const Param* FindParam(const char* name) {
142   const Param* p;
143   if (!name)
144     return NULL;
145   /* "legacy" term deprecated in favour of "altfw" (see: b/179458327) */
146   if (!strcasecmp(name, "dev_boot_legacy")) {
147     fprintf(stderr,
148             "!!!\n"
149             "!!! PLEASE USE 'dev_boot_altfw' INSTEAD OF 'dev_boot_legacy'\n"
150             "!!!\n");
151     name = "dev_boot_altfw";
152   }
153   for (p = sys_param_list; p->name; p++) {
154     if (!strcasecmp(p->name, name))
155       return p;
156   }
157   return NULL;
158 }
159 
160 /* Return code of SetParam() below. */
161 enum {
162   PARAM_SUCCESS = 0,
163   PARAM_ERROR_UNKNOWN,
164   PARAM_ERROR_READ_ONLY,
165   PARAM_ERROR_INVALID_INT,
166 };
167 
168 /* Set the specified parameter.
169  *
170  * Returns PARAM_SUCCESS if success, PARAM_ERROR_* if error. */
SetParam(const Param * p,const char * value)171 static int SetParam(const Param* p, const char* value) {
172   if (!(p->flags & CAN_WRITE))
173     return PARAM_ERROR_READ_ONLY;
174 
175   if (p->flags & IS_STRING) {
176     return (0 == VbSetSystemPropertyString(p->name, value) ?
177             0 : PARAM_ERROR_UNKNOWN);
178   } else {
179     char* e;
180     int i = (int)strtol(value, &e, 0);
181     if (!*value || (e && *e))
182       return PARAM_ERROR_INVALID_INT;
183     return (0 == VbSetSystemPropertyInt(p->name, i) ?
184             0 : PARAM_ERROR_UNKNOWN);
185   }
186 }
187 
188 /* Compares the parameter with the expected value.
189  *
190  * Returns 0 if success (match), non-zero if error (mismatch). */
CheckParam(const Param * p,const char * expect)191 static int CheckParam(const Param* p, const char* expect) {
192   if (p->flags & IS_STRING) {
193     char buf[VB_MAX_STRING_PROPERTY];
194   const int v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
195   if (v != 0 || 0 != strcmp(buf, expect))
196     return 1;
197   } else {
198     char* e;
199     int i = (int)strtol(expect, &e, 0);
200     int v = VbGetSystemPropertyInt(p->name);
201     if (!*expect || (e && *e))
202       return 1;
203     if (v == -1 || i != v)
204       return 1;
205   }
206   return 0;
207 }
208 
209 
210 /* Print the specified parameter.
211  *
212  * Returns 0 if success, non-zero if error. */
PrintParam(const Param * p)213 static int PrintParam(const Param* p) {
214   if (p->flags & IS_STRING) {
215     char buf[VB_MAX_STRING_PROPERTY];
216   const int v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
217   if (v != 0)
218     return 1;
219   printf("%s", buf);
220   } else {
221     int v = VbGetSystemPropertyInt(p->name);
222     if (v == -1)
223       return 1;
224     printf(p->format ? p->format : "%d", v);
225   }
226   return 0;
227 }
228 
229 
230 /* Print all parameters with descriptions.  If force_all!=0, prints even
231  * parameters that specify the NO_PRINT_ALL flag.
232  *
233  * Returns 0 if success, non-zero if error. */
PrintAllParams(int force_all)234 static int PrintAllParams(int force_all) {
235   const Param* p;
236   int retval = 0;
237   char buf[VB_MAX_STRING_PROPERTY];
238   int result = 0;
239 
240   for (p = sys_param_list; p->name; p++) {
241     if (force_all == 0 && (p->flags & NO_PRINT_ALL))
242       continue;
243     if (p->flags & IS_STRING) {
244       result = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
245     } else {
246       result = VbGetSystemPropertyInt(p->name);
247       if (result != -1)
248         snprintf(buf, sizeof(buf), p->format ? p->format : "%d", result);
249       }
250       printf("%-*s = %-30s # [%s/%s] %s\n", kNameWidth, p->name,
251             ((result != -1) ? buf : "(error)"),
252               (p->flags & CAN_WRITE) ? "RW" : "RO",
253               (p->flags & IS_STRING) ? "str" : "int",
254               p->desc);
255   }
256   return retval;
257 }
258 
259 
main(int argc,char * argv[])260 int main(int argc, char* argv[]) {
261   int retval = 0;
262   int i;
263 
264   char* progname = strrchr(argv[0], '/');
265   if (progname)
266     progname++;
267   else
268     progname = argv[0];
269 
270   /* If no args specified, print all params */
271   if (argc == 1)
272     return PrintAllParams(0);
273   /* --all or -a prints all params including normally hidden ones */
274   if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
275     return PrintAllParams(1);
276 
277   /* Print help if needed */
278   if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?") ||
279       !strcmp(argv[1], "--help")) {
280     PrintHelp(progname);
281     return 0;
282   }
283 
284   /* Otherwise, loop through params and get/set them */
285   for (i = 1; i < argc && retval == 0; i++) {
286     char* has_set = strchr(argv[i], '=');
287     char* has_expect = strchr(argv[i], '?');
288     char* name = strtok(argv[i], "=?");
289     const char* value = strtok(NULL, "=?");
290     const Param* p;
291 
292     /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
293     if (!name || has_set == argv[i] || has_expect == argv[i]) {
294       fprintf(stderr, "Poorly formed parameter\n");
295       PrintHelp(progname);
296       return 1;
297     }
298     if (!value)
299       value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
300     if (has_set && has_expect) {
301       fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
302       PrintHelp(progname);
303       return 1;
304     }
305 
306     /* Find the parameter */
307     p = FindParam(name);
308     if (!p) {
309       fprintf(stderr, "Invalid parameter name: %s\n", name);
310       PrintHelp(progname);
311       return 1;
312     }
313 
314     if (i > 1)
315       printf(" ");  /* Output params space-delimited */
316     if (has_set) {
317       retval = SetParam(p, value);
318       switch (retval) {
319       case PARAM_SUCCESS:
320         break;
321       case PARAM_ERROR_READ_ONLY:
322         fprintf(stderr, "Parameter %s is read-only\n", p->name);
323         break;
324       case PARAM_ERROR_INVALID_INT:
325         fprintf(stderr, "Value %s is not a valid integer\n", value);
326         break;
327       default:
328         fprintf(stderr, "Failed to set parameter %s\n", p->name);
329         break;
330       }
331     } else if (has_expect)
332       retval = CheckParam(p, value);
333     else
334       retval = PrintParam(p);
335   }
336 
337   return retval;
338 }
339