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