sec_unlock.c 15 KB

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