1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007 Semihalf
5 * Written by: Rafal Jaworowski <raj@semihalf.com>
12 #include <environment.h>
13 #include <linux/types.h>
14 #include <api_public.h>
16 #include "api_private.h"
21 /*****************************************************************************
23 * This is the API core.
25 * API_ functions are part of U-Boot code and constitute the lowest level
28 * - they know what values they need as arguments
29 * - their direct return value pertains to the API_ "shell" itself (0 on
30 * success, some error code otherwise)
31 * - if the call returns a value it is buried within arguments
33 ****************************************************************************/
36 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
38 #define debugf(fmt, args...)
41 typedef int (*cfp_t)(va_list argp);
48 * int API_getc(int *c)
50 static int API_getc(va_list ap)
54 if ((c = (int *)va_arg(ap, uintptr_t)) == NULL)
64 * int API_tstc(int *c)
66 static int API_tstc(va_list ap)
70 if ((t = (int *)va_arg(ap, uintptr_t)) == NULL)
80 * int API_putc(char *ch)
82 static int API_putc(va_list ap)
86 if ((c = (char *)va_arg(ap, uintptr_t)) == NULL)
96 * int API_puts(char **s)
98 static int API_puts(va_list ap)
102 if ((s = (char *)va_arg(ap, uintptr_t)) == NULL)
112 * int API_reset(void)
114 static int API_reset(va_list ap)
116 do_reset(NULL, 0, 0, NULL);
125 * int API_get_sys_info(struct sys_info *si)
127 * fill out the sys_info struct containing selected parameters about the
130 static int API_get_sys_info(va_list ap)
134 si = (struct sys_info *)va_arg(ap, uintptr_t);
138 return (platform_sys_info(si)) ? 0 : API_ENODEV;
144 * int API_udelay(unsigned long *udelay)
146 static int API_udelay(va_list ap)
150 if ((d = (unsigned long *)va_arg(ap, unsigned long)) == NULL)
160 * int API_get_timer(unsigned long *current, unsigned long *base)
162 static int API_get_timer(va_list ap)
164 unsigned long *base, *cur;
166 cur = (unsigned long *)va_arg(ap, unsigned long);
170 base = (unsigned long *)va_arg(ap, unsigned long);
174 *cur = get_timer(*base);
179 /*****************************************************************************
183 * int API_dev_enum(struct device_info *)
186 * cookies uniqely identify the previously enumerated device instance and
187 * provide a hint for what to inspect in current enum iteration:
189 * - net: ð_device struct address from list pointed to by eth_devices
191 * - storage: struct blk_desc struct address from &ide_dev_desc[n],
192 * &scsi_dev_desc[n] and similar tables
194 ****************************************************************************/
196 static int API_dev_enum(va_list ap)
198 struct device_info *di;
200 /* arg is ptr to the device_info struct we are going to fill out */
201 di = (struct device_info *)va_arg(ap, uintptr_t);
205 if (di->cookie == NULL) {
206 /* start over - clean up enumeration */
207 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
208 debugf("RESTART ENUM\n");
210 /* net device enumeration first */
211 if (dev_enum_net(di))
216 * The hidden assumption is there can only be one active network
217 * device and it is identified upon enumeration (re)start, so there's
218 * no point in trying to find network devices in other cases than the
219 * (re)start and hence the 'next' device can only be storage
221 if (!dev_enum_storage(di))
222 /* make sure we mark there are no more devices */
229 static int API_dev_open(va_list ap)
231 struct device_info *di;
234 /* arg is ptr to the device_info struct */
235 di = (struct device_info *)va_arg(ap, uintptr_t);
239 /* Allow only one consumer of the device at a time */
240 if (di->state == DEV_STA_OPEN)
243 if (di->cookie == NULL)
246 if (di->type & DEV_TYP_STOR)
247 err = dev_open_stor(di->cookie);
249 else if (di->type & DEV_TYP_NET)
250 err = dev_open_net(di->cookie);
255 di->state = DEV_STA_OPEN;
261 static int API_dev_close(va_list ap)
263 struct device_info *di;
266 /* arg is ptr to the device_info struct */
267 di = (struct device_info *)va_arg(ap, uintptr_t);
271 if (di->state == DEV_STA_CLOSED)
274 if (di->cookie == NULL)
277 if (di->type & DEV_TYP_STOR)
278 err = dev_close_stor(di->cookie);
280 else if (di->type & DEV_TYP_NET)
281 err = dev_close_net(di->cookie);
284 * In case of unknown device we cannot change its state, so
285 * only return error code
290 di->state = DEV_STA_CLOSED;
297 * Notice: this is for sending network packets only, as U-Boot does not
298 * support writing to storage at the moment (12.2007)
303 * struct device_info *di,
308 * buf: ptr to buffer from where to get the data to send
310 * len: length of packet to be sent (in bytes)
313 static int API_dev_write(va_list ap)
315 struct device_info *di;
320 /* 1. arg is ptr to the device_info struct */
321 di = (struct device_info *)va_arg(ap, uintptr_t);
325 /* XXX should we check if device is open? i.e. the ->state ? */
327 if (di->cookie == NULL)
330 /* 2. arg is ptr to buffer from where to get data to write */
331 buf = (void *)va_arg(ap, uintptr_t);
335 /* 3. arg is length of buffer */
336 len = (int *)va_arg(ap, uintptr_t);
342 if (di->type & DEV_TYP_STOR)
344 * write to storage is currently not supported by U-Boot:
345 * no storage device implements block_write() method
349 else if (di->type & DEV_TYP_NET)
350 err = dev_write_net(di->cookie, buf, *len);
362 * struct device_info *di,
365 * unsigned long *start
369 * buf: ptr to buffer where to put the read data
371 * len: ptr to length to be read
372 * - network: len of packet to read (in bytes)
373 * - storage: # of blocks to read (can vary in size depending on define)
375 * start: ptr to start block (only used for storage devices, ignored for
378 * act_len: ptr to where to put the len actually read
380 static int API_dev_read(va_list ap)
382 struct device_info *di;
384 lbasize_t *len_stor, *act_len_stor;
386 int *len_net, *act_len_net;
388 /* 1. arg is ptr to the device_info struct */
389 di = (struct device_info *)va_arg(ap, uintptr_t);
393 /* XXX should we check if device is open? i.e. the ->state ? */
395 if (di->cookie == NULL)
398 /* 2. arg is ptr to buffer from where to put the read data */
399 buf = (void *)va_arg(ap, uintptr_t);
403 if (di->type & DEV_TYP_STOR) {
404 /* 3. arg - ptr to var with # of blocks to read */
405 len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
411 /* 4. arg - ptr to var with start block */
412 start = (lbastart_t *)va_arg(ap, uintptr_t);
414 /* 5. arg - ptr to var where to put the len actually read */
415 act_len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
419 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
421 } else if (di->type & DEV_TYP_NET) {
423 /* 3. arg points to the var with length of packet to read */
424 len_net = (int *)va_arg(ap, uintptr_t);
430 /* 4. - ptr to var where to put the len actually read */
431 act_len_net = (int *)va_arg(ap, uintptr_t);
435 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
447 * int API_env_get(const char *name, char **value)
449 * name: ptr to name of env var
451 static int API_env_get(va_list ap)
455 if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
457 if ((value = (char **)va_arg(ap, uintptr_t)) == NULL)
460 *value = env_get(name);
468 * int API_env_set(const char *name, const char *value)
470 * name: ptr to name of env var
472 * value: ptr to value to be set
474 static int API_env_set(va_list ap)
478 if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
480 if ((value = (char *)va_arg(ap, uintptr_t)) == NULL)
483 env_set(name, value);
491 * int API_env_enum(const char *last, char **next)
493 * last: ptr to name of env var found in last iteration
495 static int API_env_enum(va_list ap)
498 char *last, **next, *s;
499 ENTRY *match, search;
502 last = (char *)va_arg(ap, unsigned long);
504 if ((next = (char **)va_arg(ap, uintptr_t)) == NULL)
512 s = strchr(var, '=');
516 i = hsearch_r(search, FIND, &match, &env_htab, 0);
523 /* match the next entry after i */
524 i = hmatch_r("", i, &match, &env_htab);
527 buflen = strlen(match->key) + strlen(match->data) + 2;
528 var = realloc(var, buflen);
529 snprintf(var, buflen, "%s=%s", match->key, match->data);
543 * int API_display_get_info(int type, struct display_info *di)
545 static int API_display_get_info(va_list ap)
548 struct display_info *di;
550 type = va_arg(ap, int);
551 di = va_arg(ap, struct display_info *);
553 return display_get_info(type, di);
559 * int API_display_draw_bitmap(ulong bitmap, int x, int y)
561 static int API_display_draw_bitmap(va_list ap)
566 bitmap = va_arg(ap, ulong);
570 return display_draw_bitmap(bitmap, x, y);
576 * void API_display_clear(void)
578 static int API_display_clear(va_list ap)
584 static cfp_t calls_table[API_MAXCALL] = { NULL, };
587 * The main syscall entry point - this is not reentrant, only one call is
588 * serviced until finished.
590 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
592 * call: syscall number
594 * retval: points to the return value placeholder, this is the place the
595 * syscall puts its return value, if NULL the caller does not
596 * expect a return value
598 * ... syscall arguments (variable number)
600 * returns: 0 if the call not found, 1 if serviced
602 int syscall(int call, int *retval, ...)
607 if (call < 0 || call >= calls_no) {
608 debugf("invalid call #%d\n", call);
612 if (calls_table[call] == NULL) {
613 debugf("syscall #%d does not have a handler\n", call);
617 va_start(ap, retval);
618 rv = calls_table[call](ap);
627 struct api_signature *sig;
629 /* TODO put this into linker set one day... */
630 calls_table[API_RSVD] = NULL;
631 calls_table[API_GETC] = &API_getc;
632 calls_table[API_PUTC] = &API_putc;
633 calls_table[API_TSTC] = &API_tstc;
634 calls_table[API_PUTS] = &API_puts;
635 calls_table[API_RESET] = &API_reset;
636 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
637 calls_table[API_UDELAY] = &API_udelay;
638 calls_table[API_GET_TIMER] = &API_get_timer;
639 calls_table[API_DEV_ENUM] = &API_dev_enum;
640 calls_table[API_DEV_OPEN] = &API_dev_open;
641 calls_table[API_DEV_CLOSE] = &API_dev_close;
642 calls_table[API_DEV_READ] = &API_dev_read;
643 calls_table[API_DEV_WRITE] = &API_dev_write;
644 calls_table[API_ENV_GET] = &API_env_get;
645 calls_table[API_ENV_SET] = &API_env_set;
646 calls_table[API_ENV_ENUM] = &API_env_enum;
647 calls_table[API_DISPLAY_GET_INFO] = &API_display_get_info;
648 calls_table[API_DISPLAY_DRAW_BITMAP] = &API_display_draw_bitmap;
649 calls_table[API_DISPLAY_CLEAR] = &API_display_clear;
650 calls_no = API_MAXCALL;
652 debugf("API initialized with %d calls\n", calls_no);
657 * Produce the signature so the API consumers can find it
659 sig = malloc(sizeof(struct api_signature));
661 printf("API: could not allocate memory for the signature!\n");
665 env_set_hex("api_address", (unsigned long)sig);
666 debugf("API sig @ 0x%lX\n", (unsigned long)sig);
667 memcpy(sig->magic, API_SIG_MAGIC, 8);
668 sig->version = API_SIG_VERSION;
669 sig->syscall = &syscall;
671 sig->checksum = crc32(0, (unsigned char *)sig,
672 sizeof(struct api_signature));
673 debugf("syscall entry: 0x%lX\n", (unsigned long)sig->syscall);
676 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
681 if (!si->mr || !size || (flags == 0))
685 for (i = 0; i < si->mr_no; i++)
686 if (si->mr[i].flags == 0) {
687 /* insert new mem region */
688 si->mr[i].start = start;
689 si->mr[i].size = size;
690 si->mr[i].flags = flags;