ccci_ld_md_sec.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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) {
  102. time_md_auth_init = get_timer(0);
  103. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  104. sec_feature_mask |= SEC_FEATURE_MASK_ANTIROLLBACK;
  105. #endif
  106. ret = sec_img_auth_init((char *)partition_name, (char *)image_name, sec_feature_mask);
  107. if (0 != ret) {
  108. ALWAYS_LOG("img cert-chain verification fail: 0x%x\n", ret);
  109. md_img_check_reminder();
  110. assert(0);
  111. }
  112. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  113. ret = sec_rollback_check(1);
  114. if (0 != ret) {
  115. ALWAYS_LOG("Verify %s image version fail!!!!\n", image_name);
  116. assert(0);
  117. }
  118. #endif
  119. ALWAYS_LOG("Verify %s cert chain cost %d ms\n", image_name, (int)get_timer(time_md_auth_init));
  120. } else {
  121. if ((0x01 == lte_sbc_en || 0x01 == c2k_sbc_en) &&
  122. (0 == g_fill_default_pubk_hash)) {
  123. ret = sec_set_md_default_pubk_hash();
  124. g_fill_default_pubk_hash = 1;
  125. if (ret) {
  126. ALWAYS_LOG("fail to set md1rom default key hash (0x%x)\n", ret);
  127. ret = 0;
  128. }
  129. }
  130. }
  131. #else
  132. #ifdef MD_RMA_SUPPORT
  133. ret = sec_set_md_default_pubk_hash();
  134. if (0 != ret) {
  135. ALWAYS_LOG("fail to set md default key hash (0x%x)\n", ret);
  136. ret = 0;
  137. }
  138. #endif
  139. #endif
  140. if (ret == 0)
  141. return 0;
  142. else
  143. return -1;
  144. }
  145. void ccci_ld_md_sec_image_verify(char *partition_name, char *image_name, unsigned char *mem_addr, int size)
  146. {
  147. #ifdef MTK_SECURITY_SW_SUPPORT
  148. int ret;
  149. #endif
  150. ALWAYS_LOG("image verify:partition[%s], image_name[%s], addr[%p], size[0x%x]\n",
  151. partition_name, image_name, mem_addr, size);
  152. #ifdef MTK_SECURITY_SW_SUPPORT
  153. /* Verify image hash value if needed */
  154. if (img_auth_required) {
  155. /* Verify md image hash value */
  156. /* When SBC_EN is fused, bypass sec_img_auth */
  157. verify_hash = 1;
  158. if (!strncmp((char const *)image_name, "md1rom",7)) {
  159. if (0x01 == lte_sbc_en) {
  160. ALWAYS_LOG("LTE now, and lte sbc en = 0x1 \n");
  161. /* Bypass image hash verification at AP side*/
  162. verify_hash = 0;
  163. } else
  164. ALWAYS_LOG("LTE now, and lte sbc en != 0x1 \n");
  165. }
  166. if (!strncmp((char const *)image_name, "md3rom",7)) {
  167. if (0x01 == c2k_sbc_en) {
  168. ALWAYS_LOG("C2K now, and c2k sbc en = 0x1 \n");
  169. /* Bypass image hash verification at AP side*/
  170. verify_hash = 0;
  171. } else
  172. ALWAYS_LOG("C2K now, and c2k sbc en != 0x1 \n");
  173. }
  174. if (1 == verify_hash) {
  175. time_md_auth = get_timer(0);
  176. ret = sec_img_auth(mem_addr, size);
  177. if (0 != ret) {
  178. ALWAYS_LOG("image hash verification fail: %d\n", ret);
  179. assert(0);
  180. }
  181. ALWAYS_LOG("Image hash verification success: ret = %d\n", ret);
  182. ALWAYS_LOG("Verify %s image hash cost %d ms\n", image_name, (int)get_timer(time_md_auth));
  183. }
  184. }
  185. #endif
  186. }