github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/tpm_info_list_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 10 11 package cbnt 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 // NewTPMInfoList returns a new instance of TPMInfoList with 32 // all default values set. 33 func NewTPMInfoList() *TPMInfoList { 34 s := &TPMInfoList{} 35 s.Rehash() 36 return s 37 } 38 39 // Validate (recursively) checks the structure if there are any unexpected 40 // values. It returns an error if so. 41 func (s *TPMInfoList) Validate() error { 42 43 return nil 44 } 45 46 // ReadFrom reads the TPMInfoList from 'r' in format defined in the document #575623. 47 func (s *TPMInfoList) ReadFrom(r io.Reader) (int64, error) { 48 totalN := int64(0) 49 50 // Capabilities (ManifestFieldType: endValue) 51 { 52 n, err := 4, binary.Read(r, binary.LittleEndian, &s.Capabilities) 53 if err != nil { 54 return totalN, fmt.Errorf("unable to read field 'Capabilities': %w", err) 55 } 56 totalN += int64(n) 57 } 58 59 // Algorithms (ManifestFieldType: list) 60 { 61 var count uint16 62 err := binary.Read(r, binary.LittleEndian, &count) 63 if err != nil { 64 return totalN, fmt.Errorf("unable to read the count for field 'Algorithms': %w", err) 65 } 66 totalN += int64(binary.Size(count)) 67 s.Algorithms = make([]Algorithm, count) 68 69 for idx := range s.Algorithms { 70 n, err := s.Algorithms[idx].ReadFrom(r) 71 if err != nil { 72 return totalN, fmt.Errorf("unable to read field 'Algorithms[%d]': %w", idx, err) 73 } 74 totalN += int64(n) 75 } 76 } 77 78 return totalN, nil 79 } 80 81 // RehashRecursive calls Rehash (see below) recursively. 82 func (s *TPMInfoList) RehashRecursive() { 83 s.Rehash() 84 } 85 86 // Rehash sets values which are calculated automatically depending on the rest 87 // data. It is usually about the total size field of an element. 88 func (s *TPMInfoList) Rehash() { 89 } 90 91 // WriteTo writes the TPMInfoList into 'w' in format defined in 92 // the document #575623. 93 func (s *TPMInfoList) WriteTo(w io.Writer) (int64, error) { 94 totalN := int64(0) 95 s.Rehash() 96 97 // Capabilities (ManifestFieldType: endValue) 98 { 99 n, err := 4, binary.Write(w, binary.LittleEndian, &s.Capabilities) 100 if err != nil { 101 return totalN, fmt.Errorf("unable to write field 'Capabilities': %w", err) 102 } 103 totalN += int64(n) 104 } 105 106 // Algorithms (ManifestFieldType: list) 107 { 108 count := uint16(len(s.Algorithms)) 109 err := binary.Write(w, binary.LittleEndian, &count) 110 if err != nil { 111 return totalN, fmt.Errorf("unable to write the count for field 'Algorithms': %w", err) 112 } 113 totalN += int64(binary.Size(count)) 114 for idx := range s.Algorithms { 115 n, err := s.Algorithms[idx].WriteTo(w) 116 if err != nil { 117 return totalN, fmt.Errorf("unable to write field 'Algorithms[%d]': %w", idx, err) 118 } 119 totalN += int64(n) 120 } 121 } 122 123 return totalN, nil 124 } 125 126 // CapabilitiesSize returns the size in bytes of the value of field Capabilities 127 func (s *TPMInfoList) CapabilitiesTotalSize() uint64 { 128 return 4 129 } 130 131 // AlgorithmsSize returns the size in bytes of the value of field Algorithms 132 func (s *TPMInfoList) AlgorithmsTotalSize() uint64 { 133 var size uint64 134 size += uint64(binary.Size(uint16(0))) 135 for idx := range s.Algorithms { 136 size += s.Algorithms[idx].TotalSize() 137 } 138 return size 139 } 140 141 // CapabilitiesOffset returns the offset in bytes of field Capabilities 142 func (s *TPMInfoList) CapabilitiesOffset() uint64 { 143 return 0 144 } 145 146 // AlgorithmsOffset returns the offset in bytes of field Algorithms 147 func (s *TPMInfoList) AlgorithmsOffset() uint64 { 148 return s.CapabilitiesOffset() + s.CapabilitiesTotalSize() 149 } 150 151 // Size returns the total size of the TPMInfoList. 152 func (s *TPMInfoList) TotalSize() uint64 { 153 if s == nil { 154 return 0 155 } 156 157 var size uint64 158 size += s.CapabilitiesTotalSize() 159 size += s.AlgorithmsTotalSize() 160 return size 161 } 162 163 // PrettyString returns the content of the structure in an easy-to-read format. 164 func (s *TPMInfoList) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 165 var lines []string 166 if withHeader { 167 lines = append(lines, pretty.Header(depth, "TPM Info List", s)) 168 } 169 if s == nil { 170 return strings.Join(lines, "\n") 171 } 172 // ManifestFieldType is endValue 173 lines = append(lines, pretty.SubValue(depth+1, "Capabilities", "", &s.Capabilities, opts...)...) 174 // ManifestFieldType is list 175 lines = append(lines, pretty.Header(depth+1, fmt.Sprintf("Algorithms: Array of \"TPM Info List\" of length %d", len(s.Algorithms)), s.Algorithms)) 176 for i := 0; i < len(s.Algorithms); i++ { 177 lines = append(lines, fmt.Sprintf("%sitem #%d: ", strings.Repeat(" ", int(depth+2)), i)+strings.TrimSpace(s.Algorithms[i].PrettyString(depth+2, true))) 178 } 179 if depth < 1 { 180 lines = append(lines, "") 181 } 182 if depth < 2 { 183 lines = append(lines, "") 184 } 185 return strings.Join(lines, "\n") 186 } 187 188 // PrettyString returns the bits of the flags in an easy-to-read format. 189 func (v TPM2PCRExtendPolicySupport) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 190 var lines []string 191 if withHeader { 192 lines = append(lines, pretty.Header(depth, "TPM 2 PCR Extend Policy Support", v)) 193 } 194 return strings.Join(lines, "\n") 195 } 196 197 // TotalSize returns the total size measured through binary.Size. 198 func (v TPM2PCRExtendPolicySupport) TotalSize() uint64 { 199 return uint64(binary.Size(v)) 200 } 201 202 // WriteTo writes the TPM2PCRExtendPolicySupport into 'w' in binary format. 203 func (v TPM2PCRExtendPolicySupport) WriteTo(w io.Writer) (int64, error) { 204 return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v) 205 } 206 207 // ReadFrom reads the TPM2PCRExtendPolicySupport from 'r' in binary format. 208 func (v TPM2PCRExtendPolicySupport) ReadFrom(r io.Reader) (int64, error) { 209 return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v) 210 } 211 212 // PrettyString returns the bits of the flags in an easy-to-read format. 213 func (v TPMCapabilities) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 214 var lines []string 215 if withHeader { 216 lines = append(lines, pretty.Header(depth, "TPM Capabilities", v)) 217 } 218 lines = append(lines, pretty.SubValue(depth+1, "TPM 2 PCR Extend Policy Support", "", v.TPM2PCRExtendPolicySupport(), opts...)...) 219 lines = append(lines, pretty.SubValue(depth+1, "TPM Family Support", "", v.TPMFamilySupport(), opts...)...) 220 return strings.Join(lines, "\n") 221 } 222 223 // TotalSize returns the total size measured through binary.Size. 224 func (v TPMCapabilities) TotalSize() uint64 { 225 return uint64(binary.Size(v)) 226 } 227 228 // WriteTo writes the TPMCapabilities into 'w' in binary format. 229 func (v TPMCapabilities) WriteTo(w io.Writer) (int64, error) { 230 return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v) 231 } 232 233 // ReadFrom reads the TPMCapabilities from 'r' in binary format. 234 func (v TPMCapabilities) ReadFrom(r io.Reader) (int64, error) { 235 return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v) 236 } 237 238 // PrettyString returns the bits of the flags in an easy-to-read format. 239 func (v TPMFamilySupport) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 240 var lines []string 241 if withHeader { 242 lines = append(lines, pretty.Header(depth, "TPM Family Support", v)) 243 } 244 if v.IsDiscreteTPM12Supported() { 245 lines = append(lines, pretty.SubValue(depth+1, "Is Discrete TPM 12 Supported", "Discrete TPM1.2 is supported", true, opts...)...) 246 } else { 247 lines = append(lines, pretty.SubValue(depth+1, "Is Discrete TPM 12 Supported", "Discrete TPM1.2 is not supported", false, opts...)...) 248 } 249 if v.IsDiscreteTPM20Supported() { 250 lines = append(lines, pretty.SubValue(depth+1, "Is Discrete TPM 20 Supported", "Discrete TPM2.0 is supported", true, opts...)...) 251 } else { 252 lines = append(lines, pretty.SubValue(depth+1, "Is Discrete TPM 20 Supported", "Discrete TPM2.0 is not supported", false, opts...)...) 253 } 254 if v.IsFirmwareTPM20Supported() { 255 lines = append(lines, pretty.SubValue(depth+1, "Is Firmware TPM 20 Supported", "Firmware TPM2.0 is supported", true, opts...)...) 256 } else { 257 lines = append(lines, pretty.SubValue(depth+1, "Is Firmware TPM 20 Supported", "Firmware TPM2.0 is not supported", false, opts...)...) 258 } 259 return strings.Join(lines, "\n") 260 } 261 262 // TotalSize returns the total size measured through binary.Size. 263 func (v TPMFamilySupport) TotalSize() uint64 { 264 return uint64(binary.Size(v)) 265 } 266 267 // WriteTo writes the TPMFamilySupport into 'w' in binary format. 268 func (v TPMFamilySupport) WriteTo(w io.Writer) (int64, error) { 269 return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v) 270 } 271 272 // ReadFrom reads the TPMFamilySupport from 'r' in binary format. 273 func (v TPMFamilySupport) ReadFrom(r io.Reader) (int64, error) { 274 return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v) 275 }