dosfstools  4.2
About: dosfstools are utilities to create, check and label (MS-DOS) FAT filesystems.
  Fossies Dox: dosfstools-4.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mkfs.fat.c File Reference
#include "version.h"
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <stdint.h>
#include <getopt.h>
#include "endian_compat.h"
#include "common.h"
#include "msdos_fs.h"
#include "device_info.h"
#include "charconv.h"
Include dependency graph for mkfs.fat.c:

Go to the source code of this file.

Data Structures

struct  msdos_volume_info
 
struct  msdos_boot_sector
 
struct  fat32_fsinfo
 

Macros

#define TRUE   1 /* Boolean constants */
 
#define FALSE   0
 
#define TEST_BUFFER_BLOCKS   16
 
#define BLOCK_SIZE   1024
 
#define HARD_SECTOR_SIZE   512
 
#define SECTORS_PER_BLOCK   ( BLOCK_SIZE / HARD_SECTOR_SIZE )
 
#define NO_NAME   "NO NAME "
 
#define mark_sector_bad(sector)   mark_FAT_sector( sector, FAT_BAD )
 
#define FAT_EOF   (atari_format ? 0x0fffffff : 0x0ffffff8)
 
#define FAT_BAD   0x0ffffff7
 
#define MSDOS_EXT_SIGN   0x29 /* extended boot sector signature */
 
#define MSDOS_FAT12_SIGN   "FAT12 " /* FAT12 filesystem signature */
 
#define MSDOS_FAT16_SIGN   "FAT16 " /* FAT16 filesystem signature */
 
#define MSDOS_FAT32_SIGN   "FAT32 " /* FAT32 filesystem signature */
 
#define BOOT_SIGN   0xAA55 /* Boot sector magic number */
 
#define MAX_CLUST_12   4084
 
#define MIN_CLUST_16   4087
 
#define MAX_CLUST_16   65524
 
#define MIN_CLUST_32   65525
 
#define MAX_CLUST_32   268435446
 
#define OLDGEMDOS_MAX_SECTORS   32765
 
#define GEMDOS_MAX_SECTORS   65531
 
#define GEMDOS_MAX_SECTOR_SIZE   (16*1024)
 
#define BOOTCODE_SIZE   448
 
#define BOOTCODE_FAT32_SIZE   420
 
#define fat32   fstype._fat32
 
#define oldfat   fstype._oldfat
 
#define MSG_OFFSET_OFFSET   3
 
#define MESSAGE_OFFSET   29 /* Offset of message in above code */
 
#define error(str)
 
#define seekto(pos, errstr)
 
#define writebuf(buf, size, errstr)
 

Functions

static int cdiv (int a, int b)
 
static void mark_FAT_cluster (int cluster, unsigned int value)
 
static void mark_FAT_sector (int sector, unsigned int value)
 
static long do_check (char *buffer, int try, off_t current_block)
 
static void alarm_intr (int alnum)
 
static void check_blocks (void)
 
static void get_list_blocks (char *filename)
 
static void check_mount (char *device_name)
 
static void establish_params (struct device_info *info)
 
static void setup_tables (void)
 
static void write_tables (void)
 
static unsigned int align_object (unsigned int sectors, unsigned int clustsize)
 
static void usage (const char *name, int exitval)
 
int main (int argc, char **argv)
 

Variables

unsigned char dummy_boot_jump [3] = { 0xeb, 0x3c, 0x90 }
 
unsigned char dummy_boot_jump_m68k [2] = { 0x60, 0x1c }
 
char dummy_boot_code [448]
 
static char initial_volume_name [] = "NO NAME "
 
static char * device_name = NULL
 
static int check = 0
 
static int verbose = 0
 
static long volume_id
 
static time_t create_time = -1
 
static char * volume_name = initial_volume_name
 
static unsigned long long blocks
 
static unsigned sector_size = 512
 
static int sector_size_set = 0
 
