misc.c 11 KB

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