decompress.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2001 MontaVista Software Inc.
  4. * Author: Matt Porter <[email protected]>
  5. *
  6. * Copyright (C) 2009 Lemote, Inc.
  7. * Author: Wu Zhangjin <[email protected]>
  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 <asm/unaligned.h>
  17. #include <asm-generic/vmlinux.lds.h>
  18. /*
  19. * These two variables specify the free mem region
  20. * that can be used for temporary malloc area
  21. */
  22. unsigned long free_mem_ptr;
  23. unsigned long free_mem_end_ptr;
  24. /* The linker tells us where the image is. */
  25. extern unsigned char __image_begin[], __image_end[];
  26. /* debug interfaces */
  27. #ifdef CONFIG_DEBUG_ZBOOT
  28. extern void puts(const char *s);
  29. extern void puthex(unsigned long long val);
  30. #else
  31. #define puts(s) do {} while (0)
  32. #define puthex(val) do {} while (0)
  33. #endif
  34. extern char __appended_dtb[];
  35. void error(char *x)
  36. {
  37. puts("\n\n");
  38. puts(x);
  39. puts("\n\n -- System halted");
  40. while (1)
  41. ; /* Halt */
  42. }
  43. /* activate the code for pre-boot environment */
  44. #define STATIC static
  45. #ifdef CONFIG_KERNEL_GZIP
  46. #include "../../../../lib/decompress_inflate.c"
  47. #endif
  48. #ifdef CONFIG_KERNEL_BZIP2
  49. #include "../../../../lib/decompress_bunzip2.c"
  50. #endif
  51. #ifdef CONFIG_KERNEL_LZ4
  52. #include "../../../../lib/decompress_unlz4.c"
  53. #endif
  54. #ifdef CONFIG_KERNEL_LZMA
  55. #include "../../../../lib/decompress_unlzma.c"
  56. #endif
  57. #ifdef CONFIG_KERNEL_LZO
  58. #include "../../../../lib/decompress_unlzo.c"
  59. #endif
  60. #ifdef CONFIG_KERNEL_XZ
  61. #include "../../../../lib/decompress_unxz.c"
  62. #endif
  63. #ifdef CONFIG_KERNEL_ZSTD
  64. #include "../../../../lib/decompress_unzstd.c"
  65. #endif
  66. const unsigned long __stack_chk_guard = 0x000a0dff;
  67. void __stack_chk_fail(void)
  68. {
  69. error("stack-protector: Kernel stack is corrupted\n");
  70. }
  71. void decompress_kernel(unsigned long boot_heap_start)
  72. {
  73. unsigned long zimage_start, zimage_size;
  74. zimage_start = (unsigned long)(__image_begin);
  75. zimage_size = (unsigned long)(__image_end) -
  76. (unsigned long)(__image_begin);
  77. puts("zimage at: ");
  78. puthex(zimage_start);
  79. puts(" ");
  80. puthex(zimage_size + zimage_start);
  81. puts("\n");
  82. /* This area are prepared for mallocing when decompressing */
  83. free_mem_ptr = boot_heap_start;
  84. free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
  85. /* Display standard Linux/MIPS boot prompt */
  86. puts("Uncompressing Linux at load address ");
  87. puthex(VMLINUX_LOAD_ADDRESS_ULL);
  88. puts("\n");
  89. /* Decompress the kernel with according algorithm */
  90. __decompress((char *)zimage_start, zimage_size, 0, 0,
  91. (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
  92. if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
  93. fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
  94. unsigned int image_size, dtb_size;
  95. dtb_size = fdt_totalsize((void *)&__appended_dtb);
  96. /* last four bytes is always image size in little endian */
  97. image_size = get_unaligned_le32((void *)__image_end - 4);
  98. /* The device tree's address must be properly aligned */
  99. image_size = ALIGN(image_size, STRUCT_ALIGNMENT);
  100. puts("Copy device tree to address ");
  101. puthex(VMLINUX_LOAD_ADDRESS_ULL + image_size);
  102. puts("\n");
  103. /* copy dtb to where the booted kernel will expect it */
  104. memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
  105. __appended_dtb, dtb_size);
  106. }
  107. /* FIXME: should we flush cache here? */
  108. puts("Now, booting the kernel...\n");
  109. }