[Cryptech-Commits] [sw/stm32] 01/01: Use a fresh port of libcli, which retains more of the original API.

git at cryptech.is git at cryptech.is
Thu Jul 21 20:33:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

paul at psgd.org pushed a commit to branch alternate_libcli
in repository sw/stm32.

commit e3fe7d89b6094bfef42f42329e15631f684f0748
Author: Paul Selkirk <paul at psgd.org>
AuthorDate: Thu Jul 21 16:32:43 2016 -0400

    Use a fresh port of libcli, which retains more of the original API.
---
 Makefile                           |   2 +-
 libraries/libcli/Makefile          |  20 +++++-
 projects/cli-test/mgmt-cli.c       |  99 +++++++---------------------
 projects/cli-test/mgmt-cli.h       |  32 +--------
 projects/cli-test/mgmt-dfu.c       |  10 +--
 projects/cli-test/mgmt-dfu.h       |   1 -
 projects/cli-test/mgmt-fpga.c      |  17 +++--
 projects/cli-test/mgmt-fpga.h      |   1 -
 projects/cli-test/mgmt-keystore.c  |  64 ++++++++----------
 projects/cli-test/mgmt-keystore.h  |   1 -
 projects/cli-test/mgmt-masterkey.c |  24 +++----
 projects/cli-test/mgmt-masterkey.h |   1 -
 projects/cli-test/mgmt-misc.c      |   5 +-
 projects/cli-test/mgmt-misc.h      |   5 +-
 projects/cli-test/mgmt-show.c      |  23 +++----
 projects/cli-test/mgmt-show.h      |   1 -
 projects/cli-test/mgmt-test.c      |  10 ++-
 projects/hsm/mgmt-bootloader.c     |   6 +-
 projects/hsm/mgmt-cli.c            | 132 ++++++++++---------------------------
 projects/hsm/mgmt-cli.h            |  31 ---------
 projects/hsm/mgmt-firmware.c       |   9 ++-
 projects/hsm/mgmt-fpga.c           |  19 +++---
 projects/hsm/mgmt-fpga.h           |   1 -
 projects/hsm/mgmt-keystore.c       |  56 +++++++---------
 projects/hsm/mgmt-keystore.h       |   1 -
 projects/hsm/mgmt-masterkey.c      |  19 +++---
 projects/hsm/mgmt-masterkey.h      |   1 -
 projects/hsm/mgmt-misc.c           |   2 +-
 projects/hsm/mgmt-misc.h           |   7 +-
 projects/hsm/mgmt-show.c           |  84 -----------------------
 projects/hsm/mgmt-show.h           |  43 ------------
 31 files changed, 217 insertions(+), 510 deletions(-)

diff --git a/Makefile b/Makefile
index c8e363c..99c8e65 100644
--- a/Makefile
+++ b/Makefile
@@ -43,7 +43,7 @@ export RTOS_DIR = $(MBED_DIR)/rtos
 export LIBHAL_SRC = $(CRYPTECH_ROOT)/sw/libhal
 export LIBHAL_BLD = $(LIBS_DIR)/libhal
 
-export LIBCLI_SRC = $(CRYPTECH_ROOT)/user/ft/libcli
+export LIBCLI_SRC = $(CRYPTECH_ROOT)/user/paul/libcli
 export LIBCLI_BLD = $(LIBS_DIR)/libcli
 
 export LIBTFM_SRC = $(CRYPTECH_ROOT)/sw/thirdparty/libtfm
diff --git a/libraries/libcli/Makefile b/libraries/libcli/Makefile
index 26bfdf8..c2dad35 100644
--- a/libraries/libcli/Makefile
+++ b/libraries/libcli/Makefile
@@ -1,4 +1,22 @@
 vpath %.c ${LIBCLI_SRC}
 vpath %.h ${LIBCLI_SRC}
 
-include ${LIBCLI_SRC}/Makefile
+CFLAGS += \
+	-DDO_CRYPT=0 \
+	-DDO_FILE=0 \
+	-DDO_FILTER=0 \
+	-DDO_IDLE_TIMEOUT=0 \
+	-DDO_MALLOC=0 \
+	-DDO_PRINT_BUFFERED=0 \
+	-DDO_REGULAR=0 \
+	-DDO_SOCKET=0 \
+	-DDO_TAB_COMPLETION=1 \
+	-DDO_TELNET=0
+
+all: libcli.a
+
+libcli.a: libcli.o
+	$(AR) rcs $@ $^
+
+clean:
+	rm libcli.[ao]
diff --git a/projects/cli-test/mgmt-cli.c b/projects/cli-test/mgmt-cli.c
index 073b045..8f5db9d 100644
--- a/projects/cli-test/mgmt-cli.c
+++ b/projects/cli-test/mgmt-cli.c
@@ -116,19 +116,19 @@ static void uart_cli_print(struct cli_def *cli __attribute__ ((unused)), const c
     uart_send_string2(STM_UART_MGMT, crlf);
 }
 
-static int uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count)
+static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count)
 {
     for (int i = 0; i < count; ++i) {
         while (ringbuf_read_char(&uart_ringbuf, (uint8_t *)(buf + i)) == 0)
             osSemaphoreWait(uart_sem, osWaitForever);
     }
-    return count;
+    return (ssize_t)count;
 }
 
-static int uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count)
+static ssize_t uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count)
 {
     uart_send_bytes(STM_UART_MGMT, (uint8_t *) buf, count);
-    return (int) count;
+    return (ssize_t)count;
 }
 
 int control_mgmt_uart_dma_rx(mgmt_cli_dma_state_t state)
@@ -148,72 +148,16 @@ int control_mgmt_uart_dma_rx(mgmt_cli_dma_state_t state)
     return 0;
 }
 
-static int embedded_cli_loop(struct cli_def *cli)
+static struct cli_def *mgmt_cli_init(void)
 {
-    unsigned char c;
-    int n = 0;
-    static struct cli_loop_ctx ctx;
-
-    memset(&ctx, 0, sizeof(ctx));
-    ctx.insertmode = 1;
-
-    cli->state = CLI_STATE_LOGIN;
-
-    /* start off in unprivileged mode */
-    cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED);
-    cli_set_configmode(cli, MODE_EXEC, NULL);
-
-    cli_error(cli, "%s", cli->banner);
-
-    while (1) {
-	cli_loop_start_new_command(cli, &ctx);
-
-	control_mgmt_uart_dma_rx(DMA_RX_START);
-
-	while (1) {
-	    cli_loop_show_prompt(cli, &ctx);
-
-	    n = cli_loop_read_next_char(cli, &ctx, &c);
-
-	    /*
-	    cli_print(cli, "Next char: '%c'/%i, ringbuf ridx %i, widx %i",
-		      c, (int) c,
-		      uart_ringbuf.ridx,
-		      RINGBUF_WIDX(uart_ringbuf)
-	    */
-	    if (n == CLI_LOOP_CTRL_BREAK)
-		break;
-	    if (n == CLI_LOOP_CTRL_CONTINUE)
-		continue;
-
-	    n = cli_loop_process_char(cli, &ctx, c);
-	    if (n == CLI_LOOP_CTRL_BREAK)
-		break;
-	    if (n == CLI_LOOP_CTRL_CONTINUE)
-		continue;
-	}
-
-	if (ctx.l < 0)
-            continue;
-
-	/* cli_print(cli, "Process command: '%s'", ctx.cmd); */
-	n = cli_loop_process_cmd(cli, &ctx);
-	if (n == CLI_LOOP_CTRL_BREAK)
-	    break;
-    }
-
-    return CLI_OK;
-}
-
-static void mgmt_cli_init(struct cli_def *cli)
-{
-    cli_init(cli);
+    struct cli_def *cli;
+    cli = cli_init();
     cli_read_callback(cli, uart_cli_read);
     cli_write_callback(cli, uart_cli_write);
     cli_print_callback(cli, uart_cli_print);
     cli_set_banner(cli, "Cryptech Alpha test CLI");
     cli_set_hostname(cli, "cryptech");
-    cli_telnet_protocol(cli, 0);
+    return cli;
 }
 
 hal_user_t user;
