-
Notifications
You must be signed in to change notification settings - Fork 4
/
fdt.go
426 lines (363 loc) · 9.64 KB
/
fdt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Linux flattened device trees.
package fdt
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"regexp"
"strings"
"unsafe"
)
const (
magic = 0xd00dfeed
begin_node = 0x1 // Start node: full name
end_node = 0x2 // End node
prop = 0x3 // Property
nop = 0x4 // nop
end = 0x9 // End of fdt
headerVer = 17
headerLastComp = 16
headerLen = 40
)
type header struct {
Magic uint32
TotalSize uint32 // total size of DT block
OffDtStruct uint32 // offset to structure
OffDtStrings uint32 // offset to strings
OffMemRsvmap uint32 // offset to memory reserve map
Version uint32
LastCompatibleVersion uint32
// version 2 fields below
BootCpuidPhys uint32 // Which physical CPU id we're
// booting on
// version 3 fields below
SizeDtStrings uint32 // size of the strings block
// version 17 fields below
SizeDtStruct uint32 // size of the structure block
}
func (h *header) String() string {
return fmt.Sprintf("magic: 0x%x, version %d %d, total size: 0x%x, offset struct 0x%x strings 0x%x mem-reserve-map 0x%x",
h.Magic, h.Version, h.LastCompatibleVersion,
h.TotalSize, h.OffDtStruct, h.OffDtStrings, h.OffMemRsvmap)
}
type Node struct {
Name string
Depth int
Properties map[string][]byte
Children map[string]*Node
}
type Tree struct {
header
Debug bool
IsLittleEndian bool
RootNode *Node
}
var defaultTree Tree
func DefaultTree() (t *Tree) {
if defaultTree.RootNode == nil {
_ = defaultTree.ParseKernel()
}
d := defaultTree // Deliberate copy - caller can modify
if d.RootNode != nil && len(d.RootNode.Properties) != 0 &&
len(d.RootNode.Children) != 0 {
return &d
}
return nil
}
func (n *Node) String() (s string) {
if n == nil {
return "nil"
}
s = fmt.Sprintf("%*s%s: ", 2*n.Depth, " ", n.Name)
for name, value := range n.Properties {
s += fmt.Sprintf("\n%*s%s = %q", 2*(1+n.Depth), " ", name,
value)
}
for _, c := range n.Children {
s += fmt.Sprintf("\n%s", c)
}
return
}
func (t *Tree) String() string { return t.RootNode.String() }
func (t *Tree) getCell(b []byte, i int) (value int, r int) {
value = int(t.PropUint32(b[i:]))
r = i + 4
return
}
func (t *Tree) getString(b []byte, offset int) string {
o := int(t.OffDtStrings) + offset
l := bytes.IndexByte(b[o:], 0)
return string(b[o : o+l])
}
func align(x int, align int) int {
return (x + align - 1) & ^(align - 1)
}
// Read FDT header from blob and convert into
// right endian
func (t *Tree) readHeader(buf []byte) {
var err error
fh := bytes.NewReader(buf)
if t.IsLittleEndian {
err = binary.Read(fh, binary.LittleEndian, &t.header)
} else {
err = binary.Read(fh, binary.BigEndian, &t.header)
}
if err != nil {
fmt.Println("binary.ReadFdtHeader failed:", err)
}
}
func (t *Tree) Parse(buf []byte) (err error) {
h := &t.header
// Parse blob header
t.readHeader(buf)
if t.Debug {
fmt.Printf("%+v\n", h)
}
// Walk thru nodes until done
cur := int(h.OffDtStruct)
stack := []*Node{}
for {
var tag int
tag, cur = t.getCell(buf, cur)
if tag == end {
break
}
switch tag {
case begin_node:
n := &Node{}
nameLen := bytes.IndexByte(buf[cur:], 0)
n.Name = "/"
if nameLen > 0 {
n.Name = string(buf[cur : cur+nameLen])
}
if t.Debug {
fmt.Printf("BEGIN_NODE: `%s'\n", n.Name)
}
cur = align(cur+nameLen+1, 4)
stack = append(stack, n)
n.Depth = len(stack)
case end_node:
// pop node stack
var l int
if l = len(stack); l == 1 {
t.RootNode = stack[0]
} else {
c := stack[l-1]
p := stack[l-2]
if p.Children == nil {
p.Children = make(map[string]*Node)
}
p.Children[c.Name] = c
}
stack = stack[:l-1]
if t.Debug {
fmt.Println("END_NODE:")
}
case nop:
if t.Debug {
fmt.Println("NOP:")
}
case prop:
var valueSize, nameOffset int
valueSize, cur = t.getCell(buf, cur)
nameOffset, cur = t.getCell(buf, cur)
name := t.getString(buf, nameOffset)
value := buf[cur : cur+valueSize]
n := stack[len(stack)-1]
if n.Properties == nil {
n.Properties = make(map[string][]byte)
}
n.Properties[name] = value
if t.Debug {
fmt.Printf("PROP: %s = %v %q\n", name, value, string(value))
}
cur = align(cur+int(valueSize), 4)
}
}
if len(stack) != 0 {
err = errors.New("node stack not balanced")
}
return
}
func (n *Node) eachProp(propName string, propValue string, f func(n *Node, name string, value string)) {
if len(propValue) > 0 {
if value := n.Properties[propName]; strings.Contains(string(value), propValue) {
f(n, propName, string(value))
}
} else if _, present := n.Properties[propName]; present {
value := n.Properties[propName]
f(n, propName, string(value))
}
for _, c := range n.Children {
c.eachProp(propName, propValue, f)
}
}
// Call user's function for each node with given property.
func (t *Tree) EachProperty(propName string, propValue string,
f func(n *Node, name string, value string)) {
t.RootNode.eachProp(propName, propValue, f)
}
// Recursive search for node named "nodeName" and when found run f()
func (n *Node) matchNode(nodeName string, f func(n *Node)) {
if n.Name == nodeName {
f(n)
}
for _, c := range n.Children {
c.matchNode(nodeName, f)
}
}
// Find node with specified name "nodeName" then run f() on it
func (t *Tree) MatchNode(nodeName string, f func(n *Node)) {
t.RootNode.matchNode(nodeName, f)
}
// Find node of given name "nodeName"
func (n *Node) getNode(nodeName string) *Node {
if n.Name == nodeName {
return n
}
for _, c := range n.Children {
cn := c.getNode(nodeName)
if cn != nil {
return cn
}
}
return nil
}
// Run f() on every node from the starting node "n"
func (n *Node) eachNode(f func(n *Node)) {
f(n)
for _, c := range n.Children {
c.eachNode(f)
}
}
// Given a starting node name, descend that node applying f() along the way
func (t *Tree) EachNodeFrom(nodeName string, f func(n *Node)) {
tn := t.RootNode.getNode(nodeName)
if tn != nil {
tn.eachNode(f)
}
}
func (n *Node) eachRegexp(pattern *regexp.Regexp, f func(n *Node)) {
for name := range n.Properties {
if pattern.MatchString(name) {
f(n)
break
}
}
for _, c := range n.Children {
c.eachRegexp(pattern, f)
}
}
// As abote but matching property name as a regexp.
func (t *Tree) EachPropertyMatching(pattern string, f func(n *Node)) {
re := regexp.MustCompile(pattern)
t.RootNode.eachRegexp(re, f)
}
// Parses property value as 32 bit integer.
func (t *Tree) PropUint32(b []byte) (value uint32) {
if t.IsLittleEndian {
value = binary.LittleEndian.Uint32(b)
} else {
value = binary.BigEndian.Uint32(b)
}
return
}
// Property value as slice of 32 bit integers.
func (t *Tree) PropUint32Slice(b []byte) (value []uint32) {
value = make([]uint32, len(b)/4)
for i := range value {
value[i] = t.PropUint32(b[i*4:])
}
return
}
// Property value as go string.
func (t *Tree) PropString(b []byte) (s string) {
v := t.PropStringSlice(b)
return v[0]
}
// Property value as go string slice.
func (t *Tree) PropStringSlice(b []byte) (s []string) {
return strings.Split(string(b), "\x00")
}
// Write support
func (t *Tree) alignTo(b []byte, align int) []byte {
for len(b)&(align-1) != 0 {
b = append(b, 0)
}
return b
}
func (t *Tree) PropUint32ToSlice(v uint32) (r []byte) {
r = make([]byte, 4)
if t.IsLittleEndian {
binary.LittleEndian.PutUint32(r, v)
} else {
binary.BigEndian.PutUint32(r, v)
}
return r
}
func (t *Tree) PropUint64ToSlice(v uint64) (r []byte) {
r = make([]byte, 8)
if t.IsLittleEndian {
binary.LittleEndian.PutUint64(r, v)
} else {
binary.BigEndian.PutUint64(r, v)
}
return r
}
func (t *Tree) putCell(b []byte, v uint32) []byte {
return append(b, t.PropUint32ToSlice(v)...)
}
func (t *Tree) putCellUint64(b []byte, v uint64) []byte {
return append(b, t.PropUint64ToSlice(v)...)
}
func (t *Tree) putNode(b []byte, s []byte, n *Node) (bOut []byte, sOut []byte) {
b = t.putCell(b, begin_node)
if n.Name != "/" {
b = append(b, []byte(n.Name)...)
}
b = append(b, 0)
b = t.alignTo(b, 4)
for name, value := range n.Properties {
b = t.putCell(b, prop)
b = t.putCell(b, uint32(len(value)))
b = t.putCell(b, uint32(len(s)))
b = append(b, []byte(value)...)
s = append(s, []byte(name)...)
s = append(s, 0)
b = t.alignTo(b, 4)
}
for _, c := range n.Children {
b, s = t.putNode(b, s, c)
}
b = t.putCell(b, end_node)
return b, s
}
func (t *Tree) putUint32(w *uint32, v uint32) {
r := t.PropUint32ToSlice(v)
*w = *(*uint32)(unsafe.Pointer(&r[0]))
}
func (t *Tree) FlattenTreeToSlice() []byte {
h := make([]byte, headerLen) // Header block
m := make([]byte, 8*2) // Dummy memory reservation block
b := make([]byte, 0) // Structure block
s := make([]byte, 0) // String block
b, s = t.putNode(b, s, t.RootNode) // Build structure and string block
b = t.putCell(b, end) // End the tree
s = t.alignTo(s, 4) // Align string block
hdr := (*header)(unsafe.Pointer(&h[0])) // Get header pointer
t.putUint32(&hdr.Magic, magic)
t.putUint32(&hdr.TotalSize, uint32(headerLen+len(m)+len(b)+len(s)))
t.putUint32(&hdr.OffDtStruct, uint32(headerLen+len(m)))
t.putUint32(&hdr.OffDtStrings, uint32(headerLen+len(m)+len(b))) // offset to strings
t.putUint32(&hdr.OffMemRsvmap, headerLen) // offset to memory reserve map
t.putUint32(&hdr.Version, headerVer)
t.putUint32(&hdr.LastCompatibleVersion, headerLastComp)
t.putUint32(&hdr.BootCpuidPhys, 0) // Which physical CPU id we're
t.putUint32(&hdr.SizeDtStrings, uint32(len(s))) // size of the strings block
t.putUint32(&hdr.SizeDtStruct, uint32(len(b))) // size of the structure block
h = append(h, m...)
h = append(h, b...)
h = append(h, s...)
return h
}