format.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "process.h"
  6. #include "vga.h"
  7. #include "vfs.h"
  8. static void list_devices(void)
  9. {
  10. unsigned int i;
  11. io_putstr("devices:");
  12. for (i = 0; i < blk_dev_count; i++) {
  13. io_putchar(' ');
  14. io_putstr(blk_devs[i].name);
  15. }
  16. io_putchar('\n');
  17. }
  18. void program_format_main(const char *arg)
  19. {
  20. blk_dev_t *d;
  21. char name[16];
  22. unsigned int i = 0, j;
  23. int k;
  24. if (format_busy && process_is_alive(format_owner_pid)) {
  25. io_putstr("format: another format is in progress\n");
  26. return;
  27. }
  28. format_busy = 0;
  29. if (!arg) {
  30. io_putstr("format: usage format <device>\n");
  31. list_devices();
  32. return;
  33. }
  34. while (arg[i] == ' ')
  35. i++;
  36. j = 0;
  37. while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
  38. name[j++] = arg[i++];
  39. name[j] = '\0';
  40. if (name[0] == '\0') {
  41. io_putstr("format: usage format <device>\n");
  42. list_devices();
  43. return;
  44. }
  45. d = blk_find(name);
  46. if (!d) {
  47. io_putstr("format: unknown device '");
  48. io_putstr(name);
  49. io_putstr("'\n");
  50. list_devices();
  51. return;
  52. }
  53. if (d->kind != BLK_FLOPPY && !d->present) {
  54. io_putstr("format: ");
  55. io_putstr(d->name);
  56. io_putstr(": not present\n");
  57. return;
  58. }
  59. io_putstr("format ");
  60. io_putstr(d->name);
  61. io_putstr("? this destroys all data (y/n) ");
  62. vga_fb_flush();
  63. /* Keep the WM from draining the keyboard ring while we poll for the
  64. * confirmation (works for PS/2 and USB keyboards alike). */
  65. io_set_raw_keyboard(1);
  66. for (;;) {
  67. k = io_poll_key_with_hw();
  68. if (k == 'y' || k == 'Y')
  69. break;
  70. if (k == 'n' || k == 'N') {
  71. io_set_raw_keyboard(0);
  72. io_putstr("aborted\n");
  73. return;
  74. }
  75. /* This kernel command holds the BKL. Sleep briefly so the
  76. * kworker can poll USB HID and the terminal can render the
  77. * confirmation prompt; otherwise a USB-only keyboard deadlocks
  78. * here. */
  79. process_sleep_ms(10);
  80. }
  81. io_set_raw_keyboard(0);
  82. io_putchar('\n');
  83. io_putstr("formatting ");
  84. io_putstr(d->name);
  85. io_putstr("...\n");
  86. vga_fb_flush();
  87. /* Other filesystem syscalls wait while the FAT is half-written. */
  88. format_owner_pid = current_proc()->pid;
  89. format_busy = 1;
  90. if (fat_format(d) != 0) {
  91. format_busy = 0;
  92. io_putstr("format: failed\n");
  93. return;
  94. }
  95. /* Remount only the formatted device; a full vfs_mount_all() would probe
  96. * every disk again and was a second large stall. */
  97. vfs_remount_device(d);
  98. format_busy = 0;
  99. io_putstr("done\n");
  100. }