Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 1 | /* 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 | |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 14 | /* Max length of a string parameter */ |
| 15 | #define MAX_STRING 8192 |
| 16 | |
Vadim Bendebury | c357408 | 2011-05-02 16:23:30 -0700 | [diff] [blame] | 17 | /* |
| 18 | * Call arch specific init, if provided, otherwise use the 'weak' stub. |
| 19 | */ |
| 20 | int __VbArchInit(void) { return 0; } |
| 21 | int VbArchInit(void) __attribute__((weak, alias("__VbArchInit"))); |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 22 | /* Flags for Param */ |
| 23 | #define IS_STRING 0x01 /* String (not present = integer) */ |
| 24 | #define CAN_WRITE 0x02 /* Writable (not present = read-only */ |
| 25 | #define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when |
| 26 | * doing a print-all */ |
| 27 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 28 | typedef struct Param { |
| 29 | const char* name; /* Parameter name */ |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 30 | int flags; /* Flags (see above) */ |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 31 | const char* desc; /* Human-readable description */ |
Randall Spangler | 0ca76fc | 2011-02-24 14:33:20 -0800 | [diff] [blame] | 32 | const char* format; /* Format string, if non-NULL and 0==is_string*/ |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 33 | } Param; |
| 34 | |
| 35 | /* List of parameters, terminated with a param with NULL name */ |
| 36 | const Param sys_param_list[] = { |
Randall Spangler | 824906b | 2011-04-08 13:34:44 -0700 | [diff] [blame] | 37 | {"arch", IS_STRING, "Platform architecture"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 38 | {"cros_debug", 0, "OS should allow debug features"}, |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 39 | {"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"}, |
Randall Spangler | daa807c | 2011-07-11 10:55:18 -0700 | [diff] [blame^] | 40 | {"dev_boot_usb", CAN_WRITE, |
| 41 | "Enable developer mode boot from USB (writable)"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 42 | {"devsw_boot", 0, "Developer switch position at boot"}, |
| 43 | {"devsw_cur", 0, "Developer switch current position"}, |
| 44 | {"ecfw_act", IS_STRING, "Active EC firmware"}, |
| 45 | {"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"}, |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 46 | {"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 47 | {"fwid", IS_STRING, "Active firmware ID"}, |
Randall Spangler | 4115b89 | 2011-04-08 14:11:48 -0700 | [diff] [blame] | 48 | {"fwupdate_tries", CAN_WRITE, |
| 49 | "Times to try OS firmware update (writable, inside kern_nv)"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 50 | {"hwid", IS_STRING, "Hardware ID"}, |
| 51 | {"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"}, |
| 52 | {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"}, |
| 53 | {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"}, |
| 54 | {"mainfw_act", IS_STRING, "Active main firmware"}, |
| 55 | {"mainfw_type", IS_STRING, "Active main firmware type"}, |
| 56 | {"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"}, |
| 57 | {"recovery_reason", 0, "Recovery mode reason for current boot"}, |
| 58 | {"recovery_request", CAN_WRITE, "Recovery mode request (writable)"}, |
| 59 | {"recoverysw_boot", 0, "Recovery switch position at boot"}, |
| 60 | {"recoverysw_cur", 0, "Recovery switch current position"}, |
| 61 | {"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"}, |
| 62 | {"ro_fwid", IS_STRING, "Read-only firmware ID"}, |
| 63 | {"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"}, |
| 64 | {"savedmem_size", 0, "RAM debug data area size in bytes"}, |
| 65 | {"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"}, |
| 66 | {"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"}, |
| 67 | {"tried_fwb", 0, "Tried firmware B before A this boot"}, |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 68 | {"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"}, |
| 69 | {"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 70 | {"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"}, |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 71 | {"vdat_lfdebug", IS_STRING|NO_PRINT_ALL, |
| 72 | "LoadFirmware() debug data (not in print-all)"}, |
| 73 | {"vdat_lkdebug", IS_STRING|NO_PRINT_ALL, |
| 74 | "LoadKernel() debug data (not in print-all)"}, |
Randall Spangler | 207825b | 2011-04-20 10:54:40 -0700 | [diff] [blame] | 75 | {"vdat_timers", IS_STRING, "Timer values from VbSharedData"}, |
| 76 | {"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"}, |
| 77 | {"wpsw_cur", 0, "Firmware write protect hardware switch current position"}, |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 78 | /* Terminate with null name */ |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 79 | {NULL, 0, NULL} |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | |
| 83 | /* Print help */ |
| 84 | void PrintHelp(const char *progname) { |
| 85 | const Param *p; |
| 86 | |
| 87 | printf("\nUsage:\n" |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 88 | " %s [--all]\n" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 89 | " Prints all parameters with descriptions and current values.\n" |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 90 | " If --all is specified, prints even normally hidden fields.\n" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 91 | " %s [param1 [param2 [...]]]\n" |
| 92 | " Prints the current value(s) of the parameter(s).\n" |
| 93 | " %s [param1=value1] [param2=value2 [...]]]\n" |
| 94 | " Sets the parameter(s) to the specified value(s).\n" |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 95 | " %s [param1?value1] [param2?value2 [...]]]\n" |
| 96 | " Checks if the parameter(s) all contain the specified value(s).\n" |
| 97 | "Stops at the first error." |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 98 | "\n" |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 99 | "Valid parameters:\n", progname, progname, progname, progname); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 100 | for (p = sys_param_list; p->name; p++) |
| 101 | printf(" %-22s %s\n", p->name, p->desc); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* Find the parameter in the list. |
| 106 | * |
| 107 | * Returns the parameter, or NULL if no match. */ |
| 108 | const Param* FindParam(const char* name) { |
| 109 | const Param* p; |
Randall Spangler | f27583f | 2011-03-21 16:58:54 -0700 | [diff] [blame] | 110 | if (!name) |
| 111 | return NULL; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 112 | for (p = sys_param_list; p->name; p++) { |
| 113 | if (!strcasecmp(p->name, name)) |
| 114 | return p; |
| 115 | } |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* Set the specified parameter. |
| 121 | * |
| 122 | * Returns 0 if success, non-zero if error. */ |
| 123 | int SetParam(const Param* p, const char* value) { |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 124 | if (!(p->flags & CAN_WRITE)) |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 125 | return 1; /* Parameter is read-only */ |
| 126 | |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 127 | if (p->flags & IS_STRING) { |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 128 | return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1); |
| 129 | } else { |
| 130 | char* e; |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 131 | int i = (int)strtol(value, &e, 0); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 132 | if (!*value || (e && *e)) |
| 133 | return 1; |
| 134 | return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 139 | /* Compares the parameter with the expected value. |
| 140 | * |
| 141 | * Returns 0 if success (match), non-zero if error (mismatch). */ |
| 142 | int CheckParam(const Param* p, char* expect) { |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 143 | if (p->flags & IS_STRING) { |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 144 | char buf[MAX_STRING]; |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 145 | const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 146 | if (!v || 0 != strcmp(v, expect)) |
| 147 | return 1; |
| 148 | } else { |
| 149 | char* e; |
| 150 | int i = (int)strtol(expect, &e, 0); |
| 151 | int v = VbGetSystemPropertyInt(p->name); |
| 152 | if (!*expect || (e && *e)) |
| 153 | return 1; |
| 154 | if (v == -1 || i != v) |
| 155 | return 1; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 161 | /* Print the specified parameter. |
| 162 | * |
| 163 | * Returns 0 if success, non-zero if error. */ |
| 164 | int PrintParam(const Param* p) { |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 165 | if (p->flags & IS_STRING) { |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 166 | char buf[MAX_STRING]; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 167 | const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 168 | if (!v) |
| 169 | return 1; |
| 170 | printf("%s", v); |
| 171 | } else { |
| 172 | int v = VbGetSystemPropertyInt(p->name); |
| 173 | if (v == -1) |
| 174 | return 1; |
Randall Spangler | 0ca76fc | 2011-02-24 14:33:20 -0800 | [diff] [blame] | 175 | printf(p->format ? p->format : "%d", v); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 181 | /* Print all parameters with descriptions. If force_all!=0, prints even |
| 182 | * parameters that specify the NO_PRINT_ALL flag. |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 183 | * |
| 184 | * Returns 0 if success, non-zero if error. */ |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 185 | int PrintAllParams(int force_all) { |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 186 | const Param* p; |
| 187 | int retval = 0; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 188 | char buf[MAX_STRING]; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 189 | const char* value; |
| 190 | |
| 191 | for (p = sys_param_list; p->name; p++) { |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 192 | if (0 == force_all && (p->flags & NO_PRINT_ALL)) |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 193 | continue; |
| 194 | if (p->flags & IS_STRING) { |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 195 | value = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 196 | } else { |
| 197 | int v = VbGetSystemPropertyInt(p->name); |
| 198 | if (v == -1) |
| 199 | value = NULL; |
| 200 | else { |
Randall Spangler | 0ca76fc | 2011-02-24 14:33:20 -0800 | [diff] [blame] | 201 | snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 202 | value = buf; |
| 203 | } |
| 204 | } |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 205 | printf("%-22s = %-30s # %s\n", |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 206 | p->name, (value ? value : "(error)"), p->desc); |
| 207 | } |
| 208 | return retval; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | int main(int argc, char* argv[]) { |
| 213 | int retval = 0; |
| 214 | int i; |
| 215 | |
| 216 | char* progname = strrchr(argv[0], '/'); |
| 217 | if (progname) |
| 218 | progname++; |
| 219 | else |
| 220 | progname = argv[0]; |
| 221 | |
Vadim Bendebury | c357408 | 2011-05-02 16:23:30 -0700 | [diff] [blame] | 222 | if (VbArchInit()) { |
| 223 | fprintf(stderr, "Failed to initialize\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 227 | /* If no args specified, print all params */ |
| 228 | if (argc == 1) |
Randall Spangler | 55af5b5 | 2011-04-08 14:26:46 -0700 | [diff] [blame] | 229 | return PrintAllParams(0); |
| 230 | /* --all or -a prints all params including normally hidden ones */ |
| 231 | if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a")) |
| 232 | return PrintAllParams(1); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 233 | |
| 234 | /* Print help if needed */ |
| 235 | if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { |
| 236 | PrintHelp(progname); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* Otherwise, loop through params and get/set them */ |
| 241 | for (i = 1; i < argc && retval == 0; i++) { |
Randall Spangler | f27583f | 2011-03-21 16:58:54 -0700 | [diff] [blame] | 242 | char* has_set = strchr(argv[i], '='); |
| 243 | char* has_expect = strchr(argv[i], '?'); |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 244 | char* name = strtok(argv[i], "=?"); |
| 245 | char* value = strtok(NULL, "=?"); |
Randall Spangler | f27583f | 2011-03-21 16:58:54 -0700 | [diff] [blame] | 246 | const Param* p; |
| 247 | |
| 248 | /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */ |
| 249 | if (!name || has_set == argv[i] || has_expect == argv[i]) { |
| 250 | fprintf(stderr, "Poorly formed parameter\n"); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 251 | PrintHelp(progname); |
| 252 | return 1; |
| 253 | } |
Randall Spangler | f27583f | 2011-03-21 16:58:54 -0700 | [diff] [blame] | 254 | if (!value) |
| 255 | value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */ |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 256 | if (has_set && has_expect) { |
| 257 | fprintf(stderr, "Use either = or ? in a parameter, but not both.\n"); |
| 258 | PrintHelp(progname); |
| 259 | return 1; |
| 260 | } |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 261 | |
Randall Spangler | f27583f | 2011-03-21 16:58:54 -0700 | [diff] [blame] | 262 | /* Find the parameter */ |
| 263 | p = FindParam(name); |
| 264 | if (!p) { |
| 265 | fprintf(stderr, "Invalid parameter name: %s\n", name); |
| 266 | PrintHelp(progname); |
| 267 | return 1; |
| 268 | } |
| 269 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 270 | if (i > 1) |
| 271 | printf(" "); /* Output params space-delimited */ |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 272 | if (has_set) |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 273 | retval = SetParam(p, value); |
Randall Spangler | 227f792 | 2011-03-11 13:34:56 -0800 | [diff] [blame] | 274 | else if (has_expect) |
| 275 | retval = CheckParam(p, value); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 276 | else |
| 277 | retval = PrintParam(p); |
| 278 | } |
| 279 | |
| 280 | return retval; |
| 281 | } |