github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntbootpolicy/pm_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 // NewPM returns a new instance of PM with 34 // all default values set. 35 func NewPM() *PM { 36 s := &PM{} 37 copy(s.StructInfo.ID[:], []byte(StructureIDPM)) 38 s.StructInfo.Version = 0x20 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 *PM) 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 // StructureIDPM is the StructureID (in terms of 57 // the document #575623) of element 'PM'. 58 const StructureIDPM = "__PMDA__" 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 *PM) 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 *PM) SetStructInfo(newStructInfo cbnt.StructInfo) { 73 s.StructInfo = newStructInfo 74 } 75 76 // ReadFrom reads the PM from 'r' in format defined in the document #575623. 77 func (s *PM) 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 PM from 'r' excluding StructInfo, 96 // in format defined in the document #575623. 97 func (s *PM) 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 // Reserved0 (ManifestFieldType: arrayStatic) 106 { 107 n, err := 2, binary.Read(r, binary.LittleEndian, s.Reserved0[:]) 108 if err != nil { 109 return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err) 110 } 111 totalN += int64(n) 112 } 113 114 // Data (ManifestFieldType: arrayDynamic) 115 { 116 var size uint16 117 err := binary.Read(r, binary.LittleEndian, &size) 118 if err != nil { 119 return totalN, fmt.Errorf("unable to the read size of field 'Data': %w", err) 120 } 121 totalN += int64(binary.Size(size)) 122 s.Data = make([]byte, size) 123 n, err := len(s.Data), binary.Read(r, binary.LittleEndian, s.Data) 124 if err != nil { 125 return totalN, fmt.Errorf("unable to read field 'Data': %w", err) 126 } 127 totalN += int64(n) 128 } 129 130 return totalN, nil 131 } 132 133 // RehashRecursive calls Rehash (see below) recursively. 134 func (s *PM) RehashRecursive() { 135 s.StructInfo.Rehash() 136 s.Rehash() 137 } 138 139 // Rehash sets values which are calculated automatically depending on the rest 140 // data. It is usually about the total size field of an element. 141 func (s *PM) Rehash() { 142 s.Variable0 = 0 143 s.ElementSize = uint16(s.TotalSize()) 144 } 145 146 // WriteTo writes the PM into 'w' in format defined in 147 // the document #575623. 148 func (s *PM) WriteTo(w io.Writer) (int64, error) { 149 totalN := int64(0) 150 s.Rehash() 151 152 // StructInfo (ManifestFieldType: structInfo) 153 { 154 n, err := s.StructInfo.WriteTo(w) 155 if err != nil { 156 return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err) 157 } 158 totalN += int64(n) 159 } 160 161 // Reserved0 (ManifestFieldType: arrayStatic) 162 { 163 n, err := 2, binary.Write(w, binary.LittleEndian, s.Reserved0[:]) 164 if err != nil { 165 return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err) 166 } 167 totalN += int64(n) 168 } 169 170 // Data (ManifestFieldType: arrayDynamic) 171 { 172 size := uint16(len(s.Data)) 173 err := binary.Write(w, binary.LittleEndian, size) 174 if err != nil { 175 return totalN, fmt.Errorf("unable to write the size of field 'Data': %w", err) 176 } 177 totalN += int64(binary.Size(size)) 178 n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data) 179 if err != nil { 180 return totalN, fmt.Errorf("unable to write field 'Data': %w", err) 181 } 182 totalN += int64(n) 183 } 184 185 return totalN, nil 186 } 187 188 // StructInfoSize returns the size in bytes of the value of field StructInfo 189 func (s *PM) StructInfoTotalSize() uint64 { 190 return s.StructInfo.TotalSize() 191 } 192 193 // Reserved0Size returns the size in bytes of the value of field Reserved0 194 func (s *PM) Reserved0TotalSize() uint64 { 195 return 2 196 } 197 198 // DataSize returns the size in bytes of the value of field Data 199 func (s *PM) DataTotalSize() uint64 { 200 size := uint64(binary.Size(uint16(0))) 201 size += uint64(len(s.Data)) 202 return size 203 } 204 205 // StructInfoOffset returns the offset in bytes of field StructInfo 206 func (s *PM) StructInfoOffset() uint64 { 207 return 0 208 } 209 210 // Reserved0Offset returns the offset in bytes of field Reserved0 211 func (s *PM) Reserved0Offset() uint64 { 212 return s.StructInfoOffset() + s.StructInfoTotalSize() 213 } 214 215 // DataOffset returns the offset in bytes of field Data 216 func (s *PM) DataOffset() uint64 { 217 return s.Reserved0Offset() + s.Reserved0TotalSize() 218 } 219 220 // Size returns the total size of the PM. 221 func (s *PM) TotalSize() uint64 { 222 if s == nil { 223 return 0 224 } 225 226 var size uint64 227 size += s.StructInfoTotalSize() 228 size += s.Reserved0TotalSize() 229 size += s.DataTotalSize() 230 return size 231 } 232 233 // PrettyString returns the content of the structure in an easy-to-read format. 234 func (s *PM) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 235 var lines []string 236 if withHeader { 237 lines = append(lines, pretty.Header(depth, "PM", s)) 238 } 239 if s == nil { 240 return strings.Join(lines, "\n") 241 } 242 // ManifestFieldType is structInfo 243 lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...) 244 // ManifestFieldType is arrayStatic 245 lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...) 246 // ManifestFieldType is arrayDynamic 247 lines = append(lines, pretty.SubValue(depth+1, "Data", "", &s.Data, opts...)...) 248 if depth < 2 { 249 lines = append(lines, "") 250 } 251 return strings.Join(lines, "\n") 252 }