[app][lkboot] provide a means to register additional commands

This commit is contained in:
Brian Swetland
2014-08-08 15:57:53 -07:00
parent cab6a749a8
commit 47c2ae27e3
3 changed files with 75 additions and 6 deletions

View File

@@ -27,6 +27,7 @@
#include <debug.h>
#include <string.h>
#include <endian.h>
#include <malloc.h>
#include <kernel/thread.h>
@@ -34,22 +35,38 @@
#include <lib/bio.h>
#include <lib/sysparam.h>
#include <app/lkboot.h>
#if PLATFORM_ZYNQ
#include <platform/fpga.h>
#endif
#define bootdevice "spi0"
typedef struct LKB lkb_t;
// returns 0 on success, -1 on failure (short read or io error)
int lkb_read(lkb_t *lkb, void *data, size_t len);
int lkb_write(lkb_t *lkb, const void *data, size_t len);
extern void *lkb_iobuffer;
extern paddr_t lkb_iobuffer_phys;
extern size_t lkb_iobuffer_size;
struct lkb_command {
struct lkb_command *next;
const char *name;
lkb_handler_t handler;
void *cookie;
};
struct lkb_command *lkb_cmd_list = NULL;
void lkb_register(const char *name, lkb_handler_t handler, void *cookie) {
struct lkb_command *cmd = malloc(sizeof(struct lkb_command));
if (cmd != NULL) {
cmd->next = lkb_cmd_list;
cmd->name = name;
cmd->handler = handler;
cmd->cookie = cookie;
lkb_cmd_list = cmd;
}
}
static int do_reboot(void *arg) {
thread_sleep(250);
platform_halt(HALT_ACTION_REBOOT, HALT_REASON_SW_RESET);
@@ -58,6 +75,13 @@ static int do_reboot(void *arg) {
// return NULL for success, error string for failure
const char *lkb_handle_command(lkb_t *lkb, const char *cmd, const char *arg, unsigned len) {
struct lkb_command *lcmd;
for (lcmd = lkb_cmd_list; lcmd; lcmd = lcmd->next) {
if (!strcmp(lcmd->name, cmd)) {
return lcmd->handler(lkb, arg, len, lcmd->cookie);
}
}
if (len > lkb_iobuffer_size) {
return "buffer too small";
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2014 Brian Swetland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
typedef struct LKB lkb_t;
// lkb_read/write may *only* be called from within a lkb_handler()
// len of 0 is invalid for both
// returns 0 on success, -1 on failure (io error, etc)
int lkb_read(lkb_t *lkb, void *data, size_t len);
int lkb_write(lkb_t *lkb, const void *data, size_t len);
// len is the number of bytes the host has declared that it will send
// use lkb_read() to read some or all of this data
// return NULL on success, or an asciiz string (message) for error
typedef const char* (*lkb_handler_t)(lkb_t *lkb,
const char *arg, unsigned len, void *cookie);
// cmd must be a string constant
void lkb_register(const char *cmd, lkb_handler_t handler, void *cookie);

View File

@@ -11,4 +11,6 @@ MODULE_SRCS += \
$(LOCAL_DIR)/lkboot.c \
$(LOCAL_DIR)/commands.c
GLOBAL_INCLUDES += $(LOCAL_DIR)/include
include make/module.mk