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