Debian Codegraph @

fat/boot_sector.go

raw symbols SCIP
1 package fat
2
3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "unicode"
8
9 "github.com/mitchellh/go-fs"
10 )
11
12 type MediaType uint8
13
14 // The standard value for "fixed", non-removable media, directly
15 // from the FAT specification.
16 const MediaFixed MediaType = 0xF8
17
18 type BootSectorCommon struct {
19 OEMName string
20 BytesPerSector uint16
21 SectorsPerCluster uint8
22 ReservedSectorCount uint16
23 NumFATs uint8
24 RootEntryCount uint16
25 TotalSectors uint32
26 Media MediaType
27 SectorsPerFat uint32
28 SectorsPerTrack uint16
29 NumHeads uint16
30 }
31
32 // DecodeBootSector takes a BlockDevice and decodes the FAT boot sector
33 // from it.
34 func DecodeBootSector(device fs.BlockDevice) (*BootSectorCommon, error) {
35 var sector [512]byte
36 if _, err := device.ReadAt(sector[:], 0); err != nil {
37 return nil, err
38 }
39
40 if sector[510] != 0x55 || sector[511] != 0xAA {
41 return nil, errors.New("corrupt boot sector signature")
42 }
43
44 result := new(BootSectorCommon)
45
46 // BS_OEMName
47 result.OEMName = string(sector[3:11])
48
49 // BPB_BytsPerSec
50 result.BytesPerSector = binary.LittleEndian.Uint16(sector[11:13])
51
52 // BPB_SecPerClus
53 result.SectorsPerCluster = sector[13]
54
55 // BPB_RsvdSecCnt
56 result.ReservedSectorCount = binary.LittleEndian.Uint16(sector[14:16])
57
58 // BPB_NumFATs
59 result.NumFATs = sector[16]
60
61 // BPB_RootEntCnt
62 result.RootEntryCount = binary.LittleEndian.Uint16(sector[17:19])
63
64 // BPB_Media
65 result.Media = MediaType(sector[21])
66
67 // BPB_SecPerTrk
68 result.SectorsPerTrack = binary.LittleEndian.Uint16(sector[24:26])
69
70 // BPB_NumHeads
71 result.NumHeads = binary.LittleEndian.Uint16(sector[26:28])
72
73 // BPB_TotSec16 / BPB_TotSec32
74 result.TotalSectors = uint32(binary.LittleEndian.Uint16(sector[19:21]))
75 if result.TotalSectors == 0 {
76 result.TotalSectors = binary.LittleEndian.Uint32(sector[32:36])
77 }
78
79 // BPB_FATSz16 / BPB_FATSz32
80 result.SectorsPerFat = uint32(binary.LittleEndian.Uint16(sector[22:24]))
81 if result.SectorsPerFat == 0 {
82 result.SectorsPerFat = binary.LittleEndian.Uint32(sector[36:40])
83 }
84
85 return result, nil
86 }
87
88 func (b *BootSectorCommon) Bytes() ([]byte, error) {
89 var sector [512]byte
90
91 // BS_jmpBoot
92 sector[0] = 0xEB
93 sector[1] = 0x3C
94 sector[2] = 0x90
95
96 // BS_OEMName
97 if len(b.OEMName) > 8 {
98 return nil, errors.New("OEMName must be 8 bytes or less")
99 }
100
101 for i, r := range b.OEMName {
102 if r > unicode.MaxASCII {
103 return nil, fmt.Errorf("%#U in OEM name not a valid ASCII char. Must be ASCII.", r)
104 }
105
106 sector[0x3+i] = byte(r)
107 }
108
109 // BPB_BytsPerSec
110 binary.LittleEndian.PutUint16(sector[11:13], b.BytesPerSector)
111
112 // BPB_SecPerClus
113 sector[13] = uint8(b.SectorsPerCluster)
114
115 // BPB_RsvdSecCnt
116 binary.LittleEndian.PutUint16(sector[14:16], b.ReservedSectorCount)
117
118 // BPB_NumFATs
119 sector[16] = b.NumFATs
120
121 // BPB_RootEntCnt
122 binary.LittleEndian.PutUint16(sector[17:19], b.RootEntryCount)
123
124 // BPB_Media
125 sector[21] = byte(b.Media)
126
127 // BPB_SecPerTrk
128 binary.LittleEndian.PutUint16(sector[24:26], b.SectorsPerTrack)
129
130 // BPB_Numheads
131 binary.LittleEndian.PutUint16(sector[26:28], b.NumHeads)
132
133 // BPB_Hiddsec
134 // sector[28:32] - it is always set to 0 because we don't partition drives yet.
135
136 // Important signature of every FAT boot sector
137 sector[510] = 0x55
138 sector[511] = 0xAA
139
140 return sector[:], nil
141 }
142
143 // BytesPerCluster returns the number of bytes per cluster.
144 func (b *BootSectorCommon) BytesPerCluster() uint32 {
145 return uint32(b.SectorsPerCluster) * uint32(b.BytesPerSector)
146 }
147
148 // ClusterOffset returns the offset of the data section of a particular
149 // cluster.
150 func (b *BootSectorCommon) ClusterOffset(n int) uint32 {
151 offset := b.DataOffset()
152 offset += (uint32(n) - FirstCluster) * b.BytesPerCluster()
153 return offset
154 }
155
156 // DataOffset returns the offset of the data section of the disk.
157 func (b *BootSectorCommon) DataOffset() uint32 {
158 offset := uint32(b.RootDirOffset())
159 offset += uint32(b.RootEntryCount * DirectoryEntrySize)
160 return offset
161 }
162
163 // FATOffset returns the offset in bytes for the given index of the FAT
164 func (b *BootSectorCommon) FATOffset(n int) int {
165 offset := uint32(b.ReservedSectorCount * b.BytesPerSector)
166 offset += b.SectorsPerFat * uint32(b.BytesPerSector) * uint32(n)
167 return int(offset)
168 }
169
170 // Calculates the FAT type that this boot sector represents.
171 func (b *BootSectorCommon) FATType() FATType {
172 var rootDirSectors uint32
173 rootDirSectors = (uint32(b.RootEntryCount) * 32) + (uint32(b.BytesPerSector) - 1)
174 rootDirSectors /= uint32(b.BytesPerSector)
175 dataSectors := b.SectorsPerFat * uint32(b.NumFATs)
176 dataSectors += uint32(b.ReservedSectorCount)
177 dataSectors += rootDirSectors
178 dataSectors = b.TotalSectors - dataSectors
179 countClusters := dataSectors / uint32(b.SectorsPerCluster)
180
181 switch {
182 case countClusters < 4085:
183 return FAT12
184 case countClusters < 65525:
185 return FAT16
186 default:
187 return FAT32
188 }
189 }
190
191 // RootDirOffset returns the byte offset when the root directory
192 // entries for FAT12/16 filesystems start. NOTE: This is absolutely useless
193 // for FAT32 because the root directory is just the beginning of the data
194 // region.
195 func (b *BootSectorCommon) RootDirOffset() int {
196 offset := b.FATOffset(0)
197 offset += int(uint32(b.NumFATs) * b.SectorsPerFat * uint32(b.BytesPerSector))
198 return offset
199 }
200
201 // BootSectorFat16 is the BootSector for FAT12 and FAT16 filesystems.
202 // It contains the common fields to all FAT filesystems and also some
203 // unique.
204 type BootSectorFat16 struct {
205 BootSectorCommon
206
207 DriveNumber uint8
208 VolumeID uint32
209 VolumeLabel string
210 FileSystemTypeLabel string
211 }
212
213 func (b *BootSectorFat16) Bytes() ([]byte, error) {
214 sector, err := b.BootSectorCommon.Bytes()
215 if err != nil {
216 return nil, err
217 }
218
219 // BPB_TotSec16 AND BPB_TotSec32
220 if b.TotalSectors < 0x10000 {
221 binary.LittleEndian.PutUint16(sector[19:21], uint16(b.TotalSectors))
222 } else {
223 binary.LittleEndian.PutUint32(sector[32:36], b.TotalSectors)
224 }
225
226 // BPB_FATSz16
227 if b.SectorsPerFat > 0x10000 {
228 return nil, fmt.Errorf("SectorsPerFat value too big for non-FAT32: %d", b.SectorsPerFat)
229 }
230
231 binary.LittleEndian.PutUint16(sector[22:24], uint16(b.SectorsPerFat))
232
233 // BS_DrvNum
234 sector[36] = b.DriveNumber
235
236 // BS_BootSig
237 sector[38] = 0x29
238
239 // BS_VolID
240 binary.LittleEndian.PutUint32(sector[39:43], b.VolumeID)
241
242 // BS_VolLab
243 if len(b.VolumeLabel) > 11 {
244 return nil, errors.New("VolumeLabel must be 11 bytes or less")
245 }
246
247 for i, r := range b.VolumeLabel {
248 if r > unicode.MaxASCII {
249 return nil, fmt.Errorf("%#U in VolumeLabel not a valid ASCII char. Must be ASCII.", r)
250 }
251
252 sector[43+i] = byte(r)
253 }
254
255 // BS_FilSysType
256 if len(b.FileSystemTypeLabel) > 8 {
257 return nil, errors.New("FileSystemTypeLabel must be 8 bytes or less")
258 }
259
260 for i, r := range b.FileSystemTypeLabel {
261 if r > unicode.MaxASCII {
262 return nil, fmt.Errorf("%#U in FileSystemTypeLabel not a valid ASCII char. Must be ASCII.", r)
263 }
264
265 sector[54+i] = byte(r)
266 }
267
268 return sector, nil
269 }
270
271 type BootSectorFat32 struct {
272 BootSectorCommon
273
274 RootCluster uint32
275 FSInfoSector uint16
276 BackupBootSector uint16
277 DriveNumber uint8
278 VolumeID uint32
279 VolumeLabel string
280 FileSystemTypeLabel string
281 }
282
283 func (b *BootSectorFat32) Bytes() ([]byte, error) {
284 sector, err := b.BootSectorCommon.Bytes()
285 if err != nil {
286 return nil, err
287 }
288
289 // BPB_RootEntCount - must be 0
290 sector[17] = 0
291 sector[18] = 0
292
293 // BPB_FATSz32
294 binary.LittleEndian.PutUint32(sector[36:40], b.SectorsPerFat)
295
296 // BPB_ExtFlags - Unused?
297
298 // BPB_FSVer. Explicitly set to 0 because that is really important
299 // to get correct.
300 sector[42] = 0
301 sector[43] = 0
302
303 // BPB_RootClus
304 binary.LittleEndian.PutUint32(sector[44:48], b.RootCluster)
305
306 // BPB_FSInfo
307 binary.LittleEndian.PutUint16(sector[48:50], b.FSInfoSector)
308
309 // BPB_BkBootSec
310 binary.LittleEndian.PutUint16(sector[50:52], b.BackupBootSector)
311
312 // BS_DrvNum
313 sector[64] = b.DriveNumber
314
315 // BS_BootSig
316 sector[66] = 0x29
317
318 // BS_VolID
319 binary.LittleEndian.PutUint32(sector[67:71], b.VolumeID)
320
321 // BS_VolLab
322 if len(b.VolumeLabel) > 11 {
323 return nil, errors.New("VolumeLabel must be 11 bytes or less")
324 }
325
326 for i, r := range b.VolumeLabel {
327 if r > unicode.MaxASCII {
328 return nil, fmt.Errorf("%#U in VolumeLabel not a valid ASCII char. Must be ASCII.", r)
329 }
330
331 sector[71+i] = byte(r)
332 }
333
334 // BS_FilSysType
335 if len(b.FileSystemTypeLabel) > 8 {
336 return nil, errors.New("FileSystemTypeLabel must be 8 bytes or less")
337 }
338
339 for i, r := range b.FileSystemTypeLabel {
340 if r > unicode.MaxASCII {
341 return nil, fmt.Errorf("%#U in FileSystemTypeLabel not a valid ASCII char. Must be ASCII.", r)
342 }
343
344 sector[82+i] = byte(r)
345 }
346
347 return sector, nil
348 }