misc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /*
  12. * we have to be careful, because no indirections are allowed here, and
  13. * paravirt_ops is a kind of one. As it will only run in baremetal anyway,
  14. * we just keep it from happening
  15. */
  16. #undef CONFIG_PARAVIRT
  17. #ifdef CONFIG_X86_32
  18. #define _ASM_X86_DESC_H 1
  19. #endif
  20. #include <linux/linkage.h>
  21. #include <linux/screen_info.h>
  22. #include <linux/elf.h>
  23. #include <linux/io.h>
  24. #include <asm/page.h>
  25. #include <asm/boot.h>
  26. #include <asm/bootparam.h>
  27. /* WARNING!!
  28. * This code is compiled with -fPIC and it is relocated dynamically
  29. * at run time, but no relocation processing is performed.
  30. * This means that it is not safe to place pointers in static structures.
  31. */
  32. /*
  33. * Getting to provable safe in place decompression is hard.
  34. * Worst case behaviours need to be analyzed.
  35. * Background information:
  36. *
  37. * The file layout is:
  38. * magic[2]
  39. * method[1]
  40. * flags[1]
  41. * timestamp[4]
  42. * extraflags[1]
  43. * os[1]
  44. * compressed data blocks[N]
  45. * crc[4] orig_len[4]
  46. *
  47. * resulting in 18 bytes of non compressed data overhead.
  48. *
  49. * Files divided into blocks
  50. * 1 bit (last block flag)
  51. * 2 bits (block type)
  52. *
  53. * 1 block occurs every 32K -1 bytes or when there 50% compression
  54. * has been achieved. The smallest block type encoding is always used.
  55. *
  56. * stored:
  57. * 32 bits length in bytes.
  58. *
  59. * fixed:
  60. * magic fixed tree.
  61. * symbols.
  62. *
  63. * dynamic:
  64. * dynamic tree encoding.
  65. * symbols.
  66. *
  67. *
  68. * The buffer for decompression in place is the length of the
  69. * uncompressed data, plus a small amount extra to keep the algorithm safe.
  70. * The compressed data is placed at the end of the buffer. The output
  71. * pointer is placed at the start of the buffer and the input pointer
  72. * is placed where the compressed data starts. Problems will occur
  73. * when the output pointer overruns the input pointer.
  74. *
  75. * The output pointer can only overrun the input pointer if the input
  76. * pointer is moving faster than the output pointer. A condition only
  77. * triggered by data whose compressed form is larger than the uncompressed
  78. * form.
  79. *
  80. * The worst case at the block level is a growth of the compressed data
  81. * of 5 bytes per 32767 bytes.
  82. *
  83. * The worst case internal to a compressed block is very hard to figure.
  84. * The worst case can at least be boundined by having one bit that represents
  85. * 32764 bytes and then all of the rest of the bytes representing the very
  86. * very last byte.
  87. *
  88. * All of which is enough to compute an amount of extra data that is required
  89. * to be safe. To avoid problems at the block level allocating 5 extra bytes
  90. * per 32767 bytes of data is sufficient. To avoind problems internal to a
  91. * block adding an extra 32767 bytes (the worst case uncompressed block size)
  92. * is sufficient, to ensure that in the worst case the decompressed data for
  93. * block will stop the byte before the compressed data for a block begins.
  94. * To avoid problems with the compressed data's meta information an extra 18
  95. * bytes are needed. Leading to the formula:
  96. *
  97. * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
  98. *
  99. * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
  100. * Adding 32768 instead of 32767 just makes for round numbers.
  101. * Adding the decompressor_size is necessary as it musht live after all
  102. * of the data as well. Last I measured the decompressor is about 14K.
  103. * 10K of actual data and 4K of bss.
  104. *
  105. */
  106. /*
  107. * gzip declarations
  108. */
  109. #define STATIC static
  110. #undef memset
  111. #undef memcpy
  112. #define memzero(s, n) memset((s), 0, (n))
  113. static void error(char *m);
  114. /*
  115. * This is set up by the setup-routine at boot-time
  116. */
  117. static struct boot_params *real_mode; /* Pointer to real-mode data */
  118. static int quiet;
  119. void *memset(void *s, int c, size_t n);
  120. void *memcpy(void *dest, const void *src, size_t n);
  121. static void __putstr(int, const char *);
  122. #define putstr(__x) __putstr(0, __x)
  123. #ifdef CONFIG_X86_64
  124. #define memptr long
  125. #else
  126. #define memptr unsigned
  127. #endif
  128. static memptr free_mem_ptr;
  129. static memptr free_mem_end_ptr;
  130. static char *vidmem;
  131. static int vidport;
  132. static int lines, cols;
  133. #ifdef CONFIG_KERNEL_GZIP
  134. #include "../../../../lib/decompress_inflate.c"
  135. #endif
  136. #ifdef CONFIG_KERNEL_BZIP2
  137. #include "../../../../lib/decompress_bunzip2.c"
  138. #endif
  139. #ifdef CONFIG_KERNEL_LZMA
  140. #include "../../../../lib/decompress_unlzma.c"
  141. #endif
  142. #ifdef CONFIG_KERNEL_LZO
  143. #include "../../../../lib/decompress_unlzo.c"
  144. #endif
  145. static void scroll(void)
  146. {
  147. int i;
  148. memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  149. for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  150. vidmem[i] = ' ';
  151. }
  152. static void __putstr(int error, const char *s)
  153. {
  154. int x, y, pos;
  155. char c;
  156. #ifndef CONFIG_X86_VERBOSE_BOOTUP
  157. if (!error)
  158. return;
  159. #endif
  160. if (real_mode->screen_info.orig_video_mode == 0 &&
  161. lines == 0 && cols == 0)
  162. return;
  163. x = real_mode->screen_info.orig_x;
  164. y = real_mode->screen_info.orig_y;
  165. while ((c = *s++) != '\0') {
  166. if (c == '\n') {
  167. x = 0;
  168. if (++y >= lines) {
  169. scroll();
  170. y--;
  171. }
  172. } else {
  173. vidmem[(x + cols * y) * 2] = c;
  174. if (++x >= cols) {
  175. x = 0;
  176. if (++y >= lines) {
  177. scroll();
  178. y--;
  179. }
  180. }
  181. }
  182. }
  183. real_mode->screen_info.orig_x = x;
  184. real_mode->screen_info.orig_y = y;
  185. pos = (x + cols * y) * 2; /* Update cursor position */
  186. outb(14, vidport);
  187. outb(0xff & (pos >> 9), vidport+1);
  188. outb(15, vidport);
  189. outb(0xff & (pos >> 1), vidport+1);
  190. }
  191. void *memset(void *s, int c, size_t n)
  192. {
  193. int i;
  194. char *ss = s;
  195. for (i = 0; i < n; i++)
  196. ss[i] = c;
  197. return s;
  198. }
  199. void *memcpy(void *dest, const void *src, size_t n)
  200. {
  201. int i;
  202. const char *s = src;
  203. char *d = dest;
  204. for (i = 0; i < n; i++)
  205. d[i] = s[i];
  206. return dest;
  207. }
  208. static void error(char *x)
  209. {
  210. __putstr(1, "\n\n");
  211. __putstr(1, x);
  212. __putstr(1, "\n\n -- System halted");
  213. while (1)
  214. asm("hlt");
  215. }
  216. static void parse_elf(void *output)
  217. {
  218. #ifdef CONFIG_X86_64
  219. Elf64_Ehdr ehdr;
  220. Elf64_Phdr *phdrs, *phdr;
  221. #else
  222. Elf32_Ehdr ehdr;
  223. Elf32_Phdr *phdrs, *phdr;
  224. #endif
  225. void *dest;
  226. int i;
  227. memcpy(&ehdr, output, sizeof(ehdr));
  228. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  229. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  230. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  231. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  232. error("Kernel is not a valid ELF file");
  233. return;
  234. }
  235. if (!quiet)
  236. putstr("Parsing ELF... ");
  237. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  238. if (!phdrs)
  239. error("Failed to allocate space for phdrs");
  240. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  241. for (i = 0; i < ehdr.e_phnum; i++) {
  242. phdr = &phdrs[i];
  243. switch (phdr->p_type) {
  244. case PT_LOAD:
  245. #ifdef CONFIG_RELOCATABLE
  246. dest = output;
  247. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  248. #else
  249. dest = (void *)(phdr->p_paddr);
  250. #endif
  251. memcpy(dest,
  252. output + phdr->p_offset,
  253. phdr->p_filesz);
  254. break;
  255. default: /* Ignore other PT_* */ break;
  256. }
  257. }
  258. }
  259. asmlinkage void decompress_kernel(void *rmode, memptr heap,
  260. unsigned char *input_data,
  261. unsigned long input_len,
  262. unsigned char *output)
  263. {
  264. real_mode = rmode;
  265. if (real_mode->hdr.loadflags & QUIET_FLAG)
  266. quiet = 1;
  267. if (real_mode->screen_info.orig_video_mode == 7) {
  268. vidmem = (char *) 0xb0000;
  269. vidport = 0x3b4;
  270. } else {
  271. vidmem = (char *) 0xb8000;
  272. vidport = 0x3d4;
  273. }
  274. lines = real_mode->screen_info.orig_video_lines;
  275. cols = real_mode->screen_info.orig_video_cols;
  276. free_mem_ptr = heap; /* Heap */
  277. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  278. if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
  279. error("Destination address inappropriately aligned");
  280. #ifdef CONFIG_X86_64
  281. if (heap > 0x3fffffffffffUL)
  282. error("Destination address too large");
  283. #else
  284. if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
  285. error("Destination address too large");
  286. #endif
  287. #ifndef CONFIG_RELOCATABLE
  288. if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
  289. error("Wrong destination address");
  290. #endif
  291. if (!quiet)
  292. putstr("\nDecompressing Linux... ");
  293. decompress(input_data, input_len, NULL, NULL, output, NULL, error);
  294. parse_elf(output);
  295. if (!quiet)
  296. putstr("done.\nBooting the kernel.\n");
  297. return;
  298. }