Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Edited version of the FritzOS boot code.
- ; Start Of Assembly Code:
- ; The BIOS loads the code into address 0x7C00, so tell NASM where our code is going to be running from.
- ORG 0x7c00
- jmp Start
- db "MSWIN4.1" ; OEM
- dw 512 ; bytes per sector
- db 1 ; sectors per cluster
- dw 1 ; number of reserved sectors ( including sector zero )
- db 2 ; number of file allocation tables
- dw 224 ; total number of directory entries
- ; (must be set so that the root directory occupies entire sectors)
- dw 2880 ; total sectors in logical volume
- db 42 ; media descriptor type (?)
- dw 9 ; sectors per FAT (FAT12/16 only)
- dw 18 ; sectors per track
- dw 2 ; number of heads or sides on storage media
- dd 0 ; number of hidden sectors
- dd 1337 ; large amount of sector on disk
- ; total sectors 2880
- db $00 ; drive number
- db $00 ; flags
- db $28 ; $28 or $29
- db "YIFF" ; volume serial number
- db "WahovipabOS" ; volume label
- db "FAT12 " ;system identifier
- [BITS 16]
- Start: mov [drive], dl ; Save the index of the floppy we booted from
- cli
- mov ax, 0x03 ; Start in text mode
- int 0x10
- ; ------------------------------------------------------
- mov ah, 02h ; set cursor pos
- mov bh,0
- mov dh, 5 ; row
- mov dl, 15 ; col
- int 10h
- mov ah, 0eh
- mov bh, 0
- mov bl, 15
- mov al, 'B'
- int 10h
- mov al, 'o'
- int 10h
- mov al, 'o'
- int 10h
- mov al, 't'
- int 10h
- mov al, ' '
- int 10h
- mov al, 'O'
- int 10h
- mov al, 'K'
- int 10h
- mov al, '!'
- int 10h
- mov al, ' '
- int 10h
- mov al, 3
- int 10h
- mov ah, 0
- int 16h
- ; ------------------------------------------------------
- ResetFloppy: ; Get ready to load the kernel from the floppy. First, we reset it.
- mov ax, 0x00 ; Select Floppy Reset BIOS Function
- mov dl, [drive] ; Select the floppy FritzOS booted from
- int 13h ; Reset the floppy drive
- jc ResetFloppy ; If there was a error, try again.
- ; Now talk to the floppy drive
- ReadFloppy:
- mov bx, 0x9000 ; Load FritzOS at 0x9000.
- mov ah, 0x02 ; Load disk data to ES:BX
- mov al, 17 ; Load two floppy head full's worth of data.
- mov ch, 0 ; First Cylinder
- mov cl, 2 ; Start at the 2nd Sector
- mov dh, 0 ; Use first floppy head
- mov dl, [drive] ; Load from the drive FritzOS booted from.
- int 0x13 ; Read the floppy disk.
- jc ReadFloppy ; Error, try again.
- ReadFloppy2: ; Read another floppy head
- mov al, 17 ; The Second Head Full
- inc dh ; Set it to the second head
- int 13h ; Read the floppy disk.
- jc ReadFloppy2 ; If there was a error, try again.
- SetPMode: ; Get ready to set PMode ( Protected Mode)
- ; ------------------------------------------------------
- mov ah, 02h ; set cursor pos
- mov bh,0
- mov dh, 6 ; row
- mov dl, 15 ; col
- int 10h
- mov ah, 0eh
- mov bh, 0
- mov bl, 15
- mov al, 'L'
- int 10h
- mov al, 'o'
- int 10h
- mov al, 'a'
- int 10h
- mov al, 'd'
- int 10h
- mov al, ' '
- int 10h
- mov al, 'O'
- int 10h
- mov al, 'K'
- int 10h
- mov al, '!'
- int 10h
- mov al, ' '
- int 10h
- mov al, 3
- int 10h
- mov ah, 0
- int 16h
- mov ax, 0x13 ; Change into mode 13h now
- int 0x10
- ; call do_e820
- ; ------------------------------------------------------
- ; http://wiki.osdev.org/Detecting_Memory_(x86)
- ; Load a temporary GDTR.
- lgdt [GDTR] ; Load the GDTR.
- ; Set Protected Mode ( PMode )
- mov eax, cr0 ; Get the first control register.
- or eax, 1 ; Poke the 'Protected mode' bit into it.
- mov cr0, eax ; Store the modified control register.
- ; The CPU needs to jump at least once to get set up the rest of the way for protected mode
- jmp dword CodeSel:PMode
- [bits 32] ; Tell NASM that we're in 32-bit protected mode now
- PMode: ; Put the Data Selector into eax for setting up other registers.
- mov eax, DataSel ; Data Selector.
- ; Make SS, DS, ES, FS and GS = the Data Selector ( In the GDT )
- mov ss, eax
- mov ds, eax
- mov es, eax
- mov fs, eax
- mov gs, eax
- ; Set up the PMode stack:
- mov ax, 0x10
- mov ss, ax
- mov esp, 0xFFFF ; Init stack
- ; Stop the floppy motor from spinning ( since we are in PMode, the BIOS can't stop it so we stop it )
- mov dl, [ drive ] ; Select which motor to stop ( the floppy drive FritzOS booted from )
- ; Select Stop Floppy Motor function:
- mov dx, 0x3f2
- mov al, 0x0c
- out dx, al ; Floppy Motor stopped!
- jmp dword CodeSel:0x9000 ; Jump to the main kernel
- ; use the INT 0x15, eax= 0xE820 BIOS function to get a memory map
- ; inputs: es:di -> destination buffer for 24 byte entries
- ; outputs: bp = entry count, trashes all registers except esi
- do_e820:
- xor ebx, ebx ; ebx must be 0 to start
- xor bp, bp ; keep an entry count in bp
- mov edx, 0x0534D4150 ; Place "SMAP" into edx
- mov eax, 0xe820
- mov [es:di + 20], dword 1 ; force a valid ACPI 3.X entry
- mov ecx, 24 ; ask for 24 bytes
- int 0x15
- jc short .failed ; carry set on first call means "unsupported function"
- mov edx, 0x0534D4150 ; Some BIOSes apparently trash this register?
- cmp eax, edx ; on success, eax must have been reset to "SMAP"
- jne short .failed
- test ebx, ebx ; ebx = 0 implies list is only 1 entry long (worthless)
- je short .failed
- jmp short .jmpin
- .e820lp:
- mov eax, 0xe820 ; eax, ecx get trashed on every int 0x15 call
- mov [es:di + 20], dword 1 ; force a valid ACPI 3.X entry
- mov ecx, 24 ; ask for 24 bytes again
- int 0x15
- jc short .e820f ; carry set means "end of list already reached"
- mov edx, 0x0534D4150 ; repair potentially trashed register
- .jmpin:
- jcxz .skipent ; skip any 0 length entries
- cmp cl, 20 ; got a 24 byte ACPI 3.X response?
- jbe short .notext
- test byte [es:di + 20], 1 ; if so: is the "ignore this data" bit clear?
- je short .skipent
- .notext:
- mov ecx, [es:di + 8] ; get lower dword of memory region length
- or ecx, [es:di + 12] ; "or" it with upper dword to test for zero
- jz .skipent ; if length qword is 0, skip entry
- inc bp ; got a good entry: ++count, move to next storage spot
- add di, 24
- .skipent:
- test ebx, ebx ; if ebx resets to 0, list is complete
- jne short .e820lp
- .e820f:
- mov [mmap_ent], bp ; store the entry count
- clc ; there is "jc" on end of list to this point, so the carry must be cleared
- ret
- .failed:
- stc ; "function unsupported" error exit
- ret
- ; Protected mode needs a list of where the segments are in memory and what privelages they get.
- GDTR dw GDTEnd-1
- dd GDT
- GDT
- nullsel equ $-GDT
- GDT0 dd 0
- dd 0
- CodeSel equ $-GDT
- dw 0ffffh
- dw 0
- db 0
- db 09ah
- db 0cfh
- db 0h
- DataSel equ $-GDT
- dw 0ffffh
- dw 0h
- db 0h
- db 092h
- db 0cfh
- db 0
- GDTEnd
- mmap_ent db 0
- ; This part makes sure the bootsector is 512 bytes.
- drive db 0 ; For storing which drive we boot from
- ; The original version from FritzOS had AA55 in the wrong place!
- times 510-($-$$) db 0 ; moved over
- dw 0xAA55 ; BIOS checks this value to make sure it is AA55
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement