| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /* storage command: refresh the filesystem cache and report devices + mounts. */
- #include "ahci.h"
- #include "blkdev.h"
- #include "io.h"
- #include "vfs.h"
- static void put_dec(unsigned int n)
- {
- char buf[12];
- unsigned int i = sizeof(buf) - 1;
- buf[i] = '\0';
- if (n == 0) {
- io_putchar('0');
- return;
- }
- while (n && i > 0) {
- buf[--i] = (char)('0' + (n % 10));
- n /= 10;
- }
- io_putstr(buf + i);
- }
- /* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
- static void put_size_sectors(unsigned int sectors)
- {
- if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
- unsigned int gb = sectors >> 21;
- unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
- put_dec(gb);
- if (tenths) {
- io_putchar('.');
- put_dec(tenths);
- }
- io_putstr(" GB");
- } else if (sectors >= 2048u) { /* >= 1 MiB */
- unsigned int mb = sectors >> 11;
- unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
- put_dec(mb);
- if (tenths) {
- io_putchar('.');
- put_dec(tenths);
- }
- io_putstr(" MB");
- } else {
- put_dec(sectors / 2u);
- io_putstr(" KB");
- }
- }
- static const char *fat_name(fat_type_t t)
- {
- switch (t) {
- case FAT12: return "FAT12";
- case FAT16: return "FAT16";
- case FAT32: return "FAT32";
- default: return "FAT";
- }
- }
- static void report_fs(const char *name, const fat_fs_t *fs)
- {
- io_putstr(name);
- io_putstr(": ");
- io_putstr(fat_name(fs->type));
- io_putchar(' ');
- put_size_sectors(fs->total_sectors);
- if (fs->label[0]) {
- io_putstr(" \"");
- io_putstr(fs->label);
- io_putchar('"');
- }
- io_putchar('\n');
- }
- /* Does mount `mount_name` belong to block device `dev_name`?
- * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
- static int mount_belongs_to(const char *mount_name, const char *dev_name)
- {
- unsigned int i = 0;
- while (dev_name[i] && mount_name[i] == dev_name[i])
- i++;
- if (dev_name[i] == '\0' && mount_name[i] == '\0')
- return 1;
- if (dev_name[i] == '\0' && mount_name[i] == 'p')
- return 1;
- return 0;
- }
- void program_storage_main(void)
- {
- unsigned int i, j;
- /* Refresh the filesystem cache (re-probes all devices). */
- vfs_mount_all();
- for (i = 0; i < blk_dev_count; i++) {
- blk_dev_t *d = &blk_devs[i];
- int found = 0;
- if (d->kind != BLK_FLOPPY && !d->present) {
- io_putstr(d->name);
- io_putstr(": not present\n");
- continue;
- }
- for (j = 0; j < vfs_mount_count; j++)
- if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
- found = 1;
- break;
- }
- /* Real disks get a header line with model and size, floppies
- * keep the old single-line output. */
- if (d->kind != BLK_FLOPPY) {
- io_putstr(d->name);
- io_putstr(": ");
- if (d->model[0]) {
- io_putstr(d->model);
- io_putchar(' ');
- }
- put_size_sectors(d->total_sectors);
- if (!found)
- io_putstr(" no valid FAT filesystem");
- io_putchar('\n');
- } else if (!found) {
- io_putstr(d->name);
- io_putstr(": no valid FAT filesystem\n");
- }
- if (found)
- for (j = 0; j < vfs_mount_count; j++)
- if (mount_belongs_to(vfs_mounts[j].name, d->name))
- report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
- }
- if (!ahci_controller_present())
- io_putstr("sata: no AHCI controller\n");
- }
|