1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "misc.h"
16#include "error.h"
17#include "../string.h"
18#include "../voffset.h"
19
20
21
22
23
24
25
26
27
28#define STATIC static
29
30
31
32
33
34
35#undef memcpy
36#undef memset
37#define memzero(s, n) memset((s), 0, (n))
38#define memmove memmove
39
40
41void *memmove(void *dest, const void *src, size_t n);
42
43
44
45
46struct boot_params *boot_params;
47
48memptr free_mem_ptr;
49memptr free_mem_end_ptr;
50
51static char *vidmem;
52static int vidport;
53static int lines, cols;
54
55#ifdef CONFIG_KERNEL_GZIP
56#include "../../../../lib/decompress_inflate.c"
57#endif
58
59#ifdef CONFIG_KERNEL_BZIP2
60#include "../../../../lib/decompress_bunzip2.c"
61#endif
62
63#ifdef CONFIG_KERNEL_LZMA
64#include "../../../../lib/decompress_unlzma.c"
65#endif
66
67#ifdef CONFIG_KERNEL_XZ
68#include "../../../../lib/decompress_unxz.c"
69#endif
70
71#ifdef CONFIG_KERNEL_LZO
72#include "../../../../lib/decompress_unlzo.c"
73#endif
74
75#ifdef CONFIG_KERNEL_LZ4
76#include "../../../../lib/decompress_unlz4.c"
77#endif
78
79
80
81
82
83static void scroll(void)
84{
85 int i;
86
87 memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
88 for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
89 vidmem[i] = ' ';
90}
91
92#define XMTRDY 0x20
93
94#define TXR 0
95#define LSR 5
96static void serial_putchar(int ch)
97{
98 unsigned timeout = 0xffff;
99
100 while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
101 cpu_relax();
102
103 outb(ch, early_serial_base + TXR);
104}
105
106void __putstr(const char *s)
107{
108 int x, y, pos;
109 char c;
110
111 if (early_serial_base) {
112 const char *str = s;
113 while (*str) {
114 if (*str == '\n')
115 serial_putchar('\r');
116 serial_putchar(*str++);
117 }
118 }
119
120 if (lines == 0 || cols == 0)
121 return;
122
123 x = boot_params->screen_info.orig_x;
124 y = boot_params->screen_info.orig_y;
125
126 while ((c = *s++) != '\0') {
127 if (c == '\n') {
128 x = 0;
129 if (++y >= lines) {
130 scroll();
131 y--;
132 }
133 } else {
134 vidmem[(x + cols * y) * 2] = c;
135 if (++x >= cols) {
136 x = 0;
137 if (++y >= lines) {
138 scroll();
139 y--;
140 }
141 }
142 }
143 }
144
145 boot_params->screen_info.orig_x = x;
146 boot_params->screen_info.orig_y = y;
147
148 pos = (x + cols * y) * 2;
149 outb(14, vidport);
150 outb(0xff & (pos >> 9), vidport+1);
151 outb(15, vidport);
152 outb(0xff & (pos >> 1), vidport+1);
153}
154
155void __puthex(unsigned long value)
156{
157 char alpha[2] = "0";
158 int bits;
159
160 for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
161 unsigned long digit = (value >> bits) & 0xf;
162
163 if (digit < 0xA)
164 alpha[0] = '0' + digit;
165 else
166 alpha[0] = 'a' + (digit - 0xA);
167
168 __putstr(alpha);
169 }
170}
171
172static bool l5_supported(void)
173{
174
175 if (native_cpuid_eax(0) < 7)
176 return 0;
177
178
179 return native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31));
180}
181
182#if CONFIG_X86_NEED_RELOCS
183static void handle_relocations(void *output, unsigned long output_len,
184 unsigned long virt_addr)
185{
186 int *reloc;
187 unsigned long delta, map, ptr;
188 unsigned long min_addr = (unsigned long)output;
189 unsigned long max_addr = min_addr + (VO___bss_start - VO__text);
190
191
192
193
194
195 delta = min_addr - LOAD_PHYSICAL_ADDR;
196
197
198
199
200
201
202
203
204 map = delta - __START_KERNEL_map;
205
206
207
208
209
210
211 if (IS_ENABLED(CONFIG_X86_64))
212 delta = virt_addr - LOAD_PHYSICAL_ADDR;
213
214 if (!delta) {
215 debug_putstr("No relocation needed... ");
216 return;
217 }
218 debug_putstr("Performing relocations... ");
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
240 long extended = *reloc;
241 extended += map;
242
243 ptr = (unsigned long)extended;
244 if (ptr < min_addr || ptr > max_addr)
245 error("32-bit relocation outside of kernel!\n");
246
247 *(uint32_t *)ptr += delta;
248 }
249#ifdef CONFIG_X86_64
250 while (*--reloc) {
251 long extended = *reloc;
252 extended += map;
253
254 ptr = (unsigned long)extended;
255 if (ptr < min_addr || ptr > max_addr)
256 error("inverse 32-bit relocation outside of kernel!\n");
257
258 *(int32_t *)ptr -= delta;
259 }
260 for (reloc--; *reloc; reloc--) {
261 long extended = *reloc;
262 extended += map;
263
264 ptr = (unsigned long)extended;
265 if (ptr < min_addr || ptr > max_addr)
266 error("64-bit relocation outside of kernel!\n");
267
268 *(uint64_t *)ptr += delta;
269 }
270#endif
271}
272#else
273static inline void handle_relocations(void *output, unsigned long output_len,
274 unsigned long virt_addr)
275{ }
276#endif
277
278static void parse_elf(void *output)
279{
280#ifdef CONFIG_X86_64
281 Elf64_Ehdr ehdr;
282 Elf64_Phdr *phdrs, *phdr;
283#else
284 Elf32_Ehdr ehdr;
285 Elf32_Phdr *phdrs, *phdr;
286#endif
287 void *dest;
288 int i;
289
290 memcpy(&ehdr, output, sizeof(ehdr));
291 if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
292 ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
293 ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
294 ehdr.e_ident[EI_MAG3] != ELFMAG3) {
295 error("Kernel is not a valid ELF file");
296 return;
297 }
298
299 debug_putstr("Parsing ELF... ");
300
301 phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
302 if (!phdrs)
303 error("Failed to allocate space for phdrs");
304
305 memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
306
307 for (i = 0; i < ehdr.e_phnum; i++) {
308 phdr = &phdrs[i];
309
310 switch (phdr->p_type) {
311 case PT_LOAD:
312#ifdef CONFIG_X86_64
313 if ((phdr->p_align % 0x200000) != 0)
314 error("Alignment of LOAD segment isn't multiple of 2MB");
315#endif
316#ifdef CONFIG_RELOCATABLE
317 dest = output;
318 dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
319#else
320 dest = (void *)(phdr->p_paddr);
321#endif
322 memmove(dest, output + phdr->p_offset, phdr->p_filesz);
323 break;
324 default: break;
325 }
326 }
327
328 free(phdrs);
329}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
349 unsigned char *input_data,
350 unsigned long input_len,
351 unsigned char *output,
352 unsigned long output_len)
353{
354 const unsigned long kernel_total_size = VO__end - VO__text;
355 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
356
357
358 boot_params = rmode;
359
360
361 boot_params->hdr.loadflags &= ~KASLR_FLAG;
362
363 sanitize_boot_params(boot_params);
364
365 if (boot_params->screen_info.orig_video_mode == 7) {
366 vidmem = (char *) 0xb0000;
367 vidport = 0x3b4;
368 } else {
369 vidmem = (char *) 0xb8000;
370 vidport = 0x3d4;
371 }
372
373 lines = boot_params->screen_info.orig_video_lines;
374 cols = boot_params->screen_info.orig_video_cols;
375
376 console_init();
377 debug_putstr("early console in extract_kernel\n");
378
379 if (IS_ENABLED(CONFIG_X86_5LEVEL) && !l5_supported()) {
380 error("This linux kernel as configured requires 5-level paging\n"
381 "This CPU does not support the required 'cr4.la57' feature\n"
382 "Unable to boot - please use a kernel appropriate for your CPU\n");
383 }
384
385 free_mem_ptr = heap;
386 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
387
388
389 debug_putaddr(input_data);
390 debug_putaddr(input_len);
391 debug_putaddr(output);
392 debug_putaddr(output_len);
393 debug_putaddr(kernel_total_size);
394
395
396
397
398
399
400 choose_random_location((unsigned long)input_data, input_len,
401 (unsigned long *)&output,
402 max(output_len, kernel_total_size),
403 &virt_addr);
404
405
406 if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
407 error("Destination physical address inappropriately aligned");
408 if (virt_addr & (MIN_KERNEL_ALIGN - 1))
409 error("Destination virtual address inappropriately aligned");
410#ifdef CONFIG_X86_64
411 if (heap > 0x3fffffffffffUL)
412 error("Destination address too large");
413 if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
414 error("Destination virtual address is beyond the kernel mapping area");
415#else
416 if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
417 error("Destination address too large");
418#endif
419#ifndef CONFIG_RELOCATABLE
420 if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
421 error("Destination address does not match LOAD_PHYSICAL_ADDR");
422 if (virt_addr != LOAD_PHYSICAL_ADDR)
423 error("Destination virtual address changed when not relocatable");
424#endif
425
426 debug_putstr("\nDecompressing Linux... ");
427 __decompress(input_data, input_len, NULL, NULL, output, output_len,
428 NULL, error);
429 parse_elf(output);
430 handle_relocations(output, output_len, virt_addr);
431 debug_putstr("done.\nBooting the kernel.\n");
432 return output;
433}
434
435void fortify_panic(const char *name)
436{
437 error("detected buffer overflow");
438}
439