arm32-stub.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
  26. struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg)
  27. {
  28. struct screen_info *si;
  29. efi_status_t status;
  30. /*
  31. * Unlike on arm64, where we can directly fill out the screen_info
  32. * structure from the stub, we need to allocate a buffer to hold
  33. * its contents while we hand over to the kernel proper from the
  34. * decompressor.
  35. */
  36. status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
  37. sizeof(*si), (void **)&si);
  38. if (status != EFI_SUCCESS)
  39. return NULL;
  40. status = efi_call_early(install_configuration_table,
  41. &screen_info_guid, si);
  42. if (status == EFI_SUCCESS)
  43. return si;
  44. efi_call_early(free_pool, si);
  45. return NULL;
  46. }
  47. void free_screen_info(efi_system_table_t *sys_table_arg, struct screen_info *si)
  48. {
  49. if (!si)
  50. return;
  51. efi_call_early(install_configuration_table, &screen_info_guid, NULL);
  52. efi_call_early(free_pool, si);
  53. }
  54. efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
  55. unsigned long *image_addr,
  56. unsigned long *image_size,
  57. unsigned long *reserve_addr,
  58. unsigned long *reserve_size,
  59. unsigned long dram_base,
  60. efi_loaded_image_t *image)
  61. {
  62. unsigned long nr_pages;
  63. efi_status_t status;
  64. /* Use alloc_addr to tranlsate between types */
  65. efi_physical_addr_t alloc_addr;
  66. /*
  67. * Verify that the DRAM base address is compatible with the ARM
  68. * boot protocol, which determines the base of DRAM by masking
  69. * off the low 27 bits of the address at which the zImage is
  70. * loaded. These assumptions are made by the decompressor,
  71. * before any memory map is available.
  72. */
  73. dram_base = round_up(dram_base, SZ_128M);
  74. /*
  75. * Reserve memory for the uncompressed kernel image. This is
  76. * all that prevents any future allocations from conflicting
  77. * with the kernel. Since we can't tell from the compressed
  78. * image how much DRAM the kernel actually uses (due to BSS
  79. * size uncertainty) we allocate the maximum possible size.
  80. * Do this very early, as prints can cause memory allocations
  81. * that may conflict with this.
  82. */
  83. alloc_addr = dram_base;
  84. *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
  85. nr_pages = round_up(*reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  86. status = sys_table->boottime->allocate_pages(EFI_ALLOCATE_ADDRESS,
  87. EFI_LOADER_DATA,
  88. nr_pages, &alloc_addr);
  89. if (status != EFI_SUCCESS) {
  90. *reserve_size = 0;
  91. pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");
  92. return status;
  93. }
  94. *reserve_addr = alloc_addr;
  95. /*
  96. * Relocate the zImage, so that it appears in the lowest 128 MB
  97. * memory window.
  98. */
  99. *image_size = image->image_size;
  100. status = efi_relocate_kernel(sys_table, image_addr, *image_size,
  101. *image_size,
  102. dram_base + MAX_UNCOMP_KERNEL_SIZE, 0);
  103. if (status != EFI_SUCCESS) {
  104. pr_efi_err(sys_table, "Failed to relocate kernel.\n");
  105. efi_free(sys_table, *reserve_size, *reserve_addr);
  106. *reserve_size = 0;
  107. return status;
  108. }
  109. /*
  110. * Check to see if we were able to allocate memory low enough
  111. * in memory. The kernel determines the base of DRAM from the
  112. * address at which the zImage is loaded.
  113. */
  114. if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {
  115. pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");
  116. efi_free(sys_table, *reserve_size, *reserve_addr);
  117. *reserve_size = 0;
  118. efi_free(sys_table, *image_size, *image_addr);
  119. *image_size = 0;
  120. return EFI_LOAD_ERROR;
  121. }
  122. return EFI_SUCCESS;
  123. }