storage.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* storage command: refresh the filesystem cache and report devices + mounts. */
  2. #include "ahci.h"
  3. #include "blkdev.h"
  4. #include "io.h"
  5. #include "vfs.h"
  6. static void put_dec(unsigned int n)
  7. {
  8. char buf[12];
  9. unsigned int i = sizeof(buf) - 1;
  10. buf[i] = '\0';
  11. if (n == 0) {
  12. io_putchar('0');
  13. return;
  14. }
  15. while (n && i > 0) {
  16. buf[--i] = (char)('0' + (n % 10));
  17. n /= 10;
  18. }
  19. io_putstr(buf + i);
  20. }
  21. /* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
  22. static void put_size_sectors(unsigned int sectors)
  23. {
  24. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  25. unsigned int gb = sectors >> 21;
  26. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  27. put_dec(gb);
  28. if (tenths) {
  29. io_putchar('.');
  30. put_dec(tenths);
  31. }
  32. io_putstr(" GB");
  33. } else if (sectors >= 2048u) { /* >= 1 MiB */
  34. unsigned int mb = sectors >> 11;
  35. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  36. put_dec(mb);
  37. if (tenths) {
  38. io_putchar('.');
  39. put_dec(tenths);
  40. }
  41. io_putstr(" MB");
  42. } else {
  43. put_dec(sectors / 2u);
  44. io_putstr(" KB");
  45. }
  46. }
  47. static const char *fat_name(fat_type_t t)
  48. {
  49. switch (t) {
  50. case FAT12: return "FAT12";
  51. case FAT16: return "FAT16";
  52. case FAT32: return "FAT32";
  53. default: return "FAT";
  54. }
  55. }
  56. static void report_fs(const char *name, const fat_fs_t *fs)
  57. {
  58. io_putstr(name);
  59. io_putstr(": ");
  60. io_putstr(fat_name(fs->type));
  61. io_putchar(' ');
  62. put_size_sectors(fs->total_sectors);
  63. if (fs->label[0]) {
  64. io_putstr(" \"");
  65. io_putstr(fs->label);
  66. io_putchar('"');
  67. }
  68. io_putchar('\n');
  69. }
  70. /* Does mount `mount_name` belong to block device `dev_name`?
  71. * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
  72. static int mount_belongs_to(const char *mount_name, const char *dev_name)
  73. {
  74. unsigned int i = 0;
  75. while (dev_name[i] && mount_name[i] == dev_name[i])
  76. i++;
  77. if (dev_name[i] == '\0' && mount_name[i] == '\0')
  78. return 1;
  79. if (dev_name[i] == '\0' && mount_name[i] == 'p')
  80. return 1;
  81. return 0;
  82. }
  83. void program_storage_main(void)
  84. {
  85. unsigned int i, j;
  86. /* Refresh the filesystem cache (re-probes all devices). */
  87. vfs_mount_all();
  88. for (i = 0; i < blk_dev_count; i++) {
  89. blk_dev_t *d = &blk_devs[i];
  90. int found = 0;
  91. if (d->kind != BLK_FLOPPY && !d->present) {
  92. io_putstr(d->name);
  93. io_putstr(": not present\n");
  94. continue;
  95. }
  96. for (j = 0; j < vfs_mount_count; j++)
  97. if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
  98. found = 1;
  99. break;
  100. }
  101. /* Real disks get a header line with model and size, floppies
  102. * keep the old single-line output. */
  103. if (d->kind != BLK_FLOPPY) {
  104. io_putstr(d->name);
  105. io_putstr(": ");
  106. if (d->model[0]) {
  107. io_putstr(d->model);
  108. io_putchar(' ');
  109. }
  110. put_size_sectors(d->total_sectors);
  111. if (!found)
  112. io_putstr(" no valid FAT filesystem");
  113. io_putchar('\n');
  114. } else if (!found) {
  115. io_putstr(d->name);
  116. io_putstr(": no valid FAT filesystem\n");
  117. }
  118. if (found)
  119. for (j = 0; j < vfs_mount_count; j++)
  120. if (mount_belongs_to(vfs_mounts[j].name, d->name))
  121. report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
  122. }
  123. if (!ahci_controller_present())
  124. io_putstr("sata: no AHCI controller\n");
  125. }