summaryrefslogtreecommitdiff
path: root/stage1/main.asm
blob: 2f8c60c81ba79db931c4c6849ee6d2cc0b4629ae (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
[org 0x7C00]

%define KSTART 0x7E00

boot:
	; init segment registers
	xor ax, ax
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax

	; init stack
	mov bp, 0x7C00
	mov sp, bp

	; print message
	mov ebx, .msg
	call print_str

	; print boot drive
	pusha
	movzx eax, dl
	call print_hex
	call newline
	popa

	; load stage2 and stage3
	call load_stages

	; reclaim MBR space for stack
	mov bp, KSTART
	mov sp, bp

	; pass offset of the file system as argument
	mov edi, KSIZE+512
	; jump into stage2
	jmp KSTART

.msg: db 10, 13, "cuddles stage1", 10, 13, "boot drive: 0x", 0

load_stages:
	mov ebx, .msg
	call print_str

	; mov si, 18 ; sectors per track
	; mov di, 2  ; number of heads
	; check if hard drive
	; test dl, 0x80
	; jz .start_load ; use defaults if not a hard drive

	; backup dl
	push dx

	; get geometry
	mov ah, 8
	int 0x13

	; restore dl
	pop ax
	mov dl, al

	movzx si, cl
	and si, 0x3f

	movzx di, dh
	inc di

.start_load:
	pusha

	mov ebx, .sectors
	call print_str
	movzx eax, si
	call print_dec
	call newline

	mov ebx, .heads
	call print_str
	movzx eax, di
	call print_dec
	call newline

	popa

	; setup buffer
	mov ax, KSTART/0x10
	mov es, ax
	mov bx, 0

	mov cl, 2       ; sector=2
	mov dh, 0       ; head=0
	mov ch, 0       ; cylinder=0

.load:
	mov ah, 0x02    ; read sectors from drive
	mov al, 1       ; number of sectors
	int 0x13
	jc .fail        ; CF set on error

	; check read sectors count
	cmp al, 1
	jne .fail

	; increase buffer pointer
	mov ax, es
	add ax, 512/0x10
	mov es, ax

	; check if finished
	cmp ax, (KSTART+KSIZE)/0x10
	jae .success

	; next sector
	inc cl
	movzx ax, cl
	cmp ax, si
	jbe .load

	; next head
	mov cl, 1
	inc dh
	movzx ax, dh
	cmp ax, di
	jb .load

	; next cylinder
	mov dh, 0
	inc ch
	jmp .load

.success:
	; restore es
	mov ax, 0
	mov es, ax

	ret

.fail:
	mov ebx, .fail_msg
	call print_str
	hlt

.msg: db "loading stage2 and stage3 from disk", 10, 13, 0
.fail_msg: db "disk failure, try rebooting", 10, 13, 0
.sectors: db "sectors per track: ", 0
.heads: db "number of heads: ", 0

%include "stage1/print.asm"

times 440-($-$$) db 0
db "hugs"

times 510-($-$$) db 0
dw 0xAA55