fatformat.c 7.3 KB

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