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