misc.c 11 KB

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