summaryrefslogtreecommitdiff
path: root/xxx/boot1/mbr.asm
blob: c0a555df47ff1070bf59f855600b6d1389c6673e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; FOOL-OS Master Boot Record
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This is the Master Boot Record for x86
;
; We are limited to 512 bytes  (one sector)
;   minus 64 bytes for the partition table
;   minus 2 bytes formagic number (0xaa55)
; 
; all we do here is :
;   
;   1. Put the Stack at 0x07bff (it is counting down)
;   2. Remeber Boot Drive at [BOOT_DRIVE]
;   3. Print PR message and Boot Drive number
;   4. Load Second Stage Bootloader from Boot Drive at next sector
;   5. Show Info Message after successfull loading
;   6. Jump to 2nd Stage Boot Loader
;
; Refer to a memory map as needed:
; http://wiki.osdev.org/Memory_Map_(x86)

; Everything here is 16bit
[bits 16]

; Per definition this will be loaded by the BIOS at 0x7c00
[org 0x7c00]

; skip constants and includes
jmp stage1

; string constants (null terminated)
STR_1:	    db "Fool Loader Stage 1. v0.1",0
STR_2:	    db "Starting Stage 2",0
STR_BOOT:   db "Boot drive: ",0

; some space (one byte) to remember the Boot Drive
BOOT_DRIVE: db 0xff

; include print and disk load routines
%include "print_string_16.asm"
%include "disk_load_16.asm"

; Here we start!
stage1:

    ; first of all, setup the stack (right under our MBR)
    ; ~30KB space guaranteed
    mov bp,0x07bff
    mov sp,bp

    ; remember BOOT_DRIVE (as was set by BIOS in dl)
    mov [BOOT_DRIVE],dl	

    ; clear screen and print PR message
    call print_clear
    call print_nextline

    mov bx, STR_1
    call print_string
    call print_nextline


    ; print bootdrive number
    mov bx, STR_BOOT
    call print_string

    mov al,[BOOT_DRIVE]
    call print_hex_byte
    call print_nextline


    ; Actually Load the Second Stage Bootloader!
    call disk_load_16


    ; show info message that 2nd Stage was loaded
    mov bx, STR_2
    call print_string
    call print_nextline
    call print_nextline


    ; save Boot Drive in dl for second stage
    mov dl,[BOOT_DRIVE]


    ; jump to the next sector where we loaded the 2nd stage
    jmp 0x7e00


    ; nothing essential under this line
    ; we need this to follow some mbr standards

    ; fill at least 4x16byte with zeroes. (partition table)
    ; (otherwise my Acer Aspire will not boot)
    times 64 db 0x0

    ; fill rest with zeroes
    times 510-($-$$) db 0x0

    ; magic number so we get identified as MBR
    dw 0xaa55