Debian Codegraph @

fat/fat.go

raw symbols SCIP
1 package fat
2
3 import (
4 "errors"
5 "fmt"
6 "math"
7
8 "github.com/mitchellh/go-fs"
9 )
10
11 // The first cluster that can really hold user data is always 2
12 const FirstCluster = 2
13
14 // FAT is the actual file allocation table data structure that is
15 // stored on disk to describe the various clusters on the disk.
16 type FAT struct {
17 bs *BootSectorCommon
18 entries []uint32
19 }
20
21 func DecodeFAT(device fs.BlockDevice, bs *BootSectorCommon, n int) (*FAT, error) {
22 if n > int(bs.NumFATs) {
23 return nil, fmt.Errorf("FAT #%d greater than total FATs: %d", n, bs.NumFATs)
24 }
25
26 data := make([]byte, bs.SectorsPerFat*uint32(bs.BytesPerSector))
27 if _, err := device.ReadAt(data, int64(bs.FATOffset(n))); err != nil {
28 return nil, err
29 }
30
31 result := &FAT{
32 bs: bs,
33 entries: make([]uint32, FATEntryCount(bs)),
34 }
35
36 fatType := bs.FATType()
37 for i := 0; i < int(FATEntryCount(bs)); i++ {
38 var entryData uint32
39 switch fatType {
40 case FAT12:
41 entryData = fatReadEntry12(data, i)
42 case FAT16:
43 entryData = fatReadEntry16(data, i)
44 default:
45 entryData = fatReadEntry32(data, i)
46 }
47
48 result.entries[i] = entryData
49 }
50
51 return result, nil
52 }
53
54 // NewFAT creates a new FAT data structure, properly initialized.
55 func NewFAT(bs *BootSectorCommon) (*FAT, error) {
56 result := &FAT{
57 bs: bs,
58 entries: make([]uint32, FATEntryCount(bs)),
59 }
60
61 // Set the initial two entries according to spec
62 result.entries[0] = (uint32(bs.Media) & 0xFF) |
63 (0xFFFFFF00 & result.entryMask())
64 result.entries[1] = 0xFFFFFFFF & result.entryMask()
65
66 return result, nil
67 }
68
69 // Bytes returns the raw bytes for the FAT that should be written to
70 // the block device.
71 func (f *FAT) Bytes() []byte {
72 result := make([]byte, f.bs.SectorsPerFat*uint32(f.bs.BytesPerSector))
73
74 for i, entry := range f.entries {
75 switch f.bs.FATType() {
76 case FAT12:
77 f.writeEntry12(result, i, entry)
78 case FAT16:
79 f.writeEntry16(result, i, entry)
80 default:
81 f.writeEntry32(result, i, entry)
82 }
83 }
84
85 return result
86 }
87
88 func (f *FAT) AllocChain() (uint32, error) {
89 return f.allocNew()
90 }
91
92 func (f *FAT) allocNew() (uint32, error) {
93 dataSize := (f.bs.TotalSectors * uint32(f.bs.BytesPerSector))
94 dataSize -= f.bs.DataOffset()
95 clusterCount := dataSize / f.bs.BytesPerCluster()
96 lastClusterIndex := clusterCount + FirstCluster
97
98 var availIdx uint32
99 found := false
100 for i := uint32(FirstCluster); i < lastClusterIndex; i++ {
101 if f.entries[i] == 0 {
102 availIdx = i
103 found = true
104 break
105 }
106 }
107
108 if !found {
109 return 0, errors.New("FAT FULL")
110 }
111
112 // Mark that this is now in use
113 f.entries[availIdx] = 0xFFFFFFFF & f.entryMask()
114
115 return availIdx, nil
116 }
117
118 // Chain returns the chain of clusters starting at a certain cluster.
119 func (f *FAT) Chain(start uint32) []uint32 {
120 chain := make([]uint32, 0, 2)
121
122 cluster := start
123 for {
124 chain = append(chain, cluster)
125 cluster = f.entries[cluster]
126
127 if f.isEofCluster(cluster) || cluster == 0 {
128 break
129 }
130 }
131
132 return chain
133 }
134
135 // ResizeChain takes a given cluster number and resizes the chain
136 // to the given length. It returns the new chain of clusters.
137 func (f *FAT) ResizeChain(start uint32, length int) ([]uint32, error) {
138 chain := f.Chain(start)
139 if len(chain) == length {
140 return chain, nil
141 }
142
143 change := int(math.Abs(float64(length - len(chain))))
144 if length > len(chain) {
145 var lastCluster uint32
146
147 lastCluster = chain[0]
148 for i := 1; i < len(chain); i++ {
149 if f.isEofCluster(f.entries[lastCluster]) {
150 break
151 }
152
153 lastCluster = chain[i]
154 }
155
156 for i := 0; i < change; i++ {
157 newCluster, err := f.allocNew()
158 if err != nil {
159 return nil, err
160 }
161
162 f.entries[lastCluster] = newCluster
163 lastCluster = newCluster
164 }
165 } else {
166 panic("making chains smaller not implemented yet")
167 }
168
169 return f.Chain(start), nil
170 }
171
172 func (f *FAT) WriteToDevice(device fs.BlockDevice) error {
173 fatBytes := f.Bytes()
174 for i := 0; i < int(f.bs.NumFATs); i++ {
175 offset := int64(f.bs.FATOffset(i))
176 if _, err := device.WriteAt(fatBytes, offset); err != nil {
177 return err
178 }
179 }
180
181 return nil
182 }
183
184 func (f *FAT) entryMask() uint32 {
185 switch f.bs.FATType() {
186 case FAT12:
187 return 0x0FFF
188 case FAT16:
189 return 0xFFFF
190 default:
191 return 0x0FFFFFFF
192 }
193 }
194
195 func (f *FAT) isEofCluster(cluster uint32) bool {
196 return cluster >= (0xFFFFFF8 & f.entryMask())
197 }
198
199 func (f *FAT) writeEntry12(data []byte, idx int, entry uint32) {
200 dataIdx := idx + (idx / 2)
201 data = data[dataIdx : dataIdx+2]
202
203 if idx%2 == 1 {
204 // ODD
205 data[0] |= byte((entry & 0x0F) << 4)
206 data[1] = byte((entry >> 4) & 0xFF)
207 } else {
208 // Even
209 data[0] = byte(entry & 0xFF)
210 data[1] = byte((entry >> 8) & 0x0F)
211 }
212 }
213
214 func (f *FAT) writeEntry16(data []byte, idx int, entry uint32) {
215 idx <<= 1
216 data[idx] = byte(entry & 0xFF)
217 data[idx+1] = byte((entry >> 8) & 0xFF)
218 }
219
220 func (f *FAT) writeEntry32(data []byte, idx int, entry uint32) {
221 idx <<= 2
222 data[idx] = byte(entry & 0xFF)
223 data[idx+1] = byte((entry >> 8) & 0xFF)
224 data[idx+2] = byte((entry >> 16) & 0xFF)
225 data[idx+3] = byte((entry >> 24) & 0xFF)
226 }
227
228 // FATEntryCount returns the number of entries per fat for the given
229 // boot sector.
230 func FATEntryCount(bs *BootSectorCommon) uint32 {
231 // Determine the number of entries that'll go in the FAT.
232 var entryCount uint32 = bs.SectorsPerFat * uint32(bs.BytesPerSector)
233 switch bs.FATType() {
234 case FAT12:
235 entryCount = uint32((uint64(entryCount) * 8) / 12)
236 case FAT16:
237 entryCount /= 2
238 case FAT32:
239 entryCount /= 4
240 default:
241 panic("impossible fat type")
242 }
243
244 return entryCount
245 }
246
247 func fatReadEntry12(data []byte, idx int) uint32 {
248 idx += idx / 2
249
250 var result uint32 = (uint32(data[idx+1]) << 8) | uint32(data[idx])
251 if idx%2 == 0 {
252 return result & 0xFFF
253 } else {
254 return result >> 4
255 }
256 }
257
258 func fatReadEntry16(data []byte, idx int) uint32 {
259 idx <<= 1
260 return (uint32(data[idx+1]) << 8) | uint32(data[idx])
261 }
262
263 func fatReadEntry32(data []byte, idx int) uint32 {
264 idx <<= 2
265 return (uint32(data[idx+3]) << 24) |
266 (uint32(data[idx+2]) << 16) |
267 (uint32(data[idx+1]) << 8) |
268 uint32(data[idx+0])
269 }