2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
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,
30 #include <environment.h>
31 #include <linux/types.h>
32 #include <api_public.h>
34 #include "api_private.h"
39 /* U-Boot routines needed */
40 extern int do_reset (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[]);
42 /*****************************************************************************
44 * This is the API core.
46 * API_ functions are part of U-Boot code and constitute the lowest level
49 * - they know what values they need as arguments
50 * - their direct return value pertains to the API_ "shell" itself (0 on
51 * success, some error code otherwise)
52 * - if the call returns a value it is buried within arguments
54 ****************************************************************************/
57 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
59 #define debugf(fmt, args...)
62 typedef int (*cfp_t
)(va_list argp
);
69 * int API_getc(int *c)
71 static int API_getc(va_list ap
)
75 if ((c
= (int *)va_arg(ap
, u_int32_t
)) == NULL
)
85 * int API_tstc(int *c)
87 static int API_tstc(va_list ap
)
91 if ((t
= (int *)va_arg(ap
, u_int32_t
)) == NULL
)
101 * int API_putc(char *ch)
103 static int API_putc(va_list ap
)
107 if ((c
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
117 * int API_puts(char **s)
119 static int API_puts(va_list ap
)
123 if ((s
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
133 * int API_reset(void)
135 static int API_reset(va_list ap
)
137 do_reset(NULL
, 0, 0, NULL
);
146 * int API_get_sys_info(struct sys_info *si)
148 * fill out the sys_info struct containing selected parameters about the
151 static int API_get_sys_info(va_list ap
)
155 si
= (struct sys_info
*)va_arg(ap
, u_int32_t
);
159 return (platform_sys_info(si
)) ? 0 : API_ENODEV
;
165 * int API_udelay(unsigned long *udelay)
167 static int API_udelay(va_list ap
)
171 if ((d
= (unsigned long *)va_arg(ap
, u_int32_t
)) == NULL
)
181 * int API_get_timer(unsigned long *current, unsigned long *base)
183 static int API_get_timer(va_list ap
)
185 unsigned long *base
, *cur
;
187 cur
= (unsigned long *)va_arg(ap
, u_int32_t
);
191 base
= (unsigned long *)va_arg(ap
, u_int32_t
);
195 *cur
= get_timer(*base
);
200 /*****************************************************************************
204 * int API_dev_enum(struct device_info *)
207 * cookies uniqely identify the previously enumerated device instance and
208 * provide a hint for what to inspect in current enum iteration:
210 * - net: ð_device struct address from list pointed to by eth_devices
212 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
213 * &scsi_dev_desc[n] and similar tables
215 ****************************************************************************/
217 static int API_dev_enum(va_list ap
)
219 struct device_info
*di
;
221 /* arg is ptr to the device_info struct we are going to fill out */
222 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
226 if (di
->cookie
== NULL
) {
227 /* start over - clean up enumeration */
228 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
229 debugf("RESTART ENUM\n");
231 /* net device enumeration first */
232 if (dev_enum_net(di
))
237 * The hidden assumption is there can only be one active network
238 * device and it is identified upon enumeration (re)start, so there's
239 * no point in trying to find network devices in other cases than the
240 * (re)start and hence the 'next' device can only be storage
242 if (!dev_enum_storage(di
))
243 /* make sure we mark there are no more devices */
250 static int API_dev_open(va_list ap
)
252 struct device_info
*di
;
255 /* arg is ptr to the device_info struct */
256 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
260 /* Allow only one consumer of the device at a time */
261 if (di
->state
== DEV_STA_OPEN
)
264 if (di
->cookie
== NULL
)
267 if (di
->type
& DEV_TYP_STOR
)
268 err
= dev_open_stor(di
->cookie
);
270 else if (di
->type
& DEV_TYP_NET
)
271 err
= dev_open_net(di
->cookie
);
276 di
->state
= DEV_STA_OPEN
;
282 static int API_dev_close(va_list ap
)
284 struct device_info
*di
;
287 /* arg is ptr to the device_info struct */
288 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
292 if (di
->state
== DEV_STA_CLOSED
)
295 if (di
->cookie
== NULL
)
298 if (di
->type
& DEV_TYP_STOR
)
299 err
= dev_close_stor(di
->cookie
);
301 else if (di
->type
& DEV_TYP_NET
)
302 err
= dev_close_net(di
->cookie
);
305 * In case of unknown device we cannot change its state, so
306 * only return error code
311 di
->state
= DEV_STA_CLOSED
;
318 * Notice: this is for sending network packets only, as U-Boot does not
319 * support writing to storage at the moment (12.2007)
324 * struct device_info *di,
329 * buf: ptr to buffer from where to get the data to send
331 * len: length of packet to be sent (in bytes)
334 static int API_dev_write(va_list ap
)
336 struct device_info
*di
;
341 /* 1. arg is ptr to the device_info struct */
342 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
346 /* XXX should we check if device is open? i.e. the ->state ? */
348 if (di
->cookie
== NULL
)
351 /* 2. arg is ptr to buffer from where to get data to write */
352 buf
= (void *)va_arg(ap
, u_int32_t
);
356 /* 3. arg is length of buffer */
357 len
= (int *)va_arg(ap
, u_int32_t
);
363 if (di
->type
& DEV_TYP_STOR
)
365 * write to storage is currently not supported by U-Boot:
366 * no storage device implements block_write() method
370 else if (di
->type
& DEV_TYP_NET
)
371 err
= dev_write_net(di
->cookie
, buf
, *len
);
383 * struct device_info *di,
386 * unsigned long *start
390 * buf: ptr to buffer where to put the read data
392 * len: ptr to length to be read
393 * - network: len of packet to read (in bytes)
394 * - storage: # of blocks to read (can vary in size depending on define)
396 * start: ptr to start block (only used for storage devices, ignored for
399 * act_len: ptr to where to put the len actually read
401 static int API_dev_read(va_list ap
)
403 struct device_info
*di
;
405 lbasize_t
*len_stor
, *act_len_stor
;
407 int *len_net
, *act_len_net
;
409 /* 1. arg is ptr to the device_info struct */
410 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
414 /* XXX should we check if device is open? i.e. the ->state ? */
416 if (di
->cookie
== NULL
)
419 /* 2. arg is ptr to buffer from where to put the read data */
420 buf
= (void *)va_arg(ap
, u_int32_t
);
424 if (di
->type
& DEV_TYP_STOR
) {
425 /* 3. arg - ptr to var with # of blocks to read */
426 len_stor
= (lbasize_t
*)va_arg(ap
, u_int32_t
);
432 /* 4. arg - ptr to var with start block */
433 start
= (lbastart_t
*)va_arg(ap
, u_int32_t
);
435 /* 5. arg - ptr to var where to put the len actually read */
436 act_len_stor
= (lbasize_t
*)va_arg(ap
, u_int32_t
);
440 *act_len_stor
= dev_read_stor(di
->cookie
, buf
, *len_stor
, *start
);
442 } else if (di
->type
& DEV_TYP_NET
) {
444 /* 3. arg points to the var with length of packet to read */
445 len_net
= (int *)va_arg(ap
, u_int32_t
);
451 /* 4. - ptr to var where to put the len actually read */
452 act_len_net
= (int *)va_arg(ap
, u_int32_t
);
456 *act_len_net
= dev_read_net(di
->cookie
, buf
, *len_net
);
468 * int API_env_get(const char *name, char **value)
470 * name: ptr to name of env var
472 static int API_env_get(va_list ap
)
476 if ((name
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
478 if ((value
= (char **)va_arg(ap
, u_int32_t
)) == NULL
)
481 *value
= getenv(name
);
489 * int API_env_set(const char *name, const char *value)
491 * name: ptr to name of env var
493 * value: ptr to value to be set
495 static int API_env_set(va_list ap
)
499 if ((name
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
501 if ((value
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
512 * int API_env_enum(const char *last, char **next)
514 * last: ptr to name of env var found in last iteration
516 static int API_env_enum(va_list ap
)
521 last
= (char *)va_arg(ap
, u_int32_t
);
523 if ((next
= (char **)va_arg(ap
, u_int32_t
)) == NULL
)
528 *next
= ((char *)env_get_addr(0));
532 for (i
= 0; env_get_char(i
) != '\0'; i
= n
+ 1) {
533 for (n
= i
; env_get_char(n
) != '\0'; ++n
) {
534 if (n
>= CONFIG_ENV_SIZE
) {
535 /* XXX shouldn't we set *next = NULL?? */
540 if (envmatch((uchar
*)last
, i
) < 0)
543 /* try to get next name */
545 if (env_get_char(i
) == '\0') {
551 *next
= ((char *)env_get_addr(i
));
559 static cfp_t calls_table
[API_MAXCALL
] = { NULL
, };
562 * The main syscall entry point - this is not reentrant, only one call is
563 * serviced until finished.
565 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
567 * call: syscall number
569 * retval: points to the return value placeholder, this is the place the
570 * syscall puts its return value, if NULL the caller does not
571 * expect a return value
573 * ... syscall arguments (variable number)
575 * returns: 0 if the call not found, 1 if serviced
577 int syscall(int call
, int *retval
, ...)
582 if (call
< 0 || call
>= calls_no
) {
583 debugf("invalid call #%d\n", call
);
587 if (calls_table
[call
] == NULL
) {
588 debugf("syscall #%d does not have a handler\n", call
);
592 va_start(ap
, retval
);
593 rv
= calls_table
[call
](ap
);
602 struct api_signature
*sig
= NULL
;
604 /* TODO put this into linker set one day... */
605 calls_table
[API_RSVD
] = NULL
;
606 calls_table
[API_GETC
] = &API_getc
;
607 calls_table
[API_PUTC
] = &API_putc
;
608 calls_table
[API_TSTC
] = &API_tstc
;
609 calls_table
[API_PUTS
] = &API_puts
;
610 calls_table
[API_RESET
] = &API_reset
;
611 calls_table
[API_GET_SYS_INFO
] = &API_get_sys_info
;
612 calls_table
[API_UDELAY
] = &API_udelay
;
613 calls_table
[API_GET_TIMER
] = &API_get_timer
;
614 calls_table
[API_DEV_ENUM
] = &API_dev_enum
;
615 calls_table
[API_DEV_OPEN
] = &API_dev_open
;
616 calls_table
[API_DEV_CLOSE
] = &API_dev_close
;
617 calls_table
[API_DEV_READ
] = &API_dev_read
;
618 calls_table
[API_DEV_WRITE
] = &API_dev_write
;
619 calls_table
[API_ENV_GET
] = &API_env_get
;
620 calls_table
[API_ENV_SET
] = &API_env_set
;
621 calls_table
[API_ENV_ENUM
] = &API_env_enum
;
622 calls_no
= API_MAXCALL
;
624 debugf("API initialized with %d calls\n", calls_no
);
629 * Produce the signature so the API consumers can find it
631 sig
= malloc(sizeof(struct api_signature
));
633 printf("API: could not allocate memory for the signature!\n");
637 debugf("API sig @ 0x%08x\n", sig
);
638 memcpy(sig
->magic
, API_SIG_MAGIC
, 8);
639 sig
->version
= API_SIG_VERSION
;
640 sig
->syscall
= &syscall
;
642 sig
->checksum
= crc32(0, (unsigned char *)sig
,
643 sizeof(struct api_signature
));
644 debugf("syscall entry: 0x%08x\n", sig
->syscall
);
647 void platform_set_mr(struct sys_info
*si
, unsigned long start
, unsigned long size
,
652 if (!si
->mr
|| !size
|| (flags
== 0))
656 for (i
= 0; i
< si
->mr_no
; i
++)
657 if (si
->mr
[i
].flags
== 0) {
658 /* insert new mem region */
659 si
->mr
[i
].start
= start
;
660 si
->mr
[i
].size
= size
;
661 si
->mr
[i
].flags
= flags
;