| 1 |
package fat |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/mitchellh/go-fs" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type SuperFloppyConfig struct { |
| 15 |
|
| 16 |
FATType FATType |
| 17 |
|
| 18 |
|
| 19 |
Label string |
| 20 |
|
| 21 |
|
| 22 |
OEMName string |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func FormatSuperFloppy(device fs.BlockDevice, config *SuperFloppyConfig) error { |
| 30 |
formatter := &superFloppyFormatter{ |
| 31 |
config: config, |
| 32 |
device: device, |
| 33 |
} |
| 34 |
|
| 35 |
return formatter.format() |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type superFloppyFormatter struct { |
| 41 |
config *SuperFloppyConfig |
| 42 |
device fs.BlockDevice |
| 43 |
} |
| 44 |
|
| 45 |
func (f *superFloppyFormatter) format() error { |
| 46 |
|
| 47 |
|
| 48 |
sectorsPerCluster, err := f.SectorsPerCluster() |
| 49 |
if err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
bsCommon := BootSectorCommon{ |
| 54 |
BytesPerSector: uint16(f.device.SectorSize()), |
| 55 |
Media: MediaFixed, |
| 56 |
NumFATs: 2, |
| 57 |
NumHeads: 16, |
| 58 |
OEMName: f.config.OEMName, |
| 59 |
ReservedSectorCount: f.ReservedSectorCount(), |
| 60 |
SectorsPerCluster: sectorsPerCluster, |
| 61 |
SectorsPerTrack: 32, |
| 62 |
TotalSectors: uint32(f.device.Len() / int64(f.device.SectorSize())), |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
switch f.config.FATType { |
| 67 |
case FAT12, FAT16: |
| 68 |
|
| 69 |
var label string |
| 70 |
if f.config.FATType == FAT12 { |
| 71 |
label = "FAT12 " |
| 72 |
} else { |
| 73 |
label = "FAT16 " |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if f.config.FATType == FAT12 && f.device.Len() == 1474560 { |
| 79 |
bsCommon.RootEntryCount = 224 |
| 80 |
bsCommon.SectorsPerFat = 9 |
| 81 |
bsCommon.SectorsPerTrack = 18 |
| 82 |
bsCommon.Media = 240 |
| 83 |
bsCommon.NumHeads = 2 |
| 84 |
} else { |
| 85 |
|
| 86 |
if f.device.Len() > 512*5*32 { |
| 87 |
bsCommon.RootEntryCount = 512 |
| 88 |
} else { |
| 89 |
bsCommon.RootEntryCount = uint16(f.device.Len() / (5 * 32)) |
| 90 |
} |
| 91 |
|
| 92 |
bsCommon.SectorsPerFat = f.sectorsPerFat(bsCommon.RootEntryCount, sectorsPerCluster) |
| 93 |
} |
| 94 |
|
| 95 |
bs := &BootSectorFat16{ |
| 96 |
BootSectorCommon: bsCommon, |
| 97 |
FileSystemTypeLabel: label, |
| 98 |
VolumeLabel: f.config.Label, |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
bsBytes, err := bs.Bytes() |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
|
| 107 |
if _, err := f.device.WriteAt(bsBytes, 0); err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
case FAT32: |
| 111 |
bsCommon.SectorsPerFat = f.sectorsPerFat(0, sectorsPerCluster) |
| 112 |
|
| 113 |
bs := &BootSectorFat32{ |
| 114 |
BootSectorCommon: bsCommon, |
| 115 |
FileSystemTypeLabel: "FAT32 ", |
| 116 |
FSInfoSector: 1, |
| 117 |
VolumeID: uint32(time.Now().Unix()), |
| 118 |
VolumeLabel: f.config.Label, |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
bsBytes, err := bs.Bytes() |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
if _, err := f.device.WriteAt(bsBytes, 0); err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
default: |
| 134 |
return fmt.Errorf("Unknown FAT type: %d", f.config.FATType) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
fat, err := NewFAT(&bsCommon) |
| 139 |
if err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
if err := fat.WriteToDevice(f.device); err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
|
| 148 |
var rootDir *DirectoryCluster |
| 149 |
if f.config.FATType == FAT32 { |
| 150 |
panic("TODO") |
| 151 |
} else { |
| 152 |
rootDir, err = NewFat16RootDirectoryCluster(&bsCommon, f.config.Label) |
| 153 |
if err != nil { |
| 154 |
return err |
| 155 |
} |
| 156 |
|
| 157 |
offset := int64(bsCommon.RootDirOffset()) |
| 158 |
if _, err := f.device.WriteAt(rootDir.Bytes(), offset); err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
func (f *superFloppyFormatter) ReservedSectorCount() uint16 { |
| 167 |
if f.config.FATType == FAT32 { |
| 168 |
return 32 |
| 169 |
} else { |
| 170 |
return 1 |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func (f *superFloppyFormatter) SectorsPerCluster() (uint8, error) { |
| 175 |
if f.config.FATType == FAT12 { |
| 176 |
return f.defaultSectorsPerCluster12() |
| 177 |
} else if f.config.FATType == FAT16 { |
| 178 |
return f.defaultSectorsPerCluster16() |
| 179 |
} else { |
| 180 |
return f.defaultSectorsPerCluster32() |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func (f *superFloppyFormatter) defaultSectorsPerCluster12() (uint8, error) { |
| 185 |
var result uint8 = 1 |
| 186 |
sectors := f.device.Len() / int64(f.device.SectorSize()) |
| 187 |
|
| 188 |
for (sectors / int64(result)) > 4084 { |
| 189 |
result *= 2 |
| 190 |
if int(result)*f.device.SectorSize() > 4096 { |
| 191 |
return 0, errors.New("disk too large for FAT12") |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return result, nil |
| 196 |
} |
| 197 |
|
| 198 |
func (f *superFloppyFormatter) defaultSectorsPerCluster16() (uint8, error) { |
| 199 |
sectors := f.device.Len() / int64(f.device.SectorSize()) |
| 200 |
|
| 201 |
if sectors <= 8400 { |
| 202 |
return 0, errors.New("disk too small for FAT16") |
| 203 |
} else if sectors > 4194304 { |
| 204 |
return 0, errors.New("disk too large for FAT16") |
| 205 |
} |
| 206 |
|
| 207 |
switch { |
| 208 |
case sectors > 2097152: |
| 209 |
return 64, nil |
| 210 |
case sectors > 1048576: |
| 211 |
return 32, nil |
| 212 |
case sectors > 524288: |
| 213 |
return 16, nil |
| 214 |
case sectors > 262144: |
| 215 |
return 8, nil |
| 216 |
case sectors > 32680: |
| 217 |
return 4, nil |
| 218 |
default: |
| 219 |
return 2, nil |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func (f *superFloppyFormatter) defaultSectorsPerCluster32() (uint8, error) { |
| 224 |
sectors := f.device.Len() / int64(f.device.SectorSize()) |
| 225 |
|
| 226 |
if sectors <= 66600 { |
| 227 |
return 0, errors.New("disk too small for FAT32") |
| 228 |
} |
| 229 |
|
| 230 |
switch { |
| 231 |
case sectors > 67108864: |
| 232 |
return 64, nil |
| 233 |
case sectors > 33554432: |
| 234 |
return 32, nil |
| 235 |
case sectors > 16777216: |
| 236 |
return 16, nil |
| 237 |
case sectors > 532480: |
| 238 |
return 8, nil |
| 239 |
default: |
| 240 |
return 1, nil |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func (f *superFloppyFormatter) fatCount() uint8 { |
| 245 |
return 2 |
| 246 |
} |
| 247 |
|
| 248 |
func (f *superFloppyFormatter) sectorsPerFat(rootEntCount uint16, sectorsPerCluster uint8) uint32 { |
| 249 |
bytesPerSec := f.device.SectorSize() |
| 250 |
totalSectors := int(f.device.Len()) / bytesPerSec |
| 251 |
rootDirSectors := ((int(rootEntCount) * 32) + (bytesPerSec - 1)) / bytesPerSec |
| 252 |
|
| 253 |
tmp1 := totalSectors - (int(f.ReservedSectorCount()) + rootDirSectors) |
| 254 |
tmp2 := (256 * int(sectorsPerCluster)) + int(f.fatCount()) |
| 255 |
|
| 256 |
if f.config.FATType == FAT32 { |
| 257 |
tmp2 /= 2 |
| 258 |
} |
| 259 |
|
| 260 |
return uint32((tmp1 + (tmp2 - 1)) / tmp2) |
| 261 |
} |