misc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Definitions and wrapper functions for kernel decompressor
  3. *
  4. * Copyright IBM Corp. 2010
  5. *
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #include <asm/uaccess.h>
  9. #include <asm/page.h>
  10. #include <asm/sclp.h>
  11. #include <asm/ipl.h>
  12. #include "sizes.h"
  13. /*
  14. * gzip declarations
  15. */
  16. #define STATIC static
  17. #undef memset
  18. #undef memcpy
  19. #undef memmove
  20. #define memmove memmove
  21. #define memzero(s, n) memset((s), 0, (n))
  22. /* Symbols defined by linker scripts */
  23. extern char input_data[];
  24. extern int input_len;
  25. extern char _text, _end;
  26. extern char _bss, _ebss;
  27. static void error(char *m);
  28. static unsigned long free_mem_ptr;
  29. static unsigned long free_mem_end_ptr;
  30. #ifdef CONFIG_HAVE_KERNEL_BZIP2
  31. #define HEAP_SIZE 0x400000
  32. #else
  33. #define HEAP_SIZE 0x10000
  34. #endif
  35. #ifdef CONFIG_KERNEL_GZIP
  36. #include "../../../../lib/decompress_inflate.c"
  37. #endif
  38. #ifdef CONFIG_KERNEL_BZIP2
  39. #include "../../../../lib/decompress_bunzip2.c"
  40. #endif
  41. #ifdef CONFIG_KERNEL_LZ4
  42. #include "../../../../lib/decompress_unlz4.c"
  43. #endif
  44. #ifdef CONFIG_KERNEL_LZMA
  45. #include "../../../../lib/decompress_unlzma.c"
  46. #endif
  47. #ifdef CONFIG_KERNEL_LZO
  48. #include "../../../../lib/decompress_unlzo.c"
  49. #endif
  50. #ifdef CONFIG_KERNEL_XZ
  51. #include "../../../../lib/decompress_unxz.c"
  52. #endif
  53. static int puts(const char *s)
  54. {
  55. _sclp_print_early(s);
  56. return 0;
  57. }
  58. void *memset(void *s, int c, size_t n)
  59. {
  60. char *xs;
  61. xs = s;
  62. while (n--)
  63. *xs++ = c;
  64. return s;
  65. }
  66. void *memcpy(void *dest, const void *src, size_t n)
  67. {
  68. const char *s = src;
  69. char *d = dest;
  70. while (n--)
  71. *d++ = *s++;
  72. return dest;
  73. }
  74. void *memmove(void *dest, const void *src, size_t n)
  75. {
  76. const char *s = src;
  77. char *d = dest;
  78. if (d <= s) {
  79. while (n--)
  80. *d++ = *s++;
  81. } else {
  82. d += n;
  83. s += n;
  84. while (n--)
  85. *--d = *--s;
  86. }
  87. return dest;
  88. }
  89. static void error(char *x)
  90. {
  91. unsigned long long psw = 0x000a0000deadbeefULL;
  92. puts("\n\n");
  93. puts(x);
  94. puts("\n\n -- System halted");
  95. asm volatile("lpsw %0" : : "Q" (psw));
  96. }
  97. /*
  98. * Safe guard the ipl parameter block against a memory area that will be
  99. * overwritten. The validity check for the ipl parameter block is complex
  100. * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
  101. * the ipl parameter block intersects with the passed memory area we can
  102. * safely assume that we can read from that memory. In that case just copy
  103. * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
  104. * block.
  105. */
  106. static void check_ipl_parmblock(void *start, unsigned long size)
  107. {
  108. void *src, *dst;
  109. src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
  110. if (src + PAGE_SIZE <= start || src >= start + size)
  111. return;
  112. dst = (void *) IPL_PARMBLOCK_ORIGIN;
  113. memmove(dst, src, PAGE_SIZE);
  114. S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
  115. }
  116. unsigned long decompress_kernel(void)
  117. {
  118. void *output, *kernel_end;
  119. output = (void *) ALIGN((unsigned long) &_end + HEAP_SIZE, PAGE_SIZE);
  120. kernel_end = output + SZ__bss_start;
  121. check_ipl_parmblock((void *) 0, (unsigned long) kernel_end);
  122. #ifdef CONFIG_BLK_DEV_INITRD
  123. /*
  124. * Move the initrd right behind the end of the decompressed
  125. * kernel image. This also prevents initrd corruption caused by
  126. * bss clearing since kernel_end will always be located behind the
  127. * current bss section..
  128. */
  129. if (INITRD_START && INITRD_SIZE && kernel_end > (void *) INITRD_START) {
  130. check_ipl_parmblock(kernel_end, INITRD_SIZE);
  131. memmove(kernel_end, (void *) INITRD_START, INITRD_SIZE);
  132. INITRD_START = (unsigned long) kernel_end;
  133. }
  134. #endif
  135. /*
  136. * Clear bss section. free_mem_ptr and free_mem_end_ptr need to be
  137. * initialized afterwards since they reside in bss.
  138. */
  139. memset(&_bss, 0, &_ebss - &_bss);
  140. free_mem_ptr = (unsigned long) &_end;
  141. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  142. puts("Uncompressing Linux... ");
  143. __decompress(input_data, input_len, NULL, NULL, output, 0, NULL, error);
  144. puts("Ok, booting the kernel.\n");
  145. return (unsigned long) output;
  146. }