API: Improve glue mid-layer of the API demo application.

- Extend ub_dev_read() and ub_dev_recv() so they return the length actually
read, which allows for better control and error handling (this introduces
additional error code API_ESYSC returned by the glue mid-layer).

- Clean up definitions naming and usage.

- Other minor cosmetics.

Note these changes do not touch the API proper, so the interface between
U-Boot and standalone applications remains unchanged.

Signed-off-by: Rafal Jaworowski <raj@semihalf.com>
This commit is contained in:
Rafal Jaworowski 2009-01-23 13:27:18 +01:00 committed by Wolfgang Denk
parent 44a94e596b
commit 923aa48126
4 changed files with 39 additions and 44 deletions

View File

@ -43,12 +43,11 @@ static char buf[BUF_SZ];
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int rv = 0; int rv = 0, h, i, j, devs_no;
int h, i, j;
int devs_no;
struct api_signature *sig = NULL; struct api_signature *sig = NULL;
ulong start, now; ulong start, now;
struct device_info *di; struct device_info *di;
lbasize_t rlen;
if (!api_search_sig(&sig)) if (!api_search_sig(&sig))
return -1; return -1;
@ -96,7 +95,6 @@ int main(int argc, char *argv[])
if (devs_no == 0) if (devs_no == 0)
return -1; return -1;
printf("\n*** Show devices ***\n"); printf("\n*** Show devices ***\n");
for (i = 0; i < devs_no; i++) { for (i = 0; i < devs_no; i++) {
test_dump_di(i); test_dump_di(i);
@ -133,7 +131,7 @@ int main(int argc, char *argv[])
if ((rv = ub_dev_open(i)) != 0) if ((rv = ub_dev_open(i)) != 0)
errf("open device %d error %d\n", i, rv); errf("open device %d error %d\n", i, rv);
else if ((rv = ub_dev_read(i, buf, 1, 0)) != 0) else if ((rv = ub_dev_read(i, buf, 1, 0, &rlen)) != 0)
errf("could not read from device %d, error %d\n", i, rv); errf("could not read from device %d, error %d\n", i, rv);
else { else {
printf("Sector 0 dump (512B):\n"); printf("Sector 0 dump (512B):\n");

View File

@ -1,7 +1,5 @@
/* /*
* (C) Copyright 2007 Semihalf * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
*
* Written by: Rafal Jaworowski <raj@semihalf.com>
* *
* See file CREDITS for list of people who contributed to this * See file CREDITS for list of people who contributed to this
* project. * project.
@ -57,8 +55,8 @@ static int valid_sig(struct api_signature *sig)
* *
* returns 1/0 depending on found/not found result * returns 1/0 depending on found/not found result
*/ */
int api_search_sig(struct api_signature **sig) { int api_search_sig(struct api_signature **sig)
{
unsigned char *sp; unsigned char *sp;
uint32_t search_start = 0; uint32_t search_start = 0;
uint32_t search_end = 0; uint32_t search_end = 0;
@ -133,8 +131,7 @@ void ub_reset(void)
syscall(API_RESET, NULL); syscall(API_RESET, NULL);
} }
#define MR_MAX 5 static struct mem_region mr[UB_MAX_MR];
static struct mem_region mr[MR_MAX];
static struct sys_info si; static struct sys_info si;
struct sys_info * ub_get_sys_info(void) struct sys_info * ub_get_sys_info(void)
@ -143,7 +140,7 @@ struct sys_info * ub_get_sys_info(void)
memset(&si, 0, sizeof(struct sys_info)); memset(&si, 0, sizeof(struct sys_info));
si.mr = mr; si.mr = mr;
si.mr_no = MR_MAX; si.mr_no = UB_MAX_MR;
memset(&mr, 0, sizeof(mr)); memset(&mr, 0, sizeof(mr));
if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si)) if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
@ -178,17 +175,15 @@ unsigned long ub_get_timer(unsigned long base)
* *
* devices * devices
* *
* Devices are identified by handles: numbers 0, 1, 2, ..., MAX_DEVS-1 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
* *
***************************************************************************/ ***************************************************************************/
#define MAX_DEVS 6 static struct device_info devices[UB_MAX_DEV];
static struct device_info devices[MAX_DEVS];
struct device_info * ub_dev_get(int i) struct device_info * ub_dev_get(int i)
{ {
return ((i < 0 || i >= MAX_DEVS) ? NULL : &devices[i]); return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
} }
/* /*
@ -202,7 +197,7 @@ int ub_dev_enum(void)
struct device_info *di; struct device_info *di;
int n = 0; int n = 0;
memset(&devices, 0, sizeof(struct device_info) * MAX_DEVS); memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
di = &devices[0]; di = &devices[0];
if (!syscall(API_DEV_ENUM, NULL, di)) if (!syscall(API_DEV_ENUM, NULL, di))
@ -210,7 +205,7 @@ int ub_dev_enum(void)
while (di->cookie != NULL) { while (di->cookie != NULL) {
if (++n >= MAX_DEVS) if (++n >= UB_MAX_DEV)
break; break;
/* take another device_info */ /* take another device_info */
@ -236,7 +231,7 @@ int ub_dev_open(int handle)
struct device_info *di; struct device_info *di;
int err = 0; int err = 0;
if (handle < 0 || handle >= MAX_DEVS) if (handle < 0 || handle >= UB_MAX_DEV)
return API_EINVAL; return API_EINVAL;
di = &devices[handle]; di = &devices[handle];
@ -251,7 +246,7 @@ int ub_dev_close(int handle)
{ {
struct device_info *di; struct device_info *di;
if (handle < 0 || handle >= MAX_DEVS) if (handle < 0 || handle >= UB_MAX_DEV)
return API_EINVAL; return API_EINVAL;
di = &devices[handle]; di = &devices[handle];
@ -272,7 +267,7 @@ int ub_dev_close(int handle)
*/ */
static int dev_valid(int handle) static int dev_valid(int handle)
{ {
if (handle < 0 || handle >= MAX_DEVS) if (handle < 0 || handle >= UB_MAX_DEV)
return 0; return 0;
if (devices[handle].state != DEV_STA_OPEN) if (devices[handle].state != DEV_STA_OPEN)
@ -292,7 +287,8 @@ static int dev_stor_valid(int handle)
return 1; return 1;
} }
int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start) int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
lbasize_t *rlen)
{ {
struct device_info *di; struct device_info *di;
lbasize_t act_len; lbasize_t act_len;
@ -303,15 +299,12 @@ int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start)
di = &devices[handle]; di = &devices[handle];
if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len)) if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
return -1; return API_ESYSC;
if (!err && rlen)
*rlen = act_len;
if (err)
return err; return err;
if (act_len != len)
return API_EIO;
return 0;
} }
static int dev_net_valid(int handle) static int dev_net_valid(int handle)
@ -325,7 +318,7 @@ static int dev_net_valid(int handle)
return 1; return 1;
} }
int ub_dev_recv(int handle, void *buf, int len) int ub_dev_recv(int handle, void *buf, int len, int *rlen)
{ {
struct device_info *di; struct device_info *di;
int err = 0, act_len; int err = 0, act_len;
@ -335,12 +328,12 @@ int ub_dev_recv(int handle, void *buf, int len)
di = &devices[handle]; di = &devices[handle];
if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len)) if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
return -1; return API_ESYSC;
if (err) if (!err && rlen)
return -1; *rlen = act_len;
return act_len; return (err);
} }
int ub_dev_send(int handle, void *buf, int len) int ub_dev_send(int handle, void *buf, int len)
@ -353,7 +346,7 @@ int ub_dev_send(int handle, void *buf, int len)
di = &devices[handle]; di = &devices[handle];
if (!syscall(API_DEV_WRITE, &err, di, buf, &len)) if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
return -1; return API_ESYSC;
return err; return err;
} }
@ -379,7 +372,6 @@ void ub_env_set(const char *name, char *value)
syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value); syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
} }
static char env_name[256]; static char env_name[256];
const char * ub_env_enum(const char *last) const char * ub_env_enum(const char *last)

