boot_mode.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /*This file implements MTK boot mode.*/
  32. #include <sys/types.h>
  33. #include <debug.h>
  34. #include <err.h>
  35. #include <reg.h>
  36. #include <platform/mt_typedefs.h>
  37. #include <platform/boot_mode.h>
  38. #include <platform/mt_reg_base.h>
  39. #include <target/cust_key.h>
  40. #include <meta.h>
  41. #include <dev/mrdump.h>
  42. #include <platform/mt_rtc.h>
  43. #include <platform/mtk_key.h>
  44. #include <platform/mt_gpt.h>
  45. #include <platform/mtk_wdt.h>
  46. #include <platform/mt_leds.h> // for mt65xx_backlight_on()
  47. // global variable for specifying boot mode (default = NORMAL)
  48. BOOTMODE g_boot_mode = NORMAL_BOOT;
  49. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  50. extern BOOL kernel_power_off_charging_detection(void);
  51. #endif
  52. BOOL meta_mode_check(void)
  53. {
  54. if (g_boot_mode == META_BOOT || g_boot_mode == ADVMETA_BOOT || g_boot_mode ==ATE_FACTORY_BOOT || g_boot_mode == FACTORY_BOOT) {
  55. return TRUE;
  56. } else {
  57. return FALSE;
  58. }
  59. }
  60. extern BOOT_ARGUMENT *g_boot_arg;
  61. #define MODULE_NAME "[FACTORY]"
  62. // check the boot mode : (1) meta mode or (2) recovery mode ...
  63. static int mrdump_check(void)
  64. {
  65. if (mrdump_detection()) {
  66. mt65xx_backlight_on();
  67. mrdump_run2();
  68. return 1;
  69. } else {
  70. return 0;
  71. }
  72. }
  73. void boot_mode_select(void)
  74. {
  75. int factory_forbidden = 0;
  76. // int forbid_mode;
  77. /*We put conditions here to filer some cases that can not do key detection*/
  78. extern int kedump_mini(void) __attribute__((weak));
  79. if (kedump_mini) {
  80. if (kedump_mini()) {
  81. mrdump_check();
  82. return;
  83. }
  84. }
  85. if (meta_detection()) {
  86. return;
  87. }
  88. mrdump_check();
  89. #if defined (HAVE_LK_TEXT_MENU)
  90. /*Check RTC to know if system want to reboot to Fastboot*/
  91. if (Check_RTC_PDN1_bit13()) {
  92. dprintf(CRITICAL, "[FASTBOOT] reboot to boot loader\n");
  93. g_boot_mode = FASTBOOT;
  94. Set_Clr_RTC_PDN1_bit13(false);
  95. return;
  96. }
  97. /*If forbidden mode is factory, cacel the factory key detection*/
  98. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  99. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  100. //Forbid to enter factory mode
  101. dprintf(CRITICAL, "%s Forbidden\n",MODULE_NAME);
  102. factory_forbidden=1;
  103. }
  104. }
  105. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  106. /*If boot reason is power key + volumn down, then
  107. disable factory mode dectection*/
  108. if (mtk_detect_pmic_just_rst()) {
  109. factory_forbidden=1;
  110. }
  111. /*Check RTC to know if system want to reboot to Recovery*/
  112. if (Check_RTC_Recovery_Mode()) {
  113. g_boot_mode = RECOVERY_BOOT;
  114. return;
  115. }
  116. /*If MISC Write has not completed in recovery mode
  117. before system reboot, go to recovery mode to
  118. finish remain tasks*/
  119. if (unshield_recovery_detection()) {
  120. return;
  121. }
  122. ulong begin = get_timer(0);
  123. /*we put key dectection here to detect key which is pressed*/
  124. dprintf(INFO, "eng build\n");
  125. #ifdef MT65XX_FACTORY_KEY
  126. dprintf(CRITICAL, "MT65XX_FACTORY_KEY 0x%x\n",MT65XX_FACTORY_KEY);
  127. #endif
  128. #ifdef MT65XX_BOOT_MENU_KEY
  129. dprintf(CRITICAL, "MT65XX_BOOT_MENU_KEY 0x%x\n",MT65XX_BOOT_MENU_KEY);
  130. #endif
  131. #ifdef MT65XX_RECOVERY_KEY
  132. dprintf(CRITICAL, "MT65XX_RECOVERY_KEY 0x%x\n",MT65XX_RECOVERY_KEY);
  133. #endif
  134. while (get_timer(begin)<50) {
  135. if (!factory_forbidden) {
  136. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  137. dprintf(CRITICAL, "%s Detect key\n",MODULE_NAME);
  138. dprintf(CRITICAL, "%s Enable factory mode\n",MODULE_NAME);
  139. g_boot_mode = FACTORY_BOOT;
  140. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  141. return;
  142. }
  143. }
  144. if (mtk_detect_key(MT65XX_BOOT_MENU_KEY)) {
  145. dprintf(CRITICAL, "\n%s Check boot menu\n",MODULE_NAME);
  146. dprintf(CRITICAL, "%s Wait 50ms for special keys\n",MODULE_NAME);
  147. mtk_wdt_disable();
  148. boot_mode_menu_select();
  149. mtk_wdt_init();
  150. return;
  151. }
  152. #ifdef MT65XX_RECOVERY_KEY
  153. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  154. dprintf(CRITICAL, "%s Detect cal key\n",MODULE_NAME);
  155. dprintf(CRITICAL, "%s Enable recovery mode\n",MODULE_NAME);
  156. g_boot_mode = RECOVERY_BOOT;
  157. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  158. return;
  159. }
  160. #endif
  161. }
  162. #else
  163. /*We put conditions here to filer some cases that can not do key detection*/
  164. /*Check RTC to know if system want to reboot to Fastboot*/
  165. #ifdef MTK_FASTBOOT_SUPPORT
  166. if (Check_RTC_PDN1_bit13()) {
  167. dprintf(INFO,"[FASTBOOT] reboot to boot loader\n");
  168. g_boot_mode = FASTBOOT;
  169. Set_Clr_RTC_PDN1_bit13(false);
  170. return TRUE;
  171. }
  172. #endif
  173. /*If forbidden mode is factory, cacel the factory key detection*/
  174. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  175. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  176. //Forbid to enter factory mode
  177. dprintf(INFO, "%s Forbidden\n",MODULE_NAME);
  178. factory_forbidden=1;
  179. }
  180. }
  181. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  182. /*If boot reason is power key + volumn down, then
  183. disable factory mode dectection*/
  184. if (mtk_detect_pmic_just_rst()) {
  185. factory_forbidden=1;
  186. }
  187. /*Check RTC to know if system want to reboot to Recovery*/
  188. if (Check_RTC_Recovery_Mode()) {
  189. g_boot_mode = RECOVERY_BOOT;
  190. return TRUE;
  191. }
  192. /*If MISC Write has not completed in recovery mode
  193. and interrupted by system reboot, go to recovery mode to
  194. finish remain tasks*/
  195. if (unshield_recovery_detection()) {
  196. return TRUE;
  197. }
  198. ulong begin = get_timer(0);
  199. /*we put key dectection here to detect key which is pressed*/
  200. while (get_timer(begin)<50) {
  201. #ifdef MTK_FASTBOOT_SUPPORT
  202. if (mtk_detect_key(MT_CAMERA_KEY)) {
  203. dprintf(INFO,"[FASTBOOT]Key Detect\n");
  204. g_boot_mode = FASTBOOT;
  205. return TRUE;
  206. }
  207. #endif
  208. if (!factory_forbidden) {
  209. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  210. dprintf(INFO, "%s Detect key\n",MODULE_NAME);
  211. dprintf(INFO, "%s Enable factory mode\n",MODULE_NAME);
  212. g_boot_mode = FACTORY_BOOT;
  213. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  214. return TRUE;
  215. }
  216. }
  217. #ifdef MT65XX_RECOVERY_KEY
  218. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  219. dprintf(INFO, "%s Detect cal key\n",MODULE_NAME);
  220. dprintf(INFO, "%s Enable recovery mode\n",MODULE_NAME);
  221. g_boot_mode = RECOVERY_BOOT;
  222. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  223. return TRUE;
  224. }
  225. #endif
  226. }
  227. #endif
  228. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  229. if (kernel_power_off_charging_detection()) {
  230. dprintf(CRITICAL, " < Kernel Power Off Charging Detection Ok> \n");
  231. return;
  232. } else {
  233. dprintf(CRITICAL, "< Kernel Enter Normal Boot > \n");
  234. }
  235. #endif
  236. }
  237. #if 1
  238. unsigned int get_chip_sw_ver_code(void)
  239. {
  240. return DRV_Reg32(APSW_VER);
  241. }
  242. CHIP_SW_VER mt_get_chip_sw_ver(void)
  243. {
  244. return (CHIP_SW_VER)get_chip_sw_ver_code();
  245. }
  246. unsigned int mt_get_chip_hw_code(void)
  247. {
  248. return DRV_Reg32(APHW_CODE);
  249. }
  250. CHIP_HW_VER mt_get_chip_hw_ver(void)
  251. {
  252. return DRV_Reg32(APHW_VER);
  253. }
  254. #endif