misc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * Two 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 32 bit relocations
  238. * 32 bit relocation repeated
  239. *
  240. * So we work backwards from the end of the decompressed image.
  241. */
  242. for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
  243. int extended = *reloc;
  244. extended += map;
  245. ptr = (unsigned long)extended;
  246. if (ptr < min_addr || ptr > max_addr)
  247. error("32-bit relocation outside of kernel!\n");
  248. *(uint32_t *)ptr += delta;
  249. }
  250. #ifdef CONFIG_X86_64
  251. for (reloc--; *reloc; reloc--) {
  252. long extended = *reloc;
  253. extended += map;
  254. ptr = (unsigned long)extended;
  255. if (ptr < min_addr || ptr > max_addr)
  256. error("64-bit relocation outside of kernel!\n");
  257. *(uint64_t *)ptr += delta;
  258. }
  259. #endif
  260. }
  261. #else
  262. static inline void handle_relocations(void *output, unsigned long output_len)
  263. { }
  264. #endif
  265. static void parse_elf(void *output)
  266. {
  267. #ifdef CONFIG_X86_64
  268. Elf64_Ehdr ehdr;
  269. Elf64_Phdr *phdrs, *phdr;
  270. #else
  271. Elf32_Ehdr ehdr;
  272. Elf32_Phdr *phdrs, *phdr;
  273. #endif
  274. void *dest;
  275. int i;
  276. memcpy(&ehdr, output, sizeof(ehdr));
  277. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  278. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  279. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  280. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  281. error("Kernel is not a valid ELF file");
  282. return;
  283. }
  284. debug_putstr("Parsing ELF... ");
  285. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  286. if (!phdrs)
  287. error("Failed to allocate space for phdrs");
  288. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  289. for (i = 0; i < ehdr.e_phnum; i++) {
  290. phdr = &phdrs[i];
  291. switch (phdr->p_type) {
  292. case PT_LOAD:
  293. #ifdef CONFIG_RELOCATABLE
  294. dest = output;
  295. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  296. #else
  297. dest = (void *)(phdr->p_paddr);
  298. #endif
  299. memcpy(dest,
  300. output + phdr->p_offset,
  301. phdr->p_filesz);
  302. break;
  303. default: /* Ignore other PT_* */ break;
  304. }
  305. }
  306. free(phdrs);
  307. }
  308. asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
  309. unsigned char *input_data,
  310. unsigned long input_len,
  311. unsigned char *output,
  312. unsigned long output_len)
  313. {
  314. real_mode = rmode;
  315. sanitize_boot_params(real_mode);
  316. if (real_mode->screen_info.orig_video_mode == 7) {
  317. vidmem = (char *) 0xb0000;
  318. vidport = 0x3b4;
  319. } else {
  320. vidmem = (char *) 0xb8000;
  321. vidport = 0x3d4;
  322. }
  323. lines = real_mode->screen_info.orig_video_lines;
  324. cols = real_mode->screen_info.orig_video_cols;
  325. console_init();
  326. debug_putstr("early console in decompress_kernel\n");
  327. free_mem_ptr = heap; /* Heap */
  328. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  329. output = choose_kernel_location(input_data, input_len,
  330. output, output_len);
  331. /* Validate memory location choices. */
  332. if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
  333. error("Destination address inappropriately aligned");
  334. #ifdef CONFIG_X86_64
  335. if (heap > 0x3fffffffffffUL)
  336. error("Destination address too large");
  337. #else
  338. if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
  339. error("Destination address too large");
  340. #endif
  341. #ifndef CONFIG_RELOCATABLE
  342. if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
  343. error("Wrong destination address");
  344. #endif
  345. debug_putstr("\nDecompressing Linux... ");
  346. decompress(input_data, input_len, NULL, NULL, output, NULL, error);
  347. parse_elf(output);
  348. handle_relocations(output, output_len);
  349. debug_putstr("done.\nBooting the kernel.\n");
  350. return output;
  351. }