github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntbootpolicy/signature_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 github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntbootpolicy 10 11 package cbntbootpolicy 12 13 import ( 14 "encoding/binary" 15 "fmt" 16 "io" 17 "strings" 18 19 "github.com/linuxboot/fiano/pkg/intel/metadata/cbnt" 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 _ = cbnt.StructInfo{} 31 ) 32 33 // NewSignature returns a new instance of Signature with 34 // all default values set. 35 func NewSignature() *Signature { 36 s := &Signature{} 37 copy(s.StructInfo.ID[:], []byte(StructureIDSignature)) 38 s.StructInfo.Version = 0x20 39 // Recursively initializing a child structure: 40 s.KeySignature = *cbnt.NewKeySignature() 41 s.Rehash() 42 return s 43 } 44 45 // Validate (recursively) checks the structure if there are any unexpected 46 // values. It returns an error if so. 47 func (s *Signature) Validate() error { 48 // Recursively validating a child structure: 49 if err := s.KeySignature.Validate(); err != nil { 50 return fmt.Errorf("error on field 'KeySignature': %w", err) 51 } 52 53 return nil 54 } 55 56 // StructureIDSignature is the StructureID (in terms of 57 // the document #575623) of element 'Signature'. 58 const StructureIDSignature = "__PMSG__" 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 *Signature) GetStructInfo() cbnt.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 *Signature) SetStructInfo(newStructInfo cbnt.StructInfo) { 73 s.StructInfo = newStructInfo 74 } 75 76 // ReadFrom reads the Signature from 'r' in format defined in the document #575623. 77 func (s *Signature) 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 Signature from 'r' excluding StructInfo, 96 // in format defined in the document #575623. 97 func (s *Signature) 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 // KeySignature (ManifestFieldType: subStruct) 106 { 107 n, err := s.KeySignature.ReadFrom(r) 108 if err != nil { 109 return totalN, fmt.Errorf("unable to read field 'KeySignature': %w", err) 110 } 111 totalN += int64(n) 112 } 113 114 return totalN, nil 115 } 116 117 // RehashRecursive calls Rehash (see below) recursively. 118 func (s *Signature) RehashRecursive() { 119 s.StructInfo.Rehash() 120 s.KeySignature.Rehash() 121 s.Rehash() 122 } 123 124 // Rehash sets values which are calculated automatically depending on the rest 125 // data. It is usually about the total size field of an element. 126 func (s *Signature) Rehash() { 127 s.Variable0 = 0 128 s.ElementSize = 0 129 } 130 131 // WriteTo writes the Signature into 'w' in format defined in 132 // the document #575623. 133 func (s *Signature) WriteTo(w io.Writer) (int64, error) { 134 totalN := int64(0) 135 s.Rehash() 136 137 // StructInfo (ManifestFieldType: structInfo) 138 { 139 n, err := s.StructInfo.WriteTo(w) 140 if err != nil { 141 return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err) 142 } 143 totalN += int64(n) 144 } 145 146 // KeySignature (ManifestFieldType: subStruct) 147 { 148 n, err := s.KeySignature.WriteTo(w) 149 if err != nil { 150 return totalN, fmt.Errorf("unable to write field 'KeySignature': %w", err) 151 } 152 totalN += int64(n) 153 } 154 155 return totalN, nil 156 } 157 158 // StructInfoSize returns the size in bytes of the value of field StructInfo 159 func (s *Signature) StructInfoTotalSize() uint64 { 160 return s.StructInfo.TotalSize() 161 } 162 163 // KeySignatureSize returns the size in bytes of the value of field KeySignature 164 func (s *Signature) KeySignatureTotalSize() uint64 { 165 return s.KeySignature.TotalSize() 166 } 167 168 // StructInfoOffset returns the offset in bytes of field StructInfo 169 func (s *Signature) StructInfoOffset() uint64 { 170 return 0 171 } 172 173 // KeySignatureOffset returns the offset in bytes of field KeySignature 174 func (s *Signature) KeySignatureOffset() uint64 { 175 return s.StructInfoOffset() + s.StructInfoTotalSize() 176 } 177 178 // Size returns the total size of the Signature. 179 func (s *Signature) TotalSize() uint64 { 180 if s == nil { 181 return 0 182 } 183 184 var size uint64 185 size += s.StructInfoTotalSize() 186 size += s.KeySignatureTotalSize() 187 return size 188 } 189 190 // PrettyString returns the content of the structure in an easy-to-read format. 191 func (s *Signature) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 192 var lines []string 193 if withHeader { 194 lines = append(lines, pretty.Header(depth, "Signature", s)) 195 } 196 if s == nil { 197 return strings.Join(lines, "\n") 198 } 199 // ManifestFieldType is structInfo 200 lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...) 201 // ManifestFieldType is subStruct 202 lines = append(lines, pretty.SubValue(depth+1, "Key Signature", "", &s.KeySignature, opts...)...) 203 if depth < 2 { 204 lines = append(lines, "") 205 } 206 return strings.Join(lines, "\n") 207 }