github.com/linuxboot/fiano@v1.2.0/pkg/cbfs/types.go (about)

     1  // Copyright 2018-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 cbfs
     6  
     7  import (
     8  	"encoding/binary"
     9  	"io"
    10  
    11  	"github.com/linuxboot/fiano/pkg/fmap"
    12  )
    13  
    14  type Props struct {
    15  	Offset uint32
    16  	Size   uint32
    17  }
    18  
    19  type Compression uint32
    20  
    21  const (
    22  	None Compression = iota
    23  	LZMA
    24  	LZ4
    25  )
    26  
    27  var Endian = binary.BigEndian
    28  
    29  // These are standard component types for well known
    30  //   components (i.e - those that coreboot needs to consume.
    31  //   Users are welcome to use any other value for their
    32  //   components.
    33  type FileType uint32
    34  
    35  const (
    36  	// FOV
    37  	TypeDeleted2    FileType = 0xffffffff
    38  	TypeDeleted     FileType = 0
    39  	TypeBootBlock   FileType = 0x1
    40  	TypeMaster      FileType = 0x2
    41  	TypeLegacyStage FileType = 0x10
    42  	TypeStage       FileType = 0x11
    43  	TypeSELF        FileType = 0x20
    44  	TypeFIT         FileType = 0x21
    45  	TypeOptionRom   FileType = 0x30
    46  	TypeBootSplash  FileType = 0x40
    47  	TypeRaw         FileType = 0x50
    48  	TypeVSA         FileType = 0x51 // very, very obsolete Geode thing
    49  	TypeMBI         FileType = 0x52
    50  	TypeMicroCode   FileType = 0x53
    51  	TypeFSP         FileType = 0x60
    52  	TypeMRC         FileType = 0x61
    53  	TypeMMA         FileType = 0x62
    54  	TypeEFI         FileType = 0x63
    55  	TypeStruct      FileType = 0x70
    56  	TypeCMOS        FileType = 0xaa
    57  	TypeSPD         FileType = 0xab
    58  	TypeMRCCache    FileType = 0xac
    59  	TypeCMOSLayout  FileType = 0x1aa
    60  )
    61  
    62  const (
    63  	HeaderMagic   = 0x4F524243
    64  	HeaderV1      = 0x31313131
    65  	HeaderV2      = 0x31313132
    66  	HeaderVersion = HeaderV2
    67  	Alignment     = 64
    68  )
    69  
    70  /** This is a component header - every entry in the CBFS
    71    will have this header.
    72  
    73    This is how the component is arranged in the ROM:
    74  
    75    --------------   <- 0
    76    component header
    77    --------------   <- sizeof(struct component)
    78    component name
    79    --------------   <- offset
    80    data
    81    ...
    82    --------------   <- offset + len
    83  */
    84  
    85  const FileMagic = "LARCHIVE"
    86  
    87  const FileSize = 24
    88  
    89  type FileHeader struct {
    90  	Magic           [8]byte
    91  	Size            uint32
    92  	Type            FileType
    93  	AttrOffset      uint32
    94  	SubHeaderOffset uint32
    95  }
    96  
    97  type File struct {
    98  	FileHeader
    99  	RecordStart uint32
   100  	Name        string
   101  	Attr        []byte
   102  	FData       []byte
   103  }
   104  
   105  type mFile struct {
   106  	Name  string
   107  	Start uint32
   108  	Size  uint32
   109  	Type  string
   110  }
   111  
   112  // The common fields of extended cbfs file attributes.
   113  // Attributes are expected to start with tag/len, then append their
   114  // specific fields.
   115  type FileAttr struct {
   116  	Tag  uint32
   117  	Size uint32 // inclusize of Tag and Size
   118  }
   119  
   120  type Tag uint32
   121  
   122  const (
   123  	Unused     Tag = 0
   124  	Unused2    Tag = 0xffffffff
   125  	Compressed Tag = 0x42435a4c
   126  	Hash       Tag = 0x68736148
   127  	PSCB       Tag = 0x42435350
   128  	ALCB       Tag = 0x42434c41
   129  	SHCB       Tag = 0x53746748
   130  )
   131  
   132  type FileAttrCompression struct {
   133  	Tag              Tag
   134  	Size             uint32
   135  	Compression      Compression
   136  	DecompressedSize uint32
   137  }
   138  
   139  type FileAttrHash struct {
   140  	Tag      Tag
   141  	Size     uint32 // includes everything including data.
   142  	HashType uint32
   143  	Data     []byte
   144  }
   145  
   146  type FileAttrPos struct {
   147  	Tag  Tag
   148  	Size uint32 // includes everything including data.
   149  	Pos  uint32
   150  }
   151  
   152  type FileAttrAlign struct {
   153  	Tag   Tag
   154  	Size  uint32 // includes everything including data.
   155  	Align uint32
   156  }
   157  
   158  type FileAttrStageHeader struct {
   159  	Tag         Tag
   160  	Size        uint32
   161  	LoadAddress uint64
   162  	EntryOffset uint32
   163  	MemSize     uint32
   164  }
   165  
   166  // Component sub-headers
   167  
   168  // Following are component sub-headers for the "standard"
   169  // component types
   170  
   171  // this is the master cbfs header - it must be located somewhere available
   172  // to bootblock (to load romstage). The last 4 bytes in the image contain its
   173  // relative offset from the end of the image (as a 32-bit signed integer).
   174  const MasterHeaderLen = 32
   175  
   176  type MasterHeader struct {
   177  	Magic         uint32
   178  	Version       uint32
   179  	RomSize       uint32
   180  	BootBlockSize uint32
   181  	Align         uint32 // always 64 bytes -- FOV
   182  	Offset        uint32
   183  	Architecture  Architecture // integer, not name -- FOV
   184  	_             uint32
   185  }
   186  
   187  type MasterRecord struct {
   188  	File
   189  	MasterHeader
   190  }
   191  
   192  type Architecture uint32
   193  
   194  const (
   195  	X86 Architecture = 1
   196  	ARM Architecture = 0x10
   197  )
   198  
   199  type StageHeader struct {
   200  	Compression Compression
   201  	Entry       uint64
   202  	LoadAddress uint64
   203  	Size        uint32
   204  	MemSize     uint32
   205  }
   206  
   207  type LegacyStageRecord struct {
   208  	File
   209  	StageHeader
   210  	Data []byte
   211  }
   212  
   213  type StageRecord struct {
   214  	File
   215  	FileAttrStageHeader
   216  	Data []byte
   217  }
   218  
   219  type UnknownRecord struct {
   220  	File
   221  }
   222  
   223  type RawRecord struct {
   224  	File
   225  }
   226  
   227  type EmptyRecord struct {
   228  	File
   229  }
   230  
   231  type CMOSRecord struct {
   232  	File
   233  }
   234  
   235  type CMOSLayoutRecord struct {
   236  	File
   237  }
   238  
   239  type MicrocodeRecord struct {
   240  	File
   241  }
   242  
   243  type OptionROMRecord struct {
   244  	File
   245  }
   246  
   247  type BootBlockRecord struct {
   248  	File
   249  }
   250  
   251  type BootSplashRecord struct {
   252  	File
   253  }
   254  
   255  type SPDRecord struct {
   256  	File
   257  }
   258  
   259  type FSPRecord struct {
   260  	File
   261  }
   262  
   263  type PayloadHeader struct {
   264  	Type        SegmentType
   265  	Compression Compression
   266  	Offset      uint32
   267  	LoadAddress uint64
   268  	Size        uint32
   269  	MemSize     uint32
   270  }
   271  
   272  type PayloadRecord struct {
   273  	File
   274  	Segs []PayloadHeader
   275  	Data []byte
   276  }
   277  
   278  // fix this mess later to use characters, not constants.
   279  // I had done this once and it never made it into coreboot
   280  // and I still don't know why.
   281  type SegmentType uint32
   282  
   283  const (
   284  	SegCode   SegmentType = 0x434F4445
   285  	SegData   SegmentType = 0x44415441
   286  	SegBSS    SegmentType = 0x42535320
   287  	SegParams SegmentType = 0x50415241
   288  	SegEntry  SegmentType = 0x454E5452
   289  )
   290  
   291  func (s SegmentType) String() string {
   292  	switch s {
   293  	case SegCode:
   294  		return "code"
   295  	case SegData:
   296  		return "data"
   297  	case SegBSS:
   298  		return "bss"
   299  	case SegParams:
   300  		return "params"
   301  	case SegEntry:
   302  		return "entry"
   303  	}
   304  	return "unknown"
   305  }
   306  
   307  type OptionRom struct {
   308  	File
   309  	Compression Compression
   310  	Size        uint32
   311  }
   312  
   313  // Each CBFS file type must implement at least this interface.
   314  type ReadWriter interface {
   315  	GetFile() *File
   316  	String() string
   317  	Read(r io.ReadSeeker) error
   318  	Write(f io.Writer) error
   319  }
   320  
   321  type Image struct {
   322  	Segs []ReadWriter
   323  	// Scarf away the fmap info.
   324  	FMAP         *fmap.FMap
   325  	FMAPMetadata *fmap.Metadata
   326  	Area         *fmap.Area
   327  	// And all the data.
   328  	Data []byte
   329  }