Files
OpenCellular/utility/crossystem_main.c
Vadim Bendebury c3574086a8 Introduce arm support in crossystem.
This CL builds upon earlier firmware and kernel changes (see CLs
related to the same bug, chromium-os:12522).

ARM firmware now simulates both Nvram storage and VDAT buffer, the
structures the x86 version uses extensively to communicate back and
forth between firmware/kernel/userland.

So, to make crossystem work on arm, all what's needed is to provide
architecture specific interface to Nvram and VDAT simulation, and
architecture specific processing for variables which are accessed on
ARM platforms in a different way.

The few discrepancies and platform specifics which had to be addressed
for ARM specifically are as follows:

- the Nvram contents are cached in the shared memory and available for
  reading as part of /sys/kernel/debug/chromeos_arm. When writing
  Nvram, the same file needs to be written, but only the 16 bytes
  (representing the Nvram contents) are aacepted.

- the VDAT buffer also comes from the shared memory (as part of the
  same sysfs file)

- when crossystem starts, it needs to read in this shared memory
  contents, a` weak' function VbArchInit() is being added such that it
  is provided on ARM platforms only, on x86 an empty stub is called.

- current developer/recovery request/ro firmware switch states are
  retrieved through GPIO drivers. The GPIO numbers are defined in the
  file, the GPIO driver is supposed to be configured before
  crsossystem can operate.

- the BINF values are supplied through an array within shared memory,
  it would be easy to refactor both x86 and ARM use the same code to
  process BINF values, but with this submission the code is duplicated
  to minimize x86 impact.

- the following crossystem variables do not have ARM equivalents,
  thier values are reported as '(error)':

   recoverysw_ec_boot
   savedmem_base
   savedmem_size

BUG=chromium-os:12522
TEST=manual:

. bring up a kaen system
. execute the following script to enable the appropriate GPIOSs:

 for gpio in 56 59 168; do echo $gpio > /sys/class/gpio/export; done

. run `crossystem' and observe reasonable output values

. to verify that it reads GPIOs properly, try

  echo $(./crossystem recoverysw_cur)

  with the miniservo 'GOOG_REC' button pressed and released, observe
  different readings (note that the state of the button is reversed,
  the released button is reported as '1')

