fatformat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* Create a fresh FAT filesystem on a block device.
  2. * Floppy: FAT12 superfloppy (no MBR). IDE/SATA: MBR + single FAT16/32 partition. */
  3. #include "fatformat.h"
  4. #include "io.h"
  5. #include "mm.h"
  6. #include "process.h"
  7. #include "sched.h"
  8. #define SEC_SIZE 512
  9. /* Zero-fill chunk size for the FAT/reserved/root areas. Large disks would
  10. * otherwise need millions of single-sector writes (a FAT32 on a 1 TB disk has
  11. * ~2 M FAT sectors). */
  12. #define ZERO_CHUNK_PAGES 32
  13. volatile int format_busy;
  14. volatile unsigned int format_owner_pid;
  15. static unsigned int fmt_total, fmt_done, fmt_last_pct;
  16. static const char *fmt_stage = "?";
  17. static int fmt_error_printed;
  18. /* Format output goes through the tty, which the netlog mirror captures. */
  19. static void format_puts(const char *s)
  20. {
  21. io_putstr(s);
  22. }
  23. void format_putdec(unsigned int v)
  24. {
  25. char b[11];
  26. unsigned int n = sizeof(b) - 1;
  27. b[n] = '\0';
  28. do {
  29. b[--n] = (char)('0' + v % 10);
  30. v /= 10;
  31. } while (v);
  32. io_putstr(&b[n]);
  33. }
  34. static void format_pct(unsigned int pct)
  35. {
  36. format_puts("format: ");
  37. format_putdec(pct);
  38. format_puts("%\n");
  39. }
  40. static void format_progress(unsigned int sectors)
  41. {
  42. unsigned int pct;
  43. if (!fmt_total)
  44. return;
  45. fmt_done += sectors;
  46. pct = (fmt_done * 100u) / fmt_total;
  47. if (pct > 100) pct = 100;
  48. if (pct >= fmt_last_pct + 1 || pct == 100) {
  49. fmt_last_pct = pct;
  50. format_pct(pct);
  51. /* Sleep briefly after the line so the ring-3 terminal drains
  52. * the tty and the WM presents the new row promptly. */
  53. process_sleep_ms(10);
  54. }
  55. }
  56. /* A chunk write releases the BKL so the WM/netlog/terminal can run between
  57. * disk transfers; format_busy keeps other filesystem syscalls out. */
  58. static int format_write(blk_dev_t *d, unsigned int lba, const void *buf,
  59. unsigned int count)
  60. {
  61. int r;
  62. bkl_pause();
  63. r = blk_write(d, lba, buf, count);
  64. bkl_resume();
  65. if (r != 0 && !fmt_error_printed) {
  66. fmt_error_printed = 1;
  67. format_puts("format: write failed at stage ");
  68. format_puts(fmt_stage);
  69. format_puts(": lba ");
  70. format_putdec(lba);
  71. format_puts(" count ");
  72. format_putdec(count);
  73. format_puts(" sectors\n");
  74. }
  75. return r;
  76. }
  77. /* Write `count` zero sectors at `lba` in `chunk`-sector pieces. */
  78. static int write_zeros(blk_dev_t *d, unsigned int lba, unsigned int count,
  79. const unsigned char *buf, unsigned int chunk)
  80. {
  81. unsigned int done = 0;
  82. while (done < count) {
  83. unsigned int n = count - done;
  84. if (n > chunk)
  85. n = chunk;
  86. if (format_write(d, lba + done, buf, n) != 0)
  87. return -1;
  88. format_progress(n);
  89. done += n;
  90. }
  91. return 0;
  92. }
  93. static void put16(unsigned char *p, unsigned int v)
  94. {
  95. p[0] = (unsigned char)(v & 0xFF);
  96. p[1] = (unsigned char)((v >> 8) & 0xFF);
  97. }
  98. static void put32(unsigned char *p, unsigned int v)
  99. {
  100. p[0] = (unsigned char)(v & 0xFF);
  101. p[1] = (unsigned char)((v >> 8) & 0xFF);
  102. p[2] = (unsigned char)((v >> 16) & 0xFF);
  103. p[3] = (unsigned char)((v >> 24) & 0xFF);
  104. }
  105. static void zero_sector(unsigned char *s)
  106. {
  107. unsigned int i;
  108. for (i = 0; i < SEC_SIZE; i++)
  109. s[i] = 0;
  110. }
  111. static unsigned int fat_sec_per_fat(unsigned int type, unsigned int clusters)
  112. {
  113. unsigned int entries = clusters + 2;
  114. if (type == 12)
  115. return (entries * 3 + 1023) / 1024;
  116. if (type == 16)
  117. return (entries * 2 + 511) / 512;
  118. return (entries * 4 + 511) / 512;
  119. }
  120. static void choose_layout(unsigned int fs_sectors, unsigned int *type,
  121. unsigned int *spc, unsigned int *root_entries)
  122. {
  123. if (fs_sectors < 4085) {
  124. *type = 12;
  125. *spc = 1;
  126. *root_entries = (fs_sectors <= 2880) ? 224 : 512;
  127. } else if (fs_sectors <= 4193536u) { /* ~2 GB */
  128. *type = 16;
  129. *spc = 1;
  130. while (fs_sectors / *spc >= 65525u)
  131. *spc <<= 1;
  132. *root_entries = 512;
  133. } else {
  134. *type = 32;
  135. *spc = 8;
  136. while (fs_sectors / *spc >= 268435445u)
  137. *spc <<= 1;
  138. *root_entries = 0;
  139. }
  140. }
  141. static void build_boot_sector(unsigned char *s, unsigned int type, unsigned int spc,
  142. unsigned int reserved, unsigned int num_fats,
  143. unsigned int root_entries, unsigned int sec_per_fat,
  144. unsigned int fs_sectors, unsigned int hidden,
  145. unsigned int media, const char *label)
  146. {
  147. unsigned int i;
  148. zero_sector(s);
  149. /* Volume label: an 11-byte, space-padded field (no dot). A NULL label
  150. * keeps the old blank behaviour used by the `format` command. */
  151. {
  152. unsigned int off = (type == 32) ? 0x47 : 0x2B;
  153. for (i = 0; i < 11; i++) {
  154. char c = label ? label[i] : '\0';
  155. if (c == '\0')
  156. break;
  157. s[off + i] = (unsigned char)c;
  158. }
  159. for (; i < 11; i++)
  160. s[off + i] = ' ';
  161. }
  162. s[0] = 0xEB;
  163. s[1] = (type == 32) ? 0x58 : 0x3C;
  164. s[2] = 0x90;
  165. s[3] = 'M'; s[4] = 'S'; s[5] = 'D'; s[6] = 'O'; s[7] = 'S';
  166. s[8] = '5'; s[9] = '.'; s[10] = '0';
  167. put16(s + 0x0B, 512);
  168. s[0x0D] = (unsigned char)spc;
  169. put16(s + 0x0E, reserved);
  170. s[0x10] = (unsigned char)num_fats;
  171. put16(s + 0x11, root_entries);
  172. put16(s + 0x13, (fs_sectors < 0x10000) ? fs_sectors : 0);
  173. s[0x15] = (unsigned char)media;
  174. put16(s + 0x16, (type != 32) ? sec_per_fat : 0);
  175. put16(s + 0x18, (hidden == 0) ? 18 : 63); /* sectors per track */
  176. put16(s + 0x1A, (hidden == 0) ? 2 : 255); /* heads */
  177. put32(s + 0x1C, hidden);
  178. put32(s + 0x20, fs_sectors);
  179. if (type == 32) {
  180. put32(s + 0x24, sec_per_fat);
  181. s[0x28] = 0; s[0x29] = 0; /* ext flags */
  182. s[0x2A] = 0; s[0x2B] = 0; /* fs version */
  183. put32(s + 0x2C, 2); /* root cluster */
  184. put16(s + 0x30, 1); /* FSInfo sector */
  185. put16(s + 0x32, 6); /* backup boot sector */
  186. s[0x40] = 0x80; /* drive number */
  187. s[0x42] = 0x29; /* boot signature */
  188. put32(s + 0x43, 0x12345678); /* volume id */
  189. s[0x52] = 'F'; s[0x53] = 'A'; s[0x54] = 'T'; s[0x55] = '3';
  190. s[0x56] = '2'; s[0x57] = ' '; s[0x58] = ' '; s[0x59] = ' ';
  191. } else {
  192. s[0x24] = (hidden == 0) ? 0x00 : 0x80; /* drive number */
  193. s[0x26] = 0x29; /* boot signature */
  194. put32(s + 0x27, 0x12345678); /* volume id */
  195. s[0x36] = 'F'; s[0x37] = 'A'; s[0x38] = 'T'; s[0x39] = '1';
  196. s[0x3A] = (type == 12) ? '2' : '6';
  197. s[0x3B] = ' '; s[0x3C] = ' '; s[0x3D] = ' ';
  198. }
  199. s[510] = 0x55;
  200. s[511] = 0xAA;
  201. }
  202. static void build_fat_first_sector(unsigned char *s, unsigned int type,
  203. unsigned int media)
  204. {
  205. zero_sector(s);
  206. s[0] = (unsigned char)media;
  207. if (type == 12) {
  208. s[1] = 0xFF;
  209. s[2] = 0xFF;
  210. } else if (type == 16) {
  211. s[1] = 0xFF;
  212. s[2] = 0xFF;
  213. s[3] = 0xFF;
  214. } else { /* FAT32 */
  215. s[1] = 0xFF; s[2] = 0xFF; s[3] = 0x0F; /* entry 0 */
  216. s[4] = 0xFF; s[5] = 0xFF; s[6] = 0xFF; s[7] = 0x0F; /* entry 1 */
  217. s[8] = 0xFF; s[9] = 0xFF; s[10] = 0xFF; s[11] = 0x0F; /* entry 2 (root) */
  218. }
  219. }
  220. static void build_fsinfo_sector(unsigned char *s)
  221. {
  222. zero_sector(s);
  223. put32(s + 0x000, 0x41615252u); /* LeadSig "RRaA" */
  224. put32(s + 0x1E4, 0x61417272u); /* StrucSig "rrAa" */
  225. put32(s + 0x1E8, 0xFFFFFFFFu); /* free cluster count: unknown */
  226. put32(s + 0x1EC, 0xFFFFFFFFu); /* next free cluster: unknown */
  227. put32(s + 0x1FC, 0xAA550000u); /* TrailSig */
  228. }
  229. /* Format one FAT region. `write_mbr` is set only for whole-disk formats;
  230. * partition formats leave the MBR to the caller (the installer merges a
  231. * preserved partition-4 entry). */
  232. static int fat_format_region(blk_dev_t *d, unsigned int part_start,
  233. unsigned int fs_sectors, int write_mbr,
  234. int is_floppy, const char *label,
  235. unsigned int *type_out)
  236. {
  237. static unsigned char sec[SEC_SIZE];
  238. static unsigned char fat_sec[SEC_SIZE];
  239. static unsigned char zero[SEC_SIZE];
  240. unsigned char *zbuf;
  241. unsigned int zchunk, zpages;
  242. unsigned int type, spc, root_entries;
  243. unsigned int reserved, num_fats = 2, sec_per_fat, root_dir_sectors;
  244. unsigned int clusters, fat_start, root_start, data_start;
  245. unsigned int media;
  246. unsigned int i;
  247. int rc = -1;
  248. fmt_stage = "layout";
  249. fmt_error_printed = 0;
  250. if (!d) {
  251. format_puts("format: no device\n");
  252. return -1;
  253. }
  254. media = is_floppy ? 0xF0 : 0xF8;
  255. choose_layout(fs_sectors, &type, &spc, &root_entries);
  256. reserved = (type == 32) ? 32 : 1;
  257. /* Converge on FAT size + cluster count. */
  258. clusters = fs_sectors / spc;
  259. for (i = 0; i < 3; i++) {
  260. unsigned int data;
  261. sec_per_fat = fat_sec_per_fat(type, clusters);
  262. data = fs_sectors - reserved - num_fats * sec_per_fat
  263. - ((root_entries * 32 + 511) / 512);
  264. clusters = data / spc;
  265. }
  266. sec_per_fat = fat_sec_per_fat(type, clusters);
  267. root_dir_sectors = (root_entries * 32 + 511) / 512;
  268. fat_start = reserved;
  269. root_start = reserved + num_fats * sec_per_fat;
  270. data_start = root_start + root_dir_sectors;
  271. fmt_total = reserved + num_fats * sec_per_fat + root_dir_sectors;
  272. if (type == 32)
  273. fmt_total += spc;
  274. fmt_done = 0;
  275. fmt_last_pct = 0;
  276. format_puts("format: ");
  277. format_puts(d->name);
  278. format_puts(": ");
  279. format_putdec(fs_sectors);
  280. format_puts(" sectors");
  281. if (is_floppy) {
  282. format_puts(" (1440 KiB)");
  283. } else if (fs_sectors >= 2048) {
  284. format_puts(" (");
  285. format_putdec(fs_sectors / 2048);
  286. format_puts(" MiB)");
  287. }
  288. format_puts(", FAT");
  289. format_putdec(type);
  290. format_puts(", partition ");
  291. format_putdec(part_start);
  292. format_puts("+");
  293. format_putdec(fs_sectors);
  294. format_puts(", cluster ");
  295. format_putdec(spc);
  296. format_puts(", ");
  297. format_putdec(num_fats);
  298. format_puts(" x ");
  299. format_putdec(sec_per_fat);
  300. format_puts(" FAT sectors\n");
  301. zero_sector(zero);
  302. /* Bulk zero buffer (a 1 TB FAT32 has ~2 M FAT sectors; single-sector
  303. * writes would take minutes). Real disks only -- floppies keep the
  304. * 512-byte path. */
  305. zbuf = zero;
  306. zchunk = 1;
  307. zpages = 0;
  308. if (!is_floppy) {
  309. /* The caller drops the BKL for the long format loop, but the
  310. * frame allocator bitmap is only protected by interrupts (not
  311. * SMP-safe), so take the BKL around the allocation. bkl_lock()
  312. * nests when the caller still holds it. */
  313. bkl_lock();
  314. zbuf = (unsigned char *)mm_alloc_pages(ZERO_CHUNK_PAGES);
  315. bkl_unlock();
  316. if (zbuf) {
  317. unsigned int k;
  318. for (k = 0; k < ZERO_CHUNK_PAGES * PAGE_SIZE; k++)
  319. zbuf[k] = 0;
  320. zchunk = ZERO_CHUNK_PAGES * (PAGE_SIZE / SEC_SIZE);
  321. zpages = ZERO_CHUNK_PAGES;
  322. } else {
  323. zbuf = zero;
  324. zchunk = 1;
  325. }
  326. }
  327. /* 1. MBR (whole-disk formats only). */
  328. if (write_mbr) {
  329. unsigned char *e;
  330. fmt_stage = "mbr";
  331. zero_sector(sec);
  332. e = sec + 0x1BE;
  333. e[0] = 0x80; /* bootable */
  334. e[1] = 0x00; e[2] = 0x02; e[3] = 0x00; /* start CHS (C0 H0 S2) */
  335. e[4] = (type == 32) ? 0x0C : (type == 16 ? 0x0E : 0x01);
  336. e[5] = 0xFE; e[6] = 0xFF; e[7] = 0xFF; /* end CHS (max, LBA) */
  337. put32(e + 8, part_start);
  338. put32(e + 12, fs_sectors);
  339. sec[510] = 0x55;
  340. sec[511] = 0xAA;
  341. if (format_write(d, 0, sec, 1) != 0)
  342. goto out;
  343. }
  344. /* 2. Reserved area + boot sector (+ FSInfo/backup for FAT32). */
  345. fmt_stage = "reserved";
  346. build_boot_sector(sec, type, spc, reserved, num_fats, root_entries,
  347. sec_per_fat, fs_sectors, part_start, media, label);
  348. if (write_zeros(d, part_start, reserved, zbuf, zchunk) != 0)
  349. goto out;
  350. fmt_stage = "boot sector";
  351. if (format_write(d, part_start, sec, 1) != 0)
  352. goto out;
  353. if (type == 32) {
  354. fmt_stage = "backup boot";
  355. if (format_write(d, part_start + 6, sec, 1) != 0)
  356. goto out;
  357. fmt_stage = "fsinfo";
  358. build_fsinfo_sector(sec);
  359. if (format_write(d, part_start + 1, sec, 1) != 0)
  360. goto out;
  361. }
  362. /* 3. FATs (two copies). The first sector carries the media byte and
  363. * the end-of-chain markers; the rest must be zero. */
  364. build_fat_first_sector(fat_sec, type, media);
  365. for (i = 0; i < num_fats; i++) {
  366. unsigned int base = part_start + fat_start + i * sec_per_fat;
  367. fmt_stage = (i == 0) ? "fat 1" : "fat 2";
  368. if (format_write(d, base, fat_sec, 1) != 0)
  369. goto out;
  370. format_progress(1);
  371. if (sec_per_fat > 1 &&
  372. write_zeros(d, base + 1, sec_per_fat - 1, zbuf, zchunk) != 0)
  373. goto out;
  374. }
  375. /* 4. Root directory (FAT12/16) or root cluster (FAT32). */
  376. fmt_stage = "root directory";
  377. if (type == 32) {
  378. if (write_zeros(d, part_start + data_start, spc, zbuf, zchunk) != 0)
  379. goto out;
  380. } else {
  381. if (write_zeros(d, part_start + root_start, root_dir_sectors,
  382. zbuf, zchunk) != 0)
  383. goto out;
  384. }
  385. rc = 0;
  386. if (type_out)
  387. *type_out = type;
  388. out:
  389. if (zpages) {
  390. unsigned int k;
  391. bkl_lock();
  392. for (k = 0; k < zpages; k++)
  393. mm_free_page((unsigned int)zbuf + k * PAGE_SIZE);
  394. bkl_unlock();
  395. }
  396. return rc;
  397. }
  398. int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
  399. {
  400. unsigned int fs_sectors, part_start;
  401. int is_floppy;
  402. if (!d) {
  403. format_puts("format: no device\n");
  404. return -1;
  405. }
  406. is_floppy = (d->kind == BLK_FLOPPY);
  407. if (is_floppy) {
  408. if (limit_sectors) {
  409. format_puts("format: a floppy is always the full 1440 KiB\n");
  410. return -1;
  411. }
  412. fs_sectors = 2880;
  413. part_start = 0;
  414. } else {
  415. unsigned int avail;
  416. if (d->total_sectors < (2048 + 64)) {
  417. format_puts("format: disk too small (");
  418. format_putdec(d->total_sectors);
  419. format_puts(" sectors)\n");
  420. return -1;
  421. }
  422. part_start = 2048;
  423. avail = d->total_sectors - part_start;
  424. fs_sectors = avail;
  425. if (limit_sectors) {
  426. if (limit_sectors > avail) {
  427. format_puts("format: size exceeds device (");
  428. format_putdec(avail / 2048);
  429. format_puts(" MiB available)\n");
  430. return -1;
  431. }
  432. fs_sectors = limit_sectors;
  433. }
  434. if (fs_sectors < 64) {
  435. format_puts("format: partition too small (");
  436. format_putdec(fs_sectors);
  437. format_puts(" sectors)\n");
  438. return -1;
  439. }
  440. }
  441. return fat_format_region(d, part_start, fs_sectors, !is_floppy,
  442. is_floppy, 0, 0);
  443. }
  444. int fat_format_partition(blk_dev_t *d, unsigned int part_lba,
  445. unsigned int sectors, const char *label,
  446. unsigned int *type_out)
  447. {
  448. if (!d) {
  449. format_puts("format: no device\n");
  450. return -1;
  451. }
  452. if (d->kind == BLK_FLOPPY) {
  453. format_puts("format: cannot format a floppy partition\n");
  454. return -1;
  455. }
  456. if (part_lba == 0 || sectors < 64 ||
  457. part_lba > d->total_sectors ||
  458. sectors > d->total_sectors - part_lba) {
  459. format_puts("format: partition range invalid (");
  460. format_putdec(part_lba);
  461. format_puts("+");
  462. format_putdec(sectors);
  463. format_puts(")\n");
  464. return -1;
  465. }
  466. return fat_format_region(d, part_lba, sectors, 0, 0, label, type_out);
  467. }
  468. int fat_format(blk_dev_t *d)
  469. {
  470. return fat_format_limited(d, 0);
  471. }