Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * misc.c | |
818a08f8 | 3 | * |
c0402881 KC |
4 | * This is a collection of several routines used to extract the kernel |
5 | * which includes KASLR relocation, decompression, ELF parsing, and | |
6 | * relocation processing. Additionally included are the screen and serial | |
7 | * output functions and related debugging support functions. | |
1da177e4 LT |
8 | * |
9 | * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 | |
10 | * puts by Nick Holloway 1993, better puts by Martin Mares 1995 | |
11 | * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 | |
12 | */ | |
13 | ||
8fee13a4 | 14 | #include "misc.h" |
820e8fec | 15 | #include "../string.h" |
968de4f0 | 16 | |
968de4f0 | 17 | /* |
4252db10 BH |
18 | * WARNING!! |
19 | * This code is compiled with -fPIC and it is relocated dynamically at | |
20 | * run time, but no relocation processing is performed. This means that | |
21 | * it is not safe to place pointers in static structures. | |
968de4f0 | 22 | */ |
1da177e4 | 23 | |
1f208de3 | 24 | /* Macros used by the included decompressor code below. */ |
1180e01d | 25 | #define STATIC static |
1da177e4 | 26 | |
04999550 | 27 | /* |
1f208de3 | 28 | * Use normal definitions of mem*() from string.c. There are already |
04999550 VG |
29 | * included header files which expect a definition of memset() and by |
30 | * the time we define memset macro, it is too late. | |
31 | */ | |
1f208de3 | 32 | #undef memcpy |
04999550 | 33 | #undef memset |
1180e01d | 34 | #define memzero(s, n) memset((s), 0, (n)) |
1da177e4 | 35 | |
1f208de3 | 36 | /* Functions used by the included decompressor code below. */ |
1da177e4 | 37 | static void error(char *m); |
fd77c7ca | 38 | |
1da177e4 LT |
39 | /* |
40 | * This is set up by the setup-routine at boot-time | |
41 | */ | |
6655e0aa | 42 | struct boot_params *boot_params; |
1da177e4 | 43 | |
82fa9637 KC |
44 | memptr free_mem_ptr; |
45 | memptr free_mem_end_ptr; | |
1da177e4 | 46 | |
03056c88 | 47 | static char *vidmem; |
1da177e4 LT |
48 | static int vidport; |
49 | static int lines, cols; | |
50 | ||
ae03c499 AK |
51 | #ifdef CONFIG_KERNEL_GZIP |
52 | #include "../../../../lib/decompress_inflate.c" | |
53 | #endif | |
54 | ||
55 | #ifdef CONFIG_KERNEL_BZIP2 | |
56 | #include "../../../../lib/decompress_bunzip2.c" | |
57 | #endif | |
58 | ||
59 | #ifdef CONFIG_KERNEL_LZMA | |
60 | #include "../../../../lib/decompress_unlzma.c" | |
61 | #endif | |
1da177e4 | 62 | |
30314804 LC |
63 | #ifdef CONFIG_KERNEL_XZ |
64 | #include "../../../../lib/decompress_unxz.c" | |
65 | #endif | |
66 | ||
13510997 AT |
67 | #ifdef CONFIG_KERNEL_LZO |
68 | #include "../../../../lib/decompress_unlzo.c" | |
69 | #endif | |
70 | ||
f9b493ac KL |
71 | #ifdef CONFIG_KERNEL_LZ4 |
72 | #include "../../../../lib/decompress_unlz4.c" | |
73 | #endif | |
4252db10 BH |
74 | /* |
75 | * NOTE: When adding a new decompressor, please update the analysis in | |
76 | * ../header.S. | |
77 | */ | |
f9b493ac | 78 | |
1da177e4 LT |
79 | static void scroll(void) |
80 | { | |
81 | int i; | |
82 | ||
fd77c7ca PC |
83 | memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2); |
84 | for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2) | |
1da177e4 LT |
85 | vidmem[i] = ' '; |
86 | } | |
87 | ||
8fee13a4 YL |
88 | #define XMTRDY 0x20 |
89 | ||
90 | #define TXR 0 /* Transmit register (WRITE) */ | |
91 | #define LSR 5 /* Line Status */ | |
92 | static void serial_putchar(int ch) | |
93 | { | |
94 | unsigned timeout = 0xffff; | |
95 | ||
96 | while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) | |
97 | cpu_relax(); | |
98 | ||
99 | outb(ch, early_serial_base + TXR); | |
100 | } | |
101 | ||
7aac3015 | 102 | void __putstr(const char *s) |
1da177e4 | 103 | { |
fd77c7ca | 104 | int x, y, pos; |
1da177e4 LT |
105 | char c; |
106 | ||
8fee13a4 YL |
107 | if (early_serial_base) { |
108 | const char *str = s; | |
109 | while (*str) { | |
110 | if (*str == '\n') | |
111 | serial_putchar('\r'); | |
112 | serial_putchar(*str++); | |
113 | } | |
114 | } | |
6bcb13b3 | 115 | |
6655e0aa | 116 | if (boot_params->screen_info.orig_video_mode == 0 && |
23968f71 | 117 | lines == 0 && cols == 0) |
a24e7851 RR |
118 | return; |
119 | ||
6655e0aa KC |
120 | x = boot_params->screen_info.orig_x; |
121 | y = boot_params->screen_info.orig_y; | |
1da177e4 | 122 | |
fd77c7ca PC |
123 | while ((c = *s++) != '\0') { |
124 | if (c == '\n') { | |
1da177e4 | 125 | x = 0; |
fd77c7ca | 126 | if (++y >= lines) { |
1da177e4 LT |
127 | scroll(); |
128 | y--; | |
129 | } | |
130 | } else { | |
020878ac | 131 | vidmem[(x + cols * y) * 2] = c; |
fd77c7ca | 132 | if (++x >= cols) { |
1da177e4 | 133 | x = 0; |
fd77c7ca | 134 | if (++y >= lines) { |
1da177e4 LT |
135 | scroll(); |
136 | y--; | |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
6655e0aa KC |
142 | boot_params->screen_info.orig_x = x; |
143 | boot_params->screen_info.orig_y = y; | |
1da177e4 LT |
144 | |
145 | pos = (x + cols * y) * 2; /* Update cursor position */ | |
b02aae9c RH |
146 | outb(14, vidport); |
147 | outb(0xff & (pos >> 9), vidport+1); | |
148 | outb(15, vidport); | |
149 | outb(0xff & (pos >> 1), vidport+1); | |
1da177e4 LT |
150 | } |
151 | ||
79063a7c KC |
152 | void __puthex(unsigned long value) |
153 | { | |
154 | char alpha[2] = "0"; | |
155 | int bits; | |
156 | ||
157 | for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) { | |
158 | unsigned long digit = (value >> bits) & 0xf; | |
159 | ||
160 | if (digit < 0xA) | |
161 | alpha[0] = '0' + digit; | |
162 | else | |
163 | alpha[0] = 'a' + (digit - 0xA); | |
164 | ||
165 | __putstr(alpha); | |
166 | } | |
167 | } | |
168 | ||
1da177e4 LT |
169 | static void error(char *x) |
170 | { | |
cb454fe1 JM |
171 | error_putstr("\n\n"); |
172 | error_putstr(x); | |
173 | error_putstr("\n\n -- System halted"); | |
1da177e4 | 174 | |
ff3cf856 IM |
175 | while (1) |
176 | asm("hlt"); | |
1da177e4 LT |
177 | } |
178 | ||
a0215061 KC |
179 | #if CONFIG_X86_NEED_RELOCS |
180 | static void handle_relocations(void *output, unsigned long output_len) | |
181 | { | |
182 | int *reloc; | |
183 | unsigned long delta, map, ptr; | |
184 | unsigned long min_addr = (unsigned long)output; | |
185 | unsigned long max_addr = min_addr + output_len; | |
186 | ||
187 | /* | |
188 | * Calculate the delta between where vmlinux was linked to load | |
189 | * and where it was actually loaded. | |
190 | */ | |
191 | delta = min_addr - LOAD_PHYSICAL_ADDR; | |
192 | if (!delta) { | |
193 | debug_putstr("No relocation needed... "); | |
194 | return; | |
195 | } | |
196 | debug_putstr("Performing relocations... "); | |
197 | ||
198 | /* | |
199 | * The kernel contains a table of relocation addresses. Those | |
200 | * addresses have the final load address of the kernel in virtual | |
201 | * memory. We are currently working in the self map. So we need to | |
202 | * create an adjustment for kernel memory addresses to the self map. | |
203 | * This will involve subtracting out the base address of the kernel. | |
204 | */ | |
205 | map = delta - __START_KERNEL_map; | |
206 | ||
207 | /* | |
208 | * Process relocations: 32 bit relocations first then 64 bit after. | |
6d24c5f7 | 209 | * Three sets of binary relocations are added to the end of the kernel |
a0215061 KC |
210 | * before compression. Each relocation table entry is the kernel |
211 | * address of the location which needs to be updated stored as a | |
212 | * 32-bit value which is sign extended to 64 bits. | |
213 | * | |
214 | * Format is: | |
215 | * | |
216 | * kernel bits... | |
217 | * 0 - zero terminator for 64 bit relocations | |
218 | * 64 bit relocation repeated | |
6d24c5f7 JB |
219 | * 0 - zero terminator for inverse 32 bit relocations |
220 | * 32 bit inverse relocation repeated | |
a0215061 KC |
221 | * 0 - zero terminator for 32 bit relocations |
222 | * 32 bit relocation repeated | |
223 | * | |
224 | * So we work backwards from the end of the decompressed image. | |
225 | */ | |
226 | for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) { | |
227 | int extended = *reloc; | |
228 | extended += map; | |
229 | ||
230 | ptr = (unsigned long)extended; | |
231 | if (ptr < min_addr || ptr > max_addr) | |
232 | error("32-bit relocation outside of kernel!\n"); | |
233 | ||
234 | *(uint32_t *)ptr += delta; | |
235 | } | |
236 | #ifdef CONFIG_X86_64 | |
6d24c5f7 JB |
237 | while (*--reloc) { |
238 | long extended = *reloc; | |
239 | extended += map; | |
240 | ||
241 | ptr = (unsigned long)extended; | |
242 | if (ptr < min_addr || ptr > max_addr) | |
243 | error("inverse 32-bit relocation outside of kernel!\n"); | |
244 | ||
245 | *(int32_t *)ptr -= delta; | |
246 | } | |
a0215061 KC |
247 | for (reloc--; *reloc; reloc--) { |
248 | long extended = *reloc; | |
249 | extended += map; | |
250 | ||
251 | ptr = (unsigned long)extended; | |
252 | if (ptr < min_addr || ptr > max_addr) | |
253 | error("64-bit relocation outside of kernel!\n"); | |
254 | ||
255 | *(uint64_t *)ptr += delta; | |
256 | } | |
257 | #endif | |
258 | } | |
259 | #else | |
260 | static inline void handle_relocations(void *output, unsigned long output_len) | |
261 | { } | |
262 | #endif | |
263 | ||
099e1377 IC |
264 | static void parse_elf(void *output) |
265 | { | |
266 | #ifdef CONFIG_X86_64 | |
267 | Elf64_Ehdr ehdr; | |
268 | Elf64_Phdr *phdrs, *phdr; | |
269 | #else | |
270 | Elf32_Ehdr ehdr; | |
271 | Elf32_Phdr *phdrs, *phdr; | |
272 | #endif | |
273 | void *dest; | |
274 | int i; | |
275 | ||
276 | memcpy(&ehdr, output, sizeof(ehdr)); | |
fd77c7ca | 277 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 || |
099e1377 IC |
278 | ehdr.e_ident[EI_MAG1] != ELFMAG1 || |
279 | ehdr.e_ident[EI_MAG2] != ELFMAG2 || | |
fd77c7ca | 280 | ehdr.e_ident[EI_MAG3] != ELFMAG3) { |
099e1377 IC |
281 | error("Kernel is not a valid ELF file"); |
282 | return; | |
283 | } | |
284 | ||
e605a425 | 285 | debug_putstr("Parsing ELF... "); |
099e1377 IC |
286 | |
287 | phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum); | |
288 | if (!phdrs) | |
289 | error("Failed to allocate space for phdrs"); | |
290 | ||
291 | memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum); | |
292 | ||
fd77c7ca | 293 | for (i = 0; i < ehdr.e_phnum; i++) { |
099e1377 IC |
294 | phdr = &phdrs[i]; |
295 | ||
296 | switch (phdr->p_type) { | |
297 | case PT_LOAD: | |
298 | #ifdef CONFIG_RELOCATABLE | |
299 | dest = output; | |
300 | dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR); | |
301 | #else | |
fd77c7ca | 302 | dest = (void *)(phdr->p_paddr); |
099e1377 IC |
303 | #endif |
304 | memcpy(dest, | |
305 | output + phdr->p_offset, | |
306 | phdr->p_filesz); | |
307 | break; | |
308 | default: /* Ignore other PT_* */ break; | |
309 | } | |
310 | } | |
5067cf53 JJ |
311 | |
312 | free(phdrs); | |
099e1377 IC |
313 | } |
314 | ||
c0402881 | 315 | asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, |
1180e01d IM |
316 | unsigned char *input_data, |
317 | unsigned long input_len, | |
a0215061 | 318 | unsigned char *output, |
e6023367 JM |
319 | unsigned long output_len, |
320 | unsigned long run_size) | |
1da177e4 | 321 | { |
f285f4a2 KC |
322 | unsigned char *output_orig = output; |
323 | ||
6655e0aa KC |
324 | /* Retain x86 boot parameters pointer passed from startup_32/64. */ |
325 | boot_params = rmode; | |
1da177e4 | 326 | |
6655e0aa KC |
327 | /* Clear flags intended for solely in-kernel use. */ |
328 | boot_params->hdr.loadflags &= ~KASLR_FLAG; | |
78cac48c | 329 | |
6655e0aa | 330 | sanitize_boot_params(boot_params); |
5dcd14ec | 331 | |
6655e0aa | 332 | if (boot_params->screen_info.orig_video_mode == 7) { |
1da177e4 LT |
333 | vidmem = (char *) 0xb0000; |
334 | vidport = 0x3b4; | |
335 | } else { | |
336 | vidmem = (char *) 0xb8000; | |
337 | vidport = 0x3d4; | |
338 | } | |
339 | ||
6655e0aa KC |
340 | lines = boot_params->screen_info.orig_video_lines; |
341 | cols = boot_params->screen_info.orig_video_cols; | |
1da177e4 | 342 | |
8fee13a4 | 343 | console_init(); |
c0402881 | 344 | debug_putstr("early console in extract_kernel\n"); |
8fee13a4 | 345 | |
4c83d653 | 346 | free_mem_ptr = heap; /* Heap */ |
7c539764 | 347 | free_mem_end_ptr = heap + BOOT_HEAP_SIZE; |
968de4f0 | 348 | |
79063a7c KC |
349 | /* Report initial kernel position details. */ |
350 | debug_putaddr(input_data); | |
351 | debug_putaddr(input_len); | |
352 | debug_putaddr(output); | |
353 | debug_putaddr(output_len); | |
354 | debug_putaddr(run_size); | |
355 | ||
e6023367 JM |
356 | /* |
357 | * The memory hole needed for the kernel is the larger of either | |
358 | * the entire decompressed kernel plus relocation table, or the | |
359 | * entire decompressed kernel plus .bss and .brk sections. | |
360 | */ | |
7de828df | 361 | output = choose_random_location(input_data, input_len, output, |
e6023367 JM |
362 | output_len > run_size ? output_len |
363 | : run_size); | |
8ab3820f KC |
364 | |
365 | /* Validate memory location choices. */ | |
7ed42a28 PA |
366 | if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1)) |
367 | error("Destination address inappropriately aligned"); | |
778cb929 | 368 | #ifdef CONFIG_X86_64 |
7ed42a28 | 369 | if (heap > 0x3fffffffffffUL) |
778cb929 IC |
370 | error("Destination address too large"); |
371 | #else | |
147dd561 | 372 | if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff)) |
968de4f0 | 373 | error("Destination address too large"); |
7ed42a28 | 374 | #endif |
968de4f0 | 375 | #ifndef CONFIG_RELOCATABLE |
7ed42a28 | 376 | if ((unsigned long)output != LOAD_PHYSICAL_ADDR) |
968de4f0 EB |
377 | error("Wrong destination address"); |
378 | #endif | |
1da177e4 | 379 | |
e605a425 | 380 | debug_putstr("\nDecompressing Linux... "); |
2d3862d2 YL |
381 | __decompress(input_data, input_len, NULL, NULL, output, output_len, |
382 | NULL, error); | |
099e1377 | 383 | parse_elf(output); |
f285f4a2 KC |
384 | /* |
385 | * 32-bit always performs relocations. 64-bit relocations are only | |
386 | * needed if kASLR has chosen a different load address. | |
387 | */ | |
388 | if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig) | |
389 | handle_relocations(output, output_len); | |
e605a425 | 390 | debug_putstr("done.\nBooting the kernel.\n"); |
8ab3820f | 391 | return output; |
1da177e4 | 392 | } |