blob: ff3e4165a3f1d1e6d003a3799dfadcb81ba22295 [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 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 */
Denys Vlasenko860d2bb2009-07-10 18:37:06 +020010#include "libbb.h"
11
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +000012#include <linux/hdreg.h> /* HDIO_GETGEO */
13#include <linux/fd.h> /* FDGETPRM */
Denys Vlasenkoda49f582009-07-08 02:58:38 +020014#include <sys/mount.h> /* BLKSSZGET */
15#if !defined(BLKSSZGET)
16# define BLKSSZGET _IO(0x12, 104)
17#endif
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +000018//#include <linux/msdos_fs.h>
19
20#define SECTOR_SIZE 512
21
22#define SECTORS_PER_BLOCK (BLOCK_SIZE / SECTOR_SIZE)
23
24// M$ says the high 4 bits of a FAT32 FAT entry are reserved
25#define EOF_FAT32 0x0FFFFFF8
26#define BAD_FAT32 0x0FFFFFF7
27#define MAX_CLUST_32 0x0FFFFFF0
28
29#define ATTR_VOLUME 8
30
31#define NUM_FATS 2
32
33/* FAT32 filesystem looks like this:
34 * sector -nn...-1: "hidden" sectors, all sectors before this partition
35 * (-h hidden-sectors sets it. Useful only for boot loaders,
36 * they need to know _disk_ offset in order to be able to correctly
37 * address sectors relative to start of disk)
38 * sector 0: boot sector
39 * sector 1: info sector
40 * sector 2: set aside for boot code which didn't fit into sector 0
41 * ...(zero-filled sectors)...
42 * sector B: backup copy of sector 0 [B set by -b backup-boot-sector]
43 * sector B+1: backup copy of sector 1
44 * sector B+2: backup copy of sector 2
45 * ...(zero-filled sectors)...
46 * sector R: FAT#1 [R set by -R reserved-sectors]
47 * ...(FAT#1)...
48 * sector R+fat_size: FAT#2
49 * ...(FAT#2)...
50 * sector R+fat_size*2: cluster #2
51 * ...(cluster #2)...
52 * sector R+fat_size*2+clust_size: cluster #3
53 * ...(the rest is filled by clusters till the end)...
54 */
55
56enum {
57// Perhaps this should remain constant
58 info_sector_number = 1,
59// TODO: make these cmdline options
60// dont forget sanity check: backup_boot_sector + 3 <= reserved_sect
61 backup_boot_sector = 3,
62 reserved_sect = 6,
63};
64
65// how many blocks we try to read while testing
66#define TEST_BUFFER_BLOCKS 16
67
68struct msdos_dir_entry {
69 char name[11]; /* 000 name and extension */
70 uint8_t attr; /* 00b attribute bits */
71 uint8_t lcase; /* 00c case for base and extension */
72 uint8_t ctime_cs; /* 00d creation time, centiseconds (0-199) */
73 uint16_t ctime; /* 00e creation time */
74 uint16_t cdate; /* 010 creation date */
75 uint16_t adate; /* 012 last access date */
76 uint16_t starthi; /* 014 high 16 bits of cluster in FAT32 */
77 uint16_t time; /* 016 time */
78 uint16_t date; /* 018 date */
79 uint16_t start; /* 01a first cluster */
80 uint32_t size; /* 01c file size in bytes */
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020081} PACKED;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +000082
83/* Example of boot sector's beginning:
840000 eb 58 90 4d 53 57 49 4e 34 2e 31 00 02 08 26 00 |...MSWIN4.1...&.|
850010 02 00 00 00 00 f8 00 00 3f 00 ff 00 3f 00 00 00 |........?...?...|
860020 54 9b d0 00 0d 34 00 00 00 00 00 00 02 00 00 00 |T....4..........|
870030 01 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
880040 80 00 29 71 df 51 e0 4e 4f 20 4e 41 4d 45 20 20 |..)q.Q.NO NAME |
890050 20 20 46 41 54 33 32 20 20 20 33 c9 8e d1 bc f4 | FAT32 3.....|
90*/
91struct msdos_volume_info { /* (offsets are relative to start of boot sector) */
92 uint8_t drive_number; /* 040 BIOS drive number */
93 uint8_t reserved; /* 041 unused */
94 uint8_t ext_boot_sign; /* 042 0x29 if fields below exist (DOS 3.3+) */
95 uint32_t volume_id32; /* 043 volume ID number */
96 char volume_label[11];/* 047 volume label */
97 char fs_type[8]; /* 052 typically "FATnn" */
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020098} PACKED; /* 05a end. Total size 26 (0x1a) bytes */
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +000099
100struct msdos_boot_sector {
101 char boot_jump[3]; /* 000 short or near jump instruction */
102 char system_id[8]; /* 003 name - can be used to special case partition manager volumes */
103 uint16_t bytes_per_sect; /* 00b bytes per logical sector */
104 uint8_t sect_per_clust; /* 00d sectors/cluster */
105 uint16_t reserved_sect; /* 00e reserved sectors (sector offset of 1st FAT relative to volume start) */
106 uint8_t fats; /* 010 number of FATs */
107 uint16_t dir_entries; /* 011 root directory entries */
108 uint16_t volume_size_sect; /* 013 volume size in sectors */
109 uint8_t media_byte; /* 015 media code */
110 uint16_t sect_per_fat; /* 016 sectors/FAT */
111 uint16_t sect_per_track; /* 018 sectors per track */
112 uint16_t heads; /* 01a number of heads */
113 uint32_t hidden; /* 01c hidden sectors (sector offset of volume within physical disk) */
114 uint32_t fat32_volume_size_sect; /* 020 volume size in sectors (if volume_size_sect == 0) */
115 uint32_t fat32_sect_per_fat; /* 024 sectors/FAT */
116 uint16_t fat32_flags; /* 028 bit 8: fat mirroring, low 4: active fat */
117 uint8_t fat32_version[2]; /* 02a major, minor filesystem version (I see 0,0) */
118 uint32_t fat32_root_cluster; /* 02c first cluster in root directory */
119 uint16_t fat32_info_sector; /* 030 filesystem info sector (usually 1) */
120 uint16_t fat32_backup_boot; /* 032 backup boot sector (usually 6) */
121 uint32_t reserved2[3]; /* 034 unused */
122 struct msdos_volume_info vi; /* 040 */
123 char boot_code[0x200 - 0x5a - 2]; /* 05a */
124#define BOOT_SIGN 0xAA55
125 uint16_t boot_sign; /* 1fe */
Denys Vlasenko8dc0e192009-09-16 00:58:11 +0200126} PACKED;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000127
128#define FAT_FSINFO_SIG1 0x41615252
129#define FAT_FSINFO_SIG2 0x61417272
130struct fat32_fsinfo {
131 uint32_t signature1; /* 0x52,0x52,0x41,0x61, "RRaA" */
132 uint32_t reserved1[128 - 8];
133 uint32_t signature2; /* 0x72,0x72,0x61,0x41, "rrAa" */
134 uint32_t free_clusters; /* free cluster count. -1 if unknown */
135 uint32_t next_cluster; /* most recently allocated cluster */
136 uint32_t reserved2[3];
137 uint16_t reserved3; /* 1fc */
138 uint16_t boot_sign; /* 1fe */
Denys Vlasenko8dc0e192009-09-16 00:58:11 +0200139} PACKED;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000140
141struct bug_check {
142 char BUG1[sizeof(struct msdos_dir_entry ) == 0x20 ? 1 : -1];
143 char BUG2[sizeof(struct msdos_volume_info) == 0x1a ? 1 : -1];
144 char BUG3[sizeof(struct msdos_boot_sector) == 0x200 ? 1 : -1];
145 char BUG4[sizeof(struct fat32_fsinfo ) == 0x200 ? 1 : -1];
146};
147
148static const char boot_code[] ALIGN1 =
149 "\x0e" /* 05a: push cs */
150 "\x1f" /* 05b: pop ds */
151 "\xbe\x77\x7c" /* write_msg: mov si, offset message_txt */
152 "\xac" /* 05f: lodsb */
153 "\x22\xc0" /* 060: and al, al */
154 "\x74\x0b" /* 062: jz key_press */
155 "\x56" /* 064: push si */
156 "\xb4\x0e" /* 065: mov ah, 0eh */
157 "\xbb\x07\x00" /* 067: mov bx, 0007h */
158 "\xcd\x10" /* 06a: int 10h */
159 "\x5e" /* 06c: pop si */
160 "\xeb\xf0" /* 06d: jmp write_msg */
161 "\x32\xe4" /* key_press: xor ah, ah */
162 "\xcd\x16" /* 071: int 16h */
163 "\xcd\x19" /* 073: int 19h */
164 "\xeb\xfe" /* foo: jmp foo */
165 /* 077: message_txt: */
166 "This is not a bootable disk\r\n";
167
168
Denis Vlasenko14ee4e62009-03-28 02:28:58 +0000169#define MARK_CLUSTER(cluster, value) \
Denys Vlasenko67743862010-05-09 00:13:40 +0200170 ((uint32_t *)fat)[cluster] = SWAP_LE32(value)
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000171
172void BUG_unsupported_field_size(void);
173#define STORE_LE(field, value) \
174do { \
175 if (sizeof(field) == 4) \
Denys Vlasenko67743862010-05-09 00:13:40 +0200176 field = SWAP_LE32(value); \
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000177 else if (sizeof(field) == 2) \
Denys Vlasenko67743862010-05-09 00:13:40 +0200178 field = SWAP_LE16(value); \
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000179 else if (sizeof(field) == 1) \
180 field = (value); \
181 else \
182 BUG_unsupported_field_size(); \
183} while (0)
184
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 */
196int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
197int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
198{
199 struct stat st;
200 const char *volume_label = "";
201 char *buf;
202 char *device_name;
203 uoff_t volume_size_bytes;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000204 uoff_t volume_size_sect;
205 uint32_t total_clust;
206 uint32_t volume_id;
207 int dev;
208 unsigned bytes_per_sect;
209 unsigned sect_per_fat;
Denys Vlasenko3581c622010-01-25 13:39:24 +0100210 unsigned opts;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000211 uint16_t sect_per_track;
212 uint8_t media_byte;
213 uint8_t sect_per_clust;
214 uint8_t heads;
215 enum {
216 OPT_A = 1 << 0, // [IGNORED] atari format
217 OPT_b = 1 << 1, // [IGNORED] location of backup boot sector
218 OPT_c = 1 << 2, // [IGNORED] check filesystem
219 OPT_C = 1 << 3, // [IGNORED] create a new file
220 OPT_f = 1 << 4, // [IGNORED] number of FATs
221 OPT_F = 1 << 5, // [IGNORED, implied 32] choose FAT size
222 OPT_h = 1 << 6, // [IGNORED] number of hidden sectors
223 OPT_I = 1 << 7, // [IGNORED] don't bark at entire disk devices
224 OPT_i = 1 << 8, // [IGNORED] volume ID
225 OPT_l = 1 << 9, // [IGNORED] bad block filename
226 OPT_m = 1 << 10, // [IGNORED] message file
227 OPT_n = 1 << 11, // volume label
228 OPT_r = 1 << 12, // [IGNORED] root directory entries
229 OPT_R = 1 << 13, // [IGNORED] number of reserved sectors
230 OPT_s = 1 << 14, // [IGNORED] sectors per cluster
231 OPT_S = 1 << 15, // [IGNORED] sector size
232 OPT_v = 1 << 16, // verbose
233 };
234
235 opt_complementary = "-1";//:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c";
236 opts = getopt32(argv, "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v",
237 NULL, NULL, NULL, NULL, NULL,
238 NULL, NULL, &volume_label, NULL, NULL, NULL, NULL);
239 argv += optind;
240
241 // cache device name
242 device_name = argv[0];
243 // default volume ID = creation time
244 volume_id = time(NULL);
245
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100246 dev = xopen(device_name, O_RDWR);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000247 if (fstat(dev, &st) < 0)
248 bb_simple_perror_msg_and_die(device_name);
249
250 //
251 // Get image size and sector size
252 //
253 bytes_per_sect = SECTOR_SIZE;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000254 if (!S_ISBLK(st.st_mode)) {
255 if (!S_ISREG(st.st_mode)) {
256 if (!argv[1])
257 bb_error_msg_and_die("image size must be specified");
258 }
259 // not a block device, skip bad sectors check
260 opts &= ~OPT_c;
261 } else {
262 int min_bytes_per_sect;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000263#if 0
264 unsigned device_num;
265 // for true block devices we do check sanity
266 device_num = st.st_rdev & 0xff3f;
267 // do we allow to format the whole disk device?
268 if (!(opts & OPT_I) && (
269 device_num == 0x0300 || // hda, hdb
270 (device_num & 0xff0f) == 0x0800 || // sd
271 device_num == 0x0d00 || // xd
272 device_num == 0x1600 ) // hdc, hdd
273 )
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000274 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 +0000275 // can't work on mounted filesystems
Denys Vlasenko6ae64262009-07-18 16:22:26 +0200276 if (find_mount_point(device_name, 0))
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000277 bb_error_msg_and_die("can't format mounted filesystem");
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000278#endif
279 // get true sector size
280 // (parameter must be int*, not long* or size_t*)
281 xioctl(dev, BLKSSZGET, &min_bytes_per_sect);
282 if (min_bytes_per_sect > SECTOR_SIZE) {
283 bytes_per_sect = min_bytes_per_sect;
284 bb_error_msg("for this device sector size is %u", min_bytes_per_sect);
285 }
286 }
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100287 volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024, /*extend:*/ 1);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000288 volume_size_sect = volume_size_bytes / bytes_per_sect;
289
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
302 // N.B. whether to use HDIO_GETGEO or HDIO_REQ?
303 if (ioctl(dev, HDIO_GETGEO, &geometry) == 0
304 && geometry.sectors
305 && geometry.heads
306 ) {
307 // hard drive
308 sect_per_track = geometry.sectors;
309 heads = geometry.heads;
310
311 set_cluster_size:
312 /* For FAT32, try to do the same as M$'s format command
313 * (see http://www.win.tue.nl/~aeb/linux/fs/fat/fatgen103.pdf p. 20):
314 * fs size <= 260M: 0.5k clusters
315 * fs size <= 8G: 4k clusters
316 * fs size <= 16G: 8k clusters
317 * fs size > 16G: 16k clusters
318 */
Denis Vlasenkoa2333c82009-03-28 19:08:23 +0000319 sect_per_clust = 1;
320 if (volume_size_bytes >= 260*1024*1024) {
321 sect_per_clust = 8;
322 /* fight gcc: */
323 /* "error: integer overflow in expression" */
324 /* "error: right shift count >= width of type" */
325 if (sizeof(off_t) > 4) {
326 unsigned t = (volume_size_bytes >> 31 >> 1);
327 if (t >= 8/4)
328 sect_per_clust = 16;
329 if (t >= 16/4)
330 sect_per_clust = 32;
331 }
332 }
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000333 } else {
Denis Vlasenko781b6722009-03-28 12:17:20 +0000334 // floppy, loop, or regular file
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000335 int not_floppy = ioctl(dev, FDGETPRM, &param);
336 if (not_floppy == 0) {
337 // floppy disk
338 sect_per_track = param.sect;
339 heads = param.head;
340 volume_size_sect = param.size;
341 volume_size_bytes = param.size * SECTOR_SIZE;
342 }
343 // setup the media descriptor byte
344 switch (volume_size_sect) {
345 case 2*360: // 5.25", 2, 9, 40 - 360K
346 media_byte = 0xfd;
347 break;
348 case 2*720: // 3.5", 2, 9, 80 - 720K
349 case 2*1200: // 5.25", 2, 15, 80 - 1200K
350 media_byte = 0xf9;
351 break;
352 default: // anything else
353 if (not_floppy)
354 goto set_cluster_size;
355 case 2*1440: // 3.5", 2, 18, 80 - 1440K
356 case 2*2880: // 3.5", 2, 36, 80 - 2880K
357 media_byte = 0xf0;
358 break;
359 }
360 // not floppy, but size matches floppy exactly.
361 // perhaps it is a floppy image.
362 // we already set media_byte as if it is a floppy,
363 // now set sect_per_track and heads.
364 heads = 2;
365 sect_per_track = (unsigned)volume_size_sect / 160;
366 if (sect_per_track < 9)
367 sect_per_track = 9;
368 }
369 }
370
371 //
372 // Calculate number of clusters, sectors/cluster, sectors/FAT
373 // (an initial guess for sect_per_clust should already be set)
374 //
Denis Vlasenko781b6722009-03-28 12:17:20 +0000375 // "mkdosfs -v -F 32 image5k 5" is the minimum:
376 // 2 sectors for FATs and 2 data sectors
377 if ((off_t)(volume_size_sect - reserved_sect) < 4)
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000378 bb_error_msg_and_die("the image is too small for FAT32");
379 sect_per_fat = 1;
380 while (1) {
381 while (1) {
Denis Vlasenko781b6722009-03-28 12:17:20 +0000382 int spf_adj;
Denys Vlasenko692bcff2009-11-03 14:12:04 +0100383 uoff_t tcl = (volume_size_sect - reserved_sect - NUM_FATS * sect_per_fat) / sect_per_clust;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000384 // tcl may be > MAX_CLUST_32 here, but it may be
385 // because sect_per_fat is underestimated,
386 // and with increased sect_per_fat it still may become
387 // <= MAX_CLUST_32. Therefore, we do not check
388 // against MAX_CLUST_32, but against a bigger const:
Denys Vlasenko692bcff2009-11-03 14:12:04 +0100389 if (tcl > 0x80ffffff)
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000390 goto next;
391 total_clust = tcl; // fits in uint32_t
Denys Vlasenko692bcff2009-11-03 14:12:04 +0100392 // Every cluster needs 4 bytes in FAT. +2 entries since
393 // FAT has space for non-existent clusters 0 and 1.
394 // Let's see how many sectors that needs.
395 //May overflow at "*4":
396 //spf_adj = ((total_clust+2) * 4 + bytes_per_sect-1) / bytes_per_sect - sect_per_fat;
397 //Same in the more obscure, non-overflowing form:
398 spf_adj = ((total_clust+2) + (bytes_per_sect/4)-1) / (bytes_per_sect/4) - sect_per_fat;
Denis Vlasenko781b6722009-03-28 12:17:20 +0000399#if 0
400 bb_error_msg("sect_per_clust:%u sect_per_fat:%u total_clust:%u",
401 sect_per_clust, sect_per_fat, (int)tcl);
402 bb_error_msg("adjust to sect_per_fat:%d", spf_adj);
403#endif
404 if (spf_adj <= 0) {
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000405 // do not need to adjust sect_per_fat.
406 // so, was total_clust too big after all?
407 if (total_clust <= MAX_CLUST_32)
408 goto found_total_clust; // no
409 // yes, total_clust is _a bit_ too big
410 goto next;
411 }
412 // adjust sect_per_fat, go back and recalc total_clust
Denis Vlasenko781b6722009-03-28 12:17:20 +0000413 // (note: just "sect_per_fat += spf_adj" isn't ok)
414 sect_per_fat += ((unsigned)spf_adj / 2) | 1;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000415 }
416 next:
417 if (sect_per_clust == 128)
418 bb_error_msg_and_die("can't make FAT32 with >128 sectors/cluster");
419 sect_per_clust *= 2;
420 sect_per_fat = (sect_per_fat / 2) | 1;
421 }
422 found_total_clust:
423
424 //
425 // Print info
426 //
427 if (opts & OPT_v) {
428 fprintf(stderr,
429 "Device '%s':\n"
430 "heads:%u, sectors/track:%u, bytes/sector:%u\n"
Denis Vlasenko781b6722009-03-28 12:17:20 +0000431 "media descriptor:%02x\n"
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000432 "total sectors:%"OFF_FMT"u, clusters:%u, sectors/cluster:%u\n"
433 "FATs:2, sectors/FAT:%u\n"
434 "volumeID:%08x, label:'%s'\n",
435 device_name,
436 heads, sect_per_track, bytes_per_sect,
437 (int)media_byte,
438 volume_size_sect, (int)total_clust, (int)sect_per_clust,
439 sect_per_fat,
440 (int)volume_id, volume_label
441 );
442 }
443
444 //
445 // Write filesystem image sequentially (no seeking)
446 //
447 {
Denis Vlasenko020f4652009-03-28 02:18:49 +0000448 // (a | b) is poor man's max(a, b)
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000449 unsigned bufsize = reserved_sect;
450 //bufsize |= sect_per_fat; // can be quite large
451 bufsize |= 2; // use this instead
452 bufsize |= sect_per_clust;
453 buf = xzalloc(bufsize * bytes_per_sect);
454 }
455
Denis Vlasenko14ee4e62009-03-28 02:28:58 +0000456 { // boot and fsinfo sectors, and their copies
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000457 struct msdos_boot_sector *boot_blk = (void*)buf;
458 struct fat32_fsinfo *info = (void*)(buf + bytes_per_sect);
459
460 strcpy(boot_blk->boot_jump, "\xeb\x58\x90" "mkdosfs"); // system_id[8] included :)
461 STORE_LE(boot_blk->bytes_per_sect, bytes_per_sect);
462 STORE_LE(boot_blk->sect_per_clust, sect_per_clust);
Denys Vlasenko245a4f82009-11-07 01:31:14 +0100463 // cast in needed on big endian to suppress a warning
464 STORE_LE(boot_blk->reserved_sect, (uint16_t)reserved_sect);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000465 STORE_LE(boot_blk->fats, 2);
466 //STORE_LE(boot_blk->dir_entries, 0); // for FAT32, stays 0
467 if (volume_size_sect <= 0xffff)
468 STORE_LE(boot_blk->volume_size_sect, volume_size_sect);
469 STORE_LE(boot_blk->media_byte, media_byte);
Denis Vlasenko020f4652009-03-28 02:18:49 +0000470 // wrong: this would make Linux think that it's fat12/16:
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000471 //if (sect_per_fat <= 0xffff)
472 // STORE_LE(boot_blk->sect_per_fat, sect_per_fat);
Denis Vlasenko020f4652009-03-28 02:18:49 +0000473 // works:
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000474 //STORE_LE(boot_blk->sect_per_fat, 0);
475 STORE_LE(boot_blk->sect_per_track, sect_per_track);
476 STORE_LE(boot_blk->heads, heads);
477 //STORE_LE(boot_blk->hidden, 0);
478 STORE_LE(boot_blk->fat32_volume_size_sect, volume_size_sect);
479 STORE_LE(boot_blk->fat32_sect_per_fat, sect_per_fat);
480 //STORE_LE(boot_blk->fat32_flags, 0);
481 //STORE_LE(boot_blk->fat32_version[2], 0,0);
482 STORE_LE(boot_blk->fat32_root_cluster, 2);
483 STORE_LE(boot_blk->fat32_info_sector, info_sector_number);
484 STORE_LE(boot_blk->fat32_backup_boot, backup_boot_sector);
485 //STORE_LE(boot_blk->reserved2[3], 0,0,0);
486 STORE_LE(boot_blk->vi.ext_boot_sign, 0x29);
487 STORE_LE(boot_blk->vi.volume_id32, volume_id);
488 strncpy(boot_blk->vi.fs_type, "FAT32 ", sizeof(boot_blk->vi.fs_type));
489 strncpy(boot_blk->vi.volume_label, volume_label, sizeof(boot_blk->vi.volume_label));
490 memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code));
491 STORE_LE(boot_blk->boot_sign, BOOT_SIGN);
492
493 STORE_LE(info->signature1, FAT_FSINFO_SIG1);
494 STORE_LE(info->signature2, FAT_FSINFO_SIG2);
495 // we've allocated cluster 2 for the root dir
496 STORE_LE(info->free_clusters, (total_clust - 1));
497 STORE_LE(info->next_cluster, 2);
498 STORE_LE(info->boot_sign, BOOT_SIGN);
499
500 // 1st copy
501 xwrite(dev, buf, bytes_per_sect * backup_boot_sector);
502 // 2nd copy and possibly zero sectors
503 xwrite(dev, buf, bytes_per_sect * (reserved_sect - backup_boot_sector));
504 }
505
506 { // file allocation tables
507 unsigned i,j;
508 unsigned char *fat = (void*)buf;
509
510 memset(buf, 0, bytes_per_sect * 2);
511 // initial FAT entries
Denis Vlasenko14ee4e62009-03-28 02:28:58 +0000512 MARK_CLUSTER(0, 0x0fffff00 | media_byte);
513 MARK_CLUSTER(1, 0xffffffff);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000514 // mark cluster 2 as EOF (used for root dir)
Denis Vlasenko14ee4e62009-03-28 02:28:58 +0000515 MARK_CLUSTER(2, EOF_FAT32);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000516 for (i = 0; i < NUM_FATS; i++) {
517 xwrite(dev, buf, bytes_per_sect);
518 for (j = 1; j < sect_per_fat; j++)
519 xwrite(dev, buf + bytes_per_sect, bytes_per_sect);
520 }
521 }
522
523 // root directory
524 // empty directory is just a set of zero bytes
525 memset(buf, 0, sect_per_clust * bytes_per_sect);
526 if (volume_label[0]) {
527 // create dir entry for volume_label
528 struct msdos_dir_entry *de;
529#if 0
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100530 struct tm tm_time;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000531 uint16_t t, d;
532#endif
533 de = (void*)buf;
534 strncpy(de->name, volume_label, sizeof(de->name));
535 STORE_LE(de->attr, ATTR_VOLUME);
536#if 0
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100537 localtime_r(&create_time, &tm_time);
538 t = (tm_time.tm_sec >> 1) + (tm_time.tm_min << 5) + (tm_time.tm_hour << 11);
539 d = tm_time.tm_mday + ((tm_time.tm_mon+1) << 5) + ((tm_time.tm_year-80) << 9);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000540 STORE_LE(de->time, t);
541 STORE_LE(de->date, d);
542 //STORE_LE(de->ctime_cs, 0);
543 de->ctime = de->time;
544 de->cdate = de->date;
545 de->adate = de->date;
546#endif
547 }
548 xwrite(dev, buf, sect_per_clust * bytes_per_sect);
549
550#if 0
551 if (opts & OPT_c) {
Denis Vlasenkof54dd092009-03-28 03:22:08 +0000552 uoff_t volume_size_blocks;
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000553 unsigned start_data_sector;
554 unsigned start_data_block;
555 unsigned badblocks = 0;
556 int try, got;
557 off_t currently_testing;
558 char *blkbuf = xmalloc(BLOCK_SIZE * TEST_BUFFER_BLOCKS);
559
Denis Vlasenkof54dd092009-03-28 03:22:08 +0000560 volume_size_blocks = (volume_size_bytes >> BLOCK_SIZE_BITS);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000561 // N.B. the two following vars are in hard sectors, i.e. SECTOR_SIZE byte sectors!
562 start_data_sector = (reserved_sect + NUM_FATS * sect_per_fat) * (bytes_per_sect / SECTOR_SIZE);
563 start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) / SECTORS_PER_BLOCK;
564
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000565 bb_info_msg("searching for bad blocks ");
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000566 currently_testing = 0;
567 try = TEST_BUFFER_BLOCKS;
568 while (currently_testing < volume_size_blocks) {
569 if (currently_testing + try > volume_size_blocks)
570 try = volume_size_blocks - currently_testing;
571 // perform a test on a block. return the number of blocks
572 // that could be read successfully.
573 // seek to the correct location
574 xlseek(dev, currently_testing * BLOCK_SIZE, SEEK_SET);
575 // try reading
576 got = read(dev, blkbuf, try * BLOCK_SIZE);
577 if (got < 0)
578 got = 0;
579 if (got & (BLOCK_SIZE - 1))
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000580 bb_error_msg("unexpected values in do_check: probably bugs");
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000581 got /= BLOCK_SIZE;
582 currently_testing += got;
583 if (got == try) {
584 try = TEST_BUFFER_BLOCKS;
585 continue;
586 }
587 try = 1;
588 if (currently_testing < start_data_block)
589 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
590
591 // mark all of the sectors in the block as bad
592 for (i = 0; i < SECTORS_PER_BLOCK; i++) {
593 int cluster = (currently_testing * SECTORS_PER_BLOCK + i - start_data_sector) / (int) (sect_per_clust) / (bytes_per_sect / SECTOR_SIZE);
594 if (cluster < 0)
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000595 bb_error_msg_and_die("invalid cluster number in mark_sector: probably bug!");
Denis Vlasenko14ee4e62009-03-28 02:28:58 +0000596 MARK_CLUSTER(cluster, BAD_FAT32);
Denis Vlasenko9d04b6b2009-03-28 02:13:01 +0000597 }
598 badblocks++;
599 currently_testing++;
600 }
601 free(blkbuf);
602 if (badblocks)
603 bb_info_msg("%d bad block(s)", badblocks);
604 }
605#endif
606
607 // cleanup
608 if (ENABLE_FEATURE_CLEAN_UP) {
609 free(buf);
610 close(dev);
611 }
612
613 return 0;
614}