]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/boot/compressed/misc.c
Merge remote-tracking branches 'asoc/topic/cs35l32', 'asoc/topic/cs35l34', 'asoc...
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / boot / compressed / misc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * misc.c
4 *
5 * This is a collection of several routines used to extract the kernel
6 * which includes KASLR relocation, decompression, ELF parsing, and
7 * relocation processing. Additionally included are the screen and serial
8 * output functions and related debugging support functions.
9 *
10 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
11 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
12 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
13 */
14
15 #include "misc.h"
16 #include "error.h"
17 #include "../string.h"
18 #include "../voffset.h"
19
20 /*
21 * WARNING!!
22 * This code is compiled with -fPIC and it is relocated dynamically at
23 * run time, but no relocation processing is performed. This means that
24 * it is not safe to place pointers in static structures.
25 */
26
27 /* Macros used by the included decompressor code below. */
28 #define STATIC static
29
30 /*
31 * Use normal definitions of mem*() from string.c. There are already
32 * included header files which expect a definition of memset() and by
33 * the time we define memset macro, it is too late.
34 */
35 #undef memcpy
36 #undef memset
37 #define memzero(s, n) memset((s), 0, (n))
38 #define memmove memmove
39
40 /* Functions used by the included decompressor code below. */
41 void *memmove(void *dest, const void *src, size_t n);
42
43 /*
44 * This is set up by the setup-routine at boot-time
45 */
46 struct boot_params *boot_params;
47
48 memptr free_mem_ptr;
49 memptr free_mem_end_ptr;
50
51 static char *vidmem;
52 static int vidport;
53 static 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 * NOTE: When adding a new decompressor, please update the analysis in
80 * ../header.S.
81 */
82
83 static 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 /* Transmit register (WRITE) */
95 #define LSR 5 /* Line Status */
96 static 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
106 void __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; /* Update cursor position */
149 outb(14, vidport);
150 outb(0xff & (pos >> 9), vidport+1);
151 outb(15, vidport);
152 outb(0xff & (pos >> 1), vidport+1);
153 }
154
155 void __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
172 static bool l5_supported(void)
173 {
174 /* Check if leaf 7 is supported. */
175 if (native_cpuid_eax(0) < 7)
176 return 0;
177
178 /* Check if la57 is supported. */
179 return native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31));
180 }
181
182 #if CONFIG_X86_NEED_RELOCS
183 static 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 * Calculate the delta between where vmlinux was linked to load
193 * and where it was actually loaded.
194 */
195 delta = min_addr - LOAD_PHYSICAL_ADDR;
196
197 /*
198 * The kernel contains a table of relocation addresses. Those
199 * addresses have the final load address of the kernel in virtual
200 * memory. We are currently working in the self map. So we need to
201 * create an adjustment for kernel memory addresses to the self map.
202 * This will involve subtracting out the base address of the kernel.
203 */
204 map = delta - __START_KERNEL_map;
205
206 /*
207 * 32-bit always performs relocations. 64-bit relocations are only
208 * needed if KASLR has chosen a different starting address offset
209 * from __START_KERNEL_map.
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 * Process relocations: 32 bit relocations first then 64 bit after.
222 * Three sets of binary relocations are added to the end of the kernel
223 * before compression. Each relocation table entry is the kernel
224 * address of the location which needs to be updated stored as a
225 * 32-bit value which is sign extended to 64 bits.
226 *
227 * Format is:
228 *
229 * kernel bits...
230 * 0 - zero terminator for 64 bit relocations
231 * 64 bit relocation repeated
232 * 0 - zero terminator for inverse 32 bit relocations
233 * 32 bit inverse relocation repeated
234 * 0 - zero terminator for 32 bit relocations
235 * 32 bit relocation repeated
236 *
237 * So we work backwards from the end of the decompressed image.
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
273 static inline void handle_relocations(void *output, unsigned long output_len,
274 unsigned long virt_addr)
275 { }
276 #endif
277
278 static 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_RELOCATABLE
313 dest = output;
314 dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
315 #else
316 dest = (void *)(phdr->p_paddr);
317 #endif
318 memmove(dest, output + phdr->p_offset, phdr->p_filesz);
319 break;
320 default: /* Ignore other PT_* */ break;
321 }
322 }
323
324 free(phdrs);
325 }
326
327 /*
328 * The compressed kernel image (ZO), has been moved so that its position
329 * is against the end of the buffer used to hold the uncompressed kernel
330 * image (VO) and the execution environment (.bss, .brk), which makes sure
331 * there is room to do the in-place decompression. (See header.S for the
332 * calculations.)
333 *
334 * |-----compressed kernel image------|
335 * V V
336 * 0 extract_offset +INIT_SIZE
337 * |-----------|---------------|-------------------------|--------|
338 * | | | |
339 * VO__text startup_32 of ZO VO__end ZO__end
340 * ^ ^
341 * |-------uncompressed kernel image---------|
342 *
343 */
344 asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
345 unsigned char *input_data,
346 unsigned long input_len,
347 unsigned char *output,
348 unsigned long output_len)
349 {
350 const unsigned long kernel_total_size = VO__end - VO__text;
351 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
352
353 /* Retain x86 boot parameters pointer passed from startup_32/64. */
354 boot_params = rmode;
355
356 /* Clear flags intended for solely in-kernel use. */
357 boot_params->hdr.loadflags &= ~KASLR_FLAG;
358
359 sanitize_boot_params(boot_params);
360
361 if (boot_params->screen_info.orig_video_mode == 7) {
362 vidmem = (char *) 0xb0000;
363 vidport = 0x3b4;
364 } else {
365 vidmem = (char *) 0xb8000;
366 vidport = 0x3d4;
367 }
368
369 lines = boot_params->screen_info.orig_video_lines;
370 cols = boot_params->screen_info.orig_video_cols;
371
372 console_init();
373 debug_putstr("early console in extract_kernel\n");
374
375 if (IS_ENABLED(CONFIG_X86_5LEVEL) && !l5_supported()) {
376 error("This linux kernel as configured requires 5-level paging\n"
377 "This CPU does not support the required 'cr4.la57' feature\n"
378 "Unable to boot - please use a kernel appropriate for your CPU\n");
379 }
380
381 free_mem_ptr = heap; /* Heap */
382 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
383
384 /* Report initial kernel position details. */
385 debug_putaddr(input_data);
386 debug_putaddr(input_len);
387 debug_putaddr(output);
388 debug_putaddr(output_len);
389 debug_putaddr(kernel_total_size);
390
391 /*
392 * The memory hole needed for the kernel is the larger of either
393 * the entire decompressed kernel plus relocation table, or the
394 * entire decompressed kernel plus .bss and .brk sections.
395 */
396 choose_random_location((unsigned long)input_data, input_len,
397 (unsigned long *)&output,
398 max(output_len, kernel_total_size),
399 &virt_addr);
400
401 /* Validate memory location choices. */
402 if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
403 error("Destination physical address inappropriately aligned");
404 if (virt_addr & (MIN_KERNEL_ALIGN - 1))
405 error("Destination virtual address inappropriately aligned");
406 #ifdef CONFIG_X86_64
407 if (heap > 0x3fffffffffffUL)
408 error("Destination address too large");
409 if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
410 error("Destination virtual address is beyond the kernel mapping area");
411 #else
412 if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
413 error("Destination address too large");
414 #endif
415 #ifndef CONFIG_RELOCATABLE
416 if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
417 error("Destination address does not match LOAD_PHYSICAL_ADDR");
418 if (virt_addr != LOAD_PHYSICAL_ADDR)
419 error("Destination virtual address changed when not relocatable");
420 #endif
421
422 debug_putstr("\nDecompressing Linux... ");
423 __decompress(input_data, input_len, NULL, NULL, output, output_len,
424 NULL, error);
425 parse_elf(output);
426 handle_relocations(output, output_len, virt_addr);
427 debug_putstr("done.\nBooting the kernel.\n");
428 return output;
429 }
430
431 void fortify_panic(const char *name)
432 {
433 error("detected buffer overflow");
434 }