github.com/linuxboot/fiano@v1.2.0/pkg/amd/manifest/bios_directory_table_test.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  	"testing"
    10  )
    11  
    12  var biosDirectoryTableDataChunk = []byte{
    13  	0x24, 0x42, 0x48, 0x44,
    14  	0xd0, 0x75, 0xc5, 0xac,
    15  	0x01, 0x00, 0x00, 0x00,
    16  	0x40, 0x04, 0x00, 0x20,
    17  
    18  	0x68,
    19  	0x00,
    20  	0x10,
    21  	0x01,
    22  	0x00, 0x20, 0x00, 0x00,
    23  	0x00, 0x30, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
    24  	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    25  }
    26  
    27  func TestBIOSDirectoryTableHeaderSize(t *testing.T) {
    28  	const expectedBIOSDirectoryTableHeaderSize = 0x10
    29  	actualSize := binary.Size(BIOSDirectoryTableHeader{})
    30  	if actualSize != expectedBIOSDirectoryTableHeaderSize {
    31  		t.Errorf("BIOSDirectoryTableHeader is incorrect: %d, expected %d", actualSize, expectedBIOSDirectoryTableHeaderSize)
    32  	}
    33  }
    34  
    35  func TestFindBIOSDirectoryTable(t *testing.T) {
    36  	firmwareChunk := []byte{
    37  		0x12, 0x00, 0x15, 0x00, 0x15, // some prefix
    38  	}
    39  
    40  	t.Run("no_bios_table_cookie", func(t *testing.T) {
    41  		table, _, err := FindBIOSDirectoryTable(firmwareChunk)
    42  		if err == nil {
    43  			t.Errorf("Expected an error when finding bios directory table in a broken firmware")
    44  		}
    45  		if table != nil {
    46  			t.Errorf("Returned BIOS Directory table is not nil")
    47  		}
    48  	})
    49  
    50  	t.Run("bios_table_cookie_found", func(t *testing.T) {
    51  		table, r, err := FindBIOSDirectoryTable(append(firmwareChunk, biosDirectoryTableDataChunk...))
    52  		if err != nil {
    53  			t.Fatalf("Unexecpted error when finding BIOS Directory table: %v", err)
    54  		}
    55  		if r.Offset != uint64(len(firmwareChunk)) {
    56  			t.Errorf("BIOS Directory Table address is incorrect: %d, expected: %d", r.Offset, uint64(len(firmwareChunk)))
    57  		}
    58  		if r.Length != uint64(len(biosDirectoryTableDataChunk)) {
    59  			t.Errorf("BIOS Directory Table size is incorrect: %d, expected: %d", r.Length, uint64(len(biosDirectoryTableDataChunk)))
    60  		}
    61  		if table == nil {
    62  			t.Errorf("Returned BIOS Directory table is nil")
    63  		}
    64  	})
    65  }
    66  
    67  func TestBiosDirectoryTableParsing(t *testing.T) {
    68  	table, readBytes, err := ParseBIOSDirectoryTable(append(biosDirectoryTableDataChunk, 0xff))
    69  	if err != nil {
    70  		t.Fatalf("Failed to parse BIOS Directory table, err: %v", err)
    71  	}
    72  	if readBytes != uint64(len(biosDirectoryTableDataChunk)) {
    73  		t.Errorf("BIOS Directory table read bytes is incorrect: %d, expected: %d", readBytes, len(biosDirectoryTableDataChunk))
    74  	}
    75  	if table == nil {
    76  		t.Fatalf("result BIOS Directory table is nil")
    77  	}
    78  
    79  	if table.BIOSCookie != BIOSDirectoryTableCookie {
    80  		t.Errorf("BIOSCookie is incorrect: %d, expected: %d", table.BIOSCookie, BIOSDirectoryTableCookie)
    81  	}
    82  	if table.Checksum != 0xacc575d0 {
    83  		t.Errorf("Checksum is incorrect: %d, expected: %d", table.Checksum, 0xacc575d0)
    84  	}
    85  	if table.TotalEntries != 1 {
    86  		t.Errorf("TotalEntries is incorrect: %d, expected: %d", table.TotalEntries, 1)
    87  	}
    88  	if len(table.Entries) != 1 {
    89  		t.Fatalf("Result number of entries is incorrect: %d, expected: %d", len(table.Entries), 1)
    90  	}
    91  
    92  	entry := table.Entries[0]
    93  	if entry.Type != 0x68 {
    94  		t.Errorf("Table entry [0] type is incorrect: %d, expected: %d", table.Entries[0].Type, 0x68)
    95  	}
    96  	if entry.ResetImage {
    97  		t.Errorf("Table entry [0] reset image is incorrect, expected false")
    98  	}
    99  	if entry.CopyImage {
   100  		t.Errorf("Table entry [0] copy image is incorrect, expected false")
   101  	}
   102  	if entry.ReadOnly {
   103  		t.Errorf("Table entry [0] read only is incorrect, expected false")
   104  	}
   105  	if entry.Compressed {
   106  		t.Errorf("Table entry [0] compress is incorrect, expected false")
   107  	}
   108  	if entry.Instance != 1 {
   109  		t.Errorf("Table entry [0] instance is incorrect, expected 1, got: %d", entry.Instance)
   110  	}
   111  	if entry.Subprogram != 1 {
   112  		t.Errorf("Table entry [0] subprogram is incorrect, expected 1, got: %d", entry.Subprogram)
   113  	}
   114  	if entry.RomID != 0 {
   115  		t.Errorf("Table entry [0] subprogram is incorrect, expected 9, got: %d", entry.RomID)
   116  	}
   117  	if entry.SourceAddress != 0x173000 {
   118  		t.Errorf("Table entry [0] source address is incorrect: %x, expected: 0x173000",
   119  			table.Entries[0].SourceAddress)
   120  	}
   121  	if entry.DestinationAddress != 0xffffffffffffffff {
   122  		t.Errorf("Table entry [0] destination address is incorrect: %x, expected: 0xffffffffffffffff",
   123  			table.Entries[0].DestinationAddress)
   124  	}
   125  }
   126  
   127  func TestBrokenTotalEntriesBiosDirectoryParsing(t *testing.T) {
   128  	biosDirectoryTableData := make([]byte, len(biosDirectoryTableDataChunk))
   129  	copy(biosDirectoryTableData, biosDirectoryTableDataChunk)
   130  
   131  	// 8 is offset of TotalEntries field
   132  	biosDirectoryTableData[8] = 0xff
   133  	biosDirectoryTableData[9] = 0xff
   134  	biosDirectoryTableData[10] = 0xff
   135  	biosDirectoryTableData[11] = 0xff
   136  
   137  	_, _, err := ParseBIOSDirectoryTable(biosDirectoryTableData)
   138  	if err == nil {
   139  		t.Errorf("expected error when parsing incorrect psp directory table contents")
   140  	}
   141  }