api.c 14 KB

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