write_protect_ab.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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) 2017. 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. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <platform/partition.h>
  38. #include <partition_wp.h>
  39. #include <printf.h>
  40. #include <platform/boot_mode.h>
  41. #include <platform/mtk_wdt.h>
  42. #include <platform/sec_export.h>
  43. #include <env.h>
  44. #include <pal_log.h>
  45. #include "part_interface.h"
  46. #include "bootctrl.h"
  47. #include <platform/verified_boot.h>
  48. #include <part_status.h>
  49. #define WRITE_PROTECT_PARTITION_NAME_SZ (32)
  50. #ifdef MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  51. int is_protect2_ready_for_wp(void);
  52. int sync_sml_data(void);
  53. #endif
  54. void set_write_protect(void)
  55. {
  56. int err;
  57. const char *ab_suffix = NULL;
  58. char wp_start[WRITE_PROTECT_PARTITION_NAME_SZ] = {0};
  59. char wp_end[WRITE_PROTECT_PARTITION_NAME_SZ] = {0};
  60. char tmp_name[WRITE_PROTECT_PARTITION_NAME_SZ] = {0};
  61. int s = 0, e = 0, t = 0;
  62. ab_suffix = (char *)get_suffix();
  63. if (ab_suffix == NULL) {
  64. dprintf(CRITICAL, "[%s] invalid address.\n", __func__);
  65. }
  66. #ifdef MTK_UFS_OTP
  67. /*
  68. * UFS OTP partition
  69. * All booting modes shall consider if OTP partition needs lock
  70. *
  71. * NOTE: Make sure OTP partition locking is executed prior than
  72. * other partitions. Because we do not allow below case:
  73. *
  74. * - If OTP entry is already existed in current Secure Write Protection
  75. * Configuration Block and OTP partition locking request is not the first
  76. * request. For this case, UFS_OTP_ALREADY_LOCKED will be returned
  77. * and all other partition locking behind will be skipped.
  78. */
  79. /* check if we needs to lock OTP partition now */
  80. if (ufs_lk_otp_lock_req("otp") == 1) {
  81. pal_log_info("[%s] Lock OTP partition ... \n", __func__);
  82. err = partition_write_prot_set("otp", "otp", WP_PERMANENT);
  83. if (err != 0)
  84. pal_log_err("[%s] Lock otp failed: %d\n", __func__, err);
  85. } else
  86. pal_log_info("[%s] Lock OTP is not required\n", __func__);
  87. #endif /* MTK_UFS_OTP */
  88. if (g_boot_mode == NORMAL_BOOT) {
  89. pal_log_info("[%s] Lock boot region \n", __func__);
  90. err = partition_write_prot_set("preloader", "preloader", WP_POWER_ON);
  91. if (err != 0)
  92. pal_log_err("[%s] Lock boot region failed: %d\n", __func__, err);
  93. s = snprintf(wp_start, WRITE_PROTECT_PARTITION_NAME_SZ, "sec1");
  94. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "logo");
  95. if (s > 0 && e > 0) {
  96. pal_log_info("[%s]: Lock %s->%s \n", __func__, wp_start, wp_end);
  97. err = partition_write_prot_set(wp_start, wp_end, WP_POWER_ON);
  98. if (err != 0)
  99. pal_log_err("[%s]: Lock %s->%s failed:%d\n",
  100. __func__, wp_start, wp_end, err);
  101. }
  102. else {
  103. pal_log_err("[%s] Fail to set write protect partition\n", __func__);
  104. }
  105. memset(wp_start, 0, sizeof(char) * WRITE_PROTECT_PARTITION_NAME_SZ);
  106. memset(wp_end, 0, sizeof(char) * WRITE_PROTECT_PARTITION_NAME_SZ);
  107. s = snprintf(wp_start, WRITE_PROTECT_PARTITION_NAME_SZ, "md1img%s", ab_suffix);
  108. #ifdef MTK_SECURITY_SW_SUPPORT
  109. t = snprintf(tmp_name, WRITE_PROTECT_PARTITION_NAME_SZ, "system%s", ab_suffix);
  110. if (t > 0) {
  111. if (TRUE == seclib_sec_boot_enabled(TRUE) &&
  112. PART_OK == partition_exists(tmp_name)) {
  113. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "system%s", ab_suffix);
  114. /* WP for super partition is disabled when AB system is enabled. */
  115. } else
  116. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  117. }
  118. else {
  119. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  120. }
  121. #else
  122. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  123. #endif
  124. if (s > 0 && e > 0) {
  125. pal_log_info("[%s]: Lock %s->%s\n", __func__, wp_start, wp_end);
  126. err = partition_write_prot_set(wp_start, wp_end, WP_POWER_ON);
  127. if (err != 0)
  128. pal_log_err("[%s]: Lock %s->%s failed:%d\n",
  129. __func__, wp_start, wp_end, err);
  130. }
  131. else {
  132. pal_log_err("[%s] Fail to set write protect partition\n", __func__);
  133. }
  134. #ifdef MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  135. /* sync protect1 sml data to protect2 if needed */
  136. mtk_wdt_restart();
  137. err = sync_sml_data();
  138. if (err != 0)
  139. pal_log_err("sml data not sync.\n");
  140. else
  141. pal_log_info("sml data sync.\n");
  142. if (0 == is_protect2_ready_for_wp()) {
  143. pal_log_info("[%s]: protect2 is fmt.\n", __func__);
  144. pal_log_info("[%s]: Lock protect2.\n", __func__);
  145. err = partition_write_prot_set("protect2", "protect2", WP_POWER_ON);
  146. if (err != 0)
  147. pal_log_err("[%s]: Lock protect region failed:%d\n", __func__, err);
  148. }
  149. #endif
  150. }
  151. pal_log_info("[%s] Lock seccfg\n", __func__);
  152. err = partition_write_prot_set("seccfg", "seccfg", WP_POWER_ON);
  153. if (err != 0)
  154. pal_log_err("[%s]: Lock seccfg failed:%d\n", __func__, err);
  155. }
  156. void write_protect_flow(void)
  157. {
  158. #if defined(MTK_POWER_ON_WRITE_PROTECT) && !defined(MACH_FPGA)
  159. #if MTK_POWER_ON_WRITE_PROTECT == 1
  160. #if defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT)
  161. int bypass_wp = 0;
  162. #ifndef USER_BUILD
  163. bypass_wp = atoi(get_env("bypass_wp"));
  164. pal_log_err("bypass write protect flag = %d! \n", bypass_wp);
  165. #endif
  166. if (!bypass_wp) {
  167. set_write_protect();
  168. pal_log_err("write protect Done! \n");
  169. } else
  170. pal_log_err("Bypass write protect! \n");
  171. #endif
  172. #endif
  173. #endif
  174. }