ucmd_storage.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* storage: ring-3 command. Refresh the filesystem cache and report devices
  2. * and mounts (the kernel service re-probes the disks, like the old command). */
  3. #include "ucmd_lib.h"
  4. #define MAX_MOUNTS 24 /* VFS_MAX_MOUNTS */
  5. static struct sys_mount mounts[MAX_MOUNTS];
  6. /* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
  7. static void put_size_sectors(unsigned int sectors)
  8. {
  9. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  10. unsigned int gb = sectors >> 21;
  11. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  12. u_putdec(gb);
  13. if (tenths) {
  14. u_putc('.');
  15. u_putdec(tenths);
  16. }
  17. u_puts(" GB");
  18. } else if (sectors >= 2048u) { /* >= 1 MiB */
  19. unsigned int mb = sectors >> 11;
  20. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  21. u_putdec(mb);
  22. if (tenths) {
  23. u_putc('.');
  24. u_putdec(tenths);
  25. }
  26. u_puts(" MB");
  27. } else {
  28. u_putdec(sectors / 2u);
  29. u_puts(" KB");
  30. }
  31. }
  32. static void report_fs(const struct sys_mount *m)
  33. {
  34. u_puts(m->name);
  35. u_puts(": ");
  36. u_puts(m->type);
  37. u_putc(' ');
  38. put_size_sectors(m->total_sectors);
  39. if (m->label[0]) {
  40. u_puts(" \"");
  41. u_puts(m->label);
  42. u_putc('"');
  43. }
  44. u_putc('\n');
  45. }
  46. /* Does mount `mount_name` belong to block device `dev_name`?
  47. * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
  48. static int mount_belongs_to(const char *mount_name, const char *dev_name)
  49. {
  50. unsigned int i = 0;
  51. while (dev_name[i] && mount_name[i] == dev_name[i])
  52. i++;
  53. if (dev_name[i] == '\0' && mount_name[i] == '\0')
  54. return 1;
  55. if (dev_name[i] == '\0' && mount_name[i] == 'p')
  56. return 1;
  57. return 0;
  58. }
  59. void _start(const char *arg)
  60. {
  61. struct sys_storage_info si;
  62. unsigned int nmounts = 0, i, j;
  63. (void)arg;
  64. /* Refresh the filesystem cache (re-probes all devices). */
  65. u_syscall1(SYS_STORAGE_INFO, (int)&si);
  66. for (i = 0; i < MAX_MOUNTS; i++) {
  67. if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&mounts[nmounts]) != 1)
  68. break;
  69. nmounts++;
  70. }
  71. for (i = 0; i < si.blkdev_count; i++) {
  72. struct sys_blkdev d;
  73. int found = 0;
  74. if (u_syscall2(SYS_BLKDEV, (int)i, (int)&d) != 1)
  75. break;
  76. if (d.kind != 0 /* BLK_FLOPPY */ && !d.present) {
  77. u_puts(d.name);
  78. u_puts(": not present\n");
  79. continue;
  80. }
  81. for (j = 0; j < nmounts; j++)
  82. if (mount_belongs_to(mounts[j].name, d.name)) {
  83. found = 1;
  84. break;
  85. }
  86. /* Real disks get a header line with model and size, floppies
  87. * keep the old single-line output. */
  88. if (d.kind != 0) {
  89. u_puts(d.name);
  90. u_puts(": ");
  91. if (d.model[0]) {
  92. u_puts(d.model);
  93. u_putc(' ');
  94. }
  95. put_size_sectors(d.total_sectors);
  96. if (!found)
  97. u_puts(" no valid FAT filesystem");
  98. u_putc('\n');
  99. } else if (!found) {
  100. u_puts(d.name);
  101. u_puts(": no valid FAT filesystem\n");
  102. }
  103. if (found)
  104. for (j = 0; j < nmounts; j++)
  105. if (mount_belongs_to(mounts[j].name, d.name))
  106. report_fs(&mounts[j]);
  107. }
  108. if (!si.ahci_present)
  109. u_puts("sata: no AHCI controller\n");
  110. u_exit(0);
  111. }