BootupSrvSecurity.c 39.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
 *
 * Filename:
 * ---------
 *  BootupSrvSecurity.c
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  Security part of bootup service.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

/****************************************************************************
 * Include
 ****************************************************************************/

#include "BootupSrv.h"
#include "UtilitySrvGprot.h"
#include "SimCtrlSrvGprot.h"
#include "mmi_msg_struct.h"
#include "smu_common_enums.h" /* enums of password_required_ind */
#include "sim_common_enums.h" /* enums of smu_fail_ind */
#include "app_str.h"
#include "MMIDataType.h"
#include "kal_general_types.h"
#include "BootupSrvGprot.h"
#include "custom_nvram_sec.h"
#include "MMI_common_app_trc.h"
#include "DebugInitDef_Int.h"
#include "kal_trace.h"
#include "mmi_common_app_trc.h"
#include "kal_public_api.h"
#include "BootupSrvIprot.h"
#include "mmi_frm_events_gprot.h"
#include "string.h"
#include "mmi_cb_mgr_gprot.h"
#include "mmi_frm_queue_gprot.h"
#include "app_ltlcom.h"
#include "stack_config.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "Unicodexdcl.h"
#include "mmi_frm_utility_gprot.h"
#include "ps_public_enum.h"


#define SRV_BOOTUP_MAX_PWD_LEN 16


/****************************************************************************
 * Typedef
 ****************************************************************************/

typedef struct
{
    struct
    {
        srv_bootup_sim_bit_set sim_set; /* Which L4C requires the phone lock */
        MMI_BOOL is_required;
        CHAR password[SRV_BOOTUP_MAX_PWD_LEN + 1];
        S8 n_auto_trials;
    } phone_lock;
    
    struct {
        mmi_sim_enum sim;
        srv_bootup_verification_type_enum type;
        srv_bootup_verify_result_callback result_callback;
        void *user_data;
    } session;
} srv_bootup_security_cntx_struct;


/****************************************************************************
 * Global variables
 ****************************************************************************/

static srv_bootup_security_cntx_struct g_srv_bootup_security_cntx;


/****************************************************************************
 * Local functions
 ****************************************************************************/

static void srv_bootup_sec_send_verify_req(
                mmi_sim_enum sim,
                const CHAR *pwd,
                const CHAR *new_pwd);

#ifdef __SIM_ME_LOCK__
static void srv_bootup_sec_pwd_req_ind_handle_person(
                mmi_sim_enum sim,
                srv_bootup_sim_cntx_struct *sim_cntx,
                const mmi_smu_password_required_ind_struct *pwd_req_ind);
#endif

static void srv_bootup_sec_notify_sim_status(mmi_sim_enum sim, MMI_BOOL fatal_error);

static void srv_bootup_try_to_verify_remaining_phone_locks(
                srv_bootup_security_cntx_struct *cntx,
                const CHAR *password);

/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_init_for_protocol
 * DESCRIPTION
 *  Initialize security submodule to wait for protocol events.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void srv_bootup_sec_init_for_protocol(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
	#endif
}


