format.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* format command: create a fresh FAT filesystem on a device.
  2. * Usage: format <device> [size] (size: 100mb, 1gb, ...; binary units) */
  3. #include "blkdev.h"
  4. #include "fatformat.h"
  5. #include "io.h"
  6. #include "process.h"
  7. #include "sched.h"
  8. #include "vga.h"
  9. #include "vfs.h"
  10. static void list_devices(void)
  11. {
  12. unsigned int i;
  13. io_putstr("devices:");
  14. for (i = 0; i < blk_dev_count; i++) {
  15. io_putchar(' ');
  16. io_putstr(blk_devs[i].name);
  17. }
  18. io_putchar('\n');
  19. }
  20. /* Parse a size such as 100mb or 1gb into 512-byte sectors. Units are
  21. * binary (1mb = 1024 * 1024 bytes); a bare number means MiB. Returns 0,
  22. * -1 for bad syntax, -2 for too large, -3 for zero. */
  23. static int parse_size(const char *s, unsigned int *out)
  24. {
  25. unsigned long long v = 0, sectors;
  26. unsigned int i = 0, shift = 2; /* MiB */
  27. int c;
  28. while (s[i] == ' ')
  29. i++;
  30. if (s[i] < '0' || s[i] > '9')
  31. return -1;
  32. while (s[i] >= '0' && s[i] <= '9') {
  33. if (v > 429496729ULL)
  34. return -2;
  35. v = v * 10 + (unsigned int)(s[i] - '0');
  36. i++;
  37. }
  38. if (v > 0xFFFFFFFFULL)
  39. return -2;
  40. while (s[i] == ' ')
  41. i++;
  42. if (s[i]) {
  43. c = s[i];
  44. if (c >= 'A' && c <= 'Z')
  45. c += 'a' - 'A';
  46. if (c == 'k')
  47. shift = 1;
  48. else if (c == 'm')
  49. shift = 2;
  50. else if (c == 'g')
  51. shift = 3;
  52. else if (c == 't')
  53. shift = 4;
  54. else
  55. return -1;
  56. i++;
  57. c = s[i];
  58. if (c >= 'A' && c <= 'Z')
  59. c += 'a' - 'A';
  60. if (c == 'b')
  61. i++;
  62. }
  63. while (s[i] == ' ')
  64. i++;
  65. if (s[i])
  66. return -1;
  67. if (v == 0)
  68. return -3;
  69. sectors = v << (shift * 10 - 9);
  70. if (sectors > 0xFFFFFFFFULL)
  71. return -2;
  72. *out = (unsigned int)sectors;
  73. return 0;
  74. }
  75. /* Show a sector limit in the friendliest exact unit. */
  76. static void print_size(unsigned int sectors)
  77. {
  78. if (sectors == 0) {
  79. io_putstr("0 sectors");
  80. } else if (sectors % 0x80000000u == 0) { /* 1 TiB */
  81. format_putdec(sectors >> 31);
  82. io_putstr(" TiB");
  83. } else if (sectors % 0x200000u == 0) { /* 1 GiB */
  84. format_putdec(sectors >> 21);
  85. io_putstr(" GiB");
  86. } else if (sectors % 2048 == 0) {
  87. format_putdec(sectors / 2048);
  88. io_putstr(" MiB");
  89. } else if (sectors % 2 == 0) {
  90. format_putdec(sectors / 2);
  91. io_putstr(" KiB");
  92. } else {
  93. format_putdec(sectors);
  94. io_putstr(" sectors");
  95. }
  96. }
  97. void program_format_main(const char *arg)
  98. {
  99. blk_dev_t *d;
  100. char name[16];
  101. const char *p;
  102. unsigned int i = 0, j, limit = 0;
  103. int k, r;
  104. if (format_busy && process_is_alive(format_owner_pid)) {
  105. io_putstr("format: another format is in progress\n");
  106. return;
  107. }
  108. format_busy = 0;
  109. if (!arg) {
  110. io_putstr("format: usage format <device> [size]\n");
  111. list_devices();
  112. return;
  113. }
  114. while (arg[i] == ' ')
  115. i++;
  116. j = 0;
  117. while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
  118. name[j++] = arg[i++];
  119. name[j] = '\0';
  120. if (name[0] == '\0') {
  121. io_putstr("format: usage format <device> [size]\n");
  122. list_devices();
  123. return;
  124. }
  125. p = arg + i;
  126. while (*p == ' ')
  127. p++;
  128. if (*p) {
  129. r = parse_size(p, &limit);
  130. if (r == -1) {
  131. io_putstr("format: bad size '");
  132. io_putstr(p);
  133. io_putstr("' (use e.g. 100mb or 1gb)\n");
  134. return;
  135. }
  136. if (r == -2) {
  137. io_putstr("format: size is too large\n");
  138. return;
  139. }
  140. if (r == -3) {
  141. io_putstr("format: size must be greater than zero\n");
  142. return;
  143. }
  144. }
  145. d = blk_find(name);
  146. if (!d) {
  147. io_putstr("format: unknown device '");
  148. io_putstr(name);
  149. io_putstr("'\n");
  150. list_devices();
  151. return;
  152. }
  153. if (d->kind != BLK_FLOPPY && !d->present) {
  154. io_putstr("format: ");
  155. io_putstr(d->name);
  156. io_putstr(": not present\n");
  157. return;
  158. }
  159. if (d->kind == BLK_FLOPPY && limit) {
  160. io_putstr("format: a floppy is always the full 1440 KiB\n");
  161. return;
  162. }
  163. /* Reject a bad limit before the destructive confirmation prompt. */
  164. if (d->kind != BLK_FLOPPY && limit) {
  165. unsigned int avail = (d->total_sectors > 2048)
  166. ? d->total_sectors - 2048 : 0;
  167. if (limit > avail) {
  168. io_putstr("format: ");
  169. print_size(limit);
  170. io_putstr(" does not fit on ");
  171. io_putstr(d->name);
  172. io_putstr(" (");
  173. print_size(avail);
  174. io_putstr(" available)\n");
  175. return;
  176. }
  177. }
  178. io_putstr("format ");
  179. io_putstr(d->name);
  180. if (limit) {
  181. io_putstr(" (");
  182. print_size(limit);
  183. io_putstr(")");
  184. } else {
  185. io_putstr(" (whole disk)");
  186. }
  187. io_putstr("? this destroys all data (y/n) ");
  188. vga_fb_flush();
  189. /* Keep the WM from draining the keyboard ring while we poll for the
  190. * confirmation (works for PS/2 and USB keyboards alike). */
  191. io_set_raw_keyboard(1);
  192. for (;;) {
  193. k = io_poll_key_with_hw();
  194. if (k == 'y' || k == 'Y')
  195. break;
  196. if (k == 'n' || k == 'N') {
  197. io_set_raw_keyboard(0);
  198. io_putstr("aborted\n");
  199. return;
  200. }
  201. /* This kernel command holds the BKL. Sleep briefly so the
  202. * kworker can poll USB HID and the terminal can render the
  203. * confirmation prompt; otherwise a USB-only keyboard deadlocks
  204. * here. */
  205. process_sleep_ms(10);
  206. }
  207. io_set_raw_keyboard(0);
  208. io_putchar('\n');
  209. io_putstr("formatting ");
  210. io_putstr(d->name);
  211. io_putstr("...\n");
  212. vga_fb_flush();
  213. /* Other filesystem syscalls wait while the FAT is half-written. */
  214. format_owner_pid = current_proc()->pid;
  215. format_busy = 1;
  216. /* The raw-sector loop does not touch shared kernel state (format_busy
  217. * keeps filesystem users off the disks; the block driver owns the
  218. * controller). Release the BKL for its duration: a kernel command
  219. * otherwise holds it for its whole lifetime, and a long format starves
  220. * every ring-3 process (the WM stops compositing, the terminal stops
  221. * draining) even though it prints progress. Reacquire it before the
  222. * remount below. */
  223. bkl_unlock();
  224. r = fat_format_limited(d, limit);
  225. bkl_lock();
  226. if (r != 0) {
  227. format_busy = 0;
  228. io_putstr("format: failed\n");
  229. return;
  230. }
  231. /* Make the drive commit the new metadata before the remount reads it
  232. * back (some IDE/SATA bridges acknowledge PIO writes before the data
  233. * reaches the medium). */
  234. {
  235. int fr;
  236. bkl_pause();
  237. fr = blk_flush(d);
  238. bkl_resume();
  239. if (fr != 0)
  240. io_putstr("format: cache flush failed; remount may be stale\n");
  241. }
  242. /* Remount only the formatted device; a full vfs_mount_all() would probe
  243. * every disk again and was a second large stall. */
  244. vfs_remount_device(d);
  245. format_busy = 0;
  246. io_putstr("done\n");
  247. }