sec_unlock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 <platform/mt_typedefs.h>
  32. #include <platform/sec_status.h>
  33. #include <platform/mt_reg_base.h>
  34. #ifdef MTK_PARTITION_COMMON
  35. #include <env.h>
  36. #else
  37. #include <platform/env.h>
  38. #endif
  39. #include "sec_unlock.h"
  40. #include "fastboot.h"
  41. #include <target/cust_key.h>
  42. #include <platform/mt_gpt.h>
  43. #include <video.h>
  44. #include <string.h>
  45. #include <debug.h>
  46. #include <platform/mtk_key.h>
  47. #include <printf.h>
  48. #ifdef MTK_GPT_SCHEME_SUPPORT
  49. #include <platform/partition.h>
  50. #else
  51. #include <mt_partition.h>
  52. #endif
  53. #include <part_interface.h>
  54. #include <part_status.h>
  55. #include <verified_boot_common.h>
  56. #define UNLOCK_KEY_SIZE 32
  57. #define SERIAL_NUMBER_SIZE 16
  58. #define DEFAULT_SERIAL_NUM "0123456789ABCDEF"
  59. char fb_unlock_key_str[UNLOCK_KEY_SIZE+1] = {0};
  60. extern u32 get_devinfo_with_index(u32 index);
  61. extern int get_serial(u64 hwkey, u32 chipid, char ser[38]);
  62. extern void sec_get_serial_number_from_unlock_key(u8 *unlock_key, u32 unlock_key_len, u8 *cal_serial, u32 cal_serial_len);
  63. extern int sec_set_device_lock(int do_lock);
  64. extern int sec_query_device_lock(int *lock_state);
  65. static void fastboot_boot_menu(void)
  66. {
  67. video_clean_screen();
  68. video_set_cursor(video_get_rows()/2, 0);
  69. video_printf("Select Boot Mode:\n[VOLUME_UP to select. VOLUME_DOWN is OK.]\n\n");
  70. video_printf("[Recovery Mode] \n");
  71. #ifdef MTK_FASTBOOT_SUPPORT
  72. video_printf("[Fastboot Mode] <<==\n");
  73. #endif
  74. video_printf("[Normal Boot] \n");
  75. #ifndef USER_BUILD
  76. video_printf("[Normal Boot +ftrace] \n");
  77. video_printf("[Normal slub debug off] \n");
  78. #endif
  79. video_printf(" => FASTBOOT mode...\n");
  80. }
  81. static int fastboot_data_part_wipe()
  82. {
  83. int ret = B_OK;
  84. int err = PART_OK;
  85. ret = set_env("unlock_erase", "start");
  86. if (ret)
  87. goto end;
  88. err = partition_erase("userdata");
  89. if (err != PART_OK) {
  90. ret = set_env("unlock_erase", "fail");
  91. if (ret)
  92. dprintf(CRITICAL,"unlock_erase fail\n");
  93. ret = -1;
  94. } else {
  95. ret = set_env("unlock_erase", "pass");
  96. if (ret)
  97. goto end;
  98. ret = B_OK;
  99. }
  100. end:
  101. return ret;
  102. }
  103. static int fastboot_get_unlock_perm(unsigned int *unlock_allowed)
  104. {
  105. int ret = B_OK;
  106. int part_ret = PART_OK;
  107. unsigned long long size = 0;
  108. unsigned long long unlock_allowed_flag_offset = 0;
  109. part_ret = partition_exists("frp");
  110. if (part_ret == PART_NOT_EXIST) {
  111. dprintf(CRITICAL,"frp paritition does not exist\n");
  112. /* backward compatible with KK, where frp partition does not exist */
  113. *unlock_allowed = 1;
  114. return B_OK;
  115. }
  116. /* get unlock enable flag address, which is inside frp partition */
  117. size = partition_get_size_by_name("frp");
  118. unlock_allowed_flag_offset = size - sizeof(unsigned int);
  119. dprintf(CRITICAL,"frp paritition size: 0x%llx\n", size);
  120. dprintf(CRITICAL,"unlock_allowed_flag_offset: 0x%llx\n", unlock_allowed_flag_offset);
  121. ret = partition_read("frp", unlock_allowed_flag_offset, (u8 *)unlock_allowed, sizeof(unsigned int));
  122. if (ret < 0) {
  123. *unlock_allowed = 0;
  124. return ret;
  125. }
  126. return B_OK;
  127. }
  128. #ifdef MTK_SEC_FASTBOOT_UNLOCK_KEY_SUPPORT
  129. static int fastboot_oem_key_chk(void)
  130. {
  131. #define SERIALNO_LEN 38
  132. int ret = B_OK;
  133. u32 chip_code = 0x0;
  134. char serial_number[SERIALNO_LEN] = {0};
  135. u64 key;
  136. char cal_serial_number[SERIAL_NUMBER_SIZE+1] = {0};
  137. /* Check for the unlock key */
  138. if (UNLOCK_KEY_SIZE != strlen(fb_unlock_key_str)) {
  139. //fastboot_fail("Unlock key length is incorrect!");
  140. ret = ERR_UNLOCK_KEY_WRONG_LENGTH;
  141. return ret;
  142. }
  143. /* Get the device serial number */
  144. key = get_devinfo_with_index(13);
  145. key = (key << 32) | get_devinfo_with_index(12);
  146. chip_code = DRV_Reg32(APHW_CODE);
  147. if (key != 0)
  148. get_serial(key, chip_code, serial_number);
  149. else
  150. memcpy(serial_number, DEFAULT_SERIAL_NUM, SERIAL_NUMBER_SIZE);
  151. /* Calculate the serial number from the unlock key */
  152. sec_get_serial_number_from_unlock_key((u8 *)fb_unlock_key_str, UNLOCK_KEY_SIZE, (u8 *)cal_serial_number, SERIAL_NUMBER_SIZE);
  153. /* Compare the results */
  154. if (0 != memcmp(serial_number, cal_serial_number, SERIAL_NUMBER_SIZE)) {
  155. //fastboot_fail("Unlock key code is incorrect!");
  156. ret = ERR_UNLOCK_WRONG_KEY_CODE;
  157. return ret;
  158. }
  159. return ret;
  160. }
  161. #endif
  162. static int fastboot_oem_unlock_chk(void)
  163. {
  164. int ret = B_OK;
  165. #ifdef MTK_SEC_FASTBOOT_UNLOCK_KEY_SUPPORT
  166. if (B_OK != (ret = fastboot_oem_key_chk())) {
  167. goto _fail;
  168. }
  169. #endif
  170. /* Do format operation of data partition */
  171. if (B_OK != (ret = fastboot_data_part_wipe())) {
  172. //fastboot_fail("Data partition wipe failed!");
  173. goto _fail;
  174. }
  175. /* Set unlock done flag */
  176. ret = sec_set_device_lock(0);
  177. _fail:
  178. return ret;
  179. }
  180. static int fastboot_oem_lock_chk()
  181. {
  182. #define TRY_LOCK 1
  183. int ret = B_OK;
  184. int inFactory = 0;
  185. int wipe_userdata = 0;
  186. if (B_OK != (ret = sec_boot_check(TRY_LOCK))) {
  187. goto _image_verify_fail;
  188. }
  189. ret = sec_is_in_factory(&inFactory);
  190. if (ret)
  191. return ret;
  192. /* tool connection in preloader is not disabled by default */
  193. /* and device must be locked before product delivery */
  194. /* at this time, don't wipe userdata to accelerate boot time */
  195. wipe_userdata = inFactory ? 0 : 1;
  196. if (wipe_userdata) {
  197. if (B_OK != (ret = fastboot_data_part_wipe())) {
  198. //fastboot_fail("Data partition wipe failed!");
  199. goto _erase_data_fail;
  200. }
  201. }
  202. /* Set lock done flag */
  203. ret = sec_set_device_lock(1);
  204. _image_verify_fail:
  205. _erase_data_fail:
  206. return ret;
  207. }
  208. void unlock_warranty(void)
  209. {
  210. video_clean_screen();
  211. video_set_cursor(video_get_rows()/2, 0);
  212. video_printf("Unlock bootloader?\n\n");
  213. video_printf("If you unlock the bootloader,you will be able to install custom operating\n");
  214. video_printf("system software on this phone.\n\n");
  215. video_printf("A custom OS is not subject to the same testing as the original OS, and can\n");
  216. video_printf("cause your phone and installed applications to stop working properly.\n\n");
  217. video_printf("To prevent unauthorized access to your personal data,unlocking the bootloader\n");
  218. video_printf("will also delete all personal data from your phone(a \"factory data reset\").\n\n");
  219. video_printf("Press the Volume UP/Down buttons to select Yes or No. \n\n");
  220. video_printf("Yes (Volume UP):Unlock(may void warranty).\n\n");
  221. video_printf("No (Volume Down):Do not unlock bootloader.\n\n");
  222. }
  223. void lock_warranty(void)
  224. {
  225. video_clean_screen();
  226. video_set_cursor(video_get_rows()/2, 0);
  227. video_printf("lock bootloader?\n\n");
  228. video_printf("If you lock the bootloader,you will need to install official operating\n");
  229. video_printf("system software on this phone.\n\n");
  230. video_printf("To prevent unauthorized access to your personal data,locking the bootloader\n");
  231. video_printf("will also delete all personal data from your phone(a \"factory data reset\").\n\n");
  232. video_printf("Press the Volume UP/Down buttons to select Yes or No. \n\n");
  233. video_printf("Yes (Volume UP):Lock bootloader.\n\n");
  234. video_printf("No (Volume Down):Do not lock bootloader.\n\n");
  235. }
  236. void fastboot_get_unlock_ability(const char *arg, void *data, unsigned sz)
  237. {
  238. #define UNLOCK_CAP_RESP_MAX_SIZE 64
  239. int ret = B_OK;
  240. unsigned int unlock_allowed = 0;
  241. char msg[UNLOCK_CAP_RESP_MAX_SIZE] = {0};
  242. ret = fastboot_get_unlock_perm(&unlock_allowed);
  243. if (ret != B_OK) {
  244. snprintf(msg, UNLOCK_CAP_RESP_MAX_SIZE, "\nFailed to get unlock permission - Err:0x%x \n", ret);
  245. msg[UNLOCK_CAP_RESP_MAX_SIZE - 1] = '\0'; /* prevent msg from not being ended with '\0'*/
  246. fastboot_fail(msg);
  247. } else {
  248. snprintf(msg, UNLOCK_CAP_RESP_MAX_SIZE, "unlock_ability = %d", unlock_allowed);
  249. msg[UNLOCK_CAP_RESP_MAX_SIZE - 1] = '\0'; /* prevent msg from not being ended with '\0'*/
  250. fastboot_info(msg);
  251. fastboot_okay("");
  252. }
  253. return;
  254. }
  255. void fastboot_oem_key(const char *arg, void *data, unsigned sz)
  256. {
  257. int key_length;
  258. key_length = strlen(arg + 1);
  259. if (key_length != UNLOCK_KEY_SIZE) {
  260. fastboot_fail("argument size is wrong\n");
  261. } else {
  262. strncpy(fb_unlock_key_str, arg + 1, sizeof(fb_unlock_key_str));
  263. fb_unlock_key_str[sizeof(fb_unlock_key_str) - 1] = '\0';
  264. dprintf(INFO,"key is '%s' and length is %d\n",fb_unlock_key_str,key_length);
  265. fastboot_okay("");
  266. }
  267. return;
  268. }
  269. void fastboot_oem_query_lock_state(const char *arg, void *data, unsigned sz)
  270. {
  271. #define LKS_RESP_MAX_SIZE 64
  272. int ret = B_OK;
  273. int lock_state;
  274. char msg[LKS_RESP_MAX_SIZE] = {0};
  275. ret = sec_query_device_lock(&lock_state);
  276. if (ret != B_OK) {
  277. snprintf(msg, LKS_RESP_MAX_SIZE, "cannot get lks (ret = 0x%x)", ret);
  278. msg[LKS_RESP_MAX_SIZE - 1] = '\0'; /* prevent msg from not being ended with '\0'*/
  279. fastboot_fail(msg);
  280. } else {
  281. snprintf(msg, LKS_RESP_MAX_SIZE, "lks = %d", lock_state);
  282. msg[LKS_RESP_MAX_SIZE - 1] = '\0'; /* prevent msg from not being ended with '\0'*/
  283. fastboot_info(msg);
  284. fastboot_okay("");
  285. }
  286. }
  287. void fastboot_oem_unlock(const char *arg, void *data, unsigned sz)
  288. {
  289. int ret = B_OK;
  290. char msg[128] = {0};
  291. unsigned int unlock_allowed = 0;
  292. unlock_warranty();
  293. while (1) {
  294. if (mtk_detect_key(MT65XX_MENU_SELECT_KEY)) { //VOL_UP
  295. fastboot_info("Start unlock flow\n");
  296. //Invoke security check after confirming "yes" by user
  297. ret = fastboot_get_unlock_perm(&unlock_allowed);
  298. if (ret != B_OK) {
  299. sprintf(msg, "\nFailed to get unlock permission - Err:0x%x \n", ret);
  300. video_printf("Unlock failed...return to fastboot in 3s\n");
  301. mdelay(3000);
  302. fastboot_boot_menu();
  303. fastboot_fail(msg);
  304. break;
  305. }
  306. dprintf(CRITICAL,"unlock_allowed = 0x%x\n", unlock_allowed);
  307. if (!unlock_allowed) {
  308. sprintf(msg, "\nUnlock operation is not allowed\n");
  309. video_printf("Unlock failed...return to fastboot in 3s\n");
  310. mdelay(3000);
  311. fastboot_boot_menu();
  312. fastboot_fail(msg);
  313. break;
  314. }
  315. ret = fastboot_oem_unlock_chk();
  316. if (ret != B_OK) {
  317. sprintf(msg, "\nUnlock failed - Err:0x%x \n", ret);
  318. video_printf("Unlock failed...return to fastboot in 3s\n");
  319. mdelay(3000);
  320. fastboot_boot_menu();
  321. fastboot_fail(msg);
  322. } else {
  323. video_printf("Unlock Pass...return to fastboot in 3s\n");
  324. mdelay(3000);
  325. fastboot_boot_menu();
  326. fastboot_okay("");
  327. }
  328. break;
  329. } else if (mtk_detect_key(MT65XX_MENU_OK_KEY)) { //VOL_DOWN
  330. video_printf("return to fastboot in 3s\n");
  331. mdelay(3000);
  332. fastboot_boot_menu();
  333. fastboot_okay("");
  334. break;
  335. } else {
  336. //If we press other keys, discard it.
  337. }
  338. }
  339. return;
  340. }
  341. void fastboot_oem_lock(const char *arg, void *data, unsigned sz)
  342. {
  343. int ret = B_OK;
  344. char msg[128] = {0};
  345. int inFactory = 0;
  346. int requireConfirmation = 0;
  347. ret = sec_is_in_factory(&inFactory);
  348. if (ret) {
  349. sprintf(msg, "\nlock failed - Err:0x%x \n", ret);
  350. video_printf("lock failed...return to fastboot in 3s\n");
  351. mdelay(3000);
  352. fastboot_boot_menu();
  353. fastboot_fail(msg);
  354. return;
  355. }
  356. requireConfirmation = inFactory ? 0 : 1;
  357. lock_warranty();
  358. while (1) {
  359. if (mtk_detect_key(MT65XX_MENU_SELECT_KEY) || !requireConfirmation) { //VOL_UP
  360. fastboot_info("Start lock flow\n");
  361. //Invoke security check after confiming "yes" by user
  362. ret = fastboot_oem_lock_chk();
  363. if (ret != B_OK) {
  364. sprintf(msg, "\nlock failed - Err:0x%x \n", ret);
  365. video_printf("lock failed...return to fastboot in 3s\n");
  366. mdelay(3000);
  367. fastboot_boot_menu();
  368. fastboot_fail(msg);
  369. } else {
  370. video_printf("lock Pass...return to fastboot in 3s\n");
  371. mdelay(3000);
  372. fastboot_boot_menu();
  373. fastboot_okay("");
  374. }
  375. break;
  376. } else if (mtk_detect_key(MT65XX_MENU_OK_KEY)) { //VOL_DOWN
  377. video_printf("return to fastboot in 3s\n");
  378. mdelay(3000);
  379. fastboot_boot_menu();
  380. fastboot_okay("");
  381. break;
  382. } else {
  383. //If we press other keys, discard it.
  384. }
  385. }
  386. return;
  387. }