Skip to content

Commit

Permalink
ratp: implement generic command support
Browse files Browse the repository at this point in the history
The RATP implementation now allows executing generic commands with a
binary interface: binary requests are received and binary responses
are returned.

Each command can define its own RATP request contents (e.g. to specify
command-specific options) as well as its own RATP response contents
(if any data is to be returned).

Each command is associated with a pair of numeric unique request and
response IDs, and for easy reference these IDs are maintained in the
common ratp_bb header. Modules may override generic implemented
commands or include their own new ones (as long as the numeric IDs
introduced are unique).

Signed-off-by: Aleksander Morgado <[email protected]>
Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
aleksander0m authored and saschahauer committed Mar 1, 2018
1 parent 1eddb0d commit ff612b8
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 18 deletions.
4 changes: 4 additions & 0 deletions arch/arm/lib32/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
Expand Down
4 changes: 4 additions & 0 deletions arch/arm/lib64/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
Expand Down
5 changes: 4 additions & 1 deletion arch/blackfin/boards/ipe337/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
___barebox_cmd_end = .;

___barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
___barebox_ratp_cmd_end = .;

___barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
___barebox_magicvar_end = .;
Expand All @@ -91,4 +95,3 @@ SECTIONS
___bss_stop = .;
_end = .;
}

4 changes: 4 additions & 0 deletions arch/mips/lib/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
Expand Down
5 changes: 4 additions & 1 deletion arch/nios2/cpu/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
Expand Down Expand Up @@ -129,4 +133,3 @@ SECTIONS
_end = .;
PROVIDE (end = .);
}

4 changes: 4 additions & 0 deletions arch/openrisc/cpu/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS } > ram
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS } > ram
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS } > ram
__barebox_magicvar_end = .;
Expand Down
4 changes: 4 additions & 0 deletions arch/ppc/boards/pcm030/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
Expand Down
4 changes: 4 additions & 0 deletions arch/ppc/mach-mpc85xx/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

__barebox_ratp_cmd_start = .;
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

__barebox_initcalls_start = .;
.barebox_initcalls : { INITCALLS }
__barebox_initcalls_end = .;
Expand Down
5 changes: 5 additions & 0 deletions arch/sandbox/board/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ SECTIONS
__barebox_cmd_start = .;
__barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

. = ALIGN(64);
__barebox_ratp_cmd_start = .;
__barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;
}

INSERT BEFORE .rodata;
7 changes: 7 additions & 0 deletions arch/x86/lib/barebox.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ SECTIONS
. = ALIGN(4);
} > barebox

.barebox_ratp_cmd : AT ( LOADADDR(.got) + SIZEOF (.got) ) {
__barebox_ratp_cmd_start = .;
BAREBOX_RATP_CMDS
__barebox_ratp_cmd_end = .;
. = ALIGN(4);
} > barebox

.barebox_magicvars : AT ( LOADADDR(.barebox_cmd) + SIZEOF (.barebox_cmd) ) {
__barebox_magicvar_start = .;
BAREBOX_MAGICVARS
Expand Down
5 changes: 5 additions & 0 deletions arch/x86/mach-efi/elf_ia32_efi.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ SECTIONS
__barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

. = ALIGN(64);
__barebox_ratp_cmd_start = .;
__barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

. = ALIGN(4096);
.dynamic : { *(.dynamic) }
. = ALIGN(4096);
Expand Down
5 changes: 5 additions & 0 deletions arch/x86/mach-efi/elf_x86_64_efi.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ SECTIONS
__barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

. = ALIGN(64);
__barebox_ratp_cmd_start = .;
__barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
__barebox_ratp_cmd_end = .;

. = ALIGN(4096);
.dynamic : { *(.dynamic) }
. = ALIGN(4096);
Expand Down
2 changes: 2 additions & 0 deletions common/module.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ SECTIONS
.got : { *(.got) }

.barebox_cmd : { BAREBOX_CMDS }
.barebox_ratp_cmd : { BAREBOX_RATP_CMDS }

. = ALIGN(4);
.bss : { *(.bss) }
}
82 changes: 66 additions & 16 deletions common/ratp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@
#include <ratp_bb.h>
#include <fs.h>

#define BB_RATP_TYPE_COMMAND 1
#define BB_RATP_TYPE_COMMAND_RETURN 2
#define BB_RATP_TYPE_CONSOLEMSG 3
#define BB_RATP_TYPE_PING 4
#define BB_RATP_TYPE_PONG 5
#define BB_RATP_TYPE_GETENV 6
#define BB_RATP_TYPE_GETENV_RETURN 7
#define BB_RATP_TYPE_FS 8
#define BB_RATP_TYPE_FS_RETURN 9

struct ratp_bb {
uint16_t type;
uint16_t flags;
uint8_t data[];
};
LIST_HEAD(ratp_command_list);
EXPORT_SYMBOL(ratp_command_list);

#define for_each_ratp_command(cmd) list_for_each_entry(cmd, &ratp_command_list, list)