static int backup_boot = 0
 
static int backup_boot_set = 0
 
static int info_sector = 0
 
static int reserved_sectors = 0
 
static int badblocks = 0
 
static int nr_fats = 2
 
static int size_fat = 0
 
static int size_fat_by_user = 0
 
static int dev = -1
 
static off_t part_sector = 0
 
static int ignore_safety_checks = 0
 
static off_t currently_testing = 0
 
static struct msdos_boot_sector bs
 
static int start_data_sector
 
static int start_data_block
 
static unsigned char * fat
 
static unsigned alloced_fat_length
 
static unsigned fat_entries
 
static unsigned char * info_sector_buffer
 
static struct msdos_dir_entryroot_dir
 
static int size_root_dir
 
static uint32_t num_sectors
 
static int sectors_per_cluster = 0
 
static int root_dir_entries = 0
 
static int root_dir_entries_set = 0
 
static char * blank_sector
 
static unsigned hidden_sectors = 0
 
static int hidden_sectors_by_user = 0
 
static int drive_number_option = 0
 
static int drive_number_by_user = 0
 
static int fat_media_byte = 0
 
static int malloc_entire_fat = 0
 
static int align_structures = 1
 
static int orphaned_sectors = 0
 
static int invariant = 0
 
static int fill_mbr_partition = -1
 

Macro Definition Documentation

◆ BLOCK_SIZE

#define BLOCK_SIZE   1024

Definition at line 80 of file mkfs.fat.c.

◆ BOOT_SIGN

#define BOOT_SIGN   0xAA55 /* Boot sector magic number */

Definition at line 108 of file mkfs.fat.c.

◆ BOOTCODE_FAT32_SIZE

#define BOOTCODE_FAT32_SIZE   420

Definition at line 136 of file mkfs.fat.c.

◆ BOOTCODE_SIZE

#define BOOTCODE_SIZE   448

Definition at line 135 of file mkfs.fat.c.

◆ error

#define error (   str)
Value:
do { \
free (fat); \
free (root_dir); \
die (str); \
} while(0)
static struct msdos_dir_entry * root_dir
Definition: mkfs.fat.c:261
static unsigned char * fat
Definition: mkfs.fat.c:257
static unsigned char * info_sector_buffer
Definition: mkfs.fat.c:260

Definition at line 1351 of file mkfs.fat.c.

◆ FALSE

#define FALSE   0

Definition at line 77 of file mkfs.fat.c.

◆ fat32

#define fat32   fstype._fat32

Definition at line 185 of file mkfs.fat.c.

◆ FAT_BAD

#define FAT_BAD   0x0ffffff7

Definition at line 101 of file mkfs.fat.c.

◆ FAT_EOF

#define FAT_EOF   (atari_format ? 0x0fffffff : 0x0ffffff8)

Definition at line 100 of file mkfs.fat.c.

◆ GEMDOS_MAX_SECTOR_SIZE

#define GEMDOS_MAX_SECTOR_SIZE   (16*1024)

Definition at line 133 of file mkfs.fat.c.

◆ GEMDOS_MAX_SECTORS

#define GEMDOS_MAX_SECTORS   65531

Definition at line 132 of file mkfs.fat.c.

◆ HARD_SECTOR_SIZE

#define HARD_SECTOR_SIZE   512

Definition at line 81 of file mkfs.fat.c.

◆ mark_sector_bad

#define mark_sector_bad (   sector)    mark_FAT_sector( sector, FAT_BAD )

Definition at line 90 of file mkfs.fat.c.

◆ MAX_CLUST_12

#define MAX_CLUST_12   4084

Definition at line 116 of file mkfs.fat.c.

◆ MAX_CLUST_16

#define MAX_CLUST_16   65524

Definition at line 124 of file mkfs.fat.c.

◆ MAX_CLUST_32

#define MAX_CLUST_32   268435446

Definition at line 129 of file mkfs.fat.c.

