fatformat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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)
  146. {
  147. unsigned int i;
  148. zero_sector(s);
  149. s[0] = 0xEB;
  150. s[1] = (type == 32) ? 0x58 : 0x3C;
  151. s[2] = 0x90;
  152. s[3] = 'M'; s[4] = 'S'; s[5] = 'D'; s[6] = 'O'; s[7] = 'S';
  153. s[8] = '5'; s[9] = '.'; s[10] = '0';
  154. put16(s + 0x0B, 512);
  155. s[0x0D] = (unsigned char)spc;
  156. put16(s + 0x0E, reserved);
  157. s[0x10] = (unsigned char)num_fats;
  158. put16(s + 0x11, root_entries);
  159. put16(s + 0x13, (fs_sectors < 0x10000) ? fs_sectors : 0);
  160. s[0x15] = (unsigned char)media;
  161. put16(s + 0x16, (type != 32) ? sec_per_fat : 0);
  162. put16(s + 0x18, (hidden == 0) ? 18 : 63); /* sectors per track */
  163. put16(s + 0x1A, (hidden == 0) ? 2 : 255); /* heads */
  164. put32(s + 0x1C, hidden);
  165. put32(s + 0x20, fs_sectors);
  166. if (type == 32) {
  167. put32(s + 0x24, sec_per_fat);
  168. s[0x28] = 0; s[0x29] = 0; /* ext flags */
  169. s[0x2A] = 0; s[0x2B] = 0; /* fs version */
  170. put32(s + 0x2C, 2); /* root cluster */
  171. put16(s + 0x30, 1); /* FSInfo sector */
  172. put16(s + 0x32, 6); /* backup boot sector */
  173. s[0x40] = 0x80; /* drive number */
  174. s[0x42] = 0x29; /* boot signature */
  175. put32(s + 0x43, 0x12345678); /* volume id */
  176. for (i = 0; i < 11; i++) s[0x47 + i] = ' ';
  177. s[0x52] = 'F'; s[0x53] = 'A'; s[0x54] = 'T'; s[0x55] = '3';
  178. s[0x56] = '2'; s[0x57] = ' '; s[0x58] = ' '; s[0x59] = ' ';
  179. } else {
  180. s[0x24] = (hidden == 0) ? 0x00 : 0x80; /* drive number */
  181. s[0x26] = 0x29; /* boot signature */
  182. put32(s + 0x27, 0x12345678); /* volume id */
  183. for (i = 0; i < 11; i++) s[0x2B + i] = ' ';
  184. s[0x36] = 'F'; s[0x37] = 'A'; s[0x38] = 'T'; s[0x39] = '1';
  185. s[0x3A] = (type == 12) ? '2' : '6';
  186. s[0x3B] = ' '; s[0x3C] = ' '; s[0x3D] = ' ';
  187. }
  188. s[510] = 0x55;
  189. s[511] = 0xAA;
  190. }
  191. static void build_fat_first_sector(unsigned char *s, unsigned int type,
  192. unsigned int media)
  193. {
  194. zero_sector(s);
  195. s[0] = (unsigned char)media;
  196. if (type == 12) {
  197. s[1] = 0xFF;
  198. s[2] = 0xFF;
  199. } else if (type == 16) {
  200. s[1] = 0xFF;
  201. s[2] = 0xFF;
  202. s[3] = 0xFF;
  203. } else { /* FAT32 */
  204. s[1] = 0xFF; s[2] = 0xFF; s[3] = 0x0F; /* entry 0 */
  205. s[4] = 0xFF; s[5] = 0xFF; s[6] = 0xFF; s[7] = 0x0F; /* entry 1 */
  206. s[8] = 0xFF; s[9] = 0xFF; s[10] = 0xFF; s[11] = 0x0F; /* entry 2 (root) */
  207. }
  208. }
  209. static void build_fsinfo_sector(unsigned char *s)
  210. {
  211. zero_sector(s);
  212. put32(s + 0x000, 0x41615252u); /* LeadSig "RRaA" */
  213. put32(s + 0x1E4, 0x61417272u); /* StrucSig "rrAa" */
  214. put32(s + 0x1E8, 0xFFFFFFFFu); /* free cluster count: unknown */
  215. put32(s + 0x1EC, 0xFFFFFFFFu); /* next free cluster: unknown */
  216. put32(s + 0x1FC, 0xAA550000u); /* TrailSig */
  217. }
  218. int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
  219. {
  220. static unsigned char sec[SEC_SIZE];
  221. static unsigned char fat_sec[SEC_SIZE];
  222. static unsigned char zero[SEC_SIZE];
  223. unsigned char *zbuf;
  224. unsigned int zchunk, zpages;
  225. unsigned int fs_sectors, part_start;
  226. unsigned int type, spc, root_entries;
  227. unsigned int reserved, num_fats = 2, sec_per_fat, root_dir_sectors;
  228. unsigned int clusters, fat_start, root_start, data_start;
  229. unsigned int media;
  230. int is_floppy;
  231. unsigned int i;
  232. int rc = -1;
  233. fmt_stage = "layout";
  234. fmt_error_printed = 0;
  235. if (!d) {
  236. format_puts("format: no device\n");
  237. return -1;
  238. }
  239. is_floppy = (d->kind == BLK_FLOPPY);
  240. if (is_floppy) {
  241. if (limit_sectors) {
  242. format_puts("format: a floppy is always the full 1440 KiB\n");
  243. return -1;
  244. }
  245. fs_sectors = 2880;
  246. part_start = 0;
  247. media = 0xF0;
  248. } else {
  249. unsigned int avail;
  250. if (d->total_sectors < (2048 + 64)) {
  251. format_puts("format: disk too small (");
  252. format_putdec(d->total_sectors);
  253. format_puts(" sectors)\n");
  254. return -1;
  255. }
  256. part_start = 2048;
  257. avail = d->total_sectors - part_start;
  258. fs_sectors = avail;
  259. if (limit_sectors) {
  260. if (limit_sectors > avail) {
  261. format_puts("format: size exceeds device (");
  262. format_putdec(avail / 2048);
  263. format_puts(" MiB available)\n");
  264. return -1;
  265. }
  266. fs_sectors = limit_sectors;
  267. }
  268. if (fs_sectors < 64) {
  269. format_puts("format: partition too small (");
  270. format_putdec(fs_sectors);
  271. format_puts(" sectors)\n");
  272. return -1;
  273. }
  274. media = 0xF8;
  275. }
  276. choose_layout(fs_sectors, &type, &spc, &root_entries);
  277. reserved = (type == 32) ? 32 : 1;
  278. /* Converge on FAT size + cluster count. */
  279. clusters = fs_sectors / spc;
  280. for (i = 0; i < 3; i++) {
  281. unsigned int data;
  282. sec_per_fat = fat_sec_per_fat(type, clusters);
  283. data = fs_sectors - reserved - num_fats * sec_per_fat
  284. - ((root_entries * 32 + 511) / 512);
  285. clusters = data / spc;
  286. }
  287. sec_per_fat = fat_sec_per_fat(type, clusters);
  288. root_dir_sectors = (root_entries * 32 + 511) / 512;
  289. fat_start = reserved;
  290. root_start = reserved + num_fats * sec_per_fat;
  291. data_start = root_start + root_dir_sectors;
  292. fmt_total = reserved + num_fats * sec_per_fat + root_dir_sectors;
  293. if (type == 32)
  294. fmt_total += spc;
  295. fmt_done = 0;
  296. fmt_last_pct = 0;
  297. format_puts("format: ");
  298. format_puts(d->name);
  299. format_puts(": ");
  300. format_putdec(d->total_sectors);
  301. format_puts(" sectors");
  302. if (is_floppy) {
  303. format_puts(" (1440 KiB)");
  304. } else if (d->total_sectors >= 2048) {
  305. format_puts(" (");
  306. format_putdec(d->total_sectors / 2048);
  307. format_puts(" MiB)");
  308. }
  309. format_puts(", FAT");
  310. format_putdec(type);
  311. format_puts(", partition ");
  312. format_putdec(part_start);
  313. format_puts("+");
  314. format_putdec(fs_sectors);
  315. format_puts(", cluster ");
  316. format_putdec(spc);
  317. format_puts(", ");
  318. format_putdec(num_fats);
  319. format_puts(" x ");
  320. format_putdec(sec_per_fat);
  321. format_puts(" FAT sectors\n");
  322. zero_sector(zero);
  323. /* Bulk zero buffer (a 1 TB FAT32 has ~2 M FAT sectors; single-sector
  324. * writes would take minutes). Real disks only -- floppies keep the
  325. * 512-byte path. */
  326. zbuf = zero;
  327. zchunk = 1;
  328. zpages = 0;
  329. if (!is_floppy) {
  330. /* The caller drops the BKL for the long format loop, but the
  331. * frame allocator bitmap is only protected by interrupts (not
  332. * SMP-safe), so take the BKL around the allocation. bkl_lock()
  333. * nests when the caller still holds it. */
  334. bkl_lock();
  335. zbuf = (unsigned char *)mm_alloc_pages(ZERO_CHUNK_PAGES);
  336. bkl_unlock();
  337. if (zbuf) {
  338. unsigned int k;
  339. for (k = 0; k < ZERO_CHUNK_PAGES * PAGE_SIZE; k++)
  340. zbuf[k] = 0;
  341. zchunk = ZERO_CHUNK_PAGES * (PAGE_SIZE / SEC_SIZE);
  342. zpages = ZERO_CHUNK_PAGES;
  343. } else {
  344. zbuf = zero;
  345. zchunk = 1;
  346. }
  347. }
  348. /* 1. MBR (IDE/SATA only). */
  349. if (!is_floppy) {
  350. unsigned char *e;
  351. fmt_stage = "mbr";
  352. zero_sector(sec);
  353. e = sec + 0x1BE;
  354. e[0] = 0x80; /* bootable */
  355. e[1] = 0x00; e[2] = 0x02; e[3] = 0x00; /* start CHS (C0 H0 S2) */
  356. e[4] = (type == 32) ? 0x0C : (type == 16 ? 0x0E : 0x01);
  357. e[5] = 0xFE; e[6] = 0xFF; e[7] = 0xFF; /* end CHS (max, LBA) */
  358. put32(e + 8, part_start);
  359. put32(e + 12, fs_sectors);
  360. sec[510] = 0x55;
  361. sec[511] = 0xAA;
  362. if (format_write(d, 0, sec, 1) != 0)
  363. goto out;
  364. }
  365. /* 2. Reserved area + boot sector (+ FSInfo/backup for FAT32). */
  366. fmt_stage = "reserved";
  367. build_boot_sector(sec, type, spc, reserved, num_fats, root_entries,
  368. sec_per_fat, fs_sectors, part_start, media);
  369. if (write_zeros(d, part_start, reserved, zbuf, zchunk) != 0)
  370. goto out;
  371. fmt_stage = "boot sector";
  372. if (format_write(d, part_start, sec, 1) != 0)
  373. goto out;
  374. if (type == 32) {
  375. fmt_stage = "backup boot";
  376. if (format_write(d, part_start + 6, sec, 1) != 0)
  377. goto out;
  378. fmt_stage = "fsinfo";
  379. build_fsinfo_sector(sec);
  380. if (format_write(d, part_start + 1, sec, 1) != 0)
  381. goto out;
  382. }
  383. /* 3. FATs (two copies). The first sector carries the media byte and
  384. * the end-of-chain markers; the rest must be zero. */
  385. build_fat_first_sector(fat_sec, type, media);
  386. for (i = 0; i < num_fats; i++) {
  387. unsigned int base = part_start + fat_start + i * sec_per_fat;
  388. fmt_stage = (i == 0) ? "fat 1" : "fat 2";
  389. if (format_write(d, base, fat_sec, 1) != 0)
  390. goto out;
  391. format_progress(1);
  392. if (sec_per_fat > 1 &&
  393. write_zeros(d, base + 1, sec_per_fat - 1, zbuf, zchunk) != 0)
  394. goto out;
  395. }
  396. /* 4. Root directory (FAT12/16) or root cluster (FAT32). */
  397. fmt_stage = "root directory";
  398. if (type == 32) {
  399. if (write_zeros(d, part_start + data_start, spc, zbuf, zchunk) != 0)
  400. goto out;
  401. } else {
  402. if (write_zeros(d, part_start + root_start, root_dir_sectors,
  403. zbuf, zchunk) != 0)
  404. goto out;
  405. }
  406. rc = 0;
  407. out:
  408. if (zpages) {
  409. unsigned int k;
  410. bkl_lock();
  411. for (k = 0; k < zpages; k++)
  412. mm_free_page((unsigned int)zbuf + k * PAGE_SIZE);
  413. bkl_unlock();
  414. }
  415. return rc;
  416. }
  417. int fat_format(blk_dev_t *d)
  418. {
  419. return fat_format_limited(d, 0);
  420. }