1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * Copyright (c) 2011 Oleg Romanenko
|
---|
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 |
|
---|
30 | /** @addtogroup fs
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file exfat_fat.c
|
---|
36 | * @brief Functions that manipulate the File Allocation Tables.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include "exfat_fat.h"
|
---|
40 | #include "exfat.h"
|
---|
41 | #include "../../vfs/vfs.h"
|
---|
42 | #include <libfs.h>
|
---|
43 | #include <libblock.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <byteorder.h>
|
---|
46 | #include <align.h>
|
---|
47 | #include <assert.h>
|
---|
48 | #include <fibril_synch.h>
|
---|
49 | #include <malloc.h>
|
---|
50 | #include <mem.h>
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
---|
55 | * during allocation of clusters. The lock does not have to be held durring
|
---|
56 | * deallocation of clusters.
|
---|
57 | */
|
---|
58 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
---|
59 |
|
---|
60 |
|
---|
61 | /** Get cluster from the FAT.
|
---|
62 | *
|
---|
63 | * @param bs Buffer holding the boot sector for the file system.
|
---|
64 | * @param devmap_handle Device handle for the file system.
|
---|
65 | * @param clst Cluster which to get.
|
---|
66 | * @param value Output argument holding the value of the cluster.
|
---|
67 | *
|
---|
68 | * @return EOK or a negative error code.
|
---|
69 | */
|
---|
70 | int
|
---|
71 | fat_get_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
|
---|
72 | exfat_cluster_t clst, exfat_cluster_t *value)
|
---|
73 | {
|
---|
74 | block_t *b;
|
---|
75 | aoff64_t offset;
|
---|
76 | int rc;
|
---|
77 |
|
---|
78 | offset = clst * sizeof(exfat_cluster_t);
|
---|
79 |
|
---|
80 | rc = block_get(&b, devmap_handle, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
81 | if (rc != EOK)
|
---|
82 | return rc;
|
---|
83 |
|
---|
84 | *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
|
---|
85 |
|
---|
86 | rc = block_put(b);
|
---|
87 |
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Set cluster in FAT.
|
---|
92 | *
|
---|
93 | * @param bs Buffer holding the boot sector for the file system.
|
---|
94 | * @param devmap_handle Device handle for the file system.
|
---|
95 | * @param clst Cluster which is to be set.
|
---|
96 | * @param value Value to set the cluster with.
|
---|
97 | *
|
---|
98 | * @return EOK on success or a negative error code.
|
---|
99 | */
|
---|
100 | int
|
---|
101 | fat_set_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
|
---|
102 | exfat_cluster_t clst, exfat_cluster_t value)
|
---|
103 | {
|
---|
104 | block_t *b;
|
---|
105 | aoff64_t offset;
|
---|
106 | int rc;
|
---|
107 |
|
---|
108 | offset = clst * sizeof(exfat_cluster_t);
|
---|
109 |
|
---|
110 | rc = block_get(&b, devmap_handle, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
111 | if (rc != EOK)
|
---|
112 | return rc;
|
---|
113 |
|
---|
114 | *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(value);
|
---|
115 |
|
---|
116 | b->dirty = true; /* need to sync block */
|
---|
117 | rc = block_put(b);
|
---|
118 | return rc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Perform basic sanity checks on the file system.
|
---|
122 | *
|
---|
123 | * Verify if values of boot sector fields are sane. Also verify media
|
---|
124 | * descriptor. This is used to rule out cases when a device obviously
|
---|
125 | * does not contain a exfat file system.
|
---|
126 | */
|
---|
127 | int exfat_sanity_check(exfat_bs_t *bs, devmap_handle_t devmap_handle)
|
---|
128 | {
|
---|
129 | return EOK;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * @}
|
---|
134 | */
|
---|