View File

@ -32,6 +32,9 @@
#define API_SEARCH_LEN (3 * 1024 * 1024) /* 3MB search range */ #define API_SEARCH_LEN (3 * 1024 * 1024) /* 3MB search range */
#define UB_MAX_MR 5 /* max mem regions number */
#define UB_MAX_DEV 6 /* max devices number */
extern void *syscall_ptr; extern void *syscall_ptr;
extern uint32_t search_hint; extern uint32_t search_hint;
@ -39,9 +42,10 @@ int syscall(int, int *, ...);
int api_search_sig(struct api_signature **sig); int api_search_sig(struct api_signature **sig);
/* /*
* ub_ library calls are part of the application, not U-Boot code! They are * The ub_ library calls are part of the application, not U-Boot code! They
* front-end wrappers that are used by the consumer application: they prepare * are front-end wrappers that are used by the consumer application: they
* arguments for particular syscall and jump to the low level syscall() * prepare arguments for particular syscall and jump to the low level
* syscall()
*/ */
/* console */ /* console */
@ -67,10 +71,10 @@ const char * ub_env_enum(const char *last);
int ub_dev_enum(void); int ub_dev_enum(void);
int ub_dev_open(int handle); int ub_dev_open(int handle);
int ub_dev_close(int handle); int ub_dev_close(int handle);
int ub_dev_read(int handle, void *buf, int ub_dev_read(int handle, void *buf, lbasize_t len,
lbasize_t len, lbastart_t start); lbastart_t start, lbasize_t *rlen);
int ub_dev_send(int handle, void *buf, int len); int ub_dev_send(int handle, void *buf, int len);
int ub_dev_recv(int handle, void *buf, int len); int ub_dev_recv(int handle, void *buf, int len, int *rlen);
struct device_info * ub_dev_get(int); struct device_info * ub_dev_get(int);
#endif /* _API_GLUE_H_ */ #endif /* _API_GLUE_H_ */

View File

@ -57,6 +57,7 @@
#define API_ENOMEM 3 /* no memory */ #define API_ENOMEM 3 /* no memory */
#define API_EBUSY 4 /* busy, occupied etc. */ #define API_EBUSY 4 /* busy, occupied etc. */
#define API_EIO 5 /* I/O error */ #define API_EIO 5 /* I/O error */
#define API_ESYSC 6 /* syscall error */
typedef int (*scp_t)(int, int *, ...); typedef int (*scp_t)(int, int *, ...);