boot_mode.c 8.7 KB

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