refcode.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Read a coreboot rmodule and execute it.
  4. * The rmodule_header struct is from coreboot.
  5. *
  6. * Copyright (c) 2016 Google, Inc
  7. */
  8. #include <common.h>
  9. #include <errno.h>
  10. #include <asm/arch/pei_data.h>
  11. #define RMODULE_MAGIC 0xf8fe
  12. #define RMODULE_VERSION_1 1
  13. /*
  14. * All fields with '_offset' in the name are byte offsets into the flat blob.
  15. * The linker and the linker script takes are of assigning the values.
  16. */
  17. struct rmodule_header {
  18. uint16_t magic;
  19. uint8_t version;
  20. uint8_t type;
  21. /* The payload represents the program's loadable code and data */
  22. uint32_t payload_begin_offset;
  23. uint32_t payload_end_offset;
  24. /* Begin and of relocation information about the program module */
  25. uint32_t relocations_begin_offset;
  26. uint32_t relocations_end_offset;
  27. /*
  28. * The starting address of the linked program. This address is vital
  29. * for determining relocation offsets as the relocation info and other
  30. * symbols (bss, entry point) need this value as a basis to calculate
  31. * the offsets.
  32. */
  33. uint32_t module_link_start_address;
  34. /*
  35. * The module_program_size is the size of memory used while running
  36. * the program. The program is assumed to consume a contiguous amount
  37. * of memory
  38. */
  39. uint32_t module_program_size;
  40. /* This is program's execution entry point */
  41. uint32_t module_entry_point;
  42. /*
  43. * Optional parameter structure that can be used to pass data into
  44. * the module
  45. */
  46. uint32_t parameters_begin;
  47. uint32_t parameters_end;
  48. /* BSS section information so the loader can clear the bss */
  49. uint32_t bss_begin;
  50. uint32_t bss_end;
  51. /* Add some room for growth */
  52. uint32_t padding[4];
  53. } __packed;
  54. /**
  55. * cpu_run_reference_code() - Run the platform reference code
  56. *
  57. * Some platforms require a binary blob to be executed once SDRAM is
  58. * available. This is used to set up various platform features, such as the
  59. * platform controller hub (PCH). This function should be implemented by the
  60. * CPU-specific code.
  61. *
  62. * @return 0 on success, -ve on failure
  63. */
  64. static int cpu_run_reference_code(void)
  65. {
  66. struct pei_data _pei_data __aligned(8);
  67. struct pei_data *pei_data = &_pei_data;
  68. asmlinkage int (*func)(void *);
  69. struct rmodule_header *hdr;
  70. char *src, *dest;
  71. int ret, dummy;
  72. int size;
  73. hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
  74. debug("Extracting code from rmodule at %p\n", hdr);
  75. if (hdr->magic != RMODULE_MAGIC) {
  76. debug("Invalid rmodule magic\n");
  77. return -EINVAL;
  78. }
  79. if (hdr->module_link_start_address != 0) {
  80. debug("Link start address must be 0\n");
  81. return -EPERM;
  82. }
  83. if (hdr->module_entry_point != 0) {
  84. debug("Entry point must be 0\n");
  85. return -EPERM;
  86. }
  87. memset(pei_data, '\0', sizeof(struct pei_data));
  88. broadwell_fill_pei_data(pei_data);
  89. mainboard_fill_pei_data(pei_data);
  90. pei_data->saved_data = (void *)&dummy;
  91. src = (char *)hdr + hdr->payload_begin_offset;
  92. dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
  93. size = hdr->payload_end_offset - hdr->payload_begin_offset;
  94. debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
  95. memcpy(dest, src, size);
  96. size = hdr->bss_end - hdr->bss_begin;
  97. debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
  98. memset(dest + hdr->bss_begin, '\0', size);
  99. func = (asmlinkage int (*)(void *))dest;
  100. debug("Running reference code at %p\n", func);
  101. #ifdef DEBUG
  102. print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
  103. #endif
  104. ret = func(pei_data);
  105. if (ret != 0) {
  106. debug("Reference code returned %d\n", ret);
  107. return -EL2HLT;
  108. }
  109. debug("Refereence code completed\n");
  110. return 0;
  111. }
  112. int arch_early_init_r(void)
  113. {
  114. return cpu_run_reference_code();
  115. }