github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/common/bgheader/bgheader.go (about)

     1  // Copyright 2017-2023 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 bgheader
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"io"
    11  )
    12  
    13  var (
    14  	binaryOrder = binary.LittleEndian
    15  )
    16  
    17  type structInfo struct {
    18  	ID      structureID `json:"StructInfoID"`
    19  	Version uint8       `json:"StructInfoVersion"`
    20  }
    21  
    22  type structureID [8]byte
    23  
    24  type BootGuardVersion uint8
    25  
    26  const (
    27  	Version10 BootGuardVersion = 1
    28  	Version20 BootGuardVersion = 2
    29  )
    30  
    31  func (bgv BootGuardVersion) String() string {
    32  	switch bgv {
    33  	case Version10:
    34  		return "1.0"
    35  	case Version20:
    36  		return "2.0"
    37  	}
    38  	return "unknown"
    39  }
    40  
    41  func DetectBGV(r io.ReadSeeker) (BootGuardVersion, error) {
    42  	var s structInfo
    43  	err := binary.Read(r, binaryOrder, &s)
    44  	if err != nil {
    45  		return 0, fmt.Errorf("unable to read field 'ID': %w", err)
    46  	}
    47  	_, err = r.Seek(0, 0)
    48  	if err != nil {
    49  		return 0, err
    50  	}
    51  	if s.Version >= 0x20 {
    52  		return Version20, nil
    53  	} else if (s.Version < 0x20) && (s.Version >= 0x10) {
    54  		return Version10, nil
    55  	} else {
    56  		return 0, fmt.Errorf("couldn't detect version")
    57  	}
    58  }