misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #include "misc.h"
  15. #include "error.h"
  16. #include "pgtable.h"
  17. #include "../string.h"
  18. #include "../voffset.h"
  19. #include <asm/bootparam_utils.h>
  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. /* Macros used by the included decompressor code below. */
  27. #define STATIC static
  28. /*
  29. * Use normal definitions of mem*() from string.c. There are already
  30. * included header files which expect a definition of memset() and by
  31. * the time we define memset macro, it is too late.
  32. */
  33. #undef memcpy
  34. #undef memset
  35. #define memzero(s, n) memset((s), 0, (n))
  36. #define memmove memmove
  37. /* Functions used by the included decompressor code below. */
  38. void *memmove(void *dest, const void *src, size_t n);
  39. /*
  40. * This is set up by the setup-routine at boot-time
  41. */
  42. struct boot_params *boot_params;
  43. memptr free_mem_ptr;
  44. memptr free_mem_end_ptr;
  45. static char *vidmem;
  46. static int vidport;
  47. static int lines, cols;
  48. #ifdef CONFIG_KERNEL_GZIP
  49. #include "../../../../lib/decompress_inflate.c"
  50. #endif
  51. #ifdef CONFIG_KERNEL_BZIP2
  52. #include "../../../../lib/decompress_bunzip2.c"
  53. #endif
  54. #ifdef CONFIG_KERNEL_LZMA
  55. #include "../../../../lib/decompress_unlzma.c"
  56. #endif
  57. #ifdef CONFIG_KERNEL_XZ
  58. #include "../../../../lib/decompress_unxz.c"
  59. #endif
  60. #ifdef CONFIG_KERNEL_LZO
  61. #include "../../../../lib/decompress_unlzo.c"
  62. #endif
  63. #ifdef CONFIG_KERNEL_LZ4
  64. #include "../../../../lib/decompress_unlz4.c"
  65. #endif
  66. /*
  67. * NOTE: When adding a new decompressor, please update the analysis in
  68. * ../header.S.
  69. */
  70. static void scroll(void)
  71. {
  72. int i;
  73. memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  74. for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  75. vidmem[i] = ' ';
  76. }
  77. #define XMTRDY 0x20
  78. #define TXR 0 /* Transmit register (WRITE) */
  79. #define LSR 5 /* Line Status */
  80. static void serial_putchar(int ch)
  81. {
  82. unsigned timeout = 0xffff;
  83. while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
  84. cpu_relax();
  85. outb(ch, early_serial_base + TXR);
  86. }
  87. void __putstr(const char *s)
  88. {
  89. int x, y, pos;
  90. char c;
  91. if (early_serial_base) {
  92. const char *str = s;
  93. while (*str) {
  94. if (*str == '\n')
  95. serial_putchar('\r');
  96. serial_putchar(*str++);
  97. }
  98. }
  99. if (lines == 0 || cols == 0)
  100. return;
  101. x = boot_params->screen_info.orig_x;
  102. y = boot_params->screen_info.orig_y;
  103. while ((c = *s++) != '\0') {
  104. if (c == '\n') {
  105. x = 0;
  106. if (++y >= lines) {
  107. scroll();
  108. y--;
  109. }
  110. } else {
  111. vidmem[(x + cols * y) * 2] = c;
  112. if (++x >= cols) {
  113. x = 0;
  114. if (++y >= lines) {
  115. scroll();
  116. y--;
  117. }
  118. }
  119. }
  120. }
  121. boot_params->screen_info.orig_x = x;
  122. boot_params->screen_info.orig_y = y;
  123. pos = (x + cols * y) * 2; /* Update cursor position */
  124. outb(14, vidport);
  125. outb(0xff & (pos >> 9), vidport+1);
  126. outb(15, vidport);
  127. outb(0xff & (pos >> 1), vidport+1);
  128. }
  129. void __puthex(unsigned long value)
  130. {
  131. char alpha[2] = "0";
  132. int bits;
  133. for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
  134. unsigned long digit = (value >> bits) & 0xf;
  135. if (digit < 0xA)
  136. alpha[0] = '0' + digit;
  137. else
  138. alpha[0] = 'a' + (digit - 0xA);
  139. __putstr(alpha);
  140. }
  141. }
  142. #if CONFIG_X86_NEED_RELOCS
  143. static void handle_relocations(void *output, unsigned long output_len,
  144. unsigned long virt_addr)
  145. {
  146. int *reloc;
  147. unsigned long delta, map, ptr;
  148. unsigned long min_addr = (unsigned long)output;
  149. unsigned long max_addr = min_addr + (VO___bss_start - VO__text);
  150. /*
  151. * Calculate the delta between where vmlinux was linked to load
  152. * and where it was actually loaded.
  153. */
  154. delta = min_addr - LOAD_PHYSICAL_ADDR;
  155. /*
  156. * The kernel contains a table of relocation addresses. Those
  157. * addresses have the final load address of the kernel in virtual
  158. * memory. We are currently working in the self map. So we need to
  159. * create an adjustment for kernel memory addresses to the self map.
  160. * This will involve subtracting out the base address of the kernel.
  161. */
  162. map = delta - __START_KERNEL_map;
  163. /*
  164. * 32-bit always performs relocations. 64-bit relocations are only
  165. * needed if KASLR has chosen a different starting address offset
  166. * from __START_KERNEL_map.
  167. */
  168. if (IS_ENABLED(CONFIG_X86_64))
  169. delta = virt_addr - LOAD_PHYSICAL_ADDR;
  170. if (!delta) {
  171. debug_putstr("No relocation needed... ");
  172. return;
  173. }
  174. debug_putstr("Performing relocations... ");
  175. /*
  176. * Process relocations: 32 bit relocations first then 64 bit after.
  177. * Three sets of binary relocations are added to the end of the kernel
  178. * before compression. Each relocation table entry is the kernel
  179. * address of the location which needs to be updated stored as a
  180. * 32-bit value which is sign extended to 64 bits.
  181. *
  182. * Format is:
  183. *
  184. * kernel bits...
  185. * 0 - zero terminator for 64 bit relocations
  186. * 64 bit relocation repeated
  187. * 0 - zero terminator for inverse 32 bit relocations
  188. * 32 bit inverse relocation repeated
  189. * 0 - zero terminator for 32 bit relocations
  190. * 32 bit relocation repeated
  191. *
  192. * So we work backwards from the end of the decompressed image.
  193. */
  194. for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
  195. long extended = *reloc;
  196. extended += map;
  197. ptr = (unsigned long)extended;
  198. if (ptr < min_addr || ptr > max_addr)
  199. error("32-bit relocation outside of kernel!\n");
  200. *(uint32_t *)ptr += delta;
  201. }
  202. #ifdef CONFIG_X86_64
  203. while (*--reloc) {
  204. long extended = *reloc;
  205. extended += map;
  206. ptr = (unsigned long)extended;
  207. if (ptr < min_addr || ptr > max_addr)
  208. error("inverse 32-bit relocation outside of kernel!\n");
  209. *(int32_t *)ptr -= delta;
  210. }
  211. for (reloc--; *reloc; reloc--) {
  212. long extended = *reloc;
  213. extended += map;
  214. ptr = (unsigned long)extended;
  215. if (ptr < min_addr || ptr > max_addr)
  216. error("64-bit relocation outside of kernel!\n");
  217. *(uint64_t *)ptr += delta;
  218. }
  219. #endif
  220. }
  221. #else
  222. static inline void handle_relocations(void *output, unsigned long output_len,
  223. unsigned long virt_addr)
  224. { }
  225. #endif
  226. static void parse_elf(void *output)
  227. {
  228. #ifdef CONFIG_X86_64
  229. Elf64_Ehdr ehdr;
  230. Elf64_Phdr *phdrs, *phdr;
  231. #else
  232. Elf32_Ehdr ehdr;
  233. Elf32_Phdr *phdrs, *phdr;
  234. #endif
  235. void *dest;
  236. int i;
  237. memcpy(&ehdr, output, sizeof(ehdr));
  238. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  239. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  240. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  241. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  242. error("Kernel is not a valid ELF file");
  243. return;
  244. }
  245. debug_putstr("Parsing ELF... ");
  246. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  247. if (!phdrs)
  248. error("Failed to allocate space for phdrs");
  249. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  250. for (i = 0; i < ehdr.e_phnum; i++) {
  251. phdr = &phdrs[i];
  252. switch (phdr->p_type) {
  253. case PT_LOAD:
  254. #ifdef CONFIG_X86_64
  255. if ((phdr->p_align % 0x200000) != 0)
  256. error("Alignment of LOAD segment isn't multiple of 2MB");
  257. #endif
  258. #ifdef CONFIG_RELOCATABLE
  259. dest = output;
  260. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  261. #else
  262. dest = (void *)(phdr->p_paddr);
  263. #endif
  264. memmove(dest, output + phdr->p_offset, phdr->p_filesz);
  265. break;
  266. default: /* Ignore other PT_* */ break;
  267. }
  268. }
  269. free(phdrs);
  270. }
  271. /*
  272. * The compressed kernel image (ZO), has been moved so that its position
  273. * is against the end of the buffer used to hold the uncompressed kernel
  274. * image (VO) and the execution environment (.bss, .brk), which makes sure
  275. * there is room to do the in-place decompression. (See header.S for the
  276. * calculations.)
  277. *
  278. * |-----compressed kernel image------|
  279. * V V
  280. * 0 extract_offset +INIT_SIZE
  281. * |-----------|---------------|-------------------------|--------|
  282. * | | | |
  283. * VO__text startup_32 of ZO VO__end ZO__end
  284. * ^ ^
  285. * |-------uncompressed kernel image---------|
  286. *
  287. */
  288. asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
  289. unsigned char *input_data,
  290. unsigned long input_len,
  291. unsigned char *output,
  292. unsigned long output_len)
  293. {
  294. const unsigned long kernel_total_size = VO__end - VO__text;
  295. unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
  296. /* Retain x86 boot parameters pointer passed from startup_32/64. */
  297. boot_params = rmode;
  298. /* Clear flags intended for solely in-kernel use. */
  299. boot_params->hdr.loadflags &= ~KASLR_FLAG;
  300. sanitize_boot_params(boot_params);
  301. if (boot_params->screen_info.orig_video_mode == 7) {
  302. vidmem = (char *) 0xb0000;
  303. vidport = 0x3b4;
  304. } else {
  305. vidmem = (char *) 0xb8000;
  306. vidport = 0x3d4;
  307. }
  308. lines = boot_params->screen_info.orig_video_lines;
  309. cols = boot_params->screen_info.orig_video_cols;
  310. console_init();
  311. debug_putstr("early console in extract_kernel\n");
  312. free_mem_ptr = heap; /* Heap */
  313. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  314. /* Report initial kernel position details. */
  315. debug_putaddr(input_data);
  316. debug_putaddr(input_len);
  317. debug_putaddr(output);
  318. debug_putaddr(output_len);
  319. debug_putaddr(kernel_total_size);
  320. #ifdef CONFIG_X86_64
  321. /* Report address of 32-bit trampoline */
  322. debug_putaddr(trampoline_32bit);
  323. #endif
  324. /*
  325. * The memory hole needed for the kernel is the larger of either
  326. * the entire decompressed kernel plus relocation table, or the
  327. * entire decompressed kernel plus .bss and .brk sections.
  328. */
  329. choose_random_location((unsigned long)input_data, input_len,
  330. (unsigned long *)&output,
  331. max(output_len, kernel_total_size),
  332. &virt_addr);
  333. /* Validate memory location choices. */
  334. if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
  335. error("Destination physical address inappropriately aligned");
  336. if (virt_addr & (MIN_KERNEL_ALIGN - 1))
  337. error("Destination virtual address inappropriately aligned");
  338. #ifdef CONFIG_X86_64
  339. if (heap > 0x3fffffffffffUL)
  340. error("Destination address too large");
  341. if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
  342. error("Destination virtual address is beyond the kernel mapping area");
  343. #else
  344. if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
  345. error("Destination address too large");
  346. #endif
  347. #ifndef CONFIG_RELOCATABLE
  348. if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
  349. error("Destination address does not match LOAD_PHYSICAL_ADDR");
  350. if (virt_addr != LOAD_PHYSICAL_ADDR)
  351. error("Destination virtual address changed when not relocatable");
  352. #endif
  353. debug_putstr("\nDecompressing Linux... ");
  354. __decompress(input_data, input_len, NULL, NULL, output, output_len,
  355. NULL, error);
  356. parse_elf(output);
  357. handle_relocations(output, output_len, virt_addr);
  358. debug_putstr("done.\nBooting the kernel.\n");
  359. return output;
  360. }
  361. void fortify_panic(const char *name)
  362. {
  363. error("detected buffer overflow");
  364. }