| OLD | NEW |
| 1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Chrome OS firmware/system interface utility | 5 * Chrome OS firmware/system interface utility |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 void PrintHelp(const char *progname) { | 63 void PrintHelp(const char *progname) { |
| 64 const Param *p; | 64 const Param *p; |
| 65 | 65 |
| 66 printf("\nUsage:\n" | 66 printf("\nUsage:\n" |
| 67 " %s\n" | 67 " %s\n" |
| 68 " Prints all parameters with descriptions and current values.\n" | 68 " Prints all parameters with descriptions and current values.\n" |
| 69 " %s [param1 [param2 [...]]]\n" | 69 " %s [param1 [param2 [...]]]\n" |
| 70 " Prints the current value(s) of the parameter(s).\n" | 70 " Prints the current value(s) of the parameter(s).\n" |
| 71 " %s [param1=value1] [param2=value2 [...]]]\n" | 71 " %s [param1=value1] [param2=value2 [...]]]\n" |
| 72 " Sets the parameter(s) to the specified value(s).\n" | 72 " Sets the parameter(s) to the specified value(s).\n" |
| 73 " %s [param1?value1] [param2?value2 [...]]]\n" |
| 74 " Checks if the parameter(s) all contain the specified value(s).\n" |
| 75 "Stops at the first error." |
| 73 "\n" | 76 "\n" |
| 74 "Valid parameters:\n", progname, progname, progname); | 77 "Valid parameters:\n", progname, progname, progname, progname); |
| 75 for (p = sys_param_list; p->name; p++) | 78 for (p = sys_param_list; p->name; p++) |
| 76 printf(" %-22s %s\n", p->name, p->desc); | 79 printf(" %-22s %s\n", p->name, p->desc); |
| 77 } | 80 } |
| 78 | 81 |
| 79 | 82 |
| 80 /* Find the parameter in the list. | 83 /* Find the parameter in the list. |
| 81 * | 84 * |
| 82 * Returns the parameter, or NULL if no match. */ | 85 * Returns the parameter, or NULL if no match. */ |
| 83 const Param* FindParam(const char* name) { | 86 const Param* FindParam(const char* name) { |
| 84 const Param* p; | 87 const Param* p; |
| 85 for (p = sys_param_list; p->name; p++) { | 88 for (p = sys_param_list; p->name; p++) { |
| 86 if (!strcasecmp(p->name, name)) | 89 if (!strcasecmp(p->name, name)) |
| 87 return p; | 90 return p; |
| 88 } | 91 } |
| 89 return NULL; | 92 return NULL; |
| 90 } | 93 } |
| 91 | 94 |
| 92 | 95 |
| 93 /* Set the specified parameter. | 96 /* Set the specified parameter. |
| 94 * | 97 * |
| 95 * Returns 0 if success, non-zero if error. */ | 98 * Returns 0 if success, non-zero if error. */ |
| 96 int SetParam(const Param* p, const char* value) { | 99 int SetParam(const Param* p, const char* value) { |
| 97 if (!p->can_write) | 100 if (!p->can_write) |
| 98 return 1; /* Parameter is read-only */ | 101 return 1; /* Parameter is read-only */ |
| 99 | 102 |
| 100 if (p->is_string) { | 103 if (p->is_string) { |
| 101 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1); | 104 return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1); |
| 102 } else { | 105 } else { |
| 103 char* e; | 106 char* e; |
| 104 int i = strtol(value, &e, 0); | 107 int i = (int)strtol(value, &e, 0); |
| 105 if (!*value || (e && *e)) | 108 if (!*value || (e && *e)) |
| 106 return 1; | 109 return 1; |
| 107 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); | 110 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); |
| 108 } | 111 } |
| 109 } | 112 } |
| 110 | 113 |
| 111 | 114 |
| 115 /* Compares the parameter with the expected value. |
| 116 * |
| 117 * Returns 0 if success (match), non-zero if error (mismatch). */ |
| 118 int CheckParam(const Param* p, char* expect) { |
| 119 if (p->is_string) { |
| 120 char buf[256]; |
| 121 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 122 if (!v || 0 != strcmp(v, expect)) |
| 123 return 1; |
| 124 } else { |
| 125 char* e; |
| 126 int i = (int)strtol(expect, &e, 0); |
| 127 int v = VbGetSystemPropertyInt(p->name); |
| 128 if (!*expect || (e && *e)) |
| 129 return 1; |
| 130 if (v == -1 || i != v) |
| 131 return 1; |
| 132 } |
| 133 return 0; |
| 134 } |
| 135 |
| 136 |
| 112 /* Print the specified parameter. | 137 /* Print the specified parameter. |
| 113 * | 138 * |
| 114 * Returns 0 if success, non-zero if error. */ | 139 * Returns 0 if success, non-zero if error. */ |
| 115 int PrintParam(const Param* p) { | 140 int PrintParam(const Param* p) { |
| 116 if (p->is_string) { | 141 if (p->is_string) { |
| 117 char buf[256]; | 142 char buf[256]; |
| 118 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); | 143 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
| 119 if (!v) | 144 if (!v) |
| 120 return 1; | 145 return 1; |
| 121 printf("%s", v); | 146 printf("%s", v); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 return PrintAllParams(); | 197 return PrintAllParams(); |
| 173 | 198 |
| 174 /* Print help if needed */ | 199 /* Print help if needed */ |
| 175 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { | 200 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { |
| 176 PrintHelp(progname); | 201 PrintHelp(progname); |
| 177 return 0; | 202 return 0; |
| 178 } | 203 } |
| 179 | 204 |
| 180 /* Otherwise, loop through params and get/set them */ | 205 /* Otherwise, loop through params and get/set them */ |
| 181 for (i = 1; i < argc && retval == 0; i++) { | 206 for (i = 1; i < argc && retval == 0; i++) { |
| 182 char* name = strtok(argv[i], "="); | 207 int has_set = (NULL != strchr(argv[i], '=')); |
| 183 char* value = strtok(NULL, "="); | 208 int has_expect = (NULL != strchr(argv[i], '?')); |
| 209 char* name = strtok(argv[i], "=?"); |
| 210 char* value = strtok(NULL, "=?"); |
| 184 const Param* p = FindParam(name); | 211 const Param* p = FindParam(name); |
| 185 if (!p) { | 212 if (!p) { |
| 186 fprintf(stderr, "Invalid parameter name: %s\n", name); | 213 fprintf(stderr, "Invalid parameter name: %s\n", name); |
| 187 PrintHelp(progname); | 214 PrintHelp(progname); |
| 188 return 1; | 215 return 1; |
| 189 } | 216 } |
| 217 if (has_set && has_expect) { |
| 218 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n"); |
| 219 PrintHelp(progname); |
| 220 return 1; |
| 221 } |
| 190 | 222 |
| 191 if (i > 1) | 223 if (i > 1) |
| 192 printf(" "); /* Output params space-delimited */ | 224 printf(" "); /* Output params space-delimited */ |
| 193 if (value) | 225 if (has_set) |
| 194 retval = SetParam(p, value); | 226 retval = SetParam(p, value); |
| 227 else if (has_expect) |
| 228 retval = CheckParam(p, value); |
| 195 else | 229 else |
| 196 retval = PrintParam(p); | 230 retval = PrintParam(p); |
| 197 } | 231 } |
| 198 | 232 |
| 199 return retval; | 233 return retval; |
| 200 } | 234 } |
| OLD | NEW |