1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "boot.h"
17#include "string.h"
18
19struct boot_params boot_params __attribute__((aligned(16)));
20
21char *HEAP = _end;
22char *heap_end = _end;
23
24
25
26
27
28
29
30static void copy_boot_params(void)
31{
32 struct old_cmdline {
33 u16 cl_magic;
34 u16 cl_offset;
35 };
36 const struct old_cmdline * const oldcmd =
37 (const struct old_cmdline *)OLD_CL_ADDRESS;
38
39 BUILD_BUG_ON(sizeof boot_params != 4096);
40 memcpy(&boot_params.hdr, &hdr, sizeof hdr);
41
42 if (!boot_params.hdr.cmd_line_ptr &&
43 oldcmd->cl_magic == OLD_CL_MAGIC) {
44
45 u16 cmdline_seg;
46
47
48
49
50 if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
51 cmdline_seg = ds();
52 else
53 cmdline_seg = 0x9000;
54
55 boot_params.hdr.cmd_line_ptr =
56 (cmdline_seg << 4) + oldcmd->cl_offset;
57 }
58}
59
60
61
62
63
64
65static void keyboard_init(void)
66{
67 struct biosregs ireg, oreg;
68 initregs(&ireg);
69
70 ireg.ah = 0x02;
71 intcall(0x16, &ireg, &oreg);
72 boot_params.kbd_status = oreg.al;
73
74 ireg.ax = 0x0305;
75 intcall(0x16, &ireg, NULL);
76}
77
78
79
80
81static void query_ist(void)
82{
83 struct biosregs ireg, oreg;
84
85
86
87 if (cpu.level < 6)
88 return;
89
90 initregs(&ireg);
91 ireg.ax = 0xe980;
92 ireg.edx = 0x47534943;
93 intcall(0x15, &ireg, &oreg);
94
95 boot_params.ist_info.signature = oreg.eax;
96 boot_params.ist_info.command = oreg.ebx;
97 boot_params.ist_info.event = oreg.ecx;
98 boot_params.ist_info.perf_level = oreg.edx;
99}
100
101
102
103
104static void set_bios_mode(void)
105{
106#ifdef CONFIG_X86_64
107 struct biosregs ireg;
108
109 initregs(&ireg);
110 ireg.ax = 0xec00;
111 ireg.bx = 2;
112 intcall(0x15, &ireg, NULL);
113#endif
114}
115
116static void init_heap(void)
117{
118 char *stack_end;
119
120 if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
121 asm("leal %P1(%%esp),%0"
122 : "=r" (stack_end) : "i" (-STACK_SIZE));
123
124 heap_end = (char *)
125 ((size_t)boot_params.hdr.heap_end_ptr + 0x200);
126 if (heap_end > stack_end)
127 heap_end = stack_end;
128 } else {
129
130 puts("WARNING: Ancient bootloader, some functionality "
131 "may be limited!\n");
132 }
133}
134
135void main(void)
136{
137
138 copy_boot_params();
139
140
141 console_init();
142 if (cmdline_find_option_bool("debug"))
143 puts("early console in setup code\n");
144
145
146 init_heap();
147
148
149 if (validate_cpu()) {
150 puts("Unable to boot - please use a kernel appropriate "
151 "for your CPU.\n");
152 die();
153 }
154
155
156 set_bios_mode();
157
158
159 detect_memory();
160
161
162 keyboard_init();
163
164
165 query_ist();
166
167
168#if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
169 query_apm_bios();
170#endif
171
172
173#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
174 query_edd();
175#endif
176
177
178 set_video();
179
180
181 go_to_protected_mode();
182}
183