-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathlayout.go
384 lines (327 loc) · 11.6 KB
/
layout.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package gadget
import (
"fmt"
"os"
"path/filepath"
"sort"
)
// LayoutConstraints defines the constraints for arranging structures within a
// volume
type LayoutConstraints struct {
// NonMBRStartOffset is the default start offset of non-MBR structure in
// the volume.
NonMBRStartOffset Size
// SectorSize is the size of the sector to be used for calculations
SectorSize Size
}
// LaidOutVolume defines the size of a volume and arrangement of all the
// structures within it
type LaidOutVolume struct {
*Volume
// Size is the total size of the volume
Size Size
// SectorSize sector size of the volume
SectorSize Size
// LaidOutStructure is a list of structures within the volume, sorted
// by their start offsets
LaidOutStructure []LaidOutStructure
// RootDir is the root directory for volume data
RootDir string
}
// PartiallyLaidOutVolume defines the layout of volume structures, but lacks the
// details about the layout of raw image content within the bare structures.
type PartiallyLaidOutVolume struct {
*Volume
// SectorSize sector size of the volume
SectorSize Size
// LaidOutStructure is a list of structures within the volume, sorted
// by their start offsets
LaidOutStructure []LaidOutStructure
}
// LaidOutStructure describes a VolumeStructure that has been placed within the
// volume
type LaidOutStructure struct {
*VolumeStructure
// StartOffset defines the start offset of the structure within the
// enclosing volume
StartOffset Size
// AbsoluteOffsetWrite is the resolved absolute position of offset-write
// for this structure element within the enclosing volume
AbsoluteOffsetWrite *Size
// Index of the structure definition in gadget YAML
Index int
// LaidOutContent is a list of raw content inside the structure
LaidOutContent []LaidOutContent
}
func (p LaidOutStructure) String() string {
return fmtIndexAndName(p.Index, p.Name)
}
type byStartOffset []LaidOutStructure
func (b byStartOffset) Len() int { return len(b) }
func (b byStartOffset) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byStartOffset) Less(i, j int) bool { return b[i].StartOffset < b[j].StartOffset }
// LaidOutContent describes raw content that has been placed within the
// encompassing structure and volume
type LaidOutContent struct {
*VolumeContent
// StartOffset defines the start offset of this content image
StartOffset Size
// AbsoluteOffsetWrite is the resolved absolute position of offset-write
// for this content element within the enclosing volume
AbsoluteOffsetWrite *Size
// Size is the maximum size occupied by this image
Size Size
// Index of the content in structure declaration inside gadget YAML
Index int
}
func (p LaidOutContent) String() string {
if p.Image != "" {
return fmt.Sprintf("#%v (%q@%#x{%v})", p.Index, p.Image, p.StartOffset, p.Size)
}
return fmt.Sprintf("#%v (source:%q)", p.Index, p.Source)
}
func layoutVolumeStructures(volume *Volume, constraints LayoutConstraints) (structures []LaidOutStructure, byName map[string]*LaidOutStructure, err error) {
previousEnd := Size(0)
structures = make([]LaidOutStructure, len(volume.Structure))
byName = make(map[string]*LaidOutStructure, len(volume.Structure))
if constraints.SectorSize == 0 {
return nil, nil, fmt.Errorf("cannot lay out volume, invalid constraints: sector size cannot be 0")
}
for idx, s := range volume.Structure {
var start Size
if s.Offset == nil {
if s.EffectiveRole() != MBR && previousEnd < constraints.NonMBRStartOffset {
start = constraints.NonMBRStartOffset
} else {
start = previousEnd
}
} else {
start = *s.Offset
}
end := start + s.Size
ps := LaidOutStructure{
VolumeStructure: &volume.Structure[idx],
StartOffset: start,
Index: idx,
}
if ps.EffectiveRole() != MBR {
if s.Size%constraints.SectorSize != 0 {
return nil, nil, fmt.Errorf("cannot lay out volume, structure %v size is not a multiple of sector size %v",
ps, constraints.SectorSize)
}
}
if ps.Name != "" {
byName[ps.Name] = &ps
}
structures[idx] = ps
previousEnd = end
}
// sort by starting offset
sort.Sort(byStartOffset(structures))
previousEnd = Size(0)
for idx, ps := range structures {
if ps.StartOffset < previousEnd {
return nil, nil, fmt.Errorf("cannot lay out volume, structure %v overlaps with preceding structure %v", ps, structures[idx-1])
}
previousEnd = ps.StartOffset + ps.Size
offsetWrite, err := resolveOffsetWrite(ps.OffsetWrite, byName)
if err != nil {
return nil, nil, fmt.Errorf("cannot resolve offset-write of structure %v: %v", ps, err)
}
structures[idx].AbsoluteOffsetWrite = offsetWrite
}
return structures, byName, nil
}
// LayoutVolumePartially attempts to lay out only the structures in the volume using provided constraints
func LayoutVolumePartially(volume *Volume, constraints LayoutConstraints) (*PartiallyLaidOutVolume, error) {
structures, _, err := layoutVolumeStructures(volume, constraints)
if err != nil {
return nil, err
}
vol := &PartiallyLaidOutVolume{
Volume: volume,
SectorSize: constraints.SectorSize,
LaidOutStructure: structures,
}
return vol, nil
}
// LayoutVolume attempts to completely lay out the volume, that is the
// structures and their content, using provided constraints
func LayoutVolume(gadgetRootDir string, volume *Volume, constraints LayoutConstraints) (*LaidOutVolume, error) {
structures, byName, err := layoutVolumeStructures(volume, constraints)
if err != nil {
return nil, err
}
farthestEnd := Size(0)
fartherstOffsetWrite := Size(0)
for idx, ps := range structures {
if ps.AbsoluteOffsetWrite != nil && *ps.AbsoluteOffsetWrite > fartherstOffsetWrite {
fartherstOffsetWrite = *ps.AbsoluteOffsetWrite
}
if end := ps.StartOffset + ps.Size; end > farthestEnd {
farthestEnd = end
}
content, err := layOutStructureContent(gadgetRootDir, &structures[idx], byName)
if err != nil {
return nil, err
}
for _, c := range content {
if c.AbsoluteOffsetWrite != nil && *c.AbsoluteOffsetWrite > fartherstOffsetWrite {
fartherstOffsetWrite = *c.AbsoluteOffsetWrite
}
}
structures[idx].LaidOutContent = content
}
volumeSize := farthestEnd
if fartherstOffsetWrite+SizeLBA48Pointer > farthestEnd {
volumeSize = fartherstOffsetWrite + SizeLBA48Pointer
}
vol := &LaidOutVolume{
Volume: volume,
Size: volumeSize,
SectorSize: constraints.SectorSize,
LaidOutStructure: structures,
RootDir: gadgetRootDir,
}
return vol, nil
}
type byContentStartOffset []LaidOutContent
func (b byContentStartOffset) Len() int { return len(b) }
func (b byContentStartOffset) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byContentStartOffset) Less(i, j int) bool { return b[i].StartOffset < b[j].StartOffset }
func getImageSize(path string) (Size, error) {
stat, err := os.Stat(path)
if err != nil {
return 0, err
}
return Size(stat.Size()), nil
}
func layOutStructureContent(gadgetRootDir string, ps *LaidOutStructure, known map[string]*LaidOutStructure) ([]LaidOutContent, error) {
if ps.HasFilesystem() {
// structures with a filesystem do not need any extra layout
return nil, nil
}
if len(ps.Content) == 0 {
return nil, nil
}
content := make([]LaidOutContent, len(ps.Content))
previousEnd := Size(0)
for idx, c := range ps.Content {
imageSize, err := getImageSize(filepath.Join(gadgetRootDir, c.Image))
if err != nil {
return nil, fmt.Errorf("cannot lay out structure %v: content %q: %v", ps, c.Image, err)
}
var start Size
if c.Offset != nil {
start = *c.Offset
} else {
start = previousEnd
}
actualSize := imageSize
if c.Size != 0 {
if c.Size < imageSize {
return nil, fmt.Errorf("cannot lay out structure %v: content %q size %v is larger than declared %v", ps, c.Image, actualSize, c.Size)
}
actualSize = c.Size
}
offsetWrite, err := resolveOffsetWrite(c.OffsetWrite, known)
if err != nil {
return nil, fmt.Errorf("cannot resolve offset-write of structure %v content %q: %v", ps, c.Image, err)
}
content[idx] = LaidOutContent{
VolumeContent: &ps.Content[idx],
Size: actualSize,
StartOffset: ps.StartOffset + start,
Index: idx,
// break for gofmt < 1.11
AbsoluteOffsetWrite: offsetWrite,
}
previousEnd = start + actualSize
if previousEnd > ps.Size {
return nil, fmt.Errorf("cannot lay out structure %v: content %q does not fit in the structure", ps, c.Image)
}
}
sort.Sort(byContentStartOffset(content))
previousEnd = ps.StartOffset
for idx, pc := range content {
if pc.StartOffset < previousEnd {
return nil, fmt.Errorf("cannot lay out structure %v: content %q overlaps with preceding image %q", ps, pc.Image, content[idx-1].Image)
}
previousEnd = pc.StartOffset + pc.Size
}
return content, nil
}
func resolveOffsetWrite(offsetWrite *RelativeOffset, knownStructs map[string]*LaidOutStructure) (*Size, error) {
if offsetWrite == nil {
return nil, nil
}
var relativeToOffset Size
if offsetWrite.RelativeTo != "" {
otherStruct, ok := knownStructs[offsetWrite.RelativeTo]
if !ok {
return nil, fmt.Errorf("refers to an unknown structure %q", offsetWrite.RelativeTo)
}
relativeToOffset = otherStruct.StartOffset
}
resolvedOffsetWrite := relativeToOffset + offsetWrite.Offset
return &resolvedOffsetWrite, nil
}
// ShiftStructureTo translates the starting offset of a laid out structure and
// its content to the provided offset.
func ShiftStructureTo(ps LaidOutStructure, offset Size) LaidOutStructure {
change := int64(offset - ps.StartOffset)
newPs := ps
newPs.StartOffset = Size(int64(ps.StartOffset) + change)
newPs.LaidOutContent = make([]LaidOutContent, len(ps.LaidOutContent))
for idx, pc := range ps.LaidOutContent {
newPc := pc
newPc.StartOffset = Size(int64(pc.StartOffset) + change)
newPs.LaidOutContent[idx] = newPc
}
return newPs
}
func isLayoutCompatible(current, new *PartiallyLaidOutVolume) error {
if current.ID != new.ID {
return fmt.Errorf("incompatible ID change from %v to %v", current.ID, new.ID)
}
if current.EffectiveSchema() != new.EffectiveSchema() {
return fmt.Errorf("incompatible schema change from %v to %v",
current.EffectiveSchema(), new.EffectiveSchema())
}
if current.Bootloader != new.Bootloader {
return fmt.Errorf("incompatible bootloader change from %v to %v",
current.Bootloader, new.Bootloader)
}
// XXX: the code below asssumes both volumes have the same number of
// structures, this limitation may be lifter later
if len(current.LaidOutStructure) != len(new.LaidOutStructure) {
return fmt.Errorf("incompatible change in the number of structures from %v to %v",
len(current.LaidOutStructure), len(new.LaidOutStructure))
}
// at the structure level we expect the volume to be identical
for i := range current.LaidOutStructure {
from := ¤t.LaidOutStructure[i]
to := &new.LaidOutStructure[i]
if err := canUpdateStructure(from, to, new.EffectiveSchema()); err != nil {
return fmt.Errorf("incompatible structure %v change: %v", to, err)
}
}
return nil
}