xref: /titanic_50/usr/src/psm/stand/bootblks/common/util.fth (revision c713350eb0c205161e2a4ab06cd996300721ac78)
1986fd29aSsetje\
2986fd29aSsetje\ CDDL HEADER START
3986fd29aSsetje\
4986fd29aSsetje\ The contents of this file are subject to the terms of the
5986fd29aSsetje\ Common Development and Distribution License (the "License").
6986fd29aSsetje\ You may not use this file except in compliance with the License.
7986fd29aSsetje\
8986fd29aSsetje\ You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9986fd29aSsetje\ or http://www.opensolaris.org/os/licensing.
10986fd29aSsetje\ See the License for the specific language governing permissions
11986fd29aSsetje\ and limitations under the License.
12986fd29aSsetje\
13986fd29aSsetje\ When distributing Covered Code, include this CDDL HEADER in each
14986fd29aSsetje\ file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15986fd29aSsetje\ If applicable, add the following below this CDDL HEADER, with the
16986fd29aSsetje\ fields enclosed by brackets "[]" replaced with your own identifying
17986fd29aSsetje\ information: Portions Copyright [yyyy] [name of copyright owner]
18986fd29aSsetje\
19986fd29aSsetje\ CDDL HEADER END
20986fd29aSsetje\
21986fd29aSsetje\
22*c713350eSJohn Johnson\ Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23986fd29aSsetje\ Use is subject to license terms.
24986fd29aSsetje\
25986fd29aSsetje
26986fd29aSsetjepurpose: utility words
27*c713350eSJohn Johnsoncopyright: Copyright 2009 Sun Microsystems, Inc. All Rights Reserved
28986fd29aSsetje
29986fd29aSsetje
30986fd29aSsetjed# 256  constant  /buf-len
31986fd29aSsetje
32986fd29aSsetje\
33986fd29aSsetje\	useful counting words
34986fd29aSsetje\
35986fd29aSsetje: roundup ( x y -- x' )  1- tuck +  swap invert and  ;
36986fd29aSsetje
37986fd29aSsetje
38986fd29aSsetje\
39986fd29aSsetje\	various useful string manipulation words
40986fd29aSsetje\
41986fd29aSsetje
42986fd29aSsetje: cstrlen ( cstr -- len )
43986fd29aSsetje   dup begin
44986fd29aSsetje      dup c@
45986fd29aSsetje   while
46986fd29aSsetje      char+
47986fd29aSsetje   repeat swap -
48986fd29aSsetje;
49986fd29aSsetje
50986fd29aSsetje: cscount ( cstr -- adr,len )  dup cstrlen  ;
51986fd29aSsetje
52986fd29aSsetje\ Append str1 to the end of str2
53986fd29aSsetje: $append ( adr,len1 adr,len2 -- )
54986fd29aSsetje   2over 2over  ca+ swap move   ( adr,len1 adr,len2 )
55986fd29aSsetje   rot + ca+ 0 swap c!  drop    (  )
56986fd29aSsetje;
57986fd29aSsetje
58986fd29aSsetje: $=  ( str1$ str2$ -- same? )
59986fd29aSsetje   rot tuck <>  if
60986fd29aSsetje      3drop false exit
61986fd29aSsetje   then  comp 0=
62986fd29aSsetje;
63986fd29aSsetje
64986fd29aSsetje\ advance str by 1
65986fd29aSsetje: str++  ( adr len --  adr' len' )
66986fd29aSsetje   swap 1+  swap 1-
67986fd29aSsetje;
68986fd29aSsetje
69e7cbe64fSgw25295: die  ( str -- )
70e7cbe64fSgw25295   cr  type  cr abort
71e7cbe64fSgw25295;
72e7cbe64fSgw25295
73986fd29aSsetje: diag-cr?  ( -- )  diagnostic-mode?  if  cr  then  ;
74986fd29aSsetje
75986fd29aSsetje
76986fd29aSsetje: find-abort ( name$ -- )
77e7cbe64fSgw25295   cr  ." Can't find " type  cr abort
78986fd29aSsetje;
79986fd29aSsetje
80986fd29aSsetje: get-package ( pkg$ -- ph )
81986fd29aSsetje   2dup  find-package 0=  if
82986fd29aSsetje      find-abort
83986fd29aSsetje   then                       ( pkg$ ph )
84986fd29aSsetje   nip nip                    ( ph )
85986fd29aSsetje;
86986fd29aSsetje
87986fd29aSsetje
88986fd29aSsetje\
89986fd29aSsetje\	CIF words for I/O and memory
90986fd29aSsetje\
91986fd29aSsetje" /openprom/client-services" get-package  constant cif-ph
92986fd29aSsetje
93986fd29aSsetjeinstance defer cif-open     ( dev$ -- ihandle|0 )
94986fd29aSsetjeinstance defer cif-close    ( ihandle -- )
95986fd29aSsetjeinstance defer cif-read     ( len adr ihandle -- #read )
96986fd29aSsetjeinstance defer cif-seek     ( low high ihandle -- -1|0|1 )
97986fd29aSsetjeinstance defer cif-release  ( size virt -- )
98986fd29aSsetje
99986fd29aSsetje: find-cif-method ( adr,len -- acf )
100986fd29aSsetje   2dup  cif-ph find-method 0=  if    ( adr,len )
101986fd29aSsetje      find-abort
102986fd29aSsetje   then                               ( adr,len acf )
103986fd29aSsetje   nip nip                            ( acf )
104986fd29aSsetje;
105986fd29aSsetje
106986fd29aSsetje" open"     find-cif-method to cif-open
107986fd29aSsetje" close"    find-cif-method to cif-close
108986fd29aSsetje" read"     find-cif-method to cif-read
109986fd29aSsetje" seek"     find-cif-method to cif-seek
110986fd29aSsetje" release"  find-cif-method to cif-release
111986fd29aSsetje
112986fd29aSsetje
113986fd29aSsetje" /chosen" get-package  constant chosen-ph
114986fd29aSsetje
115986fd29aSsetje: get-property  ( name$ ph -- prop$ )
116986fd29aSsetje   >r 2dup  r>  get-package-property  if   ( name$ )
117986fd29aSsetje      find-abort
118986fd29aSsetje   then                                    ( name$ prop$ )
119986fd29aSsetje   2swap  2drop                            ( prop$ )
120986fd29aSsetje;
121986fd29aSsetje
122986fd29aSsetje: get-string-prop  ( name$ ph -- val$ )
123986fd29aSsetje   get-property decode-string            ( prop$' val$ )
124986fd29aSsetje   2swap 2drop                           ( val$ )
125986fd29aSsetje;
126986fd29aSsetje
127986fd29aSsetje: get-int-prop  ( name$ ph -- n )
128986fd29aSsetje   get-property decode-int               ( prop$' n )
129986fd29aSsetje   nip nip                               ( n )
130986fd29aSsetje;
131986fd29aSsetje
132986fd29aSsetje\
133986fd29aSsetje\	memory allocation
134986fd29aSsetje\	we bypass cif claim so we can do large page
135986fd29aSsetje\	allocations like promif can
136986fd29aSsetje\
137986fd29aSsetje
138986fd29aSsetje" mmu"    chosen-ph  get-int-prop  constant mmu-ih
139986fd29aSsetje
140986fd29aSsetje" memory" chosen-ph  get-int-prop  constant mem-ih
141986fd29aSsetje
142986fd29aSsetje: mmu-claim  ( [ virt ] size align -- base )
143986fd29aSsetje   " claim" mmu-ih $call-method
144986fd29aSsetje;
145986fd29aSsetje
146986fd29aSsetje: mmu-map  ( phys.lo phys.hi virt size -- )
147986fd29aSsetje   -1  " map" mmu-ih $call-method
148986fd29aSsetje;
149986fd29aSsetje
150986fd29aSsetje: mem-claim  ( size align -- phys.lo phys.hi )
151986fd29aSsetje   " claim" mem-ih $call-method
152986fd29aSsetje;
153986fd29aSsetje
154986fd29aSsetje: (mem-alloc)   ( size virt align -- virt )
155986fd29aSsetje   \ claim memory first since it may throw if fragmented
156986fd29aSsetje   rot  2dup swap  mem-claim           ( virt align size phys.lo phys.hi )
157986fd29aSsetje   >r >r  rot ?dup  if                 ( align size virt  r: phys.lo phys.hi )
158986fd29aSsetje      \ we picked virt - zero alignment
159986fd29aSsetje      over 0  mmu-claim                ( align size virt  r: phys.lo phys.hi )
160986fd29aSsetje   else                                ( align size  r: phys.lo phys.hi )
161986fd29aSsetje      \ OBP picks virt - pass alignment
162986fd29aSsetje      2dup swap  mmu-claim             ( align size virt  r: phys.lo phys.hi )
163986fd29aSsetje   then                                ( align size virt  r: phys.lo phys.hi )
164986fd29aSsetje   r> r>  2over swap  mmu-map          ( align size virt )
165986fd29aSsetje   nip nip                             ( virt )
166986fd29aSsetje;
167986fd29aSsetje
168986fd29aSsetje: vmem-alloc ( size virt -- virt )
169986fd29aSsetje   swap  h# 2000 roundup  swap
170986fd29aSsetje   1 (mem-alloc)
171986fd29aSsetje;
172986fd29aSsetje
173986fd29aSsetje: mem-alloc ( size -- virt )
174986fd29aSsetje   h# 2000  roundup
175986fd29aSsetje   0 1 (mem-alloc)
176986fd29aSsetje;
177986fd29aSsetje
178986fd29aSsetje: mem-free  ( virt size -- )
179986fd29aSsetje   h# 2000  roundup
180986fd29aSsetje   swap  cif-release    (  )
181986fd29aSsetje;
182986fd29aSsetje
183986fd29aSsetje
184986fd29aSsetje
185986fd29aSsetje\ put ramdisk fcode 256 bytes from end of bootblk
186986fd29aSsetje\ (currently 244 bytes in size)
187986fd29aSsetjed# 256               constant /rd-fcode
188986fd29aSsetjed# 8192 /rd-fcode -  constant rd-offset
189986fd29aSsetje
190986fd29aSsetje: open-abort  ( file$ -- )
191e7cbe64fSgw25295   cr  ." Can't open "  type  cr abort
192986fd29aSsetje;
193986fd29aSsetje
194986fd29aSsetje/buf-len  buffer: open-cstr
195986fd29aSsetje
196986fd29aSsetje: dev-open ( dev$ -- ih | 0 )
197986fd29aSsetje   \ copy to C string for open
198986fd29aSsetje   0  over open-cstr +  c!
199986fd29aSsetje   open-cstr swap  move
200986fd29aSsetje   open-cstr  cif-open
201986fd29aSsetje;
202986fd29aSsetje
203986fd29aSsetje: dev-close ( ih -- )
204986fd29aSsetje   cif-close
205986fd29aSsetje;
206986fd29aSsetje
207986fd29aSsetje: read-disk    ( adr len off ih -- )
208986fd29aSsetje   dup >r  0 swap  cif-seek  if     ( adr len  r: ih )
209e7cbe64fSgw25295      " seek failed"  die
210986fd29aSsetje   then
211986fd29aSsetje
212986fd29aSsetje   tuck  swap r>  cif-read  <>  if  (  )
213e7cbe64fSgw25295      " read failed"  die
214986fd29aSsetje   then
215986fd29aSsetje;
216