first commit
This commit is contained in:
239
src/mbr/mbr.s
Executable file
239
src/mbr/mbr.s
Executable file
@@ -0,0 +1,239 @@
|
||||
; Copyright (C) 2020 Bradley Claus
|
||||
;
|
||||
; This program 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) any later version.
|
||||
;
|
||||
; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
[BITS 16]
|
||||
[ORG 0x7A00]
|
||||
[CPU KATMAI]
|
||||
jmp short init
|
||||
nop
|
||||
|
||||
%include "entry.inc"
|
||||
init:
|
||||
cli ; We do not want to be interrupted
|
||||
|
||||
xor ax, ax ; 0 AX
|
||||
mov ds, ax ; Set segment registers to 0
|
||||
mov es, ax ; *
|
||||
|
||||
mov ss, ax ; Set Stack Segment to 0
|
||||
mov sp, STACK_START
|
||||
|
||||
mov ch, 0x01 ; 256 WORDs in MBR (512 bytes), 0x0100 in cx
|
||||
mov si, 0x7C00 ; Current MBR Address (loaded here by BIOS)
|
||||
mov di, MBR_ENTRY ; New MBR Address (our new relocation address)
|
||||
rep movsw ; copy 512 bytes from 0x0000:7c00 to 0x0000:0600
|
||||
|
||||
jmp 0:main
|
||||
nop
|
||||
|
||||
%include "config.inc"
|
||||
%include "memory.inc"
|
||||
%include "partition_table.inc"
|
||||
%include "errors.inc"
|
||||
|
||||
main:
|
||||
sti
|
||||
mov [boot_drive], dl ; BIOS passes drive number in DL
|
||||
|
||||
.check_disk:
|
||||
cmp dl, 0x80
|
||||
jae main.find_active
|
||||
ERROR MBR_ERROR_DISK_T_ERR
|
||||
|
||||
.check_extentions:
|
||||
xor ax, ax
|
||||
mov bx, 0x55AA
|
||||
mov dl, byte [boot_drive]
|
||||
int 0x13
|
||||
jnc main.find_active
|
||||
ERROR MBR_ERROR_NO_INT32E ; no extended function support
|
||||
|
||||
.find_active:
|
||||
mov bx, PartEntry1 ; base = first partition
|
||||
mov cx, 4 ; there are only 4 entries
|
||||
.find_active_L0:
|
||||
mov al, byte [bx + PartEntry_t.attributes]
|
||||
test al, 0x80 ; 0x80 == 1000_0000b
|
||||
jnz main.active_found
|
||||
|
||||
add bx, 0x10 ; add 16 bytes to offset
|
||||
loop main.find_active_L0
|
||||
ERROR MBR_ERROR_NO_NO_BOOT_PART
|
||||
|
||||
.active_found:
|
||||
mov ax, bx
|
||||
sub ax, DiskSig ; leaves us with the offset relative from start of table
|
||||
; this gives us an offset from the begining of the partition table
|
||||
mov word [part_offset], ax ; update part_offset
|
||||
.read_data:
|
||||
push bp
|
||||
mov bp, sp
|
||||
|
||||
;uint8_t read_disk_raw(size_t count, uint16_t buf_segment, uint16_t buf_offset,
|
||||
; uint16_t lower_lower_lba, uint16_t upper_lower_lba)
|
||||
mov eax, dword [bx + PartEntry_t.lba_start]
|
||||
ror eax, 16
|
||||
push ax
|
||||
|
||||
ror eax, 16
|
||||
push ax
|
||||
|
||||
mov ax, VBR_ENTRY
|
||||
push ax
|
||||
|
||||
xor ax, ax
|
||||
push ax ; segment = 0
|
||||
|
||||
mov ax, 1
|
||||
push ax
|
||||
|
||||
call read_disk_raw
|
||||
leave
|
||||
|
||||
jnc main.goto_vbr
|
||||
ERROR MBR_ERROR_DISK_READ_ERR ; error in LBA read
|
||||
.goto_vbr:
|
||||
cmp word [VBR_ENTRY + 0x1FE], 0xAA55
|
||||
je main.sig_ok
|
||||
ERROR MBR_ERROR_NO_VBR_SIG ; no signature present
|
||||
|
||||
.sig_ok:
|
||||
push bp
|
||||
mov bp, sp
|
||||
|
||||
mov ax, partition_table_SIZE ; 72 bytes of data
|
||||
push ax
|
||||
mov ax, DiskSig ; start of partition table
|
||||
push ax
|
||||
mov ax, partition_table ; defined in memory.inc
|
||||
push ax
|
||||
call kmemcpy ; copy partition table to memory
|
||||
leave
|
||||
|
||||
xor ah, ah ; Set Video mode BIOS function
|
||||
mov al, 0x02 ; 16 color 80x25 Text mode
|
||||
int 0x10 ; Call video interrupt
|
||||
|
||||
mov ah, 0x05 ; Select active display page BIOS function
|
||||
xor al, al ; page 0
|
||||
int 0x10 ; call video interrupt
|
||||
|
||||
mov si, word [part_offset]
|
||||
mov dl, byte [boot_drive]
|
||||
jmp 0:0x7C00
|
||||
|
||||
; Wrapper for AH=0x42 INT13h (Extended Read)
|
||||
;
|
||||
; BIOS call details
|
||||
; AH = 42h
|
||||
; DL = drive number
|
||||
; DS:SI -> disk address packet
|
||||
;
|
||||
; Return:
|
||||
; CF clear if successful
|
||||
; AH = 00h
|
||||
; CF set on error
|
||||
; AH = error code
|
||||
; disk address packet's block count field set to number of blocks
|
||||
; successfully transferred
|
||||
;
|
||||
;
|
||||
; uint8_t read_disk_raw(uint16_t buf_segment, uint16_t buf_offset, uint16_t lower_lower_lba, uint16_t upper_lower_lba)
|
||||
; bp-0 = call frame
|
||||
; bp-2 = upper_lower_lba
|
||||
; bp-4 = lower_lower_lba
|
||||
; bp-6 = offset
|
||||
; bp-8 = segment
|
||||
; bp-10 = count
|
||||
; bp-12 = ret ptr
|
||||
read_disk_raw:
|
||||
push si
|
||||
|
||||
; uint8_t* kmemset(void* dest, uint8_t val, size_t len);
|
||||
push bp
|
||||
mov bp, sp
|
||||
|
||||
mov ax, 0x10
|
||||
push ax ; len = 16 bytes
|
||||
xor ax, ax
|
||||
push ax ; val = 0
|
||||
mov ax, lba_packet
|
||||
push ax ; dest = lba_packet address
|
||||
|
||||
call kmemset
|
||||
leave
|
||||
|
||||
mov byte [lba_packet + LBAPkt_t.size], 0x10
|
||||
mov word [lba_packet + LBAPkt_t.xfer_size], STAGE2_SECTOR_COUNT
|
||||
|
||||
mov ax, [bp-2]
|
||||
shl eax, 16
|
||||
mov ax, [bp-4]
|
||||
mov dword [lba_packet + LBAPkt_t.lower_lba], eax
|
||||
|
||||
mov ax, [bp-6]
|
||||
mov word [lba_packet + LBAPkt_t.offset], ax
|
||||
|
||||
mov ax, [bp-8]
|
||||
mov word [lba_packet + LBAPkt_t.segment], ax
|
||||
|
||||
mov si, lba_packet
|
||||
mov ah, 0x42
|
||||
mov dl, byte [boot_drive]
|
||||
int 0x13
|
||||
jnc read_disk_raw.endf
|
||||
|
||||
mov al, 'B'
|
||||
jmp error
|
||||
.endf:
|
||||
pop si
|
||||
ret
|
||||
|
||||
; #############
|
||||
;
|
||||
; Locals
|
||||
;
|
||||
; #############
|
||||
|
||||
boot_drive:
|
||||
db 0x00
|
||||
mbr_reserved1:
|
||||
db 0x00
|
||||
|
||||
; OFFSET from BEGINING of partition table, ie. DiskSig (-6 from PartEntry1)
|
||||
part_offset:
|
||||
dw 0x0000
|
||||
|
||||
%assign bytes_remaining (440 - ($ - $$))
|
||||
%warning MBR has bytes_remaining bytes remaining for code (MAX: 440 bytes)
|
||||
times ((512 - 72) - ($ - $$)) nop ; Fill the rest of sector with nop
|
||||
|
||||
DiskSig:
|
||||
times 4 db 0x00
|
||||
Reserved1:
|
||||
db 0x00
|
||||
Reserved2:
|
||||
db 0x00
|
||||
|
||||
PartEntry1:
|
||||
times 16 db 0x00
|
||||
PartEntry2:
|
||||
times 16 db 0x00
|
||||
PartEntry3:
|
||||
times 16 db 0x00
|
||||
PartEntry4:
|
||||
times 16 db 0x00
|
||||
BootSig:
|
||||
dw 0xAA55 ; Add boot signature at the end of bootloader
|
||||
554
src/miniboot32/BOOT_386.s
Executable file
554
src/miniboot32/BOOT_386.s
Executable file
@@ -0,0 +1,554 @@
|
||||
; Copyright (C) 2020 Bradley Claus
|
||||
;
|
||||
; This program 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) any later version.
|
||||
;
|
||||
; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
[BITS 32]
|
||||
[ORG 0x100000]
|
||||
[CPU KATMAI]
|
||||
|
||||
jmp short start32
|
||||
nop
|
||||
|
||||
; Unless noted otherwise, functions will be the standard x86 cdecl used in GCC
|
||||
|
||||
; In cdecl, subroutine arguments are passed on the stack.
|
||||
; Integer values and memory addresses are returned in the EAX register,
|
||||
; floating point values in the ST0 x87 register.
|
||||
; Registers EAX, ECX, and EDX are caller-saved,
|
||||
; and the rest are callee-saved.
|
||||
; The x87 floating point registers ST0 to ST7 must be empty (popped or freed)
|
||||
; when calling a new function, and ST1 to ST7 must be empty on exiting a function.
|
||||
; ST0 must also be empty when not used for returning a value.
|
||||
;
|
||||
; EXAMPLE: to call uint8_t* kmemset(uint8_t* dest, uint8_t val, uint8_t size);
|
||||
; => kmemset(*((char*) 0xb8000), 'F', (80*25))
|
||||
;
|
||||
; push ebp ; save old call frame pointer
|
||||
; mov ebp, esp ; new call frame pointer
|
||||
; push 0x000007D0 ; push args RTL, EBP - 4
|
||||
; push 'F' ; * EBP - 8
|
||||
; push 0x000b8000 ; * EBP - 12
|
||||
; call kmemset ; call function, this also places a return pointer on stack
|
||||
; add esp, 12 ; remove call arguments from stack frame
|
||||
; mov esp, ebp ; restore stack frame pointer (callee saves EBP)
|
||||
; pop ebp ; restore call frame pointer
|
||||
;
|
||||
;
|
||||
|
||||
; 33 bytes BPB + 26 Byte EBPB
|
||||
struc BPBStruct
|
||||
.OemName resb 8
|
||||
.BytesPerSect resw 1
|
||||
.SecsPerClust resb 1
|
||||
.ResSectors resw 1
|
||||
.FATs resb 1
|
||||
.RootDirEnts resw 1
|
||||
.Sectors resw 1
|
||||
.Media resb 1
|
||||
.SectPerFAT resw 1
|
||||
.SectPerTrack resw 1
|
||||
.Heads resw 1
|
||||
.Hidden resd 1
|
||||
.SectorHuge resd 1
|
||||
; begin EBPB
|
||||
.DriveNumber resb 1
|
||||
.NTReserved resb 1
|
||||
.Signature resb 1
|
||||
.VolumeID resd 1
|
||||
.VolumeLabel resb 11
|
||||
.SysIdent resb 8
|
||||
endstruc
|
||||
|
||||
; 12 bytes
|
||||
struc FSInfoStruct
|
||||
.first_root_dir_sector resw 1
|
||||
.last_root_dir_sector resw 1
|
||||
.root_dir_len resw 1
|
||||
.first_data_sector resw 1
|
||||
.active_cluster resw 1
|
||||
.active_FAT_cluster resw 1
|
||||
endstruc
|
||||
|
||||
; 20 bytes
|
||||
struc KInfoStruct
|
||||
.load_address resd 1
|
||||
.file_len resd 1
|
||||
.file_name resb 8
|
||||
.file_ext resb 3
|
||||
.reserved1 resb 1
|
||||
endstruc
|
||||
|
||||
; Address Range Descriptor Structure
|
||||
;
|
||||
; Offset in Bytes Name Description
|
||||
; 0 BaseAddrLow u32 - Low 32 Bits of Base Address
|
||||
; 4 BaseAddrHigh u32 - High 32 Bits of Base Address
|
||||
; 8 LengthLow u32 - Low 32 Bits of Length in Bytes
|
||||
; 12 LengthHigh u32 - High 32 Bits of Length in Bytes
|
||||
; 16 Type u32 - Address type of this range.
|
||||
; 20 ExtType u32 - ACPI 3.0 extended type
|
||||
struc AddressRangeDescStruct
|
||||
.BaseAddrLow resd 1
|
||||
.BaseAddrHigh resd 1
|
||||
.LengthLow resd 1
|
||||
.LengthHigh resd 1
|
||||
.Type resd 1
|
||||
.ExtType resd 1
|
||||
endstruc
|
||||
|
||||
; 20 bytes, passed to loaded kernel
|
||||
struc CBootInfoStruct
|
||||
.MemoryMapPtr resd 1
|
||||
.MemoryMapEntries resd 1
|
||||
.BPBDataPtr resd 1
|
||||
.FSInfoPtr resd 1
|
||||
.KInfoPtr resd 1
|
||||
endstruc
|
||||
|
||||
;;;
|
||||
; Errors
|
||||
; 0 = unused
|
||||
; 1 = No CPUID support on this platform (VM or some very old hardware?)
|
||||
; 2 = magic signature not found at end of file
|
||||
; 3 = Unexpected/unhandled artithmetic overflow/carry
|
||||
;;;
|
||||
|
||||
%define MAX_BYTES (1024 * 8)
|
||||
|
||||
%define VGA_BUF 0xb8000
|
||||
;black BG/Green FG
|
||||
%define TTY_COLOR 0x0200
|
||||
;black BG/Red FG
|
||||
%define TTY_ERR_CLR 0x0400
|
||||
;subtract 1 for max array values
|
||||
%define VGA_MAX_Y 25
|
||||
%define VGA_MAX_X 80
|
||||
|
||||
; VGA memory is row-major => offset = (row * MAX_Colums) + colum
|
||||
; macro array is counted from 0,0 = 1,1 => 79,24 is the last usable space in a page
|
||||
%define VGA_OFFSET(x,y) (((y*VGA_MAX_X) + x)*2)
|
||||
|
||||
start32:
|
||||
mov ax, 0x10
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
mov ss, ax ; load data registers with 2nd GDT selector
|
||||
mov esp, stack_top
|
||||
mov ebp, esp
|
||||
|
||||
; debugger hack because gdb doesn't like to switch register sizes
|
||||
; issue a set $eax = 1 to continue
|
||||
%ifdef DEBUG_HACK
|
||||
mov eax, 0
|
||||
.gdb_hack:
|
||||
test eax, eax
|
||||
jz start32.gdb_hack
|
||||
%endif
|
||||
|
||||
mov eax, dword [BOOT_SIG]
|
||||
cmp eax, 0xA0B0C0D0
|
||||
jz start32.signature_present
|
||||
|
||||
mov al, "2"
|
||||
call error
|
||||
|
||||
|
||||
.signature_present:
|
||||
call check_cpuid
|
||||
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
lea eax, [welcome_cstr]
|
||||
push eax
|
||||
call vga_puts
|
||||
leave
|
||||
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
lea eax, [version_cstr]
|
||||
push eax
|
||||
call vga_puts
|
||||
leave
|
||||
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
lea eax, [datetime_cstr]
|
||||
push eax
|
||||
call vga_puts
|
||||
leave
|
||||
|
||||
mov eax, VGA_BUF
|
||||
add eax, VGA_OFFSET(78, 24)
|
||||
mov dword [eax], 0x2f4b2f4f
|
||||
hlt
|
||||
|
||||
; Early error printer. Prints 'ERR: X' where X is an error code in al
|
||||
error:
|
||||
mov eax, VGA_BUF
|
||||
add eax, VGA_OFFSET(73, 24)
|
||||
|
||||
mov dword [eax], 0x4f524f45
|
||||
add eax, 0x2
|
||||
mov dword [eax], 0x4f3a4f52
|
||||
add eax, 0x2
|
||||
mov dword [eax], 0x4f204f20
|
||||
add eax, 0x2
|
||||
mov byte [eax], al
|
||||
hlt
|
||||
|
||||
check_cpuid:
|
||||
; flip cpuid id bit (bit 21) to check for cpuid support
|
||||
|
||||
; copy flags to eax via stack
|
||||
pushfd
|
||||
pop eax
|
||||
|
||||
mov ecx, eax ; copy to ecx for compare
|
||||
|
||||
xor eax, 1 << 21 ; xor eax with the 21st bit set
|
||||
|
||||
push eax ; push new flags and pop to flags register
|
||||
popfd
|
||||
|
||||
pushfd
|
||||
pop eax
|
||||
|
||||
cmp eax, ecx ; can we change the bit?
|
||||
je .no_cpuid
|
||||
ret
|
||||
.no_cpuid:
|
||||
mov al, "1"
|
||||
call error
|
||||
|
||||
; int vga_puts(char* str)
|
||||
;
|
||||
; INPUT:
|
||||
; str: C-Sytle string
|
||||
;
|
||||
; OUTPUT:
|
||||
; EAX = characters printed
|
||||
;
|
||||
vga_puts:
|
||||
; prolog, EBP = SFP (Stack frame pointer), [EBP-4] = char* str
|
||||
push edi
|
||||
push esi
|
||||
push ebx
|
||||
|
||||
.get_str_len:
|
||||
mov edi, [ebp - 4]
|
||||
|
||||
xor ecx, ecx ; ECX = 0
|
||||
not ecx ; ECX = -1 == 0xFFFFFFFF
|
||||
xor eax, eax ; search for al = 0x0
|
||||
cld
|
||||
repne scasb ; deincrement ecx while searching for al
|
||||
not ecx ; the inverse of a neg number = abs(x) - 1
|
||||
dec ecx ; ECX contains the length of the string - nul byte at end
|
||||
|
||||
|
||||
mov edi, VGA_BUF ; load dest index with VGA buffer + calculated offset
|
||||
mov esi, [ebp -4] ; load source index with src string
|
||||
|
||||
; ECX = string length
|
||||
; ESI = source string to print
|
||||
; EDI = destination in the VGA buffer
|
||||
.print_char:
|
||||
cmp ecx, 0x0
|
||||
jle vga_puts.endf
|
||||
|
||||
cmp byte [vga_buf_pos_x], 80
|
||||
je vga_puts.end_of_line
|
||||
|
||||
cmp byte [vga_buf_pos_y], 25
|
||||
je vga_puts.scroll_up
|
||||
|
||||
cmp byte [esi], 0x20
|
||||
jl vga_puts.special_character_switch
|
||||
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
|
||||
xor eax, eax
|
||||
mov al, [vga_buf_pos_y]
|
||||
push eax ; vga_pos_y
|
||||
|
||||
xor eax, eax
|
||||
mov al, [vga_buf_pos_x]
|
||||
push eax ; vga_pos_x
|
||||
|
||||
; uint16_t vga_calc_offset(uint8_t vga_pos_x, uint8_t vga_pos_y)
|
||||
call vga_calc_offset
|
||||
leave
|
||||
|
||||
xor bx, bx
|
||||
mov bl, byte [esi]
|
||||
or bx, TTY_COLOR ; create VGA character
|
||||
|
||||
mov [edi+eax], bx
|
||||
inc esi
|
||||
inc byte [vga_buf_pos_x]
|
||||
|
||||
|
||||
dec ecx
|
||||
jmp vga_puts.print_char ; loop until ECX == 0
|
||||
|
||||
.end_of_line:
|
||||
; advance to next row
|
||||
inc byte [vga_buf_pos_y]
|
||||
mov byte [vga_buf_pos_x], 0
|
||||
jmp vga_puts.print_char
|
||||
.scroll_up:
|
||||
; after 25 rows, move to next page (either swap pages [memcpy] or erase page 0)
|
||||
mov byte [vga_buf_pos_x], 0
|
||||
mov byte [vga_buf_pos_y], 0
|
||||
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
|
||||
mov eax, 0x7D0
|
||||
push eax ; 1 VGA page = 0x7D0 elements
|
||||
|
||||
xor eax, eax
|
||||
push eax ; we want to blank the page with 0x0000
|
||||
|
||||
mov eax, VGA_BUF
|
||||
push eax ; load vga buffer base (0xb8000)
|
||||
|
||||
; uint16_t* kmemset_word(void* dest, uint16_t val, size_t len);
|
||||
call kmemset_byte
|
||||
leave
|
||||
|
||||
jmp vga_puts.print_char
|
||||
|
||||
.special_character_switch:
|
||||
|
||||
cmp byte[esi], 0xA
|
||||
je vga_puts.handle_LF
|
||||
|
||||
cmp byte[esi], 0xD
|
||||
je vga_puts.handle_CR
|
||||
|
||||
; Default case
|
||||
.special_character_endp:
|
||||
dec ecx
|
||||
inc esi
|
||||
|
||||
jmp vga_puts.print_char
|
||||
|
||||
.handle_LF:
|
||||
; advance to next row
|
||||
inc byte [vga_buf_pos_y]
|
||||
jmp vga_puts.special_character_endp
|
||||
|
||||
.handle_CR:
|
||||
mov byte [vga_buf_pos_x], 0
|
||||
jmp vga_puts.special_character_endp
|
||||
|
||||
|
||||
.endf:
|
||||
pop ebx
|
||||
pop esi
|
||||
pop edi
|
||||
|
||||
ret
|
||||
|
||||
; short vga_get_xy(void)
|
||||
;
|
||||
; INPUT:
|
||||
;
|
||||
; OUTPUT:
|
||||
; EAX = xy_pos
|
||||
; > x = xy_pos && 0xFF00
|
||||
; y = xy_pos && 0x00FF
|
||||
; 2 MSB of EAX are 0.
|
||||
;
|
||||
vga_get_xy:
|
||||
.endf:
|
||||
ret
|
||||
|
||||
; void vga_set_xy(short xy_pos)
|
||||
; xy_pos is a byte packed word (16bit value), MSB is x, LSB is y
|
||||
; => x = xy_pos && 0xFF00
|
||||
; y = xy_pos && 0x00FF
|
||||
;
|
||||
; INPUT:
|
||||
; xy_pos
|
||||
;
|
||||
; OUTPUT:
|
||||
; NONE
|
||||
;
|
||||
vga_set_xy:
|
||||
.endf:
|
||||
ret
|
||||
|
||||
; uint16_t vga_calc_offset(uint8_t vga_pos_x, uint8_t vga_pos_y)
|
||||
; VGA_OFFSET(x,y) == (((y*VGA_MAX_X) + x)*2)
|
||||
vga_calc_offset:
|
||||
xor eax, eax
|
||||
xor edx, edx
|
||||
|
||||
mov edx, [ebp-4]
|
||||
mov ax, VGA_MAX_X
|
||||
mul dx ; DX:AX contains (y*VGA_MAX_X)
|
||||
jc vga_calc_offset.artithmetic_error
|
||||
|
||||
mov edx, [ebp-8]
|
||||
add ax, dx
|
||||
jc vga_calc_offset.artithmetic_error
|
||||
|
||||
mov dx, 0x02
|
||||
mul dx ; DX:AX contains (y*VGA_MAX_X) + x) * 2
|
||||
jnc vga_calc_offset.endf
|
||||
|
||||
.artithmetic_error:
|
||||
mov al, '3'
|
||||
call error
|
||||
|
||||
.endf:
|
||||
; (e)AX contains the offset
|
||||
ret
|
||||
|
||||
; uint32_t* kmemset(void* dest, uint32_t val, size_t len);
|
||||
kmemset_dword:
|
||||
push edi ; function uses edi, so save it.
|
||||
; [ebp] = previous call frame
|
||||
mov ecx, [ebp - 4] ; size_t len
|
||||
|
||||
mov eax, [ebp - 8] ; uint32_t val
|
||||
|
||||
mov edi, [ebp - 12] ; void * ptr
|
||||
; [ebp - 16] = return adress
|
||||
; [ebp - 20] = saved EDI
|
||||
rep stosd
|
||||
.endf:
|
||||
mov eax, [ebp - 12] ; return pointer to dest
|
||||
pop edi ; restore edi
|
||||
ret
|
||||
|
||||
; uint16_t* kmemset(void* dest, uint16_t val, size_t len);
|
||||
kmemset_word:
|
||||
push edi ; function uses edi, so save it.
|
||||
; [ebp] = previous call frame
|
||||
mov ecx, [ebp - 4] ; size_t len
|
||||
|
||||
mov ax, [ebp - 8] ; uint16_t val
|
||||
|
||||
mov edi, [ebp - 12] ; void * ptr
|
||||
; [ebp - 16] = return adress
|
||||
; [ebp - 20] = saved EDI
|
||||
rep stosw
|
||||
.endf:
|
||||
mov eax, [ebp - 12] ; return pointer to dest
|
||||
pop edi ; restore edi
|
||||
ret
|
||||
|
||||
; uint8_t* kmemset(void* dest, uint8_t val, size_t len);
|
||||
kmemset_byte:
|
||||
push edi ; function uses edi, so save it.
|
||||
; [ebp] = previous call frame
|
||||
mov ecx, [ebp - 4] ; size_t len
|
||||
|
||||
mov al, [ebp - 8] ; uint8_t val
|
||||
|
||||
mov edi, [ebp - 12] ; void * ptr
|
||||
; [ebp - 16] = return adress
|
||||
; [ebp - 20] = saved EDI
|
||||
rep stosb
|
||||
.endf:
|
||||
mov eax, [ebp - 12] ; return pointer to dest
|
||||
pop edi ; restore edi
|
||||
ret
|
||||
|
||||
; uint32_t* kmemset(uint32_t* dest, uint32_t* src, size_t len);
|
||||
kmemcpy_dword:
|
||||
push edi
|
||||
push esi ; edi, esi are callee save
|
||||
|
||||
mov edi, [ebp-12] ; dest
|
||||
mov esi, [ebp-8] ; source
|
||||
mov ecx, [ebp-4] ; length
|
||||
|
||||
cld ; ensure we are incrementing
|
||||
rep movsd
|
||||
|
||||
.endf:
|
||||
mov eax, [ebp-12] ; return pointer to dest
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
|
||||
; uint16_t* kmemset(uint16_t* dest, uint16_t* src, size_t len);
|
||||
kmemcpy_word:
|
||||
push edi
|
||||
push esi ; edi, esi are callee save
|
||||
|
||||
mov edi, [ebp-12] ; dest
|
||||
mov esi, [ebp-8] ; source
|
||||
mov ecx, [ebp-4] ; length
|
||||
|
||||
cld ; ensure we are incrementing
|
||||
rep movsw
|
||||
|
||||
.endf:
|
||||
mov eax, [ebp-12] ; return pointer to dest
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
|
||||
; uint8_t* kmemset(uint8_t* dest, uint8_t* src, size_t len);
|
||||
; not overlap safe
|
||||
kmemcpy_byte:
|
||||
push edi
|
||||
push esi ; edi, esi are callee save
|
||||
|
||||
mov edi, [ebp-12] ; dest
|
||||
mov esi, [ebp-8] ; source
|
||||
mov ecx, [ebp-4] ; length
|
||||
|
||||
cld ; ensure we are incrementing
|
||||
rep movsb
|
||||
|
||||
.endf:
|
||||
mov eax, [ebp-12] ; return pointer to dest
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
|
||||
; Static Values
|
||||
vga_buf_pos_x:
|
||||
db 0x00
|
||||
vga_buf_pos_y:
|
||||
db 0x00
|
||||
|
||||
; Strings
|
||||
%define StrCRLF_NUL 0Dh, 0Ah, 00h
|
||||
welcome_cstr:
|
||||
db 'CBoot Stage3', StrCRLF_NUL
|
||||
version_cstr:
|
||||
db 'CBoot v0.0.3 ', 'NASM - ', __NASM_VER__, StrCRLF_NUL
|
||||
datetime_cstr:
|
||||
db 'Assembled - ', __DATE__, ' ', __TIME__, StrCRLF_NUL
|
||||
|
||||
ALIGN 16, db 0
|
||||
stack_bottom:
|
||||
times (512 * 8) db 0x00 ; 4KiB stack
|
||||
stack_top:
|
||||
|
||||
%assign bytes_remaining ((MAX_BYTES - 4) - ($ - $$))
|
||||
%warning boot32 has bytes_remaining bytes remaining for code (MAX: MMAX_BYTES)
|
||||
|
||||
times ((MAX_BYTES - 4) - ($ - $$)) db 0xFE
|
||||
BOOT_SIG: dd 0xA0B0C0D0
|
||||
1182
src/stage2/stage2.s
Executable file
1182
src/stage2/stage2.s
Executable file
File diff suppressed because it is too large
Load Diff
219
src/vbr/vbr.s
Executable file
219
src/vbr/vbr.s
Executable file
@@ -0,0 +1,219 @@
|
||||
; Copyright (C) 2020 Bradley Claus
|
||||
;
|
||||
; This program 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) any later version.
|
||||
;
|
||||
; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
[BITS 16]
|
||||
[ORG 0x7C00]
|
||||
[CPU KATMAI]
|
||||
jmp short init
|
||||
nop
|
||||
|
||||
bpb_start:
|
||||
; fill BPB area with 0x00 since we skip writing this part to disk
|
||||
; but we need it for the 'jmp short entry; nop' above
|
||||
times 33 db 0x00
|
||||
|
||||
ebpb_start:
|
||||
; fill BPB area with 0x00 since we skip writing this part to disk
|
||||
; but we need it for the 'jmp short entry; nop' above
|
||||
times 54 db 0x00
|
||||
|
||||
%include "entry.inc"
|
||||
init:
|
||||
cli ; We do not want to be interrupted
|
||||
|
||||
xor ax, ax ; 0 AX
|
||||
mov ds, ax ; Set segment registers to 0
|
||||
mov es, ax ; *
|
||||
|
||||
mov ss, ax ; Set Stack Segment to 0
|
||||
mov sp, STACK_START ; Setup stack
|
||||
mov bp, sp ; base ptr = stack ptr
|
||||
|
||||
mov bx, VBR_ENTRY ; move BP to the new start of the initial boot sector
|
||||
|
||||
jmp 0:main ; fix up cs:ip just in case and jump to relocated code
|
||||
|
||||
%include "config.inc"
|
||||
%include "errors.inc"
|
||||
%include "memory.inc"
|
||||
|
||||
%include "partition_table.inc"
|
||||
|
||||
%include "fat32/bpb.inc"
|
||||
|
||||
|
||||
|
||||
main:
|
||||
sti ; all done with inital setup and relocation, reenable interupts
|
||||
mov [bsDriveNumber], dl ; BIOS passes drive number in DL
|
||||
mov [partition_offset], si ; save passed partition entry offset
|
||||
|
||||
.check_FAT_size:
|
||||
cmp dword [bsSectorHuge], 0 ; SectorsHuge will not be set if FAT12/16
|
||||
ja main.load_stage2
|
||||
|
||||
ERROR VBR_ERROR_WRONG_FAT_SIZE
|
||||
|
||||
; read sectors 1-63 to stage2 entry point
|
||||
.load_stage2:
|
||||
push bp
|
||||
mov bp, sp
|
||||
|
||||
;uint8_t read_disk_raw(size_t count, uint16_t buf_segment, uint16_t buf_offset,
|
||||
; uint16_t lower_lower_lba, uint16_t upper_lower_lba)
|
||||
|
||||
xor ax, ax
|
||||
push ax ; upper_lower_lba = 0
|
||||
|
||||
mov ax , 1
|
||||
push ax ; lower_lower_lba = 1
|
||||
|
||||
xor ax, ax
|
||||
push ax ; offset = 0
|
||||
|
||||
; 07E0:0
|
||||
mov ax, STAGE2_ENTRY
|
||||
shr ax, 4
|
||||
push ax ; segment = 7E0
|
||||
|
||||
mov ax, STAGE2_SECTOR_COUNT
|
||||
push ax
|
||||
|
||||
call read_disk_raw
|
||||
leave
|
||||
|
||||
.check_sig:
|
||||
mov ax, 0x7E0
|
||||
mov fs, ax
|
||||
cmp dword [fs:0x7FFC], 0xDEADBEEF
|
||||
je main.sig_ok
|
||||
|
||||
ERROR VBR_ERROR_NO_SIGNATURE ; no signature present in stage2
|
||||
|
||||
.sig_ok:
|
||||
push bp
|
||||
mov bp, sp
|
||||
mov ax, fat32_bpb_SIZE ; size in byte
|
||||
push ax
|
||||
mov ax, bpb_start ; start of bpb
|
||||
push ax
|
||||
mov ax, fat32_bpb ; defined in memory.inc, destination
|
||||
push ax
|
||||
call kmemcpy ; copy bpb to memory
|
||||
leave
|
||||
|
||||
push bp
|
||||
mov bp, sp
|
||||
mov ax, fat32_ebpb_SIZE ; 72 bytes of data
|
||||
push ax
|
||||
mov ax, ebpb_start ; start of ebpb
|
||||
push ax
|
||||
mov ax, fat32_ebpb ; defined in memory.inc, destination
|
||||
push ax
|
||||
call kmemcpy ; copy ebpb to memory
|
||||
leave
|
||||
|
||||
mov si, [partition_offset]
|
||||
mov dl, [bsDriveNumber]
|
||||
jmp 0:0x7E00
|
||||
|
||||
stop:
|
||||
hlt
|
||||
jmp short stop
|
||||
|
||||
; Wrapper for AH=0x42 INT13h (Extended Read)
|
||||
;
|
||||
; BIOS call details
|
||||
; AH = 42h
|
||||
; DL = drive number
|
||||
; DS:SI -> disk address packet
|
||||
;
|
||||
; Return:
|
||||
; CF clear if successful
|
||||
; AH = 00h
|
||||
; CF set on error
|
||||
; AH = error code
|
||||
; disk address packet's block count field set to number of blocks
|
||||
; successfully transferred
|
||||
;
|
||||
;
|
||||
; uint8_t read_disk_raw(uint16_t buf_segment, uint16_t buf_offset, uint16_t lower_lower_lba, uint16_t upper_lower_lba)
|
||||
; bp-0 = call frame
|
||||
; bp-2 = upper_lower_lba
|
||||
; bp-4 = lower_lower_lba
|
||||
; bp-6 = offset
|
||||
; bp-8 = segment
|
||||
; bp-10 = count
|
||||
; bp-12 = ret ptr
|
||||
read_disk_raw:
|
||||
push si
|
||||
|
||||
; uint8_t* kmemset(void* dest, uint8_t val, size_t len);
|
||||
push bp
|
||||
mov bp, sp
|
||||
|
||||
mov ax, 0x10
|
||||
push ax ; len = 16 bytes
|
||||
xor ax, ax
|
||||
push ax ; val = 0
|
||||
mov ax, lba_packet
|
||||
push ax ; dest = lba_packet address
|
||||
|
||||
call kmemset
|
||||
leave
|
||||
|
||||
mov byte [lba_packet + LBAPkt_t.size], 0x10
|
||||
mov word [lba_packet + LBAPkt_t.xfer_size], STAGE2_SECTOR_COUNT
|
||||
|
||||
mov ax, [bp-2]
|
||||
shl eax, 16
|
||||
mov ax, [bp-4]
|
||||
mov dword [lba_packet + LBAPkt_t.lower_lba], eax
|
||||
|
||||
mov ax, [bp-6]
|
||||
mov word [lba_packet + LBAPkt_t.offset], ax
|
||||
|
||||
mov ax, [bp-8]
|
||||
mov word [lba_packet + LBAPkt_t.segment], ax
|
||||
|
||||
mov si, lba_packet
|
||||
mov ah, 0x42
|
||||
mov dl, byte [bsDriveNumber]
|
||||
int 0x13
|
||||
jnc read_disk_raw.endf
|
||||
|
||||
ERROR VBR_ERROR_DISK_READ_ERR
|
||||
.endf:
|
||||
pop si
|
||||
ret
|
||||
; Data
|
||||
|
||||
; #############
|
||||
;
|
||||
; Locals
|
||||
;
|
||||
; #############
|
||||
|
||||
; offset from the begining of sector 0 to the active partition.
|
||||
partition_offset:
|
||||
dw 0x0000
|
||||
|
||||
%assign bytes_remaining (420 - ($ - $$))
|
||||
%warning VBR has bytes_remaining bytes remaining for code (MAX: 420 bytes)
|
||||
|
||||
times (510 - ($ - $$)) nop ; Fill the rest of sector with nop
|
||||
|
||||
BootSig:
|
||||
dw 0xAA55 ; Add boot signature at the end of bootloader
|
||||
Reference in New Issue
Block a user