◆ MESSAGE_OFFSET

#define MESSAGE_OFFSET   29 /* Offset of message in above code */

Definition at line 227 of file mkfs.fat.c.

◆ MIN_CLUST_16

#define MIN_CLUST_16   4087

Definition at line 117 of file mkfs.fat.c.

◆ MIN_CLUST_32

#define MIN_CLUST_32   65525

Definition at line 125 of file mkfs.fat.c.

◆ MSDOS_EXT_SIGN

#define MSDOS_EXT_SIGN   0x29 /* extended boot sector signature */

Definition at line 103 of file mkfs.fat.c.

◆ MSDOS_FAT12_SIGN

#define MSDOS_FAT12_SIGN   "FAT12 " /* FAT12 filesystem signature */

Definition at line 104 of file mkfs.fat.c.

◆ MSDOS_FAT16_SIGN

#define MSDOS_FAT16_SIGN   "FAT16 " /* FAT16 filesystem signature */

Definition at line 105 of file mkfs.fat.c.

◆ MSDOS_FAT32_SIGN

#define MSDOS_FAT32_SIGN   "FAT32 " /* FAT32 filesystem signature */

Definition at line 106 of file mkfs.fat.c.

◆ MSG_OFFSET_OFFSET

#define MSG_OFFSET_OFFSET   3

Definition at line 204 of file mkfs.fat.c.

◆ NO_NAME

#define NO_NAME   "NO NAME "

Definition at line 84 of file mkfs.fat.c.

◆ oldfat

#define oldfat   fstype._oldfat

Definition at line 186 of file mkfs.fat.c.

◆ OLDGEMDOS_MAX_SECTORS

#define OLDGEMDOS_MAX_SECTORS   32765

Definition at line 131 of file mkfs.fat.c.

◆ SECTORS_PER_BLOCK

#define SECTORS_PER_BLOCK   ( BLOCK_SIZE / HARD_SECTOR_SIZE )

Definition at line 82 of file mkfs.fat.c.

◆ seekto

#define seekto (   pos,
  errstr 
)
Value:
do { \
off_t __pos = (pos); \
if (lseek (dev, part_sector * sector_size + __pos, SEEK_SET) != part_sector * sector_size + __pos) \
error ("seek to " errstr " failed whilst writing tables"); \
} while(0)
static int dev
Definition: mkfs.fat.c:250
static off_t part_sector
Definition: mkfs.fat.c:251
static unsigned sector_size
Definition: mkfs.fat.c:240

Definition at line 1359 of file mkfs.fat.c.

◆ TEST_BUFFER_BLOCKS

#define TEST_BUFFER_BLOCKS   16

Definition at line 79 of file mkfs.fat.c.

◆ TRUE

#define TRUE   1 /* Boolean constants */

Definition at line 76 of file mkfs.fat.c.

◆ writebuf

#define writebuf (   buf,
  size,
  errstr 
)
Value:
do { \
int __size = (size); \
if (write (dev, buf, __size) != __size) \
error ("failed whilst writing " errstr); \
} while(0)

Definition at line 1366 of file mkfs.fat.c.

Function Documentation

◆ alarm_intr()

static void alarm_intr ( int  alnum)
static

Definition at line 376 of file mkfs.fat.c.

References blocks, and currently_testing.

Referenced by check_blocks().

◆ align_object()

static unsigned int align_object ( unsigned int  sectors,
unsigned int  clustsize 
)
static

Definition at line 644 of file mkfs.fat.c.

References align_structures.

Referenced by setup_tables().

◆ cdiv()

static int cdiv ( int  a,
int  b 
)
inlinestatic

Definition at line 94 of file mkfs.fat.c.

Referenced by setup_tables().

◆ check_blocks()

static void check_blocks ( void  )
static

◆ check_mount()

static void check_mount ( char *  device_name)
static

Definition at line 514 of file mkfs.fat.c.

