1
0

io.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. ******************************************************************************
  6. * @file io.c
  7. * @author
  8. * @version V0.1
  9. * @date 10-Jun-2019
  10. * @brief read and write memory
  11. *
  12. ******************************************************************************
  13. */
  14. #include <rtthread.h>
  15. #ifdef RT_DEBUG_USING_IO
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include "hal_base.h"
  21. static void usage(void)
  22. {
  23. rt_kprintf("Raw memory i/o utility - $Revision: 1.5 $\n\n");
  24. rt_kprintf("io -v -1|2|4 -r|w [-l <len>] <addr> [<value>]\n\n");
  25. rt_kprintf(" -v Verbose, asks for confirmation\n");
  26. rt_kprintf(" -1|2|4 Sets memory access size in bytes (default byte)\n");
  27. rt_kprintf(" -l <len> Length in bytes of area to access (defaults to\n");
  28. rt_kprintf(" one access, or whole file length)\n");
  29. rt_kprintf(" -r|w Read from or Write to memory (default read)\n");
  30. rt_kprintf(" <addr> The memory address to access\n");
  31. rt_kprintf(" <val> The value to write (implies -w)\n\n");
  32. rt_kprintf("Examples:\n");
  33. rt_kprintf(" io 0x1000 Reads one byte from 0x1000\n");
  34. rt_kprintf(" io 0x1000 0x12 Writes 0x12 to location 0x1000\n");
  35. rt_kprintf(" io -2 -l 8 0x1000 Reads 8 words from 0x1000\n\n");
  36. rt_kprintf("Note access size (-1|2|4) does not apply to file based accesses.\n\n");
  37. }
  38. static void
  39. read_memory(unsigned long phys_addr, uint8_t *addr, int len, int iosize)
  40. {
  41. int i;
  42. while (len)
  43. {
  44. rt_kprintf("%08lx: ", phys_addr);
  45. i = 0;
  46. while (i < 16 && len)
  47. {
  48. switch (iosize)
  49. {
  50. case 1:
  51. rt_kprintf(" %02x", *(uint8_t *)addr);
  52. break;
  53. case 2:
  54. rt_kprintf(" %04x", *(uint16_t *)addr);
  55. break;
  56. case 4:
  57. rt_kprintf(" %08lx", *(uint32_t *)addr);
  58. break;
  59. }
  60. i += iosize;
  61. addr += iosize;
  62. len -= iosize;
  63. }
  64. phys_addr += 16;
  65. rt_kprintf("\n");
  66. }
  67. }
  68. static void
  69. write_memory(uint8_t *addr, int len, int iosize, unsigned long value)
  70. {
  71. switch (iosize)
  72. {
  73. case 1:
  74. while (len)
  75. {
  76. *(uint8_t *)addr = value;
  77. len -= iosize;
  78. addr += iosize;
  79. }
  80. break;
  81. case 2:
  82. while (len)
  83. {
  84. *(uint16_t *)addr = value;
  85. len -= iosize;
  86. addr += iosize;
  87. }
  88. break;
  89. case 4:
  90. while (len)
  91. {
  92. *(uint32_t *)addr = value;
  93. len -= iosize;
  94. addr += iosize;
  95. }
  96. break;
  97. }
  98. }
  99. int io_mem(int argc, char **argv)
  100. {
  101. int req_len = 0, opt;
  102. uint8_t *real_io;
  103. unsigned long req_addr, req_value = 0;
  104. char *endptr;
  105. int memread = 1;
  106. int iosize = 1;
  107. int verbose = 0;
  108. opterr = 0;
  109. optind = 0;
  110. if (argc == 1)
  111. {
  112. usage();
  113. return 0;
  114. }
  115. while ((opt = getopt(argc, argv, "hv124rwl:f:")) > 0)
  116. {
  117. switch (opt)
  118. {
  119. case 'h':
  120. usage();
  121. case 'v':
  122. verbose = 1;
  123. break;
  124. case '1':
  125. case '2':
  126. case '4':
  127. iosize = opt - '0';
  128. break;
  129. case 'r':
  130. memread = 1;
  131. break;
  132. case 'w':
  133. memread = 0;
  134. break;
  135. case 'l':
  136. req_len = strtoul(optarg, &endptr, 0);
  137. if (*endptr)
  138. {
  139. rt_kprintf("Bad <size> value '%s'\n", optarg);
  140. return 0;
  141. }
  142. break;
  143. default:
  144. rt_kprintf("Unknown option: %c\n", opt);
  145. usage();
  146. }
  147. }
  148. if (optind == argc)
  149. {
  150. rt_kprintf("No address given\n");
  151. return 0;
  152. }
  153. req_addr = strtoul(argv[optind], &endptr, 0);
  154. if (*endptr)
  155. {
  156. rt_kprintf("Bad <addr> value '%s'\n", argv[optind]);
  157. return 0;
  158. }
  159. optind++;
  160. if (optind < argc)
  161. memread = 0;
  162. if (!memread && optind == argc)
  163. {
  164. rt_kprintf("No value given for WRITE\n");
  165. return 0;
  166. }
  167. if (!memread)
  168. {
  169. req_value = strtoul(argv[optind], &endptr, 0);
  170. if (*endptr)
  171. {
  172. rt_kprintf("Bad <value> value '%s'\n", argv[optind]);
  173. return 0;
  174. }
  175. if ((iosize == 1 && (req_value & 0xffffff00)) ||
  176. (iosize == 2 && (req_value & 0xffff0000)))
  177. {
  178. rt_kprintf("<value> too large\n");
  179. return 0;
  180. }
  181. optind++;
  182. }
  183. if (optind < argc)
  184. {
  185. rt_kprintf("Too many arguments '%s'...\n", argv[optind]);
  186. return 0;
  187. }
  188. if (!req_len)
  189. req_len = iosize;
  190. if ((iosize == 2 && (req_addr & 1)) ||
  191. (iosize == 4 && (req_addr & 3)))
  192. {
  193. rt_kprintf("Badly aligned <addr> for access size\n");
  194. return 0;
  195. }
  196. if ((iosize == 2 && (req_len & 1)) ||
  197. (iosize == 4 && (req_len & 3)))
  198. {
  199. rt_kprintf("Badly aligned <size> for access size\n");
  200. return 0;
  201. }
  202. if (!verbose)
  203. /* Nothing */;
  204. else if (memread)
  205. rt_kprintf("Request to memread 0x%x bytes from address 0x%08lx\n"
  206. "\tusing %d byte accesses\n",
  207. req_len, req_addr, iosize);
  208. else
  209. rt_kprintf("Request to write 0x%x bytes to address 0x%08lx\n"
  210. "\tusing %d byte accesses of value 0x%0*lx\n",
  211. req_len, req_addr, iosize, iosize * 2, req_value);
  212. real_io = (uint8_t *)req_addr;
  213. if (memread)
  214. read_memory(req_addr, real_io, req_len, iosize);
  215. else
  216. write_memory(real_io, req_len, iosize, req_value);
  217. return 0;
  218. }
  219. #ifdef RT_USING_FINSH
  220. #include <finsh.h>
  221. MSH_CMD_EXPORT_ALIAS(io_mem, io, memory read or write cmd);
  222. #endif
  223. #endif /* RT_DEBUG_USING_IO */