/
manifest_manifestcodegen.go
337 lines (302 loc) · 9.11 KB
/
manifest_manifestcodegen.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
// Copyright 2017-2021 the LinuxBoot Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !manifestcodegen
// +build !manifestcodegen
// Code generated by "menifestcodegen". DO NOT EDIT.
// To reproduce: go run github.com/linuxboot/fiano/pkg/intel/metadata/common/manifestcodegen/cmd/manifestcodegen -package bg github.com/linuxboot/fiano/pkg/intel/metadata/bg/bgbootpolicy
package bgbootpolicy
import (
"encoding/binary"
"fmt"
"io"
"strings"
"github.com/linuxboot/fiano/pkg/intel/metadata/bg"
"github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty"
)
var (
// Just to avoid errors in "import" above in case if it wasn't used below
_ = binary.LittleEndian
_ = (fmt.Stringer)(nil)
_ = (io.Reader)(nil)
_ = pretty.Header
_ = strings.Join
_ = bg.StructInfo{}
)
// NewManifest returns a new instance of Manifest with
// all default values set.
func NewManifest() *Manifest {
s := &Manifest{}
// Recursively initializing a child structure:
s.BPMH = *NewBPMH()
// Recursively initializing a child structure:
s.PMSE = *NewSignature()
s.Rehash()
return s
}
// Validate (recursively) checks the structure if there are any unexpected
// values. It returns an error if so.
func (s *Manifest) Validate() error {
// Recursively validating a child structure:
if err := s.BPMH.Validate(); err != nil {
return fmt.Errorf("error on field 'BPMH': %w", err)
}
// See tag "rehashValue"
{
expectedValue := BPMH(s.rehashedBPMH())
if s.BPMH != expectedValue {
return fmt.Errorf("field 'BPMH' expects write-value '%v', but has %v", expectedValue, s.BPMH)
}
}
// Recursively validating a child structure:
if err := s.PMSE.Validate(); err != nil {
return fmt.Errorf("error on field 'PMSE': %w", err)
}
return nil
}
// fieldIndexByStructID returns the position index within
// structure Manifest of the field by its StructureID
// (see document #575623, an example of StructureID value is "__KEYM__").
func (_ Manifest) fieldIndexByStructID(structID string) int {
switch structID {
case StructureIDBPMH:
return 0
case StructureIDSE:
return 1
case StructureIDPM:
return 2
case StructureIDSignature:
return 3
}
return -1
}
// fieldNameByIndex returns the name of the field by its position number
// within structure Manifest.
func (_ Manifest) fieldNameByIndex(fieldIndex int) string {
switch fieldIndex {
case 0:
return "BPMH"
case 1:
return "SE"
case 2:
return "PME"
case 3:
return "PMSE"
}
return fmt.Sprintf("invalidFieldIndex_%d", fieldIndex)
}
// ReadFrom reads the Manifest from 'r' in format defined in the document #575623.
func (s *Manifest) ReadFrom(r io.Reader) (returnN int64, returnErr error) {
var missingFieldsByIndices = [4]bool{
0: true,
3: true,
}
defer func() {
if returnErr != nil {
return
}
for fieldIndex, v := range missingFieldsByIndices {
if v {
returnErr = fmt.Errorf("field '%s' is missing", s.fieldNameByIndex(fieldIndex))
break
}
}
}()
var totalN int64
previousFieldIndex := int(-1)
for {
var structInfo bg.StructInfo
err := binary.Read(r, binary.LittleEndian, &structInfo)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return totalN, nil
}
if err != nil {
return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err)
}
totalN += int64(binary.Size(structInfo))
structID := structInfo.ID.String()
fieldIndex := s.fieldIndexByStructID(structID)
if fieldIndex < 0 {
// TODO: report error "unknown structure ID: '"+structID+"'"
continue
}
if bg.StrictOrderCheck && fieldIndex < previousFieldIndex {
return totalN, fmt.Errorf("invalid order of fields (%d < %d): structure '%s' is out of order", fieldIndex, previousFieldIndex, structID)
}
missingFieldsByIndices[fieldIndex] = false
var n int64
switch structID {
case StructureIDBPMH:
if fieldIndex == previousFieldIndex {
return totalN, fmt.Errorf("field 'BPMH' is not a slice, but multiple elements found")
}
s.BPMH.SetStructInfo(structInfo)
n, err = s.BPMH.ReadDataFrom(r)
if err != nil {
return totalN, fmt.Errorf("unable to read field BPMH at %d: %w", totalN, err)
}
case StructureIDSE:
var el SE
el.SetStructInfo(structInfo)
n, err = el.ReadDataFrom(r)
s.SE = append(s.SE, el)
if err != nil {
return totalN, fmt.Errorf("unable to read field SE at %d: %w", totalN, err)
}
case StructureIDPM:
if fieldIndex == previousFieldIndex {
return totalN, fmt.Errorf("field 'PME' is not a slice, but multiple elements found")
}
s.PME = &PM{}
s.PME.SetStructInfo(structInfo)
n, err = s.PME.ReadDataFrom(r)
if err != nil {
return totalN, fmt.Errorf("unable to read field PME at %d: %w", totalN, err)
}
case StructureIDSignature:
if fieldIndex == previousFieldIndex {
return totalN, fmt.Errorf("field 'PMSE' is not a slice, but multiple elements found")
}
s.PMSE.SetStructInfo(structInfo)
n, err = s.PMSE.ReadDataFrom(r)
if err != nil {
return totalN, fmt.Errorf("unable to read field PMSE at %d: %w", totalN, err)
}
default:
return totalN, fmt.Errorf("there is no field with structure ID '%s' in Manifest", structInfo.ID)
}
totalN += n
previousFieldIndex = fieldIndex
}
}
// RehashRecursive calls Rehash (see below) recursively.
func (s *Manifest) RehashRecursive() {
s.BPMH.Rehash()
if s.PME != nil {
s.PME.Rehash()
}
s.PMSE.Rehash()
s.Rehash()
}
// Rehash sets values which are calculated automatically depending on the rest
// data. It is usually about the total size field of an element.
func (s *Manifest) Rehash() {
s.BPMH = BPMH(s.rehashedBPMH())
}
// WriteTo writes the Manifest into 'w' in format defined in
// the document #575623.
func (s *Manifest) WriteTo(w io.Writer) (int64, error) {
totalN := int64(0)
s.Rehash()
// BPMH (ManifestFieldType: element)
{
n, err := s.BPMH.WriteTo(w)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'BPMH': %w", err)
}
totalN += int64(n)
}
// SE (ManifestFieldType: elementList)
{
for idx := range s.SE {
n, err := s.SE[idx].WriteTo(w)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'SE[%d]': %w", idx, err)
}
totalN += int64(n)
}
}
// PME (ManifestFieldType: element)
if s.PME != nil {
n, err := s.PME.WriteTo(w)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'PME': %w", err)
}
totalN += int64(n)
}
// PMSE (ManifestFieldType: element)
{
n, err := s.PMSE.WriteTo(w)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'PMSE': %w", err)
}
totalN += int64(n)
}
return totalN, nil
}
// BPMHSize returns the size in bytes of the value of field BPMH
func (s *Manifest) BPMHTotalSize() uint64 {
return s.BPMH.TotalSize()
}
// SESize returns the size in bytes of the value of field SE
func (s *Manifest) SETotalSize() uint64 {
var size uint64
for idx := range s.SE {
size += s.SE[idx].TotalSize()
}
return size
}
// PMESize returns the size in bytes of the value of field PME
func (s *Manifest) PMETotalSize() uint64 {
return s.PME.TotalSize()
}
// PMSESize returns the size in bytes of the value of field PMSE
func (s *Manifest) PMSETotalSize() uint64 {
return s.PMSE.TotalSize()
}
// BPMHOffset returns the offset in bytes of field BPMH
func (s *Manifest) BPMHOffset() uint64 {
return 0
}
// SEOffset returns the offset in bytes of field SE
func (s *Manifest) SEOffset() uint64 {
return s.BPMHOffset() + s.BPMHTotalSize()
}
// PMEOffset returns the offset in bytes of field PME
func (s *Manifest) PMEOffset() uint64 {
return s.SEOffset() + s.SETotalSize()
}
// PMSEOffset returns the offset in bytes of field PMSE
func (s *Manifest) PMSEOffset() uint64 {
return s.PMEOffset() + s.PMETotalSize()
}
// Size returns the total size of the Manifest.
func (s *Manifest) TotalSize() uint64 {
if s == nil {
return 0
}
var size uint64
size += s.BPMHTotalSize()
size += s.SETotalSize()
size += s.PMETotalSize()
size += s.PMSETotalSize()
return size
}
// PrettyString returns the content of the structure in an easy-to-read format.
func (s *Manifest) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
var lines []string
if withHeader {
lines = append(lines, pretty.Header(depth, "Boot Policy Manifest", s))
}
if s == nil {
return strings.Join(lines, "\n")
}
// ManifestFieldType is element
lines = append(lines, pretty.SubValue(depth+1, "BPMH: Header", "", &s.BPMH, opts...)...)
// ManifestFieldType is elementList
lines = append(lines, pretty.Header(depth+1, fmt.Sprintf("SE: Array of \"Boot Policy Manifest\" of length %d", len(s.SE)), s.SE))
for i := 0; i < len(s.SE); i++ {
lines = append(lines, fmt.Sprintf("%sitem #%d: ", strings.Repeat(" ", int(depth+2)), i)+strings.TrimSpace(s.SE[i].PrettyString(depth+2, true)))
}
if depth < 1 {
lines = append(lines, "")
}
// ManifestFieldType is element
lines = append(lines, pretty.SubValue(depth+1, "PME: Platform Manufacturer", "", s.PME, opts...)...)
// ManifestFieldType is element
lines = append(lines, pretty.SubValue(depth+1, "PMSE: Signature", "", &s.PMSE, opts...)...)
if depth < 2 {
lines = append(lines, "")
}
return strings.Join(lines, "\n")
}