github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntbootpolicy/pcd_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 // NewPCD returns a new instance of PCD with 34 // all default values set. 35 func NewPCD() *PCD { 36 s := &PCD{} 37 copy(s.StructInfo.ID[:], []byte(StructureIDPCD)) 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 *PCD) Validate() error { 46 47 return nil 48 } 49 50 // StructureIDPCD is the StructureID (in terms of 51 // the document #575623) of element 'PCD'. 52 const StructureIDPCD = "__PCDS__" 53 54 // GetStructInfo returns current value of StructInfo of the structure. 55 // 56 // StructInfo is a set of standard fields with presented in any element 57 // ("element" in terms of document #575623). 58 func (s *PCD) GetStructInfo() cbnt.StructInfo { 59 return s.StructInfo 60 } 61 62 // SetStructInfo sets new value of StructInfo to the structure. 63 // 64 // StructInfo is a set of standard fields with presented in any element 65 // ("element" in terms of document #575623). 66 func (s *PCD) SetStructInfo(newStructInfo cbnt.StructInfo) { 67 s.StructInfo = newStructInfo 68 } 69 70 // ReadFrom reads the PCD from 'r' in format defined in the document #575623. 71 func (s *PCD) ReadFrom(r io.Reader) (int64, error) { 72 var totalN int64 73 74 err := binary.Read(r, binary.LittleEndian, &s.StructInfo) 75 if err != nil { 76 return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err) 77 } 78 totalN += int64(binary.Size(s.StructInfo)) 79 80 n, err := s.ReadDataFrom(r) 81 if err != nil { 82 return totalN, fmt.Errorf("unable to read data: %w", err) 83 } 84 totalN += n 85 86 return totalN, nil 87 } 88 89 // ReadDataFrom reads the PCD from 'r' excluding StructInfo, 90 // in format defined in the document #575623. 91 func (s *PCD) ReadDataFrom(r io.Reader) (int64, error) { 92 totalN := int64(0) 93 94 // StructInfo (ManifestFieldType: structInfo) 95 { 96 // ReadDataFrom does not read Struct, use ReadFrom for that. 97 } 98 99 // Reserved0 (ManifestFieldType: arrayStatic) 100 { 101 n, err := 2, binary.Read(r, binary.LittleEndian, s.Reserved0[:]) 102 if err != nil { 103 return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err) 104 } 105 totalN += int64(n) 106 } 107 108 // Data (ManifestFieldType: arrayDynamic) 109 { 110 var size uint16 111 err := binary.Read(r, binary.LittleEndian, &size) 112 if err != nil { 113 return totalN, fmt.Errorf("unable to the read size of field 'Data': %w", err) 114 } 115 totalN += int64(binary.Size(size)) 116 s.Data = make([]byte, size) 117 n, err := len(s.Data), binary.Read(r, binary.LittleEndian, s.Data) 118 if err != nil { 119 return totalN, fmt.Errorf("unable to read field 'Data': %w", err) 120 } 121 totalN += int64(n) 122 } 123 124 return totalN, nil 125 } 126 127 // RehashRecursive calls Rehash (see below) recursively. 128 func (s *PCD) RehashRecursive() { 129 s.StructInfo.Rehash() 130 s.Rehash() 131 } 132 133 // Rehash sets values which are calculated automatically depending on the rest 134 // data. It is usually about the total size field of an element. 135 func (s *PCD) Rehash() { 136 s.Variable0 = 0 137 s.ElementSize = uint16(s.TotalSize()) 138 } 139 140 // WriteTo writes the PCD into 'w' in format defined in 141 // the document #575623. 142 func (s *PCD) WriteTo(w io.Writer) (int64, error) { 143 totalN := int64(0) 144 s.Rehash() 145 146 // StructInfo (ManifestFieldType: structInfo) 147 { 148 n, err := s.StructInfo.WriteTo(w) 149 if err != nil { 150 return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err) 151 } 152 totalN += int64(n) 153 } 154 155 // Reserved0 (ManifestFieldType: arrayStatic) 156 { 157 n, err := 2, binary.Write(w, binary.LittleEndian, s.Reserved0[:]) 158 if err != nil { 159 return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err) 160 } 161 totalN += int64(n) 162 } 163 164 // Data (ManifestFieldType: arrayDynamic) 165 { 166 size := uint16(len(s.Data)) 167 err := binary.Write(w, binary.LittleEndian, size) 168 if err != nil { 169 return totalN, fmt.Errorf("unable to write the size of field 'Data': %w", err) 170 } 171 totalN += int64(binary.Size(size)) 172 n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data) 173 if err != nil { 174 return totalN, fmt.Errorf("unable to write field 'Data': %w", err) 175 } 176 totalN += int64(n) 177 } 178 179 return totalN, nil 180 } 181 182 // StructInfoSize returns the size in bytes of the value of field StructInfo 183 func (s *PCD) StructInfoTotalSize() uint64 { 184 return s.StructInfo.TotalSize() 185 } 186 187 // Reserved0Size returns the size in bytes of the value of field Reserved0 188 func (s *PCD) Reserved0TotalSize() uint64 { 189 return 2 190 } 191 192 // DataSize returns the size in bytes of the value of field Data 193 func (s *PCD) DataTotalSize() uint64 { 194 size := uint64(binary.Size(uint16(0))) 195 size += uint64(len(s.Data)) 196 return size 197 } 198 199 // StructInfoOffset returns the offset in bytes of field StructInfo 200 func (s *PCD) StructInfoOffset() uint64 { 201 return 0 202 } 203 204 // Reserved0Offset returns the offset in bytes of field Reserved0 205 func (s *PCD) Reserved0Offset() uint64 { 206 return s.StructInfoOffset() + s.StructInfoTotalSize() 207 } 208 209 // DataOffset returns the offset in bytes of field Data 210 func (s *PCD) DataOffset() uint64 { 211 return s.Reserved0Offset() + s.Reserved0TotalSize() 212 } 213 214 // Size returns the total size of the PCD. 215 func (s *PCD) TotalSize() uint64 { 216 if s == nil { 217 return 0 218 } 219 220 var size uint64 221 size += s.StructInfoTotalSize() 222 size += s.Reserved0TotalSize() 223 size += s.DataTotalSize() 224 return size 225 } 226 227 // PrettyString returns the content of the structure in an easy-to-read format. 228 func (s *PCD) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 229 var lines []string 230 if withHeader { 231 lines = append(lines, pretty.Header(depth, "PCD", s)) 232 } 233 if s == nil { 234 return strings.Join(lines, "\n") 235 } 236 // ManifestFieldType is structInfo 237 lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...) 238 // ManifestFieldType is arrayStatic 239 lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...) 240 // ManifestFieldType is arrayDynamic 241 lines = append(lines, pretty.SubValue(depth+1, "Data", "", &s.Data, opts...)...) 242 if depth < 2 { 243 lines = append(lines, "") 244 } 245 return strings.Join(lines, "\n") 246 }