storage.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* storage command: probe floppy + IDE devices and report FAT filesystems. */
  2. #include "blkdev.h"
  3. #include "fatdetect.h"
  4. #include "io.h"
  5. static void put_dec(unsigned int n)
  6. {
  7. char buf[12];
  8. unsigned int i = sizeof(buf) - 1;
  9. buf[i] = '\0';
  10. if (n == 0) {
  11. io_putchar('0');
  12. return;
  13. }
  14. while (n && i > 0) {
  15. buf[--i] = (char)('0' + (n % 10));
  16. n /= 10;
  17. }
  18. io_putstr(buf + i);
  19. }
  20. static void put_size(unsigned int bytes)
  21. {
  22. if (bytes >= 1024u * 1024u * 1024u) {
  23. unsigned int gb = bytes / (1024u * 1024u * 1024u);
  24. unsigned int tenths = (bytes % (1024u * 1024u * 1024u)) * 10u / (1024u * 1024u * 1024u);
  25. put_dec(gb);
  26. if (tenths) {
  27. io_putchar('.');
  28. put_dec(tenths);
  29. }
  30. io_putstr(" GB");
  31. } else if (bytes >= 1024u * 1024u) {
  32. unsigned int mb = bytes / (1024u * 1024u);
  33. unsigned int tenths = (bytes % (1024u * 1024u)) * 10u / (1024u * 1024u);
  34. put_dec(mb);
  35. if (tenths) {
  36. io_putchar('.');
  37. put_dec(tenths);
  38. }
  39. io_putstr(" MB");
  40. } else {
  41. put_dec(bytes / 1024u);
  42. io_putstr(" KB");
  43. }
  44. }
  45. static const char *fat_name(fat_type_t t)
  46. {
  47. switch (t) {
  48. case FAT12: return "FAT12";
  49. case FAT16: return "FAT16";
  50. case FAT32: return "FAT32";
  51. default: return "FAT";
  52. }
  53. }
  54. static void report_fs(const char *name, const fat_info_t *info)
  55. {
  56. io_putstr(name);
  57. io_putstr(": ");
  58. io_putstr(fat_name(info->type));
  59. io_putchar(' ');
  60. put_size(info->total_sectors * info->bytes_per_sector);
  61. if (info->label[0]) {
  62. io_putstr(" \"");
  63. io_putstr(info->label);
  64. io_putchar('"');
  65. }
  66. io_putchar('\n');
  67. }
  68. static void part_name(char *out, const char *base, unsigned int idx)
  69. {
  70. unsigned int i = 0;
  71. while (base[i] && i < 14) {
  72. out[i] = base[i];
  73. i++;
  74. }
  75. out[i++] = 'p';
  76. out[i++] = (char)('0' + idx + 1);
  77. out[i] = '\0';
  78. }
  79. void program_storage_main(void)
  80. {
  81. static unsigned char buf[512];
  82. static unsigned char pbuf[512];
  83. unsigned int i;
  84. blk_scan();
  85. for (i = 0; i < blk_dev_count; i++) {
  86. blk_dev_t *d = &blk_devs[i];
  87. fat_info_t info;
  88. if (d->kind == BLK_IDE && !d->present) {
  89. io_putstr(d->name);
  90. io_putstr(": not present\n");
  91. continue;
  92. }
  93. if (blk_read(d, 0, buf, 1) != 0) {
  94. io_putstr(d->name);
  95. io_putstr(": not present / unreadable\n");
  96. continue;
  97. }
  98. if (d->kind == BLK_FLOPPY) {
  99. if (fat_detect(buf, &info) == 0)
  100. report_fs(d->name, &info);
  101. else {
  102. io_putstr(d->name);
  103. io_putstr(": no valid FAT filesystem\n");
  104. }
  105. } else {
  106. mbr_part_t parts[4];
  107. int n = mbr_parse(buf, parts);
  108. int found = 0;
  109. int j;
  110. for (j = 0; j < n; j++) {
  111. char pname[16];
  112. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  113. parts[j].sectors == 0)
  114. continue;
  115. if (blk_read(d, parts[j].start_lba, pbuf, 1) != 0)
  116. continue;
  117. if (fat_detect(pbuf, &info) == 0) {
  118. part_name(pname, d->name, parts[j].index);
  119. report_fs(pname, &info);
  120. found = 1;
  121. }
  122. }
  123. if (!found) {
  124. if (fat_detect(buf, &info) == 0)
  125. report_fs(d->name, &info);
  126. else {
  127. io_putstr(d->name);
  128. io_putstr(": no valid FAT filesystem\n");
  129. }
  130. }
  131. }
  132. }
  133. }