debug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <lib/console.h>
  27. #include <lib/bio.h>
  28. #include <lib/partition.h>
  29. #include <platform.h>
  30. #if defined(WITH_LIB_CONSOLE)
  31. #if DEBUGLEVEL > 0
  32. static int cmd_bio(int argc, const cmd_args *argv);
  33. STATIC_COMMAND_START
  34. STATIC_COMMAND("bio", "block io debug commands", &cmd_bio)
  35. STATIC_COMMAND_END(bio);
  36. static int cmd_bio(int argc, const cmd_args *argv)
  37. {
  38. int rc = 0;
  39. if (argc < 2) {
  40. printf("not enough arguments:\n");
  41. usage:
  42. printf("%s list\n", argv[0].str);
  43. printf("%s read <device> <address> <offset> <len>\n", argv[0].str);
  44. printf("%s write <device> <address> <offset> <len>\n", argv[0].str);
  45. printf("%s erase <device> <offset> <len>\n", argv[0].str);
  46. printf("%s ioctl <device> <request> <arg>\n", argv[0].str);
  47. printf("%s remove <device>\n", argv[0].str);
  48. #if WITH_LIB_PARTITION
  49. printf("%s partscan <device> [offset]\n", argv[0].str);
  50. #endif
  51. return -1;
  52. }
  53. if (!strcmp(argv[1].str, "list")) {
  54. bio_dump_devices();
  55. } else if (!strcmp(argv[1].str, "read")) {
  56. if (argc < 6) {
  57. printf("not enough arguments:\n");
  58. goto usage;
  59. }
  60. addr_t address = argv[3].u;
  61. off_t offset = argv[4].u; // XXX use long
  62. size_t len = argv[5].u;
  63. bdev_t *dev = bio_open(argv[2].str);
  64. if (!dev) {
  65. printf("error opening block device\n");
  66. return -1;
  67. }
  68. time_t t = current_time();
  69. ssize_t err = bio_read(dev, (void *)address, offset, len);
  70. t = current_time() - t;
  71. dprintf(INFO, "bio_read returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t));
  72. bio_close(dev);
  73. rc = err;
  74. } else if (!strcmp(argv[1].str, "write")) {
  75. if (argc < 6) {
  76. printf("not enough arguments:\n");
  77. goto usage;
  78. }
  79. addr_t address = argv[3].u;
  80. off_t offset = argv[4].u; // XXX use long
  81. size_t len = argv[5].u;
  82. bdev_t *dev = bio_open(argv[2].str);
  83. if (!dev) {
  84. printf("error opening block device\n");
  85. return -1;
  86. }
  87. time_t t = current_time();
  88. ssize_t err = bio_write(dev, (void *)address, offset, len);
  89. t = current_time() - t;
  90. dprintf(INFO, "bio_write returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t));
  91. bio_close(dev);
  92. rc = err;
  93. } else if (!strcmp(argv[1].str, "erase")) {
  94. if (argc < 5) {
  95. printf("not enough arguments:\n");
  96. goto usage;
  97. }
  98. off_t offset = argv[3].u; // XXX use long
  99. size_t len = argv[4].u;
  100. bdev_t *dev = bio_open(argv[2].str);
  101. if (!dev) {
  102. printf("error opening block device\n");
  103. return -1;
  104. }
  105. time_t t = current_time();
  106. ssize_t err = bio_erase(dev, offset, len);
  107. t = current_time() - t;
  108. dprintf(INFO, "bio_erase returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t));
  109. bio_close(dev);
  110. rc = err;
  111. } else if (!strcmp(argv[1].str, "ioctl")) {
  112. if (argc < 4) {
  113. printf("not enough arguments:\n");
  114. goto usage;
  115. }
  116. int request = argv[3].u;
  117. int arg = (argc == 5) ? argv[4].u : 0;
  118. bdev_t *dev = bio_open(argv[2].str);
  119. if (!dev) {
  120. printf("error opening block device\n");
  121. return -1;
  122. }
  123. int err = bio_ioctl(dev, request, (void *)arg);
  124. dprintf(INFO, "bio_ioctl returns %d\n", err);
  125. bio_close(dev);
  126. rc = err;
  127. } else if (!strcmp(argv[1].str, "remove")) {
  128. if (argc < 3) {
  129. printf("not enough arguments:\n");
  130. goto usage;
  131. }
  132. bdev_t *dev = bio_open(argv[2].str);
  133. if (!dev) {
  134. printf("error opening block device\n");
  135. return -1;
  136. }
  137. bio_unregister_device(dev);
  138. bio_close(dev);
  139. #if WITH_LIB_PARTITION
  140. } else if (!strcmp(argv[1].str, "partscan")) {
  141. if (argc < 3) {
  142. printf("not enough arguments:\n");
  143. goto usage;
  144. }
  145. off_t offset = 0;
  146. if (argc > 3)
  147. offset = argv[3].u;
  148. rc = partition_publish(argv[2].str, offset);
  149. dprintf(INFO, "partition_publish returns %d\n", rc);
  150. #endif
  151. } else {
  152. printf("unrecognized subcommand\n");
  153. goto usage;
  154. }
  155. return rc;
  156. }
  157. #endif
  158. #endif