103 lines
4.3 KiB
C
103 lines
4.3 KiB
C
#ifndef EMBPART_FAT32_H
|
|
#define EMBPART_FAT32_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#ifdef __linux
|
|
#include <stdio.h>
|
|
#include "../../MassStorage.h"
|
|
|
|
#define MSG(...) printf(__VA_ARGS__)
|
|
#endif
|
|
|
|
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;
|
|
|
|
int fat32_load(Fat32_CtrlBlock *ctrl, uint32_t bpb_s, const MassStorage * mstg);
|
|
|
|
int fat32_list_dir(const Fat32_CtrlBlock *ctrl, const char * dir);
|
|
|
|
int fat32_read_file(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);
|
|
|
|
#endif //EMBPART_FAT32_H
|