github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/bgbootpolicy/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 -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  // 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 = 0x10
    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  
    47  	return nil
    48  }
    49  
    50  // StructureIDPM is the StructureID (in terms of
    51  // the document #575623) of element 'PM'.
    52  const StructureIDPM = "__PMDA__"
    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 *PM) GetStructInfo() bg.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 *PM) SetStructInfo(newStructInfo bg.StructInfo) {
    67  	s.StructInfo = newStructInfo
    68  }
    69  
    70  // ReadFrom reads the PM from 'r' in format defined in the document #575623.
    71  func (s *PM) 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 PM from 'r' excluding StructInfo,
    90  // in format defined in the document #575623.
    91  func (s *PM) 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  	// DataSize (ManifestFieldType: endValue)
   100  	{
   101  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.DataSize)
   102  		if err != nil {
   103  			return totalN, fmt.Errorf("unable to read field 'DataSize': %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 *PM) 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 *PM) Rehash() {
   136  }
   137  
   138  // WriteTo writes the PM into 'w' in format defined in
   139  // the document #575623.
   140  func (s *PM) WriteTo(w io.Writer) (int64, error) {
   141  	totalN := int64(0)
   142  	s.Rehash()
   143  
   144  	// StructInfo (ManifestFieldType: structInfo)
   145  	{
   146  		n, err := s.StructInfo.WriteTo(w)
   147  		if err != nil {
   148  			return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
   149  		}
   150  		totalN += int64(n)
   151  	}
   152  
   153  	// DataSize (ManifestFieldType: endValue)
   154  	{
   155  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.DataSize)
   156  		if err != nil {
   157  			return totalN, fmt.Errorf("unable to write field 'DataSize': %w", err)
   158  		}
   159  		totalN += int64(n)
   160  	}
   161  
   162  	// Data (ManifestFieldType: arrayDynamic)
   163  	{
   164  		size := uint16(len(s.Data))
   165  		err := binary.Write(w, binary.LittleEndian, size)
   166  		if err != nil {
   167  			return totalN, fmt.Errorf("unable to write the size of field 'Data': %w", err)
   168  		}
   169  		totalN += int64(binary.Size(size))
   170  		n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data)
   171  		if err != nil {
   172  			return totalN, fmt.Errorf("unable to write field 'Data': %w", err)
   173  		}
   174  		totalN += int64(n)
   175  	}
   176  
   177  	return totalN, nil
   178  }
   179  
   180  // StructInfoSize returns the size in bytes of the value of field StructInfo
   181  func (s *PM) StructInfoTotalSize() uint64 {
   182  	return s.StructInfo.TotalSize()
   183  }
   184  
   185  // DataSizeSize returns the size in bytes of the value of field DataSize
   186  func (s *PM) DataSizeTotalSize() uint64 {
   187  	return 2
   188  }
   189  
   190  // DataSize returns the size in bytes of the value of field Data
   191  func (s *PM) DataTotalSize() uint64 {
   192  	size := uint64(binary.Size(uint16(0)))
   193  	size += uint64(len(s.Data))
   194  	return size
   195  }
   196  
   197  // StructInfoOffset returns the offset in bytes of field StructInfo
   198  func (s *PM) StructInfoOffset() uint64 {
   199  	return 0
   200  }
   201  
   202  // DataSizeOffset returns the offset in bytes of field DataSize
   203  func (s *PM) DataSizeOffset() uint64 {
   204  	return s.StructInfoOffset() + s.StructInfoTotalSize()
   205  }
   206  
   207  // DataOffset returns the offset in bytes of field Data
   208  func (s *PM) DataOffset() uint64 {
   209  	return s.DataSizeOffset() + s.DataSizeTotalSize()
   210  }
   211  
   212  // Size returns the total size of the PM.
   213  func (s *PM) TotalSize() uint64 {
   214  	if s == nil {
   215  		return 0
   216  	}
   217  
   218  	var size uint64
   219  	size += s.StructInfoTotalSize()
   220  	size += s.DataSizeTotalSize()
   221  	size += s.DataTotalSize()
   222  	return size
   223  }
   224  
   225  // PrettyString returns the content of the structure in an easy-to-read format.
   226  func (s *PM) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   227  	var lines []string
   228  	if withHeader {
   229  		lines = append(lines, pretty.Header(depth, "PM", s))
   230  	}
   231  	if s == nil {
   232  		return strings.Join(lines, "\n")
   233  	}
   234  	// ManifestFieldType is structInfo
   235  	lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
   236  	// ManifestFieldType is endValue
   237  	lines = append(lines, pretty.SubValue(depth+1, "Data Size", "", &s.DataSize, opts...)...)
   238  	// ManifestFieldType is arrayDynamic
   239  	lines = append(lines, pretty.SubValue(depth+1, "Data", "", &s.Data, opts...)...)
   240  	if depth < 2 {
   241  		lines = append(lines, "")
   242  	}
   243  	return strings.Join(lines, "\n")
   244  }