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