/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_notify_verify_result
 * DESCRIPTION
 *  Callback client to notify the result of verification.
 *  This function checks previous verification succeeded or not. And callback
 *  if there is client waiting for the result.
 * PARAMETERS
 *  sim                 [IN] From which protocol layer
 *  new_required_type   [IN] New verification type required by protocol.
 *  n_remaining_attempts [IN] Number of remaining attempts reported by protocol
 *  sim_fatal_error     [IN] There is an non-recoverable error on SIM
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_bootup_sec_notify_verify_result(
                mmi_sim_enum sim,
                srv_bootup_verification_type_enum new_required_type,
                S32 n_remaining_attempts,
                MMI_BOOL sim_fatal_error)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_security_cntx_struct *cntx;
    srv_bootup_verify_result_struct result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cntx = &(g_srv_bootup_security_cntx);

    if (cntx->session.result_callback == NULL)
    {
        return;
    }

    if (cntx->session.sim != sim)
    {
        /* Not meaningful to previous verifying */
        return;
    }
    
    result.sim = sim;
    result.type = cntx->session.type;
    result.success = MMI_FALSE;
    result.fatal_error = sim_fatal_error;
    result.n_remaining_attempts = n_remaining_attempts;

    /*
     * We still run following check even if no callback is required.
     * As the previous code can also motify other fields by the result.
     * The influence on performance is not important and ignorable.
     */
    switch (cntx->session.type)
    {
        case SRV_BOOTUP_VERI_PHONE_LOCK:
            /*
             * L4C owner:
             * SMU_FAIL_IND or SMU_PASSWORD_REQUIRED_IND(Non-phone lock)
             * will be sent only if phone lock is unlocked.
             * So if we received SMU_FAIL_IND it also implies phone
             * lock has been verified successfully
             */
        
            if (new_required_type == SRV_BOOTUP_VERI_PHONE_LOCK)
            {
                result.success = MMI_FALSE;
                
                /*
                 * Phone password verification failed. If it is our auto-trial
                 * for user's previous input, set this flag to MMI_TRUE to
                 * avoid future trial that may cause an infinite loop.
                 */
                cntx->phone_lock.is_required = MMI_TRUE;
            }
            else
            {
                result.success = MMI_TRUE;
                result.n_remaining_attempts = SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER;
                result.fatal_error = MMI_FALSE;

                /*
                 * Phone lock may be also required by other layer.
                 * We remember the phone password to avoid require user again,
                 * and do auto-trial in the future.
                 * Assume our phone only has one phone password.
                 */
                cntx->phone_lock.is_required = MMI_FALSE;

                if (cntx->phone_lock.sim_set != 0)
                {
                    /*
                     * Try to verify remaining phone locks. cntx->session.sim
                     * will be modified as the L4C in verifying.
                     */
                    srv_bootup_try_to_verify_remaining_phone_locks(
                        cntx,
                        cntx->phone_lock.password);

                    /*
                     * We will callback only until all phone locks are
                     * verified.
                     */
                    return;
                }
            }
            break;

        case SRV_BOOTUP_VERI_CHV1:
            if (new_required_type == SRV_BOOTUP_VERI_CHV1)
            {
                result.success = MMI_FALSE;
            }
            else if (new_required_type == SRV_BOOTUP_VERI_UBCHV1)
            {
                result.success = MMI_FALSE;
                result.n_remaining_attempts = 0;
            }
            else
            {
                result.success = MMI_TRUE;
                result.n_remaining_attempts = 3;
            }
            break;

        case SRV_BOOTUP_VERI_UBCHV1:
            if (new_required_type == SRV_BOOTUP_VERI_UBCHV1)
            {
                result.success = MMI_FALSE;
            }
            else if (sim_fatal_error)
            {
                result.success = MMI_FALSE;
                result.n_remaining_attempts = 0;
            }
            else
            {
                result.success = MMI_TRUE;
                result.n_remaining_attempts = 10;
            }
            break;

#ifdef __SIM_ME_LOCK__
        case SRV_BOOTUP_VERI_NP:
        case SRV_BOOTUP_VERI_NSP:
        case SRV_BOOTUP_VERI_SP:
        case SRV_BOOTUP_VERI_CP:
        case SRV_BOOTUP_VERI_SIMP:
        case SRV_BOOTUP_VERI_NSSP:
        case SRV_BOOTUP_VERI_SIMCP:
        default:
            if (new_required_type == result.type)
            {
                result.success = MMI_FALSE;
            }
            else if (sim_fatal_error)
            {
                result.success = MMI_FALSE;
                result.n_remaining_attempts = 0;
            }
            else
            {
                result.success = MMI_TRUE;
                result.n_remaining_attempts = SRV_BOOTUP_MAX_PERSONALIZATION_TRIAL_NUM;
            }
            break;
#endif /* __SIM_ME_LOCK__ */
    }

    MMI_TRACE(SRV_BOOTUP_TRC_GROUP, TRC_SRV_BOOTUP_NOTIFY_VERIFY_RESULT,
        result.sim, result.type, result.success, result.fatal_error, result.n_remaining_attempts);

    if (cntx->session.result_callback != NULL)
    {
        cntx->session.result_callback(cntx->session.user_data, &result);
        cntx->session.result_callback = NULL;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_password_required_ind_hdlr
 * DESCRIPTION
 *  Listens to SMU_PASSWORD_REQUIRED_IND from L4C.
 * PARAMETERS
 *  sim     [IN] From which protocol layer
 *  msg     [IN] mmi_smu_password_required_ind_struct*
 * RETURNS
 *  MMI_FALSE; means continue routing the message
 *****************************************************************************/
MMI_BOOL srv_bootup_sec_password_required_ind_hdlr(mmi_sim_enum sim, void *msg)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_sim_cntx_struct *sim_cntx;
    mmi_smu_password_required_ind_struct *pwd_req_ind;
    srv_bootup_security_cntx_struct *cntx;
	MMI_BOOL flag;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    sim_cntx = srv_bootup_get_sim_cntx(sim);
    cntx = &(g_srv_bootup_security_cntx);
    
    MMI_ASSERT(msg != NULL);
    pwd_req_ind = (mmi_smu_password_required_ind_struct*)msg;
    
    switch (pwd_req_ind->type)
    {
        case TYPE_NO_REQUIRED:
            cntx->phone_lock.is_required = MMI_FALSE;
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
			sim_cntx->n_remaining_attempts = SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER;
			flag = MMI_FALSE;
            break;

        case TYPE_PHONELOCK:
            if (cntx->phone_lock.password[0] != '\0' && /* User inputted */
                !cntx->phone_lock.is_required &&        /* Phone lock passed */
                cntx->phone_lock.n_auto_trials > 0)     /* Protect from infinite trial */
            {
                /* Auto verify phone lock */
                
                /*
                 * User has inputted phone password and verified successfully.
                 * Send previous input to verify other protocol layers directly.
                 * We assume all phone password of each layer are the same.
                 */
                cntx->phone_lock.n_auto_trials--;
                srv_bootup_sec_send_verify_req(
                    sim,
                    cntx->phone_lock.password,
                    NULL);

                /*
                 * We don't callback here. We can still get the result correctly
                 * by the state after the phone lock.
                 */
            }
            else
            {
                cntx->phone_lock.sim_set |= sim;
                cntx->phone_lock.is_required = MMI_TRUE;
                
                srv_bootup_sec_notify_verify_result(
                    sim,
                    SRV_BOOTUP_VERI_PHONE_LOCK,
                    SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER,
                    MMI_FALSE);
            }
			return MMI_FALSE;
            break;
#ifdef __MMI_TELEPHONY_SUPPORT__
        case TYPE_CHV1:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_CHV1;
            sim_cntx->n_remaining_attempts =
                srv_sim_ctrl_get_n_remaining_attempts(sim, SRV_SIM_CTRL_PWD_TYPE_CHV1);
			flag = MMI_FALSE;
            break;

        case TYPE_UBCHV1:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_UBCHV1;
            sim_cntx->n_remaining_attempts =
                srv_sim_ctrl_get_n_remaining_attempts(sim, SRV_SIM_CTRL_PWD_TYPE_UBCHV1);
			flag = MMI_FALSE;
            break;

        case TYPE_SIM_CARD_BLOCKED:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
            sim_cntx->n_remaining_attempts = 0;
			flag = MMI_TRUE;

            break;
#endif
#ifdef __SIM_ME_LOCK__
        case TYPE_NP:
        case TYPE_NSP:
        case TYPE_SP:
        case TYPE_CP:
        case TYPE_IMSI_LOCK:
        case TYPE_LINK_NS_SP:
        case TYPE_LINK_SIM_C:
        
            srv_bootup_sec_pwd_req_ind_handle_person(
                sim,
                sim_cntx,
                pwd_req_ind);
                
			return MMI_FALSE;
			//break;
#endif /* __SIM_ME_LOCK__ */

        default:
            /* Ignore */
			return MMI_FALSE;
            //break;
    }

	srv_bootup_sec_notify_verify_result(
		sim,
		sim_cntx->verification_type,
		sim_cntx->n_remaining_attempts,
		flag); /* NOTE: MMI_TRUE here */
	srv_bootup_sec_notify_sim_status(flag, MMI_TRUE);
    return MMI_FALSE;
}


