format.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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, yes = 0;
  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. /* `-y` skips the prompt: the `disks` app confirms in its own dialog
  117. * and runs the command on a private tty with no keyboard. */
  118. if (arg[i] == '-' && arg[i + 1] == 'y' &&
  119. (arg[i + 2] == ' ' || !arg[i + 2])) {
  120. yes = 1;
  121. i += 2;
  122. while (arg[i] == ' ')
  123. i++;
  124. }
  125. j = 0;
  126. while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
  127. name[j++] = arg[i++];
  128. name[j] = '\0';
  129. if (name[0] == '\0') {
  130. io_putstr("format: usage format <device> [size]\n");
  131. list_devices();
  132. return;
  133. }
  134. p = arg + i;
  135. while (*p == ' ')
  136. p++;
  137. if (*p) {
  138. r = parse_size(p, &limit);
  139. if (r == -1) {
  140. io_putstr("format: bad size '");
  141. io_putstr(p);
  142. io_putstr("' (use e.g. 100mb or 1gb)\n");
  143. return;
  144. }
  145. if (r == -2) {
  146. io_putstr("format: size is too large\n");
  147. return;
  148. }
  149. if (r == -3) {
  150. io_putstr("format: size must be greater than zero\n");
  151. return;
  152. }
  153. }
  154. d = blk_find(name);
  155. if (!d) {
  156. io_putstr("format: unknown device '");
  157. io_putstr(name);
  158. io_putstr("'\n");
  159. list_devices();
  160. return;
  161. }
  162. if (d->kind != BLK_FLOPPY && !d->present) {
  163. io_putstr("format: ");
  164. io_putstr(d->name);
  165. io_putstr(": not present\n");
  166. return;
  167. }
  168. if (d->kind == BLK_FLOPPY && limit) {
  169. io_putstr("format: a floppy is always the full 1440 KiB\n");
  170. return;
  171. }
  172. /* Reject a bad limit before the destructive confirmation prompt. */
  173. if (d->kind != BLK_FLOPPY && limit) {
  174. unsigned int avail = (d->total_sectors > 2048)
  175. ? d->total_sectors - 2048 : 0;
  176. if (limit > avail) {
  177. io_putstr("format: ");
  178. print_size(limit);
  179. io_putstr(" does not fit on ");
  180. io_putstr(d->name);
  181. io_putstr(" (");
  182. print_size(avail);
  183. io_putstr(" available)\n");
  184. return;
  185. }
  186. }
  187. io_putstr("format ");
  188. io_putstr(d->name);
  189. if (limit) {
  190. io_putstr(" (");
  191. print_size(limit);
  192. io_putstr(")");
  193. } else {
  194. io_putstr(" (whole disk)");
  195. }
  196. io_putstr("? this destroys all data (y/n) ");
  197. if (yes) {
  198. io_putstr("y\n");
  199. goto confirmed;
  200. }
  201. vga_fb_flush();
  202. /* Keep the WM from draining the keyboard ring while we poll for the
  203. * confirmation (works for PS/2 and USB keyboards alike). */
  204. io_set_raw_keyboard(1);
  205. for (;;) {
  206. k = io_poll_key_with_hw();
  207. if (k == 'y' || k == 'Y')
  208. break;
  209. if (k == 'n' || k == 'N') {
  210. io_set_raw_keyboard(0);
  211. io_putstr("aborted\n");
  212. return;
  213. }
  214. /* This kernel command holds the BKL. Sleep briefly so the
  215. * kworker can poll USB HID and the terminal can render the
  216. * confirmation prompt; otherwise a USB-only keyboard deadlocks
  217. * here. */
  218. process_sleep_ms(10);
  219. }
  220. io_set_raw_keyboard(0);
  221. io_putchar('\n');
  222. confirmed:
  223. io_putstr("formatting ");
  224. io_putstr(d->name);
  225. io_putstr("...\n");
  226. vga_fb_flush();
  227. /* Other filesystem syscalls wait while the FAT is half-written. */
  228. format_owner_pid = current_proc()->pid;
  229. format_busy = 1;
  230. /* The raw-sector loop does not touch shared kernel state (format_busy
  231. * keeps filesystem users off the disks; the block driver owns the
  232. * controller). Release the BKL for its duration: a kernel command
  233. * otherwise holds it for its whole lifetime, and a long format starves
  234. * every ring-3 process (the WM stops compositing, the terminal stops
  235. * draining) even though it prints progress. Reacquire it before the
  236. * remount below. */
  237. bkl_unlock();
  238. r = fat_format_limited(d, limit);
  239. bkl_lock();
  240. if (r != 0) {
  241. format_busy = 0;
  242. io_putstr("format: failed\n");
  243. return;
  244. }
  245. /* Make the drive commit the new metadata before the remount reads it
  246. * back (some IDE/SATA bridges acknowledge PIO writes before the data
  247. * reaches the medium). */
  248. {
  249. int fr;
  250. bkl_pause();
  251. fr = blk_flush(d);
  252. bkl_resume();
  253. if (fr != 0)
  254. io_putstr("format: cache flush failed; remount may be stale\n");
  255. }
  256. /* Remount only the formatted device; a full vfs_mount_all() would probe
  257. * every disk again and was a second large stall. */
  258. vfs_remount_device(d);
  259. format_busy = 0;
  260. io_putstr("done\n");
  261. }