/
pm_manifestcodegen.go
252 lines (215 loc) · 6.83 KB
/
pm_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
// 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 github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntbootpolicy
package cbntbootpolicy
import (
"encoding/binary"
"fmt"
"io"
"strings"
"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
"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
_ = cbnt.StructInfo{}
)
// NewPM returns a new instance of PM with
// all default values set.
func NewPM() *PM {
s := &PM{}
copy(s.StructInfo.ID[:], []byte(StructureIDPM))
s.StructInfo.Version = 0x20
s.Rehash()
return s
}
// Validate (recursively) checks the structure if there are any unexpected
// values. It returns an error if so.
func (s *PM) Validate() error {
// See tag "require"
for idx := range s.Reserved0 {
if s.Reserved0[idx] != 0 {
return fmt.Errorf("'Reserved0[%d]' is expected to be 0, but it is %v", idx, s.Reserved0[idx])
}
}
return nil
}
// StructureIDPM is the StructureID (in terms of
// the document #575623) of element 'PM'.
const StructureIDPM = "__PMDA__"
// GetStructInfo returns current value of StructInfo of the structure.
//
// StructInfo is a set of standard fields with presented in any element
// ("element" in terms of document #575623).
func (s *PM) GetStructInfo() cbnt.StructInfo {
return s.StructInfo
}
// SetStructInfo sets new value of StructInfo to the structure.
//
// StructInfo is a set of standard fields with presented in any element
// ("element" in terms of document #575623).
func (s *PM) SetStructInfo(newStructInfo cbnt.StructInfo) {
s.StructInfo = newStructInfo
}
// ReadFrom reads the PM from 'r' in format defined in the document #575623.
func (s *PM) ReadFrom(r io.Reader) (int64, error) {
var totalN int64
err := binary.Read(r, binary.LittleEndian, &s.StructInfo)
if err != nil {
return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err)
}
totalN += int64(binary.Size(s.StructInfo))
n, err := s.ReadDataFrom(r)
if err != nil {
return totalN, fmt.Errorf("unable to read data: %w", err)
}
totalN += n
return totalN, nil
}
// ReadDataFrom reads the PM from 'r' excluding StructInfo,
// in format defined in the document #575623.
func (s *PM) ReadDataFrom(r io.Reader) (int64, error) {
totalN := int64(0)
// StructInfo (ManifestFieldType: structInfo)
{
// ReadDataFrom does not read Struct, use ReadFrom for that.
}
// Reserved0 (ManifestFieldType: arrayStatic)
{
n, err := 2, binary.Read(r, binary.LittleEndian, s.Reserved0[:])
if err != nil {
return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err)
}
totalN += int64(n)
}
// Data (ManifestFieldType: arrayDynamic)
{
var size uint16
err := binary.Read(r, binary.LittleEndian, &size)
if err != nil {
return totalN, fmt.Errorf("unable to the read size of field 'Data': %w", err)
}
totalN += int64(binary.Size(size))
s.Data = make([]byte, size)
n, err := len(s.Data), binary.Read(r, binary.LittleEndian, s.Data)
if err != nil {
return totalN, fmt.Errorf("unable to read field 'Data': %w", err)
}
totalN += int64(n)
}
return totalN, nil
}
// RehashRecursive calls Rehash (see below) recursively.
func (s *PM) RehashRecursive() {
s.StructInfo.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 *PM) Rehash() {
s.Variable0 = 0
s.ElementSize = uint16(s.TotalSize())
}
// WriteTo writes the PM into 'w' in format defined in
// the document #575623.
func (s *PM) WriteTo(w io.Writer) (int64, error) {
totalN := int64(0)
s.Rehash()
// StructInfo (ManifestFieldType: structInfo)
{
n, err := s.StructInfo.WriteTo(w)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
}
totalN += int64(n)
}
// Reserved0 (ManifestFieldType: arrayStatic)
{
n, err := 2, binary.Write(w, binary.LittleEndian, s.Reserved0[:])
if err != nil {
return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err)
}
totalN += int64(n)
}
// Data (ManifestFieldType: arrayDynamic)
{
size := uint16(len(s.Data))
err := binary.Write(w, binary.LittleEndian, size)
if err != nil {
return totalN, fmt.Errorf("unable to write the size of field 'Data': %w", err)
}
totalN += int64(binary.Size(size))
n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data)
if err != nil {
return totalN, fmt.Errorf("unable to write field 'Data': %w", err)
}
totalN += int64(n)
}
return totalN, nil
}
// StructInfoSize returns the size in bytes of the value of field StructInfo
func (s *PM) StructInfoTotalSize() uint64 {
return s.StructInfo.TotalSize()
}
// Reserved0Size returns the size in bytes of the value of field Reserved0
func (s *PM) Reserved0TotalSize() uint64 {
return 2
}
// DataSize returns the size in bytes of the value of field Data
func (s *PM) DataTotalSize() uint64 {
size := uint64(binary.Size(uint16(0)))
size += uint64(len(s.Data))
return size
}
// StructInfoOffset returns the offset in bytes of field StructInfo
func (s *PM) StructInfoOffset() uint64 {
return 0
}
// Reserved0Offset returns the offset in bytes of field Reserved0
func (s *PM) Reserved0Offset() uint64 {
return s.StructInfoOffset() + s.StructInfoTotalSize()
}
// DataOffset returns the offset in bytes of field Data
func (s *PM) DataOffset() uint64 {
return s.Reserved0Offset() + s.Reserved0TotalSize()
}
// Size returns the total size of the PM.
func (s *PM) TotalSize() uint64 {
if s == nil {
return 0
}
var size uint64
size += s.StructInfoTotalSize()
size += s.Reserved0TotalSize()
size += s.DataTotalSize()
return size
}
// PrettyString returns the content of the structure in an easy-to-read format.
func (s *PM) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
var lines []string
if withHeader {
lines = append(lines, pretty.Header(depth, "PM", s))
}
if s == nil {
return strings.Join(lines, "\n")
}
// ManifestFieldType is structInfo
lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
// ManifestFieldType is arrayStatic
lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...)
// ManifestFieldType is arrayDynamic
lines = append(lines, pretty.SubValue(depth+1, "Data", "", &s.Data, opts...)...)
if depth < 2 {
lines = append(lines, "")
}
return strings.Join(lines, "\n")
}