arm32-stub.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 handle_kernel_image(efi_system_table_t *sys_table,
  12. unsigned long *image_addr,
  13. unsigned long *image_size,
  14. unsigned long *reserve_addr,
  15. unsigned long *reserve_size,
  16. unsigned long dram_base,
  17. efi_loaded_image_t *image)
  18. {
  19. unsigned long nr_pages;
  20. efi_status_t status;
  21. /* Use alloc_addr to tranlsate between types */
  22. efi_physical_addr_t alloc_addr;
  23. /*
  24. * Verify that the DRAM base address is compatible with the ARM
  25. * boot protocol, which determines the base of DRAM by masking
  26. * off the low 27 bits of the address at which the zImage is
  27. * loaded. These assumptions are made by the decompressor,
  28. * before any memory map is available.
  29. */
  30. dram_base = round_up(dram_base, SZ_128M);
  31. /*
  32. * Reserve memory for the uncompressed kernel image. This is
  33. * all that prevents any future allocations from conflicting
  34. * with the kernel. Since we can't tell from the compressed
  35. * image how much DRAM the kernel actually uses (due to BSS
  36. * size uncertainty) we allocate the maximum possible size.
  37. * Do this very early, as prints can cause memory allocations
  38. * that may conflict with this.
  39. */
  40. alloc_addr = dram_base;
  41. *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
  42. nr_pages = round_up(*reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  43. status = sys_table->boottime->allocate_pages(EFI_ALLOCATE_ADDRESS,
  44. EFI_LOADER_DATA,
  45. nr_pages, &alloc_addr);
  46. if (status != EFI_SUCCESS) {
  47. *reserve_size = 0;
  48. pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");
  49. return status;
  50. }
  51. *reserve_addr = alloc_addr;
  52. /*
  53. * Relocate the zImage, so that it appears in the lowest 128 MB
  54. * memory window.
  55. */
  56. *image_size = image->image_size;
  57. status = efi_relocate_kernel(sys_table, image_addr, *image_size,
  58. *image_size,
  59. dram_base + MAX_UNCOMP_KERNEL_SIZE, 0);
  60. if (status != EFI_SUCCESS) {
  61. pr_efi_err(sys_table, "Failed to relocate kernel.\n");
  62. efi_free(sys_table, *reserve_size, *reserve_addr);
  63. *reserve_size = 0;
  64. return status;
  65. }
  66. /*
  67. * Check to see if we were able to allocate memory low enough
  68. * in memory. The kernel determines the base of DRAM from the
  69. * address at which the zImage is loaded.
  70. */
  71. if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {
  72. pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");
  73. efi_free(sys_table, *reserve_size, *reserve_addr);
  74. *reserve_size = 0;
  75. efi_free(sys_table, *image_size, *image_addr);
  76. *image_size = 0;
  77. return EFI_LOAD_ERROR;
  78. }
  79. return EFI_SUCCESS;
  80. }