img_workbuf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2020. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <debug.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <stdint.h>
  35. #include <platform/mt_typedefs.h>
  36. #include <mblock.h>
  37. #include <memory_layout.h>
  38. #include <pal_typedefs.h>
  39. #include <pal_log.h>
  40. #include <platform/boot_mode.h>
  41. #include <target.h>
  42. #ifndef LK_DL_MAX_SIZE
  43. #define LK_DL_MAX_SIZE SCRATCH_SIZE
  44. #endif
  45. #define VENDOR_BOOT_MBLOCK_LIMIT 0xc0000000
  46. struct img_buf {
  47. const char *name[3];
  48. void *work_buf;
  49. uint32_t work_buf_len;
  50. void *(*alloc_work_buf)(uint32_t buf_len, const char *name);
  51. void (*free_work_buf)(void *buf, uint32_t len);
  52. };
  53. static void *bootimg_work_buf_alloc(uint32_t buf_len, const char *name);
  54. static void bootimg_work_buf_free(void *buf, uint32_t len);
  55. static void *vendorboot_work_buf_alloc(uint32_t buf_len, const char *name);
  56. static void vendorboot_work_buf_free(void *buf, uint32_t len);
  57. #undef MAX_IMG_NAME_LEN
  58. #define MAX_IMG_NAME_LEN 12
  59. static struct img_buf bootimgs[] = {
  60. {
  61. .name={"boot", "recovery", NULL},
  62. .work_buf=0,
  63. .work_buf_len=0,
  64. .alloc_work_buf=bootimg_work_buf_alloc,
  65. .free_work_buf=bootimg_work_buf_free,
  66. },
  67. {
  68. .name={"vendor_boot", NULL, NULL},
  69. .work_buf=0,
  70. .work_buf_len=0,
  71. .alloc_work_buf=vendorboot_work_buf_alloc,
  72. .free_work_buf=vendorboot_work_buf_free,
  73. }
  74. };
  75. static struct img_buf *find_img_buf(const char* name)
  76. {
  77. uint32_t i;
  78. struct img_buf *found = NULL;
  79. const char **nameptr = NULL;
  80. for(i = 0 ; i < countof(bootimgs) ; i++) {
  81. nameptr = bootimgs[i].name;
  82. while(*nameptr) {
  83. if (strncmp(*nameptr, name, MAX_IMG_NAME_LEN) == 0) {
  84. found = &bootimgs[i];
  85. goto end;
  86. }
  87. nameptr++;
  88. }
  89. }
  90. end:
  91. return found;
  92. }
  93. static void *bootimg_work_buf_alloc(uint32_t buf_len, const char *name)
  94. {
  95. /* reuse scratch buffer */
  96. if (buf_len > LK_DL_MAX_SIZE)
  97. return NULL;
  98. else
  99. return target_get_scratch_address();
  100. }
  101. static void bootimg_work_buf_free(void *buf, uint32_t len)
  102. {
  103. /* do nothing */
  104. }
  105. static void *vendorboot_work_buf_alloc(uint32_t buf_len, const char *name)
  106. {
  107. u64 mblock_vendorboot_work_buf;
  108. void *work_buf;
  109. uintptr_t mblock_vendorboot_work_buf_intptr;
  110. mblock_vendorboot_work_buf = mblock_reserve_ext(&g_boot_arg->mblock_info,
  111. (uint64_t)buf_len, PAGE_SIZE, (uint64_t)VENDOR_BOOT_MBLOCK_LIMIT,
  112. 0, "vendorboot_work_buf");
  113. if (!mblock_vendorboot_work_buf) {
  114. dprintf(CRITICAL, "%s fail to mblock reserve\n", __func__);
  115. work_buf = NULL;
  116. } else {
  117. mblock_vendorboot_work_buf_intptr = (uintptr_t)mblock_vendorboot_work_buf;
  118. work_buf = (void *)mblock_vendorboot_work_buf_intptr;
  119. }
  120. return work_buf;
  121. }
  122. static void vendorboot_work_buf_free(void *buf, uint32_t len)
  123. {
  124. int ret;
  125. ret = mblock_create(&g_boot_arg->mblock_info,
  126. &g_boot_arg->orig_dram_info,
  127. (uint64_t)((uintptr_t)buf & 0xffffffff), (uint64_t)(len & 0xffffffff));
  128. if (ret)
  129. dprintf(CRITICAL, "%s fail to mblock create\n", __func__);
  130. }
  131. void *bootimg_alloc_work_buf(const char *name, uint32_t img_len)
  132. {
  133. struct img_buf *entry;
  134. void *buf;
  135. entry = find_img_buf(name);
  136. if (!entry) {
  137. pal_log_err("cannot find %s img_buf\n", name);
  138. return NULL;
  139. }
  140. if (entry->work_buf) {
  141. pal_log_err("allocate again?:%s\n", name);
  142. entry->free_work_buf(entry->work_buf, entry->work_buf_len);
  143. }
  144. buf = entry->alloc_work_buf(img_len, name);
  145. if (buf == NULL)
  146. goto error;
  147. entry->work_buf = buf;
  148. entry->work_buf_len = img_len;
  149. return buf;
  150. error:
  151. entry->work_buf = NULL;
  152. entry->work_buf_len = 0;
  153. pal_log_err("%s allocate fail, request buf_len:0x%x\n", __func__, img_len);
  154. return NULL;
  155. }
  156. void bootimg_free_work_buf(const char *name)
  157. {
  158. struct img_buf *entry;
  159. entry = find_img_buf(name);
  160. if (!entry) {
  161. pal_log_err("cannot find %s img_buf\n", name);
  162. return;
  163. }
  164. if (entry->work_buf)
  165. entry->free_work_buf(entry->work_buf, entry->work_buf_len);
  166. else
  167. pal_log_err("%s img_buf has been freed?\n", name);
  168. entry->work_buf = NULL;
  169. entry->work_buf_len = 0;
  170. }
  171. void *get_img_work_buf(const char *name)
  172. {
  173. struct img_buf *entry;
  174. entry = find_img_buf(name);
  175. if (entry)
  176. return entry->work_buf;
  177. else
  178. return NULL;
  179. }
  180. void free_bootimgs(void)
  181. {
  182. uint32_t i;
  183. struct img_buf *buf_ptr;
  184. for (i = 0 ; i < ARRAY_SIZE(bootimgs) ; i++) {
  185. buf_ptr = &bootimgs[i];
  186. if (buf_ptr->work_buf)
  187. bootimg_free_work_buf(buf_ptr->name[0]);
  188. }
  189. }