boot_mode.c 8.6 KB

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