boot_mode.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*This file implements MTK boot mode.*/
  24. #include <sys/types.h>
  25. #include <debug.h>
  26. #include <err.h>
  27. #include <reg.h>
  28. #include <string.h>
  29. #include <platform/mt_typedefs.h>
  30. #include <platform/boot_mode.h>
  31. #include <platform/mt_reg_base.h>
  32. #include <target/cust_key.h>
  33. #include <meta.h>
  34. #include <dev/mrdump.h>
  35. #include <platform/mt_rtc.h>
  36. #include <platform/mtk_key.h>
  37. #include <platform/mt_gpt.h>
  38. #include <platform/mtk_wdt.h>
  39. #include <platform/mt_leds.h> // for mt65xx_backlight_on()
  40. #include <env.h>
  41. // global variable for specifying boot mode (default = NORMAL)
  42. BOOTMODE g_boot_mode = NORMAL_BOOT;
  43. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  44. extern BOOL kernel_power_off_charging_detection(void);
  45. #endif
  46. BOOL meta_mode_check(void)
  47. {
  48. if (g_boot_mode == META_BOOT || g_boot_mode == ADVMETA_BOOT || g_boot_mode ==ATE_FACTORY_BOOT || g_boot_mode == FACTORY_BOOT) {
  49. return TRUE;
  50. } else {
  51. return FALSE;
  52. }
  53. }
  54. extern BOOT_ARGUMENT *g_boot_arg;
  55. #define MODULE_NAME "[FACTORY]"
  56. // check the boot mode : (1) meta mode or (2) recovery mode ...
  57. static int mrdump_check(void)
  58. {
  59. if (mrdump_detection()) {
  60. mt65xx_backlight_on();
  61. if (g_boot_mode == FASTBOOT)
  62. return 1;
  63. mrdump_run2();
  64. return 1;
  65. } else {
  66. return 0;
  67. }
  68. }
  69. /* Add for reboot from normal mode to meta mode by dialer */
  70. #define REBOOT_META_FLAG "reboot_meta_flag"
  71. #define REBOOT_META_FLAG_VALUE "meta"
  72. void boot_mode_select(void)
  73. {
  74. char *meta_value = NULL;
  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. if (g_boot_mode == FASTBOOT)
  83. return;
  84. #ifdef MTK_PMIC_FULL_RESET
  85. dprintf(CRITICAL, "kedump:full pmic reset!\n");
  86. mtk_arch_full_reset();
  87. #else
  88. dprintf(CRITICAL, "kedump:sw reset!\n");
  89. mtk_arch_reset(1);
  90. #endif
  91. return;
  92. }
  93. }
  94. if (meta_detection()) {
  95. return;
  96. }
  97. meta_value = get_env(REBOOT_META_FLAG);
  98. if (meta_value != NULL) {
  99. if (strncmp(meta_value, REBOOT_META_FLAG_VALUE, strlen(REBOOT_META_FLAG_VALUE)+1) == 0) {
  100. g_boot_mode = META_BOOT;
  101. if (set_env(REBOOT_META_FLAG, "0") != 0)
  102. dprintf(INFO, "REBOOT_META_FLAG set failed!\n");
  103. return;
  104. }
  105. }
  106. #if defined(HAVE_LK_TEXT_MENU) || defined(MTK_FASTBOOT_SUPPORT)
  107. /*Check RTC to know if system want to reboot to Fastboot*/
  108. if (Check_RTC_PDN1_bit13()) {
  109. g_boot_mode = FASTBOOT;
  110. Set_Clr_RTC_PDN1_bit13(false);
  111. }
  112. /*Check RGU to know if system want to reboot to Fastboot*/
  113. #ifdef MTK_ENTER_FASTBOOT_VIA_RGU
  114. if (mtk_wdt_fastboot_check()) {
  115. g_boot_mode = FASTBOOT;
  116. mtk_wdt_fastboot_set(0); // clear RGU fastboot mode bit
  117. }
  118. #endif
  119. if (g_boot_mode == FASTBOOT) {
  120. dprintf(CRITICAL, "[FASTBOOT] reboot to boot loader\n");
  121. return;
  122. }
  123. #endif
  124. #if defined (HAVE_LK_TEXT_MENU)
  125. /*If forbidden mode is factory, cacel the factory key detection*/
  126. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  127. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  128. //Forbid to enter factory mode
  129. dprintf(CRITICAL, "%s Forbidden\n",MODULE_NAME);
  130. factory_forbidden=1;
  131. }
  132. }
  133. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  134. /*If boot reason is power key + volumn down, then
  135. disable factory mode dectection*/
  136. if (mtk_detect_pmic_just_rst()) {
  137. factory_forbidden=1;
  138. }
  139. /*Check RTC to know if system want to reboot to Recovery*/
  140. if (Check_RTC_Recovery_Mode()) {
  141. g_boot_mode = RECOVERY_BOOT;
  142. return;
  143. }
  144. /*If MISC Write has not completed in recovery mode
  145. before system reboot, go to recovery mode to
  146. finish remain tasks*/
  147. if (unshield_recovery_detection()) {
  148. return;
  149. }
  150. ulong begin = get_timer(0);
  151. /*we put key dectection here to detect key which is pressed*/
  152. dprintf(INFO, "eng build\n");
  153. while (get_timer(begin)<50) {
  154. if (!factory_forbidden) {
  155. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  156. dprintf(CRITICAL, "%s Detect key\n",MODULE_NAME);
  157. dprintf(CRITICAL, "%s Enable factory mode\n",MODULE_NAME);
  158. g_boot_mode = FACTORY_BOOT;
  159. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  160. return;
  161. }
  162. }
  163. if (mtk_detect_key(MT65XX_BOOT_MENU_KEY)) {
  164. dprintf(CRITICAL, "\n%s Check boot menu\n",MODULE_NAME);
  165. dprintf(CRITICAL, "%s Wait 50ms for special keys\n",MODULE_NAME);
  166. mtk_wdt_disable();
  167. /*************************/
  168. mt65xx_backlight_on();
  169. /*************************/
  170. boot_mode_menu_select();
  171. mtk_wdt_init();
  172. return;
  173. }
  174. #ifdef MT65XX_RECOVERY_KEY
  175. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  176. dprintf(CRITICAL, "%s Detect cal key\n",MODULE_NAME);
  177. dprintf(CRITICAL, "%s Enable recovery mode\n",MODULE_NAME);
  178. g_boot_mode = RECOVERY_BOOT;
  179. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  180. return;
  181. }
  182. #endif
  183. }
  184. #else
  185. /*We put conditions here to filer some cases that can not do key detection*/
  186. /*If forbidden mode is factory, cacel the factory key detection*/
  187. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  188. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  189. //Forbid to enter factory mode
  190. dprintf(INFO, "%s Forbidden\n",MODULE_NAME);
  191. factory_forbidden=1;
  192. }
  193. }
  194. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  195. /*If boot reason is power key + volumn down, then
  196. disable factory mode dectection*/
  197. if (mtk_detect_pmic_just_rst()) {
  198. factory_forbidden=1;
  199. }
  200. /*Check RTC to know if system want to reboot to Recovery*/
  201. if (Check_RTC_Recovery_Mode()) {
  202. g_boot_mode = RECOVERY_BOOT;
  203. return TRUE;
  204. }
  205. /*If MISC Write has not completed in recovery mode
  206. and interrupted by system reboot, go to recovery mode to
  207. finish remain tasks*/
  208. if (unshield_recovery_detection()) {
  209. return TRUE;
  210. }
  211. ulong begin = get_timer(0);
  212. /*we put key dectection here to detect key which is pressed*/
  213. while (get_timer(begin)<50) {
  214. #ifdef MTK_FASTBOOT_SUPPORT
  215. if (mtk_detect_key(MT_CAMERA_KEY)) {
  216. dprintf(INFO,"[FASTBOOT]Key Detect\n");
  217. g_boot_mode = FASTBOOT;
  218. return TRUE;
  219. }
  220. #endif
  221. if (!factory_forbidden) {
  222. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  223. dprintf(INFO, "%s Detect key\n",MODULE_NAME);
  224. dprintf(INFO, "%s Enable factory mode\n",MODULE_NAME);
  225. g_boot_mode = FACTORY_BOOT;
  226. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  227. return TRUE;
  228. }
  229. }
  230. #ifdef MT65XX_RECOVERY_KEY
  231. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  232. dprintf(INFO, "%s Detect cal key\n",MODULE_NAME);
  233. dprintf(INFO, "%s Enable recovery mode\n",MODULE_NAME);
  234. g_boot_mode = RECOVERY_BOOT;
  235. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  236. return TRUE;
  237. }
  238. #endif
  239. }
  240. #endif
  241. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  242. if (kernel_power_off_charging_detection()) {
  243. dprintf(CRITICAL, " < Kernel Power Off Charging Detection Ok> \n");
  244. return;
  245. } else {
  246. dprintf(CRITICAL, "< Kernel Enter Normal Boot > \n");
  247. }
  248. #endif
  249. }
  250. #if 1
  251. unsigned int get_chip_sw_ver_code(void)
  252. {
  253. return DRV_Reg32(APSW_VER);
  254. }
  255. CHIP_SW_VER mt_get_chip_sw_ver(void)
  256. {
  257. return (CHIP_SW_VER)get_chip_sw_ver_code();
  258. }
  259. #endif
  260. CHIP_HW_VER mt_get_chip_hw_ver(void)
  261. {
  262. return DRV_Reg32(APHW_VER);
  263. }