Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshaling out #11

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,8 @@ jobs:
strategy:
matrix:
go-version: ['1.21.x', '1.22.x']
vmarch: [ 'amd64', 'arm64', 'riscv64' ]
vmarch: [ 'amd64' ]
goarm: ['']
include:
# QEMU arm -M virt seems to support only v5. GOARM default as of 1.21
# is v7.
- go-version: '1.21.x'
vmarch: 'arm'
goarm: '5'
# QEMU arm -M virt seems to support only v5. GOARM default as of 1.21
# is v7.
- go-version: '1.22.x'
vmarch: 'arm'
goarm: '5'

env:
VMTEST_ARCH: ${{ matrix.vmarch }}
Expand Down
123 changes: 117 additions & 6 deletions dmidecode/struct_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,37 @@
package dmidecode

import (
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
"strings"

"github.com/u-root/smbios"
)

// Generic errors.
var (
ErrInvalidArg = errors.New("invalid argument")
)

// We need this for testing.
type parseStructure func(t *smbios.Table, off int, complete bool, sp interface{}) (int, error)

type fieldParser interface {
ParseField(t *smbios.Table, off int) (int, error)
}

type fieldWriter interface {
WriteField(t *smbios.Table) (int, error)
}

var (
fieldTagKey = "smbios" // Tag key for annotations.
fieldParserInterfaceType = reflect.TypeOf((*fieldParser)(nil)).Elem()
fieldWriterInterfaceType = reflect.TypeOf((*fieldWriter)(nil)).Elem()
)

func parseStruct(t *smbios.Table, off int, complete bool, sp interface{}) (int, error) {
Expand All @@ -49,9 +61,6 @@
switch tp[0] {
case "-":
ignore = true
case "skip":
numBytes, _ := strconv.Atoi(tp[1])
off += numBytes
}
}
if ignore {
Expand Down Expand Up @@ -83,6 +92,11 @@
v, _ := t.GetStringAt(off)
fv.SetString(v)
off++
case reflect.Array:
v, err := t.GetBytesAt(off, fv.Len())
reflect.Copy(fv, reflect.ValueOf(v))
verr = err
off += fv.Len()
default:
if reflect.PtrTo(ft).Implements(fieldParserInterfaceType) {
off, err = fv.Addr().Interface().(fieldParser).ParseField(t, off)
Expand Down Expand Up @@ -125,9 +139,6 @@
switch tp[0] {
case "-":
ignore = true
case "skip":
numBytes, _ := strconv.Atoi(tp[1])
off += numBytes
case "default":
defValue, _ = strconv.ParseUint(tp[1], 0, 64)
}
Expand All @@ -139,6 +150,10 @@
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fv.SetUint(defValue)
off += int(ft.Size())

case reflect.Array:
return off, fmt.Errorf("%w: array does not support default values", ErrInvalidArg)

case reflect.Struct:
off, err := parseStruct(t, off, false /* complete */, fv)
if err != nil {
Expand All @@ -149,3 +164,99 @@

return off, nil
}

// Table is an SMBIOS table.
type Table interface {
Typ() smbios.TableType
}

const headerLen = 4

// ToTable converts a struct to an smbios.Table.
func ToTable(val Table, handle uint16) (*smbios.Table, error) {
t := smbios.Table{
Header: smbios.Header{
Type: val.Typ(),
Handle: handle,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we have to pass in handle when we can directly obtain that from val.Handle?

},
}
m, err := toTable(&t, val)
if err != nil {
return nil, err

Check warning on line 185 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L185

Added line #L185 was not covered by tests
}
if m+headerLen > math.MaxUint8 {
return nil, fmt.Errorf("%w: table is too long, got %d bytes, max is 256", ErrInvalidArg, m+headerLen)
}
t.Length = uint8(m + headerLen)
return &t, nil
}

func toTable(t *smbios.Table, sp any) (int, error) {
sv, ok := sp.(reflect.Value)
if !ok {
sv = reflect.Indirect(reflect.ValueOf(sp)) // must be a pointer to struct then, dereference it
}
svtn := sv.Type().Name()

n := 0
for i := 0; i < sv.NumField(); i++ {
f := sv.Type().Field(i)
fv := sv.Field(i)
ft := fv.Type()
tags := f.Tag.Get(fieldTagKey)
// Check tags first
ignore := false
for _, tag := range strings.Split(tags, ",") {
tp := strings.Split(tag, "=")
switch tp[0] { //nolint
case "-":
ignore = true
}
}
if ignore {
continue
}
// fieldWriter takes precedence.
if reflect.PtrTo(ft).Implements(fieldWriterInterfaceType) {
m, err := fv.Addr().Interface().(fieldWriter).WriteField(t)
n += m
if err != nil {
return n, err

Check warning on line 224 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L224

Added line #L224 was not covered by tests
}
continue
}

var verr error
switch fv.Kind() {
case reflect.Uint8:
t.WriteByte(uint8(fv.Uint()))
n++
case reflect.Uint16:
t.WriteWord(uint16(fv.Uint()))
n += 2
case reflect.Uint32:
t.WriteDWord(uint32(fv.Uint()))
n += 4
case reflect.Uint64:
t.WriteQWord(fv.Uint())
n += 8
case reflect.String:
t.WriteString(fv.String())
n++
case reflect.Array:
t.WriteBytes(fv.Slice(0, fv.Len()).Bytes())
n += fv.Len()
case reflect.Struct:
var m int
// If it's a struct, just invoke toTable recursively.
m, verr = toTable(t, fv)
n += m
default:
return n, fmt.Errorf("%s.%s: unsupported type %s", svtn, f.Name, fv.Kind())

Check warning on line 255 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L254-L255

Added lines #L254 - L255 were not covered by tests
}
if verr != nil {
return n, fmt.Errorf("failed to parse %s.%s: %w", svtn, f.Name, verr)

Check warning on line 258 in dmidecode/struct_parser.go

View check run for this annotation

Codecov / codecov/patch

dmidecode/struct_parser.go#L258

Added line #L258 was not covered by tests
}
}
return n, nil
}
Loading
Loading