backported fixes from 256boss
[bootcensus] / src / boot / boot.s
1 # pcboot - bootable PC demo/game kernel
2 # Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY, without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17         .code16
18         .section .boot,"ax"
19
20         .set stack_top, 0x7be0
21         .set read_retries, 0x7be8
22         .set drive_number, 0x7bec
23         .set cursor_x, 0x7bee
24         .set scratchbuf, 0x7bf0
25         .set scratchbuf_size, 16
26
27 boot:
28         cli
29         # move stack to just below the code
30         xor %ax, %ax
31         mov %ax, %ss
32         mov $stack_top, %sp
33         # use the code segment for data access
34         mov %cs, %ax
35         mov %ax, %ds
36         mov %ax, %es
37
38         mov %dl, drive_number
39
40         call get_drive_chs
41
42         mov $loading_msg, %si
43         call print_str
44
45         # load the second stage boot loader and jump to it
46         mov $_boot2_size, %eax
47         call print_num
48
49         mov $_boot2_size, %eax
50         mov %eax, %ebx
51         shr $9, %eax
52         and $0x1ff, %ebx
53         jz 0f
54         inc %ax
55 0:      pushw %ax
56         pushw $1
57         # set es to the start of the destination buffer to allow reading in
58         # full 64k chunks
59         mov $boot2_addr, %bx
60         shr $4, %bx
61         mov %bx, %es
62         xor %bx, %bx
63         call read_sectors
64         jmp boot2_addr
65
66 loading_msg: .asciz "\nLoad "
67 driveno_msg: .asciz "Drv:"
68
69         .global sect_per_track
70 sect_per_track: .short 18
71         .global num_cylinders
72 num_cylinders: .short 80
73         .global num_heads
74 num_heads: .short 2
75         .global heads_mask
76 heads_mask: .byte 1
77
78 get_drive_chs:
79         mov $driveno_msg, %si
80         call print_str
81         xor %eax, %eax
82         movb drive_number, %dl
83         mov %dl, %al
84         call print_num
85         mov $10, %al
86         call print_char
87
88         mov $8, %ah
89         int $0x13
90         jnc ok
91         ret
92
93 ok:     xor %eax, %eax
94         mov %ch, %al
95         mov %cl, %ah
96         rol $2, %ah
97         inc %ax
98         and $0x3ff, %ax
99         mov %ax, num_cylinders
100
101         and $0x3f, %cx
102         mov %cx, sect_per_track
103
104         shr $8, %dx
105         mov %dl, heads_mask
106         inc %dx
107         mov %dx, num_heads
108
109         call print_num
110         mov $47, %al
111         call print_char
112         mov %dx, %ax
113         call print_num
114         mov $47, %al
115         call print_char
116         mov %cx, %ax
117         call print_num
118         ret
119
120
121         .set ARG_NSECT, 6
122         .set ARG_SIDX, 4
123
124 # read_sectors(first, num)
125 read_sectors:
126         push %bp
127         mov %sp, %bp
128
129         mov ARG_SIDX(%bp), %ax
130         xor %cx, %cx
131
132         jmp 1f
133 0:      push %ax
134         call read_sector
135         pop %ax
136         inc %ax
137         inc %cx
138 1:      cmp ARG_NSECT(%bp), %cx
139         jnz 0b
140
141         pop %bp
142         ret
143
144 # read_sector(sidx)
145 read_sector:
146         push %bp
147         mov %sp, %bp
148         push %cx
149         push %dx
150
151         movw $3, read_retries
152
153 read_try:
154         # calculate the track (sidx / sectors_per_track)
155         mov 4(%bp), %ax
156
157         xor %dx, %dx
158         mov sect_per_track, %cx
159         div %cx
160         mov %ax, %cx
161         # save the remainder
162         push %dx
163         # head in dh
164         mov %cl, %dh
165         and heads_mask, %dh
166         # cylinder (track/heads) in ch [0-7] and cl[6,7]<-[8,9]
167         push %dx
168         xor %dx, %dx
169         movw num_heads, %cx
170         div %cx
171         pop %dx
172         mov %ax, %cx
173         rol $8, %cx
174         ror $2, %cl
175         and $0xc0, %cl
176         # sector num cl[0-5] is sidx % sectors_per_track + 1
177         pop %ax
178         inc %al
179         or %al, %cl
180
181         # ah = 2 (read), al = 1 sectors
182         mov $0x0201, %ax
183         movb drive_number, %dl
184         int $0x13
185         jnc read_ok
186
187         # abort after 3 attempts
188         decw read_retries
189         jz read_fail
190
191         # error detected, reset controller and retry
192         xor %ah, %ah
193         int $0x13
194         jmp read_try
195
196 read_fail:
197         mov 4(%bp), %ax
198         jmp abort_read
199
200 read_ok:
201         mov $46, %ax
202         call print_char
203
204         # increment es:bx accordingly (advance es if bx overflows)
205         add $512, %bx
206         jnc 0f
207         mov %es, %ax
208         add $4096, %ax
209         mov %ax, %es
210
211 0:      pop %dx
212         pop %cx
213         pop %bp
214         ret
215
216 str_read_error: .asciz "rderr:"
217
218 abort_read:
219         mov $str_read_error, %si
220         call print_str
221         and $0xffff, %eax
222         call print_num
223         mov $10, %al
224         call print_char
225         hlt
226
227         # expects string pointer in ds:si
228 print_str:
229         pusha
230
231 0:      mov (%si), %al
232         cmp $0, %al
233         jz end
234         call print_char
235         inc %si
236         jmp 0b
237
238 end:    popa
239         ret
240
241         # expects character in al
242 print_char:
243         push %es
244
245         cmp $10, %ax
246         jnz 0f
247         mov $32, %ax
248
249 0:      pushw $0xb800
250         pop %es
251         movw cursor_x, %di
252         shl $1, %di
253
254         mov %al, %es:(%di)
255         movb $7, %es:1(%di)
256         incw cursor_x
257
258         pop %es
259         ret
260
261         # expects number in eax
262         .global print_num
263 print_num:
264         # save registers
265         pusha
266
267         movw $scratchbuf + scratchbuf_size, %si
268         movb $0, (%si)
269         mov $10, %ebx
270 convloop:
271         xor %edx, %edx
272         div %ebx
273         add $48, %dl
274         dec %si
275         mov %dl, (%si)
276         cmp $0, %eax
277         jnz convloop
278
279         call print_str
280
281         # restore regs
282         popa
283         ret
284
285         .org 510
286         .byte 0x55
287         .byte 0xaa
288 boot2_addr: