dt

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2024 License: BSD-3-Clause Imports: 12 Imported by: 21

Documentation

Overview

Package dt contains utilities for device tree.

Package dt contains utilities for device tree reading on Linux.

Index

Constants

View Source
const (
	// Magic value seen in the FDT Header.
	Magic uint32 = 0xd00dfeed

	// MaxTotalSize is a limitation imposed by this implementation. This
	// prevents the integers from wrapping around. Typically, the total size is
	// a few megabytes, so this is not restrictive.
	MaxTotalSize = 1024 * 1024 * 1024
)

Variables

View Source
var ErrNoValidReaders = errors.New("no FDT readers succeeded")

ErrNoValidReaders indicates that no readers succeeded.

View Source
var (
	ErrPropertyRegionInvalid = errors.New("property value is not <u64x2>")
)
View Source
var StandardPropertyTypes = map[string]PropertyType{
	"compatible":     StringListType,
	"model":          StringType,
	"phandle":        PHandleType,
	"status":         StringType,
	"#address-cells": U32Type,
	"#size-cells":    U32Type,
	"reg":            PropEncodedArrayType,
	"virtual-reg":    U32Type,
	"ranges":         PropEncodedArrayType,
	"dma-ranges":     PropEncodedArrayType,
	"name":           StringType,
	"device_tree":    StringType,
}

StandardPropertyTypes maps properties to values as defined by the spec.

Functions

This section is empty.

Types

type Empty

type Empty struct{}

Empty represents an empty Device Tree value.

type FDT

type FDT struct {
	Header         Header
	ReserveEntries []ReserveEntry
	RootNode       *Node
}

FDT contains the parsed contents of a Flattend Device Tree (.dtb).

The format is relatively simple and defined in chapter 5 of the Devicetree Specification Release 0.2.

See: https://github.com/devicetree-org/devicetree-specification/releases/tag/v0.2

This package is compatible with version 16 and 17 of DTSpec.

func LoadFDT added in v0.9.0

func LoadFDT(dtb io.ReaderAt, names ...string) (*FDT, error)

LoadFDT loads a flattened device tree from current running system.

It first tries to load it from given io.ReaderAt, then from that passed-in file name. If there are not passed-in file names, it will try sysfsFDT.

BUGS: It is a bit clunky due to its origins; in the original version it even had a race. hopefully we can deprecate it in a future u-root release.

func New added in v0.11.0

func New(readers ...FDTReader) (*FDT, error)

New returns a new FDT, trying each FDTReader in turn until it succeeds or all have failed. It will return the last error. TODO: once we move to go 1.20, use the new error tree support.

func ReadFDT

func ReadFDT(f io.ReadSeeker) (*FDT, error)

ReadFDT reads an FDT from an io.ReadSeeker.

func ReadFile added in v0.11.0

func ReadFile(n string) (*FDT, error)

ReadFile accepts a file name and returns an *FDT or error.

func (*FDT) NodeByName

func (fdt *FDT) NodeByName(name string) (*Node, bool)

NodeByName finds a node by name.

func (*FDT) PrintDTS

func (fdt *FDT) PrintDTS(f io.Writer) error

PrintDTS prints the FDT in the .dts format. TODO: not yet implemented

func (*FDT) Root

func (fdt *FDT) Root() *NodeWalk

Root returns the Root node from an FDT to start the walk.

func (*FDT) String

func (fdt *FDT) String() string

String implements String() for an FDT

func (*FDT) Write

func (fdt *FDT) Write(f io.Writer) (int, error)

Write marshals the FDT to an io.Writer and returns the size.

type FDTReader added in v0.11.0

type FDTReader func() (*FDT, error)

FDTReader is a function type with no args that returns a *FDT or an error.

func WithFileName added in v0.11.0

func WithFileName(n string) FDTReader

WithFileName constructs an FDTReader with the provided file name.

func WithReaderAt added in v0.11.0

func WithReaderAt(r io.ReaderAt) FDTReader

WithReaderAt constructs an FDTReader with the provided io.ReaderAt.

