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