github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/bgkey/manifest_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/bgkey 10 11 package bgkey 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 // NewManifest returns a new instance of Manifest with 34 // all default values set. 35 func NewManifest() *Manifest { 36 s := &Manifest{} 37 copy(s.StructInfo.ID[:], []byte(StructureIDManifest)) 38 s.StructInfo.Version = 0x10 39 // Recursively initializing a child structure: 40 s.BPKey = *bg.NewHashStructure() 41 // Recursively initializing a child structure: 42 s.KeyAndSignature = *bg.NewKeySignature() 43 s.Rehash() 44 return s 45 } 46 47 // Validate (recursively) checks the structure if there are any unexpected 48 // values. It returns an error if so. 49 func (s *Manifest) Validate() error { 50 // Recursively validating a child structure: 51 if err := s.BPKey.Validate(); err != nil { 52 return fmt.Errorf("error on field 'BPKey': %w", err) 53 } 54 // Recursively validating a child structure: 55 if err := s.KeyAndSignature.Validate(); err != nil { 56 return fmt.Errorf("error on field 'KeyAndSignature': %w", err) 57 } 58 59 return nil 60 } 61 62 // StructureIDManifest is the StructureID (in terms of 63 // the document #575623) of element 'Manifest'. 64 const StructureIDManifest = "__KEYM__" 65 66 // GetStructInfo returns current value of StructInfo of the structure. 67 // 68 // StructInfo is a set of standard fields with presented in any element 69 // ("element" in terms of document #575623). 70 func (s *Manifest) GetStructInfo() bg.StructInfo { 71 return s.StructInfo 72 } 73 74 // SetStructInfo sets new value of StructInfo to the structure. 75 // 76 // StructInfo is a set of standard fields with presented in any element 77 // ("element" in terms of document #575623). 78 func (s *Manifest) SetStructInfo(newStructInfo bg.StructInfo) { 79 s.StructInfo = newStructInfo 80 } 81 82 // ReadFrom reads the Manifest from 'r' in format defined in the document #575623. 83 func (s *Manifest) ReadFrom(r io.Reader) (int64, error) { 84 var totalN int64 85 86 err := binary.Read(r, binary.LittleEndian, &s.StructInfo) 87 if err != nil { 88 return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err) 89 } 90 totalN += int64(binary.Size(s.StructInfo)) 91 92 n, err := s.ReadDataFrom(r) 93 if err != nil { 94 return totalN, fmt.Errorf("unable to read data: %w", err) 95 } 96 totalN += n 97 98 return totalN, nil 99 } 100 101 // ReadDataFrom reads the Manifest from 'r' excluding StructInfo, 102 // in format defined in the document #575623. 103 func (s *Manifest) ReadDataFrom(r io.Reader) (int64, error) { 104 totalN := int64(0) 105 106 // StructInfo (ManifestFieldType: structInfo) 107 { 108 // ReadDataFrom does not read Struct, use ReadFrom for that. 109 } 110 111 // KMVersion (ManifestFieldType: endValue) 112 { 113 n, err := 1, binary.Read(r, binary.LittleEndian, &s.KMVersion) 114 if err != nil { 115 return totalN, fmt.Errorf("unable to read field 'KMVersion': %w", err) 116 } 117 totalN += int64(n) 118 } 119 120 // KMSVN (ManifestFieldType: endValue) 121 { 122 n, err := 1, binary.Read(r, binary.LittleEndian, &s.KMSVN) 123 if err != nil { 124 return totalN, fmt.Errorf("unable to read field 'KMSVN': %w", err) 125 } 126 totalN += int64(n) 127 } 128 129 // KMID (ManifestFieldType: endValue) 130 { 131 n, err := 1, binary.Read(r, binary.LittleEndian, &s.KMID) 132 if err != nil { 133 return totalN, fmt.Errorf("unable to read field 'KMID': %w", err) 134 } 135 totalN += int64(n) 136 } 137 138 // BPKey (ManifestFieldType: subStruct) 139 { 140 n, err := s.BPKey.ReadFrom(r) 141 if err != nil { 142 return totalN, fmt.Errorf("unable to read field 'BPKey': %w", err) 143 } 144 totalN += int64(n) 145 } 146 147 // KeyAndSignature (ManifestFieldType: subStruct) 148 { 149 n, err := s.KeyAndSignature.ReadFrom(r) 150 if err != nil { 151 return totalN, fmt.Errorf("unable to read field 'KeyAndSignature': %w", err) 152 } 153 totalN += int64(n) 154 } 155 156 return totalN, nil 157 } 158 159 // RehashRecursive calls Rehash (see below) recursively. 160 func (s *Manifest) RehashRecursive() { 161 s.StructInfo.Rehash() 162 s.BPKey.Rehash() 163 s.KeyAndSignature.Rehash() 164 s.Rehash() 165 } 166 167 // Rehash sets values which are calculated automatically depending on the rest 168 // data. It is usually about the total size field of an element. 169 func (s *Manifest) Rehash() { 170 } 171 172 // WriteTo writes the Manifest into 'w' in format defined in 173 // the document #575623. 174 func (s *Manifest) WriteTo(w io.Writer) (int64, error) { 175 totalN := int64(0) 176 s.Rehash() 177 178 // StructInfo (ManifestFieldType: structInfo) 179 { 180 n, err := s.StructInfo.WriteTo(w) 181 if err != nil { 182 return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err) 183 } 184 totalN += int64(n) 185 } 186 187 // KMVersion (ManifestFieldType: endValue) 188 { 189 n, err := 1, binary.Write(w, binary.LittleEndian, &s.KMVersion) 190 if err != nil { 191 return totalN, fmt.Errorf("unable to write field 'KMVersion': %w", err) 192 } 193 totalN += int64(n) 194 } 195 196 // KMSVN (ManifestFieldType: endValue) 197 { 198 n, err := 1, binary.Write(w, binary.LittleEndian, &s.KMSVN) 199 if err != nil { 200 return totalN, fmt.Errorf("unable to write field 'KMSVN': %w", err) 201 } 202 totalN += int64(n) 203 } 204 205 // KMID (ManifestFieldType: endValue) 206 { 207 n, err := 1, binary.Write(w, binary.LittleEndian, &s.KMID) 208 if err != nil { 209 return totalN, fmt.Errorf("unable to write field 'KMID': %w", err) 210 } 211 totalN += int64(n) 212 } 213 214 // BPKey (ManifestFieldType: subStruct) 215 { 216 n, err := s.BPKey.WriteTo(w) 217 if err != nil { 218 return totalN, fmt.Errorf("unable to write field 'BPKey': %w", err) 219 } 220 totalN += int64(n) 221 } 222 223 // KeyAndSignature (ManifestFieldType: subStruct) 224 { 225 n, err := s.KeyAndSignature.WriteTo(w) 226 if err != nil { 227 return totalN, fmt.Errorf("unable to write field 'KeyAndSignature': %w", err) 228 } 229 totalN += int64(n) 230 } 231 232 return totalN, nil 233 } 234 235 // StructInfoSize returns the size in bytes of the value of field StructInfo 236 func (s *Manifest) StructInfoTotalSize() uint64 { 237 return s.StructInfo.TotalSize() 238 } 239 240 // KMVersionSize returns the size in bytes of the value of field KMVersion 241 func (s *Manifest) KMVersionTotalSize() uint64 { 242 return 1 243 } 244 245 // KMSVNSize returns the size in bytes of the value of field KMSVN 246 func (s *Manifest) KMSVNTotalSize() uint64 { 247 return 1 248 } 249 250 // KMIDSize returns the size in bytes of the value of field KMID 251 func (s *Manifest) KMIDTotalSize() uint64 { 252 return 1 253 } 254 255 // BPKeySize returns the size in bytes of the value of field BPKey 256 func (s *Manifest) BPKeyTotalSize() uint64 { 257 return s.BPKey.TotalSize() 258 } 259 260 // KeyAndSignatureSize returns the size in bytes of the value of field KeyAndSignature 261 func (s *Manifest) KeyAndSignatureTotalSize() uint64 { 262 return s.KeyAndSignature.TotalSize() 263 } 264 265 // StructInfoOffset returns the offset in bytes of field StructInfo 266 func (s *Manifest) StructInfoOffset() uint64 { 267 return 0 268 } 269 270 // KMVersionOffset returns the offset in bytes of field KMVersion 271 func (s *Manifest) KMVersionOffset() uint64 { 272 return s.StructInfoOffset() + s.StructInfoTotalSize() 273 } 274 275 // KMSVNOffset returns the offset in bytes of field KMSVN 276 func (s *Manifest) KMSVNOffset() uint64 { 277 return s.KMVersionOffset() + s.KMVersionTotalSize() 278 } 279 280 // KMIDOffset returns the offset in bytes of field KMID 281 func (s *Manifest) KMIDOffset() uint64 { 282 return s.KMSVNOffset() + s.KMSVNTotalSize() 283 } 284 285 // BPKeyOffset returns the offset in bytes of field BPKey 286 func (s *Manifest) BPKeyOffset() uint64 { 287 return s.KMIDOffset() + s.KMIDTotalSize() 288 } 289 290 // KeyAndSignatureOffset returns the offset in bytes of field KeyAndSignature 291 func (s *Manifest) KeyAndSignatureOffset() uint64 { 292 return s.BPKeyOffset() + s.BPKeyTotalSize() 293 } 294 295 // Size returns the total size of the Manifest. 296 func (s *Manifest) TotalSize() uint64 { 297 if s == nil { 298 return 0 299 } 300 301 var size uint64 302 size += s.StructInfoTotalSize() 303 size += s.KMVersionTotalSize() 304 size += s.KMSVNTotalSize() 305 size += s.KMIDTotalSize() 306 size += s.BPKeyTotalSize() 307 size += s.KeyAndSignatureTotalSize() 308 return size 309 } 310 311 // PrettyString returns the content of the structure in an easy-to-read format. 312 func (s *Manifest) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 313 var lines []string 314 if withHeader { 315 lines = append(lines, pretty.Header(depth, "BG Key Manifest", s)) 316 } 317 if s == nil { 318 return strings.Join(lines, "\n") 319 } 320 // ManifestFieldType is structInfo 321 lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...) 322 // ManifestFieldType is endValue 323 lines = append(lines, pretty.SubValue(depth+1, "KM Version", "", &s.KMVersion, opts...)...) 324 // ManifestFieldType is endValue 325 lines = append(lines, pretty.SubValue(depth+1, "KMSVN", "", &s.KMSVN, opts...)...) 326 // ManifestFieldType is endValue 327 lines = append(lines, pretty.SubValue(depth+1, "KMID", "", &s.KMID, opts...)...) 328 // ManifestFieldType is subStruct 329 lines = append(lines, pretty.SubValue(depth+1, "BP Key", "", &s.BPKey, opts...)...) 330 // ManifestFieldType is subStruct 331 lines = append(lines, pretty.SubValue(depth+1, "Key And Signature", "", &s.KeyAndSignature, opts...)...) 332 if depth < 2 { 333 lines = append(lines, "") 334 } 335 return strings.Join(lines, "\n") 336 }