sec_unlock.c 16 KB

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