summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/slof/fs/boot.fs
blob: 9a0ded0c2706423b0401ee8c4a229010c902783e (plain)
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
\ *****************************************************************************
\ * Copyright (c) 2004, 2008 IBM Corporation
\ * All rights reserved.
\ * This program and the accompanying materials
\ * are made available under the terms of the BSD License
\ * which accompanies this distribution, and is available at
\ * http://www.opensource.org/licenses/bsd-license.php
\ *
\ * Contributors:
\ *     IBM Corporation - initial implementation
\ ****************************************************************************/

0 VALUE load-size
0 VALUE go-entry
VARIABLE state-valid false state-valid !
CREATE go-args 2 cells allot go-args 2 cells erase

\ \\\\\\\\\\\\\\ Structure/Implementation Dependent Methods

: $bootargs
   bootargs 2@ ?dup IF
   ELSE s" diagnostic-mode?" evaluate and IF s" diag-file" evaluate
   ELSE s" boot-file" evaluate THEN THEN
;

: $bootdev ( -- device-name len )
   bootdevice 2@ dup IF s"  " $cat THEN
   s" diagnostic-mode?" evaluate IF
      s" diag-device" evaluate
   ELSE
      s" boot-device" evaluate
   THEN
   $cat \ prepend bootdevice setting from vpd-bootlist
   strdup
   ?dup 0= IF
      disable-watchdog
      drop true ABORT" No boot device!"
   THEN
;


\ \\\\\\\\\\\\\\ Implementation Independent Methods (Depend on Previous)
\ *
\ *
: set-boot-args ( str len -- ) dup IF strdup ELSE nip dup THEN bootargs 2! ;

: (set-boot-device) ( str len -- )
   ?dup IF 1+ strdup 1- ELSE drop 0 0 THEN bootdevice 2!
;

' (set-boot-device) to set-boot-device

: (add-boot-device) ( str len -- )	\ Concatenate " str" to "bootdevice"
   bootdevice 2@ ?dup IF $cat-space ELSE drop THEN set-boot-device
;

' (add-boot-device) to add-boot-device

0 value claim-list

: no-go ( -- ) -64 boot-exception-handler ABORT ;

defer go ( -- )

: go-32 ( -- )
   state-valid @ IF
      0 ciregs >r3 ! 0 ciregs >r4 !
      go-args 2@ go-entry start-elf client-data
      claim-list elf-release 0 to claim-list
   THEN
   -6d boot-exception-handler ABORT
;

: go-64 ( args len entry r2 -- )
    0 ciregs >r3 ! 0 ciregs >r4 !
    start-elf64 client-data
    claim-list elf-release 0 to claim-list
;

: set-le ( -- )
    1 ciregs >r13 !
;

: set-be ( -- )
    0 ciregs >r13 !
;

: go-64-be ( -- )
    state-valid @ IF
	set-be
	go-args 2@
	go-entry @
	go-entry 8 + @
	go-64
    THEN
    -6d boot-exception-handler ABORT
;


: go-32-be
    set-be
    go-32
;

: go-32-lev1
    set-le
    go-32
;

: go-64-lev1
    state-valid @ IF
	go-args 2@
	go-entry @ xbflip
	go-entry 8 + @ xbflip
	set-le
	go-64
    THEN
    -6d boot-exception-handler ABORT
;

: go-64-lev2
    state-valid @ IF
	go-args 2@
	go-entry 0
	set-le
	go-64
    THEN
    -6d boot-exception-handler ABORT
;

