sec_unlock.c 13 KB

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