demo.c 5.9 KB

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