format.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* format command: create a fresh FAT filesystem on a device. Usage: format <device>. */
  2. #include "blkdev.h"
  3. #include "fatformat.h"
  4. #include "io.h"
  5. #include "vga.h"
  6. #include "vfs.h"
  7. static void list_devices(void)
  8. {
  9. unsigned int i;
  10. io_putstr("devices:");
  11. for (i = 0; i < blk_dev_count; i++) {
  12. io_putchar(' ');
  13. io_putstr(blk_devs[i].name);
  14. }
  15. io_putchar('\n');
  16. }
  17. void program_format_main(const char *arg)
  18. {
  19. blk_dev_t *d;
  20. char name[16];
  21. unsigned int i = 0, j;
  22. int k;
  23. if (!arg) {
  24. io_putstr("format: usage format <device>\n");
  25. blk_scan();
  26. list_devices();
  27. return;
  28. }
  29. while (arg[i] == ' ')
  30. i++;
  31. j = 0;
  32. while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
  33. name[j++] = arg[i++];
  34. name[j] = '\0';
  35. if (name[0] == '\0') {
  36. io_putstr("format: usage format <device>\n");
  37. blk_scan();
  38. list_devices();
  39. return;
  40. }
  41. blk_scan();
  42. d = blk_find(name);
  43. if (!d) {
  44. io_putstr("format: unknown device '");
  45. io_putstr(name);
  46. io_putstr("'\n");
  47. list_devices();
  48. return;
  49. }
  50. if (d->kind != BLK_FLOPPY && !d->present) {
  51. io_putstr("format: ");
  52. io_putstr(d->name);
  53. io_putstr(": not present\n");
  54. return;
  55. }
  56. io_putstr("format ");
  57. io_putstr(d->name);
  58. io_putstr("? this destroys all data (y/n) ");
  59. vga_fb_flush();
  60. /* Keep the WM from draining the keyboard ring while we poll for the
  61. * confirmation (works for PS/2 and USB keyboards alike). */
  62. io_set_raw_keyboard(1);
  63. for (;;) {
  64. k = io_poll_key_with_hw();
  65. if (k == 'y' || k == 'Y')
  66. break;
  67. if (k == 'n' || k == 'N') {
  68. io_set_raw_keyboard(0);
  69. io_putstr("aborted\n");
  70. return;
  71. }
  72. }
  73. io_set_raw_keyboard(0);
  74. io_putchar('\n');
  75. io_putstr("formatting ");
  76. io_putstr(d->name);
  77. io_putstr("...\n");
  78. vga_fb_flush();
  79. if (fat_format(d) != 0) {
  80. io_putstr("format: failed\n");
  81. return;
  82. }
  83. /* Make the fresh filesystem immediately usable (save/ls/cat). */
  84. vfs_mount_all();
  85. io_putstr("done\n");
  86. }