ccci_ld_md_sec.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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) 2015. 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 <sys/types.h>
  32. #include <stdint.h>
  33. #include <platform/partition.h>
  34. #include <platform/mt_typedefs.h>
  35. #include <platform/boot_mode.h>
  36. #include <platform/mt_reg_base.h>
  37. #include <platform/errno.h>
  38. #include <printf.h>
  39. #include <string.h>
  40. #include <malloc.h>
  41. #include <libfdt.h>
  42. #include <platform/mt_gpt.h>
  43. #include <platform/sec_export.h>
  44. #include <debug.h>
  45. #include "ccci_ld_md_core.h"
  46. #include "ccci_ld_md_errno.h"
  47. #include <assert.h>
  48. #include <block_generic_interface.h>
  49. #include <video.h>
  50. #include <platform/mtk_wdt.h>
  51. #include <verified_boot_common.h>
  52. #include <platform/verified_boot.h>
  53. #define MODULE_NAME "LK_LD_MD"
  54. /* Security policy */
  55. #ifdef MTK_SECURITY_SW_SUPPORT
  56. static unsigned int policy_entry_idx;
  57. static unsigned int img_auth_required;
  58. static unsigned int verify_hash;
  59. static unsigned int lte_sbc_en;
  60. static unsigned int c2k_sbc_en;
  61. static int time_md_auth;
  62. static int time_md_auth_init;
  63. static unsigned int sec_feature_mask;
  64. static int g_fill_default_pubk_hash = 0;
  65. #endif
  66. void md_img_check_reminder(void)
  67. {
  68. #ifdef MTK_SECURITY_SW_SUPPORT
  69. video_set_cursor(video_get_rows() / 2, 0);
  70. video_printf("Reminder:\n");
  71. video_printf("MD authentication failed.\n");
  72. video_printf("Please download signed MD image(-verified.img)\n");
  73. video_printf("or disable verified boot.\n");
  74. mtk_wdt_restart();
  75. mdelay(5000);
  76. mtk_wdt_restart();
  77. #endif
  78. }
  79. void ccci_ld_md_sec_init(void)
  80. {
  81. #ifdef MTK_SECURITY_SW_SUPPORT
  82. policy_entry_idx = 0;
  83. img_auth_required = 0;
  84. verify_hash = 0;
  85. lte_sbc_en = sec_get_ltemd_sbcen();
  86. c2k_sbc_en = sec_get_c2kmd_sbcen();
  87. sec_feature_mask = 0;
  88. #endif
  89. }
  90. int ccci_ld_md_sec_ptr_hdr_verify(char *partition_name, char *image_name)
  91. {
  92. int ret = 0;
  93. ALWAYS_LOG("ptr hdr verify:partition[%s], image_name[%s]\n", partition_name, image_name);
  94. /* Check sec policy to see if verification is required */
  95. #ifdef MTK_SECURITY_SW_SUPPORT
  96. policy_entry_idx = get_policy_entry_idx((char *)partition_name);
  97. ALWAYS_LOG("policy_entry_idx = %d \n", policy_entry_idx);
  98. img_auth_required = get_vfy_policy(policy_entry_idx);
  99. ALWAYS_LOG("img_auth_required = %d \n", img_auth_required);
  100. /* do verify md cert-chain if need */
  101. if (img_auth_required || 0x01 == lte_sbc_en ||
  102. 0x01 == c2k_sbc_en) {
  103. time_md_auth_init = get_timer(0);
  104. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  105. sec_feature_mask |= SEC_FEATURE_MASK_ANTIROLLBACK;
  106. #endif
  107. ret = sec_img_auth_init((char *)partition_name, (char *)image_name, sec_feature_mask);
  108. if (0 != ret) {
  109. ALWAYS_LOG("img cert-chain verification fail: 0x%x\n", ret);
  110. md_img_check_reminder();
  111. assert(0);
  112. }
  113. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  114. ret = sec_rollback_check(1);
  115. if (0 != ret) {
  116. ALWAYS_LOG("Verify %s image version fail!!!!\n", image_name);
  117. assert(0);
  118. }
  119. #endif
  120. ALWAYS_LOG("Verify %s cert chain cost %d ms\n", image_name, (int)get_timer(time_md_auth_init));
  121. } else {
  122. if ((0x01 == lte_sbc_en || 0x01 == c2k_sbc_en) &&
  123. (0 == g_fill_default_pubk_hash)) {
  124. ret = sec_set_md_default_pubk_hash();
  125. g_fill_default_pubk_hash = 1;
  126. if (ret) {
  127. ALWAYS_LOG("fail to set md1rom default key hash (0x%x)\n", ret);
  128. ret = 0;
  129. }
  130. }
  131. }
  132. #else
  133. #ifdef MD_RMA_SUPPORT
  134. ret = sec_set_md_default_pubk_hash();
  135. if (0 != ret) {
  136. ALWAYS_LOG("fail to set md default key hash (0x%x)\n", ret);
  137. ret = 0;
  138. }
  139. #endif
  140. #endif
  141. if (ret == 0)
  142. return 0;
  143. else
  144. return -1;
  145. }
  146. void ccci_ld_md_sec_image_verify(char *partition_name, char *image_name, unsigned char *mem_addr, int size)
  147. {
  148. #ifdef MTK_SECURITY_SW_SUPPORT
  149. int ret;
  150. #endif
  151. ALWAYS_LOG("image verify:partition[%s], image_name[%s], addr[%p], size[0x%x]\n",
  152. partition_name, image_name, mem_addr, size);
  153. #ifdef MTK_SECURITY_SW_SUPPORT
  154. /* Verify image hash value if needed */
  155. if (img_auth_required) {
  156. /* Verify md image hash value */
  157. /* When SBC_EN is fused, bypass sec_img_auth */
  158. verify_hash = 1;
  159. if (!strncmp((char const *)image_name, "md1rom",7)) {
  160. if (0x01 == lte_sbc_en) {
  161. ALWAYS_LOG("LTE now, and lte sbc en = 0x1 \n");
  162. /* Bypass image hash verification at AP side*/
  163. verify_hash = 0;
  164. } else
  165. ALWAYS_LOG("LTE now, and lte sbc en != 0x1 \n");
  166. }
  167. if (!strncmp((char const *)image_name, "md3rom",7)) {
  168. if (0x01 == c2k_sbc_en) {
  169. ALWAYS_LOG("C2K now, and c2k sbc en = 0x1 \n");
  170. /* Bypass image hash verification at AP side*/
  171. verify_hash = 0;
  172. } else
  173. ALWAYS_LOG("C2K now, and c2k sbc en != 0x1 \n");
  174. }
  175. if (1 == verify_hash) {
  176. time_md_auth = get_timer(0);
  177. ret = sec_img_auth(mem_addr, size);
  178. if (0 != ret) {
  179. ALWAYS_LOG("image hash verification fail: %d\n", ret);
  180. assert(0);
  181. }
  182. ALWAYS_LOG("Image hash verification success: ret = %d\n", ret);
  183. ALWAYS_LOG("Verify %s image hash cost %d ms\n", image_name, (int)get_timer(time_md_auth));
  184. }
  185. }
  186. #endif
  187. }