struct ratp_bb_command_return {
uint32_t errno;
Expand All @@ -66,6 +55,51 @@ struct ratp_ctx {
struct poller_struct poller;
};

static int compare_ratp_command(struct list_head *a, struct list_head *b)
{
int id_a = list_entry(a, struct ratp_command, list)->request_id;
int id_b = list_entry(b, struct ratp_command, list)->request_id;

return (id_a - id_b);
}

int register_ratp_command(struct ratp_command *cmd)
{
debug("register ratp command: request %hu, response %hu\n",
cmd->request_id, cmd->response_id);
list_add_sort(&cmd->list, &ratp_command_list, compare_ratp_command);
return 0;
}
EXPORT_SYMBOL(register_ratp_command);

struct ratp_command *find_ratp_request(uint16_t request_id)
{
struct ratp_command *cmdtp;

for_each_ratp_command(cmdtp)
if (request_id == cmdtp->request_id)
return cmdtp;

return NULL; /* not found */
}

extern struct ratp_command __barebox_ratp_cmd_start;
extern struct ratp_command __barebox_ratp_cmd_end;

static int init_ratp_command_list(void)
{
struct ratp_command *cmdtp;

for (cmdtp = &__barebox_ratp_cmd_start;
cmdtp != &__barebox_ratp_cmd_end;
cmdtp++)
register_ratp_command(cmdtp);

return 0;
}

late_initcall(init_ratp_command_list);

static int console_recv(struct ratp *r, uint8_t *data)
{
struct ratp_ctx *ctx = container_of(r, struct ratp_ctx, ratp);
Expand Down Expand Up @@ -207,8 +241,24 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const void *buf, int len)
int dlen = len - sizeof(struct ratp_bb);
char *varname;
int ret = 0;
uint16_t type = be16_to_cpu(rbb->type);
struct ratp_command *cmd;

/* See if there's a command registered to this type */
cmd = find_ratp_request(type);
if (cmd) {
struct ratp_bb *rsp = NULL;
int rsp_len = 0;

ret = cmd->cmd(rbb, len, &rsp, &rsp_len);
if (!ret)
ret = ratp_send(&ctx->ratp, rsp, rsp_len);

free(rsp);
return ret;
}

switch (be16_to_cpu(rbb->type)) {
switch (type) {
case BB_RATP_TYPE_COMMAND:
if (ratp_command)
return 0;
Expand Down
2 changes: 2 additions & 0 deletions include/asm-generic/barebox.lds.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#define BAREBOX_CMDS KEEP(*(SORT_BY_NAME(.barebox_cmd*)))

#define BAREBOX_RATP_CMDS KEEP(*(SORT_BY_NAME(.barebox_ratp_cmd*)))

#define BAREBOX_SYMS KEEP(*(__usymtab))

#define BAREBOX_MAGICVARS KEEP(*(SORT_BY_NAME(.barebox_magicvar*)))
Expand Down
47 changes: 47 additions & 0 deletions include/ratp_bb.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#ifndef __RATP_BB_H
#define __RATP_BB_H

#include <linux/stringify.h>

#define BB_RATP_TYPE_COMMAND 1
#define BB_RATP_TYPE_COMMAND_RETURN 2
#define BB_RATP_TYPE_CONSOLEMSG 3
#define BB_RATP_TYPE_PING 4
#define BB_RATP_TYPE_PONG 5
#define BB_RATP_TYPE_GETENV 6
#define BB_RATP_TYPE_GETENV_RETURN 7
#define BB_RATP_TYPE_FS 8
#define BB_RATP_TYPE_FS_RETURN 9

struct ratp_bb {
uint16_t type;
uint16_t flags;
uint8_t data[];
};

struct ratp_bb_pkt {
unsigned int len;
uint8_t data[];
Expand All @@ -11,4 +29,33 @@ void barebox_ratp_command_run(void);
int barebox_ratp_fs_call(struct ratp_bb_pkt *tx, struct ratp_bb_pkt **rx);
int barebox_ratp_fs_mount(const char *path);

/*
* RATP commands definition
*/

struct ratp_command {
struct list_head list;
uint16_t request_id;
uint16_t response_id;
int (*cmd)(const struct ratp_bb *req,
int req_len,
struct ratp_bb **rsp,
int *rsp_len);
}
#ifdef __x86_64__
/* This is required because the linker will put symbols on a 64 bit alignment */
__attribute__((aligned(64)))
#endif
;

#define BAREBOX_RATP_CMD_START(_name) \
extern const struct ratp_command __barebox_ratp_cmd_##_name; \
const struct ratp_command __barebox_ratp_cmd_##_name \
__attribute__ ((unused,section (".barebox_ratp_cmd_" __stringify(_name)))) = {

#define BAREBOX_RATP_CMD_END \
};

int register_ratp_command(struct ratp_command *cmd);

#endif /* __RATP_BB_H */

0 comments on commit ff612b8

Please sign in to comment.