Skip to content

Commit 950a59a

Browse files
committed
rename bootsector to biosParamBlock
1 parent 21c4afb commit 950a59a

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

Diff for: fat_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,16 @@ var fatInit = map[int64][512]byte{
330330
510: 0x55, 0xaa},
331331

332332
// Below is the FAT.
333-
32: {0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f,
334-
0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f},
335-
15368: {0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f,
336-
0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f},
333+
32: {
334+
0xf8, 0xff, 0xff, 0x0f, // EOC
335+
0xff, 0xff, 0xff, 0x0f,
336+
0xf8, 0xff, 0xff, 0x0f,
337+
0xff, 0xff, 0xff, 0x0f,
338+
0xff, 0xff, 0xff, 0x0f,
339+
0xff, 0xff, 0xff, 0x0f,
340+
},
341+
// FAT copy for redundancy. Same as above.
342+
15368: {0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f},
337343

338344
30704: { // Root directory contents.
339345
0x6b, 0x65, 0x79, 0x6c, 0x61, 0x72, 0x67, 0x6f, 0x20, 0x20, 0x20, 0x08, 0x00, 0x00, 0xba, 0x53, // |keylargo ....S|
@@ -390,7 +396,7 @@ const dirFileContents = "this is not\nnot the root\nnot the root file\nnope. \nT
390396
func TestBootSector(t *testing.T) {
391397
// Print out the boot sector data.
392398
dat := fatInit[0]
393-
bs := bootsector{data: dat[:]}
399+
bs := biosParamBlock{data: dat[:]}
394400
fsiLBA := bs.FSInfo()
395401
fatLBA := 0 + bs.ReservedSectors()
396402
fatSz := bs.SectorsPerFAT() * uint32(bs.SectorSize())

Diff for: format.go

+37-33
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,26 @@ func (f *Formatter) move_window(addr lba) error {
9191
return nil
9292
}
9393

94-
type bootsector struct {
94+
// biosParamBlock a.k.a BPB is the BIOS Parameter Block for FAT32 volumes.
95+
// It provides details on the filesystem type (FAT12, FAT16, FAT32),
96+
// sectors per cluster, total sectors, FAT size, and more, which are essential
97+
// for understanding the filesystem layout and capacity.
98+
type biosParamBlock struct {
9599
data []byte
96100
}
97101

98102
// SectorSize returns the size of a sector in bytes.
99-
func (bs *bootsector) SectorSize() uint16 {
103+
func (bs *biosParamBlock) SectorSize() uint16 {
100104
return binary.LittleEndian.Uint16(bs.data[bpbBytsPerSec:])
101105
}
102106

103107
// SetSectorSize sets the size of a sector in bytes.
104-
func (bs *bootsector) SetSectorSize(size uint16) {
108+
func (bs *biosParamBlock) SetSectorSize(size uint16) {
105109
binary.LittleEndian.PutUint16(bs.data[bpbBytsPerSec:], size)
106110
}
107111

108112
// SectorsPerFAT returns the number of sectors per File Allocation Table.
109-
func (bs *bootsector) SectorsPerFAT() uint32 {
113+
func (bs *biosParamBlock) SectorsPerFAT() uint32 {
110114
fatsz := uint32(binary.LittleEndian.Uint16(bs.data[bpbFATSz16:]))
111115
if fatsz == 0 {
112116
fatsz = binary.LittleEndian.Uint32(bs.data[bpbFATSz32:])
@@ -115,29 +119,29 @@ func (bs *bootsector) SectorsPerFAT() uint32 {
115119
}
116120

117121
// SetSectorsPerFAT sets the number of sectors per File Allocation Table.
118-
func (bs *bootsector) SetSectorsPerFAT(fatsz uint32) {
122+
func (bs *biosParamBlock) SetSectorsPerFAT(fatsz uint32) {
119123
binary.LittleEndian.PutUint16(bs.data[bpbFATSz16:], 0)
120124
binary.LittleEndian.PutUint32(bs.data[bpbFATSz32:], fatsz)
121125
}
122126

123127
// NumberOfFATs returns the number of File Allocation Tables. Should be 1 or 2.
124-
func (bs *bootsector) NumberOfFATs() uint8 {
128+
func (bs *biosParamBlock) NumberOfFATs() uint8 {
125129
return bs.data[bpbNumFATs]
126130
}
127131

128132
// SetNumberOfFATs sets the number of FATs.
129-
func (bs *bootsector) SetNumberOfFATs(nfats uint8) {
133+
func (bs *biosParamBlock) SetNumberOfFATs(nfats uint8) {
130134
bs.data[bpbNumFATs] = nfats
131135
}
132136

133137
// SectorsPerCluster returns the number of sectors per cluster.
134138
// Should be a power of 2 and not larger than 128.
135-
func (bs *bootsector) SectorsPerCluster() uint16 {
139+
func (bs *biosParamBlock) SectorsPerCluster() uint16 {
136140
return uint16(bs.data[bpbSecPerClus])
137141
}
138142

139143
// SetSectorsPerCluster sets the number of sectors per cluster. Should be power of 2.
140-
func (bs *bootsector) SetSectorsPerCluster(spclus uint16) {
144+
func (bs *biosParamBlock) SetSectorsPerCluster(spclus uint16) {
141145
bs.data[bpbSecPerClus] = byte(spclus)
142146
}
143147

@@ -146,18 +150,18 @@ func (bs *bootsector) SetSectorsPerCluster(spclus uint16) {
146150
// redundant sectors with these first two. The number of reserved sectors is usually
147151
// 32 for FAT32 systems (~16k for 512 byte sectors).
148152
// Sectors 6 and 7 are usually the backup boot sector and the FS information sector, respectively.
149-
func (bs *bootsector) ReservedSectors() uint16 {
153+
func (bs *biosParamBlock) ReservedSectors() uint16 {
150154
return binary.LittleEndian.Uint16(bs.data[bpbRsvdSecCnt:])
151155
}
152156

153157
// SetReservedSectors sets the number of reserved sectors at the beginning of the volume.
154-
func (bs *bootsector) SetReservedSectors(rsvd uint16) {
158+
func (bs *biosParamBlock) SetReservedSectors(rsvd uint16) {
155159
binary.LittleEndian.PutUint16(bs.data[bpbRsvdSecCnt:], rsvd)
156160
}
157161

158162
// TotalSectors returns the total number of sectors in the volume that
159163
// can be used by the filesystem.
160-
func (bs *bootsector) TotalSectors() uint32 {
164+
func (bs *biosParamBlock) TotalSectors() uint32 {
161165
totsec := uint32(binary.LittleEndian.Uint16(bs.data[bpbTotSec16:]))
162166
if totsec == 0 {
163167
totsec = binary.LittleEndian.Uint32(bs.data[bpbTotSec32:])
@@ -167,111 +171,111 @@ func (bs *bootsector) TotalSectors() uint32 {
167171

168172
// SetTotalSectors sets the total number of sectors in the volume that
169173
// can be used by the filesystem.
170-
func (bs *bootsector) SetTotalSectors(totsec uint32) {
174+
func (bs *biosParamBlock) SetTotalSectors(totsec uint32) {
171175
binary.LittleEndian.PutUint16(bs.data[bpbTotSec16:], 0)
172176
binary.LittleEndian.PutUint32(bs.data[bpbTotSec32:], totsec)
173177
}
174178

175179
// RootDirEntries returns the number of sectors occupied by the root directory.
176180
// Should be divisible by SectorSize/32.
177-
func (bs *bootsector) RootDirEntries() uint16 {
181+
func (bs *biosParamBlock) RootDirEntries() uint16 {
178182
return binary.LittleEndian.Uint16(bs.data[bpbRootEntCnt:])
179183
}
180184

181185
// SetRootDirEntries sets the number of sectors occupied by the root directory.
182-
func (bs *bootsector) SetRootDirEntries(entries uint16) {
186+
func (bs *biosParamBlock) SetRootDirEntries(entries uint16) {
183187
binary.LittleEndian.PutUint16(bs.data[bpbRootEntCnt:], entries)
184188
}
185189

186190
// RootCluster returns the first cluster of the root directory.
187-
func (bs *bootsector) RootCluster() uint32 {
191+
func (bs *biosParamBlock) RootCluster() uint32 {
188192
return binary.LittleEndian.Uint32(bs.data[bpbRootClus32:])
189193
}
190194

191195
// SetRootCluster sets the first cluster of the root directory.
192-
func (bs *bootsector) SetRootCluster(cluster uint32) {
196+
func (bs *biosParamBlock) SetRootCluster(cluster uint32) {
193197
binary.LittleEndian.PutUint32(bs.data[bpbRootClus32:], cluster)
194198
}
195199

196200
// Version returns the filesystem version, should be 0.0 for FAT32.
197-
func (bs *bootsector) Version() (major, minor uint8) {
201+
func (bs *biosParamBlock) Version() (major, minor uint8) {
198202
return bs.data[bpbFSVer32], bs.data[bpbFSVer32+1]
199203
}
200204

201-
func (bs *bootsector) ExtendedBootSignature() uint8 {
205+
func (bs *biosParamBlock) ExtendedBootSignature() uint8 {
202206
return bs.data[bsBootSig32]
203207
}
204208

205209
// BootSignature returns the boot signature at offset 510 which should be 0xAA55.
206-
func (bs *bootsector) BootSignature() uint16 {
210+
func (bs *biosParamBlock) BootSignature() uint16 {
207211
return binary.LittleEndian.Uint16(bs.data[bs55AA:])
208212
}
209213

210214
// FSInfo returns the sector number of the FS Information Sector.
211215
// Expect =1 for FAT32.
212-
func (bs *bootsector) FSInfo() uint16 {
216+
func (bs *biosParamBlock) FSInfo() uint16 {
213217
return binary.LittleEndian.Uint16(bs.data[bpbFSInfo32:])
214218
}
215219

216220
// DriveNumber returns the drive number.
217-
func (bs *bootsector) DriveNumber() uint8 {
221+
func (bs *biosParamBlock) DriveNumber() uint8 {
218222
return bs.data[bsDrvNum32]
219223
}
220224

221225
// VolumeSerialNumber returns the volume serial number.
222-
func (bs *bootsector) VolumeSerialNumber() uint32 {
226+
func (bs *biosParamBlock) VolumeSerialNumber() uint32 {
223227
return binary.LittleEndian.Uint32(bs.data[bsVolID32:])
224228
}
225229

226230
// VolumeLabel returns the volume label string.
227-
func (bs *bootsector) VolumeLabel() [11]byte {
231+
func (bs *biosParamBlock) VolumeLabel() [11]byte {
228232
var label [11]byte
229233
copy(label[:], bs.data[bsVolLab32:])
230234
return label
231235
}
232236

233-
func (bs *bootsector) SetVolumeLabel(label string) {
237+
func (bs *biosParamBlock) SetVolumeLabel(label string) {
234238
n := copy(bs.data[bsVolLab32:bsVolLab32+11], label)
235239
for i := n; i < 11; i++ {
236240
bs.data[bsVolLab32+i] = ' '
237241
}
238242
}
239243

240244
// FilesystemType returns the filesystem type string, usually "FAT32 ".
241-
func (bs *bootsector) FilesystemType() [8]byte {
245+
func (bs *biosParamBlock) FilesystemType() [8]byte {
242246
var label [8]byte
243247
copy(label[:], bs.data[bsFilSysType32:])
244248
return label
245249
}
246250

247251
// JumpInstruction returns the x86 jump instruction at the beginning of the boot sector.
248-
func (bs *bootsector) JumpInstruction() [3]byte {
252+
func (bs *biosParamBlock) JumpInstruction() [3]byte {
249253
var jmpboot [3]byte
250254
copy(jmpboot[:], bs.data[0:])
251255
return jmpboot
252256
}
253257

254258
// OEMName returns the Original Equipment Manufacturer name at the start of the bootsector.
255-
func (bs *bootsector) OEMName() [8]byte {
259+
func (bs *biosParamBlock) OEMName() [8]byte {
256260
var oemname [8]byte
257261
copy(oemname[:], bs.data[bsOEMName:])
258262
return oemname
259263
}
260264

261265
// SetOEMName sets the Original Equipment Manufacturer name at the start of the bootsector.
262266
// Will clip off any characters beyond the 8th.
263-
func (bs *bootsector) SetOEMName(name string) {
267+
func (bs *biosParamBlock) SetOEMName(name string) {
264268
n := copy(bs.data[bsOEMName:bsOEMName+8], name)
265269
for i := n; i < 8; i++ {
266270
bs.data[bsOEMName+i] = ' '
267271
}
268272
}
269273

270-
func (bs *bootsector) VolumeOffset() uint32 {
274+
func (bs *biosParamBlock) VolumeOffset() uint32 {
271275
return binary.LittleEndian.Uint32(bs.data[bpbHiddSec:])
272276
}
273277

274-
func (bs *bootsector) String() string {
278+
func (bs *biosParamBlock) String() string {
275279
return string(bs.Appendf(nil, '\n'))
276280
}
277281

@@ -298,7 +302,7 @@ func labelAppendUint32(label string, dst []byte, data uint32, sep byte) []byte {
298302
return labelAppendUint(label, dst, uint64(data), sep)
299303
}
300304

301-
func (bs *bootsector) Appendf(dst []byte, separator byte) []byte {
305+
func (bs *biosParamBlock) Appendf(dst []byte, separator byte) []byte {
302306
appendData := func(name string, data []byte, sep byte) {
303307
dst = labelAppend(dst, name, data, sep)
304308
}
@@ -332,7 +336,7 @@ func (bs *bootsector) Appendf(dst []byte, separator byte) []byte {
332336
}
333337

334338
// bootcode returns the boot code at the end of the boot sector.
335-
func (bs *bootsector) bootcode() []byte {
339+
func (bs *biosParamBlock) bootcode() []byte {
336340
return bs.data[bsBootCode32:bs55AA]
337341
}
338342

0 commit comments

Comments
 (0)