write_protect_ab.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. return ERR_INVALID_ADDR;
  66. }
  67. #ifdef MTK_UFS_OTP
  68. /*
  69. * UFS OTP partition
  70. * All booting modes shall consider if OTP partition needs lock
  71. *
  72. * NOTE: Make sure OTP partition locking is executed prior than
  73. * other partitions. Because we do not allow below case:
  74. *
  75. * - If OTP entry is already existed in current Secure Write Protection
  76. * Configuration Block and OTP partition locking request is not the first
  77. * request. For this case, UFS_OTP_ALREADY_LOCKED will be returned
  78. * and all other partition locking behind will be skipped.
  79. */
  80. /* check if we needs to lock OTP partition now */
  81. if (ufs_lk_otp_lock_req("otp") == 1) {
  82. pal_log_info("[%s] Lock OTP partition ... \n", __func__);
  83. err = partition_write_prot_set("otp", "otp", WP_PERMANENT);
  84. if (err != 0)
  85. pal_log_err("[%s] Lock otp failed: %d\n", __func__, err);
  86. } else
  87. pal_log_info("[%s] Lock OTP is not required\n", __func__);
  88. #endif /* MTK_UFS_OTP */
  89. #if defined(MTK_POWER_ON_WRITE_PROTECT)
  90. if (g_boot_mode == NORMAL_BOOT) {
  91. pal_log_info("[%s] Lock boot region \n", __func__);
  92. err = partition_write_prot_set("preloader", "preloader", WP_POWER_ON);
  93. if (err != 0)
  94. pal_log_err("[%s] Lock boot region failed: %d\n", __func__, err);
  95. s = snprintf(wp_start, WRITE_PROTECT_PARTITION_NAME_SZ, "sec1");
  96. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "logo");
  97. if (s > 0 && e > 0) {
  98. pal_log_info("[%s]: Lock %s->%s \n", __func__, wp_start, wp_end);
  99. err = partition_write_prot_set(wp_start, wp_end, WP_POWER_ON);
  100. if (err != 0)
  101. pal_log_err("[%s]: Lock %s->%s failed:%d\n",
  102. __func__, wp_start, wp_end, err);
  103. }
  104. else {
  105. pal_log_err("[%s]: Unknown error! \n", __func__);
  106. }
  107. memset(wp_start, 0, sizeof(char) * WRITE_PROTECT_PARTITION_NAME_SZ);
  108. memset(wp_end, 0, sizeof(char) * WRITE_PROTECT_PARTITION_NAME_SZ);
  109. s= snprintf(wp_start, WRITE_PROTECT_PARTITION_NAME_SZ, "md1img%s", ab_suffix);
  110. #ifdef MTK_SECURITY_SW_SUPPORT
  111. t = snprintf(tmp_name, WRITE_PROTECT_PARTITION_NAME_SZ, "system%s", ab_suffix);
  112. if (t > 0) {
  113. if (TRUE == seclib_sec_boot_enabled(TRUE) &&
  114. PART_OK == partition_exists(tmp_name)) {
  115. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "system%s", ab_suffix);
  116. /* WP for super partition is disabled when AB system is enabled. */
  117. } else
  118. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  119. }
  120. else {
  121. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  122. }
  123. #else
  124. e = snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee%s", ab_suffix);
  125. #endif
  126. if (s > 0 && e > 0) {
  127. pal_log_info("[%s]: Lock %s->%s\n", __func__, wp_start, wp_end);
  128. err = partition_write_prot_set(wp_start, wp_end, WP_POWER_ON);
  129. if (err != 0)
  130. pal_log_err("[%s]: Lock %s->%s failed:%d\n",
  131. __func__, wp_start, wp_end, err);
  132. }
  133. else {
  134. pal_log_err("[%s] Fail to set write protect partition\n", __func__);
  135. }
  136. #ifdef MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  137. /* sync protect1 sml data to protect2 if needed */
  138. mtk_wdt_restart();
  139. err = sync_sml_data();
  140. if (err != 0)
  141. pal_log_err("sml data not sync.\n");
  142. else
  143. pal_log_info("sml data sync.\n");
  144. if (0 == is_protect2_ready_for_wp()) {
  145. pal_log_info("[%s]: protect2 is fmt.\n", __func__);
  146. pal_log_info("[%s]: Lock protect2.\n", __func__);
  147. err = partition_write_prot_set("protect2", "protect2", WP_POWER_ON);
  148. if (err != 0)
  149. pal_log_err("[%s]: Lock protect region failed:%d\n", __func__, err);
  150. }
  151. #endif
  152. }
  153. pal_log_info("[%s] Lock seccfg\n", __func__);
  154. err = partition_write_prot_set("seccfg", "seccfg", WP_POWER_ON);
  155. if (err != 0)
  156. pal_log_err("[%s]: Lock seccfg failed:%d\n", __func__, err);
  157. #endif
  158. }
  159. void write_protect_flow(void)
  160. {
  161. #if !defined(MACH_FPGA)
  162. #if defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT)
  163. int bypass_wp = 0;
  164. int ret = 0;
  165. #ifndef USER_BUILD
  166. bypass_wp = atoi(get_env("bypass_wp"));
  167. pal_log_err("bypass write protect flag = %d! \n", bypass_wp);
  168. #endif
  169. if (!bypass_wp) {
  170. set_write_protect();
  171. pal_log_err("write protect Done! \n");
  172. } else
  173. pal_log_err("Bypass write protect! \n");
  174. #endif
  175. #endif
  176. }