@@ -122,27 +122,54 @@ static bool filename_character_valid(char character);
122
122
COMPILER_ASSERT (0x0200 == VFS_SECTOR_SIZE );
123
123
// If root directory size changes update max_root_dir_entries
124
124
COMPILER_ASSERT (0x0020 == sizeof (root_dir_t ) / sizeof (FatDirectoryEntry_t ));
125
- static const mbr_t mbr_tmpl = {
125
+
126
+ #define MBR_BYTES_PER_SECTOR 0x0200
127
+ #define MBR_SECTORS_PER_CLUSTER 0x08
128
+ #define TOTAL_SECTORS ((VFS_DISK_SIZE + KB(64)) / MBR_BYTES_PER_SECTOR)
129
+ #if (TOTAL_SECTORS < (FAT_CLUSTERS_MIN * MBR_SECTORS_PER_CLUSTER ))
130
+ // #warning "MBR Total Sector resulting in smaller number of FAT clusters than expected"
131
+ #define MBR_TOTAL_SECTORS (FAT_CLUSTERS_MIN * MBR_SECTORS_PER_CLUSTER)
132
+ #elif (TOTAL_SECTORS > (FAT_CLUSTERS_MAX * MBR_SECTORS_PER_CLUSTER ))
133
+ // #warning "MBR Total Sector resulting in larger number of FAT clusters than expected"
134
+ #define MBR_TOTAL_SECTORS (FAT_CLUSTERS_MAX * MBR_SECTORS_PER_CLUSTER)
135
+ #else
136
+ #define MBR_TOTAL_SECTORS TOTAL_SECTORS
137
+ #endif
138
+
139
+ #if (MBR_TOTAL_SECTORS >= 0x10000 )
140
+ #define MBR_TOTAL_LOGICAL_SECTORS 0
141
+ #define MBR_BIG_SECTORS_ON_DRIVE MBR_TOTAL_SECTORS
142
+ #else
143
+ #define MBR_TOTAL_LOGICAL_SECTORS MBR_TOTAL_SECTORS
144
+ #define MBR_BIG_SECTORS_ON_DRIVE 0
145
+ #endif
146
+
147
+ // FAT table will likely be larger than needed, but this is allowed by the
148
+ // fat specification
149
+ #define MBR_NUMBER_OF_CLUSTERS (MBR_TOTAL_SECTORS / MBR_SECTORS_PER_CLUSTER)
150
+ #define MBR_LOGICAL_SECTORS_PER_FAT ((MBR_NUMBER_OF_CLUSTERS * 2 + VFS_SECTOR_SIZE - 1) / VFS_SECTOR_SIZE)
151
+
152
+ const mbr_t mbr = {
126
153
/*uint8_t[11]*/ .boot_sector = {
127
154
0xEB , 0x3C , 0x90 ,
128
155
'M' , 'S' , 'D' , '0' , 'S' , '4' , '.' , '1' // OEM Name in text (8 chars max)
129
156
},
130
- /*uint16_t*/ .bytes_per_sector = 0x0200 , // 512 bytes per sector
131
- /*uint8_t */ .sectors_per_cluster = 0x08 , // 4k cluser
132
- /*uint16_t*/ .reserved_logical_sectors = 0x0001 , // mbr is 1 sector
133
- /*uint8_t */ .num_fats = 0x02 , // 2 FATs
134
- /*uint16_t*/ .max_root_dir_entries = 0x0020 , // 32 dir entries (max)
135
- /*uint16_t*/ .total_logical_sectors = 0x1f50 , // sector size * # of sectors = drive size
136
- /*uint8_t */ .media_descriptor = 0xf8 , // fixed disc = F8, removable = F0
137
- /*uint16_t*/ .logical_sectors_per_fat = 0x0001 , // FAT is 1k - ToDO:need to edit this
138
- /*uint16_t*/ .physical_sectors_per_track = 0x0001 , // flat
139
- /*uint16_t*/ .heads = 0x0001 , // flat
140
- /*uint32_t*/ .hidden_sectors = 0x00000000 , // before mbt, 0
141
- /*uint32_t*/ .big_sectors_on_drive = 0x00000000 , // 4k sector. not using large clusters
157
+ /*uint16_t*/ .bytes_per_sector = MBR_BYTES_PER_SECTOR , // 512 bytes per sector
158
+ /*uint8_t */ .sectors_per_cluster = MBR_SECTORS_PER_CLUSTER , // 4k cluser
159
+ /*uint16_t*/ .reserved_logical_sectors = 0x0001 , // mbr is 1 sector
160
+ /*uint8_t */ .num_fats = 0x02 , // 2 FATs
161
+ /*uint16_t*/ .max_root_dir_entries = 0x0020 , // 32 dir entries (max)
162
+ /*uint16_t*/ .total_logical_sectors = MBR_TOTAL_LOGICAL_SECTORS , // sector size * # of sectors = drive size
163
+ /*uint8_t */ .media_descriptor = 0xf8 , // fixed disc = F8, removable = F0
164
+ /*uint16_t*/ .logical_sectors_per_fat = MBR_LOGICAL_SECTORS_PER_FAT , // FAT is 1k - ToDO:need to edit this
165
+ /*uint16_t*/ .physical_sectors_per_track = 0x0001 , // flat
166
+ /*uint16_t*/ .heads = 0x0001 , // flat
167
+ /*uint32_t*/ .hidden_sectors = 0x00000000 , // before mbt, 0
168
+ /*uint32_t*/ .big_sectors_on_drive = MBR_BIG_SECTORS_ON_DRIVE , // 4k sector. not using large clusters
142
169
/*uint8_t */ .physical_drive_number = 0x00 ,
143
- /*uint8_t */ .not_used = 0x00 , // Current head. Linux tries to set this to 0x1
144
- /*uint8_t */ .boot_record_signature = 0x29 , // signature is present
145
- /*uint32_t*/ .volume_id = 0x27021974 , // serial number
170
+ /*uint8_t */ .not_used = 0x00 , // Current head. Linux tries to set this to 0x1
171
+ /*uint8_t */ .boot_record_signature = 0x29 , // signature is present
172
+ /*uint32_t*/ .volume_id = 0x27021974 , // serial number
146
173
// needs to match the root dir label
147
174
/*char[11]*/ .volume_label = {'D' , 'A' , 'P' , 'L' , 'I' , 'N' , 'K' , '-' , 'D' , 'N' , 'D' },
148
175
// unused by msft - just a label (FAT, FAT12, FAT16)
@@ -289,7 +316,6 @@ static const FatDirectoryEntry_t dir_entry_tmpl = {
289
316
/*uint32_t*/ .filesize = 0x00000000
290
317
};
291
318
292
- mbr_t mbr ;
293
319
file_allocation_table_t fat ;
294
320
virtual_media_t virtual_media [16 ];
295
321
root_dir_t dir_current ;
@@ -320,13 +346,11 @@ static void write_fat(file_allocation_table_t *fat, uint32_t idx, uint16_t val)
320
346
fat -> f [high_idx ] = (val >> 8 ) & 0xFF ;
321
347
}
322
348
323
- void vfs_init (const vfs_filename_t drive_name , uint32_t disk_size )
349
+ void vfs_init (const vfs_filename_t drive_name )
324
350
{
325
351
uint32_t i ;
326
- uint32_t num_clusters ;
327
- uint32_t total_sectors ;
352
+
328
353
// Clear everything
329
- memset (& mbr , 0 , sizeof (mbr ));
330
354
memset (& fat , 0 , sizeof (fat ));
331
355
fat_idx = 0 ;
332
356
memset (& virtual_media , 0 , sizeof (virtual_media ));
@@ -336,28 +360,7 @@ void vfs_init(const vfs_filename_t drive_name, uint32_t disk_size)
336
360
file_change_cb = file_change_cb_stub ;
337
361
virtual_media_idx = 0 ;
338
362
data_start = 0 ;
339
- // Initialize MBR
340
- memcpy (& mbr , & mbr_tmpl , sizeof (mbr_t ));
341
- total_sectors = ((disk_size + KB (64 )) / mbr .bytes_per_sector );
342
- // Make sure this is the right size for a FAT16 volume
343
- if (total_sectors < FAT_CLUSTERS_MIN * mbr .sectors_per_cluster ) {
344
- util_assert (0 );
345
- total_sectors = FAT_CLUSTERS_MIN * mbr .sectors_per_cluster ;
346
- } else if (total_sectors > FAT_CLUSTERS_MAX * mbr .sectors_per_cluster ) {
347
- util_assert (0 );
348
- total_sectors = FAT_CLUSTERS_MAX * mbr .sectors_per_cluster ;
349
- }
350
- if (total_sectors >= 0x10000 ) {
351
- mbr .total_logical_sectors = 0 ;
352
- mbr .big_sectors_on_drive = total_sectors ;
353
- } else {
354
- mbr .total_logical_sectors = total_sectors ;
355
- mbr .big_sectors_on_drive = 0 ;
356
- }
357
- // FAT table will likely be larger than needed, but this is allowed by the
358
- // fat specification
359
- num_clusters = total_sectors / mbr .sectors_per_cluster ;
360
- mbr .logical_sectors_per_fat = (num_clusters * 2 + VFS_SECTOR_SIZE - 1 ) / VFS_SECTOR_SIZE ;
363
+
361
364
// Initailize virtual media
362
365
memcpy (& virtual_media , & virtual_media_tmpl , sizeof (virtual_media_tmpl ));
363
366
virtual_media [MEDIA_IDX_FAT1 ].length = VFS_SECTOR_SIZE * mbr .logical_sectors_per_fat ;
0 commit comments