misc.c 8.3 KB

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