fatformat.c 12 KB

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