Skip to content

Commit 7495fab

Browse files
rihter007orangecms
authored andcommitted
Add checksum calculation for AMD's BIOS and PSP Directories
Signed-off-by: Ilya <rihter007@inbox.ru>
1 parent f52d095 commit 7495fab

4 files changed

Lines changed: 108 additions & 4 deletions

File tree

pkg/amd/manifest/bios_directory_table_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
var biosDirectoryTableDataChunk = []byte{
1313
0x24, 0x42, 0x48, 0x44,
14-
0xee, 0x7f, 0xd9, 0xab,
14+
0xd0, 0x75, 0xc5, 0xac,
1515
0x01, 0x00, 0x00, 0x00,
1616
0x40, 0x04, 0x00, 0x20,
1717

@@ -79,6 +79,9 @@ func TestBiosDirectoryTableParsing(t *testing.T) {
7979
if table.BIOSCookie != BIOSDirectoryTableCookie {
8080
t.Errorf("BIOSCookie is incorrect: %d, expected: %d", table.BIOSCookie, BIOSDirectoryTableCookie)
8181
}
82+
if table.Checksum != 0xacc575d0 {
83+
t.Errorf("Checksum is incorrect: %d, expected: %d", table.Checksum, 0xacc575d0)
84+
}
8285
if table.TotalEntries != 1 {
8386
t.Errorf("TotalEntries is incorrect: %d, expected: %d", table.TotalEntries, 1)
8487
}

pkg/amd/manifest/checksum.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
const (
8+
biosDirectoryChecksumDataOffset = 8
9+
pspDirectoryChecksumDataOffset = 8
10+
)
11+
12+
// CalculateBiosDirectoryCheckSum calculates expected checksum of BIOS Directory represented in serialised form
13+
func CalculateBiosDirectoryCheckSum(biosDirRaw []byte) uint32 {
14+
return fletcherCRC32(biosDirRaw[biosDirectoryChecksumDataOffset:])
15+
}
16+
17+
// CalculatePSPDirectoryCheckSum calculates expected checksum of PSP Directory represented in serialised form
18+
func CalculatePSPDirectoryCheckSum(pspDirRaw []byte) uint32 {
19+
return fletcherCRC32(pspDirRaw[pspDirectoryChecksumDataOffset:])
20+
}
21+
22+
func fletcherCRC32(data []byte) uint32 {
23+
var c0, c1 uint32
24+
var i int
25+
l := (len(data) + 1) & ^1
26+
27+
for l > 0 {
28+
blockLen := l
29+
if blockLen > 360*2 {
30+
blockLen = 360 * 2
31+
}
32+
l -= blockLen
33+
34+
for {
35+
val := uint16(data[i])
36+
i++
37+
if i < len(data) {
38+
val += uint16(data[i]) << 8
39+
i++
40+
}
41+
c0 = c0 + uint32(val)
42+
c1 = c1 + c0
43+
blockLen -= 2
44+
if blockLen == 0 {
45+
break
46+
}
47+
}
48+
49+
c0 = c0 % 65535
50+
c1 = c1 % 65535
51+
}
52+
return c1<<16 | c0
53+
}

pkg/amd/manifest/checksum_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
"testing"
9+
)
10+
11+
func TestFletcherCRC32(t *testing.T) {
12+
assertEqual := func(expected, actual uint32) {
13+
if expected != actual {
14+
t.Errorf("Expected: %d, but got: %d", expected, actual)
15+
}
16+
}
17+
assertEqual(0xF04FC729, fletcherCRC32([]byte("abcde")))
18+
assertEqual(0x56502D2A, fletcherCRC32([]byte("abcdef")))
19+
assertEqual(0xEBE19591, fletcherCRC32([]byte("abcdefgh")))
20+
}
21+
22+
func TestPSPDirectoryCheckSum(t *testing.T) {
23+
actualCheckSum := CalculatePSPDirectoryCheckSum(pspDirectoryTableDataChunk)
24+
25+
table, _, err := ParsePSPDirectoryTable(pspDirectoryTableDataChunk)
26+
if err != nil {
27+
t.Fatalf("Failed to parse PSP Directory table, err: %v", err)
28+
}
29+
if table.Checksum != actualCheckSum {
30+
t.Errorf("Incorrect checksum: 0x%X, expected: 0x%X", actualCheckSum, table.Checksum)
31+
}
32+
}
33+
34+
func TestBIOSDirectoryCheckSum(t *testing.T) {
35+
actualCheckSum := CalculateBiosDirectoryCheckSum(biosDirectoryTableDataChunk)
36+
37+
table, _, err := ParseBIOSDirectoryTable(biosDirectoryTableDataChunk)
38+
if err != nil {
39+
t.Fatalf("Failed to parse PSP Directory table, err: %v", err)
40+
}
41+
if table.Checksum != actualCheckSum {
42+
t.Errorf("Incorrect checksum: 0x%X, expected: 0x%X", actualCheckSum, table.Checksum)
43+
}
44+
}

pkg/amd/manifest/psp_directory_table_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
var pspDirectoryTableDataChunk = []byte{
1313
0x24, 0x50, 0x53, 0x50,
14-
0xcf, 0x55, 0x73, 0x1b,
14+
0x57, 0x4d, 0x3f, 0xfc,
1515
0x01, 0x00, 0x00, 0x00,
1616
0x10, 0x05, 0x00, 0x20,
1717

@@ -63,7 +63,8 @@ func TestFindPSPDirectoryTable(t *testing.T) {
6363
}
6464

6565
func TestPspDirectoryTableParsing(t *testing.T) {
66-
table, length, err := ParsePSPDirectoryTable(append(pspDirectoryTableDataChunk, 0xff))
66+
data := append(pspDirectoryTableDataChunk, 0xff)
67+
table, length, err := ParsePSPDirectoryTable(data)
6768
if err != nil {
6869
t.Fatalf("Failed to parse PSP Directory table, err: %v", err)
6970
}
@@ -75,7 +76,10 @@ func TestPspDirectoryTableParsing(t *testing.T) {
7576
}
7677

7778
if table.PSPCookie != PSPDirectoryTableCookie {
78-
t.Errorf("BIOSCookie is incorrect: %d, expected: %d", table.PSPCookie, PSPDirectoryTableCookie)
79+
t.Errorf("PSPCookie is incorrect: %d, expected: %d", table.PSPCookie, PSPDirectoryTableCookie)
80+
}
81+
if table.Checksum != 0xfc3f4d57 {
82+
t.Errorf("Checksum is incorrect: %d, expected: %d", table.Checksum, 0xfc3f4d57)
7983
}
8084
if table.TotalEntries != 1 {
8185
t.Errorf("TotalEntries is incorrect: %d, expected: %d", table.TotalEntries, 1)

0 commit comments

Comments
 (0)