#ifdef __SIM_ME_LOCK__
/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_pwd_req_ind_handle_person
 * DESCRIPTION
 *  Handle personalization part of SMU_PASSWORD_REQUIRED_IND.
 * PARAMETERS
 *  sim         [IN] From which protocol layer
 *  sim_cntx    [IN] Sim context
 *  pwd_req_ind [IN] mmi_smu_password_required_ind_struct*
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_bootup_sec_pwd_req_ind_handle_person(
                mmi_sim_enum sim,
                srv_bootup_sim_cntx_struct *sim_cntx,
                const mmi_smu_password_required_ind_struct *pwd_req_ind)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL fatal_error;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch (pwd_req_ind->type)
    {
        case TYPE_NP:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NP;
            break;
            
        case TYPE_NSP:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NSP;
            break;
            
        case TYPE_SP:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_SP;
            break;
            
        case TYPE_CP:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_CP;
            break;
            
        case TYPE_IMSI_LOCK:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_SIMP;
            break;
            
        case TYPE_LINK_NS_SP:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NSSP;
            break;
            
        case TYPE_LINK_SIM_C:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_SIMCP;
            break;
            
        default:
            MMI_ASSERT(MMI_FALSE);
            break;
    }

    sim_cntx->n_remaining_attempts = pwd_req_ind->cphs_retry_count;

    fatal_error = (MMI_BOOL)(sim_cntx->n_remaining_attempts == 0);
    if (fatal_error)
    {
        sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
    }
    
    srv_bootup_sec_notify_verify_result(
        sim,
        sim_cntx->verification_type,
        sim_cntx->n_remaining_attempts,
        fatal_error);

    srv_bootup_sec_notify_sim_status(sim, fatal_error);
}
#endif /* __SIM_ME_LOCK__ */


