github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntbootpolicy/bpmh_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  // NewBPMH returns a new instance of BPMH with
    34  // all default values set.
    35  func NewBPMH() *BPMH {
    36  	s := &BPMH{}
    37  	copy(s.StructInfo.ID[:], []byte(StructureIDBPMH))
    38  	s.StructInfo.Version = 0x23
    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 *BPMH) 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  // StructureIDBPMH is the StructureID (in terms of
    57  // the document #575623) of element 'BPMH'.
    58  const StructureIDBPMH = "__ACBP__"
    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 *BPMH) 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 *BPMH) SetStructInfo(newStructInfo cbnt.StructInfo) {
    73  	s.StructInfo = newStructInfo
    74  }
    75  
    76  // ReadFrom reads the BPMH from 'r' in format defined in the document #575623.
    77  func (s *BPMH) 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 BPMH from 'r' excluding StructInfo,
    96  // in format defined in the document #575623.
    97  func (s *BPMH) 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  	// KeySignatureOffset (ManifestFieldType: endValue)
   106  	{
   107  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.KeySignatureOffset)
   108  		if err != nil {
   109  			return totalN, fmt.Errorf("unable to read field 'KeySignatureOffset': %w", err)
   110  		}
   111  		totalN += int64(n)
   112  	}
   113  
   114  	// BPMRevision (ManifestFieldType: endValue)
   115  	{
   116  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.BPMRevision)
   117  		if err != nil {
   118  			return totalN, fmt.Errorf("unable to read field 'BPMRevision': %w", err)
   119  		}
   120  		totalN += int64(n)
   121  	}
   122  
   123  	// BPMSVN (ManifestFieldType: endValue)
   124  	{
   125  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.BPMSVN)
   126  		if err != nil {
   127  			return totalN, fmt.Errorf("unable to read field 'BPMSVN': %w", err)
   128  		}
   129  		totalN += int64(n)
   130  	}
   131  
   132  	// ACMSVNAuth (ManifestFieldType: endValue)
   133  	{
   134  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.ACMSVNAuth)
   135  		if err != nil {
   136  			return totalN, fmt.Errorf("unable to read field 'ACMSVNAuth': %w", err)
   137  		}
   138  		totalN += int64(n)
   139  	}
   140  
   141  	// Reserved0 (ManifestFieldType: arrayStatic)
   142  	{
   143  		n, err := 1, binary.Read(r, binary.LittleEndian, s.Reserved0[:])
   144  		if err != nil {
   145  			return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err)
   146  		}
   147  		totalN += int64(n)
   148  	}
   149  
   150  	// NEMDataStack (ManifestFieldType: endValue)
   151  	{
   152  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.NEMDataStack)
   153  		if err != nil {
   154  			return totalN, fmt.Errorf("unable to read field 'NEMDataStack': %w", err)
   155  		}
   156  		totalN += int64(n)
   157  	}
   158  
   159  	return totalN, nil
   160  }
   161  
   162  // RehashRecursive calls Rehash (see below) recursively.
   163  func (s *BPMH) RehashRecursive() {
   164  	s.StructInfo.Rehash()
   165  	s.Rehash()
   166  }
   167  
   168  // Rehash sets values which are calculated automatically depending on the rest
   169  // data. It is usually about the total size field of an element.
   170  func (s *BPMH) Rehash() {
   171  	s.Variable0 = 0x20
   172  	s.ElementSize = uint16(s.TotalSize())
   173  }
   174  
   175  // WriteTo writes the BPMH into 'w' in format defined in
   176  // the document #575623.
   177  func (s *BPMH) WriteTo(w io.Writer) (int64, error) {
   178  	totalN := int64(0)
   179  	s.Rehash()
   180  
   181  	// StructInfo (ManifestFieldType: structInfo)
   182  	{
   183  		n, err := s.StructInfo.WriteTo(w)
   184  		if err != nil {
   185  			return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
   186  		}
   187  		totalN += int64(n)
   188  	}
   189  
   190  	// KeySignatureOffset (ManifestFieldType: endValue)
   191  	{
   192  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.KeySignatureOffset)
   193  		if err != nil {
   194  			return totalN, fmt.Errorf("unable to write field 'KeySignatureOffset': %w", err)
   195  		}
   196  		totalN += int64(n)
   197  	}
   198  
   199  	// BPMRevision (ManifestFieldType: endValue)
   200  	{
   201  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.BPMRevision)
   202  		if err != nil {
   203  			return totalN, fmt.Errorf("unable to write field 'BPMRevision': %w", err)
   204  		}
   205  		totalN += int64(n)
   206  	}
   207  
   208  	// BPMSVN (ManifestFieldType: endValue)
   209  	{
   210  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.BPMSVN)
   211  		if err != nil {
   212  			return totalN, fmt.Errorf("unable to write field 'BPMSVN': %w", err)
   213  		}
   214  		totalN += int64(n)
   215  	}
   216  
   217  	// ACMSVNAuth (ManifestFieldType: endValue)
   218  	{
   219  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.ACMSVNAuth)
   220  		if err != nil {
   221  			return totalN, fmt.Errorf("unable to write field 'ACMSVNAuth': %w", err)
   222  		}
   223  		totalN += int64(n)
   224  	}
   225  
   226  	// Reserved0 (ManifestFieldType: arrayStatic)
   227  	{
   228  		n, err := 1, binary.Write(w, binary.LittleEndian, s.Reserved0[:])
   229  		if err != nil {
   230  			return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err)
   231  		}
   232  		totalN += int64(n)
   233  	}
   234  
   235  	// NEMDataStack (ManifestFieldType: endValue)
   236  	{
   237  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.NEMDataStack)
   238  		if err != nil {
   239  			return totalN, fmt.Errorf("unable to write field 'NEMDataStack': %w", err)
   240  		}
   241  		totalN += int64(n)
   242  	}
   243  
   244  	return totalN, nil
   245  }
   246  
   247  // StructInfoSize returns the size in bytes of the value of field StructInfo
   248  func (s *BPMH) StructInfoTotalSize() uint64 {
   249  	return s.StructInfo.TotalSize()
   250  }
   251  
   252  // KeySignatureOffsetSize returns the size in bytes of the value of field KeySignatureOffset
   253  func (s *BPMH) KeySignatureOffsetTotalSize() uint64 {
   254  	return 2
   255  }
   256  
   257  // BPMRevisionSize returns the size in bytes of the value of field BPMRevision
   258  func (s *BPMH) BPMRevisionTotalSize() uint64 {
   259  	return 1
   260  }
   261  
   262  // BPMSVNSize returns the size in bytes of the value of field BPMSVN
   263  func (s *BPMH) BPMSVNTotalSize() uint64 {
   264  	return 1
   265  }
   266  
   267  // ACMSVNAuthSize returns the size in bytes of the value of field ACMSVNAuth
   268  func (s *BPMH) ACMSVNAuthTotalSize() uint64 {
   269  	return 1
   270  }
   271  
   272  // Reserved0Size returns the size in bytes of the value of field Reserved0
   273  func (s *BPMH) Reserved0TotalSize() uint64 {
   274  	return 1
   275  }
   276  
   277  // NEMDataStackSize returns the size in bytes of the value of field NEMDataStack
   278  func (s *BPMH) NEMDataStackTotalSize() uint64 {
   279  	return 2
   280  }
   281  
   282  // StructInfoOffset returns the offset in bytes of field StructInfo
   283  func (s *BPMH) StructInfoOffset() uint64 {
   284  	return 0
   285  }
   286  
   287  // KeySignatureOffsetOffset returns the offset in bytes of field KeySignatureOffset
   288  func (s *BPMH) KeySignatureOffsetOffset() uint64 {
   289  	return s.StructInfoOffset() + s.StructInfoTotalSize()
   290  }
   291  
   292  // BPMRevisionOffset returns the offset in bytes of field BPMRevision
   293  func (s *BPMH) BPMRevisionOffset() uint64 {
   294  	return s.KeySignatureOffsetOffset() + s.KeySignatureOffsetTotalSize()
   295  }
   296  
   297  // BPMSVNOffset returns the offset in bytes of field BPMSVN
   298  func (s *BPMH) BPMSVNOffset() uint64 {
   299  	return s.BPMRevisionOffset() + s.BPMRevisionTotalSize()
   300  }
   301  
   302  // ACMSVNAuthOffset returns the offset in bytes of field ACMSVNAuth
   303  func (s *BPMH) ACMSVNAuthOffset() uint64 {
   304  	return s.BPMSVNOffset() + s.BPMSVNTotalSize()
   305  }
   306  
   307  // Reserved0Offset returns the offset in bytes of field Reserved0
   308  func (s *BPMH) Reserved0Offset() uint64 {
   309  	return s.ACMSVNAuthOffset() + s.ACMSVNAuthTotalSize()
   310  }
   311  
   312  // NEMDataStackOffset returns the offset in bytes of field NEMDataStack
   313  func (s *BPMH) NEMDataStackOffset() uint64 {
   314  	return s.Reserved0Offset() + s.Reserved0TotalSize()
   315  }
   316  
   317  // Size returns the total size of the BPMH.
   318  func (s *BPMH) TotalSize() uint64 {
   319  	if s == nil {
   320  		return 0
   321  	}
   322  
   323  	var size uint64
   324  	size += s.StructInfoTotalSize()
   325  	size += s.KeySignatureOffsetTotalSize()
   326  	size += s.BPMRevisionTotalSize()
   327  	size += s.BPMSVNTotalSize()
   328  	size += s.ACMSVNAuthTotalSize()
   329  	size += s.Reserved0TotalSize()
   330  	size += s.NEMDataStackTotalSize()
   331  	return size
   332  }
   333  
   334  // PrettyString returns the content of the structure in an easy-to-read format.
   335  func (s *BPMH) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   336  	var lines []string
   337  	if withHeader {
   338  		lines = append(lines, pretty.Header(depth, "BPMH", s))
   339  	}
   340  	if s == nil {
   341  		return strings.Join(lines, "\n")
   342  	}
   343  	// ManifestFieldType is structInfo
   344  	lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
   345  	// ManifestFieldType is endValue
   346  	lines = append(lines, pretty.SubValue(depth+1, "Key Signature Offset", "", &s.KeySignatureOffset, opts...)...)
   347  	// ManifestFieldType is endValue
   348  	lines = append(lines, pretty.SubValue(depth+1, "BPM Revision", "", &s.BPMRevision, opts...)...)
   349  	// ManifestFieldType is endValue
   350  	lines = append(lines, pretty.SubValue(depth+1, "BPM SVN", "", &s.BPMSVN, opts...)...)
   351  	// ManifestFieldType is endValue
   352  	lines = append(lines, pretty.SubValue(depth+1, "ACM SVN Auth", "", &s.ACMSVNAuth, opts...)...)
   353  	// ManifestFieldType is arrayStatic
   354  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...)
   355  	// ManifestFieldType is endValue
   356  	lines = append(lines, pretty.SubValue(depth+1, "NEM Data Stack", "", &s.NEMDataStack, opts...)...)
   357  	if depth < 2 {
   358  		lines = append(lines, "")
   359  	}
   360  	return strings.Join(lines, "\n")
   361  }
   362  
   363  // PrettyString returns the bits of the flags in an easy-to-read format.
   364  func (v Size4K) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   365  	var lines []string
   366  	if withHeader {
   367  		lines = append(lines, pretty.Header(depth, "Size 4 K", v))
   368  	}
   369  	lines = append(lines, pretty.SubValue(depth+1, "In Bytes", "", v.InBytes(), opts...)...)
   370  	return strings.Join(lines, "\n")
   371  }
   372  
   373  // TotalSize returns the total size measured through binary.Size.
   374  func (v Size4K) TotalSize() uint64 {
   375  	return uint64(binary.Size(v))
   376  }
   377  
   378  // WriteTo writes the Size4K into 'w' in binary format.
   379  func (v Size4K) WriteTo(w io.Writer) (int64, error) {
   380  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
   381  }
   382  
   383  // ReadFrom reads the Size4K from 'r' in binary format.
   384  func (v Size4K) ReadFrom(r io.Reader) (int64, error) {
   385  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
   386  }