@@ -229,24 +173,25 @@ static int check_auth(const char *username, const char *password)
 
 int cli_main(void)
 {
-    static struct cli_def cli;
-
     uart_sem = osSemaphoreCreate(osSemaphore(uart_sem), 0);
 
-    mgmt_cli_init(&cli);
-    cli_set_auth_callback(&cli, check_auth);
+    struct cli_def *cli;
+    cli = mgmt_cli_init();
+    cli_set_auth_callback(cli, check_auth);
 
-    configure_cli_show(&cli);
-    configure_cli_fpga(&cli);
-    configure_cli_misc(&cli);
-    configure_cli_test(&cli);
-    configure_cli_keystore(&cli);
-    configure_cli_masterkey(&cli);
+    configure_cli_show(cli);
+    configure_cli_fpga(cli);
+    configure_cli_misc(cli);
+    configure_cli_test(cli);
+    configure_cli_keystore(cli);
+    configure_cli_masterkey(cli);
 
     while (1) {
-        embedded_cli_loop(&cli);
-        /* embedded_cli_loop returns when the user enters 'quit' or 'exit' */
-        cli_print(&cli, "\nLogging out...\n");
+        control_mgmt_uart_dma_rx(DMA_RX_START);
+
+        cli_loop(cli, 0);
+        /* cli_loop returns when the user enters 'quit' or 'exit' */
+        cli_print(cli, "\nLogging out...\n");
         user = HAL_USER_NONE;
     }
 
diff --git a/projects/cli-test/mgmt-cli.h b/projects/cli-test/mgmt-cli.h
index a50e092..0b9c40c 100644
--- a/projects/cli-test/mgmt-cli.h
+++ b/projects/cli-test/mgmt-cli.h
@@ -35,45 +35,15 @@
 #ifndef __STM32_MGMT_CLI_H
 #define __STM32_MGMT_CLI_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
-
-/* A bunch of defines to make it easier to add/maintain the CLI commands.
- *
- */
-#define _cli_cmd_struct(name, fullname, func, help)		\
-    static struct cli_command cmd_##fullname##_s =		\
-	{(char *) #name, func, 0, help,				\
-	 PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL, NULL, NULL}
-
-/* ROOT is a top-level label with no command */
-#define cli_command_root(name)					\
-    _cli_cmd_struct(name, name, NULL, NULL);			\
-    cli_register_command2(cli, &cmd_##name##_s, NULL)
-
-/* BRANCH is a label with a parent, but no command */
-#define cli_command_branch(parent, name)				\
-    _cli_cmd_struct(name, parent##_##name, NULL, NULL);			\
-    cli_register_command2(cli, &cmd_##parent##_##name##_s, &cmd_##parent##_s)
-
-/* NODE is a label with a parent and with a command associated with it */
-#define cli_command_node(parent, name, help)				\
-    _cli_cmd_struct(name, parent##_##name, cmd_##parent##_##name, (char *) help); \
-    cli_register_command2(cli, &cmd_##parent##_##name##_s, &cmd_##parent##_s)
-
-/* ROOT NODE is a label without a parent, but with a command associated with it */
-#define cli_command_root_node(name, help)				\
-    _cli_cmd_struct(name, name, cmd_##name, (char *) help);		\
-    cli_register_command2(cli, &cmd_##name##_s, NULL)
-
-
 typedef enum {
     DMA_RX_STOP,
     DMA_RX_START,
 } mgmt_cli_dma_state_t;
 
 extern int control_mgmt_uart_dma_rx(mgmt_cli_dma_state_t state);
+
 extern int cli_main(void);
 
 #endif /* __STM32_MGMT_CLI_H */
diff --git a/projects/cli-test/mgmt-dfu.c b/projects/cli-test/mgmt-dfu.c
index e57c521..8f258ea 100644
--- a/projects/cli-test/mgmt-dfu.c
+++ b/projects/cli-test/mgmt-dfu.c
@@ -115,9 +115,11 @@ static int cmd_dfu_jump(struct cli_def *cli, const char *command, char *argv[],
 
 void configure_cli_dfu(struct cli_def *cli)
 {
-    cli_command_root(dfu);
+    struct cli_command *c;
 
-    cli_command_node(dfu, dump, "Show the first 256 bytes of the loaded firmware");
-    cli_command_node(dfu, jump, "Jump to the loaded firmware");
-    cli_command_node(dfu, erase, "Erase the firmware memory (will crash the CLI)");
+    c = cli_register_command(cli, NULL, "dfu", NULL, 0, 0, NULL);
+
+    cli_register_command(cli, c, "dump", cmd_dfu_dump, 0, 0, "Show the first 256 bytes of the loaded firmware");
+    cli_register_command(cli, c, "jump", cmd_dfu_jump, 0, 0, "Jump to the loaded firmware");
+    cli_register_command(cli, c, "erase", cmd_dfu_erase, 0, 0, "Erase the firmware memory (will crash the CLI)");
 }
diff --git a/projects/cli-test/mgmt-dfu.h b/projects/cli-test/mgmt-dfu.h
index 047e30a..6128b35 100644
--- a/projects/cli-test/mgmt-dfu.h
+++ b/projects/cli-test/mgmt-dfu.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_DFU_H
 #define __STM32_CLI_MGMT_DFU_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 /* symbols defined in the linker script (STM32F429BI.ld) */
diff --git a/projects/cli-test/mgmt-fpga.c b/projects/cli-test/mgmt-fpga.c
index 059e4a9..31a6e27 100644
--- a/projects/cli-test/mgmt-fpga.c
+++ b/projects/cli-test/mgmt-fpga.c
@@ -122,16 +122,19 @@ static int cmd_fpga_reset_registers(struct cli_def *cli, const char *command, ch
 
 void configure_cli_fpga(struct cli_def *cli)
 {
-    /* fpga */
-    cli_command_root(fpga);
+    struct cli_command *c = cli_register_command(cli, NULL, "fpga", NULL, 0, 0, NULL);
+
     /* fpga reset */
-    cli_command_node(fpga, reset, "Reset FPGA (config reset)");
+    struct cli_command *c_reset = cli_register_command(cli, c, "reset", cmd_fpga_reset, 0, 0, "Reset FPGA (config reset)");
+
     /* fpga reset registers */
-    cli_command_node(fpga_reset, registers, "Reset FPGA registers (soft reset)");
+    cli_register_command(cli, c_reset, "registers", cmd_fpga_reset_registers, 0, 0, "Reset FPGA registers (soft reset)");
+
+    struct cli_command *c_bitstream = cli_register_command(cli, c, "bitstream", NULL, 0, 0, NULL);
 
-    cli_command_branch(fpga, bitstream);
     /* fpga bitstream upload */
-    cli_command_node(fpga_bitstream, upload, "Upload new FPGA bitstream");
+    cli_register_command(cli, c_bitstream, "upload", cmd_fpga_bitstream_upload, 0, 0, "Upload new FPGA bitstream");
+
     /* fpga bitstream erase */
-    cli_command_node(fpga_bitstream, erase, "Erase FPGA config memory");
+    cli_register_command(cli, c_bitstream, "erase", cmd_fpga_bitstream_erase, 0, 0, "Erase FPGA config memory");
 }
diff --git a/projects/cli-test/mgmt-fpga.h b/projects/cli-test/mgmt-fpga.h
index ce185de..9d0aedc 100644
--- a/projects/cli-test/mgmt-fpga.h
+++ b/projects/cli-test/mgmt-fpga.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_FPGA_H
 #define __STM32_CLI_MGMT_FPGA_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 
diff --git a/projects/cli-test/mgmt-keystore.c b/projects/cli-test/mgmt-keystore.c
index 01f3be2..72cc5da 100644
--- a/projects/cli-test/mgmt-keystore.c
+++ b/projects/cli-test/mgmt-keystore.c
@@ -32,8 +32,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #define HAL_OK CMSIS_HAL_OK
-
 #include "stm-init.h"
 #include "stm-keystore.h"
 #include "stm-fpgacfg.h"
@@ -42,7 +42,6 @@
 #include "mgmt-cli.h"
 #include "mgmt-show.h"
 
-/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #undef HAL_OK
 #define LIBHAL_OK HAL_OK
 #include "hal.h"
@@ -54,7 +53,7 @@
 #include <string.h>
 
 
-int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     hal_user_t user;
@@ -92,7 +91,7 @@ int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[],
     return CLI_OK;
 }
 
-int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     hal_user_t user;
@@ -130,7 +129,7 @@ int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[
     return CLI_OK;
 }
 
-int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     hal_client_handle_t client = { -1 };
@@ -150,7 +149,7 @@ int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, ch
     return CLI_OK;
 }
 
-int cmd_keystore_set_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -206,7 +205,7 @@ static int key_by_index(struct cli_def *cli, char *str, const uint8_t **name, si
     return CLI_ERROR;
 }
 
-int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -242,7 +241,7 @@ int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv
     return CLI_OK;
 }
 
-int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -278,7 +277,7 @@ int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv
     return CLI_OK;
 }
 
-int cmd_keystore_show_data(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_show_data(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     uint8_t buf[KEYSTORE_PAGE_SIZE];
     uint32_t i;
@@ -300,7 +299,7 @@ int cmd_keystore_show_data(struct cli_def *cli, const char *command, char *argv[
     return CLI_OK;
 }
 
-int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     uint8_t name[HAL_RPC_PKEY_NAME_MAX + 1];
@@ -369,7 +368,7 @@ int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[
     return CLI_OK;
 }
 
-int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     int status;
 
@@ -393,43 +392,38 @@ int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], i
 
 void configure_cli_keystore(struct cli_def *cli)
 {
-    /* keystore */
-    cli_command_root(keystore);
-    /* keystore set */
-    cli_command_branch(keystore, set);
-    /* keystore clear */
-    cli_command_branch(keystore, clear);
-    /* keystore delete */
-    cli_command_branch(keystore, delete);
-    /* keystore rename */
-    cli_command_branch(keystore, rename);
-    /* keystore show */
-    cli_command_branch(keystore, show);
+    struct cli_command *c = cli_register_command(cli, NULL, "keystore", NULL, 0, 0, NULL);
 
-    /* keystore erase */
-    cli_command_node(keystore, erase, "Erase the whole keystore");
+    struct cli_command *c_set = cli_register_command(cli, c, "set", NULL, 0, 0, NULL);
+    struct cli_command *c_clear = cli_register_command(cli, c, "clear", NULL, 0, 0, NULL);
+    struct cli_command *c_delete = cli_register_command(cli, c, "delete", NULL, 0, 0, NULL);
+    struct cli_command *c_rename = cli_register_command(cli, c, "rename", NULL, 0, 0, NULL);
+    struct cli_command *c_show = cli_register_command(cli, c, "show", NULL, 0, 0, NULL);
 
+    /* keystore erase */
+    cli_register_command(cli, c, "erase", cmd_keystore_erase, 0, 0, "Erase the whole keystore");
     /* keystore set pin */
-    cli_command_node(keystore_set, pin, "Set either 'wheel', 'user' or 'so' PIN");
+    struct cli_command *c_set_pin = cli_register_command(cli, c_set, "pin", cmd_keystore_set_pin, 0, 0, "Set either 'wheel', 'user' or 'so' PIN");
 
     /* keystore set pin iterations */
-    cli_command_node(keystore_set_pin, iterations, "Set PBKDF2 iterations for PINs");
+    cli_register_command(cli, c_set_pin, "iterations", cmd_keystore_set_pin_iterations, 0, 0, "Set PBKDF2 iterations for PINs");
 
     /* keystore clear pin */
-    cli_command_node(keystore_clear, pin, "Clear either 'wheel', 'user' or 'so' PIN");
+    cli_register_command(cli, c_clear, "pin", cmd_keystore_clear_pin, 0, 0, "Clear either 'wheel', 'user' or 'so' PIN");
 
     /* keystore set key */
-    cli_command_node(keystore_set, key, "Set a key");
+    cli_register_command(cli, c_set, "key", cmd_keystore_set_key, 0, 0, "Set a key");
 
     /* keystore delete key */
-    cli_command_node(keystore_delete, key, "Delete a key");
+    cli_register_command(cli, c_delete, "key", cmd_keystore_delete_key, 0, 0, "Delete a key");
 
     /* keystore rename key */
-    cli_command_node(keystore_rename, key, "Rename a key");
-
+    cli_register_command(cli, c_rename, "key", cmd_keystore_rename_key, 0, 0, "Rename a key");
+ 
     /* keystore show data */
-    cli_command_node(keystore_show, data, "Dump the first page from the keystore memory");
+    cli_register_command(cli, c_show, "data", cmd_keystore_show_data, 0, 0, "Dump the first page from the keystore memory");
+
+   /* keystore show keys */
+    cli_register_command(cli, c_show, "keys", cmd_keystore_show_keys, 0, 0, "Show what PINs and keys are in the keystore");
 
-    /* keystore show keys */
-    cli_command_node(keystore_show, keys, "Show what PINs and keys are in the keystore");
 }
diff --git a/projects/cli-test/mgmt-keystore.h b/projects/cli-test/mgmt-keystore.h
index 62efa51..9e14ac6 100644
--- a/projects/cli-test/mgmt-keystore.h
+++ b/projects/cli-test/mgmt-keystore.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_KEYSTORE_H
 #define __STM32_CLI_MGMT_KEYSTORE_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 extern void configure_cli_keystore(struct cli_def *cli);
diff --git a/projects/cli-test/mgmt-masterkey.c b/projects/cli-test/mgmt-masterkey.c
index 7938e33..69dc430 100644
--- a/projects/cli-test/mgmt-masterkey.c
+++ b/projects/cli-test/mgmt-masterkey.c
@@ -32,14 +32,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #define HAL_OK CMSIS_HAL_OK
-
 #include "stm-init.h"
 #include "stm-uart.h"
 #include "mgmt-cli.h"
 #include "mgmt-masterkey.h"
 
-/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #undef HAL_OK
 #define LIBHAL_OK HAL_OK
 #include <hal.h>
@@ -74,7 +73,7 @@ static int _parse_hex_groups(uint8_t *buf, size_t len, char *argv[], int argc)
 
     for (i = 0; i < argc; i++) {
 	if (dst >= end) return -1;
-	*dst++ = strtol(argv[i], &err_ptr, 16);
+	*dst++ = strtoul(argv[i], &err_ptr, 16);
 	if (*err_ptr) return -2;
     }
 
@@ -191,19 +190,22 @@ static int cmd_masterkey_unsecure_erase(struct cli_def *cli, const char *command
 
 void configure_cli_masterkey(struct cli_def *cli)
 {
-    /* masterkey */
-    cli_command_root(masterkey);
+    struct cli_command *c = cli_register_command(cli, NULL, "masterkey", NULL, 0, 0, NULL);
+
     /* masterkey status */
-    cli_command_node(masterkey, status, "Show status of master key in RAM/flash");
+    cli_register_command(cli, c, "status", cmd_masterkey_status, 0, 0, "Show status of master key in RAM/flash");
 
     /* masterkey set */
-    cli_command_node(masterkey, set, "Set the master key in the volatile Master Key Memory");
+    cli_register_command(cli, c, "set", cmd_masterkey_set, 0, 0, "Set the master key in the volatile Master Key Memory");
+
     /* masterkey erase */
-    cli_command_node(masterkey, erase, "Erase the master key from the volatile Master Key Memory");
+    cli_register_command(cli, c, "erase", cmd_masterkey_erase, 0, 0, "Erase the master key from the volatile Master Key Memory");
+
+    struct cli_command *c_unsecure = cli_register_command(cli, c, "unsecure", NULL, 0, 0, NULL);
 
-    cli_command_branch(masterkey, unsecure);
     /* masterkey unsecure set */
-    cli_command_node(masterkey_unsecure, set, "Set master key in unprotected flash memory (if unsure, DON'T)");
+    cli_register_command(cli, c_unsecure, "set", cmd_masterkey_unsecure_set, 0, 0, "Set master key in unprotected flash memory (if unsure, DON'T)");
+
     /* masterkey unsecure erase */
-    cli_command_node(masterkey_unsecure, erase, "Erase master key from unprotected flash memory");
+    cli_register_command(cli, c_unsecure, "erase", cmd_masterkey_unsecure_erase, 0, 0, "Erase master key from unprotected flash memory");
 }
diff --git a/projects/cli-test/mgmt-masterkey.h b/projects/cli-test/mgmt-masterkey.h
index 5d2624a..67835e9 100644
--- a/projects/cli-test/mgmt-masterkey.h
+++ b/projects/cli-test/mgmt-masterkey.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_MASTERKEY_H
 #define __STM32_CLI_MGMT_MASTERKEY_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 extern void configure_cli_masterkey(struct cli_def *cli);
diff --git a/projects/cli-test/mgmt-misc.c b/projects/cli-test/mgmt-misc.c
index 67bc875..535f0af 100644
--- a/projects/cli-test/mgmt-misc.c
+++ b/projects/cli-test/mgmt-misc.c
@@ -131,8 +131,9 @@ static int cmd_reboot(struct cli_def *cli, const char *command, char *argv[], in
 void configure_cli_misc(struct cli_def *cli)
 {
     /* filetransfer */
-    cli_command_root_node(filetransfer, "Test file transfering");
+    cli_register_command(cli, NULL, "filetransfer", cmd_filetransfer, 0, 0, "Test file transfering");
+
     /* reboot */
-    cli_command_root_node(reboot, "Reboot the STM32");
+    cli_register_command(cli, NULL, "reboot", cmd_reboot, 0, 0, "Reboot the STM32");
 }
 
diff --git a/projects/cli-test/mgmt-misc.h b/projects/cli-test/mgmt-misc.h
index b7eb4f4..c549f63 100644
--- a/projects/cli-test/mgmt-misc.h
+++ b/projects/cli-test/mgmt-misc.h
@@ -35,15 +35,14 @@
 #ifndef __STM32_CLI_MGMT_MISC_H
 #define __STM32_CLI_MGMT_MISC_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
-
 #define FILETRANSFER_UPLOAD_CHUNK_SIZE 256
 
 typedef int (*cli_data_callback)(uint8_t *, size_t);
 
-extern void configure_cli_misc(struct cli_def *cli);
 extern int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback);
 
+extern void configure_cli_misc(struct cli_def *cli);
+
 #endif /* __STM32_CLI_MGMT_MISC_H */
diff --git a/projects/cli-test/mgmt-show.c b/projects/cli-test/mgmt-show.c
index b918cce..e88b784 100644
--- a/projects/cli-test/mgmt-show.c
+++ b/projects/cli-test/mgmt-show.c
@@ -32,8 +32,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #define HAL_OK CMSIS_HAL_OK
-
 #include "stm-init.h"
 #include "stm-keystore.h"
 #include "stm-fpgacfg.h"
@@ -42,7 +42,6 @@
 #include "mgmt-cli.h"
 #include "mgmt-show.h"
 
-/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #undef HAL_OK
 #define LIBHAL_OK HAL_OK
 #include "hal.h"
@@ -53,7 +52,6 @@
 
 #include <string.h>
 
-
 static int cmd_show_cpuspeed(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     volatile uint32_t hclk;
@@ -127,18 +125,21 @@ static int cmd_show_keystore_data(struct cli_def *cli, const char *command, char
 
 void configure_cli_show(struct cli_def *cli)
 {
-    /* show */
-    cli_command_root(show);
+    struct cli_command *c = cli_register_command(cli, NULL, "show", NULL, 0, 0, NULL);
 
     /* show cpuspeed */
-    cli_command_node(show, cpuspeed, "Show the speed at which the CPU currently operates");
+    cli_register_command(cli, c, "cpuspeed", cmd_show_cpuspeed, 0, 0, "Show the speed at which the CPU currently operates");
+
+    struct cli_command *c_fpga = cli_register_command(cli, c, "fpga", NULL, 0, 0, NULL);
 
-    cli_command_branch(show, fpga);
     /* show fpga status*/
-    cli_command_node(show_fpga, status, "Show status about the FPGA");
+    cli_register_command(cli, c_fpga, "status", cmd_show_fpga_status, 0, 0, "Show status about the FPGA");
+
+    struct cli_command *c_keystore = cli_register_command(cli, c, "keystore", NULL, 0, 0, NULL);
 
-    cli_command_branch(show, keystore);
     /* show keystore status*/
-    cli_command_node(show_keystore, status, "Show status of the keystore memory");
-    cli_command_node(show_keystore, data, "Show the first page of the keystore memory");
+    cli_register_command(cli, c_keystore, "status", cmd_show_keystore_status, 0, 0, "Show status of the keystore memory");
+
+    /* show keystore data */
+    cli_register_command(cli, c_keystore, "data", cmd_show_keystore_data, 0, 0, "Show the first page of the keystore memory");
 }
diff --git a/projects/cli-test/mgmt-show.h b/projects/cli-test/mgmt-show.h
index e459acb..7b80a30 100644
--- a/projects/cli-test/mgmt-show.h
+++ b/projects/cli-test/mgmt-show.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_SHOW_H
 #define __STM32_CLI_MGMT_SHOW_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 extern void configure_cli_show(struct cli_def *cli);
diff --git a/projects/cli-test/mgmt-test.c b/projects/cli-test/mgmt-test.c
index 35c6fb6..138982f 100644
--- a/projects/cli-test/mgmt-test.c
+++ b/projects/cli-test/mgmt-test.c
@@ -44,8 +44,7 @@
 
 #include <stdlib.h>
 
-
-int cmd_test_sdram(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_test_sdram(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     // run external memory initialization sequence
     HAL_StatusTypeDef status;
@@ -110,12 +109,11 @@ int cmd_test_sdram(struct cli_def *cli, const char *command, char *argv[], int a
 
 void configure_cli_test(struct cli_def *cli)
 {
-    /* test */
-    cli_command_root(test);
+    struct cli_command *c = cli_register_command(cli, NULL, "test", NULL, 0, 0, NULL);
 
     /* test sdram */
-    cli_command_node(test, sdram, "Run SDRAM tests");
+    cli_register_command(cli, c, "sdram", cmd_test_sdram, 0, 0, "Run SDRAM tests");
 
     /* test mkmif */
-    cli_command_node(test, mkmif, "Run Master Key Memory Interface tests");
+    cli_register_command(cli, c, "mkmif", cmd_test_mkmif, 0, 0, "Run Master Key Memory Interface tests");
 }
diff --git a/projects/hsm/mgmt-bootloader.c b/projects/hsm/mgmt-bootloader.c
index a062fd9..a4783cc 100644
--- a/projects/hsm/mgmt-bootloader.c
+++ b/projects/hsm/mgmt-bootloader.c
@@ -75,7 +75,9 @@ static int cmd_bootloader_upload(struct cli_def *cli, const char *command, char
 
 void configure_cli_bootloader(struct cli_def *cli)
 {
-    cli_command_root(bootloader);
+    struct cli_command *c;
 
-    cli_command_node(bootloader, upload, "Upload new bootloader image");
+    c = cli_register_command(cli, NULL, "bootloader", NULL, 0, 0, NULL);
+
+    cli_register_command(cli, c, "upload", cmd_bootloader_upload, 0, 0, "Upload new bootloader image");
 }
diff --git a/projects/hsm/mgmt-cli.c b/projects/hsm/mgmt-cli.c
index a610db6..539ba4c 100644
--- a/projects/hsm/mgmt-cli.c
+++ b/projects/hsm/mgmt-cli.c
@@ -47,7 +47,6 @@
 #include "mgmt-bootloader.h"
 #include "mgmt-fpga.h"
 #include "mgmt-misc.h"
-#include "mgmt-show.h"
 #include "mgmt-keystore.h"
 #include "mgmt-masterkey.h"
 
@@ -62,7 +61,7 @@
 
 typedef struct {
     int ridx;
-    volatile int widx;	
+    volatile int widx;
     mgmt_cli_dma_state_t rx_state;
     uint8_t buf[CLI_UART_RECVBUF_SIZE];
 } ringbuf_t;
@@ -117,106 +116,38 @@ static void uart_cli_print(struct cli_def *cli __attribute__ ((unused)), const c
     uart_send_string2(STM_UART_MGMT, crlf);
 }
 
-static int uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count)
+static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count)
 {
     for (int i = 0; i < count; ++i) {
         while (ringbuf_read_char(&uart_ringbuf, (uint8_t *)(buf + i)) == 0)
             osSemaphoreWait(uart_sem, osWaitForever);
     }
-    return count;
+    return (ssize_t)count;
 }
 
-static int uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count)
+static ssize_t uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count)
 {
     uart_send_bytes(STM_UART_MGMT, (uint8_t *) buf, count);
-    return (int) count;
+    return (ssize_t)count;
 }
 
 int control_mgmt_uart_dma_rx(mgmt_cli_dma_state_t state)
 {
     if (state == DMA_RX_START) {
-	if (uart_ringbuf.rx_state != DMA_RX_START) {
+        if (uart_ringbuf.rx_state != DMA_RX_START) {
             ringbuf_init(&uart_ringbuf);
-	    HAL_UART_Receive_DMA(&huart_mgmt, &uart_rx, 1);
-	    uart_ringbuf.rx_state = DMA_RX_START;
-	}
-	return 1;
+            HAL_UART_Receive_DMA(&huart_mgmt, &uart_rx, 1);
+            uart_ringbuf.rx_state = DMA_RX_START;
+        }
+        return 1;
     } else if (state == DMA_RX_STOP) {
-	if (HAL_UART_DMAStop(&huart_mgmt) != CMSIS_HAL_OK) return 0;
-	uart_ringbuf.rx_state = DMA_RX_STOP;
-	return 1;
+        if (HAL_UART_DMAStop(&huart_mgmt) != CMSIS_HAL_OK) return 0;
+        uart_ringbuf.rx_state = DMA_RX_STOP;
+        return 1;
     }
     return 0;
 }
 
-static int embedded_cli_loop(struct cli_def *cli)
-{
-    unsigned char c;
-    int n = 0;
-    static struct cli_loop_ctx ctx;
-
-    memset(&ctx, 0, sizeof(ctx));
-    ctx.insertmode = 1;
-
-    cli->state = CLI_STATE_LOGIN;
-
-    /* start off in unprivileged mode */
-    cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED);
-    cli_set_configmode(cli, MODE_EXEC, NULL);
-
-    cli_error(cli, "%s", cli->banner);
-
-    while (1) {
-	cli_loop_start_new_command(cli, &ctx);
-
-	control_mgmt_uart_dma_rx(DMA_RX_START);
-
-	while (1) {
-	    cli_loop_show_prompt(cli, &ctx);
-
-	    n = cli_loop_read_next_char(cli, &ctx, &c);
-
-	    /*
-	    cli_print(cli, "Next char: '%c'/%i, ringbuf ridx %i, widx %i",
-		      c, (int) c,
-		      uart_ringbuf.ridx,
-		      RINGBUF_WIDX(uart_ringbuf)
-	    */
-	    if (n == CLI_LOOP_CTRL_BREAK)
-		break;
-	    if (n == CLI_LOOP_CTRL_CONTINUE)
-		continue;
-
-	    n = cli_loop_process_char(cli, &ctx, c);
-	    if (n == CLI_LOOP_CTRL_BREAK)
-		break;
-	    if (n == CLI_LOOP_CTRL_CONTINUE)
-		continue;
-	}
-
-	if (ctx.l < 0)
-            break;
-
-	/* cli_print(cli, "Process command: '%s'", ctx.cmd); */
-	n = cli_loop_process_cmd(cli, &ctx);
-	if (n == CLI_LOOP_CTRL_BREAK)
-	    break;
-    }
-
-    return CLI_OK;
-}
-
-static void mgmt_cli_init(struct cli_def *cli)
-{
-    cli_init(cli);
-    cli_read_callback(cli, uart_cli_read);
-    cli_write_callback(cli, uart_cli_write);
-    cli_print_callback(cli, uart_cli_print);
-    cli_set_banner(cli, "Cryptech Alpha");
-    cli_set_hostname(cli, "cryptech");
-    cli_telnet_protocol(cli, 0);
-}
-
 hal_user_t user;
 
 static int check_auth(const char *username, const char *password)
@@ -242,27 +173,36 @@ static int check_auth(const char *username, const char *password)
 
 int cli_main(void)
 {
-    static struct cli_def cli;
-
     uart_sem = osSemaphoreCreate(osSemaphore(uart_sem), 0);
 
-    mgmt_cli_init(&cli);
-    cli_set_auth_callback(&cli, check_auth);
+    struct cli_def *cli;
+    cli = cli_init();
+    if (cli == NULL)
+        Error_Handler();
+
+    cli_read_callback(cli, uart_cli_read);
+    cli_write_callback(cli, uart_cli_write);
+    cli_print_callback(cli, uart_cli_print);
+    cli_set_banner(cli, "Cryptech Alpha");
+    cli_set_hostname(cli, "cryptech");
+    cli_set_auth_callback(cli, check_auth);
 
     /* we don't have any privileged commands at the moment */
-    cli_unregister_command(&cli, "enable");
+    cli_unregister_command(cli, "enable");
 
-    configure_cli_fpga(&cli);
-    configure_cli_keystore(&cli);
-    configure_cli_masterkey(&cli);
-    configure_cli_firmware(&cli);
-    configure_cli_bootloader(&cli);
-    configure_cli_misc(&cli);
+    configure_cli_fpga(cli);
+    configure_cli_keystore(cli);
+    configure_cli_masterkey(cli);
+    configure_cli_firmware(cli);
+    configure_cli_bootloader(cli);
+    configure_cli_misc(cli);
 
     while (1) {
-        embedded_cli_loop(&cli);
-        /* embedded_cli_loop returns when the user enters 'quit' or 'exit' */
-        cli_print(&cli, "\nLogging out...\n");
+        control_mgmt_uart_dma_rx(DMA_RX_START);
+
+        cli_loop(cli, 0);
+        /* cli_loop returns when the user enters 'quit' or 'exit' */
+        cli_print(cli, "\nLogging out...\n");
         user = HAL_USER_NONE;
     }
 
diff --git a/projects/hsm/mgmt-cli.h b/projects/hsm/mgmt-cli.h
index c96dae6..0b9c40c 100644
--- a/projects/hsm/mgmt-cli.h
+++ b/projects/hsm/mgmt-cli.h
@@ -35,39 +35,8 @@
 #ifndef __STM32_MGMT_CLI_H
 #define __STM32_MGMT_CLI_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
-
-/* A bunch of defines to make it easier to add/maintain the CLI commands.
- *
- */
-#define _cli_cmd_struct(name, fullname, func, help)		\
-    static struct cli_command cmd_##fullname##_s =		\
-	{(char *) #name, func, 0, help,				\
-	 PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL, NULL, NULL}
-
-/* ROOT is a top-level label with no command */
-#define cli_command_root(name)					\
-    _cli_cmd_struct(name, name, NULL, NULL);			\
-    cli_register_command2(cli, &cmd_##name##_s, NULL)
-
-/* BRANCH is a label with a parent, but no command */
-#define cli_command_branch(parent, name)				\
-    _cli_cmd_struct(name, parent##_##name, NULL, NULL);			\
-    cli_register_command2(cli, &cmd_##parent##_##name##_s, &cmd_##parent##_s)
-
-/* NODE is a label with a parent and with a command associated with it */
-#define cli_command_node(parent, name, help)				\
-    _cli_cmd_struct(name, parent##_##name, cmd_##parent##_##name, (char *) help); \
-    cli_register_command2(cli, &cmd_##parent##_##name##_s, &cmd_##parent##_s)
-
-/* ROOT NODE is a label without a parent, but with a command associated with it */
-#define cli_command_root_node(name, help)				\
-    _cli_cmd_struct(name, name, cmd_##name, (char *) help);		\
-    cli_register_command2(cli, &cmd_##name##_s, NULL)
-
-
 typedef enum {
     DMA_RX_STOP,
     DMA_RX_START,
diff --git a/projects/hsm/mgmt-firmware.c b/projects/hsm/mgmt-firmware.c
index 1a0e184..ec8a69d 100644
--- a/projects/hsm/mgmt-firmware.c
+++ b/projects/hsm/mgmt-firmware.c
@@ -35,10 +35,11 @@
 /* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */
 #define HAL_OK CMSIS_HAL_OK
 #include "stm-init.h"
-#include "mgmt-cli.h"
 #include "stm-uart.h"
 #include "stm-flash.h"
 
+#include "mgmt-cli.h"
+
 #undef HAL_OK
 #define HAL_OK LIBHAL_OK
 #include "hal.h"
@@ -63,7 +64,9 @@ static int cmd_firmware_upload(struct cli_def *cli, const char *command, char *a
 
 void configure_cli_firmware(struct cli_def *cli)
 {
-    cli_command_root(firmware);
+    struct cli_command *c;
+
+    c = cli_register_command(cli, NULL, "firmware", NULL, 0, 0, NULL);
 
-    cli_command_node(firmware, upload, "Upload new firmware image");
+    cli_register_command(cli, c, "upload", cmd_firmware_upload, 0, 0, "Upload new firmware image");
 }
diff --git a/projects/hsm/mgmt-fpga.c b/projects/hsm/mgmt-fpga.c
index adb45b3..778ded9 100644
--- a/projects/hsm/mgmt-fpga.c
+++ b/projects/hsm/mgmt-fpga.c
@@ -145,19 +145,20 @@ static int cmd_fpga_show_cores(struct cli_def *cli, const char *command, char *a
 
 void configure_cli_fpga(struct cli_def *cli)
 {
-    /* fpga */
-    cli_command_root(fpga);
+    struct cli_command *c = cli_register_command(cli, NULL, "fpga", NULL, 0, 0, NULL);
 
-    cli_command_branch(fpga, show);
-    /* show fpga cores*/
-    cli_command_node(fpga_show, cores, "Show FPGA core names and versions");
+    struct cli_command *c_show = cli_register_command(cli, c, "show", NULL, 0, 0, NULL);
+    struct cli_command *c_bitstream = cli_register_command(cli, c, "bitstream", NULL, 0, 0, NULL);
+
+    /* fpga show cores */
+    cli_register_command(cli, c_show, "cores", cmd_fpga_show_cores, 0, 0, "Show FPGA core names and versions");
 
     /* fpga reset */
-    cli_command_node(fpga, reset, "Reset FPGA (config reset)");
+    cli_register_command(cli, c, "reset", cmd_fpga_reset, 0, 0, "Reset FPGA (config reset)");
 
-    cli_command_branch(fpga, bitstream);
     /* fpga bitstream upload */
-    cli_command_node(fpga_bitstream, upload, "Upload new FPGA bitstream");
+    cli_register_command(cli, c_bitstream, "upload", cmd_fpga_bitstream_upload, 0, 0, "Upload new FPGA bitstream");
+
     /* fpga bitstream erase */
-    cli_command_node(fpga_bitstream, erase, "Erase FPGA config memory");
+    cli_register_command(cli, c_bitstream, "erase", cmd_fpga_bitstream_erase, 0, 0, "Erase FPGA config memory");
 }
diff --git a/projects/hsm/mgmt-fpga.h b/projects/hsm/mgmt-fpga.h
index ce185de..9d0aedc 100644
--- a/projects/hsm/mgmt-fpga.h
+++ b/projects/hsm/mgmt-fpga.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_FPGA_H
 #define __STM32_CLI_MGMT_FPGA_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 
diff --git a/projects/hsm/mgmt-keystore.c b/projects/hsm/mgmt-keystore.c
index 0471430..c7e20b0 100644
--- a/projects/hsm/mgmt-keystore.c
+++ b/projects/hsm/mgmt-keystore.c
@@ -40,7 +40,6 @@
 #include "stm-uart.h"
 
 #include "mgmt-cli.h"
-#include "mgmt-show.h"
 
 #undef HAL_OK
 #define LIBHAL_OK HAL_OK
@@ -54,7 +53,7 @@
 #include <ctype.h>
 
 
-int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     hal_user_t user;
@@ -92,7 +91,7 @@ int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char *argv[],
     return CLI_OK;
 }
 
-int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     hal_user_t user;
@@ -130,7 +129,7 @@ int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char *argv[
     return CLI_OK;
 }
 
-int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     hal_client_handle_t client = { -1 };
@@ -151,7 +150,7 @@ int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *command, ch
 }
 
 #if 0
-int cmd_keystore_set_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_set_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -208,7 +207,7 @@ static int key_by_index(struct cli_def *cli, char *str, const uint8_t **name, si
     return CLI_ERROR;
 }
 
-int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -251,7 +250,7 @@ int cmd_keystore_delete_key(struct cli_def *cli, const char *command, char *argv
     return CLI_OK;
 }
 
-int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     hal_error_t status;
     int hint = 0;
@@ -294,7 +293,7 @@ int cmd_keystore_rename_key(struct cli_def *cli, const char *command, char *argv
     return CLI_OK;
 }
 
-int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     const hal_ks_keydb_t *db;
     char *type;
@@ -369,7 +368,7 @@ int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[
     return CLI_OK;
 }
 
-int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc)
+static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc)
 {
     int status;
 
@@ -393,42 +392,37 @@ int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], i
 
 void configure_cli_keystore(struct cli_def *cli)
 {
-    /* keystore */
-    cli_command_root(keystore);
-    /* keystore set */
-    cli_command_branch(keystore, set);
-    /* keystore clear */
-    cli_command_branch(keystore, clear);
-    /* keystore delete */
-    cli_command_branch(keystore, delete);
-    /* keystore rename */
-    cli_command_branch(keystore, rename);
-    /* keystore show */
-    cli_command_branch(keystore, show);
+    struct cli_command *c = cli_register_command(cli, NULL, "keystore", NULL, 0, 0, NULL);
+
+    struct cli_command *c_show = cli_register_command(cli, c, "show", NULL, 0, 0, NULL);
+    struct cli_command *c_set = cli_register_command(cli, c, "set", NULL, 0, 0, NULL);
+    struct cli_command *c_clear = cli_register_command(cli, c, "clear", NULL, 0, 0, NULL);
+    struct cli_command *c_delete = cli_register_command(cli, c, "delete", NULL, 0, 0, NULL);
+    struct cli_command *c_rename = cli_register_command(cli, c, "rename", NULL, 0, 0, NULL);
+
+    /* keystore show keys */
+    cli_register_command(cli, c_show, "keys", cmd_keystore_show_keys, 0, 0, "Show what PINs and keys are in the keystore");
 
     /* keystore set pin */
-    cli_command_node(keystore_set, pin, "Set either 'wheel', 'user' or 'so' PIN");
+    struct cli_command *c_set_pin = cli_register_command(cli, c_set, "pin", cmd_keystore_set_pin, 0, 0, "Set either 'wheel', 'user' or 'so' PIN");
 
     /* keystore set pin iterations */
-    cli_command_node(keystore_set_pin, iterations, "Set PBKDF2 iterations for PINs");
+    cli_register_command(cli, c_set_pin, "iterations", cmd_keystore_set_pin_iterations, 0, 0, "Set PBKDF2 iterations for PINs");
 
     /* keystore clear pin */
-    cli_command_node(keystore_clear, pin, "Clear either 'wheel', 'user' or 'so' PIN");
-
-    /* keystore show keys */
-    cli_command_node(keystore_show, keys, "Show what PINs and keys are in the keystore");
+    cli_register_command(cli, c_clear, "pin", cmd_keystore_clear_pin, 0, 0, "Clear either 'wheel', 'user' or 'so' PIN");
 
 #if 0
     /* keystore set key */
-    cli_command_node(keystore_set, key, "Set a key");
+    cli_register_command(cli, c_set, "key", cmd_keystore_set_key, 0, 0, "Set a key");
 #endif
 
     /* keystore rename key */
-    cli_command_node(keystore_rename, key, "Rename a key");
+    cli_register_command(cli, c_rename, "key", cmd_keystore_rename_key, 0, 0, "Rename a key");
 
     /* keystore delete key */
-    cli_command_node(keystore_delete, key, "Delete a key");
+    cli_register_command(cli, c_delete, "key", cmd_keystore_delete_key, 0, 0, "Delete a key");
 
     /* keystore erase */
-    cli_command_node(keystore, erase, "Erase the whole keystore");
+    cli_register_command(cli, c, "erase", cmd_keystore_erase, 0, 0, "Erase the whole keystore");
 }
diff --git a/projects/hsm/mgmt-keystore.h b/projects/hsm/mgmt-keystore.h
index 62efa51..9e14ac6 100644
--- a/projects/hsm/mgmt-keystore.h
+++ b/projects/hsm/mgmt-keystore.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_KEYSTORE_H
 #define __STM32_CLI_MGMT_KEYSTORE_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 extern void configure_cli_keystore(struct cli_def *cli);
diff --git a/projects/hsm/mgmt-masterkey.c b/projects/hsm/mgmt-masterkey.c
index 6aa7338..33ce395 100644
--- a/projects/hsm/mgmt-masterkey.c
+++ b/projects/hsm/mgmt-masterkey.c
@@ -168,19 +168,22 @@ static int cmd_masterkey_unsecure_erase(struct cli_def *cli, const char *command
 
 void configure_cli_masterkey(struct cli_def *cli)
 {
-    /* masterkey */
-    cli_command_root(masterkey);
+    struct cli_command *c = cli_register_command(cli, NULL, "masterkey", NULL, 0, 0, NULL);
+
     /* masterkey status */
-    cli_command_node(masterkey, status, "Show status of master key in RAM/flash");
+    cli_register_command(cli, c, "status", cmd_masterkey_status, 0, 0, "Show status of master key in RAM/flash");
 
     /* masterkey set */
-    cli_command_node(masterkey, set, "Set the master key in the volatile Master Key Memory");
+    cli_register_command(cli, c, "set", cmd_masterkey_set, 0, 0, "Set the master key in the volatile Master Key Memory");
+
     /* masterkey erase */
-    cli_command_node(masterkey, erase, "Erase the master key from the volatile Master Key Memory");
+    cli_register_command(cli, c, "erase", cmd_masterkey_erase, 0, 0, "Erase the master key from the volatile Master Key Memory");
+
+    struct cli_command *c_unsecure = cli_register_command(cli, c, "unsecure", NULL, 0, 0, NULL);
 
-    cli_command_branch(masterkey, unsecure);
     /* masterkey unsecure set */
-    cli_command_node(masterkey_unsecure, set, "Set master key in unprotected flash memory (if unsure, DON'T)");
+    cli_register_command(cli, c_unsecure, "set", cmd_masterkey_unsecure_set, 0, 0, "Set master key in unprotected flash memory (if unsure, DON'T)");
+
     /* masterkey unsecure erase */
-    cli_command_node(masterkey_unsecure, erase, "Erase master key from unprotected flash memory");
+    cli_register_command(cli, c_unsecure, "erase", cmd_masterkey_unsecure_erase, 0, 0, "Erase master key from unprotected flash memory");
 }
diff --git a/projects/hsm/mgmt-masterkey.h b/projects/hsm/mgmt-masterkey.h
index 5d2624a..67835e9 100644
--- a/projects/hsm/mgmt-masterkey.h
+++ b/projects/hsm/mgmt-masterkey.h
@@ -35,7 +35,6 @@
 #ifndef __STM32_CLI_MGMT_MASTERKEY_H
 #define __STM32_CLI_MGMT_MASTERKEY_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
 extern void configure_cli_masterkey(struct cli_def *cli);
diff --git a/projects/hsm/mgmt-misc.c b/projects/hsm/mgmt-misc.c
index 15b2d13..31a1c49 100644
--- a/projects/hsm/mgmt-misc.c
+++ b/projects/hsm/mgmt-misc.c
@@ -114,6 +114,6 @@ static int cmd_reboot(struct cli_def *cli, const char *command, char *argv[], in
 void configure_cli_misc(struct cli_def *cli)
 {
     /* reboot */
-    cli_command_root_node(reboot, "Reboot the STM32");
+    cli_register_command(cli, NULL, "reboot", cmd_reboot, 0, 0, "Reboot the STM32");
 }
 
diff --git a/projects/hsm/mgmt-misc.h b/projects/hsm/mgmt-misc.h
index b7eb4f4..862ca0c 100644
--- a/projects/hsm/mgmt-misc.h
+++ b/projects/hsm/mgmt-misc.h
@@ -35,15 +35,12 @@
 #ifndef __STM32_CLI_MGMT_MISC_H
 #define __STM32_CLI_MGMT_MISC_H
 
-#include "stm-init.h"
 #include <libcli.h>
 
-
-#define FILETRANSFER_UPLOAD_CHUNK_SIZE 256
-
 typedef int (*cli_data_callback)(uint8_t *, size_t);
 
-extern void configure_cli_misc(struct cli_def *cli);
 extern int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback);
 
+extern void configure_cli_misc(struct cli_def *cli);
+
 #endif /* __STM32_CLI_MGMT_MISC_H */
diff --git a/projects/hsm/mgmt-show.c b/projects/hsm/mgmt-show.c
deleted file mode 100644
index ac78f30..0000000
--- a/projects/hsm/mgmt-show.c
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * mgmt-show.c
- * -----------
- * CLI 'show' functions.
- *
- * Copyright (c) 2016, NORDUnet A/S All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * - Redistributions of source code must retain the above copyright notice,
- *   this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "stm-init.h"
-#include "stm-keystore.h"
-#include "stm-fpgacfg.h"
-#include "stm-uart.h"
-
-#include "mgmt-cli.h"
-#include "mgmt-show.h"
-
-#include <string.h>
-
-
-static int cmd_show_cpuspeed(struct cli_def *cli, const char *command, char *argv[], int argc)
-{
-    volatile uint32_t hclk;
-
-    hclk = HAL_RCC_GetHCLKFreq();
-    cli_print(cli, "HSE_VALUE:       %li", HSE_VALUE);
-    cli_print(cli, "HCLK:            %li (%i MHz)", hclk, (int) hclk / 1000 / 1000);
-    cli_print(cli, "SystemCoreClock: %li (%i MHz)", SystemCoreClock, (int) SystemCoreClock / 1000 / 1000);
-    return CLI_OK;
-}
-
-static int cmd_show_fpga_status(struct cli_def *cli, const char *command, char *argv[], int argc)
-{
-    cli_print(cli, "FPGA has %sloaded a bitstream", fpgacfg_check_done() ? "":"NOT ");
-    return CLI_OK;
-}
-
-static int cmd_show_keystore_status(struct cli_def *cli, const char *command, char *argv[], int argc)
-{
-    cli_print(cli, "Keystore memory is %sonline", (keystore_check_id() != 1) ? "NOT ":"");
-    return CLI_OK;
-}
-
-void configure_cli_show(struct cli_def *cli)
-{
-    /* show */
-    cli_command_root(show);
-
-    /* show cpuspeed */
-    cli_command_node(show, cpuspeed, "Show the speed at which the CPU currently operates");
-
-    cli_command_branch(show, fpga);
-    /* show fpga status*/
-    cli_command_node(show_fpga, status, "Show status about the FPGA");
-
-    cli_command_branch(show, keystore);
-    /* show keystore status*/
-    cli_command_node(show_keystore, status, "Show status of the keystore memory");
-}
diff --git a/projects/hsm/mgmt-show.h b/projects/hsm/mgmt-show.h
deleted file mode 100644
index e459acb..0000000
--- a/projects/hsm/mgmt-show.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * mgmt-show.h
- * -----------
- * Management CLI 'show' functions.
- *
- * Copyright (c) 2016, NORDUnet A/S All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * - Redistributions of source code must retain the above copyright notice,
- *   this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef __STM32_CLI_MGMT_SHOW_H
-#define __STM32_CLI_MGMT_SHOW_H
-
-#include "stm-init.h"
-#include <libcli.h>
-
-extern void configure_cli_show(struct cli_def *cli);
-
-#endif /* __STM32_CLI_MGMT_SHOW_H */



More information about the Commits mailing list