/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_smu_fail_ind_hdlr
 * DESCRIPTION
 *  Listens to SMU_FAIL_IND from L4C.
 * PARAMETERS
 *  sim     [IN] From which protocol layer
 *  msg     [IN] mmi_smu_fail_ind_struct*
 * RETURNS
 *  MMI_FALSE; means continue routing the message
 *****************************************************************************/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __CANCEL_LOCK_POWERON__
/* under construction !*/
#endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#if defined(__SIM_RECOVERY_ENHANCEMENT__)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* defined(__SIM_RECOVERY_ENHANCEMENT__) */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

MMI_BOOL srv_bootup_sec_smu_fail_ind_hdlr(mmi_sim_enum sim, void *msg)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_sim_cntx_struct *sim_cntx;
    mmi_smu_fail_ind_struct *fail_ind;
	MMI_BOOL flag;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    sim_cntx = srv_bootup_get_sim_cntx(sim);

    MMI_ASSERT(msg != NULL);
    fail_ind = (mmi_smu_fail_ind_struct*)msg;
	flag = MMI_TRUE; 

    switch (fail_ind->cause)
    {
        case SIM_ACCESS_ERROR:
        case SIM_CMD_FAIL:
        case SIM_FATAL_ERROR:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;			           
            break;

        case SIM_CARD_REMOVED:
        case SIM_NO_INSERTED:
        case SIM_PLUG_OUT:
            sim_cntx->is_inserted = MMI_FALSE;
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
#ifdef __CANCEL_LOCK_POWERON__
            srv_bootup_cv_notify_sim_removed(sim);
#endif            
            break;

        case SIM_PLUG_IN:
            sim_cntx->is_inserted = MMI_TRUE;
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
			flag = MMI_FALSE;            
            break;

#if defined(__SIM_RECOVERY_ENHANCEMENT__)
        case SIM_RECOVERY_START:
            /* We don't change the state of insertion */
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
            break;

        case SIM_RECOVERY_END:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
			flag = MMI_FALSE;            
            break;
#endif /* defined(__SIM_RECOVERY_ENHANCEMENT__) */

        case SIM_ACCESS_PROFILE_ON:
        case DUALSIM_DISCONNECTED:
            sim_cntx->verification_type = SRV_BOOTUP_VERI_NONE;
            break;

        case SIM_CHV_BLOCK:            
        case SIM_CMD_SUCCESS:
        default:
            /* Ignore */
			return MMI_FALSE;
            //break;
    }

	if(flag)
	{
		srv_bootup_sec_notify_verify_result(
			sim,
			sim_cntx->verification_type,
			0,
			MMI_TRUE);
	}
	srv_bootup_sec_notify_sim_status(sim, flag);

    return MMI_FALSE;
}

