github.com/linuxboot/fiano@v1.2.0/pkg/amd/manifest/psp_header.go (about)

     1  // Copyright 2019 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 manifest
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"io"
    11  )
    12  
    13  // PSPBootloaderCookie is a special identifier of a PSP binary
    14  const PSPBootloaderCookie = 0x31535024 // "$PS1"
    15  
    16  // FirmwareVersion represents PSP firmware version
    17  type FirmwareVersion [4]byte
    18  
    19  // String converts FirmwareVersion into a string
    20  func (v FirmwareVersion) String() string {
    21  	return fmt.Sprintf("%x.%x.%x.%x", v[3], v[2], v[1], v[0])
    22  }
    23  
    24  // PSPHeader represents a header of each firmware binary
    25  // See: https://doc.coreboot.org/soc/amd/psp_integration.html
    26  type PSPHeader struct {
    27  	Reserved1 [16]byte
    28  	Cookie    uint32
    29  	Reserved2 [76]byte
    30  	Version   FirmwareVersion
    31  	Reserved3 [156]byte
    32  }
    33  
    34  // Reserved1Offset returns the offset in bytes of field Reserved1
    35  func (h *PSPHeader) Reserved1Offset() uint64 {
    36  	return 0
    37  }
    38  
    39  func (h *PSPHeader) Reserved1Length() uint64 {
    40  	return uint64(binary.Size(h.Reserved1))
    41  }
    42  
    43  // CookieOffset returns the offset in bytes of field Cookie
    44  func (h *PSPHeader) CookieOffset() uint64 {
    45  	return h.Reserved1Offset() + h.Reserved1Length()
    46  }
    47  
    48  // CookieLength returns the size in bytes of field Cookie
    49  func (h *PSPHeader) CookieLength() uint64 {
    50  	return uint64(binary.Size(h.Cookie))
    51  }
    52  
    53  // Reserved2Offset returns the offset in bytes of field Reserved2
    54  func (h *PSPHeader) Reserved2Offset() uint64 {
    55  	return h.CookieOffset() + h.CookieLength()
    56  }
    57  
    58  // Reserved2Length returns the size in bytes of field Reserved2
    59  func (h *PSPHeader) Reserved2Length() uint64 {
    60  	return uint64(binary.Size(h.Reserved2))
    61  }
    62  
    63  // VersionOffset returns the offset in bytes of field Version
    64  func (h *PSPHeader) VersionOffset() uint64 {
    65  	return h.Reserved2Offset() + h.Reserved2Length()
    66  }
    67  
    68  // VersionLength returns the size in bytes of field Version
    69  func (h *PSPHeader) VersionLength() uint64 {
    70  	return uint64(binary.Size(h.Version))
    71  }
    72  
    73  // ParsePSPHeader parses the PSP header that is supposed to be the beginning of each PSP binary
    74  func ParsePSPHeader(r io.Reader) (*PSPHeader, error) {
    75  	var result PSPHeader
    76  	if err := binary.Read(r, binary.LittleEndian, &result); err != nil {
    77  		return nil, err
    78  	}
    79  	return &result, nil
    80  }