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 | |
| 14 | typedef 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 */ |
| 19 | } Param; |
| 20 | |
| 21 | /* List of parameters, terminated with a param with NULL name */ |
| 22 | const Param sys_param_list[] = { |
| 23 | /* Read-only integers */ |
| 24 | {"devsw_cur", 0, 0, "Developer switch current position"}, |
| 25 | {"devsw_boot", 0, 0, "Developer switch position at boot"}, |
| 26 | {"recoverysw_cur", 0, 0, "Recovery switch current position"}, |
| 27 | {"recoverysw_boot", 0, 0, "Recovery switch position at boot"}, |
| 28 | {"recoverysw_ec_boot", 0, 0, "Recovery switch position at EC boot"}, |
| 29 | {"wpsw_cur", 0, 0, "Firmware write protect switch current position"}, |
| 30 | {"wpsw_boot", 0, 0, "Firmware write protect switch position at boot"}, |
| 31 | /* Read-only strings */ |
| 32 | {"hwid", 1, 0, "Hardware ID"}, |
| 33 | {"fwid", 1, 0, "Active firmware ID"}, |
| 34 | {"ro_fwid", 1, 0, "Read-only firmware ID"}, |
| 35 | /* Terminate with null name */ |
| 36 | {NULL, 0, 0, NULL} |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | /* Print help */ |
| 41 | void PrintHelp(const char *progname) { |
| 42 | const Param *p; |
| 43 | |
| 44 | printf("\nUsage:\n" |
| 45 | " %s\n" |
| 46 | " Prints all parameters with descriptions and current values.\n" |
| 47 | " %s [param1 [param2 [...]]]\n" |
| 48 | " Prints the current value(s) of the parameter(s).\n" |
| 49 | " %s [param1=value1] [param2=value2 [...]]]\n" |
| 50 | " Sets the parameter(s) to the specified value(s).\n" |
| 51 | "\n" |
| 52 | "Valid parameters:\n", progname, progname, progname); |
| 53 | for (p = sys_param_list; p->name; p++) |
| 54 | printf(" %-22s %s\n", p->name, p->desc); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* Find the parameter in the list. |
| 59 | * |
| 60 | * Returns the parameter, or NULL if no match. */ |
| 61 | const Param* FindParam(const char* name) { |
| 62 | const Param* p; |
| 63 | for (p = sys_param_list; p->name; p++) { |
| 64 | if (!strcasecmp(p->name, name)) |
| 65 | return p; |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* Set the specified parameter. |
| 72 | * |
| 73 | * Returns 0 if success, non-zero if error. */ |
| 74 | int SetParam(const Param* p, const char* value) { |
| 75 | if (!p->can_write) |
| 76 | return 1; /* Parameter is read-only */ |
| 77 | |
| 78 | if (p->is_string) { |
| 79 | return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1); |
| 80 | } else { |
| 81 | char* e; |
| 82 | int i = strtol(value, &e, 0); |
| 83 | if (!*value || (e && *e)) |
| 84 | return 1; |
| 85 | return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* Print the specified parameter. |
| 91 | * |
| 92 | * Returns 0 if success, non-zero if error. */ |
| 93 | int PrintParam(const Param* p) { |
| 94 | if (p->is_string) { |
| 95 | char buf[256]; |
| 96 | const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 97 | if (!v) |
| 98 | return 1; |
| 99 | printf("%s", v); |
| 100 | } else { |
| 101 | int v = VbGetSystemPropertyInt(p->name); |
| 102 | if (v == -1) |
| 103 | return 1; |
| 104 | printf("%d", v); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /* Print all parameters with descriptions, |
| 111 | * |
| 112 | * Returns 0 if success, non-zero if error. */ |
| 113 | int PrintAllParams(void) { |
| 114 | const Param* p; |
| 115 | int retval = 0; |
| 116 | char buf[256]; |
| 117 | const char* value; |
| 118 | |
| 119 | for (p = sys_param_list; p->name; p++) { |
| 120 | if (p->is_string) { |
| 121 | value = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 122 | } else { |
| 123 | int v = VbGetSystemPropertyInt(p->name); |
| 124 | if (v == -1) |
| 125 | value = NULL; |
| 126 | else { |
| 127 | snprintf(buf, sizeof(buf), "%d", v); |
| 128 | value = buf; |
| 129 | } |
| 130 | } |
| 131 | printf("%-22s = %-20s # %s\n", |
| 132 | p->name, (value ? value : "(error)"), p->desc); |
| 133 | } |
| 134 | return retval; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | int main(int argc, char* argv[]) { |
| 139 | int retval = 0; |
| 140 | int i; |
| 141 | |
| 142 | char* progname = strrchr(argv[0], '/'); |
| 143 | if (progname) |
| 144 | progname++; |
| 145 | else |
| 146 | progname = argv[0]; |
| 147 | |
| 148 | /* If no args specified, print all params */ |
| 149 | if (argc == 1) |
| 150 | return PrintAllParams(); |
| 151 | |
| 152 | /* Print help if needed */ |
| 153 | if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { |
| 154 | PrintHelp(progname); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* Otherwise, loop through params and get/set them */ |
| 159 | for (i = 1; i < argc && retval == 0; i++) { |
| 160 | char* name = strtok(argv[i], "="); |
| 161 | char* value = strtok(NULL, "="); |
| 162 | const Param* p = FindParam(name); |
| 163 | if (!p) { |
| 164 | fprintf(stderr, "Invalid parameter name: %s\n", name); |
| 165 | PrintHelp(progname); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | if (i > 1) |
| 170 | printf(" "); /* Output params space-delimited */ |
| 171 | if (value) |
| 172 | retval = SetParam(p, value); |
| 173 | else |
| 174 | retval = PrintParam(p); |
| 175 | } |
| 176 | |
| 177 | return retval; |
| 178 | } |