misc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_LZMA
  41. #include "../../../../lib/decompress_unlzma.c"
  42. #endif
  43. #ifdef CONFIG_KERNEL_LZO
  44. #include "../../../../lib/decompress_unlzo.c"
  45. #endif
  46. #ifdef CONFIG_KERNEL_XZ
  47. #include "../../../../lib/decompress_unxz.c"
  48. #endif
  49. extern _sclp_print_early(const char *);
  50. static int puts(const char *s)
  51. {
  52. _sclp_print_early(s);
  53. return 0;
  54. }
  55. void *memset(void *s, int c, size_t n)
  56. {
  57. char *xs;
  58. xs = s;
  59. while (n--)
  60. *xs++ = c;
  61. return s;
  62. }
  63. void *memcpy(void *dest, const void *src, size_t n)
  64. {
  65. const char *s = src;
  66. char *d = dest;
  67. while (n--)
  68. *d++ = *s++;
  69. return dest;
  70. }
  71. void *memmove(void *dest, const void *src, size_t n)
  72. {
  73. const char *s = src;
  74. char *d = dest;
  75. if (d <= s) {
  76. while (n--)
  77. *d++ = *s++;
  78. } else {
  79. d += n;
  80. s += n;
  81. while (n--)
  82. *--d = *--s;
  83. }
  84. return dest;
  85. }
  86. static void error(char *x)
  87. {
  88. unsigned long long psw = 0x000a0000deadbeefULL;
  89. puts("\n\n");
  90. puts(x);
  91. puts("\n\n -- System halted");
  92. asm volatile("lpsw %0" : : "Q" (psw));
  93. }
  94. /*
  95. * Safe guard the ipl parameter block against a memory area that will be
  96. * overwritten. The validity check for the ipl parameter block is complex
  97. * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
  98. * the ipl parameter block intersects with the passed memory area we can
  99. * safely assume that we can read from that memory. In that case just copy
  100. * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
  101. * block.
  102. */
  103. static void check_ipl_parmblock(void *start, unsigned long size)
  104. {
  105. void *src, *dst;
  106. src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
  107. if (src + PAGE_SIZE <= start || src >= start + size)
  108. return;
  109. dst = (void *) IPL_PARMBLOCK_ORIGIN;
  110. memmove(dst, src, PAGE_SIZE);
  111. S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
  112. }
  113. unsigned long decompress_kernel(void)
  114. {
  115. unsigned long output_addr;
  116. unsigned char *output;
  117. output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL;
  118. check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start);
  119. memset(&_bss, 0, &_ebss - &_bss);
  120. free_mem_ptr = (unsigned long)&_end;
  121. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  122. output = (unsigned char *) output_addr;
  123. #ifdef CONFIG_BLK_DEV_INITRD
  124. /*
  125. * Move the initrd right behind the end of the decompressed
  126. * kernel image.
  127. */
  128. if (INITRD_START && INITRD_SIZE &&
  129. INITRD_START < (unsigned long) output + SZ__bss_start) {
  130. check_ipl_parmblock(output + SZ__bss_start,
  131. INITRD_START + INITRD_SIZE);
  132. memmove(output + SZ__bss_start,
  133. (void *) INITRD_START, INITRD_SIZE);
  134. INITRD_START = (unsigned long) output + SZ__bss_start;
  135. }
  136. #endif
  137. puts("Uncompressing Linux... ");
  138. decompress(input_data, input_len, NULL, NULL, output, NULL, error);
  139. puts("Ok, booting the kernel.\n");
  140. return (unsigned long) output;
  141. }