type Header struct {
	Magic           uint32
	TotalSize       uint32
	OffDtStruct     uint32
	OffDtStrings    uint32
	OffMemRsvmap    uint32
	Version         uint32
	LastCompVersion uint32
	BootCpuidPhys   uint32
	SizeDtStrings   uint32
	SizeDtStruct    uint32
}

Header appears at offset 0.

type Node

type Node struct {
	Name       string
	Properties []Property `json:",omitempty"`
	Children   []*Node    `json:",omitempty"`
}

Node is one Node in the Device Tree.

func NewNode added in v0.13.0

func NewNode(name string, opts ...NodeOptioner) *Node

NewNode creates a node.

func (*Node) Find

func (n *Node) Find(f func(*Node) bool) (*Node, bool)

Find returns the first matching Node (recursively) starting at a node, and given a matching function.

func (*Node) FindAll

func (n *Node) FindAll(f func(*Node) bool) ([]*Node, bool)

FindAll returns all Nodes (recursively) starting at a node, and given a matching function.

func (*Node) FindFirstMatchingChildIndex added in v0.12.0

func (n *Node) FindFirstMatchingChildIndex(predicate func(*Node) bool) (int, bool)

FindFirstMatchingChildIndex returns the index of the first immediate child node satisfying the given predicate.

func (*Node) LookProperty

func (n *Node) LookProperty(name string) (*Property, bool)

LookProperty finds a property by name.

func (*Node) LookupChildByName added in v0.12.0

func (n *Node) LookupChildByName(name string) (*Node, bool)

LookupChildByName returns the immediate child node with the given name.

func (*Node) NodeByName

func (n *Node) NodeByName(name string) (*Node, bool)

NodeByName uses Find to find a node by name recursively.

func (*Node) RemoveProperty added in v0.9.0

func (n *Node) RemoveProperty(name string) bool

RemoveProperty deletes a property by name.

func (*Node) RemoveSubTreeAtIndex added in v0.12.0

func (n *Node) RemoveSubTreeAtIndex(idx int) error

RemoveSubTreeAtIndex deletes the child at the given index (and all its children).

func (*Node) String

func (n *Node) String() string

func (*Node) Update added in v0.13.0

func (n *Node) Update(prop Property) bool

Update updates an existing property with the given one, if they match in name, or adds a new property.

func (*Node) UpdateProperty added in v0.9.0

func (n *Node) UpdateProperty(name string, value []byte) bool

UpdateProperty updates a property in the node, adding it if it does not exist.

Returning boolean to indicate if the property was found.

func (*Node) Walk

func (n *Node) Walk(f func(*Node) error) error

Walk calls f on a Node and alls its descendents.

type NodeOptioner added in v0.13.0

type NodeOptioner func(n *Node)

NodeOptioner is used to modify a Node for NewNode.

func WithChildren added in v0.13.0

func WithChildren(c ...*Node) NodeOptioner

WithChildren adds childen to the node.

func WithProperty added in v0.13.0

func WithProperty(p ...Property) NodeOptioner

WithProperty adds the given properties to the node.

type NodeWalk

type NodeWalk struct {
	// contains filtered or unexported fields
}

NodeWalk is used to contain state for walking the FDT, such as an error. A Walk with a non-nil error value can not proceed. Many walks will start with a root, but it is possible to Walk to a node, make a copy of the Walk, and in that way do multiple Walks from that one node. This is very similar to how 9p clients walk 9p servers.

func (*NodeWalk) AsString

func (nq *NodeWalk) AsString() (string, error)

AsString returns the NodeWalk Name and error as a string.

func (*NodeWalk) Find

func (nq *NodeWalk) Find(f func(*Node) bool) (*Node, error)

Find returns a Node given a matching function starting at the current NodeWalk.

func (*NodeWalk) FindAll

func (nq *NodeWalk) FindAll(f func(*Node) bool) ([]*Node, error)

FindAll returns all Nodes given a matching function starting at the current NodeWalk.

func (*NodeWalk) ListChildNodes

func (nq *NodeWalk) ListChildNodes() ([]string, error)

ListChildNodes returns a string array with the Names of each child Node

