decompress.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2001 MontaVista Software Inc.
  4. * Author: Matt Porter <mporter@mvista.com>
  5. *
  6. * Copyright (C) 2009 Lemote, Inc.
  7. * Author: Wu Zhangjin <wuzhangjin@gmail.com>
  8. */
  9. #define DISABLE_BRANCH_PROFILING
  10. #define __NO_FORTIFY
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/libfdt.h>
  15. #include <asm/addrspace.h>
  16. #include <linux/unaligned.h>
  17. #include <asm-generic/vmlinux.lds.h>
  18. #include "decompress.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. void error(char *x)
  26. {
  27. puts("\n\n");
  28. puts(x);
  29. puts("\n\n -- System halted");
  30. while (1)
  31. ; /* Halt */
  32. }
  33. /* activate the code for pre-boot environment */
  34. #define STATIC static
  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. #ifdef CONFIG_KERNEL_ZSTD
  54. #include "../../../../lib/decompress_unzstd.c"
  55. #endif
  56. const unsigned long __stack_chk_guard = 0x000a0dff;
  57. void __stack_chk_fail(void)
  58. {
  59. error("stack-protector: Kernel stack is corrupted\n");
  60. }
  61. void decompress_kernel(unsigned long boot_heap_start)
  62. {
  63. unsigned long zimage_start, zimage_size;
  64. zimage_start = (unsigned long)(__image_begin);
  65. zimage_size = (unsigned long)(__image_end) -
  66. (unsigned long)(__image_begin);
  67. puts("zimage at: ");
  68. puthex(zimage_start);
  69. puts(" ");
  70. puthex(zimage_size + zimage_start);
  71. puts("\n");
  72. /* This area are prepared for mallocing when decompressing */
  73. free_mem_ptr = boot_heap_start;
  74. free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
  75. /* Display standard Linux/MIPS boot prompt */
  76. puts("Uncompressing Linux at load address ");
  77. puthex(VMLINUX_LOAD_ADDRESS_ULL);
  78. puts("\n");
  79. /* Decompress the kernel with according algorithm */
  80. __decompress((char *)zimage_start, zimage_size, 0, 0,
  81. (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
  82. if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
  83. fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
  84. unsigned int image_size, dtb_size;
  85. dtb_size = fdt_totalsize((void *)&__appended_dtb);
  86. /* last four bytes is always image size in little endian */
  87. image_size = get_unaligned_le32((void *)__image_end - 4);
  88. /* The device tree's address must be properly aligned */
  89. image_size = ALIGN(image_size, STRUCT_ALIGNMENT);
  90. puts("Copy device tree to address ");
  91. puthex(VMLINUX_LOAD_ADDRESS_ULL + image_size);
  92. puts("\n");
  93. /* copy dtb to where the booted kernel will expect it */
  94. memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
  95. __appended_dtb, dtb_size);
  96. }
  97. /* FIXME: should we flush cache here? */
  98. puts("Now, booting the kernel...\n");
  99. }