misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * misc.c
  3. *
  4. * This is a collection of several routines from gzip-1.0.3
  5. * adapted for Linux.
  6. *
  7. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  8. * puts by Nick Holloway 1993, better puts by Martin Mares 1995
  9. * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  10. */
  11. #include "misc.h"
  12. #include "../string.h"
  13. /* WARNING!!
  14. * This code is compiled with -fPIC and it is relocated dynamically
  15. * at run time, but no relocation processing is performed.
  16. * This means that it is not safe to place pointers in static structures.
  17. */
  18. /*
  19. * Getting to provable safe in place decompression is hard.
  20. * Worst case behaviours need to be analyzed.
  21. * Background information:
  22. *
  23. * The file layout is:
  24. * magic[2]
  25. * method[1]
  26. * flags[1]
  27. * timestamp[4]
  28. * extraflags[1]
  29. * os[1]
  30. * compressed data blocks[N]
  31. * crc[4] orig_len[4]
  32. *
  33. * resulting in 18 bytes of non compressed data overhead.
  34. *
  35. * Files divided into blocks
  36. * 1 bit (last block flag)
  37. * 2 bits (block type)
  38. *
  39. * 1 block occurs every 32K -1 bytes or when there 50% compression
  40. * has been achieved. The smallest block type encoding is always used.
  41. *
  42. * stored:
  43. * 32 bits length in bytes.
  44. *
  45. * fixed:
  46. * magic fixed tree.
  47. * symbols.
  48. *
  49. * dynamic:
  50. * dynamic tree encoding.
  51. * symbols.
  52. *
  53. *
  54. * The buffer for decompression in place is the length of the
  55. * uncompressed data, plus a small amount extra to keep the algorithm safe.
  56. * The compressed data is placed at the end of the buffer. The output
  57. * pointer is placed at the start of the buffer and the input pointer
  58. * is placed where the compressed data starts. Problems will occur
  59. * when the output pointer overruns the input pointer.
  60. *
  61. * The output pointer can only overrun the input pointer if the input
  62. * pointer is moving faster than the output pointer. A condition only
  63. * triggered by data whose compressed form is larger than the uncompressed
  64. * form.
  65. *
  66. * The worst case at the block level is a growth of the compressed data
  67. * of 5 bytes per 32767 bytes.
  68. *
  69. * The worst case internal to a compressed block is very hard to figure.
  70. * The worst case can at least be boundined by having one bit that represents
  71. * 32764 bytes and then all of the rest of the bytes representing the very
  72. * very last byte.
  73. *
  74. * All of which is enough to compute an amount of extra data that is required
  75. * to be safe. To avoid problems at the block level allocating 5 extra bytes
  76. * per 32767 bytes of data is sufficient. To avoind problems internal to a
  77. * block adding an extra 32767 bytes (the worst case uncompressed block size)
  78. * is sufficient, to ensure that in the worst case the decompressed data for
  79. * block will stop the byte before the compressed data for a block begins.
  80. * To avoid problems with the compressed data's meta information an extra 18
  81. * bytes are needed. Leading to the formula:
  82. *
  83. * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
  84. *
  85. * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
  86. * Adding 32768 instead of 32767 just makes for round numbers.
  87. * Adding the decompressor_size is necessary as it musht live after all
  88. * of the data as well. Last I measured the decompressor is about 14K.
  89. * 10K of actual data and 4K of bss.
  90. *
  91. */
  92. /*
  93. * gzip declarations
  94. */
  95. #define STATIC static
  96. #undef memcpy
  97. /*
  98. * Use a normal definition of memset() from string.c. There are already
  99. * included header files which expect a definition of memset() and by
  100. * the time we define memset macro, it is too late.
  101. */
  102. #undef memset
  103. #define memzero(s, n) memset((s), 0, (n))
  104. static void error(char *m);
  105. /*
  106. * This is set up by the setup-routine at boot-time
  107. */
  108. struct boot_params *real_mode; /* Pointer to real-mode data */
  109. memptr free_mem_ptr;
  110. memptr free_mem_end_ptr;
  111. static char *vidmem;
  112. static int vidport;
  113. static int lines, cols;
  114. #ifdef CONFIG_KERNEL_GZIP
  115. #include "../../../../lib/decompress_inflate.c"
  116. #endif
  117. #ifdef CONFIG_KERNEL_BZIP2
  118. #include "../../../../lib/decompress_bunzip2.c"
  119. #endif
  120. #ifdef CONFIG_KERNEL_LZMA
  121. #include "../../../../lib/decompress_unlzma.c"
  122. #endif
  123. #ifdef CONFIG_KERNEL_XZ
  124. #include "../../../../lib/decompress_unxz.c"
  125. #endif
  126. #ifdef CONFIG_KERNEL_LZO
  127. #include "../../../../lib/decompress_unlzo.c"
  128. #endif
  129. #ifdef CONFIG_KERNEL_LZ4
  130. #include "../../../../lib/decompress_unlz4.c"
  131. #endif
  132. static void scroll(void)
  133. {
  134. int i;
  135. memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  136. for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  137. vidmem[i] = ' ';
  138. }
  139. #define XMTRDY 0x20
  140. #define TXR 0 /* Transmit register (WRITE) */
  141. #define LSR 5 /* Line Status */
  142. static void serial_putchar(int ch)
  143. {
  144. unsigned timeout = 0xffff;
  145. while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
  146. cpu_relax();
  147. outb(ch, early_serial_base + TXR);
  148. }
  149. void __putstr(const char *s)
  150. {
  151. int x, y, pos;
  152. char c;
  153. if (early_serial_base) {
  154. const char *str = s;
  155. while (*str) {
  156. if (*str == '\n')
  157. serial_putchar('\r');
  158. serial_putchar(*str++);
  159. }
  160. }
  161. if (real_mode->screen_info.orig_video_mode == 0 &&
  162. lines == 0 && cols == 0)
  163. return;
  164. x = real_mode->screen_info.orig_x;
  165. y = real_mode->screen_info.orig_y;
  166. while ((c = *s++) != '\0') {
  167. if (c == '\n') {
  168. x = 0;
  169. if (++y >= lines) {
  170. scroll();
  171. y--;
  172. }
  173. } else {
  174. vidmem[(x + cols * y) * 2] = c;
  175. if (++x >= cols) {
  176. x = 0;
  177. if (++y >= lines) {
  178. scroll();
  179. y--;
  180. }
  181. }
  182. }
  183. }
  184. real_mode->screen_info.orig_x = x;
  185. real_mode->screen_info.orig_y = y;
  186. pos = (x + cols * y) * 2; /* Update cursor position */
  187. outb(14, vidport);
  188. outb(0xff & (pos >> 9), vidport+1);
  189. outb(15, vidport);
  190. outb(0xff & (pos >> 1), vidport+1);
  191. }
  192. static void error(char *x)
  193. {
  194. error_putstr("\n\n");
  195. error_putstr(x);
  196. error_putstr("\n\n -- System halted");
  197. while (1)
  198. asm("hlt");
  199. }
  200. #if CONFIG_X86_NEED_RELOCS
  201. static void handle_relocations(void *output, unsigned long output_len)
  202. {
  203. int *reloc;
  204. unsigned long delta, map, ptr;
  205. unsigned long min_addr = (unsigned long)output;
  206. unsigned long max_addr = min_addr + output_len;
  207. /*
  208. * Calculate the delta between where vmlinux was linked to load
  209. * and where it was actually loaded.
  210. */
  211. delta = min_addr - LOAD_PHYSICAL_ADDR;
  212. if (!delta) {
  213. debug_putstr("No relocation needed... ");
  214. return;
  215. }
  216. debug_putstr("Performing relocations... ");
  217. /*
  218. * The kernel contains a table of relocation addresses. Those
  219. * addresses have the final load address of the kernel in virtual
  220. * memory. We are currently working in the self map. So we need to
  221. * create an adjustment for kernel memory addresses to the self map.
  222. * This will involve subtracting out the base address of the kernel.
  223. */
  224. map = delta - __START_KERNEL_map;
  225. /*
  226. * Process relocations: 32 bit relocations first then 64 bit after.
  227. * Three sets of binary relocations are added to the end of the kernel
  228. * before compression. Each relocation table entry is the kernel
  229. * address of the location which needs to be updated stored as a
  230. * 32-bit value which is sign extended to 64 bits.
  231. *
  232. * Format is:
  233. *
  234. * kernel bits...
  235. * 0 - zero terminator for 64 bit relocations
  236. * 64 bit relocation repeated
  237. * 0 - zero terminator for inverse 32 bit relocations
  238. * 32 bit inverse relocation repeated
  239. * 0 - zero terminator for 32 bit relocations
  240. * 32 bit relocation repeated
  241. *
  242. * So we work backwards from the end of the decompressed image.
  243. */
  244. for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
  245. int extended = *reloc;
  246. extended += map;
  247. ptr = (unsigned long)extended;
  248. if (ptr < min_addr || ptr > max_addr)
  249. error("32-bit relocation outside of kernel!\n");
  250. *(uint32_t *)ptr += delta;
  251. }
  252. #ifdef CONFIG_X86_64
  253. while (*--reloc) {
  254. long extended = *reloc;
  255. extended += map;
  256. ptr = (unsigned long)extended;
  257. if (ptr < min_addr || ptr > max_addr)
  258. error("inverse 32-bit relocation outside of kernel!\n");
  259. *(int32_t *)ptr -= delta;
  260. }
  261. for (reloc--; *reloc; reloc--) {
  262. long extended = *reloc;
  263. extended += map;
  264. ptr = (unsigned long)extended;
  265. if (ptr < min_addr || ptr > max_addr)
  266. error("64-bit relocation outside of kernel!\n");
  267. *(uint64_t *)ptr += delta;
  268. }
  269. #endif
  270. }
  271. #else
  272. static inline void handle_relocations(void *output, unsigned long output_len)
  273. { }
  274. #endif
  275. static void parse_elf(void *output)
  276. {
  277. #ifdef CONFIG_X86_64
  278. Elf64_Ehdr ehdr;
  279. Elf64_Phdr *phdrs, *phdr;
  280. #else
  281. Elf32_Ehdr ehdr;
  282. Elf32_Phdr *phdrs, *phdr;
  283. #endif
  284. void *dest;
  285. int i;
  286. memcpy(&ehdr, output, sizeof(ehdr));
  287. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  288. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  289. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  290. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  291. error("Kernel is not a valid ELF file");
  292. return;
  293. }
  294. debug_putstr("Parsing ELF... ");
  295. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  296. if (!phdrs)
  297. error("Failed to allocate space for phdrs");
  298. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  299. for (i = 0; i < ehdr.e_phnum; i++) {
  300. phdr = &phdrs[i];
  301. switch (phdr->p_type) {
  302. case PT_LOAD:
  303. #ifdef CONFIG_RELOCATABLE
  304. dest = output;
  305. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  306. #else
  307. dest = (void *)(phdr->p_paddr);
  308. #endif
  309. memcpy(dest,
  310. output + phdr->p_offset,
  311. phdr->p_filesz);
  312. break;
  313. default: /* Ignore other PT_* */ break;
  314. }
  315. }
  316. free(phdrs);
  317. }
  318. asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
  319. unsigned char *input_data,
  320. unsigned long input_len,
  321. unsigned char *output,
  322. unsigned long output_len,
  323. unsigned long run_size)
  324. {
  325. unsigned char *output_orig = output;
  326. real_mode = rmode;
  327. /* Clear it for solely in-kernel use */
  328. real_mode->hdr.loadflags &= ~KASLR_FLAG;
  329. sanitize_boot_params(real_mode);
  330. if (real_mode->screen_info.orig_video_mode == 7) {
  331. vidmem = (char *) 0xb0000;
  332. vidport = 0x3b4;
  333. } else {
  334. vidmem = (char *) 0xb8000;
  335. vidport = 0x3d4;
  336. }
  337. lines = real_mode->screen_info.orig_video_lines;
  338. cols = real_mode->screen_info.orig_video_cols;
  339. console_init();
  340. debug_putstr("early console in decompress_kernel\n");
  341. free_mem_ptr = heap; /* Heap */
  342. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  343. /*
  344. * The memory hole needed for the kernel is the larger of either
  345. * the entire decompressed kernel plus relocation table, or the
  346. * entire decompressed kernel plus .bss and .brk sections.
  347. */
  348. output = choose_kernel_location(real_mode, input_data, input_len, output,
  349. output_len > run_size ? output_len
  350. : run_size);
  351. /* Validate memory location choices. */
  352. if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
  353. error("Destination address inappropriately aligned");
  354. #ifdef CONFIG_X86_64
  355. if (heap > 0x3fffffffffffUL)
  356. error("Destination address too large");
  357. #else
  358. if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
  359. error("Destination address too large");
  360. #endif
  361. #ifndef CONFIG_RELOCATABLE
  362. if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
  363. error("Wrong destination address");
  364. #endif
  365. debug_putstr("\nDecompressing Linux... ");
  366. decompress(input_data, input_len, NULL, NULL, output, NULL, error);
  367. parse_elf(output);
  368. /*
  369. * 32-bit always performs relocations. 64-bit relocations are only
  370. * needed if kASLR has chosen a different load address.
  371. */
  372. if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig)
  373. handle_relocations(output, output_len);
  374. debug_putstr("done.\nBooting the kernel.\n");
  375. return output;
  376. }