github.com/rminnich/u-root@v7.0.0+incompatible/pkg/smbios/entry64.go (about)

     1  // Copyright 2016-2019 the u-root 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 smbios
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/binary"
    10  	"fmt"
    11  )
    12  
    13  // Entry64 is the SMBIOS 64-Bit entry point structure, described in DSP0134 5.2.2.
    14  type Entry64 struct {
    15  	Anchor             [5]uint8
    16  	Checksum           uint8
    17  	Length             uint8
    18  	SMBIOSMajorVersion uint8
    19  	SMBIOSMinorVersion uint8
    20  	SMBIOSDocRev       uint8
    21  	Revision           uint8
    22  	Reserved           uint8
    23  	StructMaxSize      uint32
    24  	StructTableAddr    uint64
    25  }
    26  
    27  // UnmarshalBinary unmarshals the SMBIOS 64-Bit entry point structure from binary data.
    28  func (e *Entry64) UnmarshalBinary(data []byte) error {
    29  	if len(data) < 0x18 {
    30  		return fmt.Errorf("invalid entry point stucture length %d", len(data))
    31  	}
    32  	if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, e); err != nil {
    33  		return err
    34  	}
    35  	if !bytes.Equal(e.Anchor[:], []byte("_SM3_")) {
    36  		return fmt.Errorf("invalid anchor string %q", string(e.Anchor[:]))
    37  	}
    38  	if int(e.Length) != 0x18 {
    39  		return fmt.Errorf("length mismatch: %d vs %d", e.Length, len(data))
    40  	}
    41  	cs := calcChecksum(data[:e.Length], 5)
    42  	if e.Checksum != cs {
    43  		return fmt.Errorf("checksum mismatch: 0x%02x vs 0x%02x", e.Checksum, cs)
    44  	}
    45  	return nil
    46  }
    47  
    48  // MarshalBinary marshals the SMBIOS 64-Bit entry point structure to binary data.
    49  func (e *Entry64) MarshalBinary() ([]byte, error) {
    50  	buf := bytes.NewBuffer(nil)
    51  	if err := binary.Write(buf, binary.LittleEndian, e); err != nil {
    52  		return nil, err
    53  	}
    54  	// Adjust checksum.
    55  	data := buf.Bytes()
    56  	data[5] = calcChecksum(data, 5)
    57  	return data, nil
    58  }