misc.c 3.8 KB

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