func (*NodeWalk) Property

func (nq *NodeWalk) Property(name string) *PropertyWalk

Property walks from a Node to a Property of that Node, returning a PropertyWalk.

func (*NodeWalk) Walk

func (nq *NodeWalk) Walk(name string) *NodeWalk

Walk walks from a node to a named Node, returning a NodeWalk.

type PHandle

type PHandle uint32

PHandle represents a pointer to another Node.

type Property

type Property struct {
	Name  string
	Value []byte
}

Property is a name-value pair. Note the PropertyType of Value is not encoded.

func PropertyRegion added in v0.13.0

func PropertyRegion(name string, start, size uint64) Property

PropertyRegion creates a property encoding start and size of a region of memory.

func PropertyString added in v0.13.0

func PropertyString(name string, value string) Property

PropertyString creates a string property.

func PropertyU64 added in v0.13.0

func PropertyU64(name string, value uint64) Property

PropertyU64 creates a uint64 property.

func (*Property) AsEmpty

func (p *Property) AsEmpty() (Empty, error)

AsEmpty converts the property to the Go fdt.Empty type.

func (*Property) AsPHandle

func (p *Property) AsPHandle() (PHandle, error)

AsPHandle converts the property to the Go fdt.PHandle type.

func (*Property) AsPropEncodedArray

func (p *Property) AsPropEncodedArray() ([]byte, error)

AsPropEncodedArray converts the property to the Go []byte type.

func (*Property) AsRegion added in v0.9.0

func (p *Property) AsRegion() (*Region, error)

AsRegion converts the property to a Region.

func (*Property) AsString

func (p *Property) AsString() (string, error)

AsString converts the property to the Go string type. The trailing null character is stripped.

func (*Property) AsStringList

func (p *Property) AsStringList() ([]string, error)

AsStringList converts the property to the Go []string type. The trailing null character of each string is stripped.

func (*Property) AsType

func (p *Property) AsType(val PropertyType) (interface{}, error)

AsType converts a Property to a Go type using one of the AsXYX() functions. The resulting Go type is as follows:

AsType(fdt.EmptyType)            -> fdt.Empty
AsType(fdt.U32Type)              -> uint32
AsType(fdt.U64Type)              -> uint64
AsType(fdt.StringType)           -> string
AsType(fdt.PropEncodedArrayType) -> []byte
AsType(fdt.PHandleType)          -> fdt.PHandle
AsType(fdt.StringListType)       -> []string

func (*Property) AsU32

func (p *Property) AsU32() (uint32, error)

AsU32 converts the property to the Go uint32 type.

func (*Property) AsU64

func (p *Property) AsU64() (uint64, error)

AsU64 converts the property to the Go uint64 type.

func (*Property) PredictType

func (p *Property) PredictType() PropertyType

PredictType makes a prediction on what value the property contains based on its name and data. The data types are not encoded in the data structure, so some heuristics are used.

func (*Property) String

func (p *Property) String() string

type PropertyType

type PropertyType int

PropertyType is an enum of possible property types.

const (
	EmptyType PropertyType = iota
	U32Type
	U64Type
	StringType
	PropEncodedArrayType
	PHandleType
	StringListType
)

These are the possible values for PropertyType.

type PropertyWalk

type PropertyWalk struct {
	// contains filtered or unexported fields
}

PropertyWalk contains the state from a Walk to a Property.

func (*PropertyWalk) AsBytes

func (pq *PropertyWalk) AsBytes() ([]byte, error)

AsBytes returns the PropertyWalk value as a []byte.

func (*PropertyWalk) AsString

func (pq *PropertyWalk) AsString() (string, error)

AsString returns the PropertyWalk value as a string.

func (*PropertyWalk) AsU64

func (pq *PropertyWalk) AsU64() (uint64, error)

AsU64 returns the PropertyWalk value as a uint64.

type Region added in v0.9.0

type Region struct {
	Start uint64
	Size  uint64
}

Region represents a memory range.

type ReserveEntry

type ReserveEntry struct {
	Address uint64
	Size    uint64
}

ReserveEntry defines a memory region which is reserved.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL