api.c 15 KB

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