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