boot.S revision 1.2 1 | file: boot.s
2 | author: chapuni(GBA02750@niftyserve.or.jp)
3 | Yasha
4 |
5 | $NetBSD: boot.S,v 1.2 2001/09/27 14:08:27 minoura Exp $
6
7 #include <machine/asm.h>
8 #include "iocscall.h"
9
10 #define BASEOFF 0x8000
11 #define BASEPTR_A (TEXTADDR+BASEOFF)
12 #define BASEPTR_R %pc@(top+BASEOFF:W)
13
14 #define SRAM 0x00ED0000 /* SRAM stat addr */
15 #define SRAM_MEMSZ (SRAM + 8) /* (L) size of main memory */
16 #define MINMEM 0x00400000 /* at least 4MB required */
17
18 #define BOOT_ERROR(s) jbsr boot_error; .asciz s; .even
19
20 .globl _C_LABEL(bootufs)
21 .text
22 ASENTRY_NOPROFILE(start)
23 ASENTRY_NOPROFILE(top)
24 bras _ASM_LABEL(entry0)
25 .ascii "SHARP/"
26 .ascii "X680x0"
27 .word 0x8199,0x94e6,0x82ea,0x82bd
28 .word 0x8e9e,0x82c9,0x82cd,0x8cbb
29 .word 0x8ec0,0x93a6,0x94f0,0x8149
30 .word 0
31 | 0x2000 (FD), 0x2400 (SASI/SCSI) ( 0x0f0000)
32 | d4 SCSI ID
33 | jmp
34 ASENTRY_NOPROFILE(entry0)
35 moveml %d0-%d7/%a0-%a7,_C_LABEL(startregs)
36 lea BASEPTR_A:l,%a5 | set base ptr
37 #define _RELOC(adr) %a5@(((adr)-(BASEPTR_A&0xffff)):W)
38 #define ASRELOC(var) _RELOC(_ASM_LABEL(var))
39 #define RELOC(var) _RELOC(_C_LABEL(var))
40
41 lea RELOC(edata),%a1
42 bra _ASM_LABEL(entry)
43
44 | Disklabel= 404bytes
45 | Since LABELLOFFSET in <machine/disklabel.h> is 0x40,
46 | entry must be after 0x000001d4 (0x000f01d4)
47 disklabel:
48 .space 406
49
50 ASENTRY_NOPROFILE(entry)
51 movew #_end-1,%d0 | bss end (low word only)
52
53 | clear out bss (must be <= 64KB)
54 subw %a1,%d0
55 clrbss: clrb %a1@+
56 dbra %d0,clrbss
57
58 movel %d4,RELOC(ID) | SCSI ID (if booted from SCSI)
59
60 | set system stack
61 lea ASRELOC(top),%a1 | set stack pointer to 0x000F0000
62 lea %a1@,%sp | a1 will be used later for IOCS calls
63
64 | we use 68020 instructions, and check MPU beforehand
65 |
66 | here d1.w = -1, and the above "subw a1,d0" = 0x9049, and
67 | if MPU <= 010 loads 0x49,
68 | if MPU >= 020 loads 0x90.
69 | This is a move, not a tst instruction
70 | because pc-relative tsts are not availble on 000/010.
71 chkmpu: moveb %pc@(clrbss-chkmpu-2:B,%d0:W:2),%d0 | 103B 02xx
72 jmi mpuok | MC68020 or later
73 BOOT_ERROR("MPU 68000?")
74 mpuok: | XXX check for MMU?
75
76 IOCS(__BOOTINF)
77 lsll #8,%d0 | clear MSByte
78 lsrl #8,%d0 |
79 movel %d0,RELOC(BOOT_INFO)
80
81 |
82 | 0x80...0x8F SASI
83 | 0x90...0x93 Floppy
84 | 0xED0000...0xED3FFE SRAM
85 | others ROM (SCSI?)
86 |
87 movel %d0,%d1
88 clrb %d1
89 tstl %d1
90 jne boot_ram_rom
91 |
92 | SASI or Floppy
93 |
94 movel %d0,%d2
95 andib #0xFC,%d0
96 cmpib #0x90,%d0
97 jne boot_dev_unsupported | boot from SASI?
98 |
99 | Floppy
100 |
101 moveb %d2,%d0
102 andib #0x03,%d0 | drive # (head=0)
103 jbsr check_fd_format
104 moveml %d0-%d1,RELOC(FDSECMINMAX) | min and max sec #
105 lslw #8,%d2
106 moveq #0x70,%d1
107 orw %d2,%d1 | PDA*256 + MODE
108 movel %d1,RELOC(FDMODE)
109 movel %d0,%d2 | read position (first sector)
110 movel #8192,%d3 | read bytes
111 IOCS(__B_READ)
112 jra boot_read_done
113
114 #include "chkfmt.s"
115
116 boot_ram_rom:
117 movel %d0,%d1
118 swap %d1
119 cmpiw #0x00ED,%d1
120 jne boot_SCSI
121 | boot from SRAM?
122
123 boot_dev_unsupported:
124 BOOT_ERROR("unsupported boot device")
125
126 |
127 | volatile void BOOT_ERROR(const char *msg);
128 | print error message, wait for key press and reboot
129 |
130 booterr_msg: .ascii "\r\n\n"
131 .ascii BOOT
132 .asciz ": "
133 reboot_msg: .asciz "\r\n[Hit key to reboot]"
134 .even
135
136 ENTRY_NOPROFILE(BOOT_ERROR)
137 addql #4,%sp
138
139 boot_error: lea %pc@(booterr_msg),%a1
140 IOCS(__B_PRINT)
141 moveal %sp@+,%a1
142 IOCS(__B_PRINT)
143 lea %pc@(reboot_msg),%a1
144 IOCS(__B_PRINT)
145
146 | wait for a key press (or release of a modifier)
147 IOCS(__B_KEYINP)
148
149 | issue software reset
150 trap #10
151 | NOTREACHED
152
153
154 |
155 | ROM boot ... probably from SCSI
156 |
157 boot_SCSI:
158 #ifdef SCSI_ADHOC_BOOTPART
159 |
160 | Find out boot partition in an ad hoc manner.
161 |
162
163 | get block length of the SCSI disk
164 SCSIIOCS(__S_READCAP) | using buffer at a1
165 tstl %d0
166 jeq 1f
167 BOOT_ERROR("READCAP failed")
168 1: moveq #0,%d5
169 moveb %a1@(6),%d5 | 1: 256, 2: 512, 4: 1024
170 lsrb #1,%d5 | 0: 256, 1: 512, 2: 1024
171 movel %d5,RELOC(SCSI_BLKLEN)
172
173 | find out the start position of the boot partition
174 | XXX VERY AD HOC
175 |
176 | ROM firmware:
177 | pass read pos (in block #) in d2
178 | SCSI IPLs (genuine and SxSI):
179 | pass read pos (in kilobytes) in d2
180 | partition table on the memory is destroyed
181 | BOOT MENU Ver.2.22:
182 | passes partition table entry address in a0
183 | d2 is cleared to zero
184 | No other IPL is supported. XXX FIXME
185 tstl %d2
186 jne sc1
187 | no information in d2 -- probably from BOOT MENU
188 | a0 points the partiion table entry
189 movel %a0@(0x0008),%d2 | in KByte
190 sc1: cmpl #0x20,%d2
191 jcs sc2
192 lsll #8,%d2 | clear MSByte
193 lsrl #6,%d2 |
194 lsrl %d5,%d2 | in sector
195 sc2: movel %d2,RELOC(SCSI_PARTTOP)
196
197 | read entire boot
198 moveq #8192/256,%d3 | size is 8KB
199 lsrl %d5,%d3 | in sector
200 jbsr scsiread | read at %a1
201 #else
202 moveq #1,%d5 | 512bytes/sec
203 movel %d5,%sp@-
204 moveq #8192/512,%d3 |
205 moveq #0x40,%d2 | (sd*a )
206 SCSIIOCS(__S_READ)
207 #endif
208
209 boot_read_done:
210 jmp first_kbyte
211
212 read_error: BOOT_ERROR("read error")
213
214 #undef RELOC /* base register a5 is no longer available */
215 #undef ASRELOC
216 #undef _RELOC
217
218 |
219 | read SCSI
220 |
221 | input: d2.l: pos in sector
222 | d3.l: len in sector
223 | d4: target SCSI ID
224 | d5: sector length (0: 256, 1: 512, 2: 1024)
225 | a1: buffer address
226 | destroy:
227 | d0, d1, a1
228 |
229 scsiread:
230 moveml %d2-%d3/%d6-%d7/%a2,%sp@-
231 | if (pos >= 0x200000 || (len > 255 && pos + len >= 0x200000))
232 | use READEXT
233 | else
234 | use READ
235 moveq #0x20,%d0
236 swap %d0 | d0.l = 0x00200000
237 moveq #0,%d6
238 subqb #1,%d6 | d6.l = 255
239 moveq #8,%d7
240 addb %d5,%d7 | d7.b = (sector length: 0-2) + 8
241 cmpl %d0,%d2
242 jcc scsiread_ext
243 moveq #__S_READ,%d1
244 cmpl %d3,%d6
245 jcc scsiread_noext
246 subl %d2,%d0 | d0.0 = 0x200000 - pos
247 cmpl %d0,%d3 | <= len
248 jcs scsiread_noext | no
249
250 scsiread_ext: | use READEXT
251 extw %d6 | d6.l = 65535
252 moveq #__S_READEXT,%d1
253
254 scsiread_noext: | use READ
255 loop_scsiread:
256 | d1: SCSI IOCS call #
257 | d6: max sector count at a time
258 movel %d3,%a2 | save original len in a2
259 cmpl %d3,%d6
260 jcc 1f
261 movel %d6,%d3
262 1: IOCS(__SCSIDRV) | SCSIIOCS(d1)
263 tstl %d0
264 jne read_error
265 movel %d3,%d0 | addr += read count << (8 + sec len)
266 asll %d7,%d0
267 addl %d0,%a1
268 exg %d3,%a2 | restore original len to d3
269 addl %a2,%d2 | pos += read count
270 subl %a2,%d3 | len -= read count
271 jne loop_scsiread
272 moveml %sp@+,%d2-%d3/%d6-%d7/%a2
273 rts
274
275 |
276 | The former part must reside in the first 1KB.
277 |
278 .globl first_kbyte
279 first_kbyte:
280 |--------------------------------------------------------------------------
281 |
282 | The latter text+data part is not accessible at the first boot time.
283 | PC-relative can be used from here.
284 |
285 jmp _C_LABEL(bootufs) | 0x0Fxxxx
286
287 .word 0
288
289 | int badbaddr __P((caddr_t adr));
290 | check if the given address is valid for byte read
291 | return: 0: valid, 1: not valid
292
293 ENTRY_NOPROFILE(badbaddr)
294 lea 0x0008:W,%a1 | MPU Bus Error vector
295 moveq #1,%d0
296 lea %pc@(badr1),%a0
297 movew %sr,%sp@-
298 oriw #0x0700,%sr | keep out interrupts
299 movel %a1@,%sp@-
300 movel %a0,%a1@ | set bus error vector
301 movel %sp,%d1 | save sp
302 moveal %sp@(10),%a0
303 tstb %a0@ | try read...
304 moveq #0,%d0 | this is skipped on bus error
305 badr1: moveal %d1,%sp | restore sp
306 movel %sp@+,%a1@
307 movew %sp@+,%sr
308 rts
309
310 | void RAW_READ __P((void *buf, u_int32_t blkpos, size_t bytelen));
311 | inputs:
312 | buf: input buffer address
313 | blkpos: read start position in the partition in 512byte-blocks
314 | bytelen: read length in bytes
315
316 Lraw_read_buf=4+(4*11)
317 Lraw_read_pos_=Lraw_read_buf+4
318 Lraw_read_len=Lraw_read_buf+8
319
320 #ifdef SCSI_ADHOC_BOOTPART
321 | RAW_READ of physical disk
322 ENTRY_NOPROFILE(RAW_READ0)
323 moveq #0,%d0
324 jra raw_read1
325 #endif
326
327 ENTRY_NOPROFILE(RAW_READ)
328 #ifdef SCSI_ADHOC_BOOTPART
329 movel _C_LABEL(SCSI_PARTTOP),%d0
330 raw_read1:
331 #endif
332 moveml %d2-%d7/%a2-%a6,%sp@-
333 moveml %sp@(Lraw_read_buf),%d1-%d3
334 movel %d1,%a1
335 | d2.l: pos in 512byte-blocks
336 | d3.l: length in bytes
337 | a1 (=d1): buffer address
338
339 lea BASEPTR_R,%a5 | set base ptr
340 #define _RELOC(adr) %a5@(((adr)-(BASEPTR_A&0xffff)):W)
341 #define ASRELOC(var) _RELOC(_ASM_LABEL(var))
342 #define RELOC(var) _RELOC(_C_LABEL(var))
343
344 tstb _RELOC(_C_LABEL(BOOT_INFO)+1) | simple check. may be incorrect!
345 beqs raw_read_floppy
346
347 raw_read_scsi:
348 movel RELOC(ID),%d4 | SCSI ID
349 #ifdef SCSI_ADHOC_BOOTPART
350 movel RELOC(SCSI_BLKLEN),%d5 | sector size: 0-2
351 | XXX length must be sector aligned
352 lsrl #8,%d3 | size in 256byte-blocks
353 lsrl %d5,%d3 | size in sector
354 bcss read_half | minimal error check
355 lsll #1,%d2 | X flag and d2: pos in 256byte-blocks
356 roxrl %d5,%d2 | pos in sector
357 addl %d0,%d2 | physical pos in sector
358 #else
359 moveq #1,%d5 | 512bytes/sec
360 moveq #9,%d0 | shift count
361 addl #511,%d3
362 lsrl %d0,%d3
363 bcss read_half | minimal error check
364
365 addl #0x40,%d2 | 'a' partition starts here
366 #endif
367 | jcc 1f
368 | BOOT_ERROR("out of seek") | pos exceeds 32bit
369 |1:
370 jbsr scsiread
371 bras raw_read_end
372
373 raw_read_floppy:
374 |
375 | Floppy read routine
376 |
377
378 | convert to seek position
379
380 asll #2,%d2 | size in 128byte-blocks
381
382 | sec = raw_read_pos (d2)
383 | sec >>= 7 + (sector length: 0-3)
384
385 lea RELOC(FDSECMINMAX),%a0
386 moveq #0,%d1
387 moveb %a0@,%d1 | d1: sector length (0-3)
388 lsrl %d1,%d2 | d2: pos in sector
389 bcss read_half | error check
390
391 | trk = sec / (# sectors)
392 | sec = sec % (# sectors)
393
394 moveb %a0@(7),%d1 | d1: max sector #
395 subb %a0@(3),%d1 | - min sector #
396 addqb #1,%d1 | d1: # sectors
397 divu %d1,%d2 | d2: (sec << 16) | track
398
399 | position = (sec length << 24) | (track/2 << 16)
400 | | (track%2 << 8) | (min sec # + sec)
401
402 movel %a0@,%d0 | d0: (sec len << 24) | min sec #
403 lsrw #1,%d2 | d2: (sec << 16) | (track / 2)
404 jcc 1f
405 bset #8,%d0 | |= (track % 2) << 8
406 1: swap %d2 | d2: ((track / 2) << 16) | sec
407 addl %d0,%d2 | d2: position
408
409 | read
410 movel RELOC(FDMODE),%d1 | PDA*256 + MODE
411
412 | B_READ (for floppy)
413 | d1.w: PDA x 256 + MODE
414 | PDA: 0x90 (drive 0) ... 0x93 (drive 3)
415 | MODE: bit6: MFM
416 | bit5: retry
417 | bit4: seek
418 | d2.l: position
419 | bit31-24: sector length (0: 128, 1: 256, 2: 512, 3: 1K)
420 | bit23-16: track # (0-79)
421 | bit15-08: side (0 or 1)
422 | bit07-00: sector # (1-)
423 | d3.l: read bytes
424 | a1: read address
425 | return:
426 | d0: bit 31-24 ST0
427 | bit 23-16 ST1
428 | bit 15- 8 ST2
429 | bit 7- 0 C
430 | -1 on parameter error
431 | destroy: d0, d2, d3, a1
432 IOCS(__B_READ)
433 andil #0xf8ffff00,%d0 | check status (must be zero)
434 jne read_error
435
436 raw_read_end:
437 moveml %sp@+,%a2-%a6/%d2-%d7
438 rts
439 #undef _RELOC /* base register a5 is no longer available */
440 #undef ASRELOC
441 #undef RELOC
442
443 read_half: BOOT_ERROR("read half of block")
444
445
446 ENTRY_NOPROFILE(B_KEYINP)
447 IOCS(__B_KEYINP)
448 rts
449
450 ENTRY_NOPROFILE(B_PUTC)
451 movel %sp@(4),%d1
452 IOCS(__B_PUTC)
453 rts
454
455 ENTRY_NOPROFILE(B_PRINT)
456 movel %sp@(4),%a1
457 IOCS(__B_PRINT)
458 rts
459
460 |
461 | void memcpy(void *dst, const void *src, size_t count);
462 | void memmove(void *dst, const void *src, size_t count);
463 |
464 | small and slow memcpy...
465 | THIS FUNCTION DOES NOT CONFORM THE ANSI STANDARD
466 |
467 ENTRY_NOPROFILE(memcpy)
468 ENTRY_NOPROFILE(memmove)
469 lea %sp@(12),%a1
470 movel %a1@,%d1 | count
471 jeq Lmcpret
472 moveal %a1@-,%a0 | src
473 moveal %a1@-,%a1 | dest
474 cmpl %a1,%a0
475 jcc Lmcpfw
476 | copy backward
477 addal %d1,%a0
478 addal %d1,%a1
479 1: moveb %a0@-,%a1@-
480 subql #1,%d1
481 jne 1b
482 jra Lmcpret
483 Lmcpfw: | copy forward
484 1: moveb %a0@+,%a1@+
485 subql #1,%d1
486 jne 1b
487 Lmcpret:
488 | movel %sp@(8),%d0 | uncomment this to conform ANSI
489 rts
490
491
492 |
493 | global variables
494 |
495 BSS(ID, 4) | SCSI ID
496 BSS(BOOT_INFO, 4) | result of IOCS(__BOOTINF)
497 BSS(FDMODE, 4) | Floppy access mode: PDA x 256 + MODE
498 BSS(FDSECMINMAX, 8) | +0: (min sector) sector length
499 | +1: (min sector) track #
500 | +2: (min sector) side
501 | +3: (min sector) sector #
502 | +4: (max sector) sector length
503 | +5: (max sector) track #
504 | +6: (max sector) side
505 | +7: (max sector) sector #
506 #ifdef SCSI_ADHOC_BOOTPART
507 BSS(SCSI_PARTTOP, 4) | start sector of boot partition
508 BSS(SCSI_BLKLEN ,4) | sector len 0: 256, 1: 512, 2: 1024
509 #endif
510