demo.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <common.h>
  26. #include <linux/types.h>
  27. #include <api_public.h>
  28. #include "glue.h"
  29. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  30. void test_dump_si(struct sys_info *);
  31. void test_dump_di(int);
  32. void test_dump_sig(struct api_signature *);
  33. char buf[2048];
  34. #define WAIT_SECS 5
  35. int main(int argc, char *argv[])
  36. {
  37. int rv = 0;
  38. int h, i, j;
  39. int devs_no;
  40. struct api_signature *sig = NULL;
  41. ulong start, now;
  42. struct device_info *di;
  43. if (!api_search_sig(&sig))
  44. return -1;
  45. syscall_ptr = sig->syscall;
  46. if (syscall_ptr == NULL)
  47. return -2;
  48. if (sig->version > API_SIG_VERSION)
  49. return -3;
  50. printf("API signature found @%x\n", (unsigned int)sig);
  51. test_dump_sig(sig);
  52. printf("\n*** Consumer API test ***\n");
  53. printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr,
  54. (unsigned int)&syscall_ptr);
  55. /* console activities */
  56. ub_putc('B');
  57. printf("*** Press any key to continue ***\n");
  58. printf("got char 0x%x\n", ub_getc());
  59. /* system info */
  60. test_dump_si(ub_get_sys_info());
  61. /* timing */
  62. printf("\n*** Timing - wait a couple of secs ***\n");
  63. start = ub_get_timer(0);
  64. printf("\ntime: start %lu\n\n", start);
  65. for (i = 0; i < WAIT_SECS; i++)
  66. for (j = 0; j < 1000; j++)
  67. ub_udelay(1000); /* wait 1 ms */
  68. /* this is the number of milliseconds that passed from ub_get_timer(0) */
  69. now = ub_get_timer(start);
  70. printf("\ntime: now %lu\n\n", now);
  71. /* enumerate devices */
  72. printf("\n*** Enumerate devices ***\n");
  73. devs_no = ub_dev_enum();
  74. printf("Number of devices found: %d\n", devs_no);
  75. if (devs_no == 0)
  76. return -1;
  77. printf("\n*** Show devices ***\n");
  78. for (i = 0; i < devs_no; i++) {
  79. test_dump_di(i);
  80. printf("\n");
  81. }
  82. printf("\n*** Operations on devices ***\n");
  83. /* test opening a device already opened */
  84. h = 0;
  85. if ((rv = ub_dev_open(h)) != 0) {
  86. errf("open device %d error %d\n", h, rv);
  87. return -1;
  88. }
  89. if ((rv = ub_dev_open(h)) != 0)
  90. errf("open device %d error %d\n", h, rv);
  91. ub_dev_close(h);
  92. /* test storage */
  93. printf("Trying storage devices...\n");
  94. for (i = 0; i < devs_no; i++) {
  95. di = ub_dev_get(i);
  96. if (di->type & DEV_TYP_STOR)
  97. break;
  98. }
  99. if (i == devs_no)
  100. printf("No storage devices available\n");
  101. else {
  102. if ((rv = ub_dev_open(i)) != 0)
  103. errf("open device %d error %d\n", i, rv);
  104. else if ((rv = ub_dev_read(i, &buf, 200, 20)) != 0)
  105. errf("could not read from device %d, error %d\n", i, rv);
  106. ub_dev_close(i);
  107. }
  108. /* test networking */
  109. printf("Trying network devices...\n");
  110. for (i = 0; i < devs_no; i++) {
  111. di = ub_dev_get(i);
  112. if (di->type == DEV_TYP_NET)
  113. break;
  114. }
  115. if (i == devs_no)
  116. printf("No network devices available\n");
  117. else {
  118. if ((rv = ub_dev_open(i)) != 0)
  119. errf("open device %d error %d\n", i, rv);
  120. else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
  121. errf("could not send to device %d, error %d\n", i, rv);
  122. ub_dev_close(i);
  123. }
  124. if (ub_dev_close(h) != 0)
  125. errf("could not close device %d\n", h);
  126. printf("\n*** Env vars ***\n");
  127. printf("ethact = %s\n", ub_env_get("ethact"));
  128. printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
  129. ub_env_set("fileaddr", "deadbeef");
  130. printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
  131. const char *env = NULL;
  132. while ((env = ub_env_enum(env)) != NULL)
  133. printf("%s = %s\n", env, ub_env_get(env));
  134. /* reset */
  135. ub_reset();
  136. printf("\nHmm, reset returned...?!\n");
  137. return rv;
  138. }
  139. void test_dump_sig(struct api_signature *sig)
  140. {
  141. printf("signature:\n");
  142. printf(" version\t= %d\n", sig->version);
  143. printf(" checksum\t= 0x%08x\n", sig->checksum);
  144. printf(" sc entry\t= 0x%08x\n", (unsigned int)sig->syscall);
  145. }
  146. void test_dump_si(struct sys_info *si)
  147. {
  148. int i;
  149. printf("sys info:\n");
  150. printf(" clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus);
  151. printf(" clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu);
  152. printf(" bar\t\t= 0x%08x\n", (unsigned int)si->bar);
  153. printf("---\n");
  154. for (i = 0; i < si->mr_no; i++) {
  155. if (si->mr[i].flags == 0)
  156. break;
  157. printf(" start\t= 0x%08lx\n", si->mr[i].start);
  158. printf(" size\t= 0x%08lx\n", si->mr[i].size);
  159. switch(si->mr[i].flags & 0x000F) {
  160. case MR_ATTR_FLASH:
  161. printf(" type FLASH\n");
  162. break;
  163. case MR_ATTR_DRAM:
  164. printf(" type DRAM\n");
  165. break;
  166. case MR_ATTR_SRAM:
  167. printf(" type SRAM\n");
  168. break;
  169. default:
  170. printf(" type UNKNOWN\n");
  171. }
  172. printf("---\n");
  173. }
  174. }
  175. static char * test_stor_typ(int type)
  176. {
  177. if (type & DT_STOR_IDE)
  178. return "IDE";
  179. if (type & DT_STOR_SCSI)
  180. return "SCSI";
  181. if (type & DT_STOR_USB)
  182. return "USB";
  183. if (type & DT_STOR_MMC);
  184. return "MMC";
  185. return "Unknown";
  186. }
  187. void test_dump_di(int handle)
  188. {
  189. int i;
  190. struct device_info *di = ub_dev_get(handle);
  191. printf("device info (%d):\n", handle);
  192. printf(" cookie\t= 0x%08x\n", (uint32_t)di->cookie);
  193. printf(" type\t\t= 0x%08x\n", di->type);
  194. if (di->type == DEV_TYP_NET) {
  195. printf(" hwaddr\t= ");
  196. for (i = 0; i < 6; i++)
  197. printf("%02x ", di->di_net.hwaddr[i]);
  198. printf("\n");
  199. } else if (di->type & DEV_TYP_STOR) {
  200. printf(" type\t\t= %s\n", test_stor_typ(di->type));
  201. printf(" blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size);
  202. printf(" blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count);
  203. }
  204. }