/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_notify_sim_status
 * DESCRIPTION
 *  Notify application the status change of SIM.
 * PARAMETERS
 *  sim             [IN] Which SIM.
 *  fatal_error     [IN] SIM fatal error, such as UBCHV1 blocked, removed, etc.
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_bootup_sec_notify_sim_status(mmi_sim_enum sim, MMI_BOOL fatal_error)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_sim_status_changed_evt_struct status_changed_evt;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(SRV_BOOTUP_TRC_GROUP, TRC_SRV_BOOTUP_NOTIFY_SIM_STATUS, sim, fatal_error);

    MMI_FRM_INIT_EVENT(&status_changed_evt, EVT_ID_SRV_BOOTUP_SIM_STATUS_CHANGED);
    status_changed_evt.sim = sim;
    status_changed_evt.fatal_error = fatal_error;
    MMI_FRM_CB_EMIT_POST_EVENT(&status_changed_evt);
}


/*****************************************************************************
 * FUNCTION
 *  srv_bootup_sec_send_verify_req
 * DESCRIPTION
 *  Send VERIFY_PIN_REQ
 * PARAMETERS
 *  sim     [IN] From which protocol layer
 *  pwd     [IN] Password
 *  new_pwd [IN] New password if required. If it is to verify UBCHV1, this
 *               field should be the new CHV1. If not required, this parameter
 *               can be NULL.
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_bootup_sec_send_verify_req(
                mmi_sim_enum sim,
                const CHAR *pwd,
                const CHAR *new_pwd)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_smu_verify_pin_req_struct *verify_pin_req;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    verify_pin_req = OslConstructDataPtr(sizeof(mmi_smu_verify_pin_req_struct));

    MMI_ASSERT(pwd != NULL);
    app_strlcpy((CHAR*)verify_pin_req->pin, pwd, sizeof(verify_pin_req->pin));

    if (new_pwd != NULL)
    {
        app_strlcpy((CHAR*)verify_pin_req->new_pin, new_pwd, sizeof(verify_pin_req->new_pin));
    }
    else
    {
        verify_pin_req->new_pin[0] = '\0';
    }

    /*
     * If pin_type is TYPE_UNSPECIFIED, the result will be reported
     * from SMU_PASSWORD_REQUIRED_IND or SMU_FAIL_IND.
     */
    verify_pin_req->pin_type = TYPE_UNSPECIFIED;
    mmi_frm_send_ilm(
        mmi_frm_sim_to_l4c_mod(sim),
        MSG_ID_MMI_SMU_VERIFY_PIN_REQ,
        (oslParaType*)verify_pin_req,
        NULL);
}


/****************************************************************************
 * Global functions
 ****************************************************************************/

/*****************************************************************************
 * Please refer to BootupSrvGprot.h
 *****************************************************************************/
MMI_BOOL srv_bootup_verification_is_passed(mmi_sim_enum sim)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_sim_cntx_struct *sim_cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!srv_bootup_protocol_is_ready() || srv_bootup_phone_lock_is_required)
    {
        return MMI_FALSE;    }
    
    

    sim_cntx = srv_bootup_get_sim_cntx(sim);

    return (MMI_BOOL)(sim_cntx->verification_type == SRV_BOOTUP_VERI_NONE);
}


/*****************************************************************************
 * Please refer to BootupSrvGprot.h
 *****************************************************************************/
srv_bootup_verification_type_enum srv_bootup_get_verification_info(
                                    mmi_sim_enum sim,
                                    S32 *n_remaining_attempts)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_bootup_sim_cntx_struct *sim_cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    sim_cntx = srv_bootup_get_sim_cntx(sim);

    if (srv_bootup_phone_lock_is_required())
    {
        if (n_remaining_attempts != NULL)
        {
            *n_remaining_attempts = SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER;
        }
        
        return SRV_BOOTUP_VERI_PHONE_LOCK;
    }

    if (n_remaining_attempts != NULL)
    {
        if (sim_cntx->verification_type == SRV_BOOTUP_VERI_CHV1)
        {
            /* Always get the latest number from SIM ctrl */
            *n_remaining_attempts =
                srv_sim_ctrl_get_n_remaining_attempts(sim, SRV_SIM_CTRL_PWD_TYPE_CHV1);
        }
        else if (sim_cntx->verification_type == SRV_BOOTUP_VERI_UBCHV1)
        {
            /* Always get the latest number from SIM ctrl */
            *n_remaining_attempts =
                srv_sim_ctrl_get_n_remaining_attempts(sim, SRV_SIM_CTRL_PWD_TYPE_UBCHV1);
        }
        else /* Personalizations */
        {
            *n_remaining_attempts = sim_cntx->n_remaining_attempts;
        }
    }

    return (sim_cntx->verification_type);
}


