123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- #include "libbb.h"
- #include <linux/hdreg.h> /* HDIO_GETGEO */
- #include <linux/fd.h> /* FDGETPRM */
- #include <sys/mount.h> /* BLKSSZGET */
- #if !defined(BLKSSZGET)
- # define BLKSSZGET _IO(0x12, 104)
- #endif
- #define SECTOR_SIZE 512
- #define SECTORS_PER_BLOCK (BLOCK_SIZE / SECTOR_SIZE)
- #define EOF_FAT32 0x0FFFFFF8
- #define BAD_FAT32 0x0FFFFFF7
- #define MAX_CLUST_32 0x0FFFFFF0
- #define ATTR_VOLUME 8
- #define NUM_FATS 2
- enum {
- info_sector_number = 1,
- backup_boot_sector = 3,
- reserved_sect = 6,
- };
- #define TEST_BUFFER_BLOCKS 16
- struct msdos_dir_entry {
- char name[11];
- uint8_t attr;
- uint8_t lcase;
- uint8_t ctime_cs;
- uint16_t ctime;
- uint16_t cdate;
- uint16_t adate;
- uint16_t starthi;
- uint16_t time;
- uint16_t date;
- uint16_t start;
- uint32_t size;
- } PACKED;
- struct msdos_volume_info {
- uint8_t drive_number;
- uint8_t reserved;
- uint8_t ext_boot_sign;
- uint32_t volume_id32;
- char volume_label[11];
- char fs_type[8];
- } PACKED;
- struct msdos_boot_sector {
-
- char boot_jump_and_sys_id[3+8];
-
- uint16_t bytes_per_sect;
- uint8_t sect_per_clust;
- uint16_t reserved_sect;
- uint8_t fats;
- uint16_t dir_entries;
- uint16_t volume_size_sect;
- uint8_t media_byte;
- uint16_t sect_per_fat;
- uint16_t sect_per_track;
- uint16_t heads;
- uint32_t hidden;
- uint32_t fat32_volume_size_sect;
- uint32_t fat32_sect_per_fat;
- uint16_t fat32_flags;
- uint8_t fat32_version[2];
- uint32_t fat32_root_cluster;
- uint16_t fat32_info_sector;
- uint16_t fat32_backup_boot;
- uint32_t reserved2[3];
- struct msdos_volume_info vi;
- char boot_code[0x200 - 0x5a - 2];
- #define BOOT_SIGN 0xAA55
- uint16_t boot_sign;
- } PACKED;
- #define FAT_FSINFO_SIG1 0x41615252
- #define FAT_FSINFO_SIG2 0x61417272
- struct fat32_fsinfo {
- uint32_t signature1;
- uint32_t reserved1[128 - 8];
- uint32_t signature2;
- uint32_t free_clusters;
- uint32_t next_cluster;
- uint32_t reserved2[3];
- uint16_t reserved3;
- uint16_t boot_sign;
- } PACKED;
- struct bug_check {
- char BUG1[sizeof(struct msdos_dir_entry ) == 0x20 ? 1 : -1];
- char BUG2[sizeof(struct msdos_volume_info) == 0x1a ? 1 : -1];
- char BUG3[sizeof(struct msdos_boot_sector) == 0x200 ? 1 : -1];
- char BUG4[sizeof(struct fat32_fsinfo ) == 0x200 ? 1 : -1];
- };
- static const char boot_code[] ALIGN1 =
- "\x0e"
- "\x1f"
- "\xbe\x77\x7c"
- "\xac"
- "\x22\xc0"
- "\x74\x0b"
- "\x56"
- "\xb4\x0e"
- "\xbb\x07\x00"
- "\xcd\x10"
- "\x5e"
- "\xeb\xf0"
- "\x32\xe4"
- "\xcd\x16"
- "\xcd\x19"
- "\xeb\xfe"
-
- "This is not a bootable disk\r\n";
- #define MARK_CLUSTER(cluster, value) \
- ((uint32_t *)fat)[cluster] = SWAP_LE32(value)
- void BUG_unsupported_field_size(void);
- #define STORE_LE(field, value) \
- do { \
- if (sizeof(field) == 4) \
- field = SWAP_LE32(value); \
- else if (sizeof(field) == 2) \
- field = SWAP_LE16(value); \
- else if (sizeof(field) == 1) \
- field = (value); \
- else \
- BUG_unsupported_field_size(); \
- } while (0)
- int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
- {
- struct stat st;
- const char *volume_label = "";
- char *buf;
- char *device_name;
- uoff_t volume_size_bytes;
- uoff_t volume_size_sect;
- uint32_t total_clust;
- uint32_t volume_id;
- int dev;
- unsigned bytes_per_sect;
- unsigned sect_per_fat;
- unsigned opts;
- uint16_t sect_per_track;
- uint8_t media_byte;
- uint8_t sect_per_clust;
- uint8_t heads;
- enum {
- OPT_A = 1 << 0,
- OPT_b = 1 << 1,
- OPT_c = 1 << 2,
- OPT_C = 1 << 3,
- OPT_f = 1 << 4,
- OPT_F = 1 << 5,
- OPT_h = 1 << 6,
- OPT_I = 1 << 7,
- OPT_i = 1 << 8,
- OPT_l = 1 << 9,
- OPT_m = 1 << 10,
- OPT_n = 1 << 11,
- OPT_r = 1 << 12,
- OPT_R = 1 << 13,
- OPT_s = 1 << 14,
- OPT_S = 1 << 15,
- OPT_v = 1 << 16,
- };
- opt_complementary = "-1";
- opts = getopt32(argv, "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v",
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, &volume_label, NULL, NULL, NULL, NULL);
- argv += optind;
-
- device_name = argv[0];
-
- volume_id = time(NULL);
- dev = xopen(device_name, O_RDWR);
- xfstat(dev, &st, device_name);
-
-
-
- bytes_per_sect = SECTOR_SIZE;
- if (!S_ISBLK(st.st_mode)) {
- if (!S_ISREG(st.st_mode)) {
- if (!argv[1])
- bb_error_msg_and_die("image size must be specified");
- }
-
- opts &= ~OPT_c;
- } else {
- int min_bytes_per_sect;
- #if 0
- unsigned device_num;
-
- device_num = st.st_rdev & 0xff3f;
-
- if (!(opts & OPT_I) && (
- device_num == 0x0300 ||
- (device_num & 0xff0f) == 0x0800 ||
- device_num == 0x0d00 ||
- device_num == 0x1600 )
- )
- bb_error_msg_and_die("will not try to make filesystem on full-disk device (use -I if wanted)");
-
- if (find_mount_point(device_name, 0))
- bb_error_msg_and_die("can't format mounted filesystem");
- #endif
-
-
- xioctl(dev, BLKSSZGET, &min_bytes_per_sect);
- if (min_bytes_per_sect > SECTOR_SIZE) {
- bytes_per_sect = min_bytes_per_sect;
- bb_error_msg("for this device sector size is %u", min_bytes_per_sect);
- }
- }
- volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024, 1);
- volume_size_sect = volume_size_bytes / bytes_per_sect;
-
-
-
- media_byte = 0xf8;
- heads = 255;
- sect_per_track = 63;
- sect_per_clust = 1;
- {
- struct hd_geometry geometry;
-
- struct floppy_struct param;
-
- if (ioctl(dev, HDIO_GETGEO, &geometry) == 0
- && geometry.sectors
- && geometry.heads
- ) {
-
- sect_per_track = geometry.sectors;
- heads = geometry.heads;
- set_cluster_size:
-
- sect_per_clust = 1;
- if (volume_size_bytes >= 260*1024*1024) {
- sect_per_clust = 8;
-
-
-
- if (sizeof(off_t) > 4) {
- unsigned t = (volume_size_bytes >> 31 >> 1);
- if (t >= 8/4)
- sect_per_clust = 16;
- if (t >= 16/4)
- sect_per_clust = 32;
- }
- }
- } else {
-
- int not_floppy = ioctl(dev, FDGETPRM, ¶m);
- if (not_floppy == 0) {
-
- sect_per_track = param.sect;
- heads = param.head;
- volume_size_sect = param.size;
- volume_size_bytes = param.size * SECTOR_SIZE;
- }
-
- switch (volume_size_sect) {
- case 2*360:
- media_byte = 0xfd;
- break;
- case 2*720:
- case 2*1200:
- media_byte = 0xf9;
- break;
- default:
- if (not_floppy)
- goto set_cluster_size;
- case 2*1440:
- case 2*2880:
- media_byte = 0xf0;
- break;
- }
-
-
-
-
- heads = 2;
- sect_per_track = (unsigned)volume_size_sect / 160;
- if (sect_per_track < 9)
- sect_per_track = 9;
- }
- }
-
-
-
-
-
-
- if ((off_t)(volume_size_sect - reserved_sect) < 4)
- bb_error_msg_and_die("the image is too small for FAT32");
- sect_per_fat = 1;
- while (1) {
- while (1) {
- int spf_adj;
- uoff_t tcl = (volume_size_sect - reserved_sect - NUM_FATS * sect_per_fat) / sect_per_clust;
-
-
-
-
-
- if (tcl > 0x80ffffff)
- goto next;
- total_clust = tcl;
-
-
-
-
-
-
- spf_adj = ((total_clust+2) + (bytes_per_sect/4)-1) / (bytes_per_sect/4) - sect_per_fat;
- #if 0
- bb_error_msg("sect_per_clust:%u sect_per_fat:%u total_clust:%u",
- sect_per_clust, sect_per_fat, (int)tcl);
- bb_error_msg("adjust to sect_per_fat:%d", spf_adj);
- #endif
- if (spf_adj <= 0) {
-
-
- if (total_clust <= MAX_CLUST_32)
- goto found_total_clust;
-
- goto next;
- }
-
-
- sect_per_fat += ((unsigned)spf_adj / 2) | 1;
- }
- next:
- if (sect_per_clust == 128)
- bb_error_msg_and_die("can't make FAT32 with >128 sectors/cluster");
- sect_per_clust *= 2;
- sect_per_fat = (sect_per_fat / 2) | 1;
- }
- found_total_clust:
-
-
-
- if (opts & OPT_v) {
- fprintf(stderr,
- "Device '%s':\n"
- "heads:%u, sectors/track:%u, bytes/sector:%u\n"
- "media descriptor:%02x\n"
- "total sectors:%"OFF_FMT"u, clusters:%u, sectors/cluster:%u\n"
- "FATs:2, sectors/FAT:%u\n"
- "volumeID:%08x, label:'%s'\n",
- device_name,
- heads, sect_per_track, bytes_per_sect,
- (int)media_byte,
- volume_size_sect, (int)total_clust, (int)sect_per_clust,
- sect_per_fat,
- (int)volume_id, volume_label
- );
- }
-
-
-
- {
-
- unsigned bufsize = reserved_sect;
-
- bufsize |= 2;
- bufsize |= sect_per_clust;
- buf = xzalloc(bufsize * bytes_per_sect);
- }
- {
- struct msdos_boot_sector *boot_blk = (void*)buf;
- struct fat32_fsinfo *info = (void*)(buf + bytes_per_sect);
- strcpy(boot_blk->boot_jump_and_sys_id, "\xeb\x58\x90" "mkdosfs");
- STORE_LE(boot_blk->bytes_per_sect, bytes_per_sect);
- STORE_LE(boot_blk->sect_per_clust, sect_per_clust);
-
- STORE_LE(boot_blk->reserved_sect, (uint16_t)reserved_sect);
- STORE_LE(boot_blk->fats, 2);
-
- if (volume_size_sect <= 0xffff)
- STORE_LE(boot_blk->volume_size_sect, volume_size_sect);
- STORE_LE(boot_blk->media_byte, media_byte);
-
-
-
-
-
- STORE_LE(boot_blk->sect_per_track, sect_per_track);
- STORE_LE(boot_blk->heads, heads);
-
- STORE_LE(boot_blk->fat32_volume_size_sect, volume_size_sect);
- STORE_LE(boot_blk->fat32_sect_per_fat, sect_per_fat);
-
-
- STORE_LE(boot_blk->fat32_root_cluster, 2);
- STORE_LE(boot_blk->fat32_info_sector, info_sector_number);
- STORE_LE(boot_blk->fat32_backup_boot, backup_boot_sector);
-
- STORE_LE(boot_blk->vi.ext_boot_sign, 0x29);
- STORE_LE(boot_blk->vi.volume_id32, volume_id);
- strncpy(boot_blk->vi.fs_type, "FAT32 ", sizeof(boot_blk->vi.fs_type));
- strncpy(boot_blk->vi.volume_label, volume_label, sizeof(boot_blk->vi.volume_label));
- memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code));
- STORE_LE(boot_blk->boot_sign, BOOT_SIGN);
- STORE_LE(info->signature1, FAT_FSINFO_SIG1);
- STORE_LE(info->signature2, FAT_FSINFO_SIG2);
-
- STORE_LE(info->free_clusters, (total_clust - 1));
- STORE_LE(info->next_cluster, 2);
- STORE_LE(info->boot_sign, BOOT_SIGN);
-
- xwrite(dev, buf, bytes_per_sect * backup_boot_sector);
-
- xwrite(dev, buf, bytes_per_sect * (reserved_sect - backup_boot_sector));
- }
- {
- unsigned i,j;
- unsigned char *fat = (void*)buf;
- memset(buf, 0, bytes_per_sect * 2);
-
- MARK_CLUSTER(0, 0x0fffff00 | media_byte);
- MARK_CLUSTER(1, 0xffffffff);
-
- MARK_CLUSTER(2, EOF_FAT32);
- for (i = 0; i < NUM_FATS; i++) {
- xwrite(dev, buf, bytes_per_sect);
- for (j = 1; j < sect_per_fat; j++)
- xwrite(dev, buf + bytes_per_sect, bytes_per_sect);
- }
- }
-
-
- memset(buf, 0, sect_per_clust * bytes_per_sect);
- if (volume_label[0]) {
-
- struct msdos_dir_entry *de;
- #if 0
- struct tm tm_time;
- uint16_t t, d;
- #endif
- de = (void*)buf;
- strncpy(de->name, volume_label, sizeof(de->name));
- STORE_LE(de->attr, ATTR_VOLUME);
- #if 0
- localtime_r(&create_time, &tm_time);
- t = (tm_time.tm_sec >> 1) + (tm_time.tm_min << 5) + (tm_time.tm_hour << 11);
- d = tm_time.tm_mday + ((tm_time.tm_mon+1) << 5) + ((tm_time.tm_year-80) << 9);
- STORE_LE(de->time, t);
- STORE_LE(de->date, d);
-
- de->ctime = de->time;
- de->cdate = de->date;
- de->adate = de->date;
- #endif
- }
- xwrite(dev, buf, sect_per_clust * bytes_per_sect);
- #if 0
- if (opts & OPT_c) {
- uoff_t volume_size_blocks;
- unsigned start_data_sector;
- unsigned start_data_block;
- unsigned badblocks = 0;
- int try, got;
- off_t currently_testing;
- char *blkbuf = xmalloc(BLOCK_SIZE * TEST_BUFFER_BLOCKS);
- volume_size_blocks = (volume_size_bytes >> BLOCK_SIZE_BITS);
-
- start_data_sector = (reserved_sect + NUM_FATS * sect_per_fat) * (bytes_per_sect / SECTOR_SIZE);
- start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) / SECTORS_PER_BLOCK;
- bb_info_msg("searching for bad blocks ");
- currently_testing = 0;
- try = TEST_BUFFER_BLOCKS;
- while (currently_testing < volume_size_blocks) {
- if (currently_testing + try > volume_size_blocks)
- try = volume_size_blocks - currently_testing;
-
-
-
- xlseek(dev, currently_testing * BLOCK_SIZE, SEEK_SET);
-
- got = read(dev, blkbuf, try * BLOCK_SIZE);
- if (got < 0)
- got = 0;
- if (got & (BLOCK_SIZE - 1))
- bb_error_msg("unexpected values in do_check: probably bugs");
- got /= BLOCK_SIZE;
- currently_testing += got;
- if (got == try) {
- try = TEST_BUFFER_BLOCKS;
- continue;
- }
- try = 1;
- if (currently_testing < start_data_block)
- bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
-
- for (i = 0; i < SECTORS_PER_BLOCK; i++) {
- int cluster = (currently_testing * SECTORS_PER_BLOCK + i - start_data_sector) / (int) (sect_per_clust) / (bytes_per_sect / SECTOR_SIZE);
- if (cluster < 0)
- bb_error_msg_and_die("invalid cluster number in mark_sector: probably bug!");
- MARK_CLUSTER(cluster, BAD_FAT32);
- }
- badblocks++;
- currently_testing++;
- }
- free(blkbuf);
- if (badblocks)
- bb_info_msg("%d bad block(s)", badblocks);
- }
- #endif
-
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(buf);
- close(dev);
- }
- return 0;
- }
|