References device_name, die(), and is_device_mounted().

Referenced by main().

◆ do_check()

static long do_check ( char *  buffer,
int  try,
off_t  current_block 
)
static

Definition at line 354 of file mkfs.fat.c.

References BLOCK_SIZE, dev, die(), part_sector, and sector_size.

Referenced by check_blocks().

◆ establish_params()

◆ get_list_blocks()

static void get_list_blocks ( char *  filename)
static

Definition at line 434 of file mkfs.fat.c.

References badblocks, check, die(), mark_sector_bad, num_sectors, SECTORS_PER_BLOCK, and start_data_sector.

Referenced by main().

◆ main()

◆ mark_FAT_cluster()

static void mark_FAT_cluster ( int  cluster,
unsigned int  value 
)
static

Definition at line 298 of file mkfs.fat.c.

References die(), fat, fat_entries, and size_fat.

Referenced by mark_FAT_sector(), and setup_tables().

◆ mark_FAT_sector()

static void mark_FAT_sector ( int  sector,
unsigned int  value 
)
static

◆ setup_tables()

static void setup_tables ( void  )
static

Definition at line 654 of file mkfs.fat.c.

References msdos_dir_entry::adate, align_object(), align_structures, alloced_fat_length, atari_format, msdos_dir_entry::attr, ATTR_VOLUME, msdos_boot_sector::backup_boot, backup_boot, backup_boot_set, blank_sector, BLOCK_SIZE, blocks, msdos_boot_sector::boot_code, msdos_boot_sector::boot_jump, BOOT_SIGN, msdos_boot_sector::boot_sign, BOOTCODE_FAT32_SIZE, BOOTCODE_SIZE, bs, msdos_dir_entry::cdate, cdiv(), msdos_boot_sector::cluster_size, create_time, msdos_dir_entry::ctime, msdos_dir_entry::ctime_cs, msdos_dir_entry::date, device_name, die(), msdos_boot_sector::dir_entries, msdos_volume_info::drive_number, drive_number_by_user, drive_number_option, dummy_boot_code, dummy_boot_jump, dummy_boot_jump_m68k, msdos_volume_info::ext_boot_sign, FALSE, fat, msdos_boot_sector::fat32_length, fat_entries, FAT_EOF, msdos_boot_sector::fat_length, fat_media_byte, msdos_boot_sector::fats, fd, fill_mbr_partition, msdos_boot_sector::flags, fat32_fsinfo::free_clusters, msdos_volume_info::fs_type, GEMDOS_MAX_SECTOR_SIZE, GEMDOS_MAX_SECTORS, generate_volume_id(), HARD_SECTOR_SIZE, msdos_boot_sector::heads, msdos_boot_sector::hidden, hidden_sectors, msdos_boot_sector::info_sector, info_sector_buffer, invariant, local_string_to_dos_string(), malloc_entire_fat, mark_FAT_cluster(), MAX_CLUST_12, MAX_CLUST_16, MAX_CLUST_32, msdos_boot_sector::media, MESSAGE_OFFSET, MIN_CLUST_16, MIN_CLUST_32, MSDOS_EXT_SIGN, MSDOS_FAT12_SIGN, MSDOS_FAT16_SIGN, MSDOS_FAT32_SIGN, MSDOS_NAME, MSG_OFFSET_OFFSET, msdos_dir_entry::name, fat32_fsinfo::next_cluster, NO_NAME, nr_fats, num_sectors, OLDGEMDOS_MAX_SECTORS, orphaned_sectors, msdos_boot_sector::reserved, msdos_boot_sector::reserved2, reserved_sectors, msdos_boot_sector::root_cluster, root_dir, root_dir_entries, root_dir_entries_set, msdos_boot_sector::secs_track, msdos_boot_sector::sector_size, sector_size, sector_size_set, msdos_boot_sector::sectors, SECTORS_PER_BLOCK, sectors_per_cluster, fat32_fsinfo::signature, msdos_dir_entry::size, size_fat, size_fat_by_user, size_root_dir, msdos_dir_entry::start, start_data_block, start_data_sector, msdos_dir_entry::starthi, msdos_boot_sector::system_id, msdos_dir_entry::time, msdos_boot_sector::total_sect, validate_volume_label(), verbose, msdos_boot_sector::version, msdos_boot_sector::vi, msdos_volume_info::volume_id, volume_id, msdos_volume_info::volume_label, and volume_name.

