arm32-stub.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/efi.h>
  10. #include <asm/efi.h>
  11. efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
  12. {
  13. int block;
  14. /* non-LPAE kernels can run anywhere */
  15. if (!IS_ENABLED(CONFIG_ARM_LPAE))
  16. return EFI_SUCCESS;
  17. /* LPAE kernels need compatible hardware */
  18. block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
  19. if (block < 5) {
  20. pr_efi_err(sys_table_arg, "This LPAE kernel is not supported by your CPU\n");
  21. return EFI_UNSUPPORTED;
  22. }
  23. return EFI_SUCCESS;
  24. }
  25. efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
  26. unsigned long *image_addr,
  27. unsigned long *image_size,
  28. unsigned long *reserve_addr,
  29. unsigned long *reserve_size,
  30. unsigned long dram_base,
  31. efi_loaded_image_t *image)
  32. {
  33. unsigned long nr_pages;
  34. efi_status_t status;
  35. /* Use alloc_addr to tranlsate between types */
  36. efi_physical_addr_t alloc_addr;
  37. /*
  38. * Verify that the DRAM base address is compatible with the ARM
  39. * boot protocol, which determines the base of DRAM by masking
  40. * off the low 27 bits of the address at which the zImage is
  41. * loaded. These assumptions are made by the decompressor,
  42. * before any memory map is available.
  43. */
  44. dram_base = round_up(dram_base, SZ_128M);
  45. /*
  46. * Reserve memory for the uncompressed kernel image. This is
  47. * all that prevents any future allocations from conflicting
  48. * with the kernel. Since we can't tell from the compressed
  49. * image how much DRAM the kernel actually uses (due to BSS
  50. * size uncertainty) we allocate the maximum possible size.
  51. * Do this very early, as prints can cause memory allocations
  52. * that may conflict with this.
  53. */
  54. alloc_addr = dram_base;
  55. *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
  56. nr_pages = round_up(*reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  57. status = sys_table->boottime->allocate_pages(EFI_ALLOCATE_ADDRESS,
  58. EFI_LOADER_DATA,
  59. nr_pages, &alloc_addr);
  60. if (status != EFI_SUCCESS) {
  61. *reserve_size = 0;
  62. pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");
  63. return status;
  64. }
  65. *reserve_addr = alloc_addr;
  66. /*
  67. * Relocate the zImage, so that it appears in the lowest 128 MB
  68. * memory window.
  69. */
  70. *image_size = image->image_size;
  71. status = efi_relocate_kernel(sys_table, image_addr, *image_size,
  72. *image_size,
  73. dram_base + MAX_UNCOMP_KERNEL_SIZE, 0);
  74. if (status != EFI_SUCCESS) {
  75. pr_efi_err(sys_table, "Failed to relocate kernel.\n");
  76. efi_free(sys_table, *reserve_size, *reserve_addr);
  77. *reserve_size = 0;
  78. return status;
  79. }
  80. /*
  81. * Check to see if we were able to allocate memory low enough
  82. * in memory. The kernel determines the base of DRAM from the
  83. * address at which the zImage is loaded.
  84. */
  85. if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {
  86. pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");
  87. efi_free(sys_table, *reserve_size, *reserve_addr);
  88. *reserve_size = 0;
  89. efi_free(sys_table, *image_size, *image_addr);
  90. *image_size = 0;
  91. return EFI_LOAD_ERROR;
  92. }
  93. return EFI_SUCCESS;
  94. }