storage.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* storage command: refresh the filesystem cache and report devices + mounts. */
  2. #include "blkdev.h"
  3. #include "io.h"
  4. #include "vfs.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. /* Does mount `mount_name` belong to block device `dev_name`?
  69. * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
  70. static int mount_belongs_to(const char *mount_name, const char *dev_name)
  71. {
  72. unsigned int i = 0;
  73. while (dev_name[i] && mount_name[i] == dev_name[i])
  74. i++;
  75. if (dev_name[i] == '\0' && mount_name[i] == '\0')
  76. return 1;
  77. if (dev_name[i] == '\0' && mount_name[i] == 'p')
  78. return 1;
  79. return 0;
  80. }
  81. void program_storage_main(void)
  82. {
  83. unsigned int i, j;
  84. /* Refresh the filesystem cache (re-probes all devices). */
  85. vfs_mount_all();
  86. for (i = 0; i < blk_dev_count; i++) {
  87. blk_dev_t *d = &blk_devs[i];
  88. int found = 0;
  89. if (d->kind == BLK_IDE && !d->present) {
  90. io_putstr(d->name);
  91. io_putstr(": not present\n");
  92. continue;
  93. }
  94. for (j = 0; j < vfs_mount_count; j++) {
  95. if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
  96. report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
  97. found = 1;
  98. }
  99. }
  100. if (!found) {
  101. io_putstr(d->name);
  102. io_putstr(": no valid FAT filesystem\n");
  103. }
  104. }
  105. }