github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/bgbootpolicy/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/bgbootpolicy
    10  
    11  package bgbootpolicy
    12  
    13  import (
    14  	"encoding/binary"
    15  	"fmt"
    16  	"io"
    17  	"strings"
    18  
    19  	"github.com/linuxboot/fiano/pkg/intel/metadata/bg"
    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  	_ = bg.StructInfo{}
    31  )
    32  
    33  // NewSignature returns a new instance of Signature with
    34  // all default values set.
    35  func NewSignature() *Signature {
    36  	s := &Signature{}
    37  	copy(s.StructInfo.ID[:], []byte(StructureIDSignature))
    38  	s.StructInfo.Version = 0x10
    39  	// Recursively initializing a child structure:
    40  	s.KeySignature = *bg.NewKeySignature()
    41  	s.Rehash()
    42  	return s
    43  }
    44  
    45  // Validate (recursively) checks the structure if there are any unexpected
    46  // values. It returns an error if so.
    47  func (s *Signature) Validate() error {
    48  	// Recursively validating a child structure:
    49  	if err := s.KeySignature.Validate(); err != nil {
    50  		return fmt.Errorf("error on field 'KeySignature': %w", err)
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // StructureIDSignature is the StructureID (in terms of
    57  // the document #575623) of element 'Signature'.
    58  const StructureIDSignature = "__PMSG__"
    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 *Signature) GetStructInfo() bg.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 *Signature) SetStructInfo(newStructInfo bg.StructInfo) {
    73  	s.StructInfo = newStructInfo
    74  }
    75  
    76  // ReadFrom reads the Signature from 'r' in format defined in the document #575623.
    77  func (s *Signature) 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 Signature from 'r' excluding StructInfo,
    96  // in format defined in the document #575623.
    97  func (s *Signature) 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  	// KeySignature (ManifestFieldType: subStruct)
   106  	{
   107  		n, err := s.KeySignature.ReadFrom(r)
   108  		if err != nil {
   109  			return totalN, fmt.Errorf("unable to read field 'KeySignature': %w", err)
   110  		}
   111  		totalN += int64(n)
   112  	}
   113  
   114  	return totalN, nil
   115  }
   116  
   117  // RehashRecursive calls Rehash (see below) recursively.
   118  func (s *Signature) RehashRecursive() {
   119  	s.StructInfo.Rehash()
   120  	s.KeySignature.Rehash()
   121  	s.Rehash()
   122  }
   123  
   124  // Rehash sets values which are calculated automatically depending on the rest
   125  // data. It is usually about the total size field of an element.
   126  func (s *Signature) Rehash() {
   127  }
   128  
   129  // WriteTo writes the Signature into 'w' in format defined in
   130  // the document #575623.
   131  func (s *Signature) WriteTo(w io.Writer) (int64, error) {
   132  	totalN := int64(0)
   133  	s.Rehash()
   134  
   135  	// StructInfo (ManifestFieldType: structInfo)
   136  	{
   137  		n, err := s.StructInfo.WriteTo(w)
   138  		if err != nil {
   139  			return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
   140  		}
   141  		totalN += int64(n)
   142  	}
   143  
   144  	// KeySignature (ManifestFieldType: subStruct)
   145  	{
   146  		n, err := s.KeySignature.WriteTo(w)
   147  		if err != nil {
   148  			return totalN, fmt.Errorf("unable to write field 'KeySignature': %w", err)
   149  		}
   150  		totalN += int64(n)
   151  	}
   152  
   153  	return totalN, nil
   154  }
   155  
   156  // StructInfoSize returns the size in bytes of the value of field StructInfo
   157  func (s *Signature) StructInfoTotalSize() uint64 {
   158  	return s.StructInfo.TotalSize()
   159  }
   160  
   161  // KeySignatureSize returns the size in bytes of the value of field KeySignature
   162  func (s *Signature) KeySignatureTotalSize() uint64 {
   163  	return s.KeySignature.TotalSize()
   164  }
   165  
   166  // StructInfoOffset returns the offset in bytes of field StructInfo
   167  func (s *Signature) StructInfoOffset() uint64 {
   168  	return 0
   169  }
   170  
   171  // KeySignatureOffset returns the offset in bytes of field KeySignature
   172  func (s *Signature) KeySignatureOffset() uint64 {
   173  	return s.StructInfoOffset() + s.StructInfoTotalSize()
   174  }
   175  
   176  // Size returns the total size of the Signature.
   177  func (s *Signature) TotalSize() uint64 {
   178  	if s == nil {
   179  		return 0
   180  	}
   181  
   182  	var size uint64
   183  	size += s.StructInfoTotalSize()
   184  	size += s.KeySignatureTotalSize()
   185  	return size
   186  }
   187  
   188  // PrettyString returns the content of the structure in an easy-to-read format.
   189  func (s *Signature) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   190  	var lines []string
   191  	if withHeader {
   192  		lines = append(lines, pretty.Header(depth, "Signature", s))
   193  	}
   194  	if s == nil {
   195  		return strings.Join(lines, "\n")
   196  	}
   197  	// ManifestFieldType is structInfo
   198  	lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
   199  	// ManifestFieldType is subStruct
   200  	lines = append(lines, pretty.SubValue(depth+1, "Key Signature", "", &s.KeySignature, opts...)...)
   201  	if depth < 2 {
   202  		lines = append(lines, "")
   203  	}
   204  	return strings.Join(lines, "\n")
   205  }