@@ -100,6 +100,11 @@ func (bs *bootsector) SectorSize() uint16 {
100
100
return binary .LittleEndian .Uint16 (bs .data [bpbBytsPerSec :])
101
101
}
102
102
103
+ // SetSectorSize sets the size of a sector in bytes.
104
+ func (bs * bootsector ) SetSectorSize (size uint16 ) {
105
+ binary .LittleEndian .PutUint16 (bs .data [bpbBytsPerSec :], size )
106
+ }
107
+
103
108
// SectorsPerFAT returns the number of sectors per File Allocation Table.
104
109
func (bs * bootsector ) SectorsPerFAT () uint32 {
105
110
fatsz := uint32 (binary .LittleEndian .Uint16 (bs .data [bpbFATSz16 :]))
@@ -109,23 +114,44 @@ func (bs *bootsector) SectorsPerFAT() uint32 {
109
114
return fatsz
110
115
}
111
116
117
+ // SetSectorsPerFAT sets the number of sectors per File Allocation Table.
118
+ func (bs * bootsector ) SetSectorsPerFAT (fatsz uint32 ) {
119
+ binary .LittleEndian .PutUint16 (bs .data [bpbFATSz16 :], 0 )
120
+ binary .LittleEndian .PutUint32 (bs .data [bpbFATSz32 :], fatsz )
121
+ }
122
+
112
123
// NumberOfFATs returns the number of File Allocation Tables. Should be 1 or 2.
113
124
func (bs * bootsector ) NumberOfFATs () uint8 {
114
125
return bs .data [bpbNumFATs ]
115
126
}
116
127
128
+ // SetNumberOfFATs sets the number of FATs.
129
+ func (bs * bootsector ) SetNumberOfFATs (nfats uint8 ) {
130
+ bs .data [bpbNumFATs ] = nfats
131
+ }
132
+
117
133
// SectorsPerCluster returns the number of sectors per cluster.
118
134
// Should be a power of 2 and not larger than 128.
119
135
func (bs * bootsector ) SectorsPerCluster () uint16 {
120
136
return uint16 (bs .data [bpbSecPerClus ])
121
137
}
122
138
139
+ // SetSectorsPerCluster sets the number of sectors per cluster. Should be power of 2.
140
+ func (bs * bootsector ) SetSectorsPerCluster (spclus uint16 ) {
141
+ bs .data [bpbSecPerClus ] = byte (spclus )
142
+ }
143
+
123
144
// ReservedSectors returns the number of reserved sectors at the beginning of the volume.
124
145
// Should be at least 1.
125
146
func (bs * bootsector ) ReservedSectors () uint16 {
126
147
return binary .LittleEndian .Uint16 (bs .data [bpbRsvdSecCnt :])
127
148
}
128
149
150
+ // SetReservedSectors sets the number of reserved sectors at the beginning of the volume.
151
+ func (bs * bootsector ) SetReservedSectors (rsvd uint16 ) {
152
+ binary .LittleEndian .PutUint16 (bs .data [bpbRsvdSecCnt :], rsvd )
153
+ }
154
+
129
155
// TotalSectors returns the total number of sectors in the volume that
130
156
// can be used by the filesystem.
131
157
func (bs * bootsector ) TotalSectors () uint32 {
@@ -136,22 +162,39 @@ func (bs *bootsector) TotalSectors() uint32 {
136
162
return totsec
137
163
}
138
164
139
- // RootDirSectors returns the number of sectors occupied by the root directory.
165
+ // SetTotalSectors sets the total number of sectors in the volume that
166
+ // can be used by the filesystem.
167
+ func (bs * bootsector ) SetTotalSectors (totsec uint32 ) {
168
+ binary .LittleEndian .PutUint16 (bs .data [bpbTotSec16 :], 0 )
169
+ binary .LittleEndian .PutUint32 (bs .data [bpbTotSec32 :], totsec )
170
+ }
171
+
172
+ // RootDirEntries returns the number of sectors occupied by the root directory.
140
173
// Should be divisible by SectorSize/32.
141
174
func (bs * bootsector ) RootDirEntries () uint16 {
142
175
return binary .LittleEndian .Uint16 (bs .data [bpbRootEntCnt :])
143
176
}
144
177
145
- // Version returns the filesystem version, should be 0.0 for FAT32 .
146
- func (bs * bootsector ) Version () ( major , minor uint8 ) {
147
- return bs .data [bpbFSVer32 ], bs . data [ bpbFSVer32 + 1 ]
178
+ // SetRootDirEntries sets the number of sectors occupied by the root directory .
179
+ func (bs * bootsector ) SetRootDirEntries ( entries uint16 ) {
180
+ binary . LittleEndian . PutUint16 ( bs .data [bpbRootEntCnt : ], entries )
148
181
}
149
182
150
183
// RootCluster returns the first cluster of the root directory.
151
184
func (bs * bootsector ) RootCluster () uint32 {
152
185
return binary .LittleEndian .Uint32 (bs .data [bpbRootClus32 :])
153
186
}
154
187
188
+ // SetRootCluster sets the first cluster of the root directory.
189
+ func (bs * bootsector ) SetRootCluster (cluster uint32 ) {
190
+ binary .LittleEndian .PutUint32 (bs .data [bpbRootClus32 :], cluster )
191
+ }
192
+
193
+ // Version returns the filesystem version, should be 0.0 for FAT32.
194
+ func (bs * bootsector ) Version () (major , minor uint8 ) {
195
+ return bs .data [bpbFSVer32 ], bs .data [bpbFSVer32 + 1 ]
196
+ }
197
+
155
198
func (bs * bootsector ) ExtendedBootSignature () uint8 {
156
199
return bs .data [bsBootSig32 ]
157
200
}
@@ -178,12 +221,19 @@ func (bs *bootsector) VolumeSerialNumber() uint32 {
178
221
}
179
222
180
223
// VolumeLabel returns the volume label string.
181
- func (bs * bootsector ) VolumeLabel () [8 ]byte {
182
- var label [8 ]byte
224
+ func (bs * bootsector ) VolumeLabel () [11 ]byte {
225
+ var label [11 ]byte
183
226
copy (label [:], bs .data [bsVolLab32 :])
184
227
return label
185
228
}
186
229
230
+ func (bs * bootsector ) SetVolumeLabel (label string ) {
231
+ n := copy (bs .data [bsVolLab32 :bsVolLab32 + 11 ], label )
232
+ for i := n ; i < 11 ; i ++ {
233
+ bs .data [bsVolLab32 + i ] = ' '
234
+ }
235
+ }
236
+
187
237
// FilesystemType returns the filesystem type string, usually "FAT32 ".
188
238
func (bs * bootsector ) FilesystemType () [8 ]byte {
189
239
var label [8 ]byte
@@ -205,6 +255,15 @@ func (bs *bootsector) OEMName() [8]byte {
205
255
return oemname
206
256
}
207
257
258
+ // SetOEMName sets the Original Equipment Manufacturer name at the start of the bootsector.
259
+ // Will clip off any characters beyond the 8th.
260
+ func (bs * bootsector ) SetOEMName (name string ) {
261
+ n := copy (bs .data [bsOEMName :bsOEMName + 8 ], name )
262
+ for i := n ; i < 8 ; i ++ {
263
+ bs .data [bsOEMName + i ] = ' '
264
+ }
265
+ }
266
+
208
267
func (bs * bootsector ) VolumeOffset () uint32 {
209
268
return binary .LittleEndian .Uint32 (bs .data [bpbHiddSec :])
210
269
}
@@ -230,11 +289,11 @@ func (bs *bootsector) Appendf(dst []byte, separator byte) []byte {
230
289
dst = append (dst , sep )
231
290
}
232
291
oem := bs .OEMName ()
233
- appendData ("OEM" , bstr (oem [:]), separator )
292
+ appendData ("OEM" , clipname (oem [:]), separator )
234
293
fstype := bs .FilesystemType ()
235
- appendData ("FSType" , bstr (fstype [:]), separator )
294
+ appendData ("FSType" , clipname (fstype [:]), separator )
236
295
volLabel := bs .VolumeLabel ()
237
- appendData ("VolumeLabel" , bstr (volLabel [:]), separator )
296
+ appendData ("VolumeLabel" , clipname (volLabel [:]), separator )
238
297
appendInt ("VolumeSerialNumber" , bs .VolumeSerialNumber (), separator )
239
298
appendInt ("VolumeOffset" , bs .VolumeOffset (), separator )
240
299
appendInt ("SectorSize" , uint32 (bs .SectorSize ()), separator )
0 commit comments