github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/pkg/acpi/generic.go (about)

     1  // Copyright 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 acpi
     6  
     7  import (
     8  	"encoding/binary"
     9  )
    10  
    11  // Generic is the generic ACPI table, with a Header and data
    12  // This makes it possible for users to change certain parts
    13  // of the Header (e.g. vendor id) without touching the data.
    14  // When the table is Marshal'ed out checksums are regenerated.
    15  type Generic struct {
    16  	Header
    17  	data []byte
    18  }
    19  
    20  var _ = Tabler(&Generic{})
    21  
    22  // NewGeneric creates a new Generic table from a byte slice.
    23  func NewGeneric(b []byte) (Tabler, error) {
    24  	t, err := NewRaw(b)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return &Generic{Header: *GetHeader(t), data: t.AllData()}, nil
    29  }
    30  
    31  // Marshal marshals Generic tables. The main use of this function
    32  // is when you want to tweak the header a bit; you can convert a Raw
    33  // table to a Generic table, do what you wish, and write it out.
    34  func (g *Generic) Marshal() ([]byte, error) {
    35  	// Marshal the header, as it may be changed.
    36  	h, err := g.Header.Marshal()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	// Append only the table data.
    41  	h = append(h, g.TableData()...)
    42  	binary.LittleEndian.PutUint32(h[LengthOffset:], uint32(len(h)))
    43  	h[CSUMOffset] = 0
    44  	c := gencsum(h)
    45  	Debug("CSUM is %#x", c)
    46  	h[CSUMOffset] = c
    47  	return h, nil
    48  }
    49  
    50  // Len returns the length of an entire table.
    51  func (g *Generic) Len() uint32 {
    52  	return uint32(len(g.data))
    53  }
    54  
    55  // AllData returns the entire table as a byte slice.
    56  func (g *Generic) AllData() []byte {
    57  	return g.data
    58  }
    59  
    60  // TableData returns the table, minus the common ACPI header.
    61  func (g *Generic) TableData() []byte {
    62  	return g.data[HeaderLength:]
    63  }
    64  
    65  // Sig returns the table signature.
    66  func (g *Generic) Sig() string {
    67  	return string(g.Header.Sig)
    68  }
    69  
    70  // OEMID returns the table OEMID.
    71  func (g *Generic) OEMID() string {
    72  	return string(g.Header.OEMID)
    73  }
    74  
    75  // OEMTableID returns the table OEMTableID.
    76  func (g *Generic) OEMTableID() string {
    77  	return string(g.Header.OEMTableID)
    78  }
    79  
    80  // OEMRevision returns the table OEMRevision.
    81  func (g *Generic) OEMRevision() uint32 {
    82  	return g.Header.OEMRevision
    83  }
    84  
    85  // CreatorID returns the table CreatorID.
    86  func (g *Generic) CreatorID() uint32 {
    87  	return g.Header.CreatorID
    88  }
    89  
    90  // CreatorRevision returns the table CreatorRevision.
    91  func (g *Generic) CreatorRevision() uint32 {
    92  	return g.Header.CreatorRevision
    93  }
    94  
    95  // Revision returns the table Revision.
    96  func (g *Generic) Revision() uint8 {
    97  	return g.Header.Revision
    98  }
    99  
   100  // CheckSum returns the table CheckSum.
   101  func (g *Generic) CheckSum() uint8 {
   102  	return g.Header.CheckSum
   103  }