Debian Codegraph @

fat/super_floppy.go

raw symbols SCIP
1 package fat
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7
8 "github.com/mitchellh/go-fs"
9 )
10
11 // SuperFloppyConfig is the configuration for various properties of
12 // a new super floppy formatted block device. Once this configuration is used
13 // to format a device, it must not be modified.
14 type SuperFloppyConfig struct {
15 // The type of FAT filesystem to use.
16 FATType FATType
17
18 // The label of the drive. Defaults to "NONAME"
19 Label string
20
21 // The OEM name for the FAT filesystem. Defaults to "gofs" if not set.
22 OEMName string
23 }
24
25 // Formats an fs.BlockDevice with the "super floppy" format according
26 // to the given configuration. The "super floppy" standard means that the
27 // device will be formatted so that it does not contain a partition table.
28 // Instead, the entire device holds a single FAT file system.
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 // An internal struct that helps maintain state and perform calculations
39 // during a single formatting pass.
40 type superFloppyFormatter struct {
41 config *SuperFloppyConfig
42 device fs.BlockDevice
43 }
44
45 func (f *superFloppyFormatter) format() error {
46 // First, create the boot sector on the device. Start by configuring
47 // the common elements of the boot sector.
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 // Next, fill in the FAT-type specific boot sector information
66 switch f.config.FATType {
67 case FAT12, FAT16:
68 // Determine the filesystem type label, standard from the spec sheet
69 var label string
70 if f.config.FATType == FAT12 {
71 label = "FAT12 "
72 } else {
73 label = "FAT16 "
74 }
75
76 // For 1.44MB Floppy, for other floppy formats see https://support.microsoft.com/en-us/kb/75131.
77 // We make an exception for this most common usecase as the calculations don't create a working image for older operating systems
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 // Determine the number of root directory entries
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 // Write the boot sector
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 // Write the boot sector
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 // TODO(mitchellh): Create the fsinfo structure
132 // TODO(mitchellh): write the boot sector copy
133 default:
134 return fmt.Errorf("Unknown FAT type: %d", f.config.FATType)
135 }
136
137 // Create the FATs
138 fat, err := NewFAT(&bsCommon)
139 if err != nil {
140 return err
141 }
142
143 // Write the FAT
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 }