-
Notifications
You must be signed in to change notification settings - Fork 117
/
partiton.go
280 lines (255 loc) · 8.29 KB
/
partiton.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gpt
import (
"encoding/binary"
"fmt"
"io"
"reflect"
"strings"
"unicode/utf16"
"github.com/diskfs/go-diskfs/util"
uuid "github.com/google/uuid"
)
// PartitionEntrySize fixed size of a GPT partition entry
const PartitionEntrySize = 128
// Partition represents the structure of a single partition on the disk
type Partition struct {
Start uint64 // start sector for the partition
End uint64 // end sector for the partition
Size uint64 // size of the partition in bytes
Type Type // parttype for the partition
Name string // name for the partition
GUID string // partition GUID, can be left blank to auto-generate
Attributes uint64 // Attributes flags
logicalSectorSize int
physicalSectorSize int
}
func reverseSlice(s interface{}) {
size := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
for i, j := 0, size-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
}
// toBytes return the 128 bytes for this partition
func (p *Partition) toBytes() ([]byte, error) {
b := make([]byte, PartitionEntrySize, PartitionEntrySize)
// if the Type is Unused, just return all zeroes
if p.Type == Unused {
return b, nil
}
// partition type GUID is first 16 bytes
typeGUID, err := uuid.Parse(string(p.Type))
if err != nil {
return nil, fmt.Errorf("Unable to parse partition type GUID: %v", err)
}
copy(b[0:16], bytesToUUIDBytes(typeGUID[0:16]))
// partition identifier GUID is next 16 bytes
idGUID, err := uuid.Parse(p.GUID)
if err != nil {
return nil, fmt.Errorf("Unable to parse partition identifier GUID: %v", err)
}
copy(b[16:32], bytesToUUIDBytes(idGUID[0:16]))
// next is first LBA and last LBA, uint64 = 8 bytes each
binary.LittleEndian.PutUint64(b[32:40], p.Start)
binary.LittleEndian.PutUint64(b[40:48], p.End)
binary.LittleEndian.PutUint64(b[48:56], p.Attributes)
// now the partition name - it is UTF16LE encoded, max 36 code units for 72 bytes
r := make([]rune, 0, len(p.Name))
// first convert to runes
for _, s := range p.Name {
r = append(r, rune(s))
}
if len(r) > 36 {
return nil, fmt.Errorf("Cannot use %s as partition name, has %d Unicode code units, maximum size is 36", p.Name, len(r))
}
// next convert the runes to uint16
nameb := utf16.Encode(r)
// and then convert to little-endian bytes
for i, u := range nameb {
pos := 56 + i*2
binary.LittleEndian.PutUint16(b[pos:pos+2], u)
}
return b, nil
}
// FromBytes create a partition entry from bytes
func partitionFromBytes(b []byte, logicalSectorSize, physicalSectorSize int) (*Partition, error) {
if len(b) != PartitionEntrySize {
return nil, fmt.Errorf("Data for partition was %d bytes instead of expected %d", len(b), PartitionEntrySize)
}
// is it all zeroes?
typeGUID, err := uuid.FromBytes(bytesToUUIDBytes(b[0:16]))
if err != nil {
return nil, fmt.Errorf("unable to read partition type GUID: %v", err)
}
typeString := typeGUID.String()
uuid, err := uuid.FromBytes(bytesToUUIDBytes(b[16:32]))
if err != nil {
return nil, fmt.Errorf("unable to read partition identifier GUID: %v", err)
}
firstLBA := binary.LittleEndian.Uint64(b[32:40])
lastLBA := binary.LittleEndian.Uint64(b[40:48])
attribs := binary.LittleEndian.Uint64(b[48:56])
// get the partition name
nameb := b[56:]
u := make([]uint16, 0, 72)
for i := 0; i < len(nameb); i += 2 {
// strip any 0s off of the end
entry := binary.LittleEndian.Uint16(nameb[i : i+2])
if entry == 0 {
break
}
u = append(u, entry)
}
r := utf16.Decode(u)
name := string(r)
return &Partition{
Start: firstLBA,
End: lastLBA,
Name: name,
GUID: strings.ToUpper(uuid.String()),
Attributes: attribs,
Type: Type(strings.ToUpper(typeString)),
logicalSectorSize: logicalSectorSize,
physicalSectorSize: physicalSectorSize,
}, nil
}
func (p *Partition) GetSize() int64 {
// size already is in Bytes
return int64(p.Size)
}
func (p *Partition) GetStart() int64 {
_, lss := p.sectorSizes()
return int64(p.Start) * int64(lss)
}
// WriteContents fills the partition with the contents provided
// reads from beginning of reader to exactly size of partition in bytes
func (p *Partition) WriteContents(f util.File, contents io.Reader) (uint64, error) {
pss, lss := p.sectorSizes()
total := uint64(0)
// validate start/end/size
calculatedSize := (p.End - p.Start + 1) * uint64(lss)
switch {
case p.Size <= 0 && p.End > p.Start:
p.Size = calculatedSize
case p.Size > 0 && p.End <= p.Start:
p.End = p.Start + p.Size/uint64(lss)
case p.Size > 0 && p.Size == calculatedSize:
// all is good
default:
return total, fmt.Errorf("Cannot reconcile partition size %d with start %d / end %d", p.Size, p.Start, p.End)
}
// chunks of physical sector size for efficient writing
b := make([]byte, pss, pss)
// we start at the correct byte location
start := p.Start * uint64(lss)
// loop in physical sector sizes
for {
read, err := contents.Read(b)
if err != nil && err != io.EOF {
return total, fmt.Errorf("Could not read contents to pass to partition: %v", err)
}
tmpTotal := uint64(read) + total
if uint64(tmpTotal) > p.Size {
return total, fmt.Errorf("Requested to write at least %d bytes to partition but maximum size is %d", tmpTotal, p.Size)
}
if read > 0 {
var written int
written, err = f.WriteAt(b[:read], int64(start+total))
if err != nil {
return total, fmt.Errorf("Error writing to file: %v", err)
}
total = total + uint64(written)
}
// increment our total
// is this the end of the data?
if err == io.EOF {
break
}
}
// did the total written equal the size of the partition?
if uint64(total) != p.Size {
return total, fmt.Errorf("Write %d bytes to partition but actual size is %d", total, p.Size)
}
return total, nil
}
// ReadContents reads the contents of the partition into a writer
// streams the entire partition to the writer
func (p *Partition) ReadContents(f util.File, out io.Writer) (int64, error) {
pss, _ := p.sectorSizes()
total := int64(0)
// chunks of physical sector size for efficient writing
b := make([]byte, pss, pss)
// we start at the correct byte location
start := p.GetStart()
size := p.GetSize()
// loop in physical sector sizes
for {
read, err := f.ReadAt(b, int64(start)+total)
if err != nil && err != io.EOF {
return total, fmt.Errorf("Error reading from file: %v", err)
}
if read > 0 {
out.Write(b[:read])
}
// increment our total
total += int64(read)
// is this the end of the data?
if err == io.EOF || total >= int64(size) {
break
}
}
return total, nil
}
// initEntry adjust the Start/End/Size entries and ensure it has a GUID
func (p *Partition) initEntry(blocksize uint64, starting uint64) error {
part := p
if part.Type == Unused {
return nil
}
var guid uuid.UUID
if part.GUID == "" {
guid, _ = uuid.NewRandom()
} else {
var err error
guid, err = uuid.Parse(part.GUID)
if err != nil {
return fmt.Errorf("Invalid UUID: %s", part.GUID)
}
}
part.GUID = strings.ToUpper(guid.String())
// check size matches sectors
// valid possibilities:
// 1- size=0, start>=0, end>start - valid - begin at start, go until end
// 2- size>0, start>=0, end=0 - valid - begin at start for size bytes
// 3- size>0, start=0, end=0 - valid - begin at end of previous partition, go for size bytes
// anything else is an error
size, start, end := part.Size, part.Start, part.End
calculatedSize := (end - start + 1) * blocksize
switch {
case start >= 0 && end > start && size == calculatedSize:
case size == 0 && start >= 0 && end > start:
// provided specific start and end, so calculate size
part.Size = uint64(calculatedSize)
case size > 0 && start > 0 && end == 0:
part.End = start + size/uint64(blocksize) - 1
case size > 0 && start == 0 && end == 0:
// we start right after the end of the previous
start = uint64(starting)
end = start + size/uint64(blocksize) - 1
part.Start = start
part.End = end
default:
return fmt.Errorf("Invalid partition entry, size %d bytes does not match start sector %d and end sector %d", size, start, end)
}
return nil
}
func (p *Partition) sectorSizes() (physical, logical int) {
physical, logical = p.physicalSectorSize, p.logicalSectorSize
if physical == 0 {
physical = physicalSectorSize
}
if logical == 0 {
logical = logicalSectorSize
}
return physical, logical
}