1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <types.h>
22#include <io.h>
23#include <console.h>
24#include <cpu.h>
25#include <globalvars.h>
26#include <lar.h>
27#include <string.h>
28#include <tables.h>
29#include <lib.h>
30#include <mc146818rtc.h>
31#include <cpu.h>
32#include <multiboot.h>
33#include <stage1.h>
34
35#ifdef CONFIG_PAYLOAD_ELF_LOADER
36
37
38
39#define UNCOMPRESS_AREA (0x400000)
40#endif
41
42
43void uart_init(void);
44void die(const char *msg);
45void hardware_stage1(void);
46void disable_car(void);
47void mainboard_pre_payload(void);
48
49static void enable_rom(void)
50{
51
52 post_code(POST_STAGE1_ENABLE_ROM);
53}
54
55void init_archive(struct mem_file *archive)
56{
57
58
59
60
61
62
63
64 archive->len = *(u32 *)0xfffffff4;
65 archive->start =(void *)(0UL-archive->len);
66
67
68
69}
70
71
72
73
74
75
76void *bottom_of_stack(void)
77{
78 u32 onstack = (u32)&onstack;
79
80
81
82
83
84 if ((onstack >= CAR_STACK_BASE - CAR_STACK_SIZE) &&
85 (onstack < CAR_STACK_BASE))
86 return (void *)CAR_STACK_BASE;
87
88 return (void *)RAM_STACK_BASE;
89}
90
91struct global_vars *global_vars(void)
92{
93 return *(struct global_vars **)(bottom_of_stack() - sizeof(struct global_vars *));
94}
95
96void global_vars_init(struct global_vars *globvars)
97{
98 memset(globvars, 0, sizeof(struct global_vars));
99 *(struct global_vars **)(bottom_of_stack() - sizeof(struct global_vars *)) = globvars;
100#ifdef CONFIG_CONSOLE_BUFFER
101
102 printk_buffer_init();
103#endif
104 console_loglevel_init();
105
106}
107
108#ifdef CONFIG_CHECK_STACK_USAGE
109
110
111
112
113#define STACKFILL_BYTE 0x0
114void check_stack()
115{
116 unsigned long stacksize, i;
117 char *lowestaddr;
118 if (global_vars()->ram_available)
119 stacksize = RAM_STACK_SIZE;
120 else
121 stacksize = CAR_STACK_SIZE;
122 lowestaddr = bottom_of_stack() - stacksize;
123 for (i = 0; i < stacksize; i++)
124 if (lowestaddr[i] != STACKFILL_BYTE)
125 break;
126 global_vars()->loweststack = lowestaddr + i;
127}
128#endif
129
130void dump_mem_range(int msg_level, unsigned char *buf, int size)
131{
132 int i;
133 printk(msg_level, "dumping memrange %p size %i:\n", buf, size);
134 for (i = 0; i < size; i++) {
135 printk(msg_level, "%02x ", buf[i]);
136 if (i % 16 == 15)
137 printk(msg_level, "\n");
138 }
139 return;
140}
141
142
143
144
145
146u64 cycles(void)
147{
148 u64 ret;
149 asm volatile ("rdtsc" : "=A" (ret));
150 return ret;
151}
152
153#ifdef CONFIG_PAYLOAD_ELF_LOADER
154
155int legacy(struct mem_file *archive, char *name, void *where, struct lb_memory *mem)
156{
157 int ret;
158 int elfboot_mem(struct lb_memory *mem, void *where, int size);
159 ret = copy_file(archive, name, where);
160 if (ret) {
161 printk(BIOS_ERR, "'%s' found, but could not load it.\n", name);
162 }
163
164 ret = elfboot_mem(mem, where, archive->reallen);
165
166 printk(BIOS_ERR, "elfboot_mem returns %d\n", ret);
167 return -1;
168}
169#endif
170
171
172static int run_address_multiboot(void *f, struct multiboot_info *mbi)
173{
174 int ret, dummy;
175 __asm__ __volatile__ ("call *%4" : "=a" (ret), "=c" (dummy) : "a" (MB_MAGIC2), "b" (mbi), "c" (f) : "edx", "memory");
176 return ret;
177}
178
179
180
181
182
183
184
185
186
187
188
189
190void __attribute__((stdcall, regparm(0))) stage1_phase1(u32 bist, u32 init_detected, u32 cpu)
191{
192 struct global_vars globvars;
193 int ret;
194 struct mem_file archive;
195
196 post_code(POST_STAGE1_MAIN);
197
198
199
200 if (cpu == 0) {
201
202
203 global_vars_init(&globvars);
204 globvars.init_detected = init_detected;
205
206 hardware_stage1();
207
208 uart_init();
209
210
211
212
213 console_init();
214
215#ifdef CONFIG_CHECK_STACK_USAGE
216 printk(BIOS_DEBUG, "Initial lowest stack is %p\n",
217 global_vars()->loweststack);
218#endif
219
220 enable_rom();
221 }
222
223 if (bist != 0) {
224 printk(BIOS_INFO, "BIST FAILED: %08x on CPU: %08x", bist, cpu);
225 die("");
226 }
227
228 init_archive(&archive);
229
230
231 if (check_normal_boot_flag()) {
232 printk(BIOS_DEBUG, "Choosing normal boot.\n");
233 ret = execute_in_place(&archive, "normal/initram/segment0");
234 } else {
235 printk(BIOS_DEBUG, "Choosing fallback boot.\n");
236 ret = execute_in_place(&archive, "fallback/initram/segment0");
237
238
239
240
241 if (ret) {
242 printk(BIOS_DEBUG, "Fallback failed. Try normal boot\n");
243 ret = execute_in_place(&archive, "normal/initram/segment0");
244 }
245 }
246
247 if (ret)
248 die("Failed RAM init code\n");
249
250 printk(BIOS_DEBUG, "Done RAM init code\n");
251#ifdef CONFIG_CHECK_STACK_USAGE
252 printk(BIOS_DEBUG, "After RAM init, lowest stack is %p\n",
253 global_vars()->loweststack);
254#endif
255
256
257
258
259
260
261 stage1_phase2();
262
263
264 die("The world is broken.\n");
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286void stage1_phase2(void)
287{
288#ifdef CONFIG_CONSOLE_BUFFER
289
290 printk_buffer_move((void *)PRINTK_BUF_ADDR_RAM, PRINTK_BUF_SIZE_RAM);
291 printk(BIOS_DEBUG, "Done printk() buffer move\n");
292#endif
293
294 disable_car();
295
296
297 die("The world is broken.\n");
298}
299
300
301
302
303
304void __attribute__((stdcall)) stage1_phase3(void)
305{
306 void *entry;
307 struct mem_file archive;
308 struct multiboot_info *mbi;
309
310#ifdef CONFIG_PAYLOAD_ELF_LOADER
311 int ret;
312 struct mem_file result;
313 int elfboot_mem(struct lb_memory *mem, void *where, int size);
314
315
316 unsigned char faker[64];
317 struct lb_memory *mem = (struct lb_memory*) faker;
318
319 mem->tag = LB_TAG_MEMORY;
320 mem->size = 28;
321 mem->map[0].start.lo = mem->map[0].start.hi = 0;
322 mem->map[0].size.lo = (32*1024*1024);
323 mem->map[0].size.hi = 0;
324 mem->map[0].type = LB_MEM_RAM;
325#endif
326
327
328 global_vars()->ram_available = 1;
329
330
331 init_archive(&archive);
332
333 entry = load_file_segments(&archive, "normal/stage2");
334 if (entry == (void *)-1)
335 die("FATAL: Failed loading stage2.");
336 mbi = (struct multiboot_info*) run_address(entry);
337 if (! mbi)
338 die("FATAL: Failed in stage2 code.");
339
340 printk(BIOS_DEBUG, "Stage2 code done.\n");
341
342#ifdef CONFIG_PAYLOAD_ELF_LOADER
343 ret = find_file(&archive, "normal/payload", &result);
344 if (! ret)
345 legacy(&archive, "normal/payload", (void *)UNCOMPRESS_AREA, mem);
346#endif
347
348 entry = load_file_segments(&archive, "normal/payload");
349#ifdef CONFIG_CHECK_STACK_USAGE
350 printk(BIOS_DEBUG, "Before handoff to payload, lowest stack is %p\n",
351 global_vars()->loweststack);
352#endif
353 if (entry != (void*)-1) {
354
355 mainboard_pre_payload();
356 run_address_multiboot(entry, mbi);
357 } else {
358 die("FATAL: No usable payload found.\n");
359 }
360 die ("FATAL: Last stage returned to coreboot.\n");
361}
362
363
364