| 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 |
|
| 15 |
|
| 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 |
|
| 33 |
|
| 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 |
|
| 47 |
result.OEMName = string(sector[3:11]) |
| 48 |
|
| 49 |
|
| 50 |
result.BytesPerSector = binary.LittleEndian.Uint16(sector[11:13]) |
| 51 |
|
| 52 |
|
| 53 |
result.SectorsPerCluster = sector[13] |
| 54 |
|
| 55 |
|
| 56 |
result.ReservedSectorCount = binary.LittleEndian.Uint16(sector[14:16]) |
| 57 |
|
| 58 |
|
| 59 |
result.NumFATs = sector[16] |
| 60 |
|
| 61 |
|
| 62 |
result.RootEntryCount = binary.LittleEndian.Uint16(sector[17:19]) |
| 63 |
|
| 64 |
|
| 65 |
result.Media = MediaType(sector[21]) |
| 66 |
|
| 67 |
|
| 68 |
result.SectorsPerTrack = binary.LittleEndian.Uint16(sector[24:26]) |
| 69 |
|
| 70 |
|
| 71 |
result.NumHeads = binary.LittleEndian.Uint16(sector[26:28]) |
| 72 |
|
| 73 |
|
| 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 |
|
| 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 |
|
| 92 |
sector[0] = 0xEB |
| 93 |
sector[1] = 0x3C |
| 94 |
sector[2] = 0x90 |
| 95 |
|
| 96 |
|
| 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 |
|
| 110 |
binary.LittleEndian.PutUint16(sector[11:13], b.BytesPerSector) |
| 111 |
|
| 112 |
|
| 113 |
sector[13] = uint8(b.SectorsPerCluster) |
| 114 |
|
| 115 |
|
| 116 |
binary.LittleEndian.PutUint16(sector[14:16], b.ReservedSectorCount) |
| 117 |
|
| 118 |
|
| 119 |
sector[16] = b.NumFATs |
| 120 |
|
| 121 |
|
| 122 |
binary.LittleEndian.PutUint16(sector[17:19], b.RootEntryCount) |
| 123 |
|
| 124 |
|
| 125 |
sector[21] = byte(b.Media) |
| 126 |
|
| 127 |
|
| 128 |
binary.LittleEndian.PutUint16(sector[24:26], b.SectorsPerTrack) |
| 129 |
|
| 130 |
|
| 131 |
binary.LittleEndian.PutUint16(sector[26:28], b.NumHeads) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
sector[510] = 0x55 |
| 138 |
sector[511] = 0xAA |
| 139 |
|
| 140 |
return sector[:], nil |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (b *BootSectorCommon) BytesPerCluster() uint32 { |
| 145 |
return uint32(b.SectorsPerCluster) * uint32(b.BytesPerSector) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 157 |
func (b *BootSectorCommon) DataOffset() uint32 { |
| 158 |
offset := uint32(b.RootDirOffset()) |
| 159 |
offset += uint32(b.RootEntryCount * DirectoryEntrySize) |
| 160 |
return offset |
| 161 |
} |
| 162 |
|
| 163 |
|
| 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 |
|
| 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 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 234 |
sector[36] = b.DriveNumber |
| 235 |
|
| 236 |
|
| 237 |
sector[38] = 0x29 |
| 238 |
|
| 239 |
|
| 240 |
binary.LittleEndian.PutUint32(sector[39:43], b.VolumeID) |
| 241 |
|
| 242 |
|
| 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 |
|
| 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 |
|
| 290 |
sector[17] = 0 |
| 291 |
sector[18] = 0 |
| 292 |
|
| 293 |
|
| 294 |
binary.LittleEndian.PutUint32(sector[36:40], b.SectorsPerFat) |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
sector[42] = 0 |
| 301 |
sector[43] = 0 |
| 302 |
|
| 303 |
|
| 304 |
binary.LittleEndian.PutUint32(sector[44:48], b.RootCluster) |
| 305 |
|
| 306 |
|
| 307 |
binary.LittleEndian.PutUint16(sector[48:50], b.FSInfoSector) |
| 308 |
|
| 309 |
|
| 310 |
binary.LittleEndian.PutUint16(sector[50:52], b.BackupBootSector) |
| 311 |
|
| 312 |
|
| 313 |
sector[64] = b.DriveNumber |
| 314 |
|
| 315 |
|
| 316 |
sector[66] = 0x29 |
| 317 |
|
| 318 |
|
| 319 |
binary.LittleEndian.PutUint32(sector[67:71], b.VolumeID) |
| 320 |
|
| 321 |
|
| 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 |
|
| 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 |
} |