| 1 |
/** @file |
| 2 |
* @brief FAT filesystem. |
| 3 |
* |
| 4 |
* Based on http://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* Copyright (c) 2014 Flávio J. Saraiva <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "fs_fat.h" |
| 12 |
|
| 13 |
/* |
| 14 |
*/ |
| 15 |
|
| 16 |
struct fat16_data { |
| 17 |
const char *volume_name; |
| 18 |
m_uint32_t volume_sectors; |
| 19 |
m_uint16_t reserved_sectors; |
| 20 |
m_uint16_t root_entry_count; |
| 21 |
m_uint16_t fat_sectors; |
| 22 |
m_uint16_t sects_per_track; |
| 23 |
m_uint16_t heads; |
| 24 |
m_uint8_t sects_per_cluster; |
| 25 |
m_uint8_t nr_fats; |
| 26 |
}; |
| 27 |
|
| 28 |
struct sec_per_clus_table |
| 29 |
{ |
| 30 |
m_uint32_t sectors; |
| 31 |
m_uint8_t sectors_per_cluster; |
| 32 |
}; |
| 33 |
|
| 34 |
static struct { |
| 35 |
m_uint32_t sectors; |
| 36 |
m_uint8_t sects_per_cluster; |
| 37 |
} cluster_size_table16[] = { |
| 38 |
{ 32680, 2}, /* 16MB - 1K */ |
| 39 |
{ 262144, 4}, /* 128MB - 2K */ |
| 40 |
{ 524288, 8}, /* 256MB - 4K */ |
| 41 |
{ 1048576, 16}, /* 512MB - 8K */ |
| 42 |
{ 2097152, 32}, /* 1GB - 16K */ |
| 43 |
{ 4194304, 64}, /* 2GB - 32K */ |
| 44 |
{ 8388608, 128},/* 2GB - 64K (not supported on some systems) */ |
| 45 |
{ 0 , 0 } /* done */ |
| 46 |
}; |
| 47 |
|
| 48 |
static inline void set_u32(m_uint8_t *p, size_t i, m_uint32_t v) { |
| 49 |
p[i+0] = (m_uint8_t)((v>>0)&0xFF); |
| 50 |
p[i+1] = (m_uint8_t)((v>>8)&0xFF); |
| 51 |
p[i+1] = (m_uint8_t)((v>>16)&0xFF); |
| 52 |
p[i+1] = (m_uint8_t)((v>>24)&0xFF); |
| 53 |
} |
| 54 |
|
| 55 |
static inline void set_u16(m_uint8_t *p, size_t i, m_uint16_t v) { |
| 56 |
p[i+0] = (m_uint8_t)((v>>0)&0xFF); |
| 57 |
p[i+1] = (m_uint8_t)((v>>8)&0xFF); |
| 58 |
} |
| 59 |
|
| 60 |
static void boot16(m_uint8_t *sector, struct fat16_data *fat16) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
|
| 64 |
memset(sector, 0x00, FS_FAT_SECTOR_SIZE); |
| 65 |
|
| 66 |
/* start of boot program */ |
| 67 |
sector[0x0] = 0xEB;// jmp 0x3E |
| 68 |
sector[0x1] = 0x3C; |
| 69 |
sector[0x2] = 0x90;// nop |
| 70 |
|
| 71 |
/* OEM string */ |
| 72 |
sector[0x3] = 'D'; |
| 73 |
sector[0x4] = 'Y'; |
| 74 |
sector[0x5] = 'N'; |
| 75 |
sector[0x6] = 'A'; |
| 76 |
sector[0x7] = 'M'; |
| 77 |
sector[0x8] = 'I'; |
| 78 |
sector[0x9] = 'P'; |
| 79 |
sector[0xA] = 'S'; |
| 80 |
|
| 81 |
// Bytes per sector |
| 82 |
set_u16(sector,0xB,FS_FAT_SECTOR_SIZE); |
| 83 |
|
| 84 |
// Sectors per cluster |
| 85 |
sector[0xD] = fat16->sects_per_cluster; |
| 86 |
|
| 87 |
// Reserved Sectors |
| 88 |
set_u16(sector,0xE,fat16->reserved_sectors); |
| 89 |
|
| 90 |
// Number of FATS |
| 91 |
sector[0x10] = fat16->nr_fats; |
| 92 |
|
| 93 |
// Max entries in root dir (FAT16 only) |
| 94 |
set_u16(sector,0x11,fat16->root_entry_count); |
| 95 |
|
| 96 |
// [FAT16] Total sectors (use FAT32 count instead) |
| 97 |
set_u16(sector,0x13,0x0000); |
| 98 |
|
| 99 |
// Media type (Fixed Disk) |
| 100 |
sector[0x15] = 0xF8; |
| 101 |
|
| 102 |
// FAT16 Bootstrap Details |
| 103 |
|
| 104 |
// Count of sectors used by the FAT table (FAT16 only) |
| 105 |
set_u16(sector,0x16,fat16->fat_sectors); |
| 106 |
|
| 107 |
// Sectors per track |
| 108 |
set_u16(sector,0x18,fat16->sects_per_track); |
| 109 |
|
| 110 |
// Heads |
| 111 |
set_u16(sector,0x1A,fat16->heads); |
| 112 |
|
| 113 |
// Hidden sectors |
| 114 |
set_u16(sector,0x1C,0x0000); |
| 115 |
|
| 116 |
// Total sectors for this volume |
| 117 |
set_u32(sector,0x20,fat16->volume_sectors); |
| 118 |
|
| 119 |
// Drive number (1st Hard Disk) |
| 120 |
sector[0x24] = 0x80; |
| 121 |
|
| 122 |
// Reserved |
| 123 |
sector[0x25] = 0x00; |
| 124 |
|
| 125 |
// Boot signature |
| 126 |
sector[0x26] = 0x29; |
| 127 |
|
| 128 |
// Volume ID |
| 129 |
sector[0x27] = (rand()&0xFF); |
| 130 |
sector[0x28] = (rand()&0xFF); |
| 131 |
sector[0x29] = (rand()&0xFF); |
| 132 |
sector[0x2A] = (rand()&0xFF); |
| 133 |
|
| 134 |
// Volume name |
| 135 |
for (i = 0; i < 11 && fat16->volume_name[i]; i++) { |
| 136 |
sector[i+0x2B] = fat16->volume_name[i]; |
| 137 |
} |
| 138 |
for (; i < 11; i++) { |
| 139 |
sector[i+0x2B] = ' '; |
| 140 |
} |
| 141 |
|
| 142 |
// File sys type |
| 143 |
sector[0x36] = 'F'; |
| 144 |
sector[0x37] = 'A'; |
| 145 |
sector[0x38] = 'T'; |
| 146 |
sector[0x39] = '1'; |
| 147 |
sector[0x3A] = '6'; |
| 148 |
sector[0x3B] = ' '; |
| 149 |
sector[0x3C] = ' '; |
| 150 |
sector[0x3D] = ' '; |
| 151 |
|
| 152 |
/* boot program (empty) */ |
| 153 |
|
| 154 |
/* Signature */ |
| 155 |
sector[0x1FE] = 0x55; |
| 156 |
sector[0x1FF] = 0xAA; |
| 157 |
} |
| 158 |
|
| 159 |
static void fat16_first(m_uint8_t *sector, struct fat16_data *fat16) |
| 160 |
{ |
| 161 |
memset(sector, 0x00, FS_FAT_SECTOR_SIZE); |
| 162 |
|
| 163 |
// Initialise default allocate / reserved clusters |
| 164 |
set_u16(sector,0x0,0xFFF8); |
| 165 |
set_u16(sector,0x2,0xFFFF); |
| 166 |
} |
| 167 |
|
| 168 |
static void fat16_empty(m_uint8_t *sector, struct fat16_data *fat16) |
| 169 |
{ |
| 170 |
memset(sector, 0x00, FS_FAT_SECTOR_SIZE); |
| 171 |
} |
| 172 |
|
| 173 |
static int write_sector(int fd, m_uint32_t lba, m_uint8_t *sector) |
| 174 |
{ |
| 175 |
off_t offset; |
| 176 |
|
| 177 |
errno = 0; |
| 178 |
offset = (off_t)lba * FS_FAT_SECTOR_SIZE; |
| 179 |
if (lseek(fd,offset,SEEK_SET) != offset) { |
| 180 |
perror("write_sector(fs_fat): lseek"); |
| 181 |
return(-1); |
| 182 |
} |
| 183 |
|
| 184 |
if (write(fd, sector, FS_FAT_SECTOR_SIZE) != FS_FAT_SECTOR_SIZE) { |
| 185 |
perror("write_sector(fs_fat): write"); |
| 186 |
return(-1); |
| 187 |
} |
| 188 |
|
| 189 |
return(0); |
| 190 |
} |
| 191 |
|
| 192 |
/** Format partition as FAT16. */ |
| 193 |
int fs_fat_format16(int fd, m_uint32_t begin_lba, m_uint32_t nr_sectors, |
| 194 |
m_uint16_t sects_per_track, m_uint16_t heads, |
| 195 |
const char *volume_name) |
| 196 |
{ |
| 197 |
m_uint8_t sector[FS_FAT_SECTOR_SIZE]; |
| 198 |
struct fat16_data data, *fat16 = NULL; |
| 199 |
size_t i, ifat, isec; |
| 200 |
m_uint32_t total_clusters; |
| 201 |
m_uint32_t fat_lba; |
| 202 |
m_uint32_t rootdir_lba; |
| 203 |
m_uint32_t rootdir_sectors; |
| 204 |
char name[12]; |
| 205 |
|
| 206 |
if (!volume_name) { |
| 207 |
name[0] = 0; |
| 208 |
snprintf(name,sizeof(name), "DISK%dMB", (nr_sectors / (1048576 / FS_FAT_SECTOR_SIZE))); |
| 209 |
volume_name = name; |
| 210 |
} |
| 211 |
|
| 212 |
/* prepare FAT16 */ |
| 213 |
fat16 = &data; |
| 214 |
memset(fat16, 0x00, sizeof(*fat16)); |
| 215 |
fat16->volume_name = volume_name; |
| 216 |
fat16->volume_sectors = nr_sectors; |
| 217 |
fat16->sects_per_track = sects_per_track; |
| 218 |
fat16->heads = heads; |
| 219 |
for (i=0; ; i++) { |
| 220 |
if (!cluster_size_table16[i].sectors) |
| 221 |
return(-1); |
| 222 |
if (nr_sectors <= cluster_size_table16[i].sectors) { |
| 223 |
fat16->sects_per_cluster = cluster_size_table16[i].sects_per_cluster; |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
total_clusters = (fat16->volume_sectors / fat16->sects_per_cluster) + 1; |
| 228 |
fat16->fat_sectors = (total_clusters/(FS_FAT_SECTOR_SIZE/2)) + 1; |
| 229 |
fat16->reserved_sectors = 1; |
| 230 |
fat16->nr_fats = 2; |
| 231 |
fat16->root_entry_count = 512; |
| 232 |
|
| 233 |
/* Boot sector */ |
| 234 |
boot16(sector, fat16); |
| 235 |
if (write_sector(fd, begin_lba, sector) < 0) |
| 236 |
return(-1); |
| 237 |
|
| 238 |
/* FAT sectors */ |
| 239 |
for (ifat = 0; ifat < fat16->nr_fats; ifat++) { |
| 240 |
fat_lba = begin_lba + fat16->reserved_sectors + ifat * fat16->fat_sectors; |
| 241 |
fat16_first(sector, fat16); |
| 242 |
if (write_sector(fd, fat_lba, sector) < 0) |
| 243 |
return(-1); |
| 244 |
|
| 245 |
fat16_empty(sector, fat16); |
| 246 |
for (isec = 1; isec < fat16->fat_sectors; isec++) { |
| 247 |
if (write_sector(fd, isec + fat_lba, sector) < 0) |
| 248 |
return(-1); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/* Root directory */ |
| 253 |
rootdir_lba = begin_lba + fat16->reserved_sectors + (fat16->nr_fats * fat16->fat_sectors); |
| 254 |
rootdir_sectors = ((fat16->root_entry_count * 32) + (FS_FAT_SECTOR_SIZE - 1)) / FS_FAT_SECTOR_SIZE; |
| 255 |
fat16_empty(sector, fat16); |
| 256 |
for (isec = 0; isec < rootdir_sectors; isec++) { |
| 257 |
if (write_sector(fd, rootdir_lba + isec, sector) < 0) { |
| 258 |
return(-1); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return(0); |
| 263 |
} |