3 \ this code implements IEEE 1275-1994 ch. 5.3.4
5 \ Copyright (C) 2003 Stefan Reinauer
7 \ See the file "COPYING" for further information about
8 \ the copyright and warranty status of this work.
11 \ variable last-package 0 last-package !
12 \ 0 value active-package
13 : current-device active-package ;
16 \ 5.3.4.1 Open/Close packages (part 1)
19 \ 0 value my-self ( -- ihandle )
21 my-self dup 0= abort" no current instance."
24 : my-parent ( -- ihandle )
25 ?my-self >in.my-parent @
28 : ihandle>non-interposed-phandle ( ihandle -- phandle )
29 begin dup >in.interposed @ while
35 : ihandle>phandle ( ihandle -- phandle )
41 \ defined in property.c
43 : peer ( phandle -- phandle.sibling )
51 : child ( phandle.parent -- phandle.child )
52 \ Assume phandle == 0 indicates root node (not documented but similar
53 \ behaviour to "peer"). Used by some versions of Solaris (e.g. 9).
54 ?dup if else device-tree @ then
61 \ 5.3.4.2 Call methods from other packages
64 : find-method ( method-str method-len phandle -- false | xt true )
65 \ should we search the private wordlist too? I don't think so...
66 >dn.methods @ find-wordlist if
73 : call-package ( ... xt ihandle -- ??? )
81 : $call-method ( ... method-str method-len ihandle -- ??? )
82 dup >r >in.device-node @ find-method if
89 : $call-parent ( ... method-str method-len -- ??? )
90 my-parent $call-method
95 \ 5.3.4.1 Open/Close packages (part 2)
98 \ find-dev ( dev-str dev-len -- false | phandle true )
99 \ find-rel-dev ( dev-str dev-len phandle -- false | phandle true )
101 \ These function works just like find-device but without
102 \ any side effects (or exceptions).
106 : find-rel-dev ( dev-str dev-len phandle -- false | phandle true )
107 active-package >r active-package!
112 : find-package ( name-str name-len -- false | phandle true )
113 \ Locate the support package named by name string.
114 \ If the package can be located, return its phandle and true; otherwise,
116 \ Interpret the name in name string relative to the "packages" device node.
117 \ If there are multiple packages with the same name (within the "packages"
118 \ node), return the phandle for the most recently created one.
120 \ This does the full path resolution stuff (including
121 \ alias expansion. If we don't want that, then we should just
122 \ iterade the children of /packages.
123 " /packages" find-dev 0= if 2drop false exit then
124 find-rel-dev 0= if false exit then
129 : open-package ( arg-str arg-len phandle -- ihandle | 0 )
130 \ Open the package indicated by phandle.
131 \ Create an instance of the package identified by phandle, save in that
132 \ instance the instance-argument specified by arg-string and invoke the
133 \ package's open method.
134 \ Return the instance handle ihandle of the new instance, or 0 if the package
135 \ could not be opened. This could occur either because that package has no
136 \ open method, or because its open method returned false, indicating an error.
137 \ The parent instance of the new instance is the instance that invoked
138 \ open-package. The current instance is not changed.
140 create-instance dup 0= if
146 strdup r@ >in.arguments 2!
149 " open" r@ ['] $call-method catch if 3drop false then
153 r> destroy-instance false
158 : $open-package ( arg-str arg-len name-str name-len -- ihandle | 0 )
159 \ Open the support package named by name string.
168 : close-package ( ihandle -- )
169 \ Close the instance identified by ihandle by calling the package's close
170 \ method and then destroying the instance.
171 dup " close" rot ['] $call-method catch if 3drop then
176 \ 5.3.4.3 Get local arguments
179 : my-address ( -- phys.lo ... )
180 ?my-self >in.device-node @
182 my-#acells tuck /l* + swap 1- 0
189 : my-space ( -- phys.hi )
190 ?my-self >in.device-node @
194 : my-unit ( -- phys.lo ... phys.hi )
196 my-#acells tuck /l* + swap 0 ?do
202 : my-args ( -- arg-str arg-len )
203 ?my-self >in.arguments 2@
206 \ char is not included. If char is not found, then R-len is zero
207 : left-parse-string ( str len char -- R-str R-len L-str L-len )
211 \ parse ints "hi,...,lo" separated by comma
212 : parse-ints ( str len num -- val.lo .. val.hi )
215 rot 1- -rot 2 pick 0>=
218 2dup ascii , strchr ?dup if
221 2 pick 2 pick - ( num n p str len len1+1 )
222 dup -rot - ( num n p str len1+1 len2 )
223 -rot 1- ( num n p len2 str len1 )
232 begin 1- dup 0>= while r> swap repeat
236 : parse-2int ( str len -- val.lo val.hi )
242 \ 5.3.4.4 Mapping tools
245 : map-low ( phys.lo ... size -- virt )
246 my-space swap s" map-in" $call-parent
249 : free-virtual ( virt size -- )
250 over s" address" get-my-property 0= if
251 decode-int -rot 2drop = if
252 s" address" delete-property
257 s" map-out" $call-parent
261 \ Deprecated functions (required for compatibility with older loaders)
263 variable package-stack-pos 0 package-stack-pos !
264 create package-stack 8 cells allot
266 : push-package ( phandle -- )
267 \ Throw an error if we attempt to push a full stack
268 package-stack-pos @ 8 >= if
269 ." cannot push-package onto full stack" cr
273 package-stack-pos @ /n * package-stack + !
274 package-stack-pos @ 1 + package-stack-pos !
279 \ Throw an error if we attempt to pop an empty stack
280 package-stack-pos @ 0 = if
281 ." cannot pop-package from empty stack" cr
284 package-stack-pos @ 1 - package-stack-pos !
285 package-stack-pos @ /n * package-stack + @