github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/fit/ent_boot_policy_manifest.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  package fit
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"io"
    12  
    13  	"github.com/linuxboot/fiano/pkg/intel/metadata/bg/bgbootpolicy"
    14  	"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntbootpolicy"
    15  	"github.com/linuxboot/fiano/pkg/intel/metadata/common/bgheader"
    16  )
    17  
    18  // EntryBootPolicyManifestRecord represents a FIT entry of type "Boot Policy Manifest" (0x0C)
    19  type EntryBootPolicyManifestRecord struct{ EntryBase }
    20  
    21  var _ EntryCustomGetDataSegmentSizer = (*EntryBootPolicyManifestRecord)(nil)
    22  
    23  func (entry *EntryBootPolicyManifestRecord) CustomGetDataSegmentSize(firmware io.ReadSeeker) (uint64, error) {
    24  	return uint64(entry.Headers.Size.Uint32()), nil
    25  }
    26  
    27  var _ EntryCustomRecalculateHeaderser = (*EntryBootPolicyManifestRecord)(nil)
    28  
    29  // CustomRecalculateHeaders recalculates metadata to be consistent with data.
    30  // For example, it fixes checksum, data size, entry type and so on.
    31  func (entry *EntryBootPolicyManifestRecord) CustomRecalculateHeaders() error {
    32  	mostCommonRecalculateHeadersOfEntry(entry)
    33  
    34  	entry.Headers.Size.SetUint32(uint32(len(entry.DataSegmentBytes)))
    35  	return nil
    36  }
    37  
    38  // Reader creates io.ReadSeeker from EntryBootPolicyManifestRecord
    39  func (entry *EntryBootPolicyManifestRecord) Reader() *bytes.Reader {
    40  	return bytes.NewReader(entry.DataSegmentBytes)
    41  }
    42  
    43  // ParseData creates EntryBootPolicyManifestRecord from EntryBootPolicyManifest
    44  func (entry *EntryBootPolicyManifestRecord) ParseData() (*bgbootpolicy.Manifest, *cbntbootpolicy.Manifest, error) {
    45  	r := bytes.NewReader(entry.DataSegmentBytes)
    46  	version, err := bgheader.DetectBGV(r)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  	switch version {
    51  	case bgheader.Version10:
    52  		manifest := bgbootpolicy.NewManifest()
    53  		_, err = manifest.ReadFrom(r)
    54  		if err != nil && !errors.Is(err, io.EOF) {
    55  			return nil, nil, err
    56  		}
    57  		return manifest, nil, nil
    58  	case bgheader.Version20:
    59  		manifest := cbntbootpolicy.NewManifest()
    60  		_, err = manifest.ReadFrom(r)
    61  		if err != nil && !errors.Is(err, io.EOF) {
    62  			return nil, nil, err
    63  		}
    64  		return nil, manifest, nil
    65  	default:
    66  		return nil, nil, fmt.Errorf("failed to parse BootPolicyManifest, err: %v", err)
    67  	}
    68  }
    69  
    70  // ParseBootPolicyManifest returns a boot policy manifest if it was able to
    71  // parse one.
    72  func (table Table) ParseBootPolicyManifest(firmware []byte) (*bgbootpolicy.Manifest, *cbntbootpolicy.Manifest, error) {
    73  	hdr := table.First(EntryTypeBootPolicyManifest)
    74  	if hdr == nil {
    75  		return nil, nil, ErrNotFound{}
    76  	}
    77  
    78  	return hdr.GetEntry(firmware).(*EntryBootPolicyManifestRecord).ParseData()
    79  }