storage.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_fs_t *fs)
  55. {
  56. io_putstr(name);
  57. io_putstr(": ");
  58. io_putstr(fat_name(fs->type));
  59. io_putchar(' ');
  60. put_size(fs->total_sectors * fs->bytes_per_sector);
  61. if (fs->label[0]) {
  62. io_putstr(" \"");
  63. io_putstr(fs->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. unsigned int i;
  83. blk_scan();
  84. for (i = 0; i < blk_dev_count; i++) {
  85. blk_dev_t *d = &blk_devs[i];
  86. fat_fs_t fs;
  87. if (d->kind == BLK_IDE && !d->present) {
  88. io_putstr(d->name);
  89. io_putstr(": not present\n");
  90. continue;
  91. }
  92. if (blk_read(d, 0, buf, 1) != 0) {
  93. io_putstr(d->name);
  94. io_putstr(": not present / unreadable\n");
  95. continue;
  96. }
  97. if (d->kind == BLK_FLOPPY) {
  98. if (fat_mount(d, 0, &fs) == 0)
  99. report_fs(d->name, &fs);
  100. else {
  101. io_putstr(d->name);
  102. io_putstr(": no valid FAT filesystem\n");
  103. }
  104. } else {
  105. mbr_part_t parts[4];
  106. int n = mbr_parse(buf, parts);
  107. int found = 0;
  108. int j;
  109. for (j = 0; j < n; j++) {
  110. char pname[16];
  111. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  112. parts[j].sectors == 0)
  113. continue;
  114. if (fat_mount(d, parts[j].start_lba, &fs) == 0) {
  115. part_name(pname, d->name, parts[j].index);
  116. report_fs(pname, &fs);
  117. found = 1;
  118. }
  119. }
  120. if (!found) {
  121. if (fat_mount(d, 0, &fs) == 0)
  122. report_fs(d->name, &fs);
  123. else {
  124. io_putstr(d->name);
  125. io_putstr(": no valid FAT filesystem\n");
  126. }
  127. }
  128. }
  129. }
  130. }