Referenced by main().

◆ usage()

static void usage ( const char *  name,
int  exitval 
)
static

Definition at line 1425 of file mkfs.fat.c.

References DEFAULT_DOS_CODEPAGE.

Referenced by main().

◆ write_tables()

Variable Documentation

◆ align_structures

int align_structures = 1
static

Definition at line 274 of file mkfs.fat.c.

Referenced by align_object(), main(), and setup_tables().

◆ alloced_fat_length

unsigned alloced_fat_length
static

Definition at line 258 of file mkfs.fat.c.

Referenced by setup_tables(), and write_tables().

◆ backup_boot

int backup_boot = 0
static

Definition at line 242 of file mkfs.fat.c.

Referenced by check_backup_boot(), main(), setup_tables(), and write_tables().

◆ backup_boot_set

int backup_boot_set = 0
static

Definition at line 243 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ badblocks

int badblocks = 0
static

Definition at line 246 of file mkfs.fat.c.

Referenced by check_blocks(), and get_list_blocks().

◆ blank_sector

char* blank_sector
static

Definition at line 267 of file mkfs.fat.c.

Referenced by setup_tables(), and write_tables().

◆ blocks

unsigned long long blocks
static

Definition at line 239 of file mkfs.fat.c.

Referenced by alarm_intr(), check_blocks(), main(), and setup_tables().

◆ bs

struct msdos_boot_sector bs
static

Definition at line 253 of file mkfs.fat.c.

Referenced by establish_params(), main(), mark_FAT_sector(), setup_tables(), and write_tables().

◆ check

int check = 0
static

Definition at line 234 of file mkfs.fat.c.

Referenced by get_list_blocks(), and main().

◆ create_time

time_t create_time = -1
static

Definition at line 237 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ currently_testing

off_t currently_testing = 0
static

Definition at line 253 of file mkfs.fat.c.

Referenced by alarm_intr(), and check_blocks().

◆ dev

int dev = -1
static

Definition at line 250 of file mkfs.fat.c.

Referenced by do_check(), and main().

◆ device_name

char* device_name = NULL
static

Definition at line 233 of file mkfs.fat.c.

Referenced by check_mount(), main(), and setup_tables().

◆ drive_number_by_user

int drive_number_by_user = 0
static

Definition at line 271 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ drive_number_option

int drive_number_option = 0
static

Definition at line 270 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ dummy_boot_code

char dummy_boot_code[448]
Initial value:
= "\x0e"
"\x1f"
"\xbe\x5b\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. Please insert a bootable floppy and\r\n"
"press any key to try again ... \r\n"

Definition at line 205 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ dummy_boot_jump

unsigned char dummy_boot_jump[3] = { 0xeb, 0x3c, 0x90 }

Definition at line 200 of file mkfs.fat.c.

Referenced by setup_tables().

◆ dummy_boot_jump_m68k

unsigned char dummy_boot_jump_m68k[2] = { 0x60, 0x1c }

Definition at line 202 of file mkfs.fat.c.

Referenced by setup_tables().

◆ fat

unsigned char* fat
static

Definition at line 257 of file mkfs.fat.c.

Referenced by get_fat(), mark_FAT_cluster(), setup_tables(), and write_tables().

◆ fat_entries

unsigned fat_entries
static

Definition at line 259 of file mkfs.fat.c.

Referenced by mark_FAT_cluster(), and setup_tables().

