misc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/ipl.h>
  11. #include "sizes.h"
  12. /*
  13. * gzip declarations
  14. */
  15. #define STATIC static
  16. #undef memset
  17. #undef memcpy
  18. #undef memmove
  19. #define memmove memmove
  20. #define memzero(s, n) memset((s), 0, (n))
  21. /* Symbols defined by linker scripts */
  22. extern char input_data[];
  23. extern int input_len;
  24. extern char _text, _end;
  25. extern char _bss, _ebss;
  26. static void error(char *m);
  27. static unsigned long free_mem_ptr;
  28. static unsigned long free_mem_end_ptr;
  29. #ifdef CONFIG_HAVE_KERNEL_BZIP2
  30. #define HEAP_SIZE 0x400000
  31. #else
  32. #define HEAP_SIZE 0x10000
  33. #endif
  34. #ifdef CONFIG_KERNEL_GZIP
  35. #include "../../../../lib/decompress_inflate.c"
  36. #endif
  37. #ifdef CONFIG_KERNEL_BZIP2
  38. #include "../../../../lib/decompress_bunzip2.c"
  39. #endif
  40. #ifdef CONFIG_KERNEL_LZ4
  41. #include "../../../../lib/decompress_unlz4.c"
  42. #endif
  43. #ifdef CONFIG_KERNEL_LZMA
  44. #include "../../../../lib/decompress_unlzma.c"
  45. #endif
  46. #ifdef CONFIG_KERNEL_LZO
  47. #include "../../../../lib/decompress_unlzo.c"
  48. #endif
  49. #ifdef CONFIG_KERNEL_XZ
  50. #include "../../../../lib/decompress_unxz.c"
  51. #endif
  52. extern _sclp_print_early(const char *);
  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. unsigned long output_addr;
  119. unsigned char *output;
  120. output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL;
  121. check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start);
  122. memset(&_bss, 0, &_ebss - &_bss);
  123. free_mem_ptr = (unsigned long)&_end;
  124. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  125. output = (unsigned char *) output_addr;
  126. #ifdef CONFIG_BLK_DEV_INITRD
  127. /*
  128. * Move the initrd right behind the end of the decompressed
  129. * kernel image.
  130. */
  131. if (INITRD_START && INITRD_SIZE &&
  132. INITRD_START < (unsigned long) output + SZ__bss_start) {
  133. check_ipl_parmblock(output + SZ__bss_start,
  134. INITRD_START + INITRD_SIZE);
  135. memmove(output + SZ__bss_start,
  136. (void *) INITRD_START, INITRD_SIZE);
  137. INITRD_START = (unsigned long) output + SZ__bss_start;
  138. }
  139. #endif
  140. puts("Uncompressing Linux... ");
  141. decompress(input_data, input_len, NULL, NULL, output, NULL, error);
  142. puts("Ok, booting the kernel.\n");
  143. return (unsigned long) output;
  144. }