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