decompress.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2001 MontaVista Software Inc.
  3. * Author: Matt Porter <mporter@mvista.com>
  4. *
  5. * Copyright (C) 2009 Lemote, Inc.
  6. * Author: Wu Zhangjin <wuzhangjin@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/libfdt.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/unaligned.h>
  19. /*
  20. * These two variables specify the free mem region
  21. * that can be used for temporary malloc area
  22. */
  23. unsigned long free_mem_ptr;
  24. unsigned long free_mem_end_ptr;
  25. /* The linker tells us where the image is. */
  26. extern unsigned char __image_begin, __image_end;
  27. /* debug interfaces */
  28. #ifdef CONFIG_DEBUG_ZBOOT
  29. extern void puts(const char *s);
  30. extern void puthex(unsigned long long val);
  31. #else
  32. #define puts(s) do {} while (0)
  33. #define puthex(val) do {} while (0)
  34. #endif
  35. extern char __appended_dtb[];
  36. void error(char *x)
  37. {
  38. puts("\n\n");
  39. puts(x);
  40. puts("\n\n -- System halted");
  41. while (1)
  42. ; /* Halt */
  43. }
  44. /* activate the code for pre-boot environment */
  45. #define STATIC static
  46. #ifdef CONFIG_KERNEL_GZIP
  47. #include "../../../../lib/decompress_inflate.c"
  48. #endif
  49. #ifdef CONFIG_KERNEL_BZIP2
  50. #include "../../../../lib/decompress_bunzip2.c"
  51. #endif
  52. #ifdef CONFIG_KERNEL_LZ4
  53. #include "../../../../lib/decompress_unlz4.c"
  54. #endif
  55. #ifdef CONFIG_KERNEL_LZMA
  56. #include "../../../../lib/decompress_unlzma.c"
  57. #endif
  58. #ifdef CONFIG_KERNEL_LZO
  59. #include "../../../../lib/decompress_unlzo.c"
  60. #endif
  61. #ifdef CONFIG_KERNEL_XZ
  62. #include "../../../../lib/decompress_unxz.c"
  63. #endif
  64. const unsigned long __stack_chk_guard = 0x000a0dff;
  65. void __stack_chk_fail(void)
  66. {
  67. error("stack-protector: Kernel stack is corrupted\n");
  68. }
  69. void decompress_kernel(unsigned long boot_heap_start)
  70. {
  71. unsigned long zimage_start, zimage_size;
  72. zimage_start = (unsigned long)(&__image_begin);
  73. zimage_size = (unsigned long)(&__image_end) -
  74. (unsigned long)(&__image_begin);
  75. puts("zimage at: ");
  76. puthex(zimage_start);
  77. puts(" ");
  78. puthex(zimage_size + zimage_start);
  79. puts("\n");
  80. /* This area are prepared for mallocing when decompressing */
  81. free_mem_ptr = boot_heap_start;
  82. free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
  83. /* Display standard Linux/MIPS boot prompt */
  84. puts("Uncompressing Linux at load address ");
  85. puthex(VMLINUX_LOAD_ADDRESS_ULL);
  86. puts("\n");
  87. /* Decompress the kernel with according algorithm */
  88. __decompress((char *)zimage_start, zimage_size, 0, 0,
  89. (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
  90. if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
  91. fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
  92. unsigned int image_size, dtb_size;
  93. dtb_size = fdt_totalsize((void *)&__appended_dtb);
  94. /* last four bytes is always image size in little endian */
  95. image_size = get_unaligned_le32((void *)&__image_end - 4);
  96. /* copy dtb to where the booted kernel will expect it */
  97. memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
  98. __appended_dtb, dtb_size);
  99. }
  100. /* FIXME: should we flush cache here? */
  101. puts("Now, booting the kernel...\n");
  102. }