FatDbg.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2011-2020 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "FatVolume.h"
  26. #include "FatFile.h"
  27. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  28. //------------------------------------------------------------------------------
  29. static void printHex(print_t* pr, uint8_t h) {
  30. if (h < 16) {
  31. pr->write('0');
  32. }
  33. pr->print(h, HEX);
  34. }
  35. //------------------------------------------------------------------------------
  36. static void printHex(print_t* pr, uint16_t val) {
  37. bool space = true;
  38. for (uint8_t i = 0; i < 4; i++) {
  39. uint8_t h = (val >> (12 - 4*i)) & 15;
  40. if (h || i == 3) {
  41. space = false;
  42. }
  43. if (space) {
  44. pr->write(' ');
  45. } else {
  46. pr->print(h, HEX);
  47. }
  48. }
  49. }
  50. //------------------------------------------------------------------------------
  51. static void printHex(print_t* pr, uint32_t val) {
  52. bool space = true;
  53. for (uint8_t i = 0; i < 8; i++) {
  54. uint8_t h = (val >> (28 - 4*i)) & 15;
  55. if (h || i == 7) {
  56. space = false;
  57. }
  58. if (space) {
  59. pr->write(' ');
  60. } else {
  61. pr->print(h, HEX);
  62. }
  63. }
  64. }
  65. //------------------------------------------------------------------------------
  66. static void printDir(print_t* pr, DirFat_t* dir) {
  67. if (!dir->name[0] || dir->name[0] == FAT_NAME_DELETED) {
  68. pr->println(F("Not Used"));
  69. } else if (isFileOrSubdir(dir)) {
  70. pr->print(F("name: "));
  71. pr->write(dir->name, 11);
  72. pr->println();
  73. uint32_t fc = ((uint32_t)getLe16(dir->firstClusterHigh) << 16)
  74. | getLe16(dir->firstClusterLow);
  75. pr->print(F("firstCluster: "));
  76. pr->println(fc, HEX);
  77. pr->print(F("fileSize: "));
  78. pr->println(getLe32(dir->fileSize));
  79. } else if (isLongName(dir)) {
  80. pr->println(F("LFN"));
  81. } else {
  82. pr->println(F("Other"));
  83. }
  84. }
  85. //------------------------------------------------------------------------------
  86. void FatPartition::dmpDirSector(print_t* pr, uint32_t sector) {
  87. DirFat_t dir[16];
  88. if (!readSector(sector, reinterpret_cast<uint8_t*>(dir))) {
  89. pr->println(F("dmpDir failed"));
  90. return;
  91. }
  92. for (uint8_t i = 0; i < 16; i++) {
  93. printDir(pr, dir + i);
  94. }
  95. }
  96. //------------------------------------------------------------------------------
  97. void FatPartition::dmpRootDir(print_t* pr) {
  98. uint32_t sector;
  99. if (fatType() == 16) {
  100. sector = rootDirStart();
  101. } else if (fatType() == 32) {
  102. sector = clusterStartSector(rootDirStart());
  103. } else {
  104. pr->println(F("dmpRootDir failed"));
  105. return;
  106. }
  107. dmpDirSector(pr, sector);
  108. }
  109. //------------------------------------------------------------------------------
  110. void FatPartition::dmpSector(print_t* pr, uint32_t sector, uint8_t bits) {
  111. uint8_t data[512];
  112. if (!readSector(sector, data)) {
  113. pr->println(F("dmpSector failed"));
  114. return;
  115. }
  116. for (uint16_t i = 0; i < 512;) {
  117. if (i%32 == 0) {
  118. if (i) {
  119. pr->println();
  120. }
  121. printHex(pr, i);
  122. }
  123. pr->write(' ');
  124. if (bits == 32) {
  125. printHex(pr, *reinterpret_cast<uint32_t*>(data + i));
  126. i += 4;
  127. } else if (bits == 16) {
  128. printHex(pr, *reinterpret_cast<uint16_t*>(data + i));
  129. i += 2;
  130. } else {
  131. printHex(pr, data[i++]);
  132. }
  133. }
  134. pr->println();
  135. }
  136. //------------------------------------------------------------------------------
  137. void FatPartition::dmpFat(print_t* pr, uint32_t start, uint32_t count) {
  138. uint16_t nf = fatType() == 16 ? 256 : fatType() == 32 ? 128 : 0;
  139. if (nf == 0) {
  140. pr->println(F("Invalid fatType"));
  141. return;
  142. }
  143. pr->println(F("FAT:"));
  144. uint32_t sector = m_fatStartSector + start;
  145. uint32_t cluster = nf*start;
  146. for (uint32_t i = 0; i < count; i++) {
  147. cache_t* pc = cacheFetchFat(sector + i, FatCache::CACHE_FOR_READ);
  148. if (!pc) {
  149. pr->println(F("cache read failed"));
  150. return;
  151. }
  152. for (size_t k = 0; k < nf; k++) {
  153. if (0 == cluster%8) {
  154. if (k) {
  155. pr->println();
  156. }
  157. printHex(pr, cluster);
  158. }
  159. cluster++;
  160. pr->write(' ');
  161. uint32_t v = fatType() == 32 ? pc->fat32[k] : pc->fat16[k];
  162. printHex(pr, v);
  163. }
  164. pr->println();
  165. }
  166. }
  167. #endif // DOXYGEN_SHOULD_SKIP_THIS