| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- ; Copyright (C) 2021 Rain
- ; This file is part of Cunix.
- ; Cunix is free software: you can redistribute it and/or modify
- ; it under the terms of the GNU General Public License as published by
- ; the Free Software Foundation, either version 3 of the License, or
- ; (at your option) and later version.
- ; Cunix is distributed in the hope that it will be useful,
- ; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ; GNU General Public License for more details.
- ; You should have received a copy of the GNU General Public License
- ; along with Cunix. If not, see <https://www.gnu.org/licenses/>.
- CYLINDERS_NEED equ 10
- org 0x7c00
- align 8
- _start:
- xor ax, ax
- mov ds, ax
- mov es, ax
- mov ss, ax
- mov sp, 0x7c00
- ; display message: `Cunix is booting...`
- ; boot device driver number is in DL (by BIOS-19)
- mov [BI_BOOTDEV], dl
- call get_e820
- call read_it
- call set_vbe_mode
- jmp 0x0000:0x8000
- diskpanic_message: db "read disk failed", 0x0d, 0x0a, 0x00
- e820panic_message: db "get E820 info failed", 0x0d, 0x0a, 0x00
- panic_message: db "boot panic: ", 0x00
- %include "arch/x86/readdisk.inc"
- print:
- ; SI: message address
- mov al, [si]
- add si, 1
- cmp al, 0x00
- je .ret
- mov ah, 0x0e
- mov bx, 0x0f
- int 0x10
- jmp print
- .ret:
- ret
- get_e820:
- xor ebx, ebx
- xor ax, ax
- mov es, ax
- ; address of E820 buffer
- mov di, 0x6000
-
- .get_loop:
- mov eax, 0x0000e820
- mov ecx, 20
- mov edx, 0x534d4150
- int 0x15
- jc .get_failed
- add di, 20
- cmp ebx, 0
- jne .get_loop
- ret
- .get_failed:
- mov di, e820panic_message
- call panic
- read_it:
- ; segment address of loader+kernel
- mov ax, 0x0800
- mov es, ax
- ; cylinder 0
- mov ch, 0
- ; header 0
- mov dh, 0
- ; start sector 2
- mov cl, 2
- .read_loop:
- ; I know it use to save source-address, but there isn't another
- ; register free.
- ; error counts
- mov si, 0
- .retry:
- mov ah, 0x02
- ; one sector
- mov al, 1
- ; offset 0x0000
- mov bx, 0
- ; boot device
- mov dl, [BI_BOOTDEV]
- ; call -> BIOS
- int 0x13
- ; if not error
- jnc .cont_read
-
- ; okay, something wrong. best way is reset it.
- .error:
- add si, 1
- ; we try it for 5 times
- mov di, diskpanic_message
- cmp si, 5
- jae panic
- .reset_disk:
- mov ah, 0x00
- mov dl, [BI_BOOTDEV]
- int 0x13
- ; and retry
- jmp .retry
- .cont_read:
- ; add for 0x0020 (not 0x0200)
- mov ax, es
- add ax, 0x0020
- mov es, ax
- ; we've been read one sector
- add cl, 1
- ; if read done
- SECTORS_1_TRACK equ 18
- cmp cl, SECTORS_1_TRACK
- jbe .read_loop
- .update_track:
- mov cl, 1
- add dh, 1
- HEADS_1_CYLINDER equ 2
- cmp dh, HEADS_1_CYLINDER
-
- jb .read_loop
- .update_head:
- mov dh, 0
- add ch, 1
- cmp ch, CYLINDERS_NEED
- jb .read_loop
- ret
- set_vbe_mode:
- mov bx, 0x10c
- .set_repeat:
- mov ax, 0x4f02
- ; we can use mode 0x108-0x10c, and save it
- push bx
- int 0x10
- ; failed?
- cmp ax, 0x4f
- je .ret
- .set_retry:
- pop bx
- ; bx--
- dec bx
- cmp bx, 0x107
- ja .set_repeat
- ; set failed, use default
- ret
-
- .ret:
- pop bx
- ret
- panic:
- mov si, panic_message
- call print
- mov si, di
- call print
- die: hlt
- jmp die
- times 510 - ($ - $$) db 0x00
- db 0x55, 0xaa
|