fatformat.c 9.8 KB

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