. to verify the write capabilities, note that the nvram contents can
  be accessed using the following shell commands

     echo 3 > /proc/sys/vm/drop_caches
     2>/dev/null dd if=/dev/mmcblk0 of=/tmp/blk bs=16 count=1 && \
       od -t x1 /tmp/blk | head -1

 (the first command cause the device cache dropped, and the second
 command accesses the device contents.

   vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
   localhost var # echo $(./crossystem fwb_tries)
   10
   localhost var # echo 3 > /proc/sys/vm/drop_caches
   localhost var # 2>/dev/null dd if=/dev/mmcblk0 of=/tmp/blk bs=16 count=1 && od -t x1 /tmp/blk | head -1
   0000000 60 0a 00 be 00 00 00 00 00 00 00 02 00 00 00 a2
   localhost var # ./crossystem fwb_tries=9
   localhost var # echo $(./crossystem fwb_tries)
   9
   localhost var # echo 3 > /proc/sys/vm/drop_caches
   localhost var # 2>/dev/null dd if=/dev/mmcblk0 of=/tmp/blk bs=16 count=1 && od -t x1 /tmp/blk | head -1
   0000000 60 09 00 be 00 00 00 00 00 00 00 02 00 00 00 8a
   localhost var #
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Change-Id: Ie4c6ff44441d98a42b1057953208fdb90c08f46d
Reviewed-on: http://gerrit.chromium.org/gerrit/113
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Tested-by: Vadim Bendebury <vbendeb@chromium.org>
2011-05-05 14:42:09 -07:00

280 lines
8.8 KiB
C

/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Chrome OS firmware/system interface utility
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crossystem.h"
/* Max length of a string parameter */
#define MAX_STRING 8192
/*
* Call arch specific init, if provided, otherwise use the 'weak' stub.
*/
int __VbArchInit(void) { return 0; }
int VbArchInit(void) __attribute__((weak, alias("__VbArchInit")));
/* Flags for Param */
#define IS_STRING 0x01 /* String (not present = integer) */
#define CAN_WRITE 0x02 /* Writable (not present = read-only */
#define NO_PRINT_ALL 0x04 /* Don't print contents of parameter when
* doing a print-all */
typedef struct Param {
const char* name; /* Parameter name */
int flags; /* Flags (see above) */
const char* desc; /* Human-readable description */
const char* format; /* Format string, if non-NULL and 0==is_string*/
} Param;
/* List of parameters, terminated with a param with NULL name */
const Param sys_param_list[] = {
{"arch", IS_STRING, "Platform architecture"},
{"cros_debug", 0, "OS should allow debug features"},
{"dbg_reset", CAN_WRITE, "Debug reset mode request (writable)"},
{"devsw_boot", 0, "Developer switch position at boot"},
{"devsw_cur", 0, "Developer switch current position"},
{"ecfw_act", IS_STRING, "Active EC firmware"},
{"fmap_base", 0, "Main firmware flashmap physical address", "0x%08x"},
{"fwb_tries", CAN_WRITE, "Try firmware B count (writable)"},
{"fwid", IS_STRING, "Active firmware ID"},
{"fwupdate_tries", CAN_WRITE,
"Times to try OS firmware update (writable, inside kern_nv)"},
{"hwid", IS_STRING, "Hardware ID"},
{"kern_nv", CAN_WRITE, "Non-volatile field for kernel use", "0x%08x"},
{"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"},
{"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"},
{"mainfw_act", IS_STRING, "Active main firmware"},
{"mainfw_type", IS_STRING, "Active main firmware type"},
{"nvram_cleared", CAN_WRITE, "Have NV settings been lost? Write 0 to clear"},
{"recovery_reason", 0, "Recovery mode reason for current boot"},
{"recovery_request", CAN_WRITE, "Recovery mode request (writable)"},
{"recoverysw_boot", 0, "Recovery switch position at boot"},
{"recoverysw_cur", 0, "Recovery switch current position"},
{"recoverysw_ec_boot", 0, "Recovery switch position at EC boot"},
{"ro_fwid", IS_STRING, "Read-only firmware ID"},
{"savedmem_base", 0, "RAM debug data area physical address", "0x%08x"},
{"savedmem_size", 0, "RAM debug data area size in bytes"},
{"tpm_fwver", 0, "Firmware version stored in TPM", "0x%08x"},
{"tpm_kernver", 0, "Kernel version stored in TPM", "0x%08x"},
{"tried_fwb", 0, "Tried firmware B before A this boot"},
{"vbtest_errfunc", CAN_WRITE, "Verified boot test error function (writable)"},
{"vbtest_errno", CAN_WRITE, "Verified boot test error number (writable)"},
{"vdat_flags", 0, "Flags from VbSharedData", "0x%08x"},
{"vdat_lfdebug", IS_STRING|NO_PRINT_ALL,
"LoadFirmware() debug data (not in print-all)"},
{"vdat_lkdebug", IS_STRING|NO_PRINT_ALL,
"LoadKernel() debug data (not in print-all)"},
{"vdat_timers", IS_STRING, "Timer values from VbSharedData"},
{"wpsw_boot", 0, "Firmware write protect hardware switch position at boot"},
{"wpsw_cur", 0, "Firmware write protect hardware switch current position"},
/* Terminate with null name */
{NULL, 0, NULL}
};
/* Print help */
void PrintHelp(const char *progname) {
const Param *p;
printf("\nUsage:\n"
" %s [--all]\n"
" Prints all parameters with descriptions and current values.\n"
" If --all is specified, prints even normally hidden fields.\n"
" %s [param1 [param2 [...]]]\n"
" 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, progname);
for (p = sys_param_list; p->name; p++)
printf(" %-22s %s\n", p->name, p->desc);
}
/* Find the parameter in the list.
*
* Returns the parameter, or NULL if no match. */
const Param* FindParam(const char* name) {
const Param* p;
if (!name)
return NULL;
for (p = sys_param_list; p->name; p++) {
if (!strcasecmp(p->name, name))
return p;
}
return NULL;
}
/* Set the specified parameter.
*
* Returns 0 if success, non-zero if error. */
int SetParam(const Param* p, const char* value) {
if (!(p->flags & CAN_WRITE))
return 1; /* Parameter is read-only */
if (p->flags & IS_STRING) {
return (0 == VbSetSystemPropertyString(p->name, value) ? 0 : 1);
} else {
char* e;
int i = (int)strtol(value, &e, 0);
if (!*value || (e && *e))
return 1;
return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1);
}
}
/* 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->flags & IS_STRING) {
char buf[MAX_STRING];
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. */
int PrintParam(const Param* p) {
if (p->flags & IS_STRING) {
char buf[MAX_STRING];
const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
if (!v)
return 1;
printf("%s", v);
} else {
int v = VbGetSystemPropertyInt(p->name);
if (v == -1)
return 1;
printf(p->format ? p->format : "%d", v);
}
return 0;
}
/* Print all parameters with descriptions. If force_all!=0, prints even
* parameters that specify the NO_PRINT_ALL flag.
*
* Returns 0 if success, non-zero if error. */
int PrintAllParams(int force_all) {
const Param* p;
int retval = 0;
char buf[MAX_STRING];
const char* value;
for (p = sys_param_list; p->name; p++) {
if (0 == force_all && (p->flags & NO_PRINT_ALL))
continue;
if (p->flags & IS_STRING) {
value = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
} else {
int v = VbGetSystemPropertyInt(p->name);
if (v == -1)
value = NULL;
else {
snprintf(buf, sizeof(buf), p->format ? p->format : "%d", v);
value = buf;
}
}
printf("%-22s = %-30s # %s\n",
p->name, (value ? value : "(error)"), p->desc);
}
return retval;
}
int main(int argc, char* argv[]) {
int retval = 0;
int i;
char* progname = strrchr(argv[0], '/');
if (progname)
progname++;
else
progname = argv[0];
if (VbArchInit()) {
fprintf(stderr, "Failed to initialize\n");
return -1;
}
/* If no args specified, print all params */
if (argc == 1)
return PrintAllParams(0);
/* --all or -a prints all params including normally hidden ones */
if (!strcasecmp(argv[1], "--all") || !strcmp(argv[1], "-a"))
return PrintAllParams(1);
/* Print help if needed */
if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) {
PrintHelp(progname);
return 0;
}
/* Otherwise, loop through params and get/set them */
for (i = 1; i < argc && retval == 0; i++) {
char* has_set = strchr(argv[i], '=');
char* has_expect = strchr(argv[i], '?');
char* name = strtok(argv[i], "=?");
char* value = strtok(NULL, "=?");
const Param* p;
/* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
if (!name || has_set == argv[i] || has_expect == argv[i]) {
fprintf(stderr, "Poorly formed parameter\n");
PrintHelp(progname);
return 1;
}
if (!value)
value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
if (has_set && has_expect) {
fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
PrintHelp(progname);
return 1;
}
/* Find the parameter */
p = FindParam(name);
if (!p) {
fprintf(stderr, "Invalid parameter name: %s\n", name);
PrintHelp(progname);
return 1;
}
if (i > 1)
printf(" "); /* Output params space-delimited */
if (has_set)
retval = SetParam(p, value);
else if (has_expect)
retval = CheckParam(p, value);
else
retval = PrintParam(p);
}
return retval;
}