Home | History | Annotate | Line # | Download | only in boot_ufs
boot.S revision 1.15
      1 | file: boot.S
      2 | author: chapuni(webmaster@chapuni.com)
      3 |         ITOH Yasufumi
      4 |
      5 | $NetBSD: boot.S,v 1.15 2020/01/18 05:48:31 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 0xf0000.  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 |  :            :        :            :        +------------+    0x006000
     28 |  :            :        :            :        | /boot      |
     29 |  :            :        :            :        +------------+
     30 |  :            :        :            :        :            :
     31 |  ~            ~        ~            ~        ~            ~
     32 |  :            :        :            :<-SP    :            :<-SP
     33 |  :            :        +------------+        +------------+    0x0f0000
     34 |  :            :        |full primary|        |full primary|
     35 |  :            :        |boot loader |        |boot loader |
     36 |  :            :        +------------+        +------------+
     37 |  :            :        :            :        :            :
     38 |
     39 
     40 #include <machine/asm.h>
     41 #include "iocscall.h"
     42 
     43 #define BASEOFF		0x8000
     44 #define BASEPTR_A	(TEXTADDR+BASEOFF)
     45 #define BASEPTR_R	%pc@(top+BASEOFF:W)
     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(bootufs)
     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	BASEPTR_A:l,%a5		| set base ptr
     68 #define _RELOC(adr)	%a5@(((adr)-(BASEPTR_A&0xffff)):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 		| set system stack
     93 		lea	ASRELOC(top),%a1 | set stack pointer to 0x000F0000
     94 		lea	%a1@,%sp	| 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 availble 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:	.ascii	"\r\n\n"
    163 		.ascii	BOOT
    164 		.asciz	": "
    165 reboot_msg:	.asciz	"\r\n[Hit key to reboot]"
    166 		.even
    167 
    168 ENTRY_NOPROFILE(BOOT_ERROR)
    169 		addql	#4,%sp
    170 
    171 boot_error:	lea	%pc@(booterr_msg),%a1
    172 		IOCS(__B_PRINT)
    173 		moveal	%sp@+,%a1
    174 		IOCS(__B_PRINT)
    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
    202 		lsrb	#1,%d5		| 0: 256, 1: 512, 2: 1024
    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	#6,%d2		|
    229 		lsrl	%d5,%d2		| in sector
    230 sc2:
    231 		| read entire boot
    232 		moveq	#8192/256,%d3	| size is 8KB
    233 		lsrl	%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 (0: 256, 1: 512, 2: 1024)
    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 		moveq	#8,%d7
    279 		addb	%d5,%d7		| d7.b = (sector length: 0-2) + 8
    280 		cmpl	%d0,%d2
    281 		jcc	scsiread_ext
    282 		moveq	#__S_READ,%d1
    283 		cmpl	%d3,%d6
    284 		jcc	scsiread_noext
    285 		subl	%d2,%d0		| d0.0 = 0x200000 - pos
    286 		cmpl	%d0,%d3		|	<= len
    287 		jcs	scsiread_noext	| no
    288 
    289 scsiread_ext:	| use READEXT
    290 		extw	%d6		| d6.l = 65535
    291 		moveq	#__S_READEXT,%d1
    292 
    293 scsiread_noext:	| use READ
    294 loop_scsiread:
    295 		| d1: SCSI IOCS call #
    296 		| d6: max sector count at a time
    297 		movel	%d3,%a2		| save original len in a2
    298 		cmpl	%d3,%d6
    299 		jcc	1f
    300 		movel	%d6,%d3
    301 1:		IOCS(__SCSIDRV)		| SCSIIOCS(d1)
    302 		tstl	%d0
    303 		jne	read_error
    304 		movel	%d3,%d0		| addr += read count << (8 + sec len)
    305 		asll	%d7,%d0
    306 		addl	%d0,%a1
    307 		exg	%d3,%a2		| restore original len to d3
    308 		addl	%a2,%d2		| pos += read count
    309 		subl	%a2,%d3		| len -= read count
    310 		jne	loop_scsiread
    311 		moveml	%sp@+,%d2-%d3/%d6-%d7/%a2
    312 		rts
    313 
    314 |
    315 |	The former part must reside in the first 1KB.
    316 |
    317 		.globl	first_kbyte
    318 first_kbyte:
    319 |--------------------------------------------------------------------------
    320 |
    321 |	The latter text+data part is not accessible at the first boot time.
    322 |	PC-relative can be used from here.
    323 |
    324 		| Initialize the screen here.  Some IPL (060turbo ROM or
    325 		| genuine boot selector) don't initialize the screen.
    326 		| Such initialization should be done as early as possible
    327 		| but it's too severe to place it in first_kbyte area.
    328 		| Therefore do it here.
    329 		moveq	#0x10,%d1
    330 		IOCS(__CRTMOD)
    331 
    332 		jmp	_C_LABEL(bootufs)	| 0x0Fxxxx 
    333 
    334 		.word	0
    335 
    336 | int badbaddr __P((void *adr));
    337 |	check if the given address is valid for byte read
    338 |	return: 0: valid, 1: not valid
    339 
    340 ENTRY_NOPROFILE(badbaddr)
    341 		lea	0x0008:W,%a1		| MPU Bus Error vector
    342 		moveq	#1,%d0
    343 		lea	%pc@(badr1),%a0
    344 		movew	%sr,%sp@-
    345 		oriw	#0x0700,%sr		| keep out interrupts
    346 		movel	%a1@,%sp@-
    347 		movel	%a0,%a1@		| set bus error vector
    348 		movel	%sp,%d1			| save sp
    349 		moveal	%sp@(10),%a0
    350 		tstb	%a0@			| try read...
    351 		moveq	#0,%d0			| this is skipped on bus error
    352 badr1:		moveal	%d1,%sp			| restore sp
    353 		movel	%sp@+,%a1@
    354 		movew	%sp@+,%sr
    355 		rts
    356 
    357 | void RAW_READ __P((void *buf, u_int32_t blkpos, size_t bytelen));
    358 | inputs:
    359 |	buf:	 input buffer address
    360 |	blkpos:	 read start position in the partition in 512byte-blocks
    361 |	bytelen: read length in bytes
    362 
    363 Lraw_read_buf=4+(4*11)
    364 Lraw_read_pos_=Lraw_read_buf+4
    365 Lraw_read_len=Lraw_read_buf+8
    366 
    367 #ifdef SCSI_ADHOC_BOOTPART
    368 |	RAW_READ of physical disk
    369 ENTRY_NOPROFILE(RAW_READ0)
    370 		moveq	#0,%d0
    371 		jra	raw_read1
    372 #endif
    373 
    374 ENTRY_NOPROFILE(RAW_READ)
    375 #ifdef SCSI_ADHOC_BOOTPART
    376 		movel	_C_LABEL(SCSI_PARTTOP),%d0
    377 raw_read1:
    378 #endif
    379 		moveml	%d2-%d7/%a2-%a6,%sp@-
    380 		moveml	%sp@(Lraw_read_buf),%d1-%d3
    381 		movel	%d1,%a1
    382 		| d2.l:		pos in 512byte-blocks
    383 		| d3.l:		length in bytes
    384 		| a1 (=d1):	buffer address
    385 
    386 		lea	BASEPTR_R,%a5	| set base ptr
    387 #define _RELOC(adr)	%a5@(((adr)-(BASEPTR_A&0xffff)):W)
    388 #define ASRELOC(var)	_RELOC(_ASM_LABEL(var))
    389 #define RELOC(var)	_RELOC(_C_LABEL(var))
    390 
    391 		tstb	_RELOC(_C_LABEL(BOOT_INFO)+1) | simple check.  may be incorrect!
    392 		beqs	raw_read_floppy
    393 
    394 raw_read_scsi:
    395 		movel	RELOC(ID),%d4	| SCSI ID
    396 #ifdef SCSI_ADHOC_BOOTPART
    397 		movel	RELOC(SCSI_BLKLEN),%d5 | sector size: 0-2
    398 		| XXX length must be sector aligned
    399 		lsrl	#8,%d3		| size in 256byte-blocks
    400 		lsrl	%d5,%d3		| size in sector
    401 		bcss	read_half	| minimal error check
    402 		lsll	#1,%d2		| X flag and d2: pos in 256byte-blocks
    403 		roxrl	%d5,%d2		| pos in sector
    404 		addl	%d0,%d2		| physical pos in sector
    405 #else
    406 		moveq	#1,%d5		| 512bytes/sec
    407 		moveq	#9,%d0		| shift count
    408 		addl	#511,%d3
    409 		lsrl	%d0,%d3
    410 		bcss	read_half	| minimal error check
    411 
    412 		addl	#0x40,%d2	| 'a' partition starts here
    413 #endif
    414 |		jcc	1f
    415 |		BOOT_ERROR("out of seek")	| pos exceeds 32bit
    416 |1:
    417 		jbsr	scsiread
    418 		bras	raw_read_end
    419 
    420 raw_read_floppy:
    421 		|
    422 		| Floppy read routine
    423 		|
    424 
    425 		| convert to seek position
    426 
    427 		asll	#2,%d2		| size in 128byte-blocks
    428 
    429 		| sec = raw_read_pos	(d2)
    430 		| sec >>= 7 + (sector length: 0-3)
    431 
    432 		lea	RELOC(FDSECMINMAX),%a0
    433 		moveq	#0,%d1
    434 		moveb	%a0@,%d1	| d1: sector length (0-3)
    435 		lsrl	%d1,%d2		| d2: pos in sector
    436 		bcss	read_half	| error check
    437 
    438 		| trk = sec / (# sectors)
    439 		| sec = sec % (# sectors)
    440 
    441 		moveb	%a0@(7),%d1	| d1: max sector #
    442 		subb	%a0@(3),%d1	|   - min sector #
    443 		addqb	#1,%d1		| d1: # sectors
    444 		divu	%d1,%d2		| d2: (sec << 16) | track
    445 
    446 		| position = (sec length << 24) | (track/2 << 16)
    447 		|		| (track%2 << 8) | (min sec # + sec)
    448 
    449 		movel	%a0@,%d0	| d0: (sec len << 24) | min sec #
    450 		lsrw	#1,%d2		| d2: (sec << 16) | (track / 2)
    451 		jcc	1f
    452 		bset	#8,%d0		| |= (track % 2) << 8
    453 1:		swap	%d2		| d2: ((track / 2) << 16) | sec
    454 		addl	%d0,%d2		| d2: position
    455 
    456 		| read
    457 		movel	RELOC(FDMODE),%d1	| PDA*256 + MODE
    458 
    459 		| B_READ (for floppy)
    460 		|  d1.w: PDA x 256 + MODE
    461 		|	PDA: 0x90 (drive 0) ... 0x93 (drive 3)
    462 		|	MODE:	bit6: MFM
    463 		|		bit5: retry
    464 		|		bit4: seek
    465 		|  d2.l: position
    466 		|	bit31-24: sector length (0: 128, 1: 256, 2: 512, 3: 1K)
    467 		|	bit23-16: track # (0-79)
    468 		|	bit15-08: side (0 or 1)
    469 		|	bit07-00: sector # (1-)
    470 		|  d3.l: read bytes
    471 		|  a1:   read address
    472 		| return:
    473 		|  d0:	bit 31-24	ST0
    474 		|	bit 23-16	ST1
    475 		|	bit 15- 8	ST2
    476 		|	bit  7- 0	C
    477 		|	-1 on parameter error
    478 		| destroy: d0, d2, d3, a1
    479 		IOCS(__B_READ)
    480 		andil	#0xf8ffff00,%d0		| check status (must be zero)
    481 		jne	read_error
    482 
    483 raw_read_end:
    484 		moveml	%sp@+,%a2-%a6/%d2-%d7
    485 		rts
    486 #undef _RELOC	/* base register  a5  is no longer available */
    487 #undef ASRELOC
    488 #undef RELOC
    489 
    490 read_half:	BOOT_ERROR("read half of block")
    491 
    492 
    493 ENTRY_NOPROFILE(B_PUTC)
    494 		movel	%sp@(4),%d1
    495 		IOCS(__B_PUTC)
    496 		rts
    497 
    498 ENTRY_NOPROFILE(B_PRINT)
    499 		movel	%sp@(4),%a1
    500 		IOCS(__B_PRINT)
    501 		rts
    502 
    503 |
    504 | void memcpy(void *dst, const void *src, size_t count);
    505 | void memmove(void *dst, const void *src, size_t count);
    506 |
    507 |	small and slow memcpy...
    508 |	THIS FUNCTION DOES NOT CONFORM THE ANSI STANDARD
    509 |
    510 ENTRY_NOPROFILE(memcpy)
    511 ENTRY_NOPROFILE(memmove)
    512 	lea	%sp@(12),%a1
    513 	movel	%a1@,%d1	| count
    514 	jeq	Lmcpret
    515 	moveal	%a1@-,%a0	| src
    516 	moveal	%a1@-,%a1	| dest
    517 	cmpl	%a1,%a0
    518 	jcc	Lmcpfw
    519 	| copy backward
    520 	addal	%d1,%a0
    521 	addal	%d1,%a1
    522 1:	moveb	%a0@-,%a1@-
    523 	subql	#1,%d1
    524 	jne	1b
    525 	jra	Lmcpret
    526 Lmcpfw:	| copy forward
    527 1:	moveb	%a0@+,%a1@+
    528 	subql	#1,%d1
    529 	jne	1b
    530 Lmcpret:
    531 |	movel	%sp@(8),%d0	| uncomment this to conform ANSI
    532 	rts
    533 
    534 
    535 |
    536 |	global variables
    537 |
    538 	BSS(ID, 4)			| SCSI ID
    539 	BSS(BOOT_INFO, 4)		| result of IOCS(__BOOTINF)
    540 	BSS(FDMODE, 4)			| Floppy access mode: PDA x 256 + MODE
    541 	BSS(FDSECMINMAX, 8)		| +0: (min sector) sector length
    542 					| +1: (min sector) track #
    543 					| +2: (min sector) side
    544 					| +3: (min sector) sector #
    545 					| +4: (max sector) sector length
    546 					| +5: (max sector) track #
    547 					| +6: (max sector) side
    548 					| +7: (max sector) sector #
    549 #ifdef SCSI_ADHOC_BOOTPART
    550 	BSS(SCSI_PARTTOP, 4)		| start sector of boot partition
    551 	BSS(SCSI_BLKLEN ,4)		| sector len 0: 256, 1: 512, 2: 1024
    552 #endif
    553