github.com/linuxboot/fiano@v1.2.0/pkg/fsp/header_test.go (about)

     1  // Copyright 2017-2018 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 fsp
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  var (
    13  	FSPTestHeaderRev3 = []byte("FSPHH\x00\x00\x00\x00\x00 \x03\x01\x03\x04\x01$APLFSP$\x00\xa0\x02\x00\x00\x00 \x00\x01\x00\x030$\x01\x00\x00\xb0\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x8a\x05\x00\x00")
    14  	FSPTestHeaderRev4 = []byte("\x46\x53\x50\x48\x4c\x00\x00\x00\x00\x00\x21\x04\x3a\x00\x02\x02\x24\x43\x50\x58\x2d\x53\x50\x24\x00\x00\x04\x00\x00\x00\xcc\xff\x02\x00\x03\x30\x7c\x01\x00\x00\x58\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x98\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa2\x02\x00\x00\x00\x00\x00\x00")
    15  	FSPTestHeaderRev5 = []byte("\x46\x53\x50\x48\x4c\x00\x00\x00\x00\x00\x22\x05\x71\x7d\x00\x0a\x54\x47\x4c\x49\x2d\x46\x53\x50\x00\xa0\x05\x00\x00\x00\xe3\xff\x03\x00\x03\x30\xb4\x06\x00\x00\xe0\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd8\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe2\x01\x00\x00\xec\x01\x00\x00")
    16  	FSPTestHeaderRev6 = []byte("\x46\x53\x50\x48\x50\x00\x00\x00\x00\x00\x23\x06\x0f\x01\x01\x01\x24\x53\x50\x52\x2d\x53\x50\x24\x00\x80\x00\x00\x00\x00\xfe\xff\x02\x00\x00\x10\x4c\x02\x00\x00\x68\x00\x00\x00\x00\x00\x00\x00\x11\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00")
    17  )
    18  
    19  func TestNewInfoHeaderRev3(t *testing.T) {
    20  	hdr, err := NewInfoHeader(FSPTestHeaderRev3)
    21  	if err != nil {
    22  		t.Errorf("NewInfoHeader failed to parse FSP header: %v", err)
    23  	}
    24  	if hdr.Signature != Signature {
    25  		t.Errorf("Invalid signature %v; want %v", hdr.Signature, Signature)
    26  	}
    27  	if hdr.HeaderLength != HeaderV3Length {
    28  		t.Errorf("Invalid header length %d; want %d", hdr.HeaderLength, HeaderV3Length)
    29  	}
    30  	if hdr.SpecVersion != SpecVersion(0x20) {
    31  		t.Errorf("Invalid spec version %s; want %s", hdr.SpecVersion, SpecVersion(0x20))
    32  	}
    33  	if hdr.HeaderRevision != 3 {
    34  		t.Errorf("Invalid header revision %d; want %d", hdr.HeaderRevision, 3)
    35  	}
    36  	if hdr.ImageRevision != ImageRevision(0x1000400030001) {
    37  		t.Errorf("Invalid image revision %s; want %s", hdr.ImageRevision, ImageRevision(0x1000400030001))
    38  	}
    39  	if !bytes.Equal(hdr.ImageID[:], []byte("$APLFSP$")) {
    40  		t.Errorf("Invalid image ID %s; want %s", hdr.ImageID, "$APLFSP$")
    41  	}
    42  	if hdr.ImageSize != 0x2a000 {
    43  		t.Errorf("Invalid image size %#x; want %#x", hdr.ImageSize, 0x2a000)
    44  	}
    45  	if hdr.ImageBase != 0x200000 {
    46  		t.Errorf("Invalid image base %#x; want %#x", hdr.ImageBase, 0x200000)
    47  	}
    48  	if hdr.ImageAttribute != 0x1 {
    49  		t.Errorf("Invalid image attribute %#x; want %#x", hdr.ImageAttribute, 0x1)
    50  	}
    51  	if hdr.ComponentAttribute != 0x3003 {
    52  		t.Errorf("Invalid component attribute %#x; want %#x", hdr.ComponentAttribute, 0x3003)
    53  	}
    54  	if hdr.CfgRegionOffset != 0x124 {
    55  		t.Errorf("Invalid cfg region offset %#x; want %#x", hdr.CfgRegionOffset, 0x124)
    56  	}
    57  	if hdr.CfgRegionSize != 0x3b0 {
    58  		t.Errorf("Invalid cfg region size %#x; want %#x", hdr.CfgRegionSize, 0x3b0)
    59  	}
    60  	if hdr.TempRAMInitEntryOffset != 0x0 {
    61  		t.Errorf("Invalid temp RAM init entry offset %#x; want %#x", hdr.TempRAMInitEntryOffset, 0x0)
    62  	}
    63  	if hdr.NotifyPhaseEntryOffset != 0x580 {
    64  		t.Errorf("Invalid notify phase entry offset %#x; want %#x", hdr.NotifyPhaseEntryOffset, 0x580)
    65  	}
    66  	if hdr.FSPMemoryInitEntryOffset != 0x0 {
    67  		t.Errorf("Invalid FSP memory init entry offset %#x; want %#x", hdr.FSPMemoryInitEntryOffset, 0x0)
    68  	}
    69  	if hdr.TempRAMExitEntryOffset != 0x0 {
    70  		t.Errorf("Invalid temp RAM exit entry offset %#x; want %#x", hdr.TempRAMExitEntryOffset, 0x0)
    71  	}
    72  	if hdr.FSPSiliconInitEntryOffset != 0x58a {
    73  		t.Errorf("Invalid FSP silicon init entry offset %#x; want %#x", hdr.FSPSiliconInitEntryOffset, 0x58a)
    74  	}
    75  	if hdr.FspMultiPhaseSiInitEntryOffset != 0 {
    76  		t.Errorf("Invalid FSP silicon init entry offset %#x; want %#x", hdr.FspMultiPhaseSiInitEntryOffset, 0)
    77  	}
    78  }
    79  
    80  func TestNewInfoHeaderRev4(t *testing.T) {
    81  	hdr, err := NewInfoHeader(FSPTestHeaderRev4)
    82  	if err != nil {
    83  		t.Errorf("NewInfoHeader failed to parse FSP header: %v", err)
    84  	}
    85  	if hdr.Signature != Signature {
    86  		t.Errorf("Invalid signature %v; want %v", hdr.Signature, Signature)
    87  	}
    88  	// This FSP violates the spec! Should be HeaderV4Length.
    89  	if hdr.HeaderLength != HeaderV5Length {
    90  		t.Errorf("Invalid header length %d; want %d", hdr.HeaderLength, HeaderV5Length)
    91  	}
    92  	if hdr.SpecVersion != SpecVersion(0x21) {
    93  		t.Errorf("Invalid spec version %s; want %s", hdr.SpecVersion, SpecVersion(0x21))
    94  	}
    95  	if hdr.HeaderRevision != 4 {
    96  		t.Errorf("Invalid header revision %d; want %d", hdr.HeaderRevision, 4)
    97  	}
    98  	if hdr.ImageRevision != ImageRevision(0x200020000003a) {
    99  		t.Errorf("Invalid image revision %s; want %s", hdr.ImageRevision, ImageRevision(0x200020000003a))
   100  	}
   101  	if !bytes.Equal(hdr.ImageID[:], []byte("$CPX-SP$")) {
   102  		t.Errorf("Invalid image ID %s; want %s", hdr.ImageID, "$CPX-SP$")
   103  	}
   104  	if hdr.ImageSize != 0x40000 {
   105  		t.Errorf("Invalid image size %#x; want %#x", hdr.ImageSize, 0x40000)
   106  	}
   107  	if hdr.ImageBase != 0xffcc0000 {
   108  		t.Errorf("Invalid image base %#x; want %#x", hdr.ImageBase, 0xffcc0000)
   109  	}
   110  	if hdr.ImageAttribute != 0x2 {
   111  		t.Errorf("Invalid image attribute %#x; want %#x", int(hdr.ImageAttribute), 0x2)
   112  	}
   113  	if hdr.ComponentAttribute != 0x3003 {
   114  		t.Errorf("Invalid component attribute %#x; want %#x", int(hdr.ComponentAttribute), 0x3003)
   115  	}
   116  	if hdr.CfgRegionOffset != 0x17c {
   117  		t.Errorf("Invalid cfg region offset %#x; want %#x", hdr.CfgRegionOffset, 0x17c)
   118  	}
   119  	if hdr.CfgRegionSize != 0x58 {
   120  		t.Errorf("Invalid cfg region size %#x; want %#x", hdr.CfgRegionSize, 0x58)
   121  	}
   122  	if hdr.TempRAMInitEntryOffset != 0x0 {
   123  		t.Errorf("Invalid temp RAM init entry offset %#x; want %#x", hdr.TempRAMInitEntryOffset, 0x0)
   124  	}
   125  	if hdr.NotifyPhaseEntryOffset != 0x298 {
   126  		t.Errorf("Invalid notify phase entry offset %#x; want %#x", hdr.NotifyPhaseEntryOffset, 0x298)
   127  	}
   128  	if hdr.FSPMemoryInitEntryOffset != 0x0 {
   129  		t.Errorf("Invalid FSP memory init entry offset %#x; want %#x", hdr.FSPMemoryInitEntryOffset, 0x0)
   130  	}
   131  	if hdr.TempRAMExitEntryOffset != 0x0 {
   132  		t.Errorf("Invalid temp RAM exit entry offset %#x; want %#x", hdr.TempRAMExitEntryOffset, 0x0)
   133  	}
   134  	if hdr.FSPSiliconInitEntryOffset != 0x2a2 {
   135  		t.Errorf("Invalid FSP silicon init entry offset %#x; want %#x", hdr.FSPSiliconInitEntryOffset, 0x2a2)
   136  	}
   137  	if hdr.FspMultiPhaseSiInitEntryOffset != 0 {
   138  		t.Errorf("Invalid FSP silicon init entry offset %#x; want %#x", hdr.FspMultiPhaseSiInitEntryOffset, 0)
   139  	}
   140  }
   141  
   142  func TestNewInfoHeaderRev5(t *testing.T) {
   143  	hdr, err := NewInfoHeader(FSPTestHeaderRev5)
   144  	if err != nil {
   145  		t.Errorf("NewInfoHeader failed to parse FSP header: %v", err)
   146  	}
   147  	if hdr.Signature != Signature {
   148  		t.Errorf("Invalid signature %v; want %v", hdr.Signature, Signature)
   149  	}
   150  	if hdr.HeaderLength != HeaderV5Length {
   151  		t.Errorf("Invalid header length %d; want %d", hdr.HeaderLength, HeaderV5Length)
   152  	}
   153  	if hdr.SpecVersion != SpecVersion(0x22) {
   154  		t.Errorf("Invalid spec version %s; want %s", hdr.SpecVersion, SpecVersion(0x22))
   155  	}
   156  	if hdr.HeaderRevision != 5 {
   157  		t.Errorf("Invalid header revision %d; want %d", hdr.HeaderRevision, 5)
   158  	}
   159  	if hdr.ImageRevision != ImageRevision(0xA0000007d0071) {
   160  		t.Errorf("Invalid image revision %s; want %s", hdr.ImageRevision, ImageRevision(0xA0000007d0071))
   161  	}
   162  	if !bytes.Equal(hdr.ImageID[:], []byte("TGLI-FSP")) {
   163  		t.Errorf("Invalid image ID %s; want %s", hdr.ImageID, "TGLI-FSP")
   164  	}
   165  	if hdr.ImageSize != 0x5a000 {
   166  		t.Errorf("Invalid image size %#x; want %#x", hdr.ImageSize, 0x5a000)
   167  	}
   168  	if hdr.ImageBase != 0xffe30000 {
   169  		t.Errorf("Invalid image base %#x; want %#x", hdr.ImageBase, 0xffe30000)
   170  	}
   171  	if hdr.ImageAttribute != 0x3 {
   172  		t.Errorf("Invalid image attribute %#x; want %#x", int(hdr.ImageAttribute), 0x3)
   173  	}
   174  	if hdr.ComponentAttribute != 0x3003 {
   175  		t.Errorf("Invalid component attribute %#x; want %#x", int(hdr.ComponentAttribute), 0x3003)
   176  	}
   177  	if hdr.CfgRegionOffset != 0x6b4 {
   178  		t.Errorf("Invalid cfg region offset %#x; want %#x", hdr.CfgRegionOffset, 0x6b4)
   179  	}
   180  	if hdr.CfgRegionSize != 0xee0 {
   181  		t.Errorf("Invalid cfg region size %#x; want %#x", hdr.CfgRegionSize, 0xee0)
   182  	}
   183  	if hdr.TempRAMInitEntryOffset != 0 {
   184  		t.Errorf("Invalid temp RAM init entry offset %#x; want %#x", hdr.TempRAMInitEntryOffset, 0)
   185  	}
   186  	if hdr.NotifyPhaseEntryOffset != 0x1d8 {
   187  		t.Errorf("Invalid notify phase entry offset %#x; want %#x", hdr.NotifyPhaseEntryOffset, 0x1d8)
   188  	}
   189  	if hdr.FSPMemoryInitEntryOffset != 0x0 {
   190  		t.Errorf("Invalid FSP memory init entry offset %#x; want %#x", hdr.FSPMemoryInitEntryOffset, 0x0)
   191  	}
   192  	if hdr.TempRAMExitEntryOffset != 0x0 {
   193  		t.Errorf("Invalid temp RAM exit entry offset %#x; want %#x", hdr.TempRAMExitEntryOffset, 0x0)
   194  	}
   195  	if hdr.FSPSiliconInitEntryOffset != 0x1e2 {
   196  		t.Errorf("Invalid silicon init entry offset %#x; want %#x", hdr.FSPSiliconInitEntryOffset, 0x1e2)
   197  	}
   198  	if hdr.FspMultiPhaseSiInitEntryOffset != 0x1ec {
   199  		t.Errorf("Invalid Multi Phase Si entry offset %#x; want %#x", hdr.FspMultiPhaseSiInitEntryOffset, 0x1ec)
   200  	}
   201  }
   202  
   203  func TestNewInfoHeaderRev6(t *testing.T) {
   204  	hdr, err := NewInfoHeader(FSPTestHeaderRev6)
   205  	if err != nil {
   206  		t.Errorf("NewInfoHeader failed to parse FSP header: %v", err)
   207  	}
   208  	if hdr.Signature != Signature {
   209  		t.Errorf("Invalid signature %v; want %v", hdr.Signature, Signature)
   210  	}
   211  	if hdr.HeaderLength != HeaderV6Length {
   212  		t.Errorf("Invalid header length %d; want %d", hdr.HeaderLength, HeaderV6Length)
   213  	}
   214  	if hdr.SpecVersion != SpecVersion(0x23) {
   215  		t.Errorf("Invalid spec version %s; want %s", hdr.SpecVersion, SpecVersion(0x23))
   216  	}
   217  	if hdr.HeaderRevision != 6 {
   218  		t.Errorf("Invalid header revision %d; want %d", hdr.HeaderRevision, 6)
   219  	}
   220  	if hdr.ImageRevision != ImageRevision(0x100010001020F) {
   221  		t.Errorf("Invalid image revision %s; want %s", hdr.ImageRevision, ImageRevision(0x100010001020F))
   222  	}
   223  	if !bytes.Equal(hdr.ImageID[:], []byte("$SPR-SP$")) {
   224  		t.Errorf("Invalid image ID %s; want %s", hdr.ImageID, "$SPR-SP$")
   225  	}
   226  	if hdr.ImageSize != 0x8000 {
   227  		t.Errorf("Invalid image size %#x; want %#x", hdr.ImageSize, 0x8000)
   228  	}
   229  	if hdr.ImageBase != 0xfffe0000 {
   230  		t.Errorf("Invalid image base %#x; want %#x", hdr.ImageBase, 0xfffe0000)
   231  	}
   232  	if hdr.ImageAttribute != 0x2 {
   233  		t.Errorf("Invalid image attribute %#x; want %#x", int(hdr.ImageAttribute), 0x2)
   234  	}
   235  	if hdr.ComponentAttribute != 0x1000 {
   236  		t.Errorf("Invalid component attribute %#x; want %#x", int(hdr.ComponentAttribute), 0x1000)
   237  	}
   238  	if hdr.CfgRegionOffset != 0x24c {
   239  		t.Errorf("Invalid cfg region offset %#x; want %#x", hdr.CfgRegionOffset, 0x24c)
   240  	}
   241  	if hdr.CfgRegionSize != 0x68 {
   242  		t.Errorf("Invalid cfg region size %#x; want %#x", hdr.CfgRegionSize, 0x68)
   243  	}
   244  	if hdr.TempRAMInitEntryOffset != 0x2411 {
   245  		t.Errorf("Invalid temp RAM init entry offset %#x; want %#x", hdr.TempRAMInitEntryOffset, 0x2411)
   246  	}
   247  	if hdr.NotifyPhaseEntryOffset != 0 {
   248  		t.Errorf("Invalid notify phase entry offset %#x; want %#x", hdr.NotifyPhaseEntryOffset, 0)
   249  	}
   250  	if hdr.FSPMemoryInitEntryOffset != 0x0 {
   251  		t.Errorf("Invalid FSP memory init entry offset %#x; want %#x", hdr.FSPMemoryInitEntryOffset, 0x0)
   252  	}
   253  	if hdr.TempRAMExitEntryOffset != 0x0 {
   254  		t.Errorf("Invalid temp RAM exit entry offset %#x; want %#x", hdr.TempRAMExitEntryOffset, 0x0)
   255  	}
   256  	if hdr.FSPSiliconInitEntryOffset != 0x0 {
   257  		t.Errorf("Invalid silicon init entry offset %#x; want %#x", hdr.FSPSiliconInitEntryOffset, 0x0)
   258  	}
   259  	if hdr.FspMultiPhaseSiInitEntryOffset != 0 {
   260  		t.Errorf("Invalid Multi Phase Si entry offset %#x; want %#x", hdr.FspMultiPhaseSiInitEntryOffset, 0)
   261  	}
   262  }
   263  
   264  func TestErrorPath(t *testing.T) {
   265  	// Header too small
   266  	tmp := make([]byte, len(FSPTestHeaderRev3))
   267  	copy(tmp, FSPTestHeaderRev3)
   268  	_, err := NewInfoHeader(tmp[:len(tmp)-1])
   269  	if err == nil {
   270  		t.Errorf("Expected an error")
   271  	}
   272  
   273  	copy(tmp, FSPTestHeaderRev3)
   274  	_, err = NewInfoHeader(tmp[:11])
   275  	if err == nil {
   276  		t.Errorf("Expected an error")
   277  	}
   278  
   279  	// Signature corrupted
   280  	copy(tmp, FSPTestHeaderRev3)
   281  	tmp[0] = 0
   282  	_, err = NewInfoHeader(tmp)
   283  	if err == nil {
   284  		t.Errorf("Expected an error")
   285  	}
   286  
   287  	// Spec version too small
   288  	copy(tmp, FSPTestHeaderRev3)
   289  	tmp[10] = 0x10
   290  	_, err = NewInfoHeader(tmp)
   291  	if err == nil {
   292  		t.Errorf("Expected an error")
   293  	}
   294  
   295  	// Spec version too big
   296  	copy(tmp, FSPTestHeaderRev3)
   297  	tmp[10] = 0x30
   298  	_, err = NewInfoHeader(tmp)
   299  	if err == nil {
   300  		t.Errorf("Expected an error")
   301  	}
   302  
   303  	// Header too small
   304  	copy(tmp, FSPTestHeaderRev3)
   305  	tmp[4] = byte(len(tmp) - 1)
   306  	tmp[5] = 0
   307  	tmp[6] = 0
   308  	tmp[7] = 0
   309  	_, err = NewInfoHeader(tmp)
   310  	if err == nil {
   311  		t.Errorf("Expected an error")
   312  	}
   313  
   314  	// Header Revision too small
   315  	copy(tmp, FSPTestHeaderRev3)
   316  	tmp[11] = 2
   317  	_, err = NewInfoHeader(tmp)
   318  	if err == nil {
   319  		t.Errorf("Expected an error")
   320  	}
   321  }
   322  
   323  func TestComponentAttribute(t *testing.T) {
   324  	ca := ComponentAttribute(0x3003)
   325  	if ca.IsDebugBuild() {
   326  		t.Errorf("Invalid component attribute: got debug build; want release build")
   327  	}
   328  	if ca.IsTestRelease() {
   329  		t.Errorf("Invalid component attribute: got test release; want official release")
   330  	}
   331  	if ca.Type() != TypeS {
   332  		t.Errorf("Invalid FSP type: got %v; want %v", ca.Type(), TypeS)
   333  	}
   334  	// test FSP type reserved
   335  	ca = ComponentAttribute(0xffff)
   336  	if ca.Type() != TypeReserved {
   337  		t.Errorf("Invalid FSP type: got %v; want %v", ca.Type(), TypeReserved)
   338  	}
   339  }
   340  
   341  func TestImageAttribute(t *testing.T) {
   342  	// graphics display not supported, dispatch mode not supported
   343  	ia := ImageAttribute(0)
   344  	if ia.IsGraphicsDisplaySupported() {
   345  		t.Errorf("Expected false, got true")
   346  	}
   347  	// graphics display supported
   348  	ia = ImageAttribute(1)
   349  	if !ia.IsGraphicsDisplaySupported() {
   350  		t.Errorf("Expected true, got false")
   351  	}
   352  	// dispatch mode supported
   353  	ia = ImageAttribute(2)
   354  	if !ia.IsDispatchModeSupported() {
   355  		t.Errorf("Expected true, got false")
   356  	}
   357  	if ia.IsGraphicsDisplaySupported() {
   358  		t.Errorf("Expected false, got true")
   359  	}
   360  	// graphics display supported, dispatch mode supported
   361  	ia = ImageAttribute(3)
   362  	if !ia.IsDispatchModeSupported() || !ia.IsGraphicsDisplaySupported() {
   363  		t.Errorf("Expected true, got false")
   364  	}
   365  }
   366  
   367  func TestNewInfoHeaderShortHeader(t *testing.T) {
   368  	_, err := NewInfoHeader([]byte{})
   369  	if err == nil {
   370  		t.Errorf("Expected error, got nil")
   371  	}
   372  }
   373  
   374  func TestNewInfoHeaderInvalidSignature(t *testing.T) {
   375  	_, err := NewInfoHeader(bytes.Repeat([]byte{0xaa}, FixedInfoHeaderLength))
   376  	if err == nil {
   377  		t.Errorf("Expected error, got nil")
   378  	}
   379  }
   380  
   381  func TestNewInfoHeaderNonZeroReserved1(t *testing.T) {
   382  	_, err := NewInfoHeader(append(Signature[:], bytes.Repeat([]byte{0xaa}, FixedInfoHeaderLength)...))
   383  	if err == nil {
   384  		t.Errorf("Expected error, got nil")
   385  	}
   386  }