github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/bgbootpolicy/bpmh_manifestcodegen.go (about) 1 // Copyright 2017-2021 the LinuxBoot Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !manifestcodegen 6 // +build !manifestcodegen 7 8 // Code generated by "menifestcodegen". DO NOT EDIT. 9 // 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 10 11 package bgbootpolicy 12 13 import ( 14 "encoding/binary" 15 "fmt" 16 "io" 17 "strings" 18 19 "github.com/linuxboot/fiano/pkg/intel/metadata/bg" 20 "github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty" 21 ) 22 23 var ( 24 // Just to avoid errors in "import" above in case if it wasn't used below 25 _ = binary.LittleEndian 26 _ = (fmt.Stringer)(nil) 27 _ = (io.Reader)(nil) 28 _ = pretty.Header 29 _ = strings.Join 30 _ = bg.StructInfo{} 31 ) 32 33 // NewBPMH returns a new instance of BPMH with 34 // all default values set. 35 func NewBPMH() *BPMH { 36 s := &BPMH{} 37 copy(s.StructInfo.ID[:], []byte(StructureIDBPMH)) 38 s.StructInfo.Version = 0x10 39 s.Rehash() 40 return s 41 } 42 43 // Validate (recursively) checks the structure if there are any unexpected 44 // values. It returns an error if so. 45 func (s *BPMH) Validate() error { 46 // See tag "require" 47 for idx := range s.Reserved0 { 48 if s.Reserved0[idx] != 0 { 49 return fmt.Errorf("'Reserved0[%d]' is expected to be 0, but it is %v", idx, s.Reserved0[idx]) 50 } 51 } 52 53 return nil 54 } 55 56 // StructureIDBPMH is the StructureID (in terms of 57 // the document #575623) of element 'BPMH'. 58 const StructureIDBPMH = "__ACBP__" 59 60 // GetStructInfo returns current value of StructInfo of the structure. 61 // 62 // StructInfo is a set of standard fields with presented in any element 63 // ("element" in terms of document #575623). 64 func (s *BPMH) GetStructInfo() bg.StructInfo { 65 return s.StructInfo 66 } 67 68 // SetStructInfo sets new value of StructInfo to the structure. 69 // 70 // StructInfo is a set of standard fields with presented in any element 71 // ("element" in terms of document #575623). 72 func (s *BPMH) SetStructInfo(newStructInfo bg.StructInfo) { 73 s.StructInfo = newStructInfo 74 } 75 76 // ReadFrom reads the BPMH from 'r' in format defined in the document #575623. 77 func (s *BPMH) ReadFrom(r io.Reader) (int64, error) { 78 var totalN int64 79 80 err := binary.Read(r, binary.LittleEndian, &s.StructInfo) 81 if err != nil { 82 return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err) 83 } 84 totalN += int64(binary.Size(s.StructInfo)) 85 86 n, err := s.ReadDataFrom(r) 87 if err != nil { 88 return totalN, fmt.Errorf("unable to read data: %w", err) 89 } 90 totalN += n 91 92 return totalN, nil 93 } 94 95 // ReadDataFrom reads the BPMH from 'r' excluding StructInfo, 96 // in format defined in the document #575623. 97 func (s *BPMH) ReadDataFrom(r io.Reader) (int64, error) { 98 totalN := int64(0) 99 100 // StructInfo (ManifestFieldType: structInfo) 101 { 102 // ReadDataFrom does not read Struct, use ReadFrom for that. 103 } 104 105 // HdrStructVersion (ManifestFieldType: endValue) 106 { 107 n, err := 1, binary.Read(r, binary.LittleEndian, &s.HdrStructVersion) 108 if err != nil { 109 return totalN, fmt.Errorf("unable to read field 'HdrStructVersion': %w", err) 110 } 111 totalN += int64(n) 112 } 113 114 // PMBPMVersion (ManifestFieldType: endValue) 115 { 116 n, err := 1, binary.Read(r, binary.LittleEndian, &s.PMBPMVersion) 117 if err != nil { 118 return totalN, fmt.Errorf("unable to read field 'PMBPMVersion': %w", err) 119 } 120 totalN += int64(n) 121 } 122 123 // BPMSVN (ManifestFieldType: endValue) 124 { 125 n, err := 1, binary.Read(r, binary.LittleEndian, &s.BPMSVN) 126 if err != nil { 127 return totalN, fmt.Errorf("unable to read field 'BPMSVN': %w", err) 128 } 129 totalN += int64(n) 130 } 131 132 // ACMSVNAuth (ManifestFieldType: endValue) 133 { 134 n, err := 1, binary.Read(r, binary.LittleEndian, &s.ACMSVNAuth) 135 if err != nil { 136 return totalN, fmt.Errorf("unable to read field 'ACMSVNAuth': %w", err) 137 } 138 totalN += int64(n) 139 } 140 141 // Reserved0 (ManifestFieldType: arrayStatic) 142 { 143 n, err := 1, binary.Read(r, binary.LittleEndian, s.Reserved0[:]) 144 if err != nil { 145 return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err) 146 } 147 totalN += int64(n) 148 } 149 150 // NEMDataStack (ManifestFieldType: endValue) 151 { 152 n, err := 2, binary.Read(r, binary.LittleEndian, &s.NEMDataStack) 153 if err != nil { 154 return totalN, fmt.Errorf("unable to read field 'NEMDataStack': %w", err) 155 } 156 totalN += int64(n) 157 } 158 159 return totalN, nil 160 } 161 162 // RehashRecursive calls Rehash (see below) recursively. 163 func (s *BPMH) RehashRecursive() { 164 s.StructInfo.Rehash() 165 s.Rehash() 166 } 167 168 // Rehash sets values which are calculated automatically depending on the rest 169 // data. It is usually about the total size field of an element. 170 func (s *BPMH) Rehash() { 171 } 172 173 // WriteTo writes the BPMH into 'w' in format defined in 174 // the document #575623. 175 func (s *BPMH) WriteTo(w io.Writer) (int64, error) { 176 totalN := int64(0) 177 s.Rehash() 178 179 // StructInfo (ManifestFieldType: structInfo) 180 { 181 n, err := s.StructInfo.WriteTo(w) 182 if err != nil { 183 return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err) 184 } 185 totalN += int64(n) 186 } 187 188 // HdrStructVersion (ManifestFieldType: endValue) 189 { 190 n, err := 1, binary.Write(w, binary.LittleEndian, &s.HdrStructVersion) 191 if err != nil { 192 return totalN, fmt.Errorf("unable to write field 'HdrStructVersion': %w", err) 193 } 194 totalN += int64(n) 195 } 196 197 // PMBPMVersion (ManifestFieldType: endValue) 198 { 199 n, err := 1, binary.Write(w, binary.LittleEndian, &s.PMBPMVersion) 200 if err != nil { 201 return totalN, fmt.Errorf("unable to write field 'PMBPMVersion': %w", err) 202 } 203 totalN += int64(n) 204 } 205 206 // BPMSVN (ManifestFieldType: endValue) 207 { 208 n, err := 1, binary.Write(w, binary.LittleEndian, &s.BPMSVN) 209 if err != nil { 210 return totalN, fmt.Errorf("unable to write field 'BPMSVN': %w", err) 211 } 212 totalN += int64(n) 213 } 214 215 // ACMSVNAuth (ManifestFieldType: endValue) 216 { 217 n, err := 1, binary.Write(w, binary.LittleEndian, &s.ACMSVNAuth) 218 if err != nil { 219 return totalN, fmt.Errorf("unable to write field 'ACMSVNAuth': %w", err) 220 } 221 totalN += int64(n) 222 } 223 224 // Reserved0 (ManifestFieldType: arrayStatic) 225 { 226 n, err := 1, binary.Write(w, binary.LittleEndian, s.Reserved0[:]) 227 if err != nil { 228 return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err) 229 } 230 totalN += int64(n) 231 } 232 233 // NEMDataStack (ManifestFieldType: endValue) 234 { 235 n, err := 2, binary.Write(w, binary.LittleEndian, &s.NEMDataStack) 236 if err != nil { 237 return totalN, fmt.Errorf("unable to write field 'NEMDataStack': %w", err) 238 } 239 totalN += int64(n) 240 } 241 242 return totalN, nil 243 } 244 245 // StructInfoSize returns the size in bytes of the value of field StructInfo 246 func (s *BPMH) StructInfoTotalSize() uint64 { 247 return s.StructInfo.TotalSize() 248 } 249 250 // HdrStructVersionSize returns the size in bytes of the value of field HdrStructVersion 251 func (s *BPMH) HdrStructVersionTotalSize() uint64 { 252 return 1 253 } 254 255 // PMBPMVersionSize returns the size in bytes of the value of field PMBPMVersion 256 func (s *BPMH) PMBPMVersionTotalSize() uint64 { 257 return 1 258 } 259 260 // BPMSVNSize returns the size in bytes of the value of field BPMSVN 261 func (s *BPMH) BPMSVNTotalSize() uint64 { 262 return 1 263 } 264 265 // ACMSVNAuthSize returns the size in bytes of the value of field ACMSVNAuth 266 func (s *BPMH) ACMSVNAuthTotalSize() uint64 { 267 return 1 268 } 269 270 // Reserved0Size returns the size in bytes of the value of field Reserved0 271 func (s *BPMH) Reserved0TotalSize() uint64 { 272 return 1 273 } 274 275 // NEMDataStackSize returns the size in bytes of the value of field NEMDataStack 276 func (s *BPMH) NEMDataStackTotalSize() uint64 { 277 return 2 278 } 279 280 // StructInfoOffset returns the offset in bytes of field StructInfo 281 func (s *BPMH) StructInfoOffset() uint64 { 282 return 0 283 } 284 285 // HdrStructVersionOffset returns the offset in bytes of field HdrStructVersion 286 func (s *BPMH) HdrStructVersionOffset() uint64 { 287 return s.StructInfoOffset() + s.StructInfoTotalSize() 288 } 289 290 // PMBPMVersionOffset returns the offset in bytes of field PMBPMVersion 291 func (s *BPMH) PMBPMVersionOffset() uint64 { 292 return s.HdrStructVersionOffset() + s.HdrStructVersionTotalSize() 293 } 294 295 // BPMSVNOffset returns the offset in bytes of field BPMSVN 296 func (s *BPMH) BPMSVNOffset() uint64 { 297 return s.PMBPMVersionOffset() + s.PMBPMVersionTotalSize() 298 } 299 300 // ACMSVNAuthOffset returns the offset in bytes of field ACMSVNAuth 301 func (s *BPMH) ACMSVNAuthOffset() uint64 { 302 return s.BPMSVNOffset() + s.BPMSVNTotalSize() 303 } 304 305 // Reserved0Offset returns the offset in bytes of field Reserved0 306 func (s *BPMH) Reserved0Offset() uint64 { 307 return s.ACMSVNAuthOffset() + s.ACMSVNAuthTotalSize() 308 } 309 310 // NEMDataStackOffset returns the offset in bytes of field NEMDataStack 311 func (s *BPMH) NEMDataStackOffset() uint64 { 312 return s.Reserved0Offset() + s.Reserved0TotalSize() 313 } 314 315 // Size returns the total size of the BPMH. 316 func (s *BPMH) TotalSize() uint64 { 317 if s == nil { 318 return 0 319 } 320 321 var size uint64 322 size += s.StructInfoTotalSize() 323 size += s.HdrStructVersionTotalSize() 324 size += s.PMBPMVersionTotalSize() 325 size += s.BPMSVNTotalSize() 326 size += s.ACMSVNAuthTotalSize() 327 size += s.Reserved0TotalSize() 328 size += s.NEMDataStackTotalSize() 329 return size 330 } 331 332 // PrettyString returns the content of the structure in an easy-to-read format. 333 func (s *BPMH) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 334 var lines []string 335 if withHeader { 336 lines = append(lines, pretty.Header(depth, "BPMH", s)) 337 } 338 if s == nil { 339 return strings.Join(lines, "\n") 340 } 341 // ManifestFieldType is structInfo 342 lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...) 343 // ManifestFieldType is endValue 344 lines = append(lines, pretty.SubValue(depth+1, "Hdr Struct Version", "", &s.HdrStructVersion, opts...)...) 345 // ManifestFieldType is endValue 346 lines = append(lines, pretty.SubValue(depth+1, "PMBPM Version", "", &s.PMBPMVersion, opts...)...) 347 // ManifestFieldType is endValue 348 lines = append(lines, pretty.SubValue(depth+1, "BPM SVN", "", &s.BPMSVN, opts...)...) 349 // ManifestFieldType is endValue 350 lines = append(lines, pretty.SubValue(depth+1, "ACM SVN Auth", "", &s.ACMSVNAuth, opts...)...) 351 // ManifestFieldType is arrayStatic 352 lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...) 353 // ManifestFieldType is endValue 354 lines = append(lines, pretty.SubValue(depth+1, "NEM Data Stack", "", &s.NEMDataStack, opts...)...) 355 if depth < 2 { 356 lines = append(lines, "") 357 } 358 return strings.Join(lines, "\n") 359 } 360 361 // PrettyString returns the bits of the flags in an easy-to-read format. 362 func (v Size4K) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 363 var lines []string 364 if withHeader { 365 lines = append(lines, pretty.Header(depth, "Size 4 K", v)) 366 } 367 lines = append(lines, pretty.SubValue(depth+1, "In Bytes", "", v.InBytes(), opts...)...) 368 return strings.Join(lines, "\n") 369 } 370 371 // TotalSize returns the total size measured through binary.Size. 372 func (v Size4K) TotalSize() uint64 { 373 return uint64(binary.Size(v)) 374 } 375 376 // WriteTo writes the Size4K into 'w' in binary format. 377 func (v Size4K) WriteTo(w io.Writer) (int64, error) { 378 return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v) 379 } 380 381 // ReadFrom reads the Size4K from 'r' in binary format. 382 func (v Size4K) ReadFrom(r io.Reader) (int64, error) { 383 return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v) 384 }