diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.c | 124 | ||||
-rw-r--r-- | src/check.c | 65 | ||||
-rw-r--r-- | src/dosfsck.c | 7 | ||||
-rw-r--r-- | src/dosfsck.h | 22 | ||||
-rw-r--r-- | src/dosfslabel.c | 6 | ||||
-rw-r--r-- | src/fat.c | 16 | ||||
-rw-r--r-- | src/lfn.c | 10 | ||||
-rw-r--r-- | src/mkdosfs.c | 137 | ||||
-rw-r--r-- | src/version.h | 4 |
9 files changed, 175 insertions, 216 deletions
@@ -60,18 +60,9 @@ static struct { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec"}, { 0xff, "5.25\" 320k floppy 2s/40tr/8sec"},}; -#if defined __alpha || defined __arm || defined __arm__ || defined __ia64__ || defined __x86_64__ \ - || defined __ppc64__ || defined __bfin__ || defined __MICROBLAZE__ -/* Unaligned fields must first be copied byte-wise */ +/* Unaligned fields must first be accessed byte-wise */ #define GET_UNALIGNED_W(f) \ - ({ \ - unsigned short __v; \ - memcpy( &__v, &f, sizeof(__v) ); \ - CF_LE_W( *(unsigned short *)&__v ); \ - }) -#else -#define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f ) -#endif + le16toh( (__u16)f[0] | ((__u16)f[1]<<8) ) static char *get_media_descr(unsigned char media) { @@ -104,8 +95,8 @@ static void dump_boot(DOS_FS * fs, struct boot_sector *b, unsigned lss) printf("Media byte 0x%02x (%s)\n", b->media, get_media_descr(b->media)); printf("%10d bytes per logical sector\n", GET_UNALIGNED_W(b->sector_size)); printf("%10d bytes per cluster\n", fs->cluster_size); - printf("%10d reserved sector%s\n", CF_LE_W(b->reserved), - CF_LE_W(b->reserved) == 1 ? "" : "s"); + printf("%10d reserved sector%s\n", le16toh(b->reserved), + le16toh(b->reserved) == 1 ? "" : "s"); printf("First FAT starts at byte %llu (sector %llu)\n", (unsigned long long)fs->fat_start, (unsigned long long)fs->fat_start / lss); @@ -126,14 +117,14 @@ static void dump_boot(DOS_FS * fs, struct boot_sector *b, unsigned lss) (unsigned long long)fs->data_start / lss); printf("%10lu data clusters (%llu bytes)\n", fs->clusters, (unsigned long long)fs->clusters * fs->cluster_size); - printf("%u sectors/track, %u heads\n", CF_LE_W(b->secs_track), - CF_LE_W(b->heads)); + printf("%u sectors/track, %u heads\n", le16toh(b->secs_track), + le16toh(b->heads)); printf("%10u hidden sectors\n", atari_format ? /* On Atari, the hidden field is only 16 bit wide and unused */ (((unsigned char *)&b->hidden)[0] | - ((unsigned char *)&b->hidden)[1] << 8) : CF_LE_L(b->hidden)); + ((unsigned char *)&b->hidden)[1] << 8) : le32toh(b->hidden)); sectors = GET_UNALIGNED_W(b->sectors); - printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect)); + printf("%10u sectors total\n", sectors ? sectors : le32toh(b->total_sect)); } static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss) @@ -142,7 +133,7 @@ static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss) if (!fs->backupboot_start) { printf("There is no backup boot sector.\n"); - if (CF_LE_W(b->reserved) < 3) { + if (le16toh(b->reserved) < 3) { printf("And there is no space for creating one!\n"); return; } @@ -154,15 +145,15 @@ static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss) int bbs; /* The usual place for the backup boot sector is sector 6. Choose * that or the last reserved sector. */ - if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6) + if (le16toh(b->reserved) >= 7 && le16toh(b->info_sector) != 6) bbs = 6; else { - bbs = CF_LE_W(b->reserved) - 1; - if (bbs == CF_LE_W(b->info_sector)) + bbs = le16toh(b->reserved) - 1; + if (bbs == le16toh(b->info_sector)) --bbs; /* this is never 0, as we checked reserved >= 3! */ } fs->backupboot_start = bbs * lss; - b->backup_boot = CT_LE_W(bbs); + b->backup_boot = htole16(bbs); fs_write(fs->backupboot_start, sizeof(*b), b); fs_write((loff_t) offsetof(struct boot_sector, backup_boot), sizeof(b->backup_boot), &b->backup_boot); @@ -216,11 +207,11 @@ static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss) static void init_fsinfo(struct info_sector *i) { - i->magic = CT_LE_L(0x41615252); - i->signature = CT_LE_L(0x61417272); - i->free_clusters = CT_LE_L(-1); - i->next_cluster = CT_LE_L(2); - i->boot_sign = CT_LE_W(0xaa55); + i->magic = htole32(0x41615252); + i->signature = htole32(0x61417272); + i->free_clusters = htole32(-1); + i->next_cluster = htole32(2); + i->boot_sign = htole16(0xaa55); } static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss) @@ -237,13 +228,13 @@ static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss) /* search for a free reserved sector (not boot sector and not * backup boot sector) */ __u32 s; - for (s = 1; s < CF_LE_W(b->reserved); ++s) - if (s != CF_LE_W(b->backup_boot)) + for (s = 1; s < le16toh(b->reserved); ++s) + if (s != le16toh(b->backup_boot)) break; - if (s > 0 && s < CF_LE_W(b->reserved)) { + if (s > 0 && s < le16toh(b->reserved)) { init_fsinfo(&i); fs_write((loff_t) s * lss, sizeof(i), &i); - b->info_sector = CT_LE_W(s); + b->info_sector = htole16(s); fs_write((loff_t) offsetof(struct boot_sector, info_sector), sizeof(b->info_sector), &b->info_sector); if (fs->backupboot_start) @@ -259,24 +250,24 @@ static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss) return; } - fs->fsinfo_start = CF_LE_W(b->info_sector) * lss; + fs->fsinfo_start = le16toh(b->info_sector) * lss; fs_read(fs->fsinfo_start, sizeof(i), &i); - if (i.magic != CT_LE_L(0x41615252) || - i.signature != CT_LE_L(0x61417272) || i.boot_sign != CT_LE_W(0xaa55)) { + if (i.magic != htole32(0x41615252) || + i.signature != htole32(0x61417272) || i.boot_sign != htole16(0xaa55)) { printf("FSINFO sector has bad magic number(s):\n"); - if (i.magic != CT_LE_L(0x41615252)) + if (i.magic != htole32(0x41615252)) printf(" Offset %llu: 0x%08x != expected 0x%08x\n", (unsigned long long)offsetof(struct info_sector, magic), - CF_LE_L(i.magic), 0x41615252); - if (i.signature != CT_LE_L(0x61417272)) + le32toh(i.magic), 0x41615252); + if (i.signature != htole32(0x61417272)) printf(" Offset %llu: 0x%08x != expected 0x%08x\n", (unsigned long long)offsetof(struct info_sector, signature), - CF_LE_L(i.signature), 0x61417272); - if (i.boot_sign != CT_LE_W(0xaa55)) + le32toh(i.signature), 0x61417272); + if (i.boot_sign != htole16(0xaa55)) printf(" Offset %llu: 0x%04x != expected 0x%04x\n", (unsigned long long)offsetof(struct info_sector, boot_sign), - CF_LE_W(i.boot_sign), 0xaa55); + le16toh(i.boot_sign), 0xaa55); if (interactive) printf("1) Correct\n2) Don't correct (FSINFO invalid then)\n"); else @@ -289,7 +280,7 @@ static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss) } if (fs->fsinfo_start) - fs->free_clusters = CF_LE_L(i.free_clusters); + fs->free_clusters = le32toh(i.free_clusters); } static char print_fat_dirty_state(void) @@ -358,16 +349,16 @@ void read_boot(DOS_FS * fs) die("Currently, only 1 or 2 FATs are supported, not %d.\n", b.fats); fs->nfats = b.fats; sectors = GET_UNALIGNED_W(b.sectors); - total_sectors = sectors ? sectors : CF_LE_L(b.total_sect); + total_sectors = sectors ? sectors : le32toh(b.total_sect); if (verbose) printf("Checking we can access the last sector of the filesystem\n"); /* Can't access last odd sector anyway, so round down */ fs_test((loff_t) ((total_sectors & ~1) - 1) * (loff_t) logical_sector_size, logical_sector_size); - fat_length = CF_LE_W(b.fat_length) ? - CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length); - fs->fat_start = (loff_t) CF_LE_W(b.reserved) * logical_sector_size; - fs->root_start = ((loff_t) CF_LE_W(b.reserved) + b.fats * fat_length) * + fat_length = le16toh(b.fat_length) ? + le16toh(b.fat_length) : le32toh(b.fat32_length); + fs->fat_start = (loff_t) le16toh(b.reserved) * logical_sector_size; + fs->root_start = ((loff_t) le16toh(b.reserved) + b.fats * fat_length) * logical_sector_size; fs->root_entries = GET_UNALIGNED_W(b.dir_entries); fs->data_start = fs->root_start + ROUND_TO_MULTIPLE(fs->root_entries << @@ -380,7 +371,7 @@ void read_boot(DOS_FS * fs) fs->free_clusters = -1; /* unknown */ if (!b.fat_length && b.fat32_length) { fs->fat_bits = 32; - fs->root_cluster = CF_LE_L(b.root_cluster); + fs->root_cluster = le32toh(b.root_cluster); if (!fs->root_cluster && fs->root_entries) /* M$ hasn't specified this, but it looks reasonable: If * root_cluster is 0 but there is a separate root dir @@ -404,7 +395,7 @@ void read_boot(DOS_FS * fs) fs->clusters, FAT16_THRESHOLD); check_fat_state_bit(fs, &b); - fs->backupboot_start = CF_LE_W(b.backup_boot) * logical_sector_size; + fs->backupboot_start = le16toh(b.backup_boot) * logical_sector_size; check_backup_boot(fs, &b, logical_sector_size); read_fsinfo(fs, &b, logical_sector_size); @@ -473,29 +464,32 @@ void read_boot(DOS_FS * fs) static void write_boot_label(DOS_FS * fs, char *label) { - struct boot_sector b; - struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b; - - fs_read(0, sizeof(b), &b); if (fs->fat_bits == 12 || fs->fat_bits == 16) { - if (b16->extended_sig != 0x29) { - b16->extended_sig = 0x29; - b16->serial = 0; - memmove(b16->fs_type, fs->fat_bits == 12 ? "FAT12 " : "FAT16 ", + struct boot_sector_16 b16; + + fs_read(0, sizeof(b16), &b16); + if (b16.extended_sig != 0x29) { + b16.extended_sig = 0x29; + b16.serial = 0; + memmove(b16.fs_type, fs->fat_bits == 12 ? "FAT12 " : "FAT16 ", 8); } - memmove(b16->label, label, 11); + memmove(b16.label, label, 11); + fs_write(0, sizeof(b16), &b16); } else if (fs->fat_bits == 32) { + struct boot_sector b; + + fs_read(0, sizeof(b), &b); if (b.extended_sig != 0x29) { b.extended_sig = 0x29; b.serial = 0; memmove(b.fs_type, "FAT32 ", 8); } memmove(b.label, label, 11); + fs_write(0, sizeof(b), &b); + if (fs->backupboot_start) + fs_write(fs->backupboot_start, sizeof(b), &b); } - fs_write(0, sizeof(b), &b); - if (fs->fat_bits == 32 && fs->backupboot_start) - fs_write(fs->backupboot_start, sizeof(b), &b); } loff_t find_volume_de(DOS_FS * fs, DIR_ENT * de) @@ -544,10 +538,10 @@ static void write_volume_label(DOS_FS * fs, char *label) offset = alloc_rootdir_entry(fs, &de, label); } memcpy(de.name, label, 11); - de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) + + de.time = htole16((unsigned short)((mtime->tm_sec >> 1) + (mtime->tm_min << 5) + (mtime->tm_hour << 11))); - de.date = CT_LE_W((unsigned short)(mtime->tm_mday + + de.date = htole16((unsigned short)(mtime->tm_mday + ((mtime->tm_mon + 1) << 5) + ((mtime->tm_year - 80) << 9))); if (created) @@ -557,9 +551,9 @@ static void write_volume_label(DOS_FS * fs, char *label) de.ctime = de.time; de.cdate = de.date; de.adate = de.date; - de.starthi = CT_LE_W(0); - de.start = CT_LE_W(0); - de.size = CT_LE_L(0); + de.starthi = 0; + de.start = 0; + de.size = 0; } fs_write(offset, sizeof(DIR_ENT), &de); diff --git a/src/check.c b/src/check.c index 1ae91c6..a3fcc93 100644 --- a/src/check.c +++ b/src/check.c @@ -42,8 +42,8 @@ static DOS_FILE *root; /* get start field of a dir entry */ #define FSTART(p,fs) \ - ((unsigned long)CF_LE_W(p->dir_ent.start) | \ - (fs->fat_bits == 32 ? CF_LE_W(p->dir_ent.starthi) << 16 : 0)) + ((unsigned long)le16toh(p->dir_ent.start) | \ + (fs->fat_bits == 32 ? le16toh(p->dir_ent.starthi) << 16 : 0)) #define MODIFY(p,i,v) \ do { \ @@ -61,17 +61,17 @@ static DOS_FILE *root; /* writing to fake entry for FAT32 root dir */ \ if (!__v) die("Oops, deleting FAT32 root dir!"); \ fs->root_cluster = __v; \ - p->dir_ent.start = CT_LE_W(__v&0xffff); \ - p->dir_ent.starthi = CT_LE_W(__v>>16); \ - __v = CT_LE_L(__v); \ + p->dir_ent.start = htole16(__v&0xffff); \ + p->dir_ent.starthi = htole16(__v>>16); \ + __v = htole32(__v); \ fs_write((loff_t)offsetof(struct boot_sector,root_cluster), \ sizeof(((struct boot_sector *)0)->root_cluster), \ &__v); \ } \ else { \ - MODIFY(p,start,CT_LE_W((__v)&0xffff)); \ + MODIFY(p,start,htole16((__v)&0xffff)); \ if (fs->fat_bits == 32) \ - MODIFY(p,starthi,CT_LE_W((__v)>>16)); \ + MODIFY(p,starthi,htole16((__v)>>16)); \ } \ } while(0) @@ -230,7 +230,7 @@ static int day_n[] = /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ -time_t date_dos2unix(unsigned short time, unsigned short date) +static time_t date_dos2unix(unsigned short time, unsigned short date) { int month, year; time_t secs; @@ -253,18 +253,19 @@ static char *file_stat(DOS_FILE * file) time_t date; date = - date_dos2unix(CF_LE_W(file->dir_ent.time), CF_LE_W(file->dir_ent.date)); + date_dos2unix(le16toh(file->dir_ent.time), le16toh(file->dir_ent.date)); tm = localtime(&date); strftime(tmp, 99, "%H:%M:%S %b %d %Y", tm); - sprintf(temp, " Size %u bytes, date %s", CF_LE_L(file->dir_ent.size), tmp); + sprintf(temp, " Size %u bytes, date %s", le32toh(file->dir_ent.size), tmp); return temp; } static int bad_name(DOS_FILE * file) { int i, spc, suspicious = 0; - char *bad_chars = atari_format ? "*?\\/:" : "*?<>|\"\\/:"; - unsigned char *name = file->dir_ent.name; + const char *bad_chars = atari_format ? "*?\\/:" : "*?<>|\"\\/:"; + const unsigned char *name = file->dir_ent.name; + const unsigned char *ext = file->dir_ent.ext; /* Do not complain about (and auto-correct) the extended attribute files * of OS/2. */ @@ -286,12 +287,12 @@ static int bad_name(DOS_FILE * file) return 1; } - for (i = 8; i < 11; i++) { - if (name[i] < ' ' || name[i] == 0x7f) + for (i = 0; i < 3; i++) { + if (ext[i] < ' ' || ext[i] == 0x7f) return 1; - if (name[i] > 0x7f) + if (ext[i] > 0x7f) ++suspicious; - if (strchr(bad_chars, name[i])) + if (strchr(bad_chars, ext[i])) return 1; } @@ -306,11 +307,11 @@ static int bad_name(DOS_FILE * file) } spc = 0; - for (i = 8; i < 11; i++) { - if (name[i] == ' ') + for (i = 0; i < 3; i++) { + if (ext[i] == ' ') spc = 1; else if (spc) - /* non-space after a space not allowed, space terminates the name + /* non-space after a space not allowed, space terminates the ext * part */ return 1; } @@ -466,7 +467,7 @@ static int handle_dot(DOS_FS * fs, DOS_FILE * file, int dots) rename_file(file); return 0; case '4': - MODIFY(file, size, CT_LE_L(0)); + MODIFY(file, size, htole32(0)); MODIFY(file, attr, file->dir_ent.attr | ATTR_DIR); break; } @@ -486,10 +487,10 @@ static int check_file(DOS_FS * fs, DOS_FILE * file) unsigned long expect, curr, this, clusters, prev, walk, clusters2; if (file->dir_ent.attr & ATTR_DIR) { - if (CF_LE_L(file->dir_ent.size)) { + if (le32toh(file->dir_ent.size)) { printf("%s\n Directory has non-zero size. Fixing it.\n", path_name(file)); - MODIFY(file, size, CT_LE_L(0)); + MODIFY(file, size, htole32(0)); } if (file->parent && !strncmp((const char *)file->dir_ent.name, MSDOS_DOT, @@ -548,14 +549,14 @@ static int check_file(DOS_FS * fs, DOS_FILE * file) MODIFY_START(file, 0, fs); break; } - if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) <= + if (!(file->dir_ent.attr & ATTR_DIR) && le32toh(file->dir_ent.size) <= (unsigned long long)clusters * fs->cluster_size) { printf ("%s\n File size is %u bytes, cluster chain length is > %llu " "bytes.\n Truncating file to %u bytes.\n", path_name(file), - CF_LE_L(file->dir_ent.size), + le32toh(file->dir_ent.size), (unsigned long long)clusters * fs->cluster_size, - CF_LE_L(file->dir_ent.size)); + le32toh(file->dir_ent.size)); truncate_file(fs, file, clusters); break; } @@ -603,7 +604,7 @@ static int check_file(DOS_FS * fs, DOS_FILE * file) else MODIFY_START(owner, 0, fs); MODIFY(owner, size, - CT_LE_L((unsigned long long)clusters * + htole32((unsigned long long)clusters * fs->cluster_size)); if (restart) return 1; @@ -632,16 +633,16 @@ static int check_file(DOS_FS * fs, DOS_FILE * file) clusters++; prev = curr; } - if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) > + if (!(file->dir_ent.attr & ATTR_DIR) && le32toh(file->dir_ent.size) > (unsigned long long)clusters * fs->cluster_size) { printf ("%s\n File size is %u bytes, cluster chain length is %llu bytes." "\n Truncating file to %llu bytes.\n", path_name(file), - CF_LE_L(file->dir_ent.size), + le32toh(file->dir_ent.size), (unsigned long long)clusters * fs->cluster_size, (unsigned long long)clusters * fs->cluster_size); MODIFY(file, size, - CT_LE_L((unsigned long long)clusters * fs->cluster_size)); + htole32((unsigned long long)clusters * fs->cluster_size)); } return 0; } @@ -869,7 +870,7 @@ static void undelete(DOS_FS * fs, DOS_FILE * file) { unsigned long clusters, left, prev, walk; - clusters = left = (CF_LE_L(file->dir_ent.size) + fs->cluster_size - 1) / + clusters = left = (le32toh(file->dir_ent.size) + fs->cluster_size - 1) / fs->cluster_size; prev = 0; @@ -931,8 +932,8 @@ static void add_file(DOS_FS * fs, DOS_FILE *** chain, DOS_FILE * parent, memcpy(de.name, " ", MSDOS_NAME); de.attr = ATTR_DIR; de.size = de.time = de.date = 0; - de.start = CT_LE_W(fs->root_cluster & 0xffff); - de.starthi = CT_LE_W((fs->root_cluster >> 16) & 0xffff); + de.start = htole16(fs->root_cluster & 0xffff); + de.starthi = htole16((fs->root_cluster >> 16) & 0xffff); } if ((type = file_type(cp, (char *)de.name)) != fdt_none) { if (type == fdt_undelete && (de.attr & ATTR_DIR)) diff --git a/src/dosfsck.c b/src/dosfsck.c index 84ecdbb..f7c1f4f 100644 --- a/src/dosfsck.c +++ b/src/dosfsck.c @@ -104,7 +104,7 @@ int main(int argc, char **argv) DOS_FS fs; int salvage_files, verify, c; unsigned n_files_check = 0, n_files_verify = 0; - unsigned long free_clusters; + unsigned long free_clusters = 0; memset(&fs, 0, sizeof(fs)); rw = salvage_files = verify = 0; @@ -217,8 +217,9 @@ exit: printf("Leaving file system unchanged.\n"); } - printf("%s: %u files, %lu/%lu clusters\n", argv[optind], - n_files, fs.clusters - free_clusters, fs.clusters); + if (!boot_only) + printf("%s: %u files, %lu/%lu clusters\n", argv[optind], + n_files, fs.clusters - free_clusters, fs.clusters); return fs_close(rw) ? 1 : 0; } diff --git a/src/dosfsck.h b/src/dosfsck.h index 9fe103a..e53bf31 100644 --- a/src/dosfsck.h +++ b/src/dosfsck.h @@ -37,23 +37,8 @@ #include <linux/msdos_fs.h> -#undef CF_LE_W -#undef CF_LE_L -#undef CT_LE_W -#undef CT_LE_L - -#if __BYTE_ORDER == __BIG_ENDIAN -#include <byteswap.h> -#define CF_LE_W(v) bswap_16(v) -#define CF_LE_L(v) bswap_32(v) -#define CT_LE_W(v) CF_LE_W(v) -#define CT_LE_L(v) CF_LE_L(v) -#else -#define CF_LE_W(v) (v) -#define CF_LE_L(v) (v) -#define CT_LE_W(v) (v) -#define CT_LE_L(v) (v) -#endif /* __BIG_ENDIAN */ +#include <stddef.h> +#include <endian.h> #define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME) @@ -188,9 +173,6 @@ typedef struct { char *label; } DOS_FS; -#ifndef offsetof -#define offsetof(t,e) ((int)&(((t *)0)->e)) -#endif extern int interactive, rw, list, verbose, test, write_immed; extern int atari_format; diff --git a/src/dosfslabel.c b/src/dosfslabel.c index dd8d36a..edfb60c 100644 --- a/src/dosfslabel.c +++ b/src/dosfslabel.c @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) int i; char *device = NULL; - char *label = NULL; + char label[12] = {0}; loff_t offset; DIR_ENT de; @@ -109,8 +109,8 @@ int main(int argc, char *argv[]) device = argv[1]; if (argc == 3) { - label = argv[2]; - if (strlen(label) > 11) { + strncpy(label, argv[2], 11); + if (strlen(argv[2]) > 11) { fprintf(stderr, "dosfslabel: labels can be no longer than 11 characters\n"); exit(1); @@ -54,13 +54,13 @@ void get_fat(FAT_ENTRY * entry, void *fat, unsigned long cluster, DOS_FS * fs) (ptr[0] | ptr[1] << 8)); break; case 16: - entry->value = CF_LE_W(((unsigned short *)fat)[cluster]); + entry->value = le16toh(((unsigned short *)fat)[cluster]); break; case 32: /* According to M$, the high 4 bits of a FAT32 entry are reserved and * are not part of the cluster number. So we cut them off. */ { - unsigned long e = CF_LE_L(((unsigned int *)fat)[cluster]); + unsigned long e = le32toh(((unsigned int *)fat)[cluster]); entry->value = e & 0xfffffff; entry->reserved = e >> 28; } @@ -207,7 +207,7 @@ void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new) case 16: data = fs->fat + cluster * 2; offs = fs->fat_start + cluster * 2; - *(unsigned short *)data = CT_LE_W(new); + *(unsigned short *)data = htole16(new); size = 2; break; case 32: @@ -219,7 +219,7 @@ void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new) offs = fs->fat_start + cluster * 4; /* According to M$, the high 4 bits of a FAT32 entry are reserved and * are not part of the cluster number. So we never touch them. */ - *(unsigned long *)data = CT_LE_L((new & 0xfffffff) | + *(unsigned long *)data = htole32((new & 0xfffffff) | (curEntry.reserved << 28)); size = 4; } @@ -478,12 +478,12 @@ void reclaim_file(DOS_FS * fs) loff_t offset; files++; offset = alloc_rootdir_entry(fs, &de, "FSCK%04dREC"); - de.start = CT_LE_W(i & 0xffff); + de.start = htole16(i & 0xffff); if (fs->fat_bits == 32) - de.starthi = CT_LE_W(i >> 16); + de.starthi = htole16(i >> 16); for (walk = i; walk > 0 && walk != -1; walk = next_cluster(fs, walk)) { - de.size = CT_LE_L(CF_LE_L(de.size) + fs->cluster_size); + de.size = htole32(le32toh(de.size) + fs->cluster_size); reclaimed++; } fs_write(offset, sizeof(DIR_ENT), &de); @@ -540,7 +540,7 @@ unsigned long update_free(DOS_FS * fs) } if (do_set) { - unsigned long le_free = CT_LE_L(free); + unsigned long le_free = htole32(free); fs->free_clusters = free; fs_write(fs->fsinfo_start + offsetof(struct info_sector, free_clusters), sizeof(le_free), &le_free); @@ -124,7 +124,7 @@ static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q) cp = out = use_q ? qalloc(&mem_queue, len + 1) : alloc(len + 1); for (up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); up += 2) { - if ((x = wctombs(cp, BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1) + if ((x = wctombs((char *)cp, BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1) cp += x; else if (UNICODE_CONVERTABLE(up[0], up[1])) *cp++ = up[0]; @@ -388,7 +388,7 @@ void lfn_add_slot(DIR_ENT * de, loff_t dir_offset) sizeof(lfn->reserved), &lfn->reserved); } } - if (lfn->start != CT_LE_W(0)) { + if (lfn->start != htole16(0)) { printf("Start cluster field in VFAT long filename slot is not 0 " "(but 0x%04x).\n", lfn->start); if (interactive) @@ -396,7 +396,7 @@ void lfn_add_slot(DIR_ENT * de, loff_t dir_offset) else printf("Auto-setting to 0.\n"); if (!interactive || get_key("12", "?") == '1') { - lfn->start = CT_LE_W(0); + lfn->start = htole16(0); fs_write(dir_offset + offsetof(LFN_ENT, start), sizeof(lfn->start), &lfn->start); } @@ -462,8 +462,10 @@ char *lfn_get(DIR_ENT * de, loff_t * lfn_offset) } } - for (sum = 0, i = 0; i < 11; i++) + for (sum = 0, i = 0; i < 8; i++) sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i]; + for (i = 0; i < 3; i++) + sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->ext[i]; if (sum != lfn_checksum) { /* checksum doesn't match, long name doesn't apply to this alias */ /* Causes: 1) alias renamed */ diff --git a/src/mkdosfs.c b/src/mkdosfs.c index d7c94f5..4142052 100644 --- a/src/mkdosfs.c +++ b/src/mkdosfs.c @@ -64,35 +64,10 @@ #include <time.h> #include <errno.h> #include <ctype.h> +#include <endian.h> #include <asm/types.h> -#if __BYTE_ORDER == __BIG_ENDIAN - -#include <asm/byteorder.h> -#ifdef __le16_to_cpu -/* ++roman: 2.1 kernel headers define these function, they're probably more - * efficient then coding the swaps machine-independently. */ -#define CF_LE_W __le16_to_cpu -#define CF_LE_L __le32_to_cpu -#define CT_LE_W __cpu_to_le16 -#define CT_LE_L __cpu_to_le32 -#else -#define CF_LE_W(v) ((((v) & 0xff) << 8) | (((v) >> 8) & 0xff)) -#define CF_LE_L(v) (((unsigned)(v)>>24) | (((unsigned)(v)>>8)&0xff00) | \ - (((unsigned)(v)<<8)&0xff0000) | ((unsigned)(v)<<24)) -#define CT_LE_W(v) CF_LE_W(v) -#define CT_LE_L(v) CF_LE_L(v) -#endif /* defined(__le16_to_cpu) */ - -#else - -#define CF_LE_W(v) (v) -#define CF_LE_L(v) (v) -#define CT_LE_W(v) (v) -#define CT_LE_L(v) (v) - -#endif /* __BIG_ENDIAN */ /* In earlier versions, an own llseek() was used, but glibc lseek() is * sufficient (or even better :) for 64 bit offsets in the meantime */ @@ -121,7 +96,7 @@ /* Compute ceil(a/b) */ -inline int cdiv(int a, int b) +static inline int cdiv(int a, int b) { return (a + b - 1) / b; } @@ -416,6 +391,8 @@ static long do_check(char *buffer, int try, off_t current_block) static void alarm_intr(int alnum) { + (void)alnum; + if (currently_testing >= blocks) return; @@ -601,8 +578,8 @@ static void establish_params(int device_num, int size) if (ioctl(dev, FDGETPRM, ¶m)) /* Can we get the diskette geometry? */ die("unable to get diskette geometry for '%s'"); } - bs.secs_track = CT_LE_W(param.sect); /* Set up the geometry information */ - bs.heads = CT_LE_W(param.head); + bs.secs_track = htole16(param.sect); /* Set up the geometry information */ + bs.heads = htole16(param.head); switch (param.size) { /* Set up the media descriptor byte */ case 720: /* 5.25", 2, 9, 40 - 360K */ bs.media = (char)0xfd; @@ -647,32 +624,32 @@ floppy_default: switch (loop_size) { /* Assuming the loop device -> floppy later */ case 720: /* 5.25", 2, 9, 40 - 360K */ - bs.secs_track = CF_LE_W(9); - bs.heads = CF_LE_W(2); + bs.secs_track = le16toh(9); + bs.heads = le16toh(2); bs.media = (char)0xfd; bs.cluster_size = (char)2; def_root_dir_entries = 112; break; case 1440: /* 3.5", 2, 9, 80 - 720K */ - bs.secs_track = CF_LE_W(9); - bs.heads = CF_LE_W(2); + bs.secs_track = le16toh(9); + bs.heads = le16toh(2); bs.media = (char)0xf9; bs.cluster_size = (char)2; def_root_dir_entries = 112; break; case 2400: /* 5.25", 2, 15, 80 - 1200K */ - bs.secs_track = CF_LE_W(15); - bs.heads = CF_LE_W(2); + bs.secs_track = le16toh(15); + bs.heads = le16toh(2); bs.media = (char)0xf9; bs.cluster_size = (char)(atari_format ? 2 : 1); def_root_dir_entries = 224; break; case 5760: /* 3.5", 2, 36, 80 - 2880K */ - bs.secs_track = CF_LE_W(36); - bs.heads = CF_LE_W(2); + bs.secs_track = le16toh(36); + bs.heads = le16toh(2); bs.media = (char)0xf0; bs.cluster_size = (char)2; bs.dir_entries[0] = (char)224; @@ -680,8 +657,8 @@ floppy_default: break; case 2880: /* 3.5", 2, 18, 80 - 1440K */ - bs.secs_track = CF_LE_W(18); - bs.heads = CF_LE_W(2); + bs.secs_track = le16toh(18); + bs.heads = le16toh(2); bs.media = (char)0xf0; bs.cluster_size = (char)(atari_format ? 2 : 1); def_root_dir_entries = 224; @@ -690,8 +667,8 @@ floppy_default: default: /* Anything else: default hd setup */ printf("Loop device does not match a floppy size, using " "default hd params\n"); - bs.secs_track = CT_LE_W(32); /* these are fake values... */ - bs.heads = CT_LE_W(64); + bs.secs_track = htole16(32); /* these are fake values... */ + bs.heads = htole16(64); goto def_hd_params; } } else @@ -702,11 +679,11 @@ floppy_default: if (ioctl(dev, HDIO_GETGEO, &geometry) || geometry.sectors == 0 || geometry.heads == 0) { printf("unable to get drive geometry, using default 255/63\n"); - bs.secs_track = CT_LE_W(63); - bs.heads = CT_LE_W(255); + bs.secs_track = htole16(63); + bs.heads = htole16(255); } else { - bs.secs_track = CT_LE_W(geometry.sectors); /* Set up the geometry information */ - bs.heads = CT_LE_W(geometry.heads); + bs.secs_track = htole16(geometry.sectors); /* Set up the geometry information */ + bs.heads = htole16(geometry.heads); } def_hd_params: bs.media = (char)0xf8; /* Set up the media descriptor for a hard drive */ @@ -761,11 +738,13 @@ static void setup_tables(void) struct msdos_volume_info *vi = (size_fat == 32 ? &bs.fat32.vi : &bs.oldfat.vi); - if (atari_format) + if (atari_format) { /* On Atari, the first few bytes of the boot sector are assigned * differently: The jump code is only 2 bytes (and m68k machine code * :-), then 6 bytes filler (ignored), then 3 byte serial number. */ - memcpy(bs.system_id - 1, "mkdosf", 6); + bs.boot_jump[2] = 'm'; + strcpy((char *)bs.system_id, "kdosf"); + } else strcpy((char *)bs.system_id, "mkdosfs"); if (sectors_per_cluster) @@ -808,7 +787,7 @@ static void setup_tables(void) } else { memcpy(bs.oldfat.boot_code, dummy_boot_code, BOOTCODE_SIZE); } - bs.boot_sign = CT_LE_W(BOOT_SIGN); + bs.boot_sign = htole16(BOOT_SIGN); } else { memcpy(bs.boot_jump, dummy_boot_jump_m68k, 2); } @@ -822,15 +801,15 @@ static void setup_tables(void) if (size_fat == 32 && reserved_sectors < 2) die("On FAT32 at least 2 reserved sectors are needed."); } - bs.reserved = CT_LE_W(reserved_sectors); + bs.reserved = htole16(reserved_sectors); if (verbose >= 2) printf("Using %d reserved sectors\n", reserved_sectors); bs.fats = (char)nr_fats; if (!atari_format || size_fat == 32) - bs.hidden = CT_LE_L(hidden_sectors); + bs.hidden = htole32(hidden_sectors); else { /* In Atari format, hidden is a 16 bit field */ - __u16 hidden = CT_LE_W(hidden_sectors); + __u16 hidden = htole16(hidden_sectors); if (hidden_sectors & ~0xffff) die("#hidden doesn't fit in 16bit field of Atari format\n"); memcpy(&bs.hidden, &hidden, 2); @@ -971,7 +950,7 @@ static void setup_tables(void) case 12: cluster_count = clust12; fat_length = fatlength12; - bs.fat_length = CT_LE_W(fatlength12); + bs.fat_length = htole16(fatlength12); memcpy(vi->fs_type, MSDOS_FAT12_SIGN, 8); break; @@ -995,7 +974,7 @@ static void setup_tables(void) } cluster_count = clust16; fat_length = fatlength16; - bs.fat_length = CT_LE_W(fatlength16); + bs.fat_length = htole16(fatlength16); memcpy(vi->fs_type, MSDOS_FAT16_SIGN, 8); break; @@ -1005,8 +984,8 @@ static void setup_tables(void) "WARNING: Not enough clusters for a 32 bit FAT!\n"); cluster_count = clust32; fat_length = fatlength32; - bs.fat_length = CT_LE_W(0); - bs.fat32.fat32_length = CT_LE_L(fatlength32); + bs.fat_length = htole16(0); + bs.fat32.fat32_length = htole32(fatlength32); memcpy(vi->fs_type, MSDOS_FAT32_SIGN, 8); root_dir_entries = 0; break; @@ -1017,7 +996,7 @@ static void setup_tables(void) /* Adjust the reserved number of sectors for alignment */ reserved_sectors = align_object(reserved_sectors, bs.cluster_size); - bs.reserved = CT_LE_W(reserved_sectors); + bs.reserved = htole16(reserved_sectors); /* Adjust the number of root directory entries to help enforce alignment */ if (align_structures) { @@ -1100,10 +1079,10 @@ static void setup_tables(void) cluster_count = clusters; if (size_fat != 32) - bs.fat_length = CT_LE_W(fat_length); + bs.fat_length = htole16(fat_length); else { bs.fat_length = 0; - bs.fat32.fat32_length = CT_LE_L(fat_length); + bs.fat32.fat32_length = htole32(fat_length); } } @@ -1115,11 +1094,11 @@ static void setup_tables(void) if (size_fat == 32) { /* set up additional FAT32 fields */ - bs.fat32.flags = CT_LE_W(0); + bs.fat32.flags = htole16(0); bs.fat32.version[0] = 0; bs.fat32.version[1] = 0; - bs.fat32.root_cluster = CT_LE_L(2); - bs.fat32.info_sector = CT_LE_W(1); + bs.fat32.root_cluster = htole32(2); + bs.fat32.info_sector = htole16(1); if (!backup_boot) backup_boot = (reserved_sectors >= 7) ? 6 : (reserved_sectors >= 2) ? reserved_sectors - 1 : 0; @@ -1132,7 +1111,7 @@ static void setup_tables(void) if (verbose >= 2) printf("Using sector %d as backup boot sector (0 = none)\n", backup_boot); - bs.fat32.backup_boot = CT_LE_W(backup_boot); + bs.fat32.backup_boot = htole16(backup_boot); memset(&bs.fat32.reserved2, 0, sizeof(bs.fat32.reserved2)); } @@ -1147,12 +1126,12 @@ static void setup_tables(void) if (num_sectors >= 65536) { bs.sectors[0] = (char)0; bs.sectors[1] = (char)0; - bs.total_sect = CT_LE_L(num_sectors); + bs.total_sect = htole32(num_sectors); } else { bs.sectors[0] = (char)(num_sectors & 0x00ff); bs.sectors[1] = (char)((num_sectors & 0xff00) >> 8); if (!atari_format) - bs.total_sect = CT_LE_L(0); + bs.total_sect = htole32(0); } if (!atari_format) @@ -1176,9 +1155,9 @@ static void setup_tables(void) if (verbose) { printf("%s has %d head%s and %d sector%s per track,\n", - device_name, CF_LE_W(bs.heads), - (CF_LE_W(bs.heads) != 1) ? "s" : "", CF_LE_W(bs.secs_track), - (CF_LE_W(bs.secs_track) != 1) ? "s" : ""); + device_name, le16toh(bs.heads), + (le16toh(bs.heads) != 1) ? "s" : "", le16toh(bs.secs_track), + (le16toh(bs.secs_track) != 1) ? "s" : ""); printf("logical sector size is %d,\n", sector_size); printf("using 0x%02x media descriptor, with %d sectors;\n", (int)(bs.media), num_sectors); @@ -1247,20 +1226,20 @@ static void setup_tables(void) memcpy(de->ext, volume_name + 8, 3); de->attr = ATTR_VOLUME; ctime = localtime(&create_time); - de->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) + + de->time = htole16((unsigned short)((ctime->tm_sec >> 1) + (ctime->tm_min << 5) + (ctime->tm_hour << 11))); de->date = - CT_LE_W((unsigned short)(ctime->tm_mday + + htole16((unsigned short)(ctime->tm_mday + ((ctime->tm_mon + 1) << 5) + ((ctime->tm_year - 80) << 9))); de->ctime_ms = 0; de->ctime = de->time; de->cdate = de->date; de->adate = de->date; - de->starthi = CT_LE_W(0); - de->start = CT_LE_W(0); - de->size = CT_LE_L(0); + de->starthi = htole16(0); + de->start = htole16(0); + de->size = htole32(0); } if (size_fat == 32) { @@ -1280,13 +1259,13 @@ static void setup_tables(void) info_sector[3] = 'A'; /* Magic for fsinfo structure */ - info->signature = CT_LE_L(0x61417272); + info->signature = htole32(0x61417272); /* We've allocated cluster 2 for the root dir. */ - info->free_clusters = CT_LE_L(cluster_count - 1); - info->next_cluster = CT_LE_L(2); + info->free_clusters = htole32(cluster_count - 1); + info->next_cluster = htole32(2); /* Info sector also must have boot sign */ - *(__u16 *) (info_sector + 0x1fe) = CT_LE_W(BOOT_SIGN); + *(__u16 *) (info_sector + 0x1fe) = htole16(BOOT_SIGN); } if (!(blank_sector = malloc(sector_size))) @@ -1324,7 +1303,7 @@ static void write_tables(void) int fat_length; fat_length = (size_fat == 32) ? - CF_LE_L(bs.fat32.fat32_length) : CF_LE_W(bs.fat_length); + le32toh(bs.fat32.fat32_length) : le16toh(bs.fat_length); seekto(0, "start of device"); /* clear all reserved sectors */ @@ -1335,7 +1314,7 @@ static void write_tables(void) writebuf((char *)&bs, sizeof(struct msdos_boot_sector), "boot sector"); /* on FAT32, write the info sector and backup boot sector */ if (size_fat == 32) { - seekto(CF_LE_W(bs.fat32.info_sector) * sector_size, "info sector"); + seekto(le16toh(bs.fat32.info_sector) * sector_size, "info sector"); writebuf(info_sector, 512, "info sector"); if (backup_boot != 0) { seekto(backup_boot * sector_size, "backup boot sector"); @@ -1366,7 +1345,7 @@ static void write_tables(void) /* Report the command usage and return a failure error code */ -void usage(void) +static void usage(void) { fatal_error("\ Usage: mkdosfs [-a][-A][-c][-C][-v][-I][-l bad-block-file][-b backup-boot-sector]\n\ diff --git a/src/version.h b/src/version.h index a7c41a0..6c5ab91 100644 --- a/src/version.h +++ b/src/version.h @@ -23,7 +23,7 @@ #ifndef _version_h #define _version_h -#define VERSION "3.0.16" -#define VERSION_DATE "01 Mar 2013" +#define VERSION "3.0.17" +#define VERSION_DATE "29 May 2013" #endif |