github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/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 -package bg github.com/linuxboot/fiano/pkg/intel/metadata/bg 10 11 package bg 12 13 import ( 14 "encoding/binary" 15 "fmt" 16 "io" 17 "strings" 18 19 "github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty" 20 ) 21 22 var ( 23 // Just to avoid errors in "import" above in case if it wasn't used below 24 _ = binary.LittleEndian 25 _ = (fmt.Stringer)(nil) 26 _ = (io.Reader)(nil) 27 _ = pretty.Header 28 _ = strings.Join 29 ) 30 31 // NewSignature returns a new instance of Signature with 32 // all default values set. 33 func NewSignature() *Signature { 34 s := &Signature{} 35 // Set through tag "required": 36 s.Version = 0x10 37 s.Rehash() 38 return s 39 } 40 41 // Validate (recursively) checks the structure if there are any unexpected 42 // values. It returns an error if so. 43 func (s *Signature) Validate() error { 44 // See tag "require" 45 if s.Version != 0x10 { 46 return fmt.Errorf("field 'Version' expects value '0x10', but has %v", s.Version) 47 } 48 49 return nil 50 } 51 52 // ReadFrom reads the Signature from 'r' in format defined in the document #575623. 53 func (s *Signature) ReadFrom(r io.Reader) (int64, error) { 54 totalN := int64(0) 55 56 // SigScheme (ManifestFieldType: endValue) 57 { 58 n, err := 2, binary.Read(r, binary.LittleEndian, &s.SigScheme) 59 if err != nil { 60 return totalN, fmt.Errorf("unable to read field 'SigScheme': %w", err) 61 } 62 totalN += int64(n) 63 } 64 65 // Version (ManifestFieldType: endValue) 66 { 67 n, err := 1, binary.Read(r, binary.LittleEndian, &s.Version) 68 if err != nil { 69 return totalN, fmt.Errorf("unable to read field 'Version': %w", err) 70 } 71 totalN += int64(n) 72 } 73 74 // KeySize (ManifestFieldType: endValue) 75 { 76 n, err := 2, binary.Read(r, binary.LittleEndian, &s.KeySize) 77 if err != nil { 78 return totalN, fmt.Errorf("unable to read field 'KeySize': %w", err) 79 } 80 totalN += int64(n) 81 } 82 83 // HashAlg (ManifestFieldType: endValue) 84 { 85 n, err := 2, binary.Read(r, binary.LittleEndian, &s.HashAlg) 86 if err != nil { 87 return totalN, fmt.Errorf("unable to read field 'HashAlg': %w", err) 88 } 89 totalN += int64(n) 90 } 91 92 // Data (ManifestFieldType: arrayDynamic) 93 { 94 size := uint16(s.KeySize.InBytes()) 95 s.Data = make([]byte, size) 96 n, err := len(s.Data), binary.Read(r, binary.LittleEndian, s.Data) 97 if err != nil { 98 return totalN, fmt.Errorf("unable to read field 'Data': %w", err) 99 } 100 totalN += int64(n) 101 } 102 103 return totalN, nil 104 } 105 106 // RehashRecursive calls Rehash (see below) recursively. 107 func (s *Signature) RehashRecursive() { 108 s.Rehash() 109 } 110 111 // Rehash sets values which are calculated automatically depending on the rest 112 // data. It is usually about the total size field of an element. 113 func (s *Signature) Rehash() { 114 } 115 116 // WriteTo writes the Signature into 'w' in format defined in 117 // the document #575623. 118 func (s *Signature) WriteTo(w io.Writer) (int64, error) { 119 totalN := int64(0) 120 s.Rehash() 121 122 // SigScheme (ManifestFieldType: endValue) 123 { 124 n, err := 2, binary.Write(w, binary.LittleEndian, &s.SigScheme) 125 if err != nil { 126 return totalN, fmt.Errorf("unable to write field 'SigScheme': %w", err) 127 } 128 totalN += int64(n) 129 } 130 131 // Version (ManifestFieldType: endValue) 132 { 133 n, err := 1, binary.Write(w, binary.LittleEndian, &s.Version) 134 if err != nil { 135 return totalN, fmt.Errorf("unable to write field 'Version': %w", err) 136 } 137 totalN += int64(n) 138 } 139 140 // KeySize (ManifestFieldType: endValue) 141 { 142 n, err := 2, binary.Write(w, binary.LittleEndian, &s.KeySize) 143 if err != nil { 144 return totalN, fmt.Errorf("unable to write field 'KeySize': %w", err) 145 } 146 totalN += int64(n) 147 } 148 149 // HashAlg (ManifestFieldType: endValue) 150 { 151 n, err := 2, binary.Write(w, binary.LittleEndian, &s.HashAlg) 152 if err != nil { 153 return totalN, fmt.Errorf("unable to write field 'HashAlg': %w", err) 154 } 155 totalN += int64(n) 156 } 157 158 // Data (ManifestFieldType: arrayDynamic) 159 { 160 n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data) 161 if err != nil { 162 return totalN, fmt.Errorf("unable to write field 'Data': %w", err) 163 } 164 totalN += int64(n) 165 } 166 167 return totalN, nil 168 } 169 170 // SigSchemeSize returns the size in bytes of the value of field SigScheme 171 func (s *Signature) SigSchemeTotalSize() uint64 { 172 return 2 173 } 174 175 // VersionSize returns the size in bytes of the value of field Version 176 func (s *Signature) VersionTotalSize() uint64 { 177 return 1 178 } 179 180 // KeySizeSize returns the size in bytes of the value of field KeySize 181 func (s *Signature) KeySizeTotalSize() uint64 { 182 return 2 183 } 184 185 // HashAlgSize returns the size in bytes of the value of field HashAlg 186 func (s *Signature) HashAlgTotalSize() uint64 { 187 return 2 188 } 189 190 // DataSize returns the size in bytes of the value of field Data 191 func (s *Signature) DataTotalSize() uint64 { 192 return uint64(len(s.Data)) 193 } 194 195 // SigSchemeOffset returns the offset in bytes of field SigScheme 196 func (s *Signature) SigSchemeOffset() uint64 { 197 return 0 198 } 199 200 // VersionOffset returns the offset in bytes of field Version 201 func (s *Signature) VersionOffset() uint64 { 202 return s.SigSchemeOffset() + s.SigSchemeTotalSize() 203 } 204 205 // KeySizeOffset returns the offset in bytes of field KeySize 206 func (s *Signature) KeySizeOffset() uint64 { 207 return s.VersionOffset() + s.VersionTotalSize() 208 } 209 210 // HashAlgOffset returns the offset in bytes of field HashAlg 211 func (s *Signature) HashAlgOffset() uint64 { 212 return s.KeySizeOffset() + s.KeySizeTotalSize() 213 } 214 215 // DataOffset returns the offset in bytes of field Data 216 func (s *Signature) DataOffset() uint64 { 217 return s.HashAlgOffset() + s.HashAlgTotalSize() 218 } 219 220 // Size returns the total size of the Signature. 221 func (s *Signature) TotalSize() uint64 { 222 if s == nil { 223 return 0 224 } 225 226 var size uint64 227 size += s.SigSchemeTotalSize() 228 size += s.VersionTotalSize() 229 size += s.KeySizeTotalSize() 230 size += s.HashAlgTotalSize() 231 size += s.DataTotalSize() 232 return size 233 } 234 235 // PrettyString returns the content of the structure in an easy-to-read format. 236 func (s *Signature) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 237 var lines []string 238 if withHeader { 239 lines = append(lines, pretty.Header(depth, "Signature", s)) 240 } 241 if s == nil { 242 return strings.Join(lines, "\n") 243 } 244 // ManifestFieldType is endValue 245 lines = append(lines, pretty.SubValue(depth+1, "Sig Scheme", "", &s.SigScheme, opts...)...) 246 // ManifestFieldType is endValue 247 lines = append(lines, pretty.SubValue(depth+1, "Version", "", &s.Version, opts...)...) 248 // ManifestFieldType is endValue 249 lines = append(lines, pretty.SubValue(depth+1, "Key Size", "", &s.KeySize, opts...)...) 250 // ManifestFieldType is endValue 251 lines = append(lines, pretty.SubValue(depth+1, "Hash Alg", "", &s.HashAlg, opts...)...) 252 // ManifestFieldType is arrayDynamic 253 lines = append(lines, pretty.SubValue(depth+1, "Data", "", s.dataPrettyValue(), opts...)...) 254 if depth < 2 { 255 lines = append(lines, "") 256 } 257 return strings.Join(lines, "\n") 258 }