Nyanix

Bootloader
Log | Files | Refs | LICENSE

second_stage.s (2786B)


      1 ; =========================================================================== ;
      2 ; src/boot_early/second_stage.s                                               ;
      3 ; Copyright (C) 2018, k4m1 <k4m1@protonmail.com>                              ;
      4 ; See /LICENSE for full license text.                                         ;
      5 ;                                                                             ;
      6 ; This is the beginning of the second stage of bootloader.                    ;
      7 ; Main purpose of the following code is to enable a20 gate, and then find     ;
      8 ; kernel from the boot disk.                                                  ;
      9 ; =========================================================================== ;
     10 
     11 [ BITS 16 ]
     12 _start:
     13 	jmp 	main
     14 
     15 ; =========================================================================== ;
     16 ; defines, macros, etc ...                                                    ;
     17 ; =========================================================================== ;
     18 %define kernel_found 1
     19 %define sector_size 0x1000
     20 
     21 ; a20 gate related operations.
     22 %include "src/boot_early/a20gate.s"
     23 
     24 ; The main_loader functionality.
     25 %include "src/main/main_loader.s"
     26 
     27 ; =========================================================================== ; 
     28 ; Messages                                                                    ;
     29 ; =========================================================================== ;
     30 msg_a20_fail db "Failed to enable A20 gate!", 0x0A, 0
     31 msg_main_loader_ret db "Unknown fatal error with main loader!", 0x0A, 0
     32 msg_panic db "[!] Panic: ", 0
     33 
     34 ; =========================================================================== ;
     35 ; boot device backup                                                          ;
     36 ; =========================================================================== ;
     37 boot_device_db db 0, 0
     38 
     39 ; =========================================================================== ;
     40 ; Main function of second stage entry.                                        ;
     41 ; =========================================================================== ;
     42 main:
     43 	xor 	ebp, ebp
     44 	mov 	esp, 0x9c00
     45 
     46 	xor 	eax, eax
     47 	mov 	al, [BOOT_DEVICE]
     48 	mov 	[boot_device_db], al
     49 
     50 	call 	check_a20gate
     51 	cmp 	eax, 0
     52 	je 	.a20_is_enabled
     53 
     54 	call 	toggle_a20_status
     55 	call 	check_a20gate
     56 	cmp 	eax, 0
     57 	je 	.a20_is_enabled
     58 
     59 	sti
     60 	; we apparently failed to enable a20 gate. :(
     61 	mov 	esi, msg_a20_fail
     62 	call 	panic
     63 
     64 	; we've enabled a20 gate now, time to start loading
     65 	; kernel from the disk.
     66 	.a20_is_enabled:
     67 		call 	main_loader
     68 
     69 		; we returned from main loader?
     70 		; that means we've failed hard, and it's time
     71 		; to panic now!
     72 
     73 		mov 	esi, msg_main_loader_ret
     74 		call 	panic
     75 
     76 panic:
     77 	lodsb
     78 	or	al, al
     79 	mov	ah, 0x0E
     80 	int	0x10
     81 	cmp	al, 0
     82 	jne	panic
     83 	.hang:
     84 		cli
     85 		hlt
     86 		jmp	.hang
     87 
     88