section_overlay.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief link memory address overlay helper
  9. */
  10. #include <kernel.h>
  11. #include <string.h>
  12. #include <section_overlay.h>
  13. #include <logging/log.h>
  14. LOG_MODULE_REGISTER(overlay, LOG_LEVEL_DBG);
  15. struct overlay_info {
  16. u32_t idcode;
  17. u32_t code_vma;
  18. u32_t code_size;
  19. u32_t code_lma;
  20. u32_t data_vma;
  21. u32_t data_size;
  22. u32_t data_lma;
  23. u32_t bss_vma;
  24. u32_t bss_size;
  25. };
  26. struct overlay_table
  27. {
  28. u32_t magic;
  29. u32_t count;
  30. const struct overlay_info items[7];
  31. };
  32. #ifndef __UVISION_VERSION
  33. /* overlay table is dynamic generated by linker */
  34. extern const struct overlay_table __overlay_table;
  35. #else
  36. extern char __mp3_p_overlay_bss_start[];
  37. extern char __mp3_p_overlay_bss_size[];
  38. extern char __mp3_p_overlay_data_start[];
  39. extern char __mp3_p_overlay_data_size[];
  40. extern char __mp3_p_overlay_data_image_start[];
  41. extern char __aac_p_overlay_bss_start[];
  42. extern char __aac_p_overlay_bss_size[];
  43. extern char __aac_p_overlay_data_start[];
  44. extern char __aac_p_overlay_data_size[];
  45. extern char __aac_p_overlay_data_image_start[];
  46. extern char __ape_p_overlay_bss_start[];
  47. extern int __ape_p_overlay_bss_size[];
  48. extern char __ape_p_overlay_data_start[];
  49. extern char __ape_p_overlay_data_size[];
  50. extern char __ape_p_overlay_data_image_start[];
  51. extern char __wav_p_overlay_bss_start[];
  52. extern char __wav_p_overlay_bss_size[];
  53. extern char __wav_p_overlay_data_start[];
  54. extern char __wav_p_overlay_data_size[];
  55. extern char __wav_p_overlay_data_image_start[];
  56. extern char __wma_p_overlay_bss_start[];
  57. extern char __wma_p_overlay_bss_size[];
  58. extern char __wma_p_overlay_data_start[];
  59. extern char __wma_p_overlay_data_size[];
  60. extern char __wma_p_overlay_data_image_start[];
  61. extern char __flac_p_overlay_bss_start[];
  62. extern char __flac_p_overlay_bss_size[];
  63. extern char __flac_p_overlay_data_start[];
  64. extern char __flac_p_overlay_data_size[];
  65. extern char __flac_p_overlay_data_image_start[];
  66. const struct overlay_table __overlay_table = {
  67. .magic = OVERLAY_TABLE_MAGIC,
  68. .count = 7,
  69. {
  70. {
  71. .idcode = OVERLAY_ID_LIBAPMP3,
  72. .code_vma = 0,
  73. .code_size = 0,
  74. .code_lma = 0,
  75. .data_vma = (int)__mp3_p_overlay_data_start,
  76. .data_size = 0,
  77. .data_lma = (int)__mp3_p_overlay_data_image_start,
  78. .bss_vma = (int)__mp3_p_overlay_bss_start,
  79. .bss_size = (int)__mp3_p_overlay_bss_size,
  80. },
  81. {
  82. .idcode = OVERLAY_ID_LIBAPFLA,
  83. .code_vma = 0,
  84. .code_size = 0,
  85. .code_lma = 0,
  86. .data_vma = (int)__flac_p_overlay_data_start,
  87. .data_size = (int)__flac_p_overlay_data_size,
  88. .data_lma = (int)__flac_p_overlay_data_image_start,
  89. .bss_vma = (int)__flac_p_overlay_bss_start,
  90. .bss_size = (int)__flac_p_overlay_bss_size,
  91. },
  92. {
  93. .idcode = OVERLAY_ID_LIBAPAPE,
  94. .code_vma = 0,
  95. .code_size = 0,
  96. .code_lma = 0,
  97. .data_vma = (int)__ape_p_overlay_data_start,
  98. .data_size = (int)__ape_p_overlay_data_size,
  99. .data_lma = (int)__ape_p_overlay_data_image_start,
  100. .bss_vma = (int)__ape_p_overlay_bss_start,
  101. .bss_size = (int)__ape_p_overlay_bss_size,
  102. },
  103. {
  104. .idcode = OVERLAY_ID_LIBAPWAV,
  105. .code_vma = 0,
  106. .code_size = 0,
  107. .code_lma = 0,
  108. .data_vma = (int)__wav_p_overlay_data_start,
  109. .data_size = (int)__wav_p_overlay_data_size,
  110. .data_lma = (int)__wav_p_overlay_data_image_start,
  111. .bss_vma = (int)__wav_p_overlay_bss_start,
  112. .bss_size = (int)__wav_p_overlay_bss_size,
  113. },
  114. {
  115. .idcode = OVERLAY_ID_LIBAPMP3,
  116. .code_vma = 0,
  117. .code_size = 0,
  118. .code_lma = 0,
  119. .data_vma = (int)__mp3_p_overlay_data_start,
  120. .data_size =(int)__mp3_p_overlay_data_size,
  121. .data_lma = (int)__mp3_p_overlay_data_image_start,
  122. .bss_vma = (int)__mp3_p_overlay_bss_start,
  123. .bss_size = (int)__mp3_p_overlay_bss_size,
  124. },
  125. {
  126. .idcode = OVERLAY_ID_LIBAPWMA,
  127. .code_vma = 0,
  128. .code_size = 0,
  129. .code_lma = 0,
  130. .data_vma = (int)__wma_p_overlay_data_start,
  131. .data_size = (int)__wma_p_overlay_data_size,
  132. .data_lma = (int)__wma_p_overlay_data_image_start,
  133. .bss_vma = (int)__wma_p_overlay_bss_start,
  134. .bss_size = (int)__wma_p_overlay_bss_size,
  135. },
  136. {
  137. .idcode = OVERLAY_ID_LIBAPAAC,
  138. .code_vma = 0,
  139. .code_size = 0,
  140. .code_lma = 0,
  141. .data_vma = (int)__aac_p_overlay_data_start,
  142. .data_size = (int)__aac_p_overlay_data_size,
  143. .data_lma = (int)__aac_p_overlay_data_image_start,
  144. .bss_vma = (int)__aac_p_overlay_bss_start,
  145. .bss_size = (int)__aac_p_overlay_bss_size,
  146. },
  147. },
  148. };
  149. #endif
  150. static int check_overlay_table(const struct overlay_table *tbl)
  151. {
  152. if (!tbl || tbl->magic != OVERLAY_TABLE_MAGIC || !tbl->count)
  153. return -1;
  154. return 0;
  155. }
  156. void overlay_dump(void)
  157. {
  158. const struct overlay_info *ovl;
  159. int i, cnt;
  160. if (check_overlay_table(&__overlay_table)) {
  161. printk("invalid overlay table\n");
  162. return;
  163. }
  164. cnt = __overlay_table.count;
  165. printk("section overlay, total count %d\n", cnt);
  166. for (i = 0; i < cnt; i++) {
  167. ovl = &__overlay_table.items[i];
  168. printk(" -- item %d --\n", i);
  169. printk(" idcode: %d\n", ovl->idcode);
  170. printk(" code_vma: 0x%08x\n", ovl->code_vma);
  171. printk(" code_size: 0x%08x\n", ovl->code_size);
  172. printk(" code_lma: 0x%08x\n", ovl->code_lma);
  173. printk(" data_vma: 0x%08x\n", ovl->data_vma);
  174. printk(" data_size: 0x%08x\n", ovl->data_size);
  175. printk(" data_lma: 0x%08x\n", ovl->data_lma);
  176. printk(" bss_vma: 0x%08x\n", ovl->bss_vma);
  177. printk(" bss_size: 0x%08x\n", ovl->bss_size);
  178. }
  179. }
  180. int overlay_section_init(u32_t idcode)
  181. {
  182. const struct overlay_info *ovl = NULL;
  183. int i, cnt;
  184. LOG_DBG("init overlay 0x%x", idcode);
  185. if (check_overlay_table(&__overlay_table)) {
  186. LOG_WRN("invalid overlay table\n");
  187. return -1;
  188. }
  189. cnt = __overlay_table.count;
  190. for (i = 0; i < cnt; i++) {
  191. ovl = &__overlay_table.items[i];
  192. if (idcode == ovl->idcode)
  193. break;
  194. }
  195. if (cnt == i) {
  196. LOG_WRN("cannot found overlay for idcode 0x%x\n", idcode);
  197. return -1;
  198. }
  199. if (ovl->code_size) {
  200. LOG_DBG("overlay id:0x%x: copy code from 0x%x to 0x%x, size 0x%x\n",
  201. idcode, ovl->code_lma, ovl->code_vma, ovl->code_size);
  202. memcpy((char *)ovl->code_vma, (char *)ovl->code_lma, ovl->code_size);
  203. }
  204. if (ovl->data_size) {
  205. LOG_DBG("overlay id:0x%x: copy data from 0x%x to 0x%x, size 0x%x\n",
  206. idcode, ovl->data_lma, ovl->data_vma, ovl->data_size);
  207. memcpy((char *)ovl->data_vma, (char *)ovl->data_lma, ovl->data_size);
  208. }
  209. if (ovl->bss_size) {
  210. LOG_DBG("overlay id:0x%x: init bss from 0x%x, size 0x%x\n",
  211. idcode, ovl->bss_vma, ovl->bss_size);
  212. memset((char *)ovl->bss_vma, 0x0, ovl->bss_size);
  213. }
  214. LOG_DBG("init overlay 0x%x ok", idcode);
  215. return 0;
  216. }