]> git.ipfire.org Git - people/ms/u-boot.git/blob - api/api.c
Make sure that argv[] argument pointers are not modified.
[people/ms/u-boot.git] / api / api.c
1 /*
2 * (C) Copyright 2007 Semihalf
3 *
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
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.
13 *
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.
18 *
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,
22 * MA 02111-1307 USA
23 *
24 */
25
26 #include <config.h>
27 #include <command.h>
28 #include <common.h>
29 #include <malloc.h>
30 #include <environment.h>
31 #include <linux/types.h>
32 #include <api_public.h>
33
34 #include "api_private.h"
35
36 #define DEBUG
37 #undef DEBUG
38
39 /* U-Boot routines needed */
40 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
41
42 /*****************************************************************************
43 *
44 * This is the API core.
45 *
46 * API_ functions are part of U-Boot code and constitute the lowest level
47 * calls:
48 *
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
53 *
54 ****************************************************************************/
55
56 #ifdef DEBUG
57 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
58 #else
59 #define debugf(fmt, args...)
60 #endif
61
62 typedef int (*cfp_t)(va_list argp);
63
64 static int calls_no;
65
66 /*
67 * pseudo signature:
68 *
69 * int API_getc(int *c)
70 */
71 static int API_getc(va_list ap)
72 {
73 int *c;
74
75 if ((c = (int *)va_arg(ap, u_int32_t)) == NULL)
76 return API_EINVAL;
77
78 *c = getc();
79 return 0;
80 }
81
82 /*
83 * pseudo signature:
84 *
85 * int API_tstc(int *c)
86 */
87 static int API_tstc(va_list ap)
88 {
89 int *t;
90
91 if ((t = (int *)va_arg(ap, u_int32_t)) == NULL)
92 return API_EINVAL;
93
94 *t = tstc();
95 return 0;
96 }
97
98 /*
99 * pseudo signature:
100 *
101 * int API_putc(char *ch)
102 */
103 static int API_putc(va_list ap)
104 {
105 char *c;
106
107 if ((c = (char *)va_arg(ap, u_int32_t)) == NULL)
108 return API_EINVAL;
109
110 putc(*c);
111 return 0;
112 }
113
114 /*
115 * pseudo signature:
116 *
117 * int API_puts(char **s)
118 */
119 static int API_puts(va_list ap)
120 {
121 char *s;
122
123 if ((s = (char *)va_arg(ap, u_int32_t)) == NULL)
124 return API_EINVAL;
125
126 puts(s);
127 return 0;
128 }
129
130 /*
131 * pseudo signature:
132 *
133 * int API_reset(void)
134 */
135 static int API_reset(va_list ap)
136 {
137 do_reset(NULL, 0, 0, NULL);
138
139 /* NOT REACHED */
140 return 0;
141 }
142
143 /*
144 * pseudo signature:
145 *
146 * int API_get_sys_info(struct sys_info *si)
147 *
148 * fill out the sys_info struct containing selected parameters about the
149 * machine
150 */
151 static int API_get_sys_info(va_list ap)
152 {
153 struct sys_info *si;
154
155 si = (struct sys_info *)va_arg(ap, u_int32_t);
156 if (si == NULL)
157 return API_ENOMEM;
158
159 return (platform_sys_info(si)) ? 0 : API_ENODEV;
160 }
161
162 /*
163 * pseudo signature:
164 *
165 * int API_udelay(unsigned long *udelay)
166 */
167 static int API_udelay(va_list ap)
168 {
169 unsigned long *d;
170
171 if ((d = (unsigned long *)va_arg(ap, u_int32_t)) == NULL)
172 return API_EINVAL;
173
174 udelay(*d);
175 return 0;
176 }
177
178 /*
179 * pseudo signature:
180 *
181 * int API_get_timer(unsigned long *current, unsigned long *base)
182 */
183 static int API_get_timer(va_list ap)
184 {
185 unsigned long *base, *cur;
186
187 cur = (unsigned long *)va_arg(ap, u_int32_t);
188 if (cur == NULL)
189 return API_EINVAL;
190
191 base = (unsigned long *)va_arg(ap, u_int32_t);
192 if (base == NULL)
193 return API_EINVAL;
194
195 *cur = get_timer(*base);
196 return 0;
197 }
198
199
200 /*****************************************************************************
201 *
202 * pseudo signature:
203 *
204 * int API_dev_enum(struct device_info *)
205 *
206 *
207 * cookies uniqely identify the previously enumerated device instance and
208 * provide a hint for what to inspect in current enum iteration:
209 *
210 * - net: &eth_device struct address from list pointed to by eth_devices
211 *
212 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
213 * &scsi_dev_desc[n] and similar tables
214 *
215 ****************************************************************************/
216
217 static int API_dev_enum(va_list ap)
218 {
219 struct device_info *di;
220
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);
223 if (di == NULL)
224 return API_EINVAL;
225
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");
230
231 /* net device enumeration first */
232 if (dev_enum_net(di))
233 return 0;
234 }
235
236 /*
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
241 */
242 if (!dev_enum_storage(di))
243 /* make sure we mark there are no more devices */
244 di->cookie = NULL;
245
246 return 0;
247 }
248
249
250 static int API_dev_open(va_list ap)
251 {
252 struct device_info *di;
253 int err = 0;
254
255 /* arg is ptr to the device_info struct */
256 di = (struct device_info *)va_arg(ap, u_int32_t);
257 if (di == NULL)
258 return API_EINVAL;
259
260 /* Allow only one consumer of the device at a time */
261 if (di->state == DEV_STA_OPEN)
262 return API_EBUSY;
263
264 if (di->cookie == NULL)
265 return API_ENODEV;
266
267 if (di->type & DEV_TYP_STOR)
268 err = dev_open_stor(di->cookie);
269
270 else if (di->type & DEV_TYP_NET)
271 err = dev_open_net(di->cookie);
272 else
273 err = API_ENODEV;
274
275 if (!err)
276 di->state = DEV_STA_OPEN;
277
278 return err;
279 }
280
281
282 static int API_dev_close(va_list ap)
283 {
284 struct device_info *di;
285 int err = 0;
286
287 /* arg is ptr to the device_info struct */
288 di = (struct device_info *)va_arg(ap, u_int32_t);
289 if (di == NULL)
290 return API_EINVAL;
291
292 if (di->state == DEV_STA_CLOSED)
293 return 0;
294
295 if (di->cookie == NULL)
296 return API_ENODEV;
297
298 if (di->type & DEV_TYP_STOR)
299 err = dev_close_stor(di->cookie);
300
301 else if (di->type & DEV_TYP_NET)
302 err = dev_close_net(di->cookie);
303 else
304 /*
305 * In case of unknown device we cannot change its state, so
306 * only return error code
307 */
308 err = API_ENODEV;
309
310 if (!err)
311 di->state = DEV_STA_CLOSED;
312
313 return err;
314 }
315
316
317 /*
318 * Notice: this is for sending network packets only, as U-Boot does not
319 * support writing to storage at the moment (12.2007)
320 *
321 * pseudo signature:
322 *
323 * int API_dev_write(
324 * struct device_info *di,
325 * void *buf,
326 * int *len
327 * )
328 *
329 * buf: ptr to buffer from where to get the data to send
330 *
331 * len: length of packet to be sent (in bytes)
332 *
333 */
334 static int API_dev_write(va_list ap)
335 {
336 struct device_info *di;
337 void *buf;
338 int *len;
339 int err = 0;
340
341 /* 1. arg is ptr to the device_info struct */
342 di = (struct device_info *)va_arg(ap, u_int32_t);
343 if (di == NULL)
344 return API_EINVAL;
345
346 /* XXX should we check if device is open? i.e. the ->state ? */
347
348 if (di->cookie == NULL)
349 return API_ENODEV;
350
351 /* 2. arg is ptr to buffer from where to get data to write */
352 buf = (void *)va_arg(ap, u_int32_t);
353 if (buf == NULL)
354 return API_EINVAL;
355
356 /* 3. arg is length of buffer */
357 len = (int *)va_arg(ap, u_int32_t);
358 if (len == NULL)
359 return API_EINVAL;
360 if (*len <= 0)
361 return API_EINVAL;
362
363 if (di->type & DEV_TYP_STOR)
364 /*
365 * write to storage is currently not supported by U-Boot:
366 * no storage device implements block_write() method
367 */
368 return API_ENODEV;
369
370 else if (di->type & DEV_TYP_NET)
371 err = dev_write_net(di->cookie, buf, *len);
372 else
373 err = API_ENODEV;
374
375 return err;
376 }
377
378
379 /*
380 * pseudo signature:
381 *
382 * int API_dev_read(
383 * struct device_info *di,
384 * void *buf,
385 * size_t *len,
386 * unsigned long *start
387 * size_t *act_len
388 * )
389 *
390 * buf: ptr to buffer where to put the read data
391 *
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)
395 *
396 * start: ptr to start block (only used for storage devices, ignored for
397 * network)
398 *
399 * act_len: ptr to where to put the len actually read
400 */
401 static int API_dev_read(va_list ap)
402 {
403 struct device_info *di;
404 void *buf;
405 lbasize_t *len_stor, *act_len_stor;
406 lbastart_t *start;
407 int *len_net, *act_len_net;
408
409 /* 1. arg is ptr to the device_info struct */
410 di = (struct device_info *)va_arg(ap, u_int32_t);
411 if (di == NULL)
412 return API_EINVAL;
413
414 /* XXX should we check if device is open? i.e. the ->state ? */
415
416 if (di->cookie == NULL)
417 return API_ENODEV;
418
419 /* 2. arg is ptr to buffer from where to put the read data */
420 buf = (void *)va_arg(ap, u_int32_t);
421 if (buf == NULL)
422 return API_EINVAL;
423
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);
427 if (!len_stor)
428 return API_EINVAL;
429 if (*len_stor <= 0)
430 return API_EINVAL;
431
432 /* 4. arg - ptr to var with start block */
433 start = (lbastart_t *)va_arg(ap, u_int32_t);
434
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);
437 if (!act_len_stor)
438 return API_EINVAL;
439
440 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
441
442 } else if (di->type & DEV_TYP_NET) {
443
444 /* 3. arg points to the var with length of packet to read */
445 len_net = (int *)va_arg(ap, u_int32_t);
446 if (!len_net)
447 return API_EINVAL;
448 if (*len_net <= 0)
449 return API_EINVAL;
450
451 /* 4. - ptr to var where to put the len actually read */
452 act_len_net = (int *)va_arg(ap, u_int32_t);
453 if (!act_len_net)
454 return API_EINVAL;
455
456 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
457
458 } else
459 return API_ENODEV;
460
461 return 0;
462 }
463
464
465 /*
466 * pseudo signature:
467 *
468 * int API_env_get(const char *name, char **value)
469 *
470 * name: ptr to name of env var
471 */
472 static int API_env_get(va_list ap)
473 {
474 char *name, **value;
475
476 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
477 return API_EINVAL;
478 if ((value = (char **)va_arg(ap, u_int32_t)) == NULL)
479 return API_EINVAL;
480
481 *value = getenv(name);
482
483 return 0;
484 }
485
486 /*
487 * pseudo signature:
488 *
489 * int API_env_set(const char *name, const char *value)
490 *
491 * name: ptr to name of env var
492 *
493 * value: ptr to value to be set
494 */
495 static int API_env_set(va_list ap)
496 {
497 char *name, *value;
498
499 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
500 return API_EINVAL;
501 if ((value = (char *)va_arg(ap, u_int32_t)) == NULL)
502 return API_EINVAL;
503
504 setenv(name, value);
505
506 return 0;
507 }
508
509 /*
510 * pseudo signature:
511 *
512 * int API_env_enum(const char *last, char **next)
513 *
514 * last: ptr to name of env var found in last iteration
515 */
516 static int API_env_enum(va_list ap)
517 {
518 int i, n;
519 char *last, **next;
520
521 last = (char *)va_arg(ap, u_int32_t);
522
523 if ((next = (char **)va_arg(ap, u_int32_t)) == NULL)
524 return API_EINVAL;
525
526 if (last == NULL)
527 /* start over */
528 *next = ((char *)env_get_addr(0));
529 else {
530 *next = last;
531
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?? */
536 return 0;
537 }
538 }
539
540 if (envmatch((uchar *)last, i) < 0)
541 continue;
542
543 /* try to get next name */
544 i = n + 1;
545 if (env_get_char(i) == '\0') {
546 /* no more left */
547 *next = NULL;
548 return 0;
549 }
550
551 *next = ((char *)env_get_addr(i));
552 return 0;
553 }
554 }
555
556 return 0;
557 }
558
559 static cfp_t calls_table[API_MAXCALL] = { NULL, };
560
561 /*
562 * The main syscall entry point - this is not reentrant, only one call is
563 * serviced until finished.
564 *
565 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
566 *
567 * call: syscall number
568 *
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
572 *
573 * ... syscall arguments (variable number)
574 *
575 * returns: 0 if the call not found, 1 if serviced
576 */
577 int syscall(int call, int *retval, ...)
578 {
579 va_list ap;
580 int rv;
581
582 if (call < 0 || call >= calls_no) {
583 debugf("invalid call #%d\n", call);
584 return 0;
585 }
586
587 if (calls_table[call] == NULL) {
588 debugf("syscall #%d does not have a handler\n", call);
589 return 0;
590 }
591
592 va_start(ap, retval);
593 rv = calls_table[call](ap);
594 if (retval != NULL)
595 *retval = rv;
596
597 return 1;
598 }
599
600 void api_init(void)
601 {
602 struct api_signature *sig = NULL;
603
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;
623
624 debugf("API initialized with %d calls\n", calls_no);
625
626 dev_stor_init();
627
628 /*
629 * Produce the signature so the API consumers can find it
630 */
631 sig = malloc(sizeof(struct api_signature));
632 if (sig == NULL) {
633 printf("API: could not allocate memory for the signature!\n");
634 return;
635 }
636
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;
641 sig->checksum = 0;
642 sig->checksum = crc32(0, (unsigned char *)sig,
643 sizeof(struct api_signature));
644 debugf("syscall entry: 0x%08x\n", sig->syscall);
645 }
646
647 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
648 int flags)
649 {
650 int i;
651
652 if (!si->mr || !size || (flags == 0))
653 return;
654
655 /* find free slot */
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;
662 return;
663 }
664 }