2 * (C) Copyright 2007 Semihalf
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #if defined(CONFIG_API)
33 #include <environment.h>
34 #include <linux/types.h>
35 #include <api_public.h>
37 #include "api_private.h"
42 /* U-Boot routines needed */
43 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
45 /*****************************************************************************
47 * This is the API core.
49 * API_ functions are part of U-Boot code and constitute the lowest level
52 * - they know what values they need as arguments
53 * - their direct return value pertains to the API_ "shell" itself (0 on
54 * success, some error code otherwise)
55 * - if the call returns a value it is buried within arguments
57 ****************************************************************************/
60 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
62 #define debugf(fmt, args...)
65 typedef int (*cfp_t)(va_list argp);
72 * int API_getc(int *c)
74 static int API_getc(va_list ap)
78 if ((c = (int *)va_arg(ap, u_int32_t)) == NULL)
88 * int API_tstc(int *c)
90 static int API_tstc(va_list ap)
94 if ((t = (int *)va_arg(ap, u_int32_t)) == NULL)
104 * int API_putc(char *ch)
106 static int API_putc(va_list ap)
110 if ((c = (char *)va_arg(ap, u_int32_t)) == NULL)
120 * int API_puts(char **s)
122 static int API_puts(va_list ap)
126 if ((s = (char *)va_arg(ap, u_int32_t)) == NULL)
136 * int API_reset(void)
138 static int API_reset(va_list ap)
140 do_reset(NULL, 0, 0, NULL);
149 * int API_get_sys_info(struct sys_info *si)
151 * fill out the sys_info struct containing selected parameters about the
154 static int API_get_sys_info(va_list ap)
158 si = (struct sys_info *)va_arg(ap, u_int32_t);
162 return (platform_sys_info(si)) ? 0 : API_ENODEV;
168 * int API_udelay(unsigned long *udelay)
170 static int API_udelay(va_list ap)
174 if ((d = (unsigned long *)va_arg(ap, u_int32_t)) == NULL)
184 * int API_get_timer(unsigned long *current, unsigned long *base)
186 static int API_get_timer(va_list ap)
188 unsigned long *base, *cur;
190 cur = (unsigned long *)va_arg(ap, u_int32_t);
194 base = (unsigned long *)va_arg(ap, u_int32_t);
198 *cur = get_timer(*base);
203 /*****************************************************************************
207 * int API_dev_enum(struct device_info *)
210 * cookies uniqely identify the previously enumerated device instance and
211 * provide a hint for what to inspect in current enum iteration:
213 * - net: ð_device struct address from list pointed to by eth_devices
215 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
216 * &scsi_dev_desc[n] and similar tables
218 ****************************************************************************/
220 static int API_dev_enum(va_list ap)
222 struct device_info *di;
224 /* arg is ptr to the device_info struct we are going to fill out */
225 di = (struct device_info *)va_arg(ap, u_int32_t);
229 if (di->cookie == NULL) {
230 /* start over - clean up enumeration */
231 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
232 debugf("RESTART ENUM\n");
234 /* net device enumeration first */
235 if (dev_enum_net(di))
240 * The hidden assumption is there can only be one active network
241 * device and it is identified upon enumeration (re)start, so there's
242 * no point in trying to find network devices in other cases than the
243 * (re)start and hence the 'next' device can only be storage
245 if (!dev_enum_storage(di))
246 /* make sure we mark there are no more devices */
253 static int API_dev_open(va_list ap)
255 struct device_info *di;
258 /* arg is ptr to the device_info struct */
259 di = (struct device_info *)va_arg(ap, u_int32_t);
263 /* Allow only one consumer of the device at a time */
264 if (di->state == DEV_STA_OPEN)
267 if (di->cookie == NULL)
270 if (di->type & DEV_TYP_STOR)
271 err = dev_open_stor(di->cookie);
273 else if (di->type & DEV_TYP_NET)
274 err = dev_open_net(di->cookie);
279 di->state = DEV_STA_OPEN;
285 static int API_dev_close(va_list ap)
287 struct device_info *di;
290 /* arg is ptr to the device_info struct */
291 di = (struct device_info *)va_arg(ap, u_int32_t);
295 if (di->state == DEV_STA_CLOSED)
298 if (di->cookie == NULL)
301 if (di->type & DEV_TYP_STOR)
302 err = dev_close_stor(di->cookie);
304 else if (di->type & DEV_TYP_NET)
305 err = dev_close_net(di->cookie);
308 * In case of unknown device we cannot change its state, so
309 * only return error code
314 di->state = DEV_STA_CLOSED;
321 * Notice: this is for sending network packets only, as U-Boot does not
322 * support writing to storage at the moment (12.2007)
327 * struct device_info *di,
332 * buf: ptr to buffer from where to get the data to send
334 * len: length of packet to be sent (in bytes)
337 static int API_dev_write(va_list ap)
339 struct device_info *di;
344 /* 1. arg is ptr to the device_info struct */
345 di = (struct device_info *)va_arg(ap, u_int32_t);
349 /* XXX should we check if device is open? i.e. the ->state ? */
351 if (di->cookie == NULL)
354 /* 2. arg is ptr to buffer from where to get data to write */
355 buf = (void *)va_arg(ap, u_int32_t);
359 /* 3. arg is length of buffer */
360 len = (int *)va_arg(ap, u_int32_t);
366 if (di->type & DEV_TYP_STOR)
368 * write to storage is currently not supported by U-Boot:
369 * no storage device implements block_write() method
373 else if (di->type & DEV_TYP_NET)
374 err = dev_write_net(di->cookie, buf, *len);
386 * struct device_info *di,
389 * unsigned long *start
393 * buf: ptr to buffer where to put the read data
395 * len: ptr to length to be read
396 * - network: len of packet to read (in bytes)
397 * - storage: # of blocks to read (can vary in size depending on define)
399 * start: ptr to start block (only used for storage devices, ignored for
402 * act_len: ptr to where to put the len actually read
404 static int API_dev_read(va_list ap)
406 struct device_info *di;
408 lbasize_t *len_stor, *act_len_stor;
410 int *len_net, *act_len_net;
412 /* 1. arg is ptr to the device_info struct */
413 di = (struct device_info *)va_arg(ap, u_int32_t);
417 /* XXX should we check if device is open? i.e. the ->state ? */
419 if (di->cookie == NULL)
422 /* 2. arg is ptr to buffer from where to put the read data */
423 buf = (void *)va_arg(ap, u_int32_t);
427 if (di->type & DEV_TYP_STOR) {
428 /* 3. arg - ptr to var with # of blocks to read */
429 len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
435 /* 4. arg - ptr to var with start block */
436 start = (lbastart_t *)va_arg(ap, u_int32_t);
438 /* 5. arg - ptr to var where to put the len actually read */
439 act_len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
443 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
445 } else if (di->type & DEV_TYP_NET) {
447 /* 3. arg points to the var with length of packet to read */
448 len_net = (int *)va_arg(ap, u_int32_t);
454 /* 4. - ptr to var where to put the len actually read */
455 act_len_net = (int *)va_arg(ap, u_int32_t);
459 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
471 * int API_env_get(const char *name, char **value)
473 * name: ptr to name of env var
475 static int API_env_get(va_list ap)
479 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
481 if ((value = (char **)va_arg(ap, u_int32_t)) == NULL)
484 *value = getenv(name);
492 * int API_env_set(const char *name, const char *value)
494 * name: ptr to name of env var
496 * value: ptr to value to be set
498 static int API_env_set(va_list ap)
502 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
504 if ((value = (char *)va_arg(ap, u_int32_t)) == NULL)
515 * int API_env_enum(const char *last, char **next)
517 * last: ptr to name of env var found in last iteration
519 static int API_env_enum(va_list ap)
524 last = (char *)va_arg(ap, u_int32_t);
526 if ((next = (char **)va_arg(ap, u_int32_t)) == NULL)
531 *next = ((char *)env_get_addr(0));
535 for (i = 0; env_get_char(i) != '\0'; i = n + 1) {
536 for (n = i; env_get_char(n) != '\0'; ++n) {
537 if (n >= CONFIG_ENV_SIZE) {
538 /* XXX shouldn't we set *next = NULL?? */
543 if (envmatch((uchar *)last, i) < 0)
546 /* try to get next name */
548 if (env_get_char(i) == '\0') {
554 *next = ((char *)env_get_addr(i));
562 static cfp_t calls_table[API_MAXCALL] = { NULL, };
565 * The main syscall entry point - this is not reentrant, only one call is
566 * serviced until finished.
568 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
570 * call: syscall number
572 * retval: points to the return value placeholder, this is the place the
573 * syscall puts its return value, if NULL the caller does not
574 * expect a return value
576 * ... syscall arguments (variable number)
578 * returns: 0 if the call not found, 1 if serviced
580 int syscall(int call, int *retval, ...)
585 if (call < 0 || call >= calls_no) {
586 debugf("invalid call #%d\n", call);
590 if (calls_table[call] == NULL) {
591 debugf("syscall #%d does not have a handler\n", call);
595 va_start(ap, retval);
596 rv = calls_table[call](ap);
605 struct api_signature *sig = NULL;
607 /* TODO put this into linker set one day... */
608 calls_table[API_RSVD] = NULL;
609 calls_table[API_GETC] = &API_getc;
610 calls_table[API_PUTC] = &API_putc;
611 calls_table[API_TSTC] = &API_tstc;
612 calls_table[API_PUTS] = &API_puts;
613 calls_table[API_RESET] = &API_reset;
614 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
615 calls_table[API_UDELAY] = &API_udelay;
616 calls_table[API_GET_TIMER] = &API_get_timer;
617 calls_table[API_DEV_ENUM] = &API_dev_enum;
618 calls_table[API_DEV_OPEN] = &API_dev_open;
619 calls_table[API_DEV_CLOSE] = &API_dev_close;
620 calls_table[API_DEV_READ] = &API_dev_read;
621 calls_table[API_DEV_WRITE] = &API_dev_write;
622 calls_table[API_ENV_GET] = &API_env_get;
623 calls_table[API_ENV_SET] = &API_env_set;
624 calls_table[API_ENV_ENUM] = &API_env_enum;
625 calls_no = API_MAXCALL;
627 debugf("API initialized with %d calls\n", calls_no);
632 * Produce the signature so the API consumers can find it
634 sig = malloc(sizeof(struct api_signature));
636 printf("API: could not allocate memory for the signature!\n");
640 debugf("API sig @ 0x%08x\n", sig);
641 memcpy(sig->magic, API_SIG_MAGIC, 8);
642 sig->version = API_SIG_VERSION;
643 sig->syscall = &syscall;
645 sig->checksum = crc32(0, (unsigned char *)sig,
646 sizeof(struct api_signature));
647 debugf("syscall entry: 0x%08x\n", sig->syscall);
650 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
655 if (!si->mr || !size || (flags == 0))
659 for (i = 0; i < si->mr_no; i++)
660 if (si->mr[i].flags == 0) {
661 /* insert new mem region */
662 si->mr[i].start = start;
663 si->mr[i].size = size;
664 si->mr[i].flags = flags;
669 #endif /* CONFIG_API */