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