km/common: implement boardId HWkey checks as u-boot cmd
BoardId and HWKey are used to identify the HW class of a given board.
The correct values are stored in the inventory eeprom. During creation
time of a boot package the boardId and HWkey for the SW is stored in
the default environment and burned into the flash. During boottime
the values in the inventory and in the environment are compared to
avoid starting of a SW which is not authorized for this board.
Some bootpackages are allowed to run on a set of different boardId
hwKey. In this case the environment variable boardIdListHex was added
to the default environment. In this case the command iterates over the
pair values and compares them with the values read from the inventory
eeprom.
The syntax of such a boardIdListHex value is e.g.: 158_1 159_1 159_2
Signed-off-by: Thomas Herzmann <thomas.herzmann@keymile.com>
Signed-off-by: Holger Brunck <holger.brunck@keymile.com>
Signed-off-by: Valentin Longchamp <valentin.longchamp@keymile.com>
Acked-by: Heiko Schocher <hs@denx.de>
cc: Wolfgang Denk <wd@denx.de>
cc: Detlev Zundel <dzu@denx.de>
diff --git a/board/keymile/common/common.c b/board/keymile/common/common.c
index 9adfefa..9be4c72 100644
--- a/board/keymile/common/common.c
+++ b/board/keymile/common/common.c
@@ -32,6 +32,7 @@
#include <net.h>
#include <netdev.h>
#include <asm/io.h>
+#include <linux/ctype.h>
#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
#include <libfdt.h>
@@ -716,3 +717,158 @@
U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
"hwkey from IVM and set in environment");
+
+/*
+ * command km_checkbidhwk
+ * if "boardid" and "hwkey" are not already set in the environment, do:
+ * if a "boardIdListHex" exists in the environment:
+ * - read ivm data for boardid and hwkey
+ * - compare each entry of the boardIdListHex with the
+ * IVM data:
+ * if match:
+ * set environment variables boardid, boardId,
+ * hwkey, hwKey to the found values
+ * both (boardid and boardId) are set because
+ * they might be used differently in the
+ * application and in the init scripts (?)
+ * return 0 in case of match, 1 if not match or error
+ */
+int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ unsigned long ivmbid = 0, ivmhwkey = 0;
+ unsigned long envbid = 0, envhwkey = 0;
+ char *p;
+ int verbose = argc > 1 && *argv[1] == 'v';
+ int rc = 0;
+
+ /*
+ * first read out the real inventory values, these values are
+ * already stored in the local hush variables
+ */
+ p = get_local_var("IVM_BoardId");
+ if (p == NULL) {
+ printf("can't get the IVM_Boardid\n");
+ return 1;
+ }
+ rc = strict_strtoul(p, 16, &ivmbid);
+
+ p = get_local_var("IVM_HWKey");
+ if (p == NULL) {
+ printf("can't get the IVM_HWKey\n");
+ return 1;
+ }
+ rc = strict_strtoul(p, 16, &ivmhwkey);
+
+ if (!ivmbid || !ivmhwkey) {
+ printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
+ return rc;
+ }
+
+ /* now try to read values from environment if available */
+ p = getenv("boardid");
+ if (p != NULL)
+ rc = strict_strtoul(p, 16, &envbid);
+ p = getenv("hwkey");
+ if (p != NULL)
+ rc = strict_strtoul(p, 16, &envhwkey);
+
+ if (rc != 0) {
+ printf("strict_strtoul returns error: %d", rc);
+ return rc;
+ }
+
+ if (!envbid || !envhwkey) {
+ /*
+ * BoardId/HWkey not available in the environment, so try the
+ * environment variable for BoardId/HWkey list
+ */
+ char *bidhwklist = getenv("boardIdListHex");
+
+ if (bidhwklist) {
+ int found = 0;
+ char *rest = bidhwklist;
+ char *endp;
+
+ if (verbose) {
+ printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
+ ivmbid, ivmhwkey);
+ printf("boardIdHwKeyList: %s\n",
+ bidhwklist);
+ }
+ while (!found) {
+ /* loop over each bid/hwkey pair in the list */
+ unsigned long bid = 0;
+ unsigned long hwkey = 0;
+
+ while (*rest && !isxdigit(*rest))
+ rest++;
+ /*
+ * use simple_strtoul because we need &end and
+ * we know we got non numeric char at the end
+ */
+ bid = simple_strtoul(rest, &endp, 16);
+ /* BoardId and HWkey are separated with a "_" */
+ if (*endp == '_') {
+ rest = endp + 1;
+ /*
+ * use simple_strtoul because we need
+ * &end
+ */
+ hwkey = simple_strtoul(rest, &endp, 16);
+ rest = endp;
+ while (*rest && !isxdigit(*rest))
+ rest++;
+ }
+ if ((!bid) || (!hwkey)) {
+ /* end of list */
+ break;
+ }
+ if (verbose) {
+ printf("trying bid=0x%lX, hwkey=%ld\n",
+ bid, hwkey);
+ }
+ /*
+ * Compare the values of the found entry in the
+ * list with the valid values which are stored
+ * in the inventory eeprom. If they are equal
+ * store the values in environment variables
+ * and save the environment.
+ * This can only happen once for the lifetime
+ * of a board, because once saved the function
+ * will never reach the while loop.
+ */
+ if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
+ char buf[10];
+
+ found = 1;
+ envbid = bid;
+ envhwkey = hwkey;
+ sprintf(buf, "%lx", bid);
+ setenv("boardid", buf);
+ sprintf(buf, "%lx", hwkey);
+ setenv("hwkey", buf);
+ saveenv();
+ }
+ } /* end while( ! found ) */
+ }
+ }
+
+ /* compare now the values */
+ if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
+ printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
+ rc = 0; /* match */
+ } else {
+ printf("Error: env bId=0x%3lX, hwKey=%ld\n", envbid, envhwkey);
+ printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
+ rc = 1; /* don't match */
+ }
+ return rc;
+}
+
+U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
+ "check boardid and hwkey",
+ "[v]\n - check environment parameter "\
+ "\"boardIdListHex\" against stored boardid and hwkey "\
+ "from the IVM\n v: verbose output"
+);