: load-elf-init ( arg len file-addr -- success )
   false state-valid !                            \ Not valid anymore ...
   claim-list IF                                    \ Release claimed mem
      claim-list elf-release 0 to claim-list        \ from last load
   THEN

   true swap -1                       ( arg len true file-addr -1 )
   elf-load-claim                     ( arg len true claim-list entry elftype )

   ( arg len true claim-list entry elftype )
   CASE
      1  OF ['] go-32-be   ENDOF           ( arg len true claim-list entry go )
      2  OF ['] go-64-be   ENDOF           ( arg len true claim-list entry go )
      3  OF ['] go-64-lev1 ENDOF           ( arg len true claim-list entry go )
      4  OF ['] go-64-lev2 ENDOF           ( arg len true claim-list entry go )
      5  OF ['] go-32-lev1 ENDOF           ( arg len true claim-list entry go )
      dup OF ['] no-go to go
         2drop 3drop false EXIT   ENDOF                   ( false )
   ENDCASE

   to go to go-entry to claim-list
   dup state-valid ! -rot

   2 pick IF
      go-args 2!
   ELSE
      2drop
   THEN
;

: init-program ( -- )
   $bootargs get-load-base ['] load-elf-init CATCH ?dup IF
      boot-exception-handler
      2drop 2drop false          \ Could not claim
   ELSE IF
         0 ciregs 2dup >r3 ! >r4 !  \ Valid (ELF ) Image
      THEN
   THEN
;


\ \\\\\\\\\\\\\\ Exported Interface:
\ *
\ Generic device load method:
\ *

: do-load ( devstr len -- img-size )	\ Device method wrapper
   use-load-watchdog? IF
      \ Set watchdog timer to 10 minutes, multiply with 2 because DHCP
      \ needs 1 second per try and add 1 min to avoid race conditions
      \ with watchdog timeout.
      4ec set-watchdog
   THEN
   my-self >r current-node @ >r         \ Save my-self
   ." Trying to load: " $bootargs type ."  from: " 2dup type ."  ... "
   2dup open-dev dup IF
      dup to my-self
      dup ihandle>phandle set-node
      -rot                              ( ihandle devstr len )
      my-args nip 0= IF
	 2dup 1- + c@ [char] : <> IF    \ Add : to device path if missing
	    1+ strdup 2dup 1- + [char] : swap c!
	 THEN
      THEN
      encode-string s" bootpath" set-chosen
      $bootargs encode-string s" bootargs" set-chosen
      get-load-base s" load" 3 pick ['] $call-method CATCH IF
	-67 boot-exception-handler 3drop drop false
      ELSE
	 dup 0> IF
	    init-program
	 ELSE
	    false state-valid !
	    drop 0                                     \ Could not load
	 THEN
      THEN
      swap close-dev device-end dup to load-size
   ELSE -68 boot-exception-handler 3drop false THEN
   r> set-node r> to my-self                           \ Restore my-self
;

: parse-load ( "{devlist}" -- success )	\ Parse-execute boot-device list
   cr BEGIN parse-word dup WHILE
	 ( de-alias ) do-load dup 0< IF drop 0 THEN IF
	    state-valid @ IF ."   Successfully loaded" cr THEN
	    true 0d parse strdup load-list 2! EXIT
	 THEN
   REPEAT 2drop 0 0 load-list 2! false
;

: load ( "{params}<eol>"} -- success )	\ Client interface to load
   parse-word 0d parse -leading 2swap ?dup IF
      de-alias
      set-boot-device
   ELSE
      drop
   THEN
   set-boot-args s" parse-load " $bootdev $cat strdup evaluate
;

: load-next ( -- success )	\ Continue after go failed
   load-list 2@ ?dup IF s" parse-load " 2swap $cat strdup evaluate
   ELSE drop false THEN
;

\ \\\\\\\\\\\\\\\\\\\\\\\\\\
\ load/go utilities
\ -> Should be in loaders.fs

: noload false ;

' no-go to go

: (go-and-catch)  ( -- )
   \ Recommended Practice: Forth Source Support (scripts starting with comment)
   get-load-base c@ 5c =  get-load-base 1+ c@ 20 = AND IF
      load-size alloc-mem            ( allocated-addr )
      ?dup 0= IF ." alloc-mem failed." cr EXIT THEN
      load-size >r >r                ( R: allocate-addr load-size )
      get-load-base r@ load-size move    \ Move away from load-base
      r@ load-size evaluate          \ Run the script
      r> r> free-mem
      EXIT
   THEN
   \ Assume it's a normal executable, use "go" to run it:
   ['] go behavior CATCH IF -69 boot-exception-handler THEN
;


\ if the board does not get the bootlist from the nvram
\ then this word is supposed to be overloaded with the
\ word to get the bootlist from VPD (or from wheresoever)
read-bootlist

\ \\\\\\\\\\\\\\ Exported Interface:
\ *
\ IEEE 1275 : load (user interface)
\ *
: boot
   load 0= IF -65 boot-exception-handler EXIT THEN
   disable-watchdog (go-and-catch)
   BEGIN load-next WHILE
      disable-watchdog (go-and-catch)
   REPEAT

   \ When we return from boot print the banner again.
   .banner
;

: load load 0= IF -65 boot-exception-handler THEN ;

\ \\\\ Temporary hacks for backwards compatibility
: yaboot ." Use 'boot disk' instead " ;

: netboot ( -- rc ) ." Use 'boot net' instead " ;

: netboot-arg ( arg-string -- rc )
   s" boot net " 2swap $cat (parse-line) $cat
   evaluate
;

: netload ( -- rc ) (parse-line)
   load-base-override >r flash-load-base to load-base-override
   s" load net:" strdup 2swap $cat strdup evaluate
   r> to load-base-override
   load-size
;

: neteval ( -- ) FLASH-LOAD-BASE netload evaluate ;