root / prex-0.9.0 / bsp / boot / x86 / pc / head.S @ 03e9c04a
History | View | Annotate | Download (5.56 KB)
1 |
/*- |
---|---|
2 |
* Copyright (c) 2005-2007, Kohsuke Ohtani |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* 3. Neither the name of the author nor the names of any co-contributors |
14 |
* may be used to endorse or promote products derived from this software |
15 |
* without specific prior written permission. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
* SUCH DAMAGE. |
28 |
*/ |
29 |
|
30 |
/* |
31 |
* head.S - low level platform support |
32 |
*/ |
33 |
|
34 |
#include <conf/config.h> |
35 |
#include <machine/memory.h> |
36 |
#include <machine/syspage.h> |
37 |
|
38 |
#define SCREEN_80x25 1 |
39 |
/* #define SCREEN_80x50 1 */ |
40 |
|
41 |
#define SEL_CODE32 0x10 |
42 |
#define SEL_DATA32 0x18 |
43 |
|
44 |
#define ENTRY(x) .global x; .align; x##: |
45 |
|
46 |
.text |
47 |
.code16 |
48 |
|
49 |
/* |
50 |
* boot_entry - Entry point for prex boot loader. |
51 |
* |
52 |
* We assume boot sector copied us at 0x30000. |
53 |
*/ |
54 |
ENTRY(boot_entry) |
55 |
cld |
56 |
cli /* Disable all interrupts */ |
57 |
|
58 |
/* |
59 |
* Relocate boot loader to 0x4000. |
60 |
*/ |
61 |
movw %cs, %ax |
62 |
movw %ax, %ds |
63 |
xorw %si, %si /* Source address cs:0 */ |
64 |
movw %si, %es |
65 |
movw $0x4000, %di /* Destination */ |
66 |
movw $0x800, %cx /* size 8K */ |
67 |
rep movsl |
68 |
ljmp $0x0000, $(reset_cs) |
69 |
reset_cs: |
70 |
movw %cs, %ax /* Reset segment registers */ |
71 |
movw %ax, %ds |
72 |
|
73 |
xorw %ax, %ax /* Reset stack */ |
74 |
movw %ax, %ss |
75 |
movw $(BOOTSTKTOP - KERNBASE), %sp |
76 |
|
77 |
/* |
78 |
* Do 16-bit BIOS call here. |
79 |
*/ |
80 |
call setup_screen |
81 |
call get_memsize |
82 |
|
83 |
/* |
84 |
* Switch to 32-bit protected mode. |
85 |
*/ |
86 |
call enable_a20 /* Enable A20 line */ |
87 |
lgdt gdt_desc /* Load GDT */ |
88 |
|
89 |
movl %cr0, %eax /* Set PE bit in CR0 */ |
90 |
orl $0x1, %eax |
91 |
movl %eax, %cr0 |
92 |
|
93 |
.byte 0x66 /* 32-bit long jump to reset CS */ |
94 |
.byte 0xea |
95 |
.long go_prot |
96 |
.word SEL_CODE32 |
97 |
.code32 |
98 |
.align 4 |
99 |
go_prot: |
100 |
movw $(SEL_DATA32), %ax /* Reset all segments */ |
101 |
movw %ax, %ds |
102 |
movw %ax, %es |
103 |
movw %ax, %ss |
104 |
movw %ax, %fs |
105 |
movw %ax, %gs |
106 |
|
107 |
/* |
108 |
* Relocate OS image. |
109 |
*/ |
110 |
movl $(CONFIG_BOOTIMG_BASE - KERNBASE), %edi /* Destination */ |
111 |
movl $0x32000, %esi /* Source address: 0x32000 */ |
112 |
movl $0x1B800, %ecx /* size 0x6e000 */ |
113 |
rep movsl |
114 |
|
115 |
jmp main /* Jump to main routine in C */ |
116 |
|
117 |
|
118 |
/* |
119 |
* get_memsize - Get memory size |
120 |
*/ |
121 |
.code16 |
122 |
get_memsize: |
123 |
xorl %eax, %eax |
124 |
int $0x12 /* Get conventional memory size */ |
125 |
movl %eax, lo_mem /* ax = K bytes */ |
126 |
|
127 |
mov $0x88, %ah |
128 |
int $0x15 |
129 |
andb $0xfc, %al /* Adjust to page boundary */ |
130 |
movl %eax, hi_mem /* ax = K bytes at 100000h */ |
131 |
ret |
132 |
|
133 |
/* |
134 |
* enable_a20 - Enable A20 |
135 |
*/ |
136 |
.code16 |
137 |
enable_a20: |
138 |
call empty_8042 |
139 |
movb $0xd1, %al |
140 |
outb %al, $0x64 |
141 |
call empty_8042 |
142 |
movb $0xdf, %al |
143 |
outb %al, $0x60 |
144 |
call empty_8042 |
145 |
call wait_a20 |
146 |
ret |
147 |
|
148 |
/* |
149 |
* empty_8042 - Empty 8042 |
150 |
*/ |
151 |
empty_8042: |
152 |
inb $0x64, %al |
153 |
testb $0x01, %al |
154 |
jz no_output |
155 |
inb $0x60, %al |
156 |
jmp empty_8042 |
157 |
no_output: |
158 |
testb $0x02, %al |
159 |
jnz empty_8042 |
160 |
ret |
161 |
|
162 |
/* |
163 |
* wait_a20 - Wait A20 ready |
164 |
*/ |
165 |
wait_a20: |
166 |
xorw %ax, %ax |
167 |
movw %ax, %fs |
168 |
movw $0xffff, %ax |
169 |
movw %ax, %gs |
170 |
movw %fs:(0x0), %ax |
171 |
cmp %gs:(16), %ax |
172 |
jne a20_ready |
173 |
movw %dx, %ax |
174 |
notw %ax |
175 |
movw %ax, %fs:(0x0) |
176 |
cmp %gs:(16), %ax |
177 |
mov %fs:(0), %dx |
178 |
jne a20_ready |
179 |
jmp wait_a20 |
180 |
a20_ready: |
181 |
ret |
182 |
|
183 |
/* |
184 |
* Setup screen |
185 |
*/ |
186 |
setup_screen: |
187 |
pushaw |
188 |
pushw %es |
189 |
pushw %ds |
190 |
pushw %bp |
191 |
movb $0x2e, %al /* print '.' for verify */ |
192 |
movb $0x0e, %ah |
193 |
movw $0x07, %bx |
194 |
int $0x10 |
195 |
|
196 |
movw $0x3, %ax /* Use mode-3 */ |
197 |
int $0x10 |
198 |
movw $0x1202, %ax /* 400 scan lines */ |
199 |
movb $0x30, %bl |
200 |
int $0x10 |
201 |
#if SCREEN_80x50 |
202 |
movw $0x1112, %ax /* Load 8x8 character set */ |
203 |
movb $0x0, %bl |
204 |
int $0x10 |
205 |
movw $0x1201, %ax /* Turn off cursor emulation */ |
206 |
movb $0x34, %bl |
207 |
int $0x10 |
208 |
movb $0x01, %ah /* Set cursor type */ |
209 |
movw $0x0607, %cx |
210 |
int $0x10 |
211 |
#endif |
212 |
popw %bp |
213 |
popw %ds |
214 |
popw %es |
215 |
popaw |
216 |
ret |
217 |
|
218 |
.code32 |
219 |
|
220 |
ENTRY(outb) |
221 |
movl 4(%esp), %edx |
222 |
movl 8(%esp), %eax |
223 |
outb %al, %dx |
224 |
ret |
225 |
|
226 |
ENTRY(inb) |
227 |
movl 4(%esp), %edx |
228 |
xorl %eax, %eax |
229 |
inb %dx, %al |
230 |
ret |
231 |
|
232 |
/* |
233 |
* Data |
234 |
*/ |
235 |
.align 16 |
236 |
gdt: |
237 |
.word 0x0,0x0,0x0,0x0 /* 0x00 - Null descritor */ |
238 |
.word 0x0,0x0,0x0,0x0 /* 0x08 - Null descritor */ |
239 |
.word 0xffff,0x0,0x9a00,0xcf /* 0x10 - 32 bit code segment */ |
240 |
.word 0xffff,0x0,0x9200,0xcf /* 0x18 - 32 bit data segment */ |
241 |
.word 0xffff,0x0,0x9a00,0x0 /* 0x20 - 16 bit code segment */ |
242 |
.word 0xffff,0x0,0x9200,0x0 /* 0x28 - 16 bit data segment */ |
243 |
|
244 |
gdt_desc: |
245 |
.word 0x2F /* limit */ |
246 |
.long gdt /* address */ |
247 |
|
248 |
.word 0x0 /* alignment */ |
249 |
idt_desc: |
250 |
.word 0x0 |
251 |
.long 0x0 |
252 |
|
253 |
e820_buf: |
254 |
.space 20 |
255 |
|
256 |
.align 16 |
257 |
.global lo_mem, hi_mem |
258 |
lo_mem: |
259 |
.long 0x0 |
260 |
hi_mem: |
261 |
.long 0x0 |
262 |
|
263 |
/* |
264 |
* Pad data |
265 |
*/ |
266 |
.section .tail,"a" |
267 |
dummy: |
268 |
.byte 0xff |