xip.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2010-2014 Wind River Systems, Inc.
  3. * Copyright (c) 2020 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <zephyr.h>
  8. #include <kernel.h>
  9. #include <string.h>
  10. #include <linker/linker-defs.h>
  11. #ifdef CONFIG_STACK_CANARIES
  12. extern volatile uintptr_t __stack_chk_guard;
  13. #endif /* CONFIG_STACK_CANARIES */
  14. /**
  15. *
  16. * @brief Copy the data section from ROM to RAM
  17. *
  18. * This routine copies the data section from ROM to RAM.
  19. *
  20. * @return N/A
  21. */
  22. void z_data_copy(void)
  23. {
  24. (void)memcpy(&__data_region_start, &__data_region_load_start,
  25. (uintptr_t)&__data_region_end - (uintptr_t)&__data_region_start);
  26. #ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT
  27. (void)memcpy(&__ramfunc_start, &__ramfunc_load_start,
  28. (uintptr_t) &__ramfunc_size);
  29. #endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */
  30. #ifdef CONFIG_SLEEP_FUNC_IN_SRAM
  31. (void)memcpy(&_sram_data_start, &_sram_data_rom_start,
  32. (uintptr_t) &_sram_data_ram_size);
  33. #endif /* CONFIG_SLEEP_FUNC_IN_SRAM */
  34. #ifdef CONFIG_SENSOR_ALGO
  35. if (__sensor_func_size > 0) {
  36. (void)memcpy(&__sensor_func_start, &__sensor_func_load_start,
  37. (uint32_t) &__sensor_func_size);
  38. }
  39. if (__sensor_data_size > 0) {
  40. (void)memcpy(&__sensor_data_start, &__sensor_data_load_start,
  41. (uint32_t) &__sensor_data_size);
  42. }
  43. #endif
  44. #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay)
  45. (void)memcpy(&__ccm_data_start, &__ccm_data_rom_start,
  46. __ccm_data_end - __ccm_data_start);
  47. #endif
  48. #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
  49. (void)memcpy(&__itcm_start, &__itcm_load_start,
  50. (uintptr_t) &__itcm_size);
  51. #endif
  52. #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
  53. (void)memcpy(&__dtcm_data_start, &__dtcm_data_load_start,
  54. __dtcm_data_end - __dtcm_data_start);
  55. #endif
  56. #ifdef CONFIG_CODE_DATA_RELOCATION
  57. extern void data_copy_xip_relocation(void);
  58. data_copy_xip_relocation();
  59. #endif /* CONFIG_CODE_DATA_RELOCATION */
  60. #ifdef CONFIG_USERSPACE
  61. #ifdef CONFIG_STACK_CANARIES
  62. /* stack canary checking is active for all C functions.
  63. * __stack_chk_guard is some uninitialized value living in the
  64. * app shared memory sections. Preserve it, and don't make any
  65. * function calls to perform the memory copy. The true canary
  66. * value gets set later in z_cstart().
  67. */
  68. uintptr_t guard_copy = __stack_chk_guard;
  69. uint8_t *src = (uint8_t *)&_app_smem_rom_start;
  70. uint8_t *dst = (uint8_t *)&_app_smem_start;
  71. uint32_t count = _app_smem_end - _app_smem_start;
  72. guard_copy = __stack_chk_guard;
  73. while (count > 0) {
  74. *(dst++) = *(src++);
  75. count--;
  76. }
  77. __stack_chk_guard = guard_copy;
  78. #else
  79. (void)memcpy(&_app_smem_start, &_app_smem_rom_start,
  80. _app_smem_end - _app_smem_start);
  81. #endif /* CONFIG_STACK_CANARIES */
  82. #endif /* CONFIG_USERSPACE */
  83. }