123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- #include <config.h>
- #include <command.h>
- #include <common.h>
- #include <malloc.h>
- #include <environment.h>
- #include <linux/types.h>
- #include <api_public.h>
- #include "api_private.h"
- #define DEBUG
- #undef DEBUG
- #ifdef DEBUG
- #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
- #else
- #define debugf(fmt, args...)
- #endif
- typedef int (*cfp_t)(va_list argp);
- static int calls_no;
- static int API_getc(va_list ap)
- {
- int *c;
- if ((c = (int *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- *c = getc();
- return 0;
- }
- static int API_tstc(va_list ap)
- {
- int *t;
- if ((t = (int *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- *t = tstc();
- return 0;
- }
- static int API_putc(va_list ap)
- {
- char *c;
- if ((c = (char *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- putc(*c);
- return 0;
- }
- static int API_puts(va_list ap)
- {
- char *s;
- if ((s = (char *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- puts(s);
- return 0;
- }
- static int API_reset(va_list ap)
- {
- do_reset(NULL, 0, 0, NULL);
-
- return 0;
- }
- static int API_get_sys_info(va_list ap)
- {
- struct sys_info *si;
- si = (struct sys_info *)va_arg(ap, uintptr_t);
- if (si == NULL)
- return API_ENOMEM;
- return (platform_sys_info(si)) ? 0 : API_ENODEV;
- }
- static int API_udelay(va_list ap)
- {
- unsigned long *d;
- if ((d = (unsigned long *)va_arg(ap, unsigned long)) == NULL)
- return API_EINVAL;
- udelay(*d);
- return 0;
- }
- static int API_get_timer(va_list ap)
- {
- unsigned long *base, *cur;
- cur = (unsigned long *)va_arg(ap, unsigned long);
- if (cur == NULL)
- return API_EINVAL;
- base = (unsigned long *)va_arg(ap, unsigned long);
- if (base == NULL)
- return API_EINVAL;
- *cur = get_timer(*base);
- return 0;
- }
- static int API_dev_enum(va_list ap)
- {
- struct device_info *di;
-
- di = (struct device_info *)va_arg(ap, uintptr_t);
- if (di == NULL)
- return API_EINVAL;
- if (di->cookie == NULL) {
-
- dev_enum_reset();
- debugf("RESTART ENUM\n");
-
- if (dev_enum_net(di))
- return 0;
- }
-
- if (!dev_enum_storage(di))
-
- di->cookie = NULL;
- return 0;
- }
- static int API_dev_open(va_list ap)
- {
- struct device_info *di;
- int err = 0;
-
- di = (struct device_info *)va_arg(ap, uintptr_t);
- if (di == NULL)
- return API_EINVAL;
-
- if (di->state == DEV_STA_OPEN)
- return API_EBUSY;
- if (di->cookie == NULL)
- return API_ENODEV;
- if (di->type & DEV_TYP_STOR)
- err = dev_open_stor(di->cookie);
- else if (di->type & DEV_TYP_NET)
- err = dev_open_net(di->cookie);
- else
- err = API_ENODEV;
- if (!err)
- di->state = DEV_STA_OPEN;
- return err;
- }
- static int API_dev_close(va_list ap)
- {
- struct device_info *di;
- int err = 0;
-
- di = (struct device_info *)va_arg(ap, uintptr_t);
- if (di == NULL)
- return API_EINVAL;
- if (di->state == DEV_STA_CLOSED)
- return 0;
- if (di->cookie == NULL)
- return API_ENODEV;
- if (di->type & DEV_TYP_STOR)
- err = dev_close_stor(di->cookie);
- else if (di->type & DEV_TYP_NET)
- err = dev_close_net(di->cookie);
- else
-
- err = API_ENODEV;
- if (!err)
- di->state = DEV_STA_CLOSED;
- return err;
- }
- static int API_dev_write(va_list ap)
- {
- struct device_info *di;
- void *buf;
- int *len;
- int err = 0;
-
- di = (struct device_info *)va_arg(ap, uintptr_t);
- if (di == NULL)
- return API_EINVAL;
-
- if (di->cookie == NULL)
- return API_ENODEV;
-
- buf = (void *)va_arg(ap, uintptr_t);
- if (buf == NULL)
- return API_EINVAL;
-
- len = (int *)va_arg(ap, uintptr_t);
- if (len == NULL)
- return API_EINVAL;
- if (*len <= 0)
- return API_EINVAL;
- if (di->type & DEV_TYP_STOR)
-
- return API_ENODEV;
- else if (di->type & DEV_TYP_NET)
- err = dev_write_net(di->cookie, buf, *len);
- else
- err = API_ENODEV;
- return err;
- }
- static int API_dev_read(va_list ap)
- {
- struct device_info *di;
- void *buf;
- lbasize_t *len_stor, *act_len_stor;
- lbastart_t *start;
- int *len_net, *act_len_net;
-
- di = (struct device_info *)va_arg(ap, uintptr_t);
- if (di == NULL)
- return API_EINVAL;
-
- if (di->cookie == NULL)
- return API_ENODEV;
-
- buf = (void *)va_arg(ap, uintptr_t);
- if (buf == NULL)
- return API_EINVAL;
- if (di->type & DEV_TYP_STOR) {
-
- len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
- if (!len_stor)
- return API_EINVAL;
- if (*len_stor <= 0)
- return API_EINVAL;
-
- start = (lbastart_t *)va_arg(ap, uintptr_t);
-
- act_len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
- if (!act_len_stor)
- return API_EINVAL;
- *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
- } else if (di->type & DEV_TYP_NET) {
-
- len_net = (int *)va_arg(ap, uintptr_t);
- if (!len_net)
- return API_EINVAL;
- if (*len_net <= 0)
- return API_EINVAL;
-
- act_len_net = (int *)va_arg(ap, uintptr_t);
- if (!act_len_net)
- return API_EINVAL;
- *act_len_net = dev_read_net(di->cookie, buf, *len_net);
- } else
- return API_ENODEV;
- return 0;
- }
- static int API_env_get(va_list ap)
- {
- char *name, **value;
- if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- if ((value = (char **)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- *value = getenv(name);
- return 0;
- }
- static int API_env_set(va_list ap)
- {
- char *name, *value;
- if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- if ((value = (char *)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- setenv(name, value);
- return 0;
- }
- static int API_env_enum(va_list ap)
- {
- int i, n;
- char *last, **next;
- last = (char *)va_arg(ap, unsigned long);
- if ((next = (char **)va_arg(ap, uintptr_t)) == NULL)
- return API_EINVAL;
- if (last == NULL)
-
- *next = ((char *)env_get_addr(0));
- else {
- *next = last;
- for (i = 0; env_get_char(i) != '\0'; i = n + 1) {
- for (n = i; env_get_char(n) != '\0'; ++n) {
- if (n >= CONFIG_ENV_SIZE) {
-
- return 0;
- }
- }
- if (envmatch((uchar *)last, i) < 0)
- continue;
-
- i = n + 1;
- if (env_get_char(i) == '\0') {
-
- *next = NULL;
- return 0;
- }
- *next = ((char *)env_get_addr(i));
- return 0;
- }
- }
- return 0;
- }
- static int API_display_get_info(va_list ap)
- {
- int type;
- struct display_info *di;
- type = va_arg(ap, int);
- di = va_arg(ap, struct display_info *);
- return display_get_info(type, di);
- }
- static int API_display_draw_bitmap(va_list ap)
- {
- ulong bitmap;
- int x, y;
- bitmap = va_arg(ap, ulong);
- x = va_arg(ap, int);
- y = va_arg(ap, int);
- return display_draw_bitmap(bitmap, x, y);
- }
- static int API_display_clear(va_list ap)
- {
- display_clear();
- return 0;
- }
- static cfp_t calls_table[API_MAXCALL] = { NULL, };
- int syscall(int call, int *retval, ...)
- {
- va_list ap;
- int rv;
- if (call < 0 || call >= calls_no) {
- debugf("invalid call #%d\n", call);
- return 0;
- }
- if (calls_table[call] == NULL) {
- debugf("syscall #%d does not have a handler\n", call);
- return 0;
- }
- va_start(ap, retval);
- rv = calls_table[call](ap);
- if (retval != NULL)
- *retval = rv;
- return 1;
- }
- void api_init(void)
- {
- struct api_signature *sig = NULL;
-
- calls_table[API_RSVD] = NULL;
- calls_table[API_GETC] = &API_getc;
- calls_table[API_PUTC] = &API_putc;
- calls_table[API_TSTC] = &API_tstc;
- calls_table[API_PUTS] = &API_puts;
- calls_table[API_RESET] = &API_reset;
- calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
- calls_table[API_UDELAY] = &API_udelay;
- calls_table[API_GET_TIMER] = &API_get_timer;
- calls_table[API_DEV_ENUM] = &API_dev_enum;
- calls_table[API_DEV_OPEN] = &API_dev_open;
- calls_table[API_DEV_CLOSE] = &API_dev_close;
- calls_table[API_DEV_READ] = &API_dev_read;
- calls_table[API_DEV_WRITE] = &API_dev_write;
- calls_table[API_ENV_GET] = &API_env_get;
- calls_table[API_ENV_SET] = &API_env_set;
- calls_table[API_ENV_ENUM] = &API_env_enum;
- calls_table[API_DISPLAY_GET_INFO] = &API_display_get_info;
- calls_table[API_DISPLAY_DRAW_BITMAP] = &API_display_draw_bitmap;
- calls_table[API_DISPLAY_CLEAR] = &API_display_clear;
- calls_no = API_MAXCALL;
- debugf("API initialized with %d calls\n", calls_no);
- dev_stor_init();
-
- sig = malloc(sizeof(struct api_signature));
- if (sig == NULL) {
- printf("API: could not allocate memory for the signature!\n");
- return;
- }
- setenv_hex("api_address", (unsigned long)sig);
- debugf("API sig @ 0x%lX\n", (unsigned long)sig);
- memcpy(sig->magic, API_SIG_MAGIC, 8);
- sig->version = API_SIG_VERSION;
- sig->syscall = &syscall;
- sig->checksum = 0;
- sig->checksum = crc32(0, (unsigned char *)sig,
- sizeof(struct api_signature));
- debugf("syscall entry: 0x%lX\n", (unsigned long)sig->syscall);
- }
- void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
- int flags)
- {
- int i;
- if (!si->mr || !size || (flags == 0))
- return;
-
- for (i = 0; i < si->mr_no; i++)
- if (si->mr[i].flags == 0) {
-
- si->mr[i].start = start;
- si->mr[i].size = size;
- si->mr[i].flags = flags;
- return;
- }
- }
|