]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - api/api.c
2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <environment.h>
14 #include <linux/types.h>
15 #include <api_public.h>
17 #include "api_private.h"
22 /*****************************************************************************
24 * This is the API core.
26 * API_ functions are part of U-Boot code and constitute the lowest level
29 * - they know what values they need as arguments
30 * - their direct return value pertains to the API_ "shell" itself (0 on
31 * success, some error code otherwise)
32 * - if the call returns a value it is buried within arguments
34 ****************************************************************************/
37 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
39 #define debugf(fmt, args...)
42 typedef int (*cfp_t
)(va_list argp
);
49 * int API_getc(int *c)
51 static int API_getc(va_list ap
)
55 if ((c
= (int *)va_arg(ap
, uintptr_t)) == NULL
)
65 * int API_tstc(int *c)
67 static int API_tstc(va_list ap
)
71 if ((t
= (int *)va_arg(ap
, uintptr_t)) == NULL
)
81 * int API_putc(char *ch)
83 static int API_putc(va_list ap
)
87 if ((c
= (char *)va_arg(ap
, uintptr_t)) == NULL
)
97 * int API_puts(char **s)
99 static int API_puts(va_list ap
)
103 if ((s
= (char *)va_arg(ap
, uintptr_t)) == NULL
)
113 * int API_reset(void)
115 static int API_reset(va_list ap
)
117 do_reset(NULL
, 0, 0, NULL
);
126 * int API_get_sys_info(struct sys_info *si)
128 * fill out the sys_info struct containing selected parameters about the
131 static int API_get_sys_info(va_list ap
)
135 si
= (struct sys_info
*)va_arg(ap
, uintptr_t);
139 return (platform_sys_info(si
)) ? 0 : API_ENODEV
;
145 * int API_udelay(unsigned long *udelay)
147 static int API_udelay(va_list ap
)
151 if ((d
= (unsigned long *)va_arg(ap
, unsigned long)) == NULL
)
161 * int API_get_timer(unsigned long *current, unsigned long *base)
163 static int API_get_timer(va_list ap
)
165 unsigned long *base
, *cur
;
167 cur
= (unsigned long *)va_arg(ap
, unsigned long);
171 base
= (unsigned long *)va_arg(ap
, unsigned long);
175 *cur
= get_timer(*base
);
180 /*****************************************************************************
184 * int API_dev_enum(struct device_info *)
187 * cookies uniqely identify the previously enumerated device instance and
188 * provide a hint for what to inspect in current enum iteration:
190 * - net: ð_device struct address from list pointed to by eth_devices
192 * - storage: struct blk_desc struct address from &ide_dev_desc[n],
193 * &scsi_dev_desc[n] and similar tables
195 ****************************************************************************/
197 static int API_dev_enum(va_list ap
)
199 struct device_info
*di
;
201 /* arg is ptr to the device_info struct we are going to fill out */
202 di
= (struct device_info
*)va_arg(ap
, uintptr_t);
206 if (di
->cookie
== NULL
) {
207 /* start over - clean up enumeration */
208 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
209 debugf("RESTART ENUM\n");
211 /* net device enumeration first */
212 if (dev_enum_net(di
))
217 * The hidden assumption is there can only be one active network
218 * device and it is identified upon enumeration (re)start, so there's
219 * no point in trying to find network devices in other cases than the
220 * (re)start and hence the 'next' device can only be storage
222 if (!dev_enum_storage(di
))
223 /* make sure we mark there are no more devices */
230 static int API_dev_open(va_list ap
)
232 struct device_info
*di
;
235 /* arg is ptr to the device_info struct */
236 di
= (struct device_info
*)va_arg(ap
, uintptr_t);
240 /* Allow only one consumer of the device at a time */
241 if (di
->state
== DEV_STA_OPEN
)
244 if (di
->cookie
== NULL
)
247 if (di
->type
& DEV_TYP_STOR
)
248 err
= dev_open_stor(di
->cookie
);
250 else if (di
->type
& DEV_TYP_NET
)
251 err
= dev_open_net(di
->cookie
);
256 di
->state
= DEV_STA_OPEN
;
262 static int API_dev_close(va_list ap
)
264 struct device_info
*di
;
267 /* arg is ptr to the device_info struct */
268 di
= (struct device_info
*)va_arg(ap
, uintptr_t);
272 if (di
->state
== DEV_STA_CLOSED
)
275 if (di
->cookie
== NULL
)
278 if (di
->type
& DEV_TYP_STOR
)
279 err
= dev_close_stor(di
->cookie
);
281 else if (di
->type
& DEV_TYP_NET
)
282 err
= dev_close_net(di
->cookie
);
285 * In case of unknown device we cannot change its state, so
286 * only return error code
291 di
->state
= DEV_STA_CLOSED
;
298 * Notice: this is for sending network packets only, as U-Boot does not
299 * support writing to storage at the moment (12.2007)
304 * struct device_info *di,
309 * buf: ptr to buffer from where to get the data to send
311 * len: length of packet to be sent (in bytes)
314 static int API_dev_write(va_list ap
)
316 struct device_info
*di
;
321 /* 1. arg is ptr to the device_info struct */
322 di
= (struct device_info
*)va_arg(ap
, uintptr_t);
326 /* XXX should we check if device is open? i.e. the ->state ? */
328 if (di
->cookie
== NULL
)
331 /* 2. arg is ptr to buffer from where to get data to write */
332 buf
= (void *)va_arg(ap
, uintptr_t);
336 /* 3. arg is length of buffer */
337 len
= (int *)va_arg(ap
, uintptr_t);
343 if (di
->type
& DEV_TYP_STOR
)
345 * write to storage is currently not supported by U-Boot:
346 * no storage device implements block_write() method
350 else if (di
->type
& DEV_TYP_NET
)
351 err
= dev_write_net(di
->cookie
, buf
, *len
);
363 * struct device_info *di,
366 * unsigned long *start
370 * buf: ptr to buffer where to put the read data
372 * len: ptr to length to be read
373 * - network: len of packet to read (in bytes)
374 * - storage: # of blocks to read (can vary in size depending on define)
376 * start: ptr to start block (only used for storage devices, ignored for
379 * act_len: ptr to where to put the len actually read
381 static int API_dev_read(va_list ap
)
383 struct device_info
*di
;
385 lbasize_t
*len_stor
, *act_len_stor
;
387 int *len_net
, *act_len_net
;
389 /* 1. arg is ptr to the device_info struct */
390 di
= (struct device_info
*)va_arg(ap
, uintptr_t);
394 /* XXX should we check if device is open? i.e. the ->state ? */
396 if (di
->cookie
== NULL
)
399 /* 2. arg is ptr to buffer from where to put the read data */
400 buf
= (void *)va_arg(ap
, uintptr_t);
404 if (di
->type
& DEV_TYP_STOR
) {
405 /* 3. arg - ptr to var with # of blocks to read */
406 len_stor
= (lbasize_t
*)va_arg(ap
, uintptr_t);
412 /* 4. arg - ptr to var with start block */
413 start
= (lbastart_t
*)va_arg(ap
, uintptr_t);
415 /* 5. arg - ptr to var where to put the len actually read */
416 act_len_stor
= (lbasize_t
*)va_arg(ap
, uintptr_t);
420 *act_len_stor
= dev_read_stor(di
->cookie
, buf
, *len_stor
, *start
);
422 } else if (di
->type
& DEV_TYP_NET
) {
424 /* 3. arg points to the var with length of packet to read */
425 len_net
= (int *)va_arg(ap
, uintptr_t);
431 /* 4. - ptr to var where to put the len actually read */
432 act_len_net
= (int *)va_arg(ap
, uintptr_t);
436 *act_len_net
= dev_read_net(di
->cookie
, buf
, *len_net
);
448 * int API_env_get(const char *name, char **value)
450 * name: ptr to name of env var
452 static int API_env_get(va_list ap
)
456 if ((name
= (char *)va_arg(ap
, uintptr_t)) == NULL
)
458 if ((value
= (char **)va_arg(ap
, uintptr_t)) == NULL
)
461 *value
= getenv(name
);
469 * int API_env_set(const char *name, const char *value)
471 * name: ptr to name of env var
473 * value: ptr to value to be set
475 static int API_env_set(va_list ap
)
479 if ((name
= (char *)va_arg(ap
, uintptr_t)) == NULL
)
481 if ((value
= (char *)va_arg(ap
, uintptr_t)) == NULL
)
492 * int API_env_enum(const char *last, char **next)
494 * last: ptr to name of env var found in last iteration
496 static int API_env_enum(va_list ap
)
501 last
= (char *)va_arg(ap
, unsigned long);
503 if ((next
= (char **)va_arg(ap
, uintptr_t)) == NULL
)
508 *next
= ((char *)env_get_addr(0));
512 for (i
= 0; env_get_char(i
) != '\0'; i
= n
+ 1) {
513 for (n
= i
; env_get_char(n
) != '\0'; ++n
) {
514 if (n
>= CONFIG_ENV_SIZE
) {
515 /* XXX shouldn't we set *next = NULL?? */
520 if (envmatch((uchar
*)last
, i
) < 0)
523 /* try to get next name */
525 if (env_get_char(i
) == '\0') {
531 *next
= ((char *)env_get_addr(i
));
542 * int API_display_get_info(int type, struct display_info *di)
544 static int API_display_get_info(va_list ap
)
547 struct display_info
*di
;
549 type
= va_arg(ap
, int);
550 di
= va_arg(ap
, struct display_info
*);
552 return display_get_info(type
, di
);
558 * int API_display_draw_bitmap(ulong bitmap, int x, int y)
560 static int API_display_draw_bitmap(va_list ap
)
565 bitmap
= va_arg(ap
, ulong
);
569 return display_draw_bitmap(bitmap
, x
, y
);
575 * void API_display_clear(void)
577 static int API_display_clear(va_list ap
)
583 static cfp_t calls_table
[API_MAXCALL
] = { NULL
, };
586 * The main syscall entry point - this is not reentrant, only one call is
587 * serviced until finished.
589 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
591 * call: syscall number
593 * retval: points to the return value placeholder, this is the place the
594 * syscall puts its return value, if NULL the caller does not
595 * expect a return value
597 * ... syscall arguments (variable number)
599 * returns: 0 if the call not found, 1 if serviced
601 int syscall(int call
, int *retval
, ...)
606 if (call
< 0 || call
>= calls_no
) {
607 debugf("invalid call #%d\n", call
);
611 if (calls_table
[call
] == NULL
) {
612 debugf("syscall #%d does not have a handler\n", call
);
616 va_start(ap
, retval
);
617 rv
= calls_table
[call
](ap
);
626 struct api_signature
*sig
= NULL
;
628 /* TODO put this into linker set one day... */
629 calls_table
[API_RSVD
] = NULL
;
630 calls_table
[API_GETC
] = &API_getc
;
631 calls_table
[API_PUTC
] = &API_putc
;
632 calls_table
[API_TSTC
] = &API_tstc
;
633 calls_table
[API_PUTS
] = &API_puts
;
634 calls_table
[API_RESET
] = &API_reset
;
635 calls_table
[API_GET_SYS_INFO
] = &API_get_sys_info
;
636 calls_table
[API_UDELAY
] = &API_udelay
;
637 calls_table
[API_GET_TIMER
] = &API_get_timer
;
638 calls_table
[API_DEV_ENUM
] = &API_dev_enum
;
639 calls_table
[API_DEV_OPEN
] = &API_dev_open
;
640 calls_table
[API_DEV_CLOSE
] = &API_dev_close
;
641 calls_table
[API_DEV_READ
] = &API_dev_read
;
642 calls_table
[API_DEV_WRITE
] = &API_dev_write
;
643 calls_table
[API_ENV_GET
] = &API_env_get
;
644 calls_table
[API_ENV_SET
] = &API_env_set
;
645 calls_table
[API_ENV_ENUM
] = &API_env_enum
;
646 calls_table
[API_DISPLAY_GET_INFO
] = &API_display_get_info
;
647 calls_table
[API_DISPLAY_DRAW_BITMAP
] = &API_display_draw_bitmap
;
648 calls_table
[API_DISPLAY_CLEAR
] = &API_display_clear
;
649 calls_no
= API_MAXCALL
;
651 debugf("API initialized with %d calls\n", calls_no
);
656 * Produce the signature so the API consumers can find it
658 sig
= malloc(sizeof(struct api_signature
));
660 printf("API: could not allocate memory for the signature!\n");
664 setenv_hex("api_address", (unsigned long)sig
);
665 debugf("API sig @ 0x%lX\n", (unsigned long)sig
);
666 memcpy(sig
->magic
, API_SIG_MAGIC
, 8);
667 sig
->version
= API_SIG_VERSION
;
668 sig
->syscall
= &syscall
;
670 sig
->checksum
= crc32(0, (unsigned char *)sig
,
671 sizeof(struct api_signature
));
672 debugf("syscall entry: 0x%lX\n", (unsigned long)sig
->syscall
);
675 void platform_set_mr(struct sys_info
*si
, unsigned long start
, unsigned long size
,
680 if (!si
->mr
|| !size
|| (flags
== 0))
684 for (i
= 0; i
< si
->mr_no
; i
++)
685 if (si
->mr
[i
].flags
== 0) {
686 /* insert new mem region */
687 si
->mr
[i
].start
= start
;
688 si
->mr
[i
].size
= size
;
689 si
->mr
[i
].flags
= flags
;