|
|
1.1 root 1: \ *****************************************************************************
2: \ * Copyright (c) 2011 IBM Corporation
3: \ * All rights reserved.
4: \ * This program and the accompanying materials
5: \ * are made available under the terms of the BSD License
6: \ * which accompanies this distribution, and is available at
7: \ * http://www.opensource.org/licenses/bsd-license.php
8: \ *
9: \ * Contributors:
10: \ * IBM Corporation - initial implementation
11: \ ****************************************************************************/
12:
13: 0 VALUE fdt-debug
14:
15: \ Bail out if no fdt
16: fdt-start 0 = IF -1 throw THEN
17:
18: struct
19: 4 field >fdth_magic
20: 4 field >fdth_tsize
21: 4 field >fdth_struct_off
22: 4 field >fdth_string_off
23: 4 field >fdth_rsvmap_off
24: 4 field >fdth_version
25: 4 field >fdth_compat_vers
26: 4 field >fdth_boot_cpu
27: 4 field >fdth_string_size
28: 4 field >fdth_struct_size
29: drop
30:
31: h# d00dfeed constant OF_DT_HEADER
32: h# 1 constant OF_DT_BEGIN_NODE
33: h# 2 constant OF_DT_END_NODE
34: h# 3 constant OF_DT_PROP
35: h# 4 constant OF_DT_NOP
36: h# 9 constant OF_DT_END
37:
38: \ Create some variables early
39: fdt-start
40: dup dup >fdth_struct_off l@ + value fdt-struct
41: dup dup >fdth_string_off l@ + value fdt-strings
42: drop
43:
44: \ Dump fdt header for all to see and check FDT validity
45: : fdt-check-header ( -- )
46: fdt-start dup 0 = IF
47: ." No flat device tree !" cr drop -1 throw EXIT THEN
48: hex
49: fdt-debug IF
50: ." Flat device tree header at 0x" dup . s" :" type cr
51: ." magic : 0x" dup >fdth_magic l@ . cr
52: ." total size : 0x" dup >fdth_tsize l@ . cr
53: ." offset to struct : 0x" dup >fdth_struct_off l@ . cr
54: ." offset to strings: 0x" dup >fdth_string_off l@ . cr
55: ." offset to rsvmap : 0x" dup >fdth_rsvmap_off l@ . cr
56: ." version : " dup >fdth_version l@ decimal . hex cr
57: ." last compat vers : " dup >fdth_compat_vers l@ decimal . hex cr
58: dup >fdth_version l@ 2 >= IF
59: ." boot CPU : 0x" dup >fdth_boot_cpu l@ . cr
60: THEN
61: dup >fdth_version l@ 3 >= IF
62: ." strings size : 0x" dup >fdth_string_size l@ . cr
63: THEN
64: dup >fdth_version l@ 17 >= IF
65: ." struct size : 0x" dup >fdth_struct_size l@ . cr
66: THEN
67: THEN
68: dup >fdth_magic l@ OF_DT_HEADER <> IF
69: ." Flat device tree has incorrect magic value !" cr
70: drop -1 throw EXIT
71: THEN
72: dup >fdth_version l@ 10 < IF
73: ." Flat device tree has usupported version !" cr
74: drop -1 throw EXIT
75: THEN
76:
77: drop
78: ;
79: fdt-check-header
80:
81: \ Fetch next tag, skip nops and increment address
82: : fdt-next-tag ( addr -- nextaddr tag )
83: 0 ( dummy tag on stack for loop )
84: BEGIN
85: drop ( drop previous tag )
86: dup l@ ( read new tag )
87: swap 4 + swap ( increment addr )
88: dup OF_DT_NOP <> UNTIL ( loop until not nop )
89: ;
90:
91: \ Parse unit name and advance addr
92: : fdt-fetch-unit ( addr -- addr $name )
93: dup from-cstring \ get string size
94: 2dup + 1 + 3 + fffffffc and -rot
95: ;
96:
97: \ Lookup a string by index
98: : fdt-fetch-string ( index -- $string)
99: fdt-strings + dup from-cstring
100: ;
101:
102: : fdt-create-dec s" decode-unit" $CREATE , DOES> @ hex-decode-unit ;
103: : fdt-create-enc s" encode-unit" $CREATE , DOES> @ hex-encode-unit ;
104:
105: \ Method to unflatten a node
106: : fdt-unflatten-node ( start -- end )
107: \ this can and will recurse
108: recursive
109:
110: \ Get & check first tag of node ( addr -- addr)
111: fdt-next-tag dup OF_DT_BEGIN_NODE <> IF
112: s" Weird tag 0x" type . " at start of node" type cr
113: -1 throw
114: THEN drop
115:
116: new-device
117:
118: \ Parse name, split unit address
119: fdt-fetch-unit
120: dup 0 = IF drop drop " /" THEN
121: 40 left-parse-string
122: \ Set name
123: device-name
124:
125: \ Set unit address
126: dup IF
127: " #address-cells" get-parent get-package-property IF
128: 2drop
129: ELSE
130: decode-int nip nip
131: hex-decode-unit
132: set-unit
133: THEN
134: ELSE 2drop THEN
135:
136: \ Iterate sub tags
137: BEGIN
138: fdt-next-tag dup OF_DT_END_NODE <>
139: WHILE
140: dup OF_DT_PROP = IF
141: \ Found property
142: drop dup ( drop tag, dup addr : a1 a1 )
143: dup l@ dup rot 4 + ( fetch size, stack is : a1 s s a2)
144: dup l@ swap 4 + ( fetch nameid, stack is : a1 s s i a3 )
145: rot ( we now have: a1 s i a3 s )
146: encode-bytes rot ( a1 s pa ps i)
147: fdt-fetch-string ( a1 s pa ps $pn )
148: property
149: + 8 + 3 + fffffffc and
150: ELSE dup OF_DT_BEGIN_NODE = IF
151: drop ( drop tag )
152: 4 -
153: fdt-unflatten-node
154: ELSE
155: drop -1 throw
156: THEN THEN
157: REPEAT drop \ drop tag
158:
159: \ Create encode/decode unit
160: " #address-cells" get-node get-package-property IF ELSE
161: decode-int dup fdt-create-dec fdt-create-enc 2drop
162: THEN
163:
164: finish-device
165: ;
166:
167: \ Start unflattening
168: : fdt-unflatten-tree
169: fdt-debug IF
170: ." Unflattening device tree..." cr THEN
171: fdt-struct fdt-unflatten-node drop
172: fdt-debug IF
173: ." Done !" cr THEN
174: ;
175: fdt-unflatten-tree
176:
177: \ Find memory size
178: : fdt-parse-memory
179: " /memory" find-device
180: " reg" get-node get-package-property IF throw -1 THEN
181:
182: \ XXX FIXME Assume one entry only in "reg" property for now
183: decode-phys 2drop decode-phys
184: my-#address-cells 1 > IF 20 << or THEN
185:
186: fdt-debug IF
187: dup ." Memory size: " . cr
188: THEN
1.1.1.2 ! root 189: \ claim.fs already released the memory between 0 and MIN-RAM-SIZE,
! 190: \ so we've got only to release the remaining memory now:
! 191: MIN-RAM-SIZE swap MIN-RAM-SIZE - release
1.1 root 192: 2drop device-end
193: ;
194: fdt-parse-memory
195:
196:
197: \ Claim fdt memory and reserve map
198: : fdt-claim-reserve
199: fdt-start
200: dup dup >fdth_tsize l@ 0 claim drop
201: dup >fdth_rsvmap_off l@ +
202: BEGIN
203: dup dup x@ swap 8 + x@
204: dup 0 <>
205: WHILE
206: fdt-debug IF
207: 2dup swap ." Reserve map entry: " . ." : " . cr
208: THEN
209: 0 claim drop
210: 10 +
211: REPEAT drop drop drop
212: ;
213: fdt-claim-reserve
214:
215: \ Remaining bits from root.fs
216:
217: defer (client-exec)
218: defer client-exec
219:
220: \ defined in slof/fs/client.fs
221: defer callback
222: defer continue-client
223:
224: : set-chosen ( prop len name len -- )
225: s" /chosen" find-node set-property ;
226:
227: : get-chosen ( name len -- [ prop len ] success )
228: s" /chosen" find-node get-property 0= ;
229:
230: " /" find-device
231:
232: new-device
233: s" aliases" device-name
234: finish-device
235:
236: new-device
237: s" options" device-name
238: finish-device
239:
240: new-device
241: s" openprom" device-name
242: s" BootROM" device-type
243: finish-device
244:
245: new-device
246: #include <packages.fs>
247: finish-device
248:
249: : open true ;
250: : close ;
251:
252: device-end
253:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.