Nyanix

Bootloader
Log | Files | Refs | LICENSE

boot.s (2519B)


      1 ; =========================================================================== ;
      2 ; /src/boot_early/boot.s                                                      ;
      3 ; Copyright (C) 2018, k4m1 <k4m1@protonmail.com>                              ;
      4 ; All rights reserved.                                                        ;
      5 ; See /LICENSE for whole license text.                                        ;
      6 ;                                                                             ;
      7 ; This code acts as the initial entry point for the bootloader.               ;
      8 ; First we'll setup unreal mode, then load rest of the loader and jump to     ;
      9 ; second stage of the bootloader.                                             ;
     10 ; =========================================================================== ;
     11 
     12 org	0x7c00
     13 bits	16
     14 align	4
     15 
     16 %define SECTOR_COUNT 2
     17 
     18 ; clearing out CS
     19 jmp	0x0000:start
     20 
     21 BOOT_DEVICE db 0
     22 BL_SECTORS db 0
     23 
     24 start:
     25 	xor	ax, ax
     26 	mov	ds, ax
     27 	mov	ss, ax
     28 	mov	sp, 0x9c00
     29 	
     30 	; swapping to unreal mode
     31 	cli
     32 	push	ds
     33 	lgdt	[dummy_gdt]
     34 	mov	eax, cr0
     35 	or	al, 1
     36 	mov	cr0, eax
     37 	jmp	.pmode
     38 .pmode:
     39 	mov	bx, 0x08
     40 	mov	ds, bx
     41 	and	al, 0xFE
     42 	mov	cr0, eax
     43 	pop	ds
     44 	sti
     45 
     46 	; now we're in unreal mode, we have access to 32-bit registers
     47 	; and to BIOS interrupts.
     48 
     49 	mov	[BOOT_DEVICE], dl
     50 
     51 	; clearing the screen
     52 	xor	eax, eax
     53 	mov	al, 0x03
     54 	int	0x10
     55 
     56 
     57 ; ============================================================================ ;
     58 ; Function to load 2nd stage bootloader from disk.                             ;
     59 ; ============================================================================ ;
     60 
     61 load_second_stage:
     62 	mov	bx,  _start
     63 	mov	dh, SECTOR_COUNT
     64 	mov	dl, [BOOT_DEVICE]
     65 	mov	byte [BL_SECTORS], dh
     66 	xor	ch, ch
     67 	xor	dh, dh
     68 	mov	cl, 0x02
     69 	.read_start:
     70 		mov	di, 5
     71 	.read:
     72 		mov	ah, 0x02
     73 		mov	al, [BL_SECTORS]
     74 		int	0x13
     75 		jc	.retry
     76 		sub	[BL_SECTORS], al
     77 		jz	.read_done
     78 		mov	cl, 0x01
     79 		xor	dh, 1
     80 		jnz	.read_start
     81 		inc	ch
     82 		jmp	.read_start
     83 	.retry:
     84 		; disk read failed, reseting disk & retrying
     85 		xor	ah, ah
     86 		int	0x13
     87 		dec	di
     88 		jnz	.read
     89 		mov	si, msg_disk_read_failed
     90 		call	panic_early
     91 	.read_done:
     92 		jmp	_start
     93 
     94 msg_disk_read_failed db "Failed to read boot disk.", 0x0A, 0
     95 
     96 hang:
     97 	cli
     98 	hlt
     99 	jmp	hang
    100 
    101 panic_early:
    102 	lodsb
    103 	or	al, al
    104 	mov	ah, 0x0E
    105 	int	0x10
    106 	cmp	al, 0
    107 	jne	panic_early
    108 .hang:
    109 	cli
    110 	hlt
    111 	jmp	.hang
    112 
    113 
    114 dummy_gdt:
    115 	dw	gdt_end - gdt - 1
    116 	dd	gdt
    117 gdt:
    118 	dq	0
    119 	dw	0xffff
    120 	dw	0
    121 	db	0
    122 	db	10010010b
    123 	db	11001111b
    124 	db	0
    125 gdt_end:
    126 
    127 times	510-($-$$) db 0
    128 dw	0xAA55
    129 
    130 %include "src/boot_early/second_stage.s"
    131 
    132