boot_mode.c 8.8 KB

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