decompress.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /*
  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. unsigned long __stack_chk_guard;
  64. void __stack_chk_guard_setup(void)
  65. {
  66. __stack_chk_guard = 0x000a0dff;
  67. }
  68. void __stack_chk_fail(void)
  69. {
  70. error("stack-protector: Kernel stack is corrupted\n");
  71. }
  72. void decompress_kernel(unsigned long boot_heap_start)
  73. {
  74. unsigned long zimage_start, zimage_size;
  75. __stack_chk_guard_setup();
  76. zimage_start = (unsigned long)(&__image_begin);
  77. zimage_size = (unsigned long)(&__image_end) -
  78. (unsigned long)(&__image_begin);
  79. puts("zimage at: ");
  80. puthex(zimage_start);
  81. puts(" ");
  82. puthex(zimage_size + zimage_start);
  83. puts("\n");
  84. /* This area are prepared for mallocing when decompressing */
  85. free_mem_ptr = boot_heap_start;
  86. free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
  87. /* Display standard Linux/MIPS boot prompt */
  88. puts("Uncompressing Linux at load address ");
  89. puthex(VMLINUX_LOAD_ADDRESS_ULL);
  90. puts("\n");
  91. /* Decompress the kernel with according algorithm */
  92. __decompress((char *)zimage_start, zimage_size, 0, 0,
  93. (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
  94. if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
  95. fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
  96. unsigned int image_size, dtb_size;
  97. dtb_size = fdt_totalsize((void *)&__appended_dtb);
  98. /* last four bytes is always image size in little endian */
  99. image_size = le32_to_cpup((void *)&__image_end - 4);
  100. /* copy dtb to where the booted kernel will expect it */
  101. memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
  102. __appended_dtb, dtb_size);
  103. }
  104. /* FIXME: should we flush cache here? */
  105. puts("Now, booting the kernel...\n");
  106. }