summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-03-12 16:34:15 -0800
committerRandall Spangler <rspangler@chromium.org>2011-03-12 16:34:15 -0800
commit4a16d08b03181c749e3bd64e2eb3caa036a54474 (patch)
tree28bbf77640005a32f090b9e348300adf7e358764
parent95c4031ce903258036beeed0705d25c7e9d25da0 (diff)
downloadvboot-4a16d08b03181c749e3bd64e2eb3caa036a54474.tar.gz
Add cros_debug option to crossystem (R11 branch)
Add check-value support and check /proc/cmdline for cros_nodebug Change-Id: I35158810184be03f18d98893e4dd640088384579 BUG=12904 TEST=manual crossystem fwb_tries=1 crossystem fwb_tries?1 && echo YES || echo NO --> YES crossystem fwb_tries?0x01 && echo YES || echo NO --> YES crossystem fwb_tries?0 && echo YES || echo NO --> NO crossystem fwb_tries=0 crossystem fwb_tries?0 && echo YES || echo NO --> YES crossystem fwb_tries?1 && echo YES || echo NO --> NO crossystem fwb_tries?0x01 && echo YES || echo NO --> NO crossystem ecfw_act --> RW (if it's not, change RW to RO in the tests below) crossystem ecfw_act?RW && echo YES || echo NO --> YES crossystem ecfw_act?BOB && echo YES || echo NO --> NO For the following tests, boot Alex with dev switch on and fwb_tries=1 Expected output of `crossystem mainfw_type mainfw_act cros_debug` under each of the following scenarios: * Neither "cros_debug" nor" cros_nodebug" in kernel command line: normal B 1 * Kernel command line changed to include "cros_nodebug": normal B 0 * Kernel command line changed to include "cros_nodebugg": normal B 1 * Kernel command line changed to include "ccros_nodebug": normal B 1 Review URL: http://codereview.chromium.org/6665005 (cherry picked from commit 227f792c1ba64eb8dd3474395659e27696fcf355) Add cros_debug option to crossystem Change-Id: Ie62364a87f7f144ee647054d2a9ef83522cdbe7d BUG=12904 TEST=manual Expected output of `crossystem mainfw_type cros_debug` under each of the following scenarios: * Boot Alex with dev switch off: normal 0 * Boot Alex with dev switch on (and dev firmware): developer 1 * Boot Alex with dev switch on (and normal firmware): normal 1 * Boot Alex with recovery firmware: recovery 0 * Boot Alex with dev switch off, then turn the dev switch on after booting: normal 0 * Boot Cr-48 with dev switch off: normal 0 * Boot Cr-48 with dev switch on: developer 1 * Boot Cr-48 with recovery firmware: recovery 0 * Boot Alex with dev switch off and kernel command line changed to include "cros_debug": normal 1 * Boot Alex with dev switch off and kernel command line changed to include "cros_debugg": normal 0 * Boot Alex with dev switch off and kernel command line changed to include "ccros_debug": normal 0 * Boot H2O BIOS with kernel command line changed to include "cros_debug": nonchrome 1 * Boot H2O BIOS with kernel command line changed to include "cros_debugg": nonchrome 0 * Boot H2O BIOS with kernel command line changed to include "ccros_debug": nonchrome 0 Review URL: http://codereview.chromium.org/6659021 (cherry picked from commit 196e1772ed9092a679767c0497343799f13d2875) Review URL: http://codereview.chromium.org/6680010
-rw-r--r--host/lib/crossystem.c56
-rw-r--r--utility/crossystem_main.c45
2 files changed, 92 insertions, 9 deletions
diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c
index 5951e10b..04dc9756 100644
--- a/host/lib/crossystem.c
+++ b/host/lib/crossystem.c
@@ -54,6 +54,10 @@
#define BINF0_S3_RESUME_FAILED 10
/* Recovery caused by TPM error */
#define BINF0_RECOVERY_TPM_ERROR 11
+/* Firmware types from BINF.3 */
+#define BINF3_RECOVERY 0
+#define BINF3_NORMAL 1
+#define BINF3_DEVELOPER 2
/* Base name for ACPI files */
#define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
@@ -69,9 +73,12 @@
#define GPIO_BASE_PATH "/sys/class/gpio"
#define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
-/* Base name for NVRAM file */
+/* Filename for NVRAM file */
#define NVRAM_PATH "/dev/nvram"
+/* Filename for kernel command line */
+#define KERNEL_CMDLINE_PATH "/proc/cmdline"
+
/* Copy up to dest_size-1 characters from src to dest, ensuring null
termination (which strncpy() doesn't do). Returns the destination
@@ -414,11 +421,11 @@ const char* VbReadMainFwType(char* dest, int size) {
/* Try reading type from BINF.3 */
switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
- case 0:
+ case BINF3_RECOVERY:
return StrCopy(dest, "recovery", size);
- case 1:
+ case BINF3_NORMAL:
return StrCopy(dest, "normal", size);
- case 2:
+ case BINF3_DEVELOPER:
return StrCopy(dest, "developer", size);
default:
break; /* Fall through to legacy handling */
@@ -450,6 +457,45 @@ const char* VbReadMainFwType(char* dest, int size) {
}
+/* Determine whether OS-level debugging should be allowed. Passed the
+ * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
+int VbGetCrosDebug(void) {
+ FILE* f = NULL;
+ char buf[4096] = "";
+ int binf3;
+ char *t, *saveptr;
+
+ /* Try reading firmware type from BINF.3. */
+ binf3 = ReadFileInt(ACPI_BINF_PATH ".3");
+ if (BINF3_RECOVERY == binf3)
+ return 0; /* Recovery mode never allows debug. */
+ else if (BINF3_DEVELOPER == binf3)
+ return 1; /* Developer firmware always allows debug. */
+
+ /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
+ * For all these cases, check /proc/cmdline for cros_[no]debug. */
+ f = fopen(KERNEL_CMDLINE_PATH, "rt");
+ if (f) {
+ if (NULL == fgets(buf, sizeof(buf), f))
+ *buf = 0;
+ fclose(f);
+ }
+ for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
+ if (0 == strcmp(t, "cros_debug"))
+ return 1;
+ else if (0 == strcmp(t, "cros_nodebug"))
+ return 0;
+ }
+
+ /* Normal new firmware or older Chrome OS firmware allows debug if the
+ * dev switch is on. */
+ if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT))
+ return 1;
+
+ /* All other cases disallow debug. */
+ return 0;
+}
+
/* Read a system property integer.
*
@@ -512,6 +558,8 @@ int VbGetSystemPropertyInt(const char* name) {
return VbGetRecoveryReason();
} else if (!strcasecmp(name,"fmap_base")) {
value = ReadFileInt(ACPI_FMAP_PATH);
+ } else if (!strcasecmp(name,"cros_debug")) {
+ value = VbGetCrosDebug();
}
return value;
diff --git a/utility/crossystem_main.c b/utility/crossystem_main.c
index 35d6ea70..90ad718b 100644
--- a/utility/crossystem_main.c
+++ b/utility/crossystem_main.c
@@ -34,6 +34,7 @@ const Param sys_param_list[] = {
{"savedmem_size", 0, 0, "RAM debug data area size in bytes"},
{"fmap_base", 0, 0, "Main firmware flashmap physical address", "0x%08x"},
{"tried_fwb", 0, 0, "Tried firmware B before A this boot"},
+ {"cros_debug", 0, 0, "OS should allow debug features"},
/* Read-only strings */
{"hwid", 1, 0, "Hardware ID"},
{"fwid", 1, 0, "Active firmware ID"},
@@ -69,8 +70,11 @@ void PrintHelp(const char *progname) {
" Prints the current value(s) of the parameter(s).\n"
" %s [param1=value1] [param2=value2 [...]]]\n"
" Sets the parameter(s) to the specified value(s).\n"
+ " %s [param1?value1] [param2?value2 [...]]]\n"
+ " Checks if the parameter(s) all contain the specified value(s).\n"
+ "Stops at the first error."
"\n"
- "Valid parameters:\n", progname, progname, progname);
+ "Valid parameters:\n", progname, progname, progname, progname);
for (p = sys_param_list; p->name; p++)
printf(" %-22s %s\n", p->name, p->desc);
}
@@ -100,7 +104,7 @@ int SetParam(const Param* p, const char* value) {
return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
} else {
char* e;
- int i = strtol(value, &e, 0);
+ int i = (int)strtol(value, &e, 0);
if (!*value || (e && *e))
return 1;
return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
@@ -108,6 +112,28 @@ int SetParam(const Param* p, const char* value) {
}
+/* Compares the parameter with the expected value.
+ *
+ * Returns 0 if success (match), non-zero if error (mismatch). */
+int CheckParam(const Param* p, char* expect) {
+ if (p->is_string) {
+ char buf[256];
+ const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
+ if (!v || 0 != strcmp(v, expect))
+ return 1;
+ } else {
+ char* e;
+ int i = (int)strtol(expect, &e, 0);
+ int v = VbGetSystemPropertyInt(p->name);
+ if (!*expect || (e && *e))
+ return 1;
+ if (v == -1 || i != v)
+ return 1;
+ }
+ return 0;
+}
+
+
/* Print the specified parameter.
*
* Returns 0 if success, non-zero if error. */
@@ -178,19 +204,28 @@ int main(int argc, char* argv[]) {
/* Otherwise, loop through params and get/set them */
for (i = 1; i < argc && retval == 0; i++) {
- char* name = strtok(argv[i], "=");
- char* value = strtok(NULL, "=");
+ int has_set = (NULL != strchr(argv[i], '='));
+ int has_expect = (NULL != strchr(argv[i], '?'));
+ char* name = strtok(argv[i], "=?");
+ char* value = strtok(NULL, "=?");
const Param* p = FindParam(name);
if (!p) {
fprintf(stderr, "Invalid parameter name: %s\n", name);
PrintHelp(progname);
return 1;
}
+ if (has_set && has_expect) {
+ fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
+ PrintHelp(progname);
+ return 1;
+ }
if (i > 1)
printf(" "); /* Output params space-delimited */
- if (value)
+ if (has_set)
retval = SetParam(p, value);
+ else if (has_expect)
+ retval = CheckParam(p, value);
else
retval = PrintParam(p);
}