github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntbootpolicy/se_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  // NewIBBSegment returns a new instance of IBBSegment with
    34  // all default values set.
    35  func NewIBBSegment() *IBBSegment {
    36  	s := &IBBSegment{}
    37  	s.Rehash()
    38  	return s
    39  }
    40  
    41  // Validate (recursively) checks the structure if there are any unexpected
    42  // values. It returns an error if so.
    43  func (s *IBBSegment) Validate() error {
    44  	// See tag "require"
    45  	for idx := range s.Reserved {
    46  		if s.Reserved[idx] != 0 {
    47  			return fmt.Errorf("'Reserved[%d]' is expected to be 0, but it is %v", idx, s.Reserved[idx])
    48  		}
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // ReadFrom reads the IBBSegment from 'r' in format defined in the document #575623.
    55  func (s *IBBSegment) ReadFrom(r io.Reader) (int64, error) {
    56  	totalN := int64(0)
    57  
    58  	// Reserved (ManifestFieldType: arrayStatic)
    59  	{
    60  		n, err := 2, binary.Read(r, binary.LittleEndian, s.Reserved[:])
    61  		if err != nil {
    62  			return totalN, fmt.Errorf("unable to read field 'Reserved': %w", err)
    63  		}
    64  		totalN += int64(n)
    65  	}
    66  
    67  	// Flags (ManifestFieldType: endValue)
    68  	{
    69  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.Flags)
    70  		if err != nil {
    71  			return totalN, fmt.Errorf("unable to read field 'Flags': %w", err)
    72  		}
    73  		totalN += int64(n)
    74  	}
    75  
    76  	// Base (ManifestFieldType: endValue)
    77  	{
    78  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.Base)
    79  		if err != nil {
    80  			return totalN, fmt.Errorf("unable to read field 'Base': %w", err)
    81  		}
    82  		totalN += int64(n)
    83  	}
    84  
    85  	// Size (ManifestFieldType: endValue)
    86  	{
    87  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.Size)
    88  		if err != nil {
    89  			return totalN, fmt.Errorf("unable to read field 'Size': %w", err)
    90  		}
    91  		totalN += int64(n)
    92  	}
    93  
    94  	return totalN, nil
    95  }
    96  
    97  // RehashRecursive calls Rehash (see below) recursively.
    98  func (s *IBBSegment) RehashRecursive() {
    99  	s.Rehash()
   100  }
   101  
   102  // Rehash sets values which are calculated automatically depending on the rest
   103  // data. It is usually about the total size field of an element.
   104  func (s *IBBSegment) Rehash() {
   105  }
   106  
   107  // WriteTo writes the IBBSegment into 'w' in format defined in
   108  // the document #575623.
   109  func (s *IBBSegment) WriteTo(w io.Writer) (int64, error) {
   110  	totalN := int64(0)
   111  	s.Rehash()
   112  
   113  	// Reserved (ManifestFieldType: arrayStatic)
   114  	{
   115  		n, err := 2, binary.Write(w, binary.LittleEndian, s.Reserved[:])
   116  		if err != nil {
   117  			return totalN, fmt.Errorf("unable to write field 'Reserved': %w", err)
   118  		}
   119  		totalN += int64(n)
   120  	}
   121  
   122  	// Flags (ManifestFieldType: endValue)
   123  	{
   124  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.Flags)
   125  		if err != nil {
   126  			return totalN, fmt.Errorf("unable to write field 'Flags': %w", err)
   127  		}
   128  		totalN += int64(n)
   129  	}
   130  
   131  	// Base (ManifestFieldType: endValue)
   132  	{
   133  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.Base)
   134  		if err != nil {
   135  			return totalN, fmt.Errorf("unable to write field 'Base': %w", err)
   136  		}
   137  		totalN += int64(n)
   138  	}
   139  
   140  	// Size (ManifestFieldType: endValue)
   141  	{
   142  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.Size)
   143  		if err != nil {
   144  			return totalN, fmt.Errorf("unable to write field 'Size': %w", err)
   145  		}
   146  		totalN += int64(n)
   147  	}
   148  
   149  	return totalN, nil
   150  }
   151  
   152  // ReservedSize returns the size in bytes of the value of field Reserved
   153  func (s *IBBSegment) ReservedTotalSize() uint64 {
   154  	return 2
   155  }
   156  
   157  // FlagsSize returns the size in bytes of the value of field Flags
   158  func (s *IBBSegment) FlagsTotalSize() uint64 {
   159  	return 2
   160  }
   161  
   162  // BaseSize returns the size in bytes of the value of field Base
   163  func (s *IBBSegment) BaseTotalSize() uint64 {
   164  	return 4
   165  }
   166  
   167  // SizeSize returns the size in bytes of the value of field Size
   168  func (s *IBBSegment) SizeTotalSize() uint64 {
   169  	return 4
   170  }
   171  
   172  // ReservedOffset returns the offset in bytes of field Reserved
   173  func (s *IBBSegment) ReservedOffset() uint64 {
   174  	return 0
   175  }
   176  
   177  // FlagsOffset returns the offset in bytes of field Flags
   178  func (s *IBBSegment) FlagsOffset() uint64 {
   179  	return s.ReservedOffset() + s.ReservedTotalSize()
   180  }
   181  
   182  // BaseOffset returns the offset in bytes of field Base
   183  func (s *IBBSegment) BaseOffset() uint64 {
   184  	return s.FlagsOffset() + s.FlagsTotalSize()
   185  }
   186  
   187  // SizeOffset returns the offset in bytes of field Size
   188  func (s *IBBSegment) SizeOffset() uint64 {
   189  	return s.BaseOffset() + s.BaseTotalSize()
   190  }
   191  
   192  // Size returns the total size of the IBBSegment.
   193  func (s *IBBSegment) TotalSize() uint64 {
   194  	if s == nil {
   195  		return 0
   196  	}
   197  
   198  	var size uint64
   199  	size += s.ReservedTotalSize()
   200  	size += s.FlagsTotalSize()
   201  	size += s.BaseTotalSize()
   202  	size += s.SizeTotalSize()
   203  	return size
   204  }
   205  
   206  // PrettyString returns the content of the structure in an easy-to-read format.
   207  func (s *IBBSegment) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   208  	var lines []string
   209  	if withHeader {
   210  		lines = append(lines, pretty.Header(depth, "IBB Segment", s))
   211  	}
   212  	if s == nil {
   213  		return strings.Join(lines, "\n")
   214  	}
   215  	// ManifestFieldType is arrayStatic
   216  	lines = append(lines, pretty.SubValue(depth+1, "Reserved", "", &s.Reserved, opts...)...)
   217  	// ManifestFieldType is endValue
   218  	lines = append(lines, pretty.SubValue(depth+1, "Flags", "", &s.Flags, opts...)...)
   219  	// ManifestFieldType is endValue
   220  	lines = append(lines, pretty.SubValue(depth+1, "Base", "", &s.Base, opts...)...)
   221  	// ManifestFieldType is endValue
   222  	lines = append(lines, pretty.SubValue(depth+1, "Size", "", &s.Size, opts...)...)
   223  	if depth < 2 {
   224  		lines = append(lines, "")
   225  	}
   226  	return strings.Join(lines, "\n")
   227  }
   228  
   229  // NewSE returns a new instance of SE with
   230  // all default values set.
   231  func NewSE() *SE {
   232  	s := &SE{}
   233  	copy(s.StructInfo.ID[:], []byte(StructureIDSE))
   234  	s.StructInfo.Version = 0x20
   235  	// Set through tag "required":
   236  	s.SetNumber = 0
   237  	// Recursively initializing a child structure:
   238  	s.PostIBBHash = *cbnt.NewHashStructure()
   239  	// Recursively initializing a child structure:
   240  	s.DigestList = *cbnt.NewHashList()
   241  	// Recursively initializing a child structure:
   242  	s.OBBHash = *cbnt.NewHashStructure()
   243  	s.Rehash()
   244  	return s
   245  }
   246  
   247  // Validate (recursively) checks the structure if there are any unexpected
   248  // values. It returns an error if so.
   249  func (s *SE) Validate() error {
   250  	// See tag "require"
   251  	for idx := range s.Reserved0 {
   252  		if s.Reserved0[idx] != 0 {
   253  			return fmt.Errorf("'Reserved0[%d]' is expected to be 0, but it is %v", idx, s.Reserved0[idx])
   254  		}
   255  	}
   256  	// See tag "require"
   257  	if s.SetNumber != 0 {
   258  		return fmt.Errorf("field 'SetNumber' expects value '0', but has %v", s.SetNumber)
   259  	}
   260  	// See tag "require"
   261  	for idx := range s.Reserved1 {
   262  		if s.Reserved1[idx] != 0 {
   263  			return fmt.Errorf("'Reserved1[%d]' is expected to be 0, but it is %v", idx, s.Reserved1[idx])
   264  		}
   265  	}
   266  	// Recursively validating a child structure:
   267  	if err := s.PostIBBHash.Validate(); err != nil {
   268  		return fmt.Errorf("error on field 'PostIBBHash': %w", err)
   269  	}
   270  	// Recursively validating a child structure:
   271  	if err := s.DigestList.Validate(); err != nil {
   272  		return fmt.Errorf("error on field 'DigestList': %w", err)
   273  	}
   274  	// Recursively validating a child structure:
   275  	if err := s.OBBHash.Validate(); err != nil {
   276  		return fmt.Errorf("error on field 'OBBHash': %w", err)
   277  	}
   278  	// See tag "require"
   279  	for idx := range s.Reserved2 {
   280  		if s.Reserved2[idx] != 0 {
   281  			return fmt.Errorf("'Reserved2[%d]' is expected to be 0, but it is %v", idx, s.Reserved2[idx])
   282  		}
   283  	}
   284  
   285  	return nil
   286  }
   287  
   288  // StructureIDSE is the StructureID (in terms of
   289  // the document #575623) of element 'SE'.
   290  const StructureIDSE = "__IBBS__"
   291  
   292  // GetStructInfo returns current value of StructInfo of the structure.
   293  //
   294  // StructInfo is a set of standard fields with presented in any element
   295  // ("element" in terms of document #575623).
   296  func (s *SE) GetStructInfo() cbnt.StructInfo {
   297  	return s.StructInfo
   298  }
   299  
   300  // SetStructInfo sets new value of StructInfo to the structure.
   301  //
   302  // StructInfo is a set of standard fields with presented in any element
   303  // ("element" in terms of document #575623).
   304  func (s *SE) SetStructInfo(newStructInfo cbnt.StructInfo) {
   305  	s.StructInfo = newStructInfo
   306  }
   307  
   308  // ReadFrom reads the SE from 'r' in format defined in the document #575623.
   309  func (s *SE) ReadFrom(r io.Reader) (int64, error) {
   310  	var totalN int64
   311  
   312  	err := binary.Read(r, binary.LittleEndian, &s.StructInfo)
   313  	if err != nil {
   314  		return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err)
   315  	}
   316  	totalN += int64(binary.Size(s.StructInfo))
   317  
   318  	n, err := s.ReadDataFrom(r)
   319  	if err != nil {
   320  		return totalN, fmt.Errorf("unable to read data: %w", err)
   321  	}
   322  	totalN += n
   323  
   324  	return totalN, nil
   325  }
   326  
   327  // ReadDataFrom reads the SE from 'r' excluding StructInfo,
   328  // in format defined in the document #575623.
   329  func (s *SE) ReadDataFrom(r io.Reader) (int64, error) {
   330  	totalN := int64(0)
   331  
   332  	// StructInfo (ManifestFieldType: structInfo)
   333  	{
   334  		// ReadDataFrom does not read Struct, use ReadFrom for that.
   335  	}
   336  
   337  	// Reserved0 (ManifestFieldType: arrayStatic)
   338  	{
   339  		n, err := 1, binary.Read(r, binary.LittleEndian, s.Reserved0[:])
   340  		if err != nil {
   341  			return totalN, fmt.Errorf("unable to read field 'Reserved0': %w", err)
   342  		}
   343  		totalN += int64(n)
   344  	}
   345  
   346  	// SetNumber (ManifestFieldType: endValue)
   347  	{
   348  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.SetNumber)
   349  		if err != nil {
   350  			return totalN, fmt.Errorf("unable to read field 'SetNumber': %w", err)
   351  		}
   352  		totalN += int64(n)
   353  	}
   354  
   355  	// Reserved1 (ManifestFieldType: arrayStatic)
   356  	{
   357  		n, err := 1, binary.Read(r, binary.LittleEndian, s.Reserved1[:])
   358  		if err != nil {
   359  			return totalN, fmt.Errorf("unable to read field 'Reserved1': %w", err)
   360  		}
   361  		totalN += int64(n)
   362  	}
   363  
   364  	// PBETValue (ManifestFieldType: endValue)
   365  	{
   366  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.PBETValue)
   367  		if err != nil {
   368  			return totalN, fmt.Errorf("unable to read field 'PBETValue': %w", err)
   369  		}
   370  		totalN += int64(n)
   371  	}
   372  
   373  	// Flags (ManifestFieldType: endValue)
   374  	{
   375  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.Flags)
   376  		if err != nil {
   377  			return totalN, fmt.Errorf("unable to read field 'Flags': %w", err)
   378  		}
   379  		totalN += int64(n)
   380  	}
   381  
   382  	// IBBMCHBAR (ManifestFieldType: endValue)
   383  	{
   384  		n, err := 8, binary.Read(r, binary.LittleEndian, &s.IBBMCHBAR)
   385  		if err != nil {
   386  			return totalN, fmt.Errorf("unable to read field 'IBBMCHBAR': %w", err)
   387  		}
   388  		totalN += int64(n)
   389  	}
   390  
   391  	// VTdBAR (ManifestFieldType: endValue)
   392  	{
   393  		n, err := 8, binary.Read(r, binary.LittleEndian, &s.VTdBAR)
   394  		if err != nil {
   395  			return totalN, fmt.Errorf("unable to read field 'VTdBAR': %w", err)
   396  		}
   397  		totalN += int64(n)
   398  	}
   399  
   400  	// DMAProtBase0 (ManifestFieldType: endValue)
   401  	{
   402  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.DMAProtBase0)
   403  		if err != nil {
   404  			return totalN, fmt.Errorf("unable to read field 'DMAProtBase0': %w", err)
   405  		}
   406  		totalN += int64(n)
   407  	}
   408  
   409  	// DMAProtLimit0 (ManifestFieldType: endValue)
   410  	{
   411  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.DMAProtLimit0)
   412  		if err != nil {
   413  			return totalN, fmt.Errorf("unable to read field 'DMAProtLimit0': %w", err)
   414  		}
   415  		totalN += int64(n)
   416  	}
   417  
   418  	// DMAProtBase1 (ManifestFieldType: endValue)
   419  	{
   420  		n, err := 8, binary.Read(r, binary.LittleEndian, &s.DMAProtBase1)
   421  		if err != nil {
   422  			return totalN, fmt.Errorf("unable to read field 'DMAProtBase1': %w", err)
   423  		}
   424  		totalN += int64(n)
   425  	}
   426  
   427  	// DMAProtLimit1 (ManifestFieldType: endValue)
   428  	{
   429  		n, err := 8, binary.Read(r, binary.LittleEndian, &s.DMAProtLimit1)
   430  		if err != nil {
   431  			return totalN, fmt.Errorf("unable to read field 'DMAProtLimit1': %w", err)
   432  		}
   433  		totalN += int64(n)
   434  	}
   435  
   436  	// PostIBBHash (ManifestFieldType: subStruct)
   437  	{
   438  		n, err := s.PostIBBHash.ReadFrom(r)
   439  		if err != nil {
   440  			return totalN, fmt.Errorf("unable to read field 'PostIBBHash': %w", err)
   441  		}
   442  		totalN += int64(n)
   443  	}
   444  
   445  	// IBBEntryPoint (ManifestFieldType: endValue)
   446  	{
   447  		n, err := 4, binary.Read(r, binary.LittleEndian, &s.IBBEntryPoint)
   448  		if err != nil {
   449  			return totalN, fmt.Errorf("unable to read field 'IBBEntryPoint': %w", err)
   450  		}
   451  		totalN += int64(n)
   452  	}
   453  
   454  	// DigestList (ManifestFieldType: subStruct)
   455  	{
   456  		n, err := s.DigestList.ReadFrom(r)
   457  		if err != nil {
   458  			return totalN, fmt.Errorf("unable to read field 'DigestList': %w", err)
   459  		}
   460  		totalN += int64(n)
   461  	}
   462  
   463  	// OBBHash (ManifestFieldType: subStruct)
   464  	{
   465  		n, err := s.OBBHash.ReadFrom(r)
   466  		if err != nil {
   467  			return totalN, fmt.Errorf("unable to read field 'OBBHash': %w", err)
   468  		}
   469  		totalN += int64(n)
   470  	}
   471  
   472  	// Reserved2 (ManifestFieldType: arrayStatic)
   473  	{
   474  		n, err := 3, binary.Read(r, binary.LittleEndian, s.Reserved2[:])
   475  		if err != nil {
   476  			return totalN, fmt.Errorf("unable to read field 'Reserved2': %w", err)
   477  		}
   478  		totalN += int64(n)
   479  	}
   480  
   481  	// IBBSegments (ManifestFieldType: list)
   482  	{
   483  		var count uint8
   484  		err := binary.Read(r, binary.LittleEndian, &count)
   485  		if err != nil {
   486  			return totalN, fmt.Errorf("unable to read the count for field 'IBBSegments': %w", err)
   487  		}
   488  		totalN += int64(binary.Size(count))
   489  		s.IBBSegments = make([]IBBSegment, count)
   490  
   491  		for idx := range s.IBBSegments {
   492  			n, err := s.IBBSegments[idx].ReadFrom(r)
   493  			if err != nil {
   494  				return totalN, fmt.Errorf("unable to read field 'IBBSegments[%d]': %w", idx, err)
   495  			}
   496  			totalN += int64(n)
   497  		}
   498  	}
   499  
   500  	return totalN, nil
   501  }
   502  
   503  // RehashRecursive calls Rehash (see below) recursively.
   504  func (s *SE) RehashRecursive() {
   505  	s.StructInfo.Rehash()
   506  	s.PostIBBHash.Rehash()
   507  	s.DigestList.Rehash()
   508  	s.OBBHash.Rehash()
   509  	s.Rehash()
   510  }
   511  
   512  // Rehash sets values which are calculated automatically depending on the rest
   513  // data. It is usually about the total size field of an element.
   514  func (s *SE) Rehash() {
   515  	s.Variable0 = 0
   516  	s.ElementSize = uint16(s.TotalSize())
   517  }
   518  
   519  // WriteTo writes the SE into 'w' in format defined in
   520  // the document #575623.
   521  func (s *SE) WriteTo(w io.Writer) (int64, error) {
   522  	totalN := int64(0)
   523  	s.Rehash()
   524  
   525  	// StructInfo (ManifestFieldType: structInfo)
   526  	{
   527  		n, err := s.StructInfo.WriteTo(w)
   528  		if err != nil {
   529  			return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
   530  		}
   531  		totalN += int64(n)
   532  	}
   533  
   534  	// Reserved0 (ManifestFieldType: arrayStatic)
   535  	{
   536  		n, err := 1, binary.Write(w, binary.LittleEndian, s.Reserved0[:])
   537  		if err != nil {
   538  			return totalN, fmt.Errorf("unable to write field 'Reserved0': %w", err)
   539  		}
   540  		totalN += int64(n)
   541  	}
   542  
   543  	// SetNumber (ManifestFieldType: endValue)
   544  	{
   545  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.SetNumber)
   546  		if err != nil {
   547  			return totalN, fmt.Errorf("unable to write field 'SetNumber': %w", err)
   548  		}
   549  		totalN += int64(n)
   550  	}
   551  
   552  	// Reserved1 (ManifestFieldType: arrayStatic)
   553  	{
   554  		n, err := 1, binary.Write(w, binary.LittleEndian, s.Reserved1[:])
   555  		if err != nil {
   556  			return totalN, fmt.Errorf("unable to write field 'Reserved1': %w", err)
   557  		}
   558  		totalN += int64(n)
   559  	}
   560  
   561  	// PBETValue (ManifestFieldType: endValue)
   562  	{
   563  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.PBETValue)
   564  		if err != nil {
   565  			return totalN, fmt.Errorf("unable to write field 'PBETValue': %w", err)
   566  		}
   567  		totalN += int64(n)
   568  	}
   569  
   570  	// Flags (ManifestFieldType: endValue)
   571  	{
   572  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.Flags)
   573  		if err != nil {
   574  			return totalN, fmt.Errorf("unable to write field 'Flags': %w", err)
   575  		}
   576  		totalN += int64(n)
   577  	}
   578  
   579  	// IBBMCHBAR (ManifestFieldType: endValue)
   580  	{
   581  		n, err := 8, binary.Write(w, binary.LittleEndian, &s.IBBMCHBAR)
   582  		if err != nil {
   583  			return totalN, fmt.Errorf("unable to write field 'IBBMCHBAR': %w", err)
   584  		}
   585  		totalN += int64(n)
   586  	}
   587  
   588  	// VTdBAR (ManifestFieldType: endValue)
   589  	{
   590  		n, err := 8, binary.Write(w, binary.LittleEndian, &s.VTdBAR)
   591  		if err != nil {
   592  			return totalN, fmt.Errorf("unable to write field 'VTdBAR': %w", err)
   593  		}
   594  		totalN += int64(n)
   595  	}
   596  
   597  	// DMAProtBase0 (ManifestFieldType: endValue)
   598  	{
   599  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.DMAProtBase0)
   600  		if err != nil {
   601  			return totalN, fmt.Errorf("unable to write field 'DMAProtBase0': %w", err)
   602  		}
   603  		totalN += int64(n)
   604  	}
   605  
   606  	// DMAProtLimit0 (ManifestFieldType: endValue)
   607  	{
   608  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.DMAProtLimit0)
   609  		if err != nil {
   610  			return totalN, fmt.Errorf("unable to write field 'DMAProtLimit0': %w", err)
   611  		}
   612  		totalN += int64(n)
   613  	}
   614  
   615  	// DMAProtBase1 (ManifestFieldType: endValue)
   616  	{
   617  		n, err := 8, binary.Write(w, binary.LittleEndian, &s.DMAProtBase1)
   618  		if err != nil {
   619  			return totalN, fmt.Errorf("unable to write field 'DMAProtBase1': %w", err)
   620  		}
   621  		totalN += int64(n)
   622  	}
   623  
   624  	// DMAProtLimit1 (ManifestFieldType: endValue)
   625  	{
   626  		n, err := 8, binary.Write(w, binary.LittleEndian, &s.DMAProtLimit1)
   627  		if err != nil {
   628  			return totalN, fmt.Errorf("unable to write field 'DMAProtLimit1': %w", err)
   629  		}
   630  		totalN += int64(n)
   631  	}
   632  
   633  	// PostIBBHash (ManifestFieldType: subStruct)
   634  	{
   635  		n, err := s.PostIBBHash.WriteTo(w)
   636  		if err != nil {
   637  			return totalN, fmt.Errorf("unable to write field 'PostIBBHash': %w", err)
   638  		}
   639  		totalN += int64(n)
   640  	}
   641  
   642  	// IBBEntryPoint (ManifestFieldType: endValue)
   643  	{
   644  		n, err := 4, binary.Write(w, binary.LittleEndian, &s.IBBEntryPoint)
   645  		if err != nil {
   646  			return totalN, fmt.Errorf("unable to write field 'IBBEntryPoint': %w", err)
   647  		}
   648  		totalN += int64(n)
   649  	}
   650  
   651  	// DigestList (ManifestFieldType: subStruct)
   652  	{
   653  		n, err := s.DigestList.WriteTo(w)
   654  		if err != nil {
   655  			return totalN, fmt.Errorf("unable to write field 'DigestList': %w", err)
   656  		}
   657  		totalN += int64(n)
   658  	}
   659  
   660  	// OBBHash (ManifestFieldType: subStruct)
   661  	{
   662  		n, err := s.OBBHash.WriteTo(w)
   663  		if err != nil {
   664  			return totalN, fmt.Errorf("unable to write field 'OBBHash': %w", err)
   665  		}
   666  		totalN += int64(n)
   667  	}
   668  
   669  	// Reserved2 (ManifestFieldType: arrayStatic)
   670  	{
   671  		n, err := 3, binary.Write(w, binary.LittleEndian, s.Reserved2[:])
   672  		if err != nil {
   673  			return totalN, fmt.Errorf("unable to write field 'Reserved2': %w", err)
   674  		}
   675  		totalN += int64(n)
   676  	}
   677  
   678  	// IBBSegments (ManifestFieldType: list)
   679  	{
   680  		count := uint8(len(s.IBBSegments))
   681  		err := binary.Write(w, binary.LittleEndian, &count)
   682  		if err != nil {
   683  			return totalN, fmt.Errorf("unable to write the count for field 'IBBSegments': %w", err)
   684  		}
   685  		totalN += int64(binary.Size(count))
   686  		for idx := range s.IBBSegments {
   687  			n, err := s.IBBSegments[idx].WriteTo(w)
   688  			if err != nil {
   689  				return totalN, fmt.Errorf("unable to write field 'IBBSegments[%d]': %w", idx, err)
   690  			}
   691  			totalN += int64(n)
   692  		}
   693  	}
   694  
   695  	return totalN, nil
   696  }
   697  
   698  // StructInfoSize returns the size in bytes of the value of field StructInfo
   699  func (s *SE) StructInfoTotalSize() uint64 {
   700  	return s.StructInfo.TotalSize()
   701  }
   702  
   703  // Reserved0Size returns the size in bytes of the value of field Reserved0
   704  func (s *SE) Reserved0TotalSize() uint64 {
   705  	return 1
   706  }
   707  
   708  // SetNumberSize returns the size in bytes of the value of field SetNumber
   709  func (s *SE) SetNumberTotalSize() uint64 {
   710  	return 1
   711  }
   712  
   713  // Reserved1Size returns the size in bytes of the value of field Reserved1
   714  func (s *SE) Reserved1TotalSize() uint64 {
   715  	return 1
   716  }
   717  
   718  // PBETValueSize returns the size in bytes of the value of field PBETValue
   719  func (s *SE) PBETValueTotalSize() uint64 {
   720  	return 1
   721  }
   722  
   723  // FlagsSize returns the size in bytes of the value of field Flags
   724  func (s *SE) FlagsTotalSize() uint64 {
   725  	return 4
   726  }
   727  
   728  // IBBMCHBARSize returns the size in bytes of the value of field IBBMCHBAR
   729  func (s *SE) IBBMCHBARTotalSize() uint64 {
   730  	return 8
   731  }
   732  
   733  // VTdBARSize returns the size in bytes of the value of field VTdBAR
   734  func (s *SE) VTdBARTotalSize() uint64 {
   735  	return 8
   736  }
   737  
   738  // DMAProtBase0Size returns the size in bytes of the value of field DMAProtBase0
   739  func (s *SE) DMAProtBase0TotalSize() uint64 {
   740  	return 4
   741  }
   742  
   743  // DMAProtLimit0Size returns the size in bytes of the value of field DMAProtLimit0
   744  func (s *SE) DMAProtLimit0TotalSize() uint64 {
   745  	return 4
   746  }
   747  
   748  // DMAProtBase1Size returns the size in bytes of the value of field DMAProtBase1
   749  func (s *SE) DMAProtBase1TotalSize() uint64 {
   750  	return 8
   751  }
   752  
   753  // DMAProtLimit1Size returns the size in bytes of the value of field DMAProtLimit1
   754  func (s *SE) DMAProtLimit1TotalSize() uint64 {
   755  	return 8
   756  }
   757  
   758  // PostIBBHashSize returns the size in bytes of the value of field PostIBBHash
   759  func (s *SE) PostIBBHashTotalSize() uint64 {
   760  	return s.PostIBBHash.TotalSize()
   761  }
   762  
   763  // IBBEntryPointSize returns the size in bytes of the value of field IBBEntryPoint
   764  func (s *SE) IBBEntryPointTotalSize() uint64 {
   765  	return 4
   766  }
   767  
   768  // DigestListSize returns the size in bytes of the value of field DigestList
   769  func (s *SE) DigestListTotalSize() uint64 {
   770  	return s.DigestList.TotalSize()
   771  }
   772  
   773  // OBBHashSize returns the size in bytes of the value of field OBBHash
   774  func (s *SE) OBBHashTotalSize() uint64 {
   775  	return s.OBBHash.TotalSize()
   776  }
   777  
   778  // Reserved2Size returns the size in bytes of the value of field Reserved2
   779  func (s *SE) Reserved2TotalSize() uint64 {
   780  	return 3
   781  }
   782  
   783  // IBBSegmentsSize returns the size in bytes of the value of field IBBSegments
   784  func (s *SE) IBBSegmentsTotalSize() uint64 {
   785  	var size uint64
   786  	size += uint64(binary.Size(uint8(0)))
   787  	for idx := range s.IBBSegments {
   788  		size += s.IBBSegments[idx].TotalSize()
   789  	}
   790  	return size
   791  }
   792  
   793  // StructInfoOffset returns the offset in bytes of field StructInfo
   794  func (s *SE) StructInfoOffset() uint64 {
   795  	return 0
   796  }
   797  
   798  // Reserved0Offset returns the offset in bytes of field Reserved0
   799  func (s *SE) Reserved0Offset() uint64 {
   800  	return s.StructInfoOffset() + s.StructInfoTotalSize()
   801  }
   802  
   803  // SetNumberOffset returns the offset in bytes of field SetNumber
   804  func (s *SE) SetNumberOffset() uint64 {
   805  	return s.Reserved0Offset() + s.Reserved0TotalSize()
   806  }
   807  
   808  // Reserved1Offset returns the offset in bytes of field Reserved1
   809  func (s *SE) Reserved1Offset() uint64 {
   810  	return s.SetNumberOffset() + s.SetNumberTotalSize()
   811  }
   812  
   813  // PBETValueOffset returns the offset in bytes of field PBETValue
   814  func (s *SE) PBETValueOffset() uint64 {
   815  	return s.Reserved1Offset() + s.Reserved1TotalSize()
   816  }
   817  
   818  // FlagsOffset returns the offset in bytes of field Flags
   819  func (s *SE) FlagsOffset() uint64 {
   820  	return s.PBETValueOffset() + s.PBETValueTotalSize()
   821  }
   822  
   823  // IBBMCHBAROffset returns the offset in bytes of field IBBMCHBAR
   824  func (s *SE) IBBMCHBAROffset() uint64 {
   825  	return s.FlagsOffset() + s.FlagsTotalSize()
   826  }
   827  
   828  // VTdBAROffset returns the offset in bytes of field VTdBAR
   829  func (s *SE) VTdBAROffset() uint64 {
   830  	return s.IBBMCHBAROffset() + s.IBBMCHBARTotalSize()
   831  }
   832  
   833  // DMAProtBase0Offset returns the offset in bytes of field DMAProtBase0
   834  func (s *SE) DMAProtBase0Offset() uint64 {
   835  	return s.VTdBAROffset() + s.VTdBARTotalSize()
   836  }
   837  
   838  // DMAProtLimit0Offset returns the offset in bytes of field DMAProtLimit0
   839  func (s *SE) DMAProtLimit0Offset() uint64 {
   840  	return s.DMAProtBase0Offset() + s.DMAProtBase0TotalSize()
   841  }
   842  
   843  // DMAProtBase1Offset returns the offset in bytes of field DMAProtBase1
   844  func (s *SE) DMAProtBase1Offset() uint64 {
   845  	return s.DMAProtLimit0Offset() + s.DMAProtLimit0TotalSize()
   846  }
   847  
   848  // DMAProtLimit1Offset returns the offset in bytes of field DMAProtLimit1
   849  func (s *SE) DMAProtLimit1Offset() uint64 {
   850  	return s.DMAProtBase1Offset() + s.DMAProtBase1TotalSize()
   851  }
   852  
   853  // PostIBBHashOffset returns the offset in bytes of field PostIBBHash
   854  func (s *SE) PostIBBHashOffset() uint64 {
   855  	return s.DMAProtLimit1Offset() + s.DMAProtLimit1TotalSize()
   856  }
   857  
   858  // IBBEntryPointOffset returns the offset in bytes of field IBBEntryPoint
   859  func (s *SE) IBBEntryPointOffset() uint64 {
   860  	return s.PostIBBHashOffset() + s.PostIBBHashTotalSize()
   861  }
   862  
   863  // DigestListOffset returns the offset in bytes of field DigestList
   864  func (s *SE) DigestListOffset() uint64 {
   865  	return s.IBBEntryPointOffset() + s.IBBEntryPointTotalSize()
   866  }
   867  
   868  // OBBHashOffset returns the offset in bytes of field OBBHash
   869  func (s *SE) OBBHashOffset() uint64 {
   870  	return s.DigestListOffset() + s.DigestListTotalSize()
   871  }
   872  
   873  // Reserved2Offset returns the offset in bytes of field Reserved2
   874  func (s *SE) Reserved2Offset() uint64 {
   875  	return s.OBBHashOffset() + s.OBBHashTotalSize()
   876  }
   877  
   878  // IBBSegmentsOffset returns the offset in bytes of field IBBSegments
   879  func (s *SE) IBBSegmentsOffset() uint64 {
   880  	return s.Reserved2Offset() + s.Reserved2TotalSize()
   881  }
   882  
   883  // Size returns the total size of the SE.
   884  func (s *SE) TotalSize() uint64 {
   885  	if s == nil {
   886  		return 0
   887  	}
   888  
   889  	var size uint64
   890  	size += s.StructInfoTotalSize()
   891  	size += s.Reserved0TotalSize()
   892  	size += s.SetNumberTotalSize()
   893  	size += s.Reserved1TotalSize()
   894  	size += s.PBETValueTotalSize()
   895  	size += s.FlagsTotalSize()
   896  	size += s.IBBMCHBARTotalSize()
   897  	size += s.VTdBARTotalSize()
   898  	size += s.DMAProtBase0TotalSize()
   899  	size += s.DMAProtLimit0TotalSize()
   900  	size += s.DMAProtBase1TotalSize()
   901  	size += s.DMAProtLimit1TotalSize()
   902  	size += s.PostIBBHashTotalSize()
   903  	size += s.IBBEntryPointTotalSize()
   904  	size += s.DigestListTotalSize()
   905  	size += s.OBBHashTotalSize()
   906  	size += s.Reserved2TotalSize()
   907  	size += s.IBBSegmentsTotalSize()
   908  	return size
   909  }
   910  
   911  // PrettyString returns the content of the structure in an easy-to-read format.
   912  func (s *SE) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   913  	var lines []string
   914  	if withHeader {
   915  		lines = append(lines, pretty.Header(depth, "IBB Segments Element", s))
   916  	}
   917  	if s == nil {
   918  		return strings.Join(lines, "\n")
   919  	}
   920  	// ManifestFieldType is structInfo
   921  	lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
   922  	// ManifestFieldType is arrayStatic
   923  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", &s.Reserved0, opts...)...)
   924  	// ManifestFieldType is endValue
   925  	lines = append(lines, pretty.SubValue(depth+1, "Set Number", "", &s.SetNumber, opts...)...)
   926  	// ManifestFieldType is arrayStatic
   927  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 1", "", &s.Reserved1, opts...)...)
   928  	// ManifestFieldType is endValue
   929  	lines = append(lines, pretty.SubValue(depth+1, "PBET Value", "", &s.PBETValue, opts...)...)
   930  	// ManifestFieldType is endValue
   931  	lines = append(lines, pretty.SubValue(depth+1, "Flags", "", &s.Flags, opts...)...)
   932  	// ManifestFieldType is endValue
   933  	lines = append(lines, pretty.SubValue(depth+1, "IBB MCHBAR", "", &s.IBBMCHBAR, opts...)...)
   934  	// ManifestFieldType is endValue
   935  	lines = append(lines, pretty.SubValue(depth+1, "VT-d BAR", "", &s.VTdBAR, opts...)...)
   936  	// ManifestFieldType is endValue
   937  	lines = append(lines, pretty.SubValue(depth+1, "DMA Protection 0 Base Address", "", &s.DMAProtBase0, opts...)...)
   938  	// ManifestFieldType is endValue
   939  	lines = append(lines, pretty.SubValue(depth+1, "DMA Protection 0 Limit Address", "", &s.DMAProtLimit0, opts...)...)
   940  	// ManifestFieldType is endValue
   941  	lines = append(lines, pretty.SubValue(depth+1, "DMA Protection 1 Base Address", "", &s.DMAProtBase1, opts...)...)
   942  	// ManifestFieldType is endValue
   943  	lines = append(lines, pretty.SubValue(depth+1, "DMA Protection 2 Limit Address", "", &s.DMAProtLimit1, opts...)...)
   944  	// ManifestFieldType is subStruct
   945  	lines = append(lines, pretty.SubValue(depth+1, "Post IBB Hash", "", &s.PostIBBHash, opts...)...)
   946  	// ManifestFieldType is endValue
   947  	lines = append(lines, pretty.SubValue(depth+1, "IBB Entry Point", "", &s.IBBEntryPoint, opts...)...)
   948  	// ManifestFieldType is subStruct
   949  	lines = append(lines, pretty.SubValue(depth+1, "Digest List", "", &s.DigestList, opts...)...)
   950  	// ManifestFieldType is subStruct
   951  	lines = append(lines, pretty.SubValue(depth+1, "OBB Hash", "", &s.OBBHash, opts...)...)
   952  	// ManifestFieldType is arrayStatic
   953  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 2", "", &s.Reserved2, opts...)...)
   954  	// ManifestFieldType is list
   955  	lines = append(lines, pretty.Header(depth+1, fmt.Sprintf("IBBSegments: Array of \"IBB Segments Element\" of length %d", len(s.IBBSegments)), s.IBBSegments))
   956  	for i := 0; i < len(s.IBBSegments); i++ {
   957  		lines = append(lines, fmt.Sprintf("%sitem #%d: ", strings.Repeat("  ", int(depth+2)), i)+strings.TrimSpace(s.IBBSegments[i].PrettyString(depth+2, true)))
   958  	}
   959  	if depth < 1 {
   960  		lines = append(lines, "")
   961  	}
   962  	if depth < 2 {
   963  		lines = append(lines, "")
   964  	}
   965  	return strings.Join(lines, "\n")
   966  }
   967  
   968  // PrettyString returns the bits of the flags in an easy-to-read format.
   969  func (v CachingType) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   970  	return v.String()
   971  }
   972  
   973  // TotalSize returns the total size measured through binary.Size.
   974  func (v CachingType) TotalSize() uint64 {
   975  	return uint64(binary.Size(v))
   976  }
   977  
   978  // WriteTo writes the CachingType into 'w' in binary format.
   979  func (v CachingType) WriteTo(w io.Writer) (int64, error) {
   980  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
   981  }
   982  
   983  // ReadFrom reads the CachingType from 'r' in binary format.
   984  func (v CachingType) ReadFrom(r io.Reader) (int64, error) {
   985  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
   986  }
   987  
   988  // PrettyString returns the bits of the flags in an easy-to-read format.
   989  func (v PBETValue) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   990  	var lines []string
   991  	if withHeader {
   992  		lines = append(lines, pretty.Header(depth, "PBET Value", v))
   993  	}
   994  	lines = append(lines, pretty.SubValue(depth+1, "PBET Value", "", v.PBETValue(), opts...)...)
   995  	return strings.Join(lines, "\n")
   996  }
   997  
   998  // TotalSize returns the total size measured through binary.Size.
   999  func (v PBETValue) TotalSize() uint64 {
  1000  	return uint64(binary.Size(v))
  1001  }
  1002  
  1003  // WriteTo writes the PBETValue into 'w' in binary format.
  1004  func (v PBETValue) WriteTo(w io.Writer) (int64, error) {
  1005  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
  1006  }
  1007  
  1008  // ReadFrom reads the PBETValue from 'r' in binary format.
  1009  func (v PBETValue) ReadFrom(r io.Reader) (int64, error) {
  1010  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
  1011  }
  1012  
  1013  // PrettyString returns the bits of the flags in an easy-to-read format.
  1014  func (v SEFlags) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
  1015  	var lines []string
  1016  	if withHeader {
  1017  		lines = append(lines, pretty.Header(depth, "SE Flags", v))
  1018  	}
  1019  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 0", "", v.Reserved0(), opts...)...)
  1020  	if v.SupportsTopSwapRemediation() {
  1021  		lines = append(lines, pretty.SubValue(depth+1, "Supports Top Swap Remediation", "BIOS supports Top Swap remediation action", true, opts...)...)
  1022  	} else {
  1023  		lines = append(lines, pretty.SubValue(depth+1, "Supports Top Swap Remediation", "BIOS does not support Top Swap remediation action", false, opts...)...)
  1024  	}
  1025  	if v.TPMFailureLeavesHierarchiesEnabled() {
  1026  		lines = append(lines, pretty.SubValue(depth+1, "TPM Failure Leaves Hierarchies Enabled", "Leave Hierarchies enabled. Cap all PCRs on failure.", true, opts...)...)
  1027  	} else {
  1028  		lines = append(lines, pretty.SubValue(depth+1, "TPM Failure Leaves Hierarchies Enabled", "Do not leave enabled. Disable all Hierarchies or deactivate on failure.", false, opts...)...)
  1029  	}
  1030  	if v.AuthorityMeasure() {
  1031  		lines = append(lines, pretty.SubValue(depth+1, "Authority Measure", "Extend Authority Measurements into the Authority PCR 7", true, opts...)...)
  1032  	} else {
  1033  		lines = append(lines, pretty.SubValue(depth+1, "Authority Measure", "Do not extend into the Authority PCR 7", false, opts...)...)
  1034  	}
  1035  	if v.Locality3Startup() {
  1036  		lines = append(lines, pretty.SubValue(depth+1, "Locality 3 Startup", "Issue TPM Start-up from Locality 3", true, opts...)...)
  1037  	} else {
  1038  		lines = append(lines, pretty.SubValue(depth+1, "Locality 3 Startup", "Disabled", false, opts...)...)
  1039  	}
  1040  	if v.DMAProtection() {
  1041  		lines = append(lines, pretty.SubValue(depth+1, "DMA Protection", "Enable DMA Protection", true, opts...)...)
  1042  	} else {
  1043  		lines = append(lines, pretty.SubValue(depth+1, "DMA Protection", "Disable DMA Protection", false, opts...)...)
  1044  	}
  1045  	return strings.Join(lines, "\n")
  1046  }
  1047  
  1048  // TotalSize returns the total size measured through binary.Size.
  1049  func (v SEFlags) TotalSize() uint64 {
  1050  	return uint64(binary.Size(v))
  1051  }
  1052  
  1053  // WriteTo writes the SEFlags into 'w' in binary format.
  1054  func (v SEFlags) WriteTo(w io.Writer) (int64, error) {
  1055  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
  1056  }
  1057  
  1058  // ReadFrom reads the SEFlags from 'r' in binary format.
  1059  func (v SEFlags) ReadFrom(r io.Reader) (int64, error) {
  1060  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
  1061  }