-
Notifications
You must be signed in to change notification settings - Fork 11
Changes to the boot sector to allow mOS to boot on a greater range of hardware. #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1725490
Fixed boot sector bug preventing booting on some hardware!!!
Alex105178 ae7faeb
Improved error handling of boot sector code and other changes.
Alex105178 9d848e9
Added code in makefile to handle size of mOS.bin binary.
Alex105178 2ef4e5b
Clean up code and add more comments.
Alex105178 f02bbdf
Merge branch 'makeopensource:main' into main
Alex105178 e42542b
Increase the total number of sectors read back to 42.
Alex105178 6d91507
Change variable names and add comments.
Alex105178 a046e98
Update docs for changes to 'boot_sect.asm'.
Alex105178 d8f9908
Remove stray '\'.
Alex105178 53c9af8
Fix mispelled word. 'assumming' -> 'assuming'.
Alex105178 161b505
Replace magic numbers with character literals in boot_sect.asm.
Alex105178 756a546
Removed redundant mov instruction in boot_sect.asm.
Alex105178 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,246 @@ | ||
| [org 0x7c00] | ||
| OS_OFFSET equ 0x1000 | ||
| MAX_READ_TRIES equ 3 | ||
|
|
||
|
|
||
| ;;; Total number of sectors to read from the boot drive, including the boot | ||
| ;;; sector. | ||
| ;;; WARNING: This MUST be equal to the identically named constant in the | ||
| ;;; Makefile. | ||
| OS_BIN_TOTAL_SECTORS equ 42 | ||
| ;;; The number of sectors to read from the boot drive in addition to the boot | ||
| ;;; sector. | ||
| ADDITIONAL_SECTORS_TO_READ equ OS_BIN_TOTAL_SECTORS-1 | ||
|
|
||
| [bits 16] | ||
| begin: | ||
| mov [BOOT_DRIVE], dl | ||
| mov ax, 0 | ||
| mov ds, ax | ||
| mov ss, ax | ||
| mov es, ax | ||
| mov fs, ax | ||
| mov gs, ax | ||
| mov bp, 0x9000 | ||
| mov sp, bp | ||
|
|
||
| ;;; Get Drive Parameters | ||
| mov ah, 8 | ||
| mov dl, [BOOT_DRIVE] | ||
| mov di, 0 | ||
| clc | ||
| int 0x13 | ||
| jnc get_params_no_error | ||
| ;;; Print and stop if there is an error reading the drive parameters | ||
| call print_error | ||
| mov al, ah | ||
| call print_byte | ||
| jmp $ | ||
|
|
||
| get_params_no_error: | ||
| ;;; Save some of the drive parameters | ||
| add dh, 1 | ||
| mov [DRIVE_N_HEADS], dh | ||
| and cl, 0x3f | ||
| mov [DRIVE_SECTS_PER_TRACK], cl | ||
| jmp load_kernel | ||
|
|
||
| %include "src/boot/gdt.asm" | ||
| %include "src/boot/enter_pm.asm" | ||
|
|
||
| [bits 16] | ||
| load_kernel: | ||
| mov ah, 2 ;read BIOS chs | ||
| mov al, 42 ;sectors to read | ||
| mov cl, 0x02 ;start at sector 2 | ||
| mov ch, 0x00 ;cylinder | ||
| mov dh, 0x00 ;head | ||
|
|
||
| ;;; Read drive one sector at a time | ||
| mov bl, 1 | ||
| read: | ||
| mov al, bl ; current sector to read | ||
| call read_drive | ||
| add bl, 1 | ||
| cmp bl, ADDITIONAL_SECTORS_TO_READ | ||
| jle read | ||
| call enter_pm | ||
|
|
||
| ;;; function read_drive: | ||
| ;;; Reads one sector from the drive at the given LBA into the kernel memory | ||
| ;;; space at the appropriate offset (assuming 512 byte sectors). | ||
| ;;; Input: | ||
| ;;; [al] = LBA of sector to read | ||
| ;;; Output: | ||
| ;;; [cf] = Set on error | ||
| ;;; [ah] = status, as returned by INT 0x13/AH=0x02 | ||
| ;;; [al] = sectors transferred, as returned by INT 0x13/AH=0x02 | ||
| read_drive: | ||
| push cx | ||
| push bx | ||
| push ax | ||
|
|
||
| mov byte [READ_TRY_COUNT], 0 | ||
| read_drive_retry: | ||
| add byte [READ_TRY_COUNT], 1 | ||
| mov cx, 0 | ||
| mov es, cx ; Ensure segment register [es] is zero for interrupt | ||
| ;;; Calculate Offset for Kernel | ||
| mov cl, al | ||
| sub cl, 1 | ||
| shl cx, 9 | ||
| mov bx, OS_OFFSET | ||
| add bx, cx | ||
|
|
||
| ;;; Calculate C,H,S using algorithm from OSDev: | ||
| ;;; https://wiki.osdev.org/Disk_access_using_the_BIOS_(INT_13h) | ||
| mov ah, 0 ; Ensure top half of [ax] is zero so that [al] behaves as dividend | ||
| div byte [DRIVE_SECTS_PER_TRACK] | ||
| add ah, 1 | ||
| mov cl, ah | ||
| mov ah, 0 | ||
| div byte [DRIVE_N_HEADS] | ||
| mov dh, ah | ||
| mov ch, al | ||
| mov al, 1 | ||
| mov ah, 2 | ||
| mov dl, [BOOT_DRIVE] | ||
| clc | ||
| int 0x13 ;do read | ||
| call enter_pm | ||
| jnc read_drive_no_error | ||
|
|
||
| ;;; If an error occurred during the read, print out the following, separated by | ||
| ;;; commas: | ||
| ;;; - [al] : The actual number of sectors read. | ||
| ;;; - [ah] : The return code from interrupt 0x13. | ||
| ;;; - READ_TRY_COUNT : The amount of times a read has been tried so far. | ||
| ;;; - Original value of [al] when read_drive was called which is the sector | ||
| ;;; at which the read was attempted. | ||
| ;;; After printing this info, try reading again if the number of retries has not | ||
| ;;; exceeded MAX_READ_TRIES, otherwise stop. | ||
| call print_error | ||
| call print_byte | ||
| call print_comma | ||
| mov al, ah | ||
| call print_byte | ||
| call print_comma | ||
| mov bl, [READ_TRY_COUNT] | ||
| mov al, bl | ||
| call print_byte | ||
| call print_comma | ||
| mov ax, [esp] ; Restore [al] (LBA of sector to read from) | ||
| call print_byte | ||
| call print_newline | ||
| cmp bl, MAX_READ_TRIES | ||
| jl read_drive_retry | ||
| jmp $ | ||
|
|
||
| read_drive_no_error: | ||
| pop bx ; Pop old value of ax which is no longer needed | ||
| pop bx | ||
| pop cx | ||
| ret | ||
|
|
||
| ;;; function print_char: | ||
| ;;; Prints the character in register [al]. | ||
| print_char: | ||
| push ax | ||
| push bx | ||
| mov ah, 0x0e | ||
| mov bl, 1 | ||
| mov bh, 0 | ||
| int 0x10 | ||
| pop bx | ||
| pop ax | ||
| ret | ||
|
|
||
| ;;; function print_half_byte: | ||
| ;;; Print the 4 least significant bits in the register [al]. | ||
| print_half_byte: | ||
| pushfd | ||
| and al, 0x0F | ||
| add al, '0' | ||
| cmp al, '9' | ||
| jle skip_hex | ||
| add al, 7 | ||
| skip_hex: | ||
| call print_char | ||
| popfd | ||
| ret | ||
|
|
||
| ;;; function print_byte: | ||
| ;;; Print the byte in register [al]. | ||
| print_byte: | ||
| push ax | ||
|
|
||
| shr al, 4 | ||
| call print_half_byte | ||
| mov ax, [esp] | ||
| mov ah, 0 | ||
| call print_half_byte | ||
|
|
||
| pop ax | ||
| ret | ||
|
|
||
| ;;; function print_word: | ||
| ;;; Print a 16-bit word in the register [ax]. | ||
| print_word: | ||
| push ax | ||
| mov al, ah | ||
| call print_byte | ||
| mov ax, [esp] | ||
| call print_byte | ||
| pop ax | ||
| ret | ||
|
|
||
| ;;; function print_newline: | ||
| ;;; Print a newline character. Takes no arguments. | ||
| print_newline: | ||
| push ax | ||
| mov al, `\n` | ||
| call print_char | ||
| mov al, `\r` | ||
| call print_char | ||
| pop ax | ||
| ret | ||
|
|
||
| ;;; function print_comma: | ||
| ;;; Print a comma character. Takes no arguments. | ||
| print_comma: | ||
| push ax | ||
| mov al, ',' | ||
| call print_char | ||
| pop ax | ||
| ret | ||
|
|
||
| ;;; function print_error: | ||
| ;;; Print the string "ERROR: ". Takes no arguments. | ||
| print_error: | ||
| push ax | ||
| mov al, 'E' | ||
| call print_char | ||
| mov al, 'R' | ||
| call print_char | ||
| mov al, 'R' | ||
| call print_char | ||
| mov al, 'O' | ||
| call print_char | ||
| mov al, 'R' | ||
| call print_char | ||
| mov al, ':' | ||
| call print_char | ||
| mov al, ' ' | ||
| call print_char | ||
| pop ax | ||
| ret | ||
|
|
||
| [bits 32] | ||
| begin_pm: | ||
| call OS_OFFSET | ||
| hlt | ||
|
|
||
|
|
||
| times 509 - ($ - $$) db 0 ;padding | ||
|
|
||
| times 506 - ($ - $$) db 0 ;padding | ||
|
|
||
| READ_TRY_COUNT db 0 | ||
| DRIVE_N_HEADS db 0 | ||
| DRIVE_SECTS_PER_TRACK db 0 | ||
| BOOT_DRIVE db 0 ;0x7dfd | ||
| ;above is data that can always be found at 0x7dfd - n during boot process | ||
|
|
||
| dw 0xaa55 ;magic boot sector number | ||
| dw 0xaa55 ;magic boot sector number |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.