misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. void __puthex(unsigned long value)
  193. {
  194. char alpha[2] = "0";
  195. int bits;
  196. for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
  197. unsigned long digit = (value >> bits) & 0xf;
  198. if (digit < 0xA)
  199. alpha[0] = '0' + digit;
  200. else
  201. alpha[0] = 'a' + (digit - 0xA);
  202. __putstr(alpha);
  203. }
  204. }
  205. static void error(char *x)
  206. {
  207. error_putstr("\n\n");
  208. error_putstr(x);
  209. error_putstr("\n\n -- System halted");
  210. while (1)
  211. asm("hlt");
  212. }
  213. #if CONFIG_X86_NEED_RELOCS
  214. static void handle_relocations(void *output, unsigned long output_len)
  215. {
  216. int *reloc;
  217. unsigned long delta, map, ptr;
  218. unsigned long min_addr = (unsigned long)output;
  219. unsigned long max_addr = min_addr + output_len;
  220. /*
  221. * Calculate the delta between where vmlinux was linked to load
  222. * and where it was actually loaded.
  223. */
  224. delta = min_addr - LOAD_PHYSICAL_ADDR;
  225. if (!delta) {
  226. debug_putstr("No relocation needed... ");
  227. return;
  228. }
  229. debug_putstr("Performing relocations... ");
  230. /*
  231. * The kernel contains a table of relocation addresses. Those
  232. * addresses have the final load address of the kernel in virtual
  233. * memory. We are currently working in the self map. So we need to
  234. * create an adjustment for kernel memory addresses to the self map.
  235. * This will involve subtracting out the base address of the kernel.
  236. */
  237. map = delta - __START_KERNEL_map;
  238. /*
  239. * Process relocations: 32 bit relocations first then 64 bit after.
  240. * Three sets of binary relocations are added to the end of the kernel
  241. * before compression. Each relocation table entry is the kernel
  242. * address of the location which needs to be updated stored as a
  243. * 32-bit value which is sign extended to 64 bits.
  244. *
  245. * Format is:
  246. *
  247. * kernel bits...
  248. * 0 - zero terminator for 64 bit relocations
  249. * 64 bit relocation repeated
  250. * 0 - zero terminator for inverse 32 bit relocations
  251. * 32 bit inverse relocation repeated
  252. * 0 - zero terminator for 32 bit relocations
  253. * 32 bit relocation repeated
  254. *
  255. * So we work backwards from the end of the decompressed image.
  256. */
  257. for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
  258. int extended = *reloc;
  259. extended += map;
  260. ptr = (unsigned long)extended;
  261. if (ptr < min_addr || ptr > max_addr)
  262. error("32-bit relocation outside of kernel!\n");
  263. *(uint32_t *)ptr += delta;
  264. }
  265. #ifdef CONFIG_X86_64
  266. while (*--reloc) {
  267. long extended = *reloc;
  268. extended += map;
  269. ptr = (unsigned long)extended;
  270. if (ptr < min_addr || ptr > max_addr)
  271. error("inverse 32-bit relocation outside of kernel!\n");
  272. *(int32_t *)ptr -= delta;
  273. }
  274. for (reloc--; *reloc; reloc--) {
  275. long extended = *reloc;
  276. extended += map;
  277. ptr = (unsigned long)extended;
  278. if (ptr < min_addr || ptr > max_addr)
  279. error("64-bit relocation outside of kernel!\n");
  280. *(uint64_t *)ptr += delta;
  281. }
  282. #endif
  283. }
  284. #else
  285. static inline void handle_relocations(void *output, unsigned long output_len)
  286. { }
  287. #endif
  288. static void parse_elf(void *output)
  289. {
  290. #ifdef CONFIG_X86_64
  291. Elf64_Ehdr ehdr;
  292. Elf64_Phdr *phdrs, *phdr;
  293. #else
  294. Elf32_Ehdr ehdr;
  295. Elf32_Phdr *phdrs, *phdr;
  296. #endif
  297. void *dest;
  298. int i;
  299. memcpy(&ehdr, output, sizeof(ehdr));
  300. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  301. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  302. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  303. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  304. error("Kernel is not a valid ELF file");
  305. return;
  306. }
  307. debug_putstr("Parsing ELF... ");
  308. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  309. if (!phdrs)
  310. error("Failed to allocate space for phdrs");
  311. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  312. for (i = 0; i < ehdr.e_phnum; i++) {
  313. phdr = &phdrs[i];
  314. switch (phdr->p_type) {
  315. case PT_LOAD:
  316. #ifdef CONFIG_RELOCATABLE
  317. dest = output;
  318. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  319. #else
  320. dest = (void *)(phdr->p_paddr);
  321. #endif
  322. memcpy(dest,
  323. output + phdr->p_offset,
  324. phdr->p_filesz);
  325. break;
  326. default: /* Ignore other PT_* */ break;
  327. }
  328. }
  329. free(phdrs);
  330. }
  331. asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
  332. unsigned char *input_data,
  333. unsigned long input_len,
  334. unsigned char *output,
  335. unsigned long output_len,
  336. unsigned long run_size)
  337. {
  338. unsigned char *output_orig = output;
  339. real_mode = rmode;
  340. /* Clear it for solely in-kernel use */
  341. real_mode->hdr.loadflags &= ~KASLR_FLAG;
  342. sanitize_boot_params(real_mode);
  343. if (real_mode->screen_info.orig_video_mode == 7) {
  344. vidmem = (char *) 0xb0000;
  345. vidport = 0x3b4;
  346. } else {
  347. vidmem = (char *) 0xb8000;
  348. vidport = 0x3d4;
  349. }
  350. lines = real_mode->screen_info.orig_video_lines;
  351. cols = real_mode->screen_info.orig_video_cols;
  352. console_init();
  353. debug_putstr("early console in decompress_kernel\n");
  354. free_mem_ptr = heap; /* Heap */
  355. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  356. /* Report initial kernel position details. */
  357. debug_putaddr(input_data);
  358. debug_putaddr(input_len);
  359. debug_putaddr(output);
  360. debug_putaddr(output_len);
  361. debug_putaddr(run_size);
  362. /*
  363. * The memory hole needed for the kernel is the larger of either
  364. * the entire decompressed kernel plus relocation table, or the
  365. * entire decompressed kernel plus .bss and .brk sections.
  366. */
  367. output = choose_kernel_location(real_mode, input_data, input_len, output,
  368. output_len > run_size ? output_len
  369. : run_size);
  370. /* Validate memory location choices. */
  371. if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
  372. error("Destination address inappropriately aligned");
  373. #ifdef CONFIG_X86_64
  374. if (heap > 0x3fffffffffffUL)
  375. error("Destination address too large");
  376. #else
  377. if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
  378. error("Destination address too large");
  379. #endif
  380. #ifndef CONFIG_RELOCATABLE
  381. if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
  382. error("Wrong destination address");
  383. #endif
  384. debug_putstr("\nDecompressing Linux... ");
  385. __decompress(input_data, input_len, NULL, NULL, output, output_len,
  386. NULL, error);
  387. parse_elf(output);
  388. /*
  389. * 32-bit always performs relocations. 64-bit relocations are only
  390. * needed if kASLR has chosen a different load address.
  391. */
  392. if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig)
  393. handle_relocations(output, output_len);
  394. debug_putstr("done.\nBooting the kernel.\n");
  395. return output;
  396. }