embpart/fs/fat32/fat32.h
Wiesner András bcd655fd8d - misplaced macro fixed
- volume size added to volume listing
2023-11-11 20:06:40 +01:00

132 lines
5.5 KiB
C

#ifndef CORE_EMBPART_FS_FAT32_FAT32
#define CORE_EMBPART_FS_FAT32_FAT32
#include <stdint.h>
#include <stdio.h>
#include "../../MassStorage.h"
#include "../fs_driver.h"
#ifdef __linux
#define MSG(...) printf(__VA_ARGS__)
#endif /* CORE_EMBPART_FS_FAT32_FAT32 */
#define FAT32_CHS_PART_ID (0x0B)
#define FAT32_LBA_PART_ID (0x0C)
typedef struct {
uint8_t boot_jump[3]; // jump instruction used for bootable volume
uint8_t formatter_name[8]; // identification of application or OS formatted this device
uint16_t bytes_per_sector; // number of bytes per sector
uint8_t sectors_per_cluster; // number of sectors per cluster
uint16_t n_reserved_sectors; // number of reserved sectors
uint8_t n_fats; // number of FAT copies
uint8_t _unused0[2];
uint8_t _unused1[2];
uint8_t media_descriptor; // media descriptor differentiating between removable and non-removable devices
uint8_t _unused2[2];
uint16_t sectors_per_track; // number of sectors per track
uint16_t n_heads; // number of heads
uint32_t n_hidden_sectors; // number of hidden sectors preceding this volume
uint32_t n_total_sectors; // total number of sectors in this volume
uint32_t n_sectors_per_fat; // number of sectors per FAT
uint16_t flags; // flags, see docs.
uint16_t version; // FAT version number
uint32_t root_first_cluster; // first cluster of the root directory
uint16_t fsinfo_sector; // sector of the FSINFO structure
uint16_t boot_backup_sector; // sector of backup boot code in the reserved region
uint8_t _reserved0[12];
uint8_t logical_driver_number; // logical drive number of the partition
uint8_t _reserved1;
uint8_t extended_boot_signature; // extended boot signature
uint32_t volume_serial_number; // volume serial number
uint8_t volume_label[11]; // volume label string
uint8_t fs_type[8]; // file system type ("FAT32")
} __attribute__((packed)) Bpb_Fat32;
typedef struct {
const MassStorage * mstg; // mass storage holding the partition
uint32_t bpb_s; // sector of bios parameter block
uint32_t fat_s; // first sector of the FAT area
uint32_t root_s; // first sector of root directory
uint32_t data_s; // first sector of the data area (~volume)
uint32_t serial_number; // serial number of the volume
uint32_t sectors_per_fat; // sectors per FAT table
uint16_t bytes_per_sector; // number of bytes creating a sector
uint16_t sectors_per_cluster; // number of sectors creating a cluster
uint16_t fat_entries_per_sector; // number of FAT entries per sector
uint8_t fat_copies; // number of FAT table copies
uint8_t volume_label[11]; // pointer to volume label
} Fat32_CtrlBlock;
typedef uint32_t Fat32_FatTableEntry;
#define FAT32_FATT_READ_ONLY (1)
#define FAT32_FATT_HIDDEN (1 << 1)
#define FAT32_FATT_SYSTEM (1 << 2)
#define FAT32_FATT_VOLUME (1 << 3)
#define FAT32_FATT_DIRECTORY (1 << 4)
#define FAT32_FATT_ARCHIVE (1 << 5)
typedef struct {
uint8_t short_fname[8]; // short filename
uint8_t extension[3]; // file extension
uint8_t attributes; // entry attributes
uint8_t _reserved0;
uint8_t creat_time_hs; // file creation time, hundredth of a second (0-199)
uint16_t creat_time_hms; // file creation time, hour, minute, second
uint16_t create_date_ymd; // file creation date year, month, day
uint16_t lastacc_data_ymd; // ...
uint16_t first_cluster_high; // high word of first cluster
uint16_t lastmod_time_hms; // ...
uint16_t lastmod_date_ymd; // ...
uint16_t first_cluster_low; // high word of first cluster
uint32_t size; // file size in bytes
} __attribute__((packed)) Fat32_FileEntry;
typedef struct {
uint8_t index; // order of this entry in the series of LFN entries
uint16_t ustr04[5]; // characters 0-4
uint8_t attributes; // must have bits 0-3 set
uint8_t type; // should be zero
uint8_t checksum; // string checksum
uint16_t ustr5A[6]; // characters 5-10
uint16_t cluster; // should be zero
uint16_t ustrBC[2]; // characters 11-12
} __attribute__((packed)) Fat32_LongFileNameEntry;
typedef struct {
uint32_t pos; // byte position
uint32_t cluster; // cluster corresponding to the position
uint32_t sector; // sector of the last read
uint32_t sector_of_cluster; // sequence number of sector in the current cluster
uint32_t sector_begin_pos; // byte position of the sector's begin
} Fat32_SeekHint;
typedef struct {
const Fat32_CtrlBlock * ctrl; // pointer to FAT32 control block
Fat32_SeekHint last_pos; // last read position
uint32_t entry_ba; // FileEntry byte address on the mass storage
uint32_t size; // file size
} Fat32_FileHelper;
extern Fs_Driver fat32_driver;
int fat32_load(Fat32_CtrlBlock *ctrl, uint32_t bpb_s, const MassStorage * mstg);
int fat32_list_dir(const Fat32_CtrlBlock *ctrl, const char * dir);
uint32_t fat32_read_file_random_access(const Fat32_CtrlBlock * ctrl, const Fat32_FileEntry * entry, uint32_t pos, uint32_t len, uint8_t * p);
const Fat32_FileEntry * fat32_locate_file(const Fat32_CtrlBlock *ctrl, const char *path, uint32_t * file_entry_ba);
int fat32_open_file(const Fat32_CtrlBlock *ctrl, const char *path, Fat32_FileHelper *file_helper);
uint32_t fat32_seek_stream(const Fat32_CtrlBlock *ctrl, uint32_t pos, Fat32_FileHelper *file_helper);
uint32_t fat32_read_file_stream(const Fat32_CtrlBlock *ctrl, Fat32_FileHelper *file_helper, uint32_t len, uint8_t *p);
void fat32_get_volume_label(const Fat32_CtrlBlock * ctrl, char * label, uint16_t maxLen);
#endif //EMBPART_FAT32_H