Skip to content

Commit fb42f8f

Browse files
committed
add gpt internal package and update mbr package
1 parent 927d393 commit fb42f8f

File tree

7 files changed

+449
-14
lines changed

7 files changed

+449
-14
lines changed

Diff for: fat.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ func (dp *dir) create_name(path string) (string, fileResult) {
16911691
if isTermLFN(wc) || isSep(wc) {
16921692
break // Break on end of path or a separator.
16931693
}
1694-
if strings.IndexByte("*:<>|\"?\x7f", byte(wc)) >= 0 {
1694+
if strings.IndexByte(forbiddenChars, byte(wc)) >= 0 {
16951695
return "", frInvalidName
16961696
}
16971697
if di >= lfnBufSize {
@@ -2352,6 +2352,14 @@ func (fsys *FS) gen_numname(dst, src []byte, lfn []uint16, seq uint32) {
23522352
}
23532353
}
23542354

2355+
func clipname(s []byte) []byte {
2356+
i := 0
2357+
for i < len(s) && isfatchar(s[i]) {
2358+
i++
2359+
}
2360+
return s[:i]
2361+
}
2362+
23552363
func bstr(s []byte) []byte {
23562364
i := 0
23572365
for i < len(s) && s[i] != 0 {
@@ -2381,3 +2389,7 @@ func str16(s []uint16) string {
23812389
}
23822390
return string(buf)
23832391
}
2392+
2393+
func isfatchar(c byte) bool {
2394+
return c != ' ' && c != 0 && c < 0x7F
2395+
}

Diff for: format.go

+68-9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func (bs *bootsector) SectorSize() uint16 {
100100
return binary.LittleEndian.Uint16(bs.data[bpbBytsPerSec:])
101101
}
102102

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+
103108
// SectorsPerFAT returns the number of sectors per File Allocation Table.
104109
func (bs *bootsector) SectorsPerFAT() uint32 {
105110
fatsz := uint32(binary.LittleEndian.Uint16(bs.data[bpbFATSz16:]))
@@ -109,23 +114,44 @@ func (bs *bootsector) SectorsPerFAT() uint32 {
109114
return fatsz
110115
}
111116

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+
112123
// NumberOfFATs returns the number of File Allocation Tables. Should be 1 or 2.
113124
func (bs *bootsector) NumberOfFATs() uint8 {
114125
return bs.data[bpbNumFATs]
115126
}
116127

128+
// SetNumberOfFATs sets the number of FATs.
129+
func (bs *bootsector) SetNumberOfFATs(nfats uint8) {
130+
bs.data[bpbNumFATs] = nfats
131+
}
132+
117133
// SectorsPerCluster returns the number of sectors per cluster.
118134
// Should be a power of 2 and not larger than 128.
119135
func (bs *bootsector) SectorsPerCluster() uint16 {
120136
return uint16(bs.data[bpbSecPerClus])
121137
}
122138

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+
123144
// ReservedSectors returns the number of reserved sectors at the beginning of the volume.
124145
// Should be at least 1.
125146
func (bs *bootsector) ReservedSectors() uint16 {
126147
return binary.LittleEndian.Uint16(bs.data[bpbRsvdSecCnt:])
127148
}
128149

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+
129155
// TotalSectors returns the total number of sectors in the volume that
130156
// can be used by the filesystem.
131157
func (bs *bootsector) TotalSectors() uint32 {
@@ -136,22 +162,39 @@ func (bs *bootsector) TotalSectors() uint32 {
136162
return totsec
137163
}
138164

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.
140173
// Should be divisible by SectorSize/32.
141174
func (bs *bootsector) RootDirEntries() uint16 {
142175
return binary.LittleEndian.Uint16(bs.data[bpbRootEntCnt:])
143176
}
144177

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)
148181
}
149182

150183
// RootCluster returns the first cluster of the root directory.
151184
func (bs *bootsector) RootCluster() uint32 {
152185
return binary.LittleEndian.Uint32(bs.data[bpbRootClus32:])
153186
}
154187

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+
155198
func (bs *bootsector) ExtendedBootSignature() uint8 {
156199
return bs.data[bsBootSig32]
157200
}
@@ -178,12 +221,19 @@ func (bs *bootsector) VolumeSerialNumber() uint32 {
178221
}
179222

180223
// 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
183226
copy(label[:], bs.data[bsVolLab32:])
184227
return label
185228
}
186229

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+
187237
// FilesystemType returns the filesystem type string, usually "FAT32 ".
188238
func (bs *bootsector) FilesystemType() [8]byte {
189239
var label [8]byte
@@ -205,6 +255,15 @@ func (bs *bootsector) OEMName() [8]byte {
205255
return oemname
206256
}
207257

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+
208267
func (bs *bootsector) VolumeOffset() uint32 {
209268
return binary.LittleEndian.Uint32(bs.data[bpbHiddSec:])
210269
}
@@ -230,11 +289,11 @@ func (bs *bootsector) Appendf(dst []byte, separator byte) []byte {
230289
dst = append(dst, sep)
231290
}
232291
oem := bs.OEMName()
233-
appendData("OEM", bstr(oem[:]), separator)
292+
appendData("OEM", clipname(oem[:]), separator)
234293
fstype := bs.FilesystemType()
235-
appendData("FSType", bstr(fstype[:]), separator)
294+
appendData("FSType", clipname(fstype[:]), separator)
236295
volLabel := bs.VolumeLabel()
237-
appendData("VolumeLabel", bstr(volLabel[:]), separator)
296+
appendData("VolumeLabel", clipname(volLabel[:]), separator)
238297
appendInt("VolumeSerialNumber", bs.VolumeSerialNumber(), separator)
239298
appendInt("VolumeOffset", bs.VolumeOffset(), separator)
240299
appendInt("SectorSize", uint32(bs.SectorSize()), separator)

Diff for: go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/soypat/fat
22

33
go 1.21
4+
5+
require golang.org/x/text v0.14.0

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
2+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

0 commit comments

Comments
 (0)