1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007 Semihalf
13 #include <env_internal.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <api_public.h>
19 #include <u-boot/crc.h>
21 #include "api_private.h"
26 /*****************************************************************************
28 * This is the API core.
30 * API_ functions are part of U-Boot code and constitute the lowest level
33 * - they know what values they need as arguments
34 * - their direct return value pertains to the API_ "shell" itself (0 on
35 * success, some error code otherwise)
36 * - if the call returns a value it is buried within arguments
38 ****************************************************************************/
41 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
43 #define debugf(fmt, args...)
46 typedef int (*cfp_t)(va_list argp);
53 * int API_getc(int *c)
55 static int API_getc(va_list ap)
59 if ((c = (int *)va_arg(ap, uintptr_t)) == NULL)
69 * int API_tstc(int *c)
71 static int API_tstc(va_list ap)
75 if ((t = (int *)va_arg(ap, uintptr_t)) == NULL)
85 * int API_putc(char *ch)
87 static int API_putc(va_list ap)
91 if ((c = (char *)va_arg(ap, uintptr_t)) == NULL)
101 * int API_puts(char **s)
103 static int API_puts(va_list ap)
107 if ((s = (char *)va_arg(ap, uintptr_t)) == NULL)
117 * int API_reset(void)
119 static int API_reset(va_list ap)
121 do_reset(NULL, 0, 0, NULL);
130 * int API_get_sys_info(struct sys_info *si)
132 * fill out the sys_info struct containing selected parameters about the
135 static int API_get_sys_info(va_list ap)
139 si = (struct sys_info *)va_arg(ap, uintptr_t);
143 return (platform_sys_info(si)) ? 0 : API_ENODEV;
149 * int API_udelay(unsigned long *udelay)
151 static int API_udelay(va_list ap)
155 if ((d = (unsigned long *)va_arg(ap, unsigned long)) == NULL)
165 * int API_get_timer(unsigned long *current, unsigned long *base)
167 static int API_get_timer(va_list ap)
169 unsigned long *base, *cur;
171 cur = (unsigned long *)va_arg(ap, unsigned long);
175 base = (unsigned long *)va_arg(ap, unsigned long);
179 *cur = get_timer(*base);
184 /*****************************************************************************
188 * int API_dev_enum(struct device_info *)
191 * cookies uniqely identify the previously enumerated device instance and
192 * provide a hint for what to inspect in current enum iteration:
194 * - net: ð_device struct address from list pointed to by eth_devices
196 * - storage: struct blk_desc struct address from &ide_dev_desc[n],
197 * &scsi_dev_desc[n] and similar tables
199 ****************************************************************************/
201 static int API_dev_enum(va_list ap)
203 struct device_info *di;
205 /* arg is ptr to the device_info struct we are going to fill out */
206 di = (struct device_info *)va_arg(ap, uintptr_t);
210 if (di->cookie == NULL) {
211 /* start over - clean up enumeration */
212 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
213 debugf("RESTART ENUM\n");
215 /* net device enumeration first */
216 if (dev_enum_net(di))
221 * The hidden assumption is there can only be one active network
222 * device and it is identified upon enumeration (re)start, so there's
223 * no point in trying to find network devices in other cases than the
224 * (re)start and hence the 'next' device can only be storage
226 if (!dev_enum_storage(di))
227 /* make sure we mark there are no more devices */
234 static int API_dev_open(va_list ap)
236 struct device_info *di;
239 /* arg is ptr to the device_info struct */
240 di = (struct device_info *)va_arg(ap, uintptr_t);
244 /* Allow only one consumer of the device at a time */
245 if (di->state == DEV_STA_OPEN)
248 if (di->cookie == NULL)
251 if (di->type & DEV_TYP_STOR)
252 err = dev_open_stor(di->cookie);
254 else if (di->type & DEV_TYP_NET)
255 err = dev_open_net(di->cookie);
260 di->state = DEV_STA_OPEN;
266 static int API_dev_close(va_list ap)
268 struct device_info *di;
271 /* arg is ptr to the device_info struct */
272 di = (struct device_info *)va_arg(ap, uintptr_t);
276 if (di->state == DEV_STA_CLOSED)
279 if (di->cookie == NULL)
282 if (di->type & DEV_TYP_STOR)
283 err = dev_close_stor(di->cookie);
285 else if (di->type & DEV_TYP_NET)
286 err = dev_close_net(di->cookie);
289 * In case of unknown device we cannot change its state, so
290 * only return error code
295 di->state = DEV_STA_CLOSED;
305 * struct device_info *di,
308 * unsigned long *start
311 * buf: ptr to buffer from where to get the data to send
313 * len: ptr to length to be read
314 * - network: len of packet to be sent (in bytes)
315 * - storage: # of blocks to write (can vary in size depending on define)
317 * start: ptr to start block (only used for storage devices, ignored for
320 static int API_dev_write(va_list ap)
322 struct device_info *di;
324 lbasize_t *len_stor, act_len_stor;
329 /* 1. arg is ptr to the device_info struct */
330 di = (struct device_info *)va_arg(ap, uintptr_t);
334 /* XXX should we check if device is open? i.e. the ->state ? */
336 if (di->cookie == NULL)
339 /* 2. arg is ptr to buffer from where to get data to write */
340 buf = (void *)va_arg(ap, uintptr_t);
344 if (di->type & DEV_TYP_STOR) {
345 /* 3. arg - ptr to var with # of blocks to write */
346 len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
352 /* 4. arg - ptr to var with start block */
353 start = (lbastart_t *)va_arg(ap, uintptr_t);
355 act_len_stor = dev_write_stor(di->cookie, buf, *len_stor, *start);
356 if (act_len_stor != *len_stor) {
357 debugf("write @ %llu: done %llu out of %llu blocks",
358 (uint64_t)blk, (uint64_t)act_len_stor,
363 } else if (di->type & DEV_TYP_NET) {
364 /* 3. arg points to the var with length of packet to write */
365 len_net = (int *)va_arg(ap, uintptr_t);
371 err = dev_write_net(di->cookie, buf, *len_net);
384 * struct device_info *di,
387 * unsigned long *start
391 * buf: ptr to buffer where to put the read data
393 * len: ptr to length to be read
394 * - network: len of packet to read (in bytes)
395 * - storage: # of blocks to read (can vary in size depending on define)
397 * start: ptr to start block (only used for storage devices, ignored for
400 * act_len: ptr to where to put the len actually read
402 static int API_dev_read(va_list ap)
404 struct device_info *di;
406 lbasize_t *len_stor, *act_len_stor;
408 int *len_net, *act_len_net;
410 /* 1. arg is ptr to the device_info struct */
411 di = (struct device_info *)va_arg(ap, uintptr_t);
415 /* XXX should we check if device is open? i.e. the ->state ? */
417 if (di->cookie == NULL)
420 /* 2. arg is ptr to buffer from where to put the read data */
421 buf = (void *)va_arg(ap, uintptr_t);
425 if (di->type & DEV_TYP_STOR) {
426 /* 3. arg - ptr to var with # of blocks to read */
427 len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
433 /* 4. arg - ptr to var with start block */
434 start = (lbastart_t *)va_arg(ap, uintptr_t);
436 /* 5. arg - ptr to var where to put the len actually read */
437 act_len_stor = (lbasize_t *)va_arg(ap, uintptr_t);
441 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
443 } else if (di->type & DEV_TYP_NET) {
445 /* 3. arg points to the var with length of packet to read */
446 len_net = (int *)va_arg(ap, uintptr_t);
452 /* 4. - ptr to var where to put the len actually read */
453 act_len_net = (int *)va_arg(ap, uintptr_t);
457 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
469 * int API_env_get(const char *name, char **value)
471 * name: ptr to name of env var
473 static int API_env_get(va_list ap)
477 if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
479 if ((value = (char **)va_arg(ap, uintptr_t)) == NULL)
482 *value = env_get(name);
490 * int API_env_set(const char *name, const char *value)
492 * name: ptr to name of env var
494 * value: ptr to value to be set
496 static int API_env_set(va_list ap)
500 if ((name = (char *)va_arg(ap, uintptr_t)) == NULL)
502 if ((value = (char *)va_arg(ap, uintptr_t)) == NULL)
505 env_set(name, value);
513 * int API_env_enum(const char *last, char **next)
515 * last: ptr to name of env var found in last iteration
517 static int API_env_enum(va_list ap)
520 char *last, **next, *s;
521 struct env_entry *match, search;
524 last = (char *)va_arg(ap, unsigned long);
526 if ((next = (char **)va_arg(ap, uintptr_t)) == NULL)
534 s = strchr(var, '=');
538 i = hsearch_r(search, ENV_FIND, &match, &env_htab, 0);
545 /* match the next entry after i */
546 i = hmatch_r("", i, &match, &env_htab);
549 buflen = strlen(match->key) + strlen(match->data) + 2;
550 var = realloc(var, buflen);
551 snprintf(var, buflen, "%s=%s", match->key, match->data);
565 * int API_display_get_info(int type, struct display_info *di)
567 static int API_display_get_info(va_list ap)
570 struct display_info *di;
572 type = va_arg(ap, int);
573 di = va_arg(ap, struct display_info *);
575 return display_get_info(type, di);
581 * int API_display_draw_bitmap(ulong bitmap, int x, int y)
583 static int API_display_draw_bitmap(va_list ap)
588 bitmap = va_arg(ap, ulong);
592 return display_draw_bitmap(bitmap, x, y);
598 * void API_display_clear(void)
600 static int API_display_clear(va_list ap)
606 static cfp_t calls_table[API_MAXCALL] = { NULL, };
609 * The main syscall entry point - this is not reentrant, only one call is
610 * serviced until finished.
612 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
614 * call: syscall number
616 * retval: points to the return value placeholder, this is the place the
617 * syscall puts its return value, if NULL the caller does not
618 * expect a return value
620 * ... syscall arguments (variable number)
622 * returns: 0 if the call not found, 1 if serviced
624 int syscall(int call, int *retval, ...)
629 if (call < 0 || call >= calls_no) {
630 debugf("invalid call #%d\n", call);
634 if (calls_table[call] == NULL) {
635 debugf("syscall #%d does not have a handler\n", call);
639 va_start(ap, retval);
640 rv = calls_table[call](ap);
649 struct api_signature *sig;
651 /* TODO put this into linker set one day... */
652 calls_table[API_RSVD] = NULL;
653 calls_table[API_GETC] = &API_getc;
654 calls_table[API_PUTC] = &API_putc;
655 calls_table[API_TSTC] = &API_tstc;
656 calls_table[API_PUTS] = &API_puts;
657 calls_table[API_RESET] = &API_reset;
658 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
659 calls_table[API_UDELAY] = &API_udelay;
660 calls_table[API_GET_TIMER] = &API_get_timer;
661 calls_table[API_DEV_ENUM] = &API_dev_enum;
662 calls_table[API_DEV_OPEN] = &API_dev_open;
663 calls_table[API_DEV_CLOSE] = &API_dev_close;
664 calls_table[API_DEV_READ] = &API_dev_read;
665 calls_table[API_DEV_WRITE] = &API_dev_write;
666 calls_table[API_ENV_GET] = &API_env_get;
667 calls_table[API_ENV_SET] = &API_env_set;
668 calls_table[API_ENV_ENUM] = &API_env_enum;
669 calls_table[API_DISPLAY_GET_INFO] = &API_display_get_info;
670 calls_table[API_DISPLAY_DRAW_BITMAP] = &API_display_draw_bitmap;
671 calls_table[API_DISPLAY_CLEAR] = &API_display_clear;
672 calls_no = API_MAXCALL;
674 debugf("API initialized with %d calls\n", calls_no);
679 * Produce the signature so the API consumers can find it
681 sig = malloc(sizeof(struct api_signature));
683 printf("API: could not allocate memory for the signature!\n");
687 env_set_hex("api_address", (unsigned long)sig);
688 debugf("API sig @ 0x%lX\n", (unsigned long)sig);
689 memcpy(sig->magic, API_SIG_MAGIC, 8);
690 sig->version = API_SIG_VERSION;
691 sig->syscall = &syscall;
693 sig->checksum = crc32(0, (unsigned char *)sig,
694 sizeof(struct api_signature));
695 debugf("syscall entry: 0x%lX\n", (unsigned long)sig->syscall);
700 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
705 if (!si->mr || !size || (flags == 0))
709 for (i = 0; i < si->mr_no; i++)
710 if (si->mr[i].flags == 0) {
711 /* insert new mem region */
712 si->mr[i].start = start;
713 si->mr[i].size = size;
714 si->mr[i].flags = flags;