fatformat.c 8.6 KB

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