mkfs_vfat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mkfs_vfat: utility to create FAT32 filesystem
  4. * inspired by dosfstools
  5. *
  6. * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. //config:config MKDOSFS
  11. //config: bool "mkdosfs (7.2 kb)"
  12. //config: default y
  13. //config: help
  14. //config: Utility to create FAT32 filesystems.
  15. //config:
  16. //config:config MKFS_VFAT
  17. //config: bool "mkfs.vfat (7.2 kb)"
  18. //config: default y
  19. //config: help
  20. //config: Alias to "mkdosfs".
  21. // APPLET_ODDNAME:name main location suid_type help
  22. //applet:IF_MKDOSFS( APPLET_ODDNAME(mkdosfs, mkfs_vfat, BB_DIR_SBIN, BB_SUID_DROP, mkfs_vfat))
  23. //applet:IF_MKFS_VFAT(APPLET_ODDNAME(mkfs.vfat, mkfs_vfat, BB_DIR_SBIN, BB_SUID_DROP, mkfs_vfat))
  24. //kbuild:lib-$(CONFIG_MKDOSFS) += mkfs_vfat.o
  25. //kbuild:lib-$(CONFIG_MKFS_VFAT) += mkfs_vfat.o
  26. //usage:#define mkfs_vfat_trivial_usage
  27. //usage: "[-v] [-n LABEL] BLOCKDEV [KBYTES]"
  28. /* Accepted but ignored:
  29. "[-c] [-C] [-I] [-l bad-block-file] [-b backup-boot-sector] "
  30. "[-m boot-msg-file] [-i volume-id] "
  31. "[-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs] "
  32. "[-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors] "
  33. */
  34. //usage:#define mkfs_vfat_full_usage "\n\n"
  35. //usage: "Make a FAT32 filesystem\n"
  36. /* //usage: "\n -c Check device for bad blocks" */
  37. //usage: "\n -v Verbose"
  38. /* //usage: "\n -I Allow to use entire disk device (e.g. /dev/hda)" */
  39. //usage: "\n -n LBL Volume label"
  40. #include "libbb.h"
  41. #include <linux/hdreg.h> /* HDIO_GETGEO */
  42. #include <linux/fd.h> /* FDGETPRM */
  43. #include <sys/mount.h> /* BLKSSZGET */
  44. #if !defined(BLKSSZGET)
  45. # define BLKSSZGET _IO(0x12, 104)
  46. #endif
  47. //#include <linux/msdos_fs.h>
  48. #define SECTOR_SIZE 512
  49. #define SECTORS_PER_BLOCK (BLOCK_SIZE / SECTOR_SIZE)
  50. // M$ says the high 4 bits of a FAT32 FAT entry are reserved
  51. #define EOF_FAT32 0x0FFFFFF8
  52. #define BAD_FAT32 0x0FFFFFF7
  53. #define MAX_CLUST_32 0x0FFFFFF0
  54. #define ATTR_VOLUME 8
  55. #define NUM_FATS 2
  56. /* FAT32 filesystem looks like this:
  57. * sector -nn...-1: "hidden" sectors, all sectors before this partition
  58. * (-h hidden-sectors sets it. Useful only for boot loaders,
  59. * they need to know _disk_ offset in order to be able to correctly
  60. * address sectors relative to start of disk)
  61. * sector 0: boot sector
  62. * sector 1: info sector
  63. * sector 2: set aside for boot code which didn't fit into sector 0
  64. * ...(zero-filled sectors)...
  65. * sector B: backup copy of sector 0 [B set by -b backup-boot-sector]
  66. * sector B+1: backup copy of sector 1
  67. * sector B+2: backup copy of sector 2
  68. * ...(zero-filled sectors)...
  69. * sector R: FAT#1 [R set by -R reserved-sectors]
  70. * ...(FAT#1)...
  71. * sector R+fat_size: FAT#2
  72. * ...(FAT#2)...
  73. * sector R+fat_size*2: cluster #2
  74. * ...(cluster #2)...
  75. * sector R+fat_size*2+clust_size: cluster #3
  76. * ...(the rest is filled by clusters till the end)...
  77. */
  78. enum {
  79. // Perhaps this should remain constant
  80. info_sector_number = 1,
  81. // TODO: make these cmdline options
  82. // dont forget sanity check: backup_boot_sector + 3 <= reserved_sect
  83. backup_boot_sector = 3,
  84. reserved_sect = 6,
  85. };
  86. // how many blocks we try to read while testing
  87. #define TEST_BUFFER_BLOCKS 16
  88. struct msdos_dir_entry {
  89. char name[11]; /* 000 name and extension */
  90. uint8_t attr; /* 00b attribute bits */
  91. uint8_t lcase; /* 00c case for base and extension */
  92. uint8_t ctime_cs; /* 00d creation time, centiseconds (0-199) */
  93. uint16_t ctime; /* 00e creation time */
  94. uint16_t cdate; /* 010 creation date */
  95. uint16_t adate; /* 012 last access date */
  96. uint16_t starthi; /* 014 high 16 bits of cluster in FAT32 */
  97. uint16_t time; /* 016 time */
  98. uint16_t date; /* 018 date */
  99. uint16_t start; /* 01a first cluster */
  100. uint32_t size; /* 01c file size in bytes */
  101. } PACKED;
  102. /* Example of boot sector's beginning:
  103. 0000 eb 58 90 4d 53 57 49 4e 34 2e 31 00 02 08 26 00 |...MSWIN4.1...&.|
  104. 0010 02 00 00 00 00 f8 00 00 3f 00 ff 00 3f 00 00 00 |........?...?...|
  105. 0020 54 9b d0 00 0d 34 00 00 00 00 00 00 02 00 00 00 |T....4..........|
  106. 0030 01 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  107. 0040 80 00 29 71 df 51 e0 4e 4f 20 4e 41 4d 45 20 20 |..)q.Q.NO NAME |
  108. 0050 20 20 46 41 54 33 32 20 20 20 33 c9 8e d1 bc f4 | FAT32 3.....|
  109. */
  110. struct msdos_volume_info { /* (offsets are relative to start of boot sector) */
  111. uint8_t drive_number; /* 040 BIOS drive number */
  112. uint8_t reserved; /* 041 unused */
  113. uint8_t ext_boot_sign; /* 042 0x29 if fields below exist (DOS 3.3+) */
  114. uint32_t volume_id32; /* 043 volume ID number */
  115. char volume_label[11];/* 047 volume label */
  116. char fs_type[8]; /* 052 typically "FATnn" */
  117. } PACKED; /* 05a end. Total size 26 (0x1a) bytes */
  118. struct msdos_boot_sector {
  119. /* We use strcpy to fill both, and gcc-4.4.x complains if they are separate */
  120. char boot_jump_and_sys_id[3+8]; /* 000 short or near jump instruction */
  121. /*char system_id[8];*/ /* 003 name - can be used to special case partition manager volumes */
  122. uint16_t bytes_per_sect; /* 00b bytes per logical sector */
  123. uint8_t sect_per_clust; /* 00d sectors/cluster */
  124. uint16_t reserved_sect; /* 00e reserved sectors (sector offset of 1st FAT relative to volume start) */
  125. uint8_t fats; /* 010 number of FATs */
  126. uint16_t dir_entries; /* 011 root directory entries */
  127. uint16_t volume_size_sect; /* 013 volume size in sectors */
  128. uint8_t media_byte; /* 015 media code */
  129. uint16_t sect_per_fat; /* 016 sectors/FAT */
  130. uint16_t sect_per_track; /* 018 sectors per track */
  131. uint16_t heads; /* 01a number of heads */
  132. uint32_t hidden; /* 01c hidden sectors (sector offset of volume within physical disk) */
  133. uint32_t fat32_volume_size_sect; /* 020 volume size in sectors (if volume_size_sect == 0) */
  134. uint32_t fat32_sect_per_fat; /* 024 sectors/FAT */
  135. uint16_t fat32_flags; /* 028 bit 8: fat mirroring, low 4: active fat */
  136. uint8_t fat32_version[2]; /* 02a major, minor filesystem version (I see 0,0) */
  137. uint32_t fat32_root_cluster; /* 02c first cluster in root directory */
  138. uint16_t fat32_info_sector; /* 030 filesystem info sector (usually 1) */
  139. uint16_t fat32_backup_boot; /* 032 backup boot sector (usually 6) */
  140. uint32_t reserved2[3]; /* 034 unused */
  141. struct msdos_volume_info vi; /* 040 */
  142. char boot_code[0x200 - 0x5a - 2]; /* 05a */
  143. #define BOOT_SIGN 0xAA55
  144. uint16_t boot_sign; /* 1fe */
  145. } PACKED;
  146. #define FAT_FSINFO_SIG1 0x41615252
  147. #define FAT_FSINFO_SIG2 0x61417272
  148. struct fat32_fsinfo {
  149. uint32_t signature1; /* 0x52,0x52,0x41,0x61, "RRaA" */
  150. uint32_t reserved1[128 - 8];
  151. uint32_t signature2; /* 0x72,0x72,0x61,0x41, "rrAa" */
  152. uint32_t free_clusters; /* free cluster count. -1 if unknown */
  153. uint32_t next_cluster; /* most recently allocated cluster */
  154. uint32_t reserved2[3];
  155. uint16_t reserved3; /* 1fc */
  156. uint16_t boot_sign; /* 1fe */
  157. } PACKED;
  158. struct bug_check {
  159. char BUG1[sizeof(struct msdos_dir_entry ) == 0x20 ? 1 : -1];
  160. char BUG2[sizeof(struct msdos_volume_info) == 0x1a ? 1 : -1];
  161. char BUG3[sizeof(struct msdos_boot_sector) == 0x200 ? 1 : -1];
  162. char BUG4[sizeof(struct fat32_fsinfo ) == 0x200 ? 1 : -1];
  163. };
  164. static const char boot_code[] ALIGN1 =
  165. "\x0e" /* 05a: push cs */
  166. "\x1f" /* 05b: pop ds */
  167. "\xbe\x77\x7c" /* write_msg: mov si, offset message_txt */
  168. "\xac" /* 05f: lodsb */
  169. "\x22\xc0" /* 060: and al, al */
  170. "\x74\x0b" /* 062: jz key_press */
  171. "\x56" /* 064: push si */
  172. "\xb4\x0e" /* 065: mov ah, 0eh */
  173. "\xbb\x07\x00" /* 067: mov bx, 0007h */
  174. "\xcd\x10" /* 06a: int 10h */
  175. "\x5e" /* 06c: pop si */
  176. "\xeb\xf0" /* 06d: jmp write_msg */
  177. "\x32\xe4" /* key_press: xor ah, ah */
  178. "\xcd\x16" /* 071: int 16h */
  179. "\xcd\x19" /* 073: int 19h */
  180. "\xeb\xfe" /* foo: jmp foo */
  181. /* 077: message_txt: */
  182. "This is not a bootable disk\r\n";
  183. #define MARK_CLUSTER(cluster, value) \
  184. ((uint32_t *)fat)[cluster] = SWAP_LE32(value)
  185. /* compat:
  186. * mkdosfs 2.11 (12 Mar 2005)
  187. * Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file]
  188. * [-b backup-boot-sector]
  189. * [-m boot-msg-file] [-n volume-name] [-i volume-id]
  190. * [-s sectors-per-cluster] [-S logical-sector-size]
  191. * [-f number-of-FATs]
  192. * [-h hidden-sectors] [-F fat-size] [-r root-dir-entries]
  193. * [-R reserved-sectors]
  194. * /dev/name [blocks]
  195. */
  196. int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  197. int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
  198. {
  199. static const char NO_NAME_11[] = "NO NAME ";
  200. struct stat st;
  201. const char *arg_volume_label = NO_NAME_11; //default
  202. char volume_label11[12];
  203. char *buf;
  204. char *device_name;
  205. uoff_t volume_size_bytes;
  206. uoff_t volume_size_sect;
  207. uint32_t total_clust;
  208. uint32_t volume_id;
  209. int dev;
  210. unsigned bytes_per_sect;
  211. unsigned sect_per_fat;
  212. unsigned opts;
  213. uint16_t sect_per_track;
  214. uint8_t media_byte;
  215. uint8_t sect_per_clust;
  216. uint8_t heads;
  217. enum {
  218. OPT_A = 1 << 0, // [IGNORED] atari format
  219. OPT_b = 1 << 1, // [IGNORED] location of backup boot sector
  220. OPT_c = 1 << 2, // [IGNORED] check filesystem
  221. OPT_C = 1 << 3, // [IGNORED] create a new file
  222. OPT_f = 1 << 4, // [IGNORED] number of FATs
  223. OPT_F = 1 << 5, // [IGNORED, implied 32] choose FAT size
  224. OPT_h = 1 << 6, // [IGNORED] number of hidden sectors
  225. OPT_I = 1 << 7, // [IGNORED] don't bark at entire disk devices
  226. OPT_i = 1 << 8, // [IGNORED] volume ID
  227. OPT_l = 1 << 9, // [IGNORED] bad block filename
  228. OPT_m = 1 << 10, // [IGNORED] message file
  229. OPT_n = 1 << 11, // volume label
  230. OPT_r = 1 << 12, // [IGNORED] root directory entries
  231. OPT_R = 1 << 13, // [IGNORED] number of reserved sectors
  232. OPT_s = 1 << 14, // [IGNORED] sectors per cluster
  233. OPT_S = 1 << 15, // [IGNORED] sector size
  234. OPT_v = 1 << 16, // verbose
  235. };
  236. opts = getopt32(argv, "^"
  237. "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v"
  238. "\0" "-1", //:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c
  239. /*b*/NULL, /*f*/NULL, /*F*/NULL, /*h*/NULL, /*i*/NULL,
  240. /*l*/NULL, /*m*/NULL, /*n*/&arg_volume_label,
  241. /*r*/NULL, /*R*/NULL, /*s*/NULL, /*S*/NULL);
  242. argv += optind;
  243. // cache device name
  244. device_name = argv[0];
  245. // default volume ID = creation time
  246. volume_id = time(NULL);
  247. // truncate to exactly 11 chars, pad with spaces
  248. sprintf(volume_label11, "%-11.11s", arg_volume_label);
  249. dev = xopen(device_name, O_RDWR);
  250. xfstat(dev, &st, device_name);
  251. //
  252. // Get image size and sector size
  253. //
  254. bytes_per_sect = SECTOR_SIZE;
  255. if (!S_ISBLK(st.st_mode)) {
  256. if (!S_ISREG(st.st_mode)) {
  257. if (!argv[1])
  258. bb_simple_error_msg_and_die("image size must be specified");
  259. }
  260. // not a block device, skip bad sectors check
  261. opts &= ~OPT_c;
  262. } else {
  263. int min_bytes_per_sect;
  264. #if 0
  265. unsigned device_num;
  266. // for true block devices we do check sanity
  267. device_num = st.st_rdev & 0xff3f;
  268. // do we allow to format the whole disk device?
  269. if (!(opts & OPT_I) && (
  270. device_num == 0x0300 || // hda, hdb
  271. (device_num & 0xff0f) == 0x0800 || // sd
  272. device_num == 0x0d00 || // xd
  273. device_num == 0x1600 ) // hdc, hdd
  274. )
  275. bb_error_msg_and_die("will not try to make filesystem on full-disk device (use -I if wanted)");
  276. // can't work on mounted filesystems
  277. if (find_mount_point(device_name, 0))
  278. bb_error_msg_and_die("can't format mounted filesystem");
  279. #endif
  280. // get true sector size
  281. // (parameter must be int*, not long* or size_t*)
  282. xioctl(dev, BLKSSZGET, &min_bytes_per_sect);
  283. if (min_bytes_per_sect > SECTOR_SIZE) {
  284. bytes_per_sect = min_bytes_per_sect;
  285. bb_error_msg("for this device sector size is %u", min_bytes_per_sect);
  286. }
  287. }
  288. volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024, /*extend:*/ 1);
  289. volume_size_sect = volume_size_bytes / bytes_per_sect;
  290. //
  291. // Find out or guess media parameters
  292. //
  293. media_byte = 0xf8;
  294. heads = 255;
  295. sect_per_track = 63;
  296. sect_per_clust = 1;
  297. {
  298. struct hd_geometry geometry;
  299. // size (in sectors), sect (per track), head
  300. struct floppy_struct param;
  301. // N.B. whether to use HDIO_GETGEO or HDIO_REQ?
  302. if (ioctl(dev, HDIO_GETGEO, &geometry) == 0
  303. && geometry.sectors
  304. && geometry.heads
  305. ) {
  306. // hard drive
  307. sect_per_track = geometry.sectors;
  308. heads = geometry.heads;
  309. set_cluster_size:
  310. /* For FAT32, try to do the same as M$'s format command
  311. * (see http://www.win.tue.nl/~aeb/linux/fs/fat/fatgen103.pdf p. 20):
  312. * fs size <= 260M: 0.5k clusters
  313. * fs size <= 8G: 4k clusters
  314. * fs size <= 16G: 8k clusters
  315. * fs size > 16G: 16k clusters
  316. */
  317. sect_per_clust = 1;
  318. if (volume_size_bytes >= 260*1024*1024) {
  319. sect_per_clust = 8;
  320. /* fight gcc: */
  321. /* "error: integer overflow in expression" */
  322. /* "error: right shift count >= width of type" */
  323. if (sizeof(off_t) > 4) {
  324. unsigned t = (volume_size_bytes >> 31 >> 1);
  325. if (t >= 8/4)
  326. sect_per_clust = 16;
  327. if (t >= 16/4)
  328. sect_per_clust = 32;
  329. }
  330. }
  331. } else {
  332. // floppy, loop, or regular file
  333. int not_floppy = ioctl(dev, FDGETPRM, &param);
  334. if (not_floppy == 0) {
  335. // floppy disk
  336. sect_per_track = param.sect;
  337. heads = param.head;
  338. volume_size_sect = param.size;
  339. volume_size_bytes = param.size * SECTOR_SIZE;
  340. }
  341. // setup the media descriptor byte
  342. switch (volume_size_sect) {
  343. case 2*360: // 5.25", 2, 9, 40 - 360K
  344. media_byte = 0xfd;
  345. break;
  346. case 2*720: // 3.5", 2, 9, 80 - 720K
  347. case 2*1200: // 5.25", 2, 15, 80 - 1200K
  348. media_byte = 0xf9;
  349. break;
  350. default: // anything else
  351. if (not_floppy)
  352. goto set_cluster_size;
  353. case 2*1440: // 3.5", 2, 18, 80 - 1440K
  354. case 2*2880: // 3.5", 2, 36, 80 - 2880K
  355. media_byte = 0xf0;
  356. break;
  357. }
  358. // not floppy, but size matches floppy exactly.
  359. // perhaps it is a floppy image.
  360. // we already set media_byte as if it is a floppy,
  361. // now set sect_per_track and heads.
  362. heads = 2;
  363. sect_per_track = (unsigned)volume_size_sect / 160;
  364. if (sect_per_track < 9)
  365. sect_per_track = 9;
  366. }
  367. }
  368. //
  369. // Calculate number of clusters, sectors/cluster, sectors/FAT
  370. // (an initial guess for sect_per_clust should already be set)
  371. //
  372. // "mkdosfs -v -F 32 image5k 5" is the minimum:
  373. // 2 sectors for FATs and 2 data sectors
  374. if ((off_t)(volume_size_sect - reserved_sect) < 4)
  375. bb_simple_error_msg_and_die("the image is too small for FAT32");
  376. sect_per_fat = 1;
  377. while (1) {
  378. while (1) {
  379. int spf_adj;
  380. uoff_t tcl = (volume_size_sect - reserved_sect - NUM_FATS * sect_per_fat) / sect_per_clust;
  381. // tcl may be > MAX_CLUST_32 here, but it may be
  382. // because sect_per_fat is underestimated,
  383. // and with increased sect_per_fat it still may become
  384. // <= MAX_CLUST_32. Therefore, we do not check
  385. // against MAX_CLUST_32, but against a bigger const:
  386. if (tcl > 0x80ffffff)
  387. goto next;
  388. total_clust = tcl; // fits in uint32_t
  389. // Every cluster needs 4 bytes in FAT. +2 entries since
  390. // FAT has space for non-existent clusters 0 and 1.
  391. // Let's see how many sectors that needs.
  392. //May overflow at "*4":
  393. //spf_adj = ((total_clust+2) * 4 + bytes_per_sect-1) / bytes_per_sect - sect_per_fat;
  394. //Same in the more obscure, non-overflowing form:
  395. spf_adj = ((total_clust+2) + (bytes_per_sect/4)-1) / (bytes_per_sect/4) - sect_per_fat;
  396. #if 0
  397. bb_error_msg("sect_per_clust:%u sect_per_fat:%u total_clust:%u",
  398. sect_per_clust, sect_per_fat, (int)tcl);
  399. bb_error_msg("adjust to sect_per_fat:%d", spf_adj);
  400. #endif
  401. if (spf_adj <= 0) {
  402. // do not need to adjust sect_per_fat.
  403. // so, was total_clust too big after all?
  404. if (total_clust <= MAX_CLUST_32)
  405. goto found_total_clust; // no
  406. // yes, total_clust is _a bit_ too big
  407. goto next;
  408. }
  409. // adjust sect_per_fat, go back and recalc total_clust
  410. // (note: just "sect_per_fat += spf_adj" isn't ok)
  411. sect_per_fat += ((unsigned)spf_adj / 2) | 1;
  412. }
  413. next:
  414. if (sect_per_clust == 128)
  415. bb_simple_error_msg_and_die("can't make FAT32 with >128 sectors/cluster");
  416. sect_per_clust *= 2;
  417. sect_per_fat = (sect_per_fat / 2) | 1;
  418. }
  419. found_total_clust:
  420. //
  421. // Print info
  422. //
  423. if (opts & OPT_v) {
  424. fprintf(stderr,
  425. "Device '%s':\n"
  426. "heads:%u, sectors/track:%u, bytes/sector:%u\n"
  427. "media descriptor:%02x\n"
  428. "total sectors:%"OFF_FMT"u, clusters:%u, sectors/cluster:%u\n"
  429. "FATs:2, sectors/FAT:%u\n"
  430. "volumeID:%08x, label:'%s'\n",
  431. device_name,
  432. heads, sect_per_track, bytes_per_sect,
  433. (int)media_byte,
  434. volume_size_sect, (int)total_clust, (int)sect_per_clust,
  435. sect_per_fat,
  436. (int)volume_id, volume_label11
  437. );
  438. }
  439. //
  440. // Write filesystem image sequentially (no seeking)
  441. //
  442. {
  443. // (a | b) is poor man's max(a, b)
  444. unsigned bufsize = reserved_sect;
  445. //bufsize |= sect_per_fat; // can be quite large
  446. bufsize |= 2; // use this instead
  447. bufsize |= sect_per_clust;
  448. buf = xzalloc(bufsize * bytes_per_sect);
  449. }
  450. { // boot and fsinfo sectors, and their copies
  451. struct msdos_boot_sector *boot_blk = (void*)buf;
  452. struct fat32_fsinfo *info = (void*)(buf + bytes_per_sect);
  453. strcpy(boot_blk->boot_jump_and_sys_id, "\xeb\x58\x90" "mkdosfs");
  454. STORE_LE(boot_blk->bytes_per_sect, bytes_per_sect);
  455. STORE_LE(boot_blk->sect_per_clust, sect_per_clust);
  456. // cast in needed on big endian to suppress a warning
  457. STORE_LE(boot_blk->reserved_sect, (uint16_t)reserved_sect);
  458. STORE_LE(boot_blk->fats, 2);
  459. //STORE_LE(boot_blk->dir_entries, 0); // for FAT32, stays 0
  460. if (volume_size_sect <= 0xffff)
  461. STORE_LE(boot_blk->volume_size_sect, volume_size_sect);
  462. STORE_LE(boot_blk->media_byte, media_byte);
  463. // wrong: this would make Linux think that it's fat12/16:
  464. //if (sect_per_fat <= 0xffff)
  465. // STORE_LE(boot_blk->sect_per_fat, sect_per_fat);
  466. // works:
  467. //STORE_LE(boot_blk->sect_per_fat, 0);
  468. STORE_LE(boot_blk->sect_per_track, sect_per_track);
  469. STORE_LE(boot_blk->heads, heads);
  470. //STORE_LE(boot_blk->hidden, 0);
  471. STORE_LE(boot_blk->fat32_volume_size_sect, volume_size_sect);
  472. STORE_LE(boot_blk->fat32_sect_per_fat, sect_per_fat);
  473. //STORE_LE(boot_blk->fat32_flags, 0);
  474. //STORE_LE(boot_blk->fat32_version[2], 0,0);
  475. STORE_LE(boot_blk->fat32_root_cluster, 2);
  476. STORE_LE(boot_blk->fat32_info_sector, info_sector_number);
  477. STORE_LE(boot_blk->fat32_backup_boot, backup_boot_sector);
  478. //STORE_LE(boot_blk->reserved2[3], 0,0,0);
  479. STORE_LE(boot_blk->vi.ext_boot_sign, 0x29);
  480. STORE_LE(boot_blk->vi.volume_id32, volume_id);
  481. memcpy(boot_blk->vi.fs_type, "FAT32 ", sizeof(boot_blk->vi.fs_type));
  482. memcpy(boot_blk->vi.volume_label, volume_label11, 11);
  483. memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code));
  484. STORE_LE(boot_blk->boot_sign, BOOT_SIGN);
  485. STORE_LE(info->signature1, FAT_FSINFO_SIG1);
  486. STORE_LE(info->signature2, FAT_FSINFO_SIG2);
  487. // we've allocated cluster 2 for the root dir
  488. STORE_LE(info->free_clusters, (total_clust - 1));
  489. STORE_LE(info->next_cluster, 2);
  490. STORE_LE(info->boot_sign, BOOT_SIGN);
  491. // 1st copy
  492. xwrite(dev, buf, bytes_per_sect * backup_boot_sector);
  493. // 2nd copy and possibly zero sectors
  494. xwrite(dev, buf, bytes_per_sect * (reserved_sect - backup_boot_sector));
  495. }
  496. { // file allocation tables
  497. unsigned i,j;
  498. unsigned char *fat = (void*)buf;
  499. memset(buf, 0, bytes_per_sect * 2);
  500. // initial FAT entries
  501. MARK_CLUSTER(0, 0x0fffff00 | media_byte);
  502. MARK_CLUSTER(1, 0xffffffff);
  503. // mark cluster 2 as EOF (used for root dir)
  504. MARK_CLUSTER(2, EOF_FAT32);
  505. for (i = 0; i < NUM_FATS; i++) {
  506. xwrite(dev, buf, bytes_per_sect);
  507. for (j = 1; j < sect_per_fat; j++)
  508. xwrite(dev, buf + bytes_per_sect, bytes_per_sect);
  509. }
  510. }
  511. // root directory
  512. // empty directory is just a set of zero bytes
  513. memset(buf, 0, sect_per_clust * bytes_per_sect);
  514. // not "NO NAME", "NO NAME " etc?
  515. // (mkfs.fat 4.1 won't create dir entry even with explicit -n 'NO NAME',
  516. // but will create one with e.g. -n '', -n ' zZz')
  517. if (strcmp(volume_label11, NO_NAME_11) != 0) {
  518. // create dir entry for volume label
  519. struct msdos_dir_entry *de;
  520. #if 0
  521. struct tm tm_time;
  522. uint16_t t, d;
  523. #endif
  524. de = (void*)buf;
  525. memcpy(de->name, volume_label11, 11);
  526. STORE_LE(de->attr, ATTR_VOLUME);
  527. #if 0
  528. localtime_r(&create_time, &tm_time);
  529. t = (tm_time.tm_sec >> 1) + (tm_time.tm_min << 5) + (tm_time.tm_hour << 11);
  530. d = tm_time.tm_mday + ((tm_time.tm_mon+1) << 5) + ((tm_time.tm_year-80) << 9);
  531. STORE_LE(de->time, t);
  532. STORE_LE(de->date, d);
  533. //STORE_LE(de->ctime_cs, 0);
  534. de->ctime = de->time;
  535. de->cdate = de->date;
  536. de->adate = de->date;
  537. #endif
  538. }
  539. xwrite(dev, buf, sect_per_clust * bytes_per_sect);
  540. #if 0
  541. if (opts & OPT_c) {
  542. uoff_t volume_size_blocks;
  543. unsigned start_data_sector;
  544. unsigned start_data_block;
  545. unsigned badblocks = 0;
  546. int try, got;
  547. off_t currently_testing;
  548. char *blkbuf = xmalloc(BLOCK_SIZE * TEST_BUFFER_BLOCKS);
  549. volume_size_blocks = (volume_size_bytes >> BLOCK_SIZE_BITS);
  550. // N.B. the two following vars are in hard sectors, i.e. SECTOR_SIZE byte sectors!
  551. start_data_sector = (reserved_sect + NUM_FATS * sect_per_fat) * (bytes_per_sect / SECTOR_SIZE);
  552. start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) / SECTORS_PER_BLOCK;
  553. bb_error_msg("searching for bad blocks");
  554. currently_testing = 0;
  555. try = TEST_BUFFER_BLOCKS;
  556. while (currently_testing < volume_size_blocks) {
  557. if (currently_testing + try > volume_size_blocks)
  558. try = volume_size_blocks - currently_testing;
  559. // perform a test on a block. return the number of blocks
  560. // that could be read successfully.
  561. // seek to the correct location
  562. xlseek(dev, currently_testing * BLOCK_SIZE, SEEK_SET);
  563. // try reading
  564. got = read(dev, blkbuf, try * BLOCK_SIZE);
  565. if (got < 0)
  566. got = 0;
  567. if (got & (BLOCK_SIZE - 1))
  568. bb_error_msg("unexpected values in do_check: probably bugs");
  569. got /= BLOCK_SIZE;
  570. currently_testing += got;
  571. if (got == try) {
  572. try = TEST_BUFFER_BLOCKS;
  573. continue;
  574. }
  575. try = 1;
  576. if (currently_testing < start_data_block)
  577. bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
  578. // mark all of the sectors in the block as bad
  579. for (i = 0; i < SECTORS_PER_BLOCK; i++) {
  580. int cluster = (currently_testing * SECTORS_PER_BLOCK + i - start_data_sector) / (int) (sect_per_clust) / (bytes_per_sect / SECTOR_SIZE);
  581. if (cluster < 0)
  582. bb_error_msg_and_die("invalid cluster number in mark_sector: probably bug!");
  583. MARK_CLUSTER(cluster, BAD_FAT32);
  584. }
  585. badblocks++;
  586. currently_testing++;
  587. }
  588. free(blkbuf);
  589. if (badblocks)
  590. bb_error_msg("%d bad block(s)", badblocks);
  591. }
  592. #endif
  593. // cleanup
  594. if (ENABLE_FEATURE_CLEAN_UP) {
  595. free(buf);
  596. close(dev);
  597. }
  598. return 0;
  599. }