◆ fat_media_byte

int fat_media_byte = 0
static

Definition at line 272 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ fill_mbr_partition

int fill_mbr_partition = -1
static

Definition at line 279 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ hidden_sectors

unsigned hidden_sectors = 0
static

Definition at line 268 of file mkfs.fat.c.

Referenced by establish_params(), main(), and setup_tables().

◆ hidden_sectors_by_user

int hidden_sectors_by_user = 0
static

Definition at line 269 of file mkfs.fat.c.

Referenced by establish_params(), and main().

◆ ignore_safety_checks

int ignore_safety_checks = 0
static

Definition at line 252 of file mkfs.fat.c.

Referenced by main().

◆ info_sector

int info_sector = 0
static

Definition at line 244 of file mkfs.fat.c.

◆ info_sector_buffer

unsigned char* info_sector_buffer
static

Definition at line 260 of file mkfs.fat.c.

Referenced by setup_tables(), and write_tables().

◆ initial_volume_name

char initial_volume_name[] = "NO NAME "
static

Definition at line 229 of file mkfs.fat.c.

◆ invariant

int invariant = 0
static

Definition at line 276 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ malloc_entire_fat

int malloc_entire_fat = 0
static

Definition at line 273 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ nr_fats

int nr_fats = 2
static

Definition at line 247 of file mkfs.fat.c.

Referenced by main(), setup_tables(), and write_tables().

◆ num_sectors

uint32_t num_sectors
static

Definition at line 263 of file mkfs.fat.c.

Referenced by get_list_blocks(), mark_FAT_sector(), and setup_tables().

◆ orphaned_sectors

int orphaned_sectors = 0
static

Definition at line 275 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ part_sector

off_t part_sector = 0
static

Definition at line 251 of file mkfs.fat.c.

Referenced by do_check(), establish_params(), and main().

◆ reserved_sectors

int reserved_sectors = 0
static

Definition at line 245 of file mkfs.fat.c.

Referenced by main(), setup_tables(), and write_tables().

◆ root_dir

struct msdos_dir_entry* root_dir
static

Definition at line 261 of file mkfs.fat.c.

Referenced by setup_tables(), and write_tables().

◆ root_dir_entries

int root_dir_entries = 0
static

Definition at line 265 of file mkfs.fat.c.

Referenced by establish_params(), main(), and setup_tables().

◆ root_dir_entries_set

int root_dir_entries_set = 0
static

Definition at line 266 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ sector_size

unsigned sector_size = 512
static

◆ sector_size_set

int sector_size_set = 0
static

Definition at line 241 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ sectors_per_cluster

int sectors_per_cluster = 0
static

Definition at line 264 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ size_fat

int size_fat = 0
static

Definition at line 248 of file mkfs.fat.c.

Referenced by establish_params(), main(), mark_FAT_cluster(), setup_tables(), and write_tables().

◆ size_fat_by_user

int size_fat_by_user = 0
static

Definition at line 249 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ size_root_dir

int size_root_dir
static

Definition at line 262 of file mkfs.fat.c.

Referenced by setup_tables(), and write_tables().

◆ start_data_block

int start_data_block
static

Definition at line 256 of file mkfs.fat.c.

Referenced by check_blocks(), and setup_tables().

◆ start_data_sector

int start_data_sector
static

Definition at line 255 of file mkfs.fat.c.

Referenced by get_list_blocks(), mark_FAT_sector(), and setup_tables().

◆ verbose

int verbose = 0
static

Definition at line 235 of file mkfs.fat.c.

Referenced by check_blocks(), establish_params(), main(), and setup_tables().

◆ volume_id

long volume_id
static

Definition at line 236 of file mkfs.fat.c.

Referenced by main(), and setup_tables().

◆ volume_name

char* volume_name = initial_volume_name
static

Definition at line 238 of file mkfs.fat.c.

Referenced by main(), and setup_tables().