demo.c 6.6 KB

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