dt

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2022 License: BSD-3-Clause Imports: 12 Imported by: 21

Documentation

Overview

Package dt contains utilities for device tree.

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 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) (*FDT, error)

LoadFDT loads a flattened device tree from current running system.

It first tries to load it from given io.ReaderAt, then from /sys/firmware/fdt.

func ReadFDT

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

ReadFDT reads FDT from an io.ReadSeeker.

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 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 (*Node) Find

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

Find finds a Node starting at a node, given a matching function.

func (*Node) FindAll

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

FindAll returns all Node starting at a node, given a matching function.

func (*Node) LookProperty

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

LookProperty finds a property by name.

func (*Node) NodeByName

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

NodeByName uses Find to find a node by name.

func (*Node) RemoveProperty added in v0.9.0

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

RemoveProperty deletes a property by name.

func (*Node) String

func (n *Node) String() string

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 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 (*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