[6ebaff9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
[97bc3ee] | 31 | */
|
---|
[6ebaff9] | 32 |
|
---|
| 33 | #ifndef FAT_FAT_FAT_H_
|
---|
| 34 | #define FAT_FAT_FAT_H_
|
---|
| 35 |
|
---|
[0f57d0e] | 36 | #include "../../vfs/vfs.h"
|
---|
| 37 | #include <stdint.h>
|
---|
[6c8d267] | 38 | #include <libblock.h>
|
---|
[0f57d0e] | 39 |
|
---|
| 40 | #define FAT1 0
|
---|
| 41 |
|
---|
[0182e5cc] | 42 | #define FAT_CLST_RES0 0
|
---|
| 43 | #define FAT_CLST_RES1 1
|
---|
| 44 | #define FAT_CLST_FIRST 2
|
---|
[fc35e98] | 45 |
|
---|
[cb052b4] | 46 | #define FAT32_CLST_BAD 0x0ffffff7
|
---|
| 47 | #define FAT32_CLST_LAST1 0x0ffffff8
|
---|
| 48 | #define FAT32_CLST_LAST8 0x0fffffff
|
---|
[0f57d0e] | 49 |
|
---|
[aa2ea13] | 50 | #define FAT12_MASK 0x0fff
|
---|
| 51 | #define FAT16_MASK 0xffff
|
---|
| 52 | #define FAT32_MASK 0x0fffffff
|
---|
[875edff6] | 53 |
|
---|
[d260a95] | 54 | #define FAT12_CLST_MAX 4085
|
---|
| 55 | #define FAT16_CLST_MAX 65525
|
---|
| 56 |
|
---|
[875edff6] | 57 | /* Size in bytes for cluster value of FAT */
|
---|
| 58 | #define FAT12_CLST_SIZE 2
|
---|
| 59 | #define FAT16_CLST_SIZE 2
|
---|
| 60 | #define FAT32_CLST_SIZE 4
|
---|
| 61 |
|
---|
[0f57d0e] | 62 | /* internally used to mark root directory's parent */
|
---|
| 63 | #define FAT_CLST_ROOTPAR FAT_CLST_RES0
|
---|
| 64 | /* internally used to mark root directory */
|
---|
| 65 | #define FAT_CLST_ROOT FAT_CLST_RES1
|
---|
| 66 |
|
---|
[97bc3ee] | 67 | /*
|
---|
| 68 | * Convenience macros for computing some frequently used values from the
|
---|
| 69 | * primitive boot sector members.
|
---|
| 70 | */
|
---|
[d260a95] | 71 | #define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \
|
---|
| 72 | (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)
|
---|
| 73 | #define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))
|
---|
| 74 | #define DS(bs) (TS(bs) - SSA(bs))
|
---|
| 75 | #define CC(bs) (DS(bs) / SPC(bs))
|
---|
| 76 |
|
---|
| 77 | #define FAT_IS_FAT12(bs) (CC(bs) < FAT12_CLST_MAX)
|
---|
| 78 | #define FAT_IS_FAT16(bs) \
|
---|
| 79 | ((CC(bs) >= FAT12_CLST_MAX) && (CC(bs) < FAT16_CLST_MAX))
|
---|
[cb052b4] | 80 | #define FAT_IS_FAT32(bs) (CC(bs) >= FAT16_CLST_MAX)
|
---|
[d260a95] | 81 |
|
---|
[aa2ea13] | 82 | #define FAT_CLST_SIZE(bs) \
|
---|
| 83 | (FAT_IS_FAT32(bs) ? FAT32_CLST_SIZE : FAT16_CLST_SIZE)
|
---|
[875edff6] | 84 |
|
---|
| 85 | #define FAT_MASK(bs) \
|
---|
[aa2ea13] | 86 | (FAT_IS_FAT12(bs) ? FAT12_MASK : \
|
---|
| 87 | (FAT_IS_FAT32(bs) ? FAT32_MASK : FAT16_MASK))
|
---|
[875edff6] | 88 |
|
---|
[88a27f1] | 89 | #define FAT_CLST_LAST1(bs) (FAT32_CLST_LAST1 & FAT_MASK((bs)))
|
---|
| 90 | #define FAT_CLST_LAST8(bs) (FAT32_CLST_LAST8 & FAT_MASK((bs)))
|
---|
| 91 | #define FAT_CLST_BAD(bs) (FAT32_CLST_BAD & FAT_MASK((bs)))
|
---|
| 92 |
|
---|
[b5db2ae] | 93 | #define FAT_ROOT_CLST(bs) \
|
---|
| 94 | (FAT_IS_FAT32(bs) ? uint32_t_le2host(bs->fat32.root_cluster) : \
|
---|
| 95 | FAT_CLST_ROOT)
|
---|
| 96 |
|
---|
[0f57d0e] | 97 | /* forward declarations */
|
---|
| 98 | struct block;
|
---|
| 99 | struct fat_node;
|
---|
[cb682eb] | 100 | struct fat_bs;
|
---|
[0f57d0e] | 101 |
|
---|
[875edff6] | 102 | typedef uint32_t fat_cluster_t;
|
---|
[0f57d0e] | 103 |
|
---|
[e402382] | 104 | #define fat_clusters_get(numc, bs, dh, fc) \
|
---|
| 105 | fat_cluster_walk((bs), (dh), (fc), NULL, (numc), (uint16_t) -1)
|
---|
[991f645] | 106 | extern int fat_cluster_walk(struct fat_bs *, devmap_handle_t, fat_cluster_t,
|
---|
[e402382] | 107 | fat_cluster_t *, uint16_t *, uint16_t);
|
---|
[6c8d267] | 108 |
|
---|
[746f623] | 109 | extern int fat_block_get(block_t **, struct fat_bs *, struct fat_node *,
|
---|
| 110 | aoff64_t, int);
|
---|
[991f645] | 111 | extern int _fat_block_get(block_t **, struct fat_bs *, devmap_handle_t,
|
---|
[6da81e0] | 112 | fat_cluster_t, fat_cluster_t *, aoff64_t, int);
|
---|
[ed903174] | 113 |
|
---|
[cca29e3c] | 114 | extern int fat_append_clusters(struct fat_bs *, struct fat_node *,
|
---|
[377cce8] | 115 | fat_cluster_t, fat_cluster_t);
|
---|
[cca29e3c] | 116 | extern int fat_chop_clusters(struct fat_bs *, struct fat_node *,
|
---|
[913a821c] | 117 | fat_cluster_t);
|
---|
[991f645] | 118 | extern int fat_alloc_clusters(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
[cb682eb] | 119 | fat_cluster_t *, fat_cluster_t *);
|
---|
[991f645] | 120 | extern int fat_free_clusters(struct fat_bs *, devmap_handle_t, fat_cluster_t);
|
---|
| 121 | extern int fat_alloc_shadow_clusters(struct fat_bs *, devmap_handle_t,
|
---|
[cb682eb] | 122 | fat_cluster_t *, unsigned);
|
---|
[88a27f1] | 123 | extern int fat_get_cluster_fat12(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 124 | fat_cluster_t, fat_cluster_t *);
|
---|
| 125 | extern int fat_get_cluster_fat16(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 126 | fat_cluster_t, fat_cluster_t *);
|
---|
| 127 | extern int fat_get_cluster_fat32(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 128 | fat_cluster_t, fat_cluster_t *);
|
---|
[991f645] | 129 | extern int fat_get_cluster(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
[711e1f32] | 130 | fat_cluster_t, fat_cluster_t *);
|
---|
[88a27f1] | 131 | extern int fat_set_cluster_fat12(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 132 | fat_cluster_t, fat_cluster_t);
|
---|
| 133 | extern int fat_set_cluster_fat16(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 134 | fat_cluster_t, fat_cluster_t);
|
---|
| 135 | extern int fat_set_cluster_fat32(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
| 136 | fat_cluster_t, fat_cluster_t);
|
---|
[991f645] | 137 | extern int fat_set_cluster(struct fat_bs *, devmap_handle_t, unsigned,
|
---|
[cb682eb] | 138 | fat_cluster_t, fat_cluster_t);
|
---|
[cca29e3c] | 139 | extern int fat_fill_gap(struct fat_bs *, struct fat_node *, fat_cluster_t,
|
---|
[ed903174] | 140 | aoff64_t);
|
---|
[991f645] | 141 | extern int fat_zero_cluster(struct fat_bs *, devmap_handle_t, fat_cluster_t);
|
---|
| 142 | extern int fat_sanity_check(struct fat_bs *, devmap_handle_t);
|
---|
[0f57d0e] | 143 |
|
---|
[6ebaff9] | 144 | #endif
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * @}
|
---|
| 148 | */
|
---|