/*****************************************************************************
 * Please refer to BootupSrvIprot.h
 *****************************************************************************/
MMI_BOOL srv_bootup_phone_lock_is_required(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return (g_srv_bootup_security_cntx.phone_lock.is_required);
}


/*****************************************************************************
 * FUNCTION
 *  srv_bootup_try_to_verify_remaining_phone_locks
 * DESCRIPTION
 *  Verify phone lock of remaining L4Cs by given password.
 *  It will change cntx->session.sim as the L4C currently verifying.
 * PARAMETERS
 *  cntx        [IN/OUT] Context
 *  password    [IN]     Password in ASCII
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_bootup_try_to_verify_remaining_phone_locks(
                srv_bootup_security_cntx_struct *cntx,
                const CHAR *password)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 i;
    mmi_sim_enum sim;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(cntx->phone_lock.sim_set != 0);

    /* Find a L4C whose phone lock is required */
    for (i = 0; i < SRV_SIM_CTRL_MAX_SIM_NUM; i++)
    {
        sim = mmi_frm_index_to_sim(i);
        
        if (cntx->phone_lock.sim_set & sim)
        {
            break;
        }
    }

    cntx->phone_lock.sim_set &= ~sim;
    cntx->phone_lock.n_auto_trials--;
    cntx->session.sim = sim;
    
    srv_bootup_sec_send_verify_req(
        sim,
        password,
        NULL);
}
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

/*****************************************************************************
 * Please refer to BootupSrvGprot.h
 *****************************************************************************/
void srv_bootup_verify(
        mmi_sim_enum sim,
        const WCHAR *password,
		const WCHAR *new_password,
		srv_bootup_verify_result_callback result_callback,
		void *user_data)
{
    srv_bootup_security_cntx_struct *cntx;
    srv_bootup_verification_type_enum veri_type;
    CHAR pwd_s8[SRV_BOOTUP_MAX_PWD_LEN + 1];
    CHAR newpwd_s8[SRV_BOOTUP_MAX_PWD_LEN + 1] = {'\0'};
    cntx = &(g_srv_bootup_security_cntx);
    veri_type = srv_bootup_get_verification_info(sim, NULL);
	switch(veri_type)
	{
		case SRV_BOOTUP_VERI_PHONE_LOCK:
			 MMI_ASSERT(cntx->session.result_callback == NULL);
			 cntx->phone_lock.n_auto_trials = SRV_SIM_CTRL_MAX_SIM_NUM;			 
			 break;
		case SRV_BOOTUP_VERI_CHV1:
			 MMI_ASSERT(password != NULL);
#ifdef __INVALID_SIM_RECOVERY__
			srv_sim_ctrl_invalid_sim_recovery_set_pin(sim, pwd_s8); 
#endif 	
			break;
		case SRV_BOOTUP_VERI_UBCHV1:
			MMI_ASSERT(password != NULL && new_password != NULL);
			srv_util_convert_n_wchars_to_ascii(newpwd_s8, new_password, sizeof(newpwd_s8) - 1);
			break;
		default:
			MMI_ASSERT(0);
	}
	
    cntx->session.sim = sim;
    cntx->session.type = veri_type;
    cntx->session.result_callback = result_callback;
    cntx->session.user_data = user_data;
    srv_util_convert_n_wchars_to_ascii(pwd_s8, password, sizeof(pwd_s8) - 1);
	if(SRV_BOOTUP_VERI_PHONE_LOCK == veri_type)
	{
		memcpy(cntx->phone_lock.password, pwd_s8, sizeof(pwd_s8) - 1);
		srv_bootup_try_to_verify_remaining_phone_locks(cntx, pwd_s8);
		return;
	}
	srv_bootup_sec_send_verify_req(sim, pwd_s8, newpwd_s8);


}

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #ifdef __INVALID_SIM_RECOVERY__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif