api.c 14 KB

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