write_protect.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <platform/partition.h>
  37. #include <partition_wp.h>
  38. #include <printf.h>
  39. #include <platform/boot_mode.h>
  40. #include <platform/mtk_wdt.h>
  41. #include <platform/sec_export.h>
  42. #include <env.h>
  43. #include <pal_log.h>
  44. #include "part_interface.h"
  45. #include <platform/verified_boot.h>
  46. #include <part_status.h>
  47. #define WRITE_PROTECT_PARTITION_NAME_SZ (32)
  48. #ifdef MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  49. int is_protect2_ready_for_wp(void);
  50. int sync_sml_data(void);
  51. #endif
  52. void set_write_protect(void)
  53. {
  54. int err;
  55. char wp_start[WRITE_PROTECT_PARTITION_NAME_SZ] = {0};
  56. char wp_end[WRITE_PROTECT_PARTITION_NAME_SZ] = {0};
  57. #ifdef MTK_UFS_OTP
  58. /*
  59. * UFS OTP partition
  60. * All booting modes shall consider if OTP partition needs lock
  61. *
  62. * NOTE: Make sure OTP partition locking is executed prior than
  63. * other partitions. Because we do not allow below case:
  64. *
  65. * - If OTP entry is already existed in current Secure Write Protection
  66. * Configuration Block and OTP partition locking request is not the first
  67. * request. For this case, UFS_OTP_ALREADY_LOCKED will be returned
  68. * and all other partition locking behind will be skipped.
  69. */
  70. /* check if we needs to lock OTP partition now */
  71. if (ufs_lk_otp_lock_req("otp") == 1) {
  72. pal_log_info("[%s] Lock OTP partition ... \n", __func__);
  73. err = partition_write_prot_set("otp", "otp", WP_PERMANENT);
  74. if (err != 0)
  75. pal_log_err("[%s] Lock otp failed: %d\n", __func__, err);
  76. } else
  77. pal_log_info("[%s] Lock OTP is not required\n", __func__);
  78. #endif /* MTK_UFS_OTP */
  79. #if defined(MTK_POWER_ON_WRITE_PROTECT)
  80. if (g_boot_mode == NORMAL_BOOT) {
  81. pal_log_info("[%s] Lock boot region \n", __func__);
  82. err = partition_write_prot_set("preloader", "preloader", WP_POWER_ON);
  83. if (err != 0)
  84. pal_log_err("[%s] Lock boot region failed: %d\n", __func__, err);
  85. snprintf(wp_start, WRITE_PROTECT_PARTITION_NAME_SZ, "sec1");
  86. #ifdef MTK_SECURITY_SW_SUPPORT
  87. if (TRUE == seclib_sec_boot_enabled(TRUE)) {
  88. if (PART_OK != partition_exists("system"))
  89. snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "super");
  90. else
  91. snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "system");
  92. } else
  93. snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee2");
  94. #else
  95. snprintf(wp_end, WRITE_PROTECT_PARTITION_NAME_SZ, "tee2");
  96. #endif
  97. pal_log_info("[%s]: Lock %s->%s\n", __func__, wp_start, wp_end);
  98. err = partition_write_prot_set(wp_start, wp_end, WP_POWER_ON);
  99. if (err != 0)
  100. pal_log_err("[%s]: Lock %s->%s failed:%d\n",
  101. __func__, wp_start, wp_end, err);
  102. #ifdef MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  103. /* sync protect1 sml data to protect2 if needed */
  104. mtk_wdt_restart();
  105. err = sync_sml_data();
  106. if (err != 0)
  107. pal_log_err("sml data not sync.\n");
  108. else
  109. pal_log_info("sml data sync.\n");
  110. if (0 == is_protect2_ready_for_wp()) {
  111. pal_log_info("[%s]: protect2 is fmt.\n", __func__);
  112. pal_log_info("[%s]: Lock protect2.\n", __func__);
  113. err = partition_write_prot_set("protect2", "protect2", WP_POWER_ON);
  114. if (err != 0)
  115. pal_log_err("[%s]: Lock protect region failed:%d\n", __func__, err);
  116. }
  117. #endif //MTK_SIM_LOCK_POWER_ON_WRITE_PROTECT
  118. }
  119. pal_log_info("[%s] Lock seccfg\n", __func__);
  120. err = partition_write_prot_set("seccfg", "seccfg", WP_POWER_ON);
  121. if (err != 0)
  122. pal_log_err("[%s]: Lock seccfg failed:%d\n", __func__, err);
  123. #endif
  124. }
  125. void write_protect_flow(void)
  126. {
  127. #if !defined(MACH_FPGA)
  128. #if defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT)
  129. int bypass_wp = 0;
  130. int ret = 0;
  131. #ifndef USER_BUILD
  132. bypass_wp = atoi(get_env("bypass_wp"));
  133. pal_log_err("bypass write protect flag = %d! \n", bypass_wp);
  134. #endif
  135. if (!bypass_wp) {
  136. set_write_protect();
  137. pal_log_err("write protect Done! \n");
  138. } else
  139. pal_log_err("Bypass write protect! \n");
  140. #endif
  141. #endif
  142. }