[dd22cc4] | 1 | /*
|
---|
[4d433fe] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2011 Oleg Romanenko
|
---|
[dd22cc4] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[b1834a01] | 30 | /** @addtogroup mkexfat
|
---|
[dd22cc4] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[acb866ae] | 34 | #include <stdint.h>
|
---|
[3e6a98c5] | 35 | #include <stdbool.h>
|
---|
[acb866ae] | 36 |
|
---|
| 37 | #define EXFAT_FILENAME_LEN 255
|
---|
| 38 | #define EXFAT_NAME_PART_LEN 15
|
---|
[f3504c1] | 39 | #define EXFAT_VOLLABEL_LEN 11
|
---|
[acb866ae] | 40 |
|
---|
| 41 | #define EXFAT_TYPE_UNUSED 0x00
|
---|
| 42 | #define EXFAT_TYPE_USED 0x80
|
---|
| 43 | #define EXFAT_TYPE_VOLLABEL 0x83
|
---|
| 44 | #define EXFAT_TYPE_BITMAP 0x81
|
---|
| 45 | #define EXFAT_TYPE_UCTABLE 0x82
|
---|
| 46 | #define EXFAT_TYPE_GUID 0xA0
|
---|
| 47 | #define EXFAT_TYPE_FILE 0x85
|
---|
| 48 | #define EXFAT_TYPE_STREAM 0xC0
|
---|
| 49 | #define EXFAT_TYPE_NAME 0xC1
|
---|
| 50 |
|
---|
| 51 | #define EXFAT_ATTR_RDONLY 0x01
|
---|
| 52 | #define EXFAT_ATTR_HIDDEN 0x02
|
---|
| 53 | #define EXFAT_ATTR_SYSTEM 0x04
|
---|
| 54 | #define EXFAT_ATTR_SUBDIR 0x10
|
---|
| 55 | #define EXFAT_ATTR_ARCHIVE 0x20
|
---|
| 56 |
|
---|
| 57 | /* All dentry structs should have 31 byte size */
|
---|
| 58 | typedef struct {
|
---|
| 59 | uint8_t size;
|
---|
| 60 | uint16_t label[11];
|
---|
| 61 | uint8_t _reserved[8];
|
---|
[1433ecda] | 62 | } __attribute__((packed)) exfat_vollabel_dentry_t;
|
---|
[acb866ae] | 63 |
|
---|
| 64 | typedef struct {
|
---|
| 65 | uint8_t flags;
|
---|
| 66 | uint8_t _reserved[18];
|
---|
| 67 | uint32_t firstc;
|
---|
| 68 | uint64_t size;
|
---|
[1433ecda] | 69 | } __attribute__((packed)) exfat_bitmap_dentry_t;
|
---|
[acb866ae] | 70 |
|
---|
| 71 | typedef struct {
|
---|
| 72 | uint8_t _reserved1[3];
|
---|
| 73 | uint32_t checksum;
|
---|
| 74 | uint8_t _reserved2[12];
|
---|
| 75 | uint32_t firstc;
|
---|
| 76 | uint64_t size;
|
---|
[1433ecda] | 77 | } __attribute__((packed)) exfat_uctable_dentry_t;
|
---|
[acb866ae] | 78 |
|
---|
| 79 | typedef struct {
|
---|
[1b20da0] | 80 | uint8_t count; /* Always zero */
|
---|
[acb866ae] | 81 | uint16_t checksum;
|
---|
| 82 | uint16_t flags;
|
---|
| 83 | uint8_t guid[16];
|
---|
| 84 | uint8_t _reserved[10];
|
---|
[1433ecda] | 85 | } __attribute__((packed)) exfat_guid_dentry_t;
|
---|
[acb866ae] | 86 |
|
---|
| 87 | typedef struct {
|
---|
| 88 | uint8_t count;
|
---|
| 89 | uint16_t checksum;
|
---|
| 90 | uint16_t attr;
|
---|
| 91 | uint8_t _reserved1[2];
|
---|
| 92 | uint32_t ctime;
|
---|
| 93 | uint32_t mtime;
|
---|
| 94 | uint32_t atime;
|
---|
| 95 | uint8_t ctime_fine;
|
---|
| 96 | uint8_t mtime_fine;
|
---|
| 97 | uint8_t ctime_tz;
|
---|
| 98 | uint8_t mtime_tz;
|
---|
| 99 | uint8_t atime_tz;
|
---|
| 100 | uint8_t _reserved2[7];
|
---|
[1433ecda] | 101 | } __attribute__((packed)) exfat_file_dentry_t;
|
---|
[acb866ae] | 102 |
|
---|
| 103 | typedef struct {
|
---|
| 104 | uint8_t flags;
|
---|
| 105 | uint8_t _reserved1[1];
|
---|
| 106 | uint8_t name_size;
|
---|
| 107 | uint16_t hash;
|
---|
| 108 | uint8_t _reserved2[2];
|
---|
| 109 | uint64_t valid_data_size;
|
---|
| 110 | uint8_t _reserved3[4];
|
---|
| 111 | uint32_t firstc;
|
---|
| 112 | uint64_t data_size;
|
---|
[1433ecda] | 113 | } __attribute__((packed)) exfat_stream_dentry_t;
|
---|
[acb866ae] | 114 |
|
---|
| 115 | typedef struct {
|
---|
| 116 | uint8_t flags;
|
---|
| 117 | uint16_t name[EXFAT_NAME_PART_LEN];
|
---|
[1433ecda] | 118 | } __attribute__((packed)) exfat_name_dentry_t;
|
---|
[acb866ae] | 119 |
|
---|
| 120 | typedef struct {
|
---|
| 121 | uint8_t type;
|
---|
| 122 | union {
|
---|
| 123 | exfat_vollabel_dentry_t vollabel;
|
---|
| 124 | exfat_bitmap_dentry_t bitmap;
|
---|
| 125 | exfat_uctable_dentry_t uctable;
|
---|
| 126 | exfat_guid_dentry_t guid;
|
---|
| 127 | exfat_file_dentry_t file;
|
---|
| 128 | exfat_stream_dentry_t stream;
|
---|
| 129 | exfat_name_dentry_t name;
|
---|
| 130 | };
|
---|
[1433ecda] | 131 | } __attribute__((packed)) exfat_dentry_t;
|
---|
[acb866ae] | 132 |
|
---|
| 133 | typedef enum {
|
---|
| 134 | EXFAT_DENTRY_SKIP,
|
---|
| 135 | EXFAT_DENTRY_LAST,
|
---|
| 136 | EXFAT_DENTRY_FREE,
|
---|
| 137 | EXFAT_DENTRY_VOLLABEL,
|
---|
| 138 | EXFAT_DENTRY_BITMAP,
|
---|
| 139 | EXFAT_DENTRY_UCTABLE,
|
---|
| 140 | EXFAT_DENTRY_GUID,
|
---|
| 141 | EXFAT_DENTRY_FILE,
|
---|
| 142 | EXFAT_DENTRY_STREAM,
|
---|
| 143 | EXFAT_DENTRY_NAME
|
---|
| 144 | } exfat_dentry_clsf_t;
|
---|
| 145 |
|
---|
[dd22cc4] | 146 | typedef struct exfat_bs {
|
---|
| 147 | uint8_t jump[3]; /* 0x00 jmp and nop instructions */
|
---|
| 148 | uint8_t oem_name[8]; /* 0x03 "EXFAT " */
|
---|
| 149 | uint8_t __reserved[53]; /* 0x0B always 0 */
|
---|
| 150 | uint64_t volume_start; /* 0x40 partition first sector */
|
---|
| 151 | uint64_t volume_count; /* 0x48 partition sectors count */
|
---|
| 152 | uint32_t fat_sector_start; /* 0x50 FAT first sector */
|
---|
| 153 | uint32_t fat_sector_count; /* 0x54 FAT sectors count */
|
---|
| 154 | uint32_t data_start_sector; /* 0x58 Data region first cluster sector */
|
---|
| 155 | uint32_t data_clusters; /* 0x5C total clusters count */
|
---|
| 156 | uint32_t rootdir_cluster; /* 0x60 first cluster of the root dir */
|
---|
| 157 | uint32_t volume_serial; /* 0x64 volume serial number */
|
---|
| 158 | struct { /* 0x68 FS version */
|
---|
| 159 | uint8_t minor;
|
---|
| 160 | uint8_t major;
|
---|
[1433ecda] | 161 | } __attribute__((packed)) version;
|
---|
[dd22cc4] | 162 | uint16_t volume_flags; /* 0x6A volume state flags */
|
---|
| 163 | uint8_t bytes_per_sector; /* 0x6C sector size as (1 << n) */
|
---|
| 164 | uint8_t sec_per_cluster; /* 0x6D sectors per cluster as (1 << n) */
|
---|
| 165 | uint8_t fat_count; /* 0x6E always 1 */
|
---|
| 166 | uint8_t drive_no; /* 0x6F always 0x80 */
|
---|
| 167 | uint8_t allocated_percent; /* 0x70 percentage of allocated space */
|
---|
| 168 | uint8_t _reserved2[7]; /* 0x71 reserved */
|
---|
| 169 | uint8_t bootcode[390]; /* Boot code */
|
---|
| 170 | uint16_t signature; /* the value of 0xAA55 */
|
---|
| 171 | } __attribute__((__packed__)) exfat_bs_t;
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * @}
|
---|
| 175 | */
|
---|