Skip to content

Commit

Permalink
Add default uint field value support
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Mar 8, 2024
1 parent eff17ce commit 9221d19
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
39 changes: 39 additions & 0 deletions dmidecode/struct_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,47 @@ func parseStruct(t *smbios.Table, off int, complete bool, sp interface{}) (int,
return off, fmt.Errorf("failed to parse %s.%s: %s", svtn, f.Name, verr)
}
}

if complete && i < sv.NumField() {
return off, fmt.Errorf("%s incomplete, got %d of %d fields", svtn, i, sv.NumField())
}

// Fill in defaults
for ; i < sv.NumField(); i++ {
f := sv.Type().Field(i)
fv := sv.Field(i)
ft := fv.Type()
tags := f.Tag.Get(fieldTagKey)
// fmt.Printf("XX %02Xh f %s t %s k %s %s\n", off, f.Name, f.Type.Name(), fv.Kind(), tags)
// Check tags first
ignore := false
var defValue uint64
for _, tag := range strings.Split(tags, ",") {
tp := strings.Split(tag, "=")
switch tp[0] {
case "-":
ignore = true
case "skip":
numBytes, _ := strconv.Atoi(tp[1])
off += numBytes

Check warning on line 129 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L125-L129

Added lines #L125 - L129 were not covered by tests
case "default":
defValue, _ = strconv.ParseUint(tp[1], 0, 64)
}
}
if ignore {
continue

Check warning on line 135 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L135

Added line #L135 was not covered by tests
}
switch fv.Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fv.SetUint(defValue)
off += int(ft.Size())
case reflect.Struct:
off, err := parseStruct(t, off, true /* complete */, fv)
if err != nil {
return off, err

Check warning on line 144 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L141-L144

Added lines #L141 - L144 were not covered by tests
}
}
}

return off, nil
}
8 changes: 6 additions & 2 deletions dmidecode/struct_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ func TestParseStructUnsupported(t *testing.T) {
}
}

type supportedTypes struct {
smbios.Table
SupportedField uint64
}

func TestParseStructSupported(t *testing.T) {
buffer := []byte{
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
}
table := smbios.Table{
Data: buffer,
}
unknownType := &UnknownTypes{
unknownType := &supportedTypes{
Table: table,
}

off, err := parseStruct(&table, 0, false, unknownType)
if err != nil {
t.Errorf("TestParseStructUnsupported : parseStruct() = %d, '%v' want: 'nil'", off, err)
Expand Down
2 changes: 1 addition & 1 deletion dmidecode/type0_bios_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ParseBIOSInfo(t *smbios.Table) (*BIOSInfo, error) {
return nil, fmt.Errorf("%w: %d", ErrUnexpectedTableType, t.Type)
}
if t.Length < 0x12 {
return nil, io.ErrUnexpectedEOF
return nil, fmt.Errorf("%w: BIOS info table must be at least %d bytes", io.ErrUnexpectedEOF, 0x12)
}
bi := &BIOSInfo{
Header: t.Header,
Expand Down
38 changes: 38 additions & 0 deletions dmidecode/type0_bios_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,44 @@ func TestParseBIOSInfo(t *testing.T) {
},
err: ErrUnexpectedTableType,
},
{
name: "BIOS short",
table: &smbios.Table{
Header: smbios.Header{
Length: 18,
Type: smbios.TableTypeBIOSInfo,
},
Data: []byte{
0x00, // vendor string
0x01, // version string
0x02, 0x03,
0x02, // release date string
0x00, // rom size
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bios chars
0x00,
0x00,
},
Strings: []string{
"version!",
"release date!",
},
},
want: &BIOSInfo{
Header: smbios.Header{
Length: 18,
Type: smbios.TableTypeBIOSInfo,
},
Version: "version!",
StartingAddressSegment: 0x302,
ReleaseDate: "release date!",
Characteristics: 0x1,
BIOSMajor: 0xff,
BIOSMinor: 0xff,
ECMajor: 0xff,
ECMinor: 0xff,
ExtendedROMSize: 0,
},
},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseBIOSInfo(tt.table)
Expand Down

0 comments on commit 9221d19

Please sign in to comment.