xref: /titanic_50/usr/src/psm/stand/bootblks/common/util.fth (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
1\
2\ CDDL HEADER START
3\
4\ The contents of this file are subject to the terms of the
5\ Common Development and Distribution License (the "License").
6\ You may not use this file except in compliance with the License.
7\
8\ You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9\ or http://www.opensolaris.org/os/licensing.
10\ See the License for the specific language governing permissions
11\ and limitations under the License.
12\
13\ When distributing Covered Code, include this CDDL HEADER in each
14\ file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15\ If applicable, add the following below this CDDL HEADER, with the
16\ fields enclosed by brackets "[]" replaced with your own identifying
17\ information: Portions Copyright [yyyy] [name of copyright owner]
18\
19\ CDDL HEADER END
20\
21\
22\ ident	"%Z%%M%	%I%	%E% SMI"
23\ Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24\ Use is subject to license terms.
25\
26
27id: %Z%%M%	%I%	%E% SMI
28purpose: utility words
29copyright: Copyright 2007 Sun Microsystems, Inc. All Rights Reserved
30
31
32[ifdef] doheaders
33headers
34[else]
35headerless
36[then]
37
38d# 256  constant  /buf-len
39
40\
41\	useful counting words
42\
43: roundup ( x y -- x' )  1- tuck +  swap invert and  ;
44
45
46\
47\	various useful string manipulation words
48\
49
50: cstrlen ( cstr -- len )
51   dup begin
52      dup c@
53   while
54      char+
55   repeat swap -
56;
57
58: cscount ( cstr -- adr,len )  dup cstrlen  ;
59
60\ Append str1 to the end of str2
61: $append ( adr,len1 adr,len2 -- )
62   2over 2over  ca+ swap move   ( adr,len1 adr,len2 )
63   rot + ca+ 0 swap c!  drop    (  )
64;
65
66: $=  ( str1$ str2$ -- same? )
67   rot tuck <>  if
68      3drop false exit
69   then  comp 0=
70;
71
72\ advance str by 1
73: str++  ( adr len --  adr' len' )
74   swap 1+  swap 1-
75;
76
77: diag-cr?  ( -- )  diagnostic-mode?  if  cr  then  ;
78
79
80: find-abort ( name$ -- )
81   ." Can't find " type abort
82;
83
84: get-package ( pkg$ -- ph )
85   2dup  find-package 0=  if
86      find-abort
87   then                       ( pkg$ ph )
88   nip nip                    ( ph )
89;
90
91
92\
93\	CIF words for I/O and memory
94\
95" /openprom/client-services" get-package  constant cif-ph
96
97instance defer cif-open     ( dev$ -- ihandle|0 )
98instance defer cif-close    ( ihandle -- )
99instance defer cif-read     ( len adr ihandle -- #read )
100instance defer cif-seek     ( low high ihandle -- -1|0|1 )
101instance defer cif-release  ( size virt -- )
102
103: find-cif-method ( adr,len -- acf )
104   2dup  cif-ph find-method 0=  if    ( adr,len )
105      find-abort
106   then                               ( adr,len acf )
107   nip nip                            ( acf )
108;
109
110" open"     find-cif-method to cif-open
111" close"    find-cif-method to cif-close
112" read"     find-cif-method to cif-read
113" seek"     find-cif-method to cif-seek
114" release"  find-cif-method to cif-release
115
116
117" /chosen" get-package  constant chosen-ph
118
119: get-property  ( name$ ph -- prop$ )
120   >r 2dup  r>  get-package-property  if   ( name$ )
121      find-abort
122   then                                    ( name$ prop$ )
123   2swap  2drop                            ( prop$ )
124;
125
126: get-string-prop  ( name$ ph -- val$ )
127   get-property decode-string            ( prop$' val$ )
128   2swap 2drop                           ( val$ )
129;
130
131: get-int-prop  ( name$ ph -- n )
132   get-property decode-int               ( prop$' n )
133   nip nip                               ( n )
134;
135
136\
137\	memory allocation
138\	we bypass cif claim so we can do large page
139\	allocations like promif can
140\
141
142" mmu"    chosen-ph  get-int-prop  constant mmu-ih
143
144" memory" chosen-ph  get-int-prop  constant mem-ih
145
146: mmu-claim  ( [ virt ] size align -- base )
147   " claim" mmu-ih $call-method
148;
149
150: mmu-map  ( phys.lo phys.hi virt size -- )
151   -1  " map" mmu-ih $call-method
152;
153
154: mem-claim  ( size align -- phys.lo phys.hi )
155   " claim" mem-ih $call-method
156;
157
158: (mem-alloc)   ( size virt align -- virt )
159   \ claim memory first since it may throw if fragmented
160   rot  2dup swap  mem-claim           ( virt align size phys.lo phys.hi )
161   >r >r  rot ?dup  if                 ( align size virt  r: phys.lo phys.hi )
162      \ we picked virt - zero alignment
163      over 0  mmu-claim                ( align size virt  r: phys.lo phys.hi )
164   else                                ( align size  r: phys.lo phys.hi )
165      \ OBP picks virt - pass alignment
166      2dup swap  mmu-claim             ( align size virt  r: phys.lo phys.hi )
167   then                                ( align size virt  r: phys.lo phys.hi )
168   r> r>  2over swap  mmu-map          ( align size virt )
169   nip nip                             ( virt )
170;
171
172: vmem-alloc ( size virt -- virt )
173   swap  h# 2000 roundup  swap
174   1 (mem-alloc)
175;
176
177: mem-alloc ( size -- virt )
178   h# 2000  roundup
179   0 1 (mem-alloc)
180;
181
182: mem-free  ( virt size -- )
183   h# 2000  roundup
184   swap  cif-release    (  )
185;
186
187
188
189\ put ramdisk fcode 256 bytes from end of bootblk
190\ (currently 244 bytes in size)
191d# 256               constant /rd-fcode
192d# 8192 /rd-fcode -  constant rd-offset
193
194: open-abort  ( file$ -- )
195   ." Can't open "  type  abort
196;
197
198/buf-len  buffer: open-cstr
199
200: dev-open ( dev$ -- ih | 0 )
201   \ copy to C string for open
202   0  over open-cstr +  c!
203   open-cstr swap  move
204   open-cstr  cif-open
205;
206
207: dev-close ( ih -- )
208   cif-close
209;
210
211: read-disk    ( adr len off ih -- )
212   dup >r  0 swap  cif-seek  if     ( adr len  r: ih )
213      ." seek failed"  abort
214   then
215
216   tuck  swap r>  cif-read  <>  if  (  )
217      ." read failed"  abort
218   then
219;
220