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