1 /*
2  * Copyright (c) 2015 Steve White
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lib/bio.h>
11 #include <lib/bcache.h>
12 
13 typedef struct {
14     bdev_t *dev;
15     bcache_t cache;
16 
17     uint32_t lba_start;
18 
19     uint32_t bytes_per_sector;
20     uint32_t sectors_per_cluster;
21     uint32_t bytes_per_cluster;
22     uint32_t reserved_sectors;
23     uint32_t fat_bits;
24     uint32_t fat_count;
25     uint32_t sectors_per_fat;
26     uint32_t total_sectors;
27     uint32_t active_fat;
28     uint32_t data_start;
29     uint32_t total_clusters;
30     uint32_t root_cluster;
31     uint32_t root_entries;
32     uint32_t root_start;
33 } fat_fs_t;
34 
35 typedef struct {
36     fat_fs_t *fat_fs;
37     uint32_t start_cluster;
38     uint32_t length;
39     uint8_t attributes;
40 } fat_file_t;
41 
42 typedef enum {
43     fat_attribute_read_only = 0x01,
44     fat_attribute_hidden = 0x02,
45     fat_attribute_system = 0x04,
46     fat_attribute_volume_id = 0x08,
47     fat_attribute_directory = 0x10,
48     fat_attribute_archive = 0x20,
49     fat_attribute_lfn = fat_attribute_read_only | fat_attribute_hidden | fat_attribute_system | fat_attribute_volume_id,
50 } fat_attributes;
51 
52 #define fat_read32(buffer,off) \
53 (((uint8_t *)buffer)[(off)] + (((uint8_t *)buffer)[(off)+1] << 8) + \
54 (((uint8_t *)buffer)[(off)+2] << 16) + (((uint8_t *)buffer)[(off)+3] << 24))
55 
56 #define fat_read16(buffer,off) \
57 (((uint8_t *)buffer)[(off)] + (((uint8_t *)buffer)[(off)+1] << 8))
58 
59