vboot_state.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include <stdlib.h>
  32. #include <sys/types.h>
  33. #include <stdint.h>
  34. #include <video.h>
  35. #ifdef MTK_GPT_SCHEME_SUPPORT
  36. #include <platform/partition.h>
  37. #else
  38. #include <mt_partition.h>
  39. #endif
  40. #include <platform/mt_typedefs.h>
  41. #include <platform/boot_mode.h>
  42. #include <platform/mt_reg_base.h>
  43. #include <platform/errno.h>
  44. #include <platform/verified_boot.h>
  45. #include <printf.h>
  46. #include <string.h>
  47. #include <malloc.h>
  48. #include <platform/mt_gpt.h>
  49. #include <platform/sec_status.h>
  50. #include <platform/sec_export.h>
  51. #include <bootimg.h>
  52. #include <part_interface.h>
  53. #include <part_status.h>
  54. #include <verified_boot_common.h>
  55. #include <pal_log.h>
  56. #include <pal_typedefs.h>
  57. #include <vboot_state.h>
  58. #include <platform/vboot_keypad.h>
  59. uint32_t g_boot_state = BOOT_STATE_GREEN;
  60. extern bool cmdline_append(const char *append_string);
  61. extern void mtk_wdt_restart(void);
  62. int print_boot_state(void)
  63. {
  64. int ret = 0;
  65. switch (g_boot_state) {
  66. case BOOT_STATE_ORANGE:
  67. pal_log_err("boot state: orange\n");
  68. break;
  69. case BOOT_STATE_YELLOW:
  70. pal_log_err("boot state: yellow\n");
  71. break;
  72. case BOOT_STATE_RED:
  73. pal_log_err("boot state: red\n");
  74. break;
  75. case BOOT_STATE_GREEN:
  76. pal_log_err("boot state: green\n");
  77. break;
  78. default:
  79. pal_log_err("boot state: unknown\n");
  80. break;
  81. }
  82. return ret;
  83. }
  84. int yellow_state_warning(void)
  85. {
  86. unsigned char pubk[PUBK_LEN] = {0};
  87. int ret = 0;
  88. video_clean_screen();
  89. video_set_cursor(video_get_rows() / 2, 0);
  90. video_printf("yellow state\n\n");
  91. video_printf("Your device has loaded a different operating system\n");
  92. video_printf("ID:\n");
  93. ret = sec_get_pubk(pubk, PUBK_LEN);
  94. if (ret) {
  95. video_printf("Cannot get custom public key, abort in 5 seconds\n");
  96. mtk_wdt_restart();
  97. mdelay(5000);
  98. mtk_wdt_restart();
  99. return -1;
  100. }
  101. video_printf("%x %x %x %x %x %x %x %x\n", pubk[0], pubk[1], pubk[2], pubk[3],
  102. pubk[4], pubk[5], pubk[6], pubk[7]);
  103. video_printf("Yes (Volume UP) : Confirm and Boot.\n\n");
  104. video_printf("No (Volume Down) : Abort.\n\n");
  105. while (1) {
  106. mtk_wdt_restart();
  107. if (mtk_detect_key(VBOOT_KEYPAD_VOL_UP))
  108. return 0;
  109. else if (mtk_detect_key(VBOOT_KEYPAD_VOL_DOWN))
  110. return -1;
  111. else {
  112. /* ignore */
  113. }
  114. }
  115. }
  116. int orange_state_warning(void)
  117. {
  118. int ret = 0;
  119. video_clean_screen();
  120. video_set_cursor(video_get_rows() / 2, 0);
  121. video_printf("Orange State\n\n");
  122. video_printf("Your device has been unlocked and can't be trusted\n");
  123. /* video_printf("Your device will boot in 5 seconds\n");
  124. mtk_wdt_restart();
  125. mdelay(5000);
  126. mtk_wdt_restart();
  127. */
  128. return ret;
  129. }
  130. int red_state_warning(const char *img_name)
  131. {
  132. int ret = -1;
  133. char show_img_name[MAX_IMG_NAME_LEN] = {0};
  134. int copy_len;
  135. if (img_name != NULL) {
  136. copy_len = strlen(img_name);
  137. if (copy_len >= MAX_IMG_NAME_LEN)
  138. copy_len = MAX_IMG_NAME_LEN - 1;
  139. if (img_name[0] == '/') {
  140. memcpy((show_img_name + 1), img_name, copy_len - 1);
  141. show_img_name[copy_len - 1] = '\0';
  142. } else {
  143. memcpy(show_img_name, img_name, copy_len);
  144. show_img_name[copy_len] = '\0';
  145. }
  146. }
  147. video_clean_screen();
  148. video_set_cursor(video_get_rows() / 2, 0);
  149. video_printf("Red State\n\n");
  150. video_printf("Your device has failed verification and may not\n");
  151. video_printf("work properly.\n");
  152. video_printf("Please download %s image with correct signature\n", show_img_name);
  153. video_printf("or disable verified boot.\n");
  154. video_printf("Your device will reboot in 5 seconds.\n");
  155. mtk_wdt_restart();
  156. mdelay(5000);
  157. mtk_wdt_restart();
  158. return ret;
  159. }
  160. int show_warning(const char *img_name)
  161. {
  162. int ret = 0;
  163. switch (g_boot_state) {
  164. case BOOT_STATE_ORANGE:
  165. ret = orange_state_warning();
  166. break;
  167. case BOOT_STATE_YELLOW:
  168. #ifdef MTK_SECURITY_YELLOW_STATE_SUPPORT
  169. ret = yellow_state_warning();
  170. if (0 == ret) /* user confirms to boot into yellow state */
  171. break;
  172. /* fall into red state if user refuses to enter yellow state */
  173. #else
  174. ret = -1;
  175. /* fall into red state since yellow state is not supported */
  176. #endif
  177. case BOOT_STATE_RED:
  178. if (img_name == NULL)
  179. ret = red_state_warning("UNKNOWN");
  180. else
  181. ret = red_state_warning(img_name);
  182. ret = -1; /* return error */
  183. break;
  184. case BOOT_STATE_GREEN:
  185. default:
  186. break;
  187. }
  188. return ret;
  189. }
  190. int set_boot_state_to_cmdline()
  191. {
  192. int ret = 0;
  193. #ifndef MTK_SECURITY_SW_SUPPORT
  194. cmdline_append("androidboot.verifiedbootstate=orange");
  195. #else
  196. switch (g_boot_state) {
  197. case BOOT_STATE_ORANGE:
  198. cmdline_append("androidboot.verifiedbootstate=orange");
  199. break;
  200. case BOOT_STATE_YELLOW:
  201. cmdline_append("androidboot.verifiedbootstate=yellow");
  202. break;
  203. case BOOT_STATE_RED:
  204. cmdline_append("androidboot.verifiedbootstate=red");
  205. break;
  206. case BOOT_STATE_GREEN:
  207. cmdline_append("androidboot.verifiedbootstate=green");
  208. break;
  209. default:
  210. break;
  211. }
  212. #endif
  213. return ret;
  214. }
  215. void show_sec_reminder_warning(void)
  216. {
  217. video_clean_screen();
  218. video_set_cursor(video_get_rows() / 2, 0);
  219. video_printf("Your device has wrong setting and may not\n");
  220. video_printf("work properly\n");
  221. video_printf("Your device will boot in 5 seconds\n");
  222. mtk_wdt_restart();
  223. mdelay(5000);
  224. mtk_wdt_restart();
  225. return;
  226. }