BootupSecurity.c 73.7 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 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
/*****************************************************************************
*  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:
 * ---------
 *  BootupSecurity.c
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  Security control of bootup app part.
 *
 * 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!
 *
 * 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 "MMI_features.h"
#include "BootupAppGprot.h"
#include "BootupAppProt.h"
#include "SimCtrlSrvGprot.h"
#include "UCMGprot.h"
#include "UcmSrvGprot.h"
#include "custom_ecc.h"         /* ecc_custom_verify_emergency_number */
#include "ProcedureGprot.h"
#include "MMIDataType.h"
#include "BootupSrvGprot.h"
#include "kal_general_types.h"
#include "BootupScrUtil.h"
#include "mmi_rp_app_bootup_def.h"
#include "kal_public_api.h"
#include "wgui_categories_util.h"
#include "GlobalResDef.h"
#include "Unicodexdcl.h"
#include "AlertScreen.h"
#include "mmi_frm_events_gprot.h"
#include "CommonScreensResDef.h"
#include "DebugInitDef_Int.h"
#include "BootupSrvIprot.h"
#include "custom_nvram_sec.h"
#include "custom_events_notify.h"
#include "ProfilesSrvGprot.h"
#include "mmi_frm_mem_gprot.h"
#include "GlobalConstants.h"
#include "MMI_common_app_trc.h"
#include "kal_trace.h"
#include "mmi_common_app_trc.h"
#include "mmi_frm_input_gprot.h"
#include "mmi_frm_utility_gprot.h"
#include "ShutdownSrvGprot.h"
#include "SecSetSrvGprot.h"
#include "PwdCuiGprot.h"
#include "SmlMenuGprot.h"


#define MMI_BOOTUP_SEC_MAX_PWD_LEN 8

/*
 * For CTA spec 6.1.2.1
 * If this value is defined and user fails to try the phone password
 * for given number of times, shutdown directly.
 */
#define MMI_BOOTUP_SEC_MAX_PHONE_PWD_ATTEMPTS 3

#define MMI_BOOTUP_CUI_NULL 0

#define MMI_BOOTUP_SEC_MAX_SIM_STR_LEN 66


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

typedef struct
{
    mmi_id grp_id;
    srv_bootup_verification_type_enum veri_type;
    S32 n_remaining_attempts;
    MMI_BOOL is_displaying_sim_screen;
    mmi_sim_enum sim_displaying;
    mmi_id password_screen;
    mmi_id ubchv1_screen;

    mmi_proc_func proc;
    void *user_data;
} mmi_bootup_security_cntx_struct;

typedef struct
{
    srv_bootup_verification_type_enum type;
    S8 min;
    S8 max;
} mmi_bootup_pwd_length_struct;


typedef struct
{
    srv_sim_ctrl_ua_cause_enum cause;
    U16 sim_error_str;
} mmi_bootup_sim_error_string_struct;

typedef void (*mmi_bootup_sec_continue_func)(mmi_bootup_security_cntx_struct *cntx);


/****************************************************************************
 * Configurations
 ****************************************************************************/
 
/* If user selects make emergency call, the default number to make call */
#define MMI_BOOTUP_SEC_DEFAULT_EMERGENCY_CALL_NUM   L"112"

/* Initial timeout of the retrial of personalization lock */
#define MMI_BOOTUP_WRONG_PERSON_PWD_INIT_TIMEOUT_SEC 30 /* sec */

/* Minimum length & maximum length of passwords */
static const mmi_bootup_pwd_length_struct g_mmi_bootup_sec_pwd_length[] =
{
    { SRV_BOOTUP_VERI_PHONE_LOCK, SRV_SECSET_MIN_PHONE_PASSWORD_LEN, SRV_SECSET_MAX_PHONE_PASSWORD_LEN },
    { SRV_BOOTUP_VERI_CHV1,     SRV_SIM_CTRL_MIN_CHV_LEN, SRV_SIM_CTRL_MAX_CHV_LEN },
    { SRV_BOOTUP_VERI_UBCHV1,   SRV_SIM_CTRL_MIN_UBCHV_LEN, SRV_SIM_CTRL_MAX_UBCHV_LEN },
#ifdef __SIM_ME_LOCK__
    { SRV_BOOTUP_VERI_NP,       8, 16 },
    { SRV_BOOTUP_VERI_NSP,      8, 16 },
    { SRV_BOOTUP_VERI_SP,       8, 16 },
    { SRV_BOOTUP_VERI_CP,       8, 16 },
    { SRV_BOOTUP_VERI_SIMP,     6, 16 }, /* Min length of PCK should be 6 digits by TS 22.022 */
    { SRV_BOOTUP_VERI_NSSP,     8, 16 },
    { SRV_BOOTUP_VERI_SIMCP,    8, 16 },
#endif /* #ifdef __SIM_ME_LOCK__ */
    /* ------------------------------------------------------------------------------ */
    { SRV_BOOTUP_VERI_END_OF_ENUM, 0, 0 }
};


/* If SIM error detected, the string we want to prompt user */
#if (MMI_MAX_SIM_NUM == 1) || defined(__MMI_DYNAMIC_SIM_DYNAMIC_UI__)
static const mmi_bootup_sim_error_string_struct g_mmi_bootup_sec_sim_errors_single[] =
{
#ifdef __SIM_RECOVERY_ENHANCEMENT__
    { SRV_SIM_CTRL_UA_CAUSE_RECOVERY, STR_ID_BOOTUP_SIM_CAME_OFF },
#endif /* __SIM_RECOVERY_ENHANCEMENT__ */
#ifdef  __SIM_HOT_SWAP_SUPPORT__
    { SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED, STR_ID_BOOTUP_SIM_REMOVED },
#else
    { SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED, STR_ID_BOOTUP_SIM_CAME_OFF },
#endif
    { SRV_SIM_CTRL_UA_CAUSE_ACCESS_ERROR, STR_ID_BOOTUP_SIM_ACCESS_ERROR },
    { SRV_SIM_CTRL_UA_CAUSE_CHV1_BLOCKED, STR_ID_BOOTUP_SIM_CHV1_BLOCKED },
    { SRV_SIM_CTRL_UA_CAUSE_UBCHV1_BLOCKED, STR_ID_BOOTUP_SIM_BLOCKED },
    { SRV_SIM_CTRL_UA_CAUSE_PERSON_BLOCKED, STR_ID_BOOTUP_SIM_BLOCKED },
    /* --------------------------------------------------------------------- */
    { SRV_SIM_CTRL_UA_CAUSE_END_OF_ENUM, 0 }
};
#endif /* (MMI_MAX_SIM_NUM == 1) || defined(__MMI_DYNAMIC_SIM_DYNAMIC_UI__)*/


#if (MMI_MAX_SIM_NUM >= 2)
static const mmi_bootup_sim_error_string_struct g_mmi_bootup_sec_sim_errors_multiple[] =
{
#ifdef __SIM_RECOVERY_ENHANCEMENT__
    { SRV_SIM_CTRL_UA_CAUSE_RECOVERY, STR_ID_BOOTUP_SIMX_CAME_OFF },
#endif /* __SIM_RECOVERY_ENHANCEMENT__ */
#ifdef  __SIM_HOT_SWAP_SUPPORT__
    { SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED, STR_ID_BOOTUP_SIMX_REMOVED },
#else /* __SIM_HOT_SWAP_SUPPORT__ */
    { SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED, STR_ID_BOOTUP_SIMX_CAME_OFF },
#endif /* __SIM_HOT_SWAP_SUPPORT__ */
    { SRV_SIM_CTRL_UA_CAUSE_ACCESS_ERROR, STR_ID_BOOTUP_SIMX_ACCESS_ERROR },
    { SRV_SIM_CTRL_UA_CAUSE_CHV1_BLOCKED, STR_ID_BOOTUP_SIMX_CHV1_BLOCKED},
    { SRV_SIM_CTRL_UA_CAUSE_UBCHV1_BLOCKED, STR_ID_BOOTUP_SIMX_BLOCKED },
    { SRV_SIM_CTRL_UA_CAUSE_PERSON_BLOCKED, STR_ID_BOOTUP_SIMX_BLOCKED },
    /* ----------------------------------------- */
    { SRV_SIM_CTRL_UA_CAUSE_END_OF_ENUM, 0 }
};
#endif /* (MMI_MAX_SIM_NUM >= 2) */


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

static mmi_bootup_security_cntx_struct g_mmi_bootup_security_cntx =
{
    GRP_ID_INVALID /* Not occupied by any group */
};


/****************************************************************************
 * Functions
 ****************************************************************************/

static void mmi_bootup_sec_continue_verification(
                mmi_bootup_security_cntx_struct *cntx);

static mmi_ret mmi_bootup_sec_key_proc(mmi_event_struct *evt);


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_n_attempts_to_str
 * DESCRIPTION
 *  Convert the number of remaining attempts to string representation.
 * PARAMETERS
 *  out_str_buffer          [OUT] Output string representation. Caller should
 *                                prepare enough space for the buffer.
 *  n_remaining_attempts    [IN]  The number to be converted.
 * RETURNS
 *  Pointer of out_str_buffer
 *****************************************************************************/
static WCHAR *mmi_bootup_sec_n_attempts_to_str(
                WCHAR *out_str_buffer,
                S32 max_str_len,
                S32 n_remaining_attempts)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    out_str_buffer[0] = L'\0';
    
    if (n_remaining_attempts == SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER)
    {
        out_str_buffer[0] = L'\0';
    }
    else
    {
        mmi_wsprintf(
            out_str_buffer,
            (max_str_len + 1) * sizeof(WCHAR),
            STR_ID_BOOTUP_REMAINING_TRIALS_PROMPT,
            n_remaining_attempts);
    }

    return out_str_buffer;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_get_length_restriction
 * DESCRIPTION
 *  Get the length restriction of the password.
 * PARAMETERS
 *  type        [IN]  Password type
 *  min_length  [OUT] Minimum length
 *  max_length  [OUT] Maximum length
 * RETURNS
 *  Length configuration is found or not. If not found, the length outputs
 *  will be given default values.
 *****************************************************************************/
static MMI_BOOL mmi_bootup_sec_get_length_restriction(
                    srv_bootup_verification_type_enum type,
                    S16 *min_length,
                    S16 *max_length)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    const mmi_bootup_pwd_length_struct *entry;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (entry = g_mmi_bootup_sec_pwd_length;
         entry->type != SRV_BOOTUP_VERI_END_OF_ENUM;
         entry++)
    {
        if (entry->type == type)
        {
            *min_length = entry->min;
            *max_length = entry->max;
            
            return MMI_TRUE;
        }
    }

    *min_length = 4;
    *max_length = MMI_BOOTUP_SEC_MAX_PWD_LEN;

    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_execute_ssc_or_make_ecc
 * DESCRIPTION
 *  Execute SSC handler or make emergency call by given number
 * PARAMETERS
 *  number      [IN] Number
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_execute_ssc_or_make_ecc(const WCHAR *number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

	U16 number_to_call[SRV_UCM_MAX_NUM_URI_LEN + 1];
    mmi_ucm_make_call_para_struct make_call_para;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	
    if (ecc_custom_verify_emergency_number(
            (kal_uint8*)number,
            mmi_wcslen(number),
            ECC_ENCODING_UCS2,
            ECC_ALL_CALL,
            NULL,
            NULL,
            NULL))
    {
        //mmi_ucm_app_make_call(SRV_UCM_VOICE_CALL_TYPE_ALL, (U8*)number);

		mmi_ucs2cpy((CHAR*)number_to_call, (const CHAR *)number);
	    mmi_ucm_init_call_para(&make_call_para);

	    make_call_para.dial_type = SRV_UCM_VOICE_CALL_TYPE_ALL;
	    make_call_para.ucs2_num_uri = number_to_call;
        mmi_ucm_call_launch(0, &make_call_para);
    }
    else
    {
        mmi_popup_display_simple(
            get_string(srv_ucm_query_error_message(SRV_UCM_RESULT_SOS_NUMBER_ONLY)),
            MMI_EVENT_FAILURE,
            g_mmi_bootup_security_cntx.grp_id,
            &g_mmi_bootup_security_cntx);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_make_emergency_call
 * DESCRIPTION
 *  Make emergency call and clean the input of active password screen.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_make_emergency_call(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_ucm_make_call_para_struct makecall_para;
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cntx = &(g_mmi_bootup_security_cntx);

    mmi_ucm_init_call_para(&makecall_para);
    makecall_para.dial_type = SRV_UCM_VOICE_CALL_TYPE_ALL;
    makecall_para.ucs2_num_uri = (U16*)MMI_BOOTUP_SEC_DEFAULT_EMERGENCY_CALL_NUM;
    mmi_ucm_call_launch(0, &makecall_para);  

    if (cntx->password_screen != MMI_BOOTUP_CUI_NULL)
    {
        /* Don't assert here, because it is emergency call! */
        cui_pwd_basic_clear_input(cntx->password_screen);
    }
    else if (cntx->ubchv1_screen != MMI_BOOTUP_CUI_NULL)
    {
        cui_pwd_guard_clear_input_of_active(cntx->ubchv1_screen);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_ec_confirm_proc
 * DESCRIPTION
 *  Event proc of emergency call confirm
 * PARAMETERS
 *  evt     [IN] mmi_alert_result_evt_struct*
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_ec_confirm_proc(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (evt->evt_id == EVT_ID_ALERT_QUIT)
    {
        mmi_alert_result_evt_struct *result_evt;

        result_evt = (mmi_alert_result_evt_struct*)evt;
        if (result_evt->result == MMI_ALERT_CNFM_YES)
        {
            mmi_bootup_sec_make_emergency_call();
        }
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_confirm_emergency_call
 * DESCRIPTION
 *  Show a confirm dialog for emergency call.
 * PARAMETERS
 *  user_data       [IN] mmi_bootup_security_cntx_struct*
 *  handle          [IN] Password screen handle
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_confirm_emergency_call(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;
    mmi_confirm_property_struct confirm_arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(evt->user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)evt->user_data;

    mmi_confirm_property_init(&confirm_arg, CNFM_TYPE_YESNO);
    confirm_arg.parent_id = cntx->grp_id;
    confirm_arg.callback = mmi_bootup_sec_ec_confirm_proc;
    confirm_arg.user_tag = cntx;
    confirm_arg.f_auto_dismiss = 1;
    mmi_confirm_display(
        get_string(STR_ID_BOOTUP_EMERGENCY_CALL_ASK),
        MMI_EVENT_QUERY,
        &confirm_arg);

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_popup_callback
 * DESCRIPTION
 *  Callback of popup.
 *  Continue execution after the popop.
 * PARAMETERS
 *  evt     [IN]
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_popup_callback(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_alert_result_evt_struct *popup_evt;
    mmi_bootup_sec_continue_func continue_func;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (evt->evt_id == EVT_ID_POPUP_QUIT)
    {
        popup_evt = (mmi_alert_result_evt_struct*)evt;
        continue_func = (mmi_bootup_sec_continue_func)(popup_evt->user_tag);
        MMI_ASSERT(continue_func != NULL);
        continue_func(&(g_mmi_bootup_security_cntx));
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_popup_and_continue
 * DESCRIPTION
 *  Popup an message then continue to execute continue_func().
 * PARAMETERS
 *  message         [IN] Message
 *  popup_type      [IN] Popup type
 *  continue_func   [IN] The function to be triggered after the popup
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_popup_and_continue(
                mmi_bootup_security_cntx_struct *cntx,
                const WCHAR* message,
                mmi_event_notify_enum popup_type,
                mmi_bootup_sec_continue_func continue_func)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_popup_property_struct property;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(continue_func != NULL);

    cntx->veri_type = SRV_BOOTUP_VERI_NONE;
    cntx->n_remaining_attempts = 0;
    /* Set to false to avoid another popup on SIM fatal error event */
    cntx->is_displaying_sim_screen = MMI_FALSE;
    cntx->sim_displaying = MMI_SIM_NONE;

    mmi_popup_property_init(&property);
    property.callback = mmi_bootup_sec_popup_callback;
    property.parent_id = cntx->grp_id;
    property.user_tag = (void*)continue_func;
    
    if (mmi_popup_display(
            (WCHAR*)message,
            popup_type,
            &property) != GRP_ID_INVALID)
    {
        /* Do nothing, continue after callback */
    }
    else
    {
        continue_func(cntx);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_popup_sim_error_and_continue
 * DESCRIPTION
 *  Get the unavailable cause of the SIM, popup proper string and continue
 *  to execute continue_func()
 * PARAMETERS
 *  cntx            [IN] Context
 *  sim             [IN] For which SIM
 *  continue_func   [IN] The function to be triggered after the popup
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_popup_sim_error_and_continue(
                mmi_bootup_security_cntx_struct *cntx,
                mmi_sim_enum sim,
                mmi_bootup_sec_continue_func continue_func)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_sim_ctrl_ua_cause_enum cause;
    U16 error_str_id;
    WCHAR error_str[MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 1];
    const mmi_bootup_sim_error_string_struct *str_entry = NULL;
    mmi_event_notify_enum popup_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    error_str_id = STR_GLOBAL_ERROR;

    cause = srv_sim_ctrl_get_unavailable_cause(sim);
    popup_type = MMI_EVENT_FAILURE;

#if (MMI_MAX_SIM_NUM == 1)
    str_entry = g_mmi_bootup_sec_sim_errors_single;
#else /* (MMI_MAX_SIM_NUM == 1) */
#if defined(__MMI_DYNAMIC_SIM_DYNAMIC_UI__)
    if (srv_sim_ctrl_get_num_of_inserted() <= 1)
    {
        str_entry = g_mmi_bootup_sec_sim_errors_single;
    }
    else
#endif
    {
        str_entry = g_mmi_bootup_sec_sim_errors_multiple;
    }
#endif /* (MMI_MAX_SIM_NUM == 1) */

    for ( ;
         str_entry->cause != SRV_SIM_CTRL_UA_CAUSE_END_OF_ENUM;
         str_entry++)
    {
        if (str_entry->cause == cause)
        {
            error_str_id = str_entry->sim_error_str;
            break;
        }
    }

#if (MMI_MAX_SIM_NUM == 1)

    mmi_wcsncpy(error_str, get_string(error_str_id), MMI_BOOTUP_SEC_MAX_SIM_STR_LEN);
    
#else /* (MMI_MAX_SIM_NUM == 1) */
#if defined(__MMI_DYNAMIC_SIM_DYNAMIC_UI__)
    if (srv_sim_ctrl_get_num_of_inserted() <= 1)
    {
        mmi_wcsncpy(error_str, get_string(error_str_id), MMI_BOOTUP_SEC_MAX_SIM_STR_LEN);
    }
    else
#endif
    {
        mmi_sim_ctrl_gen_str(
            error_str,
            MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
            error_str_id,
            sim);
    }
#endif /* (MMI_MAX_SIM_NUM == 1) */

#ifdef __SIM_HOT_SWAP_SUPPORT__
    if (cause == SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED)
    {
        popup_type = MMI_EVENT_INFO;
    }
#endif

    mmi_bootup_sec_popup_and_continue(
        cntx,
        error_str,
        popup_type,
        continue_func);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_close_password_screen_and_continue
 * DESCRIPTION
 *  Close password screen and continue next verification.
 * PARAMETERS
 *  cntx            [IN] Context
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_close_password_screen_and_continue(
                mmi_bootup_security_cntx_struct *cntx)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cui_pwd_basic_close(cntx->password_screen);
    cntx->password_screen = MMI_BOOTUP_CUI_NULL;
    
    mmi_bootup_sec_continue_verification(cntx);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_close_password_screen_and_continue
 * DESCRIPTION
 *  Close UBCHV1 screen and continue next verification.
 * PARAMETERS
 *  cntx            [IN] Context
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_close_ubchv1_screen_and_continue(
                mmi_bootup_security_cntx_struct *cntx)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cui_pwd_guard_close(cntx->ubchv1_screen);
    cntx->ubchv1_screen = MMI_BOOTUP_CUI_NULL;
    
    mmi_bootup_sec_continue_verification(cntx);
}


#ifdef __MMI_SML_UNLOCK_RETRY_TIMER__ /* wrapped under __MMI_SML_MENU__, which is dependent upon __SIM_ME_LOCK__ */
/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_is_personalization_type
 * DESCRIPTION
 *  Query whether the type is personalization lock.
 * PARAMETERS
 *  type    [IN] Verification type
 * RETURNS
 *  MMI_TRUE for personalization type
 *****************************************************************************/
static MMI_BOOL mmi_bootup_sec_is_personalization_type(
                    srv_bootup_verification_type_enum type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch (type)
    {
        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:
            return MMI_TRUE;

        default:
            break;
    }

    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_per_on_waiting_close
 * DESCRIPTION
 *  To control the close of personalization waiting screen.
 *  Allow to close only if timeout.
 * PARAMETERS
 *  evt     [IN]
 * RETURNS
 *  MMI_BOOTUP_SCR_ALLOW_TO_CLOSE only if timeout.
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_per_on_waiting_close(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (evt->evt_id == EVT_ID_MMI_BOOTUP_SCR_TIMEOUT)
    {
        return MMI_BOOTUP_SCR_ALLOW_TO_CLOSE;
    }

    return MMI_BOOTUP_SCR_DONT_CLOSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_per_show_waiting
 * DESCRIPTION
 *  Show count-down waiting for wrong personalization password input after
 *  popup quited.
 * PARAMETERS
 *  evt     [IN] 
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_per_show_waiting(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;
    mmi_alert_result_evt_struct *popup_evt;
    S32 n_remaining_attempts;
    S32 n_already_trials;
    S32 duration_sec;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (evt->evt_id != EVT_ID_POPUP_QUIT)
    {
        return MMI_RET_OK; /* Ignore, we only wait for QUIT event */
    }

    popup_evt = (mmi_alert_result_evt_struct*)evt;
    cntx = (mmi_bootup_security_cntx_struct*)popup_evt->user_tag;

    srv_bootup_get_verification_info(cntx->sim_displaying, &n_remaining_attempts);

    n_already_trials = SRV_BOOTUP_MAX_PERSONALIZATION_TRIAL_NUM - 1 - n_remaining_attempts;
    duration_sec = (1 << n_already_trials) * MMI_BOOTUP_WRONG_PERSON_PWD_INIT_TIMEOUT_SEC;
    
	#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 !*/
	#endif

    return MMI_RET_OK;
}
#endif /* __MMI_SML_UNLOCK_RETRY_TIMER__ */


#ifdef MMI_BOOTUP_SEC_MAX_PHONE_PWD_ATTEMPTS
/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_shutdown_after_popup
 * DESCRIPTION
 *  Launch shutdown after popup exits.
 * PARAMETERS
 *  evt     [IN]
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_shutdown_after_popup(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (evt->evt_id == EVT_ID_POPUP_QUIT)
    {
        srv_shutdown_normal_start(APP_BOOTUP);
    }

    return MMI_RET_OK;
}
#endif


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_verify_result
 * DESCRIPTION
 *  Result callback for verfication of phone lock, CHV1 and personalizeion.
 * PARAMETERS
 *  user_data   [IN] mmi_bootup_security_cntx_struct*
 *  result      [IN] Result
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_verify_result(
                void *user_data,
                const srv_bootup_verify_result_struct *result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)user_data;
    
    if (result->success)
    {
        mmi_bootup_sec_popup_and_continue(
            cntx,
            get_string(STR_GLOBAL_DONE),
            MMI_EVENT_SUCCESS,
            mmi_bootup_close_password_screen_and_continue);
    }
    else if (!result->fatal_error && result->n_remaining_attempts > 0)
    {
        WCHAR *str_buffer;

#ifdef MMI_BOOTUP_SEC_MAX_PHONE_PWD_ATTEMPTS
        if (result->type == SRV_BOOTUP_VERI_PHONE_LOCK)
        {
            cntx->n_remaining_attempts--;
            if (cntx->n_remaining_attempts == 0)
            {
                /* Popup then shutdown */
                mmi_popup_property_struct property;

                mmi_popup_property_init(&property);
                property.callback = mmi_bootup_sec_shutdown_after_popup;
                property.parent_id = cntx->grp_id;
                property.user_tag = cntx;
                mmi_popup_display(
                    get_string(STR_ID_BOOTUP_WRONG_PHONE_PASSWORD),
                    MMI_EVENT_FAILURE,
                    &property);

                return;
            }
        }
        else
        {
            cntx->n_remaining_attempts = result->n_remaining_attempts;
        }
#else
        cntx->n_remaining_attempts = result->n_remaining_attempts;
#endif

        str_buffer = (WCHAR*)mmi_malloc(
            (MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 1) * sizeof(WCHAR));

#ifdef __MMI_SML_UNLOCK_RETRY_TIMER__
        if (mmi_bootup_sec_is_personalization_type(result->type))
        {
            mmi_popup_property_struct property;
            
            mmi_popup_property_init(&property);
            property.callback = mmi_bootup_sec_per_show_waiting;
            property.parent_id = cntx->grp_id;
            property.user_tag = cntx;
            mmi_popup_display(
                mmi_bootup_sec_get_message_of_result(result, str_buffer, MMI_BOOTUP_SEC_MAX_SIM_STR_LEN),
                MMI_EVENT_FAILURE,
                &property);
        }
        else
#endif
        {
            mmi_popup_display_simple(
                mmi_bootup_sec_get_message_of_result(result, str_buffer, MMI_BOOTUP_SEC_MAX_SIM_STR_LEN),
                MMI_EVENT_FAILURE,
                cntx->grp_id,
                cntx);
        }

        /* Update the prompt of the number of remaining attempts */
        mmi_bootup_sec_n_attempts_to_str(
            str_buffer,
            MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
            cntx->n_remaining_attempts);
        cui_pwd_basic_set_extra_msg(
            cntx->password_screen,
            str_buffer);
        mmi_mfree(str_buffer);
        
        cui_pwd_basic_clear_input(cntx->password_screen);
    }
    else if (result->type == SRV_BOOTUP_VERI_PHONE_LOCK)
    {
        /* We can try phone lock infinitely */
        MMI_ASSERT(MMI_FALSE);
    }
    else /* Error on SIM or entered other state(e.g. CHV1 -> UBCHV1) */
    {
        mmi_bootup_popup_sim_error_and_continue(
            cntx,
            result->sim,
            mmi_bootup_close_password_screen_and_continue);
    }

    mmi_bootup_waiting_screen_close(cntx->grp_id);
}


static mmi_ret mmi_bootup_sec_verify_phone_lock_proc(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(evt->user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)evt->user_data;

    switch (evt->evt_id)
    {
        case EVT_ID_CUI_PWD_BASIC_DONE:
        {
            cui_pwd_basic_done_evt_struct *done_evt;

            done_evt = (cui_pwd_basic_done_evt_struct*)evt;
            mmi_bootup_waiting_screen_show(
                cntx->grp_id,
                mmi_get_event_based_image(MMI_EVENT_PROGRESS),
                STR_GLOBAL_PLEASE_WAIT);

            srv_bootup_verify(
				MMI_SIM1,
                done_evt->input,
                NULL,
                mmi_bootup_sec_verify_result,
                cntx);

            break;
        }
        case EVT_ID_CUI_PWD_BASIC_KEY:
            return mmi_bootup_sec_key_proc(evt);

        case EVT_ID_CUI_PWD_BASIC_AUX:
            return mmi_bootup_sec_confirm_emergency_call(evt);
            
        case EVT_ID_CUI_PWD_BASIC_PASSIVE_CLOSING:
            return CUI_PWD_DONT_CLOSE;
    }

    return MMI_RET_OK;
}


#ifdef __CANCEL_LOCK_POWERON__
/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_cancel_veri_result_callback
 * DESCRIPTION
 *  Result callback of cancelling verification of the SIM.
 * PARAMETERS
 *  user_data   [IN] mmi_bootup_security_cntx_struct*
 *  result      [IN] Result
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_cancel_veri_result_callback(
                void *user_data,
                const srv_bootup_cancel_verification_result_struct *result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)user_data;

    if (result->success)
    {
        /*
         * We don't need to input the password if the verification
         * is cancelled successfully.
         */
        if (cntx->password_screen != MMI_BOOTUP_CUI_NULL)
        {
            mmi_bootup_close_password_screen_and_continue(cntx);
        }
        else if (cntx->ubchv1_screen != MMI_BOOTUP_CUI_NULL)
        {
            mmi_bootup_close_ubchv1_screen_and_continue(cntx);
        }
    }
    else
    {
        // TODO: Display a more suitable prompt, e.g. "Please input password"
        mmi_popup_display_simple(
            get_string(STR_GLOBAL_NOT_AVAILABLE),
            MMI_EVENT_FAILURE,
            cntx->grp_id,
            cntx);
    }

    mmi_bootup_waiting_screen_close(cntx->grp_id);
}
#endif


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_verify_chv1_or_per_proc
 * DESCRIPTION
 *  Verify phone lock.
 * PARAMETERS
 *  user_data   [IN] mmi_bootup_security_cntx_struct*
 *  input       [IN] Input information of the password screen
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_verify_chv1_or_per_proc(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(evt->user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)evt->user_data;

    switch (evt->evt_id)
    {
        case EVT_ID_CUI_PWD_BASIC_DONE:
        {
            cui_pwd_basic_done_evt_struct *done_evt;

            done_evt = (cui_pwd_basic_done_evt_struct*)evt;
        
            mmi_bootup_waiting_screen_show(
                cntx->grp_id,
                mmi_get_event_based_image(MMI_EVENT_PROGRESS),
                STR_GLOBAL_PLEASE_WAIT);

            srv_bootup_verify(
                cntx->sim_displaying,
                done_evt->input,
                NULL,
                mmi_bootup_sec_verify_result,
                cntx);
                
            break;
        }
        case EVT_ID_CUI_PWD_BASIC_KEY:
            return mmi_bootup_sec_key_proc(evt);

        case EVT_ID_CUI_PWD_BASIC_AUX:
            return mmi_bootup_sec_confirm_emergency_call(evt);
            
        case EVT_ID_CUI_PWD_BASIC_PASSIVE_CLOSING:
            return CUI_PWD_DONT_CLOSE;
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_key_proc
 * DESCRIPTION
 *  Key proc to detect keys on password screen.
 *  Execute SSC or make call for Send key.
 *  Cancel verification for End key if possible.
 * PARAMETERS
 *  evt     [IN] cui_pwd_general_key_evt_struct*
 * RETURNS
 *  mmi_ret
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_key_proc(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;
    cui_pwd_general_key_evt_struct *key_evt;
    const WCHAR *input;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    key_evt = (cui_pwd_general_key_evt_struct*)evt;
    cntx = (mmi_bootup_security_cntx_struct*)evt->user_data;

    if (key_evt->key_type != KEY_EVENT_DOWN)
    {
        return MMI_RET_OK;
    }

    MMI_TRACE(MMI_BOOTUP_TRC_GROUP, TRC_MMI_BOOTUP_SEC_KEY_ON_PASSWORD,
        key_evt->key_code, key_evt->key_type);

    switch (key_evt->key_code)
    {
        case KEY_SEND:
        case KEY_SEND1:
        case KEY_SEND2:
            input = NULL;
            
            if (key_evt->sender_id == cntx->password_screen)
            {
                input = cui_pwd_basic_get_input(cntx->password_screen);
            }
            else if (key_evt->sender_id == cntx->ubchv1_screen)
            {
                input = cui_pwd_guard_get_input_of_active(cntx->ubchv1_screen);
            }
            
            if (input != NULL && input[0] != L'\0')
            {
                mmi_bootup_sec_execute_ssc_or_make_ecc(input);
            }
            
            if (key_evt->sender_id == cntx->password_screen)
            {
                cui_pwd_basic_clear_input(cntx->password_screen);
                return MMI_RET_KEY_HANDLED;
            }
            else if (key_evt->sender_id == cntx->ubchv1_screen)
            {
                cui_pwd_guard_clear_input_of_active(cntx->ubchv1_screen);
                return MMI_RET_KEY_HANDLED;
            }
            
            break;

#ifdef __CANCEL_LOCK_POWERON__
        case KEY_END:
            if (cntx->is_displaying_sim_screen &&
                srv_bootup_veri_can_be_cancelled(cntx->sim_displaying))
            {
                mmi_bootup_waiting_screen_show(
                    cntx->grp_id,
                    mmi_get_event_based_image(MMI_EVENT_PROGRESS),
                    STR_GLOBAL_PLEASE_WAIT);
                
                srv_bootup_cancel_verification(
                    cntx->sim_displaying,
                    mmi_bootup_sec_cancel_veri_result_callback,
                    cntx);

                return MMI_RET_KEY_HANDLED;
            }
            break;
#endif

        default:
            break;
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_show_sim_password
 * DESCRIPTION
 *  Show password screen for the SIM.
 * PARAMETERS
 *  cntx                    [IN] Context
 *  sim                     [IN] Which SIM
 *  veri_type               [IN] Password type
 *  n_remaining_attempts    [IN] Number of remaining attempts
 *  str_buffer              [IN] Buffer for temporary strings
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_show_sim_password(
                mmi_bootup_security_cntx_struct *cntx,
                mmi_sim_enum sim,
                srv_bootup_verification_type_enum veri_type,
                S32 n_remaining_attempts,
                WCHAR *str_buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    WCHAR input_prompt[MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 1];
    S16 min_pwd_length;
    S16 max_pwd_length;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_bootup_sec_get_input_prompt(
        sim,
        veri_type,
        input_prompt,
        sizeof(input_prompt) / sizeof(input_prompt[0]) - 1);
    
    mmi_bootup_sec_n_attempts_to_str(
        str_buffer,
        MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
        n_remaining_attempts);

    mmi_bootup_sec_get_length_restriction(
        veri_type,
        &min_pwd_length,
        &max_pwd_length);

    cntx->password_screen = cui_pwd_basic_create_dynamic_str(
        cntx->grp_id,
        input_prompt,
        min_pwd_length,
        max_pwd_length);
    cui_pwd_basic_set_extra_msg(cntx->password_screen, str_buffer);
    cui_pwd_basic_set_action_str(cntx->password_screen, STR_GLOBAL_OK, 0);
    cui_pwd_basic_set_auxiliary(
        cntx->password_screen,
        CUI_PWD_AUXILIARY_BASIC_DEFAULT,
        MMI_TRUE,
        STR_ID_BOOTUP_SOFTKEY_EMERGENCY);
    cui_pwd_basic_set_proc(cntx->password_screen, mmi_bootup_sec_verify_chv1_or_per_proc, cntx);

    cui_pwd_basic_run(cntx->password_screen);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_verify_ubchv1_result
 * DESCRIPTION
 *  Result of verifying UBCHV1.
 * PARAMETERS
 *  user_data   [IN] mmi_bootup_security_cntx_struct
 *  result      [IN] Result
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_verify_ubchv1_result(
                void *user_data,
                const srv_bootup_verify_result_struct *result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)user_data;
    
    if (result->success)
    {
        mmi_bootup_sec_popup_and_continue(
            cntx,
            get_string(STR_GLOBAL_DONE),
            MMI_EVENT_SUCCESS,
            mmi_bootup_close_ubchv1_screen_and_continue);
    }
    else if (!result->fatal_error)
    {
        WCHAR *str_buffer;

        cntx->n_remaining_attempts = result->n_remaining_attempts;

        str_buffer = (WCHAR*)mmi_malloc(
            (MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 20) * sizeof(WCHAR));

        mmi_popup_display_simple(
            mmi_bootup_sec_get_message_of_result(result, str_buffer, MMI_BOOTUP_SEC_MAX_SIM_STR_LEN),
            MMI_EVENT_FAILURE,
            cntx->grp_id,
            cntx);

        /* Update the prompt of the number of remaining attempts */
        mmi_bootup_sec_n_attempts_to_str(
            str_buffer,
            MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
            result->n_remaining_attempts);;
        cui_pwd_guard_set_guard_extra_msg(
            cntx->ubchv1_screen,
            str_buffer);
        mmi_mfree(str_buffer);
            
        cui_pwd_guard_reset(cntx->ubchv1_screen);
    }
    else /* Unrecoverable error on SIM */
    {
        mmi_bootup_popup_sim_error_and_continue(
            cntx,
            result->sim,
            mmi_bootup_close_ubchv1_screen_and_continue);
    }

    mmi_bootup_waiting_screen_close(cntx->grp_id);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_verify_ubchv1
 * DESCRIPTION
 *  Verify UBCHV1.
 * PARAMETERS
 *  user_data   [IN] mmi_bootup_security_cntx_struct
 *  input       [IN] Input information from the change password screen
 *                   sequence.
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
static mmi_ret mmi_bootup_sec_verify_ubchv1_proc(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(evt->user_data != NULL);
    cntx = (mmi_bootup_security_cntx_struct*)evt->user_data;

    switch (evt->evt_id)
    {
        case EVT_ID_CUI_PWD_GUARD_DONE:
        {
            cui_pwd_guard_done_evt_struct *done_evt;

            done_evt = (cui_pwd_guard_done_evt_struct*)evt;
        
            mmi_bootup_waiting_screen_show(
                cntx->grp_id,
                mmi_get_event_based_image(MMI_EVENT_PROGRESS),
                STR_GLOBAL_PLEASE_WAIT);

            srv_bootup_verify(
                cntx->sim_displaying,
                done_evt->guard,
                done_evt->input,
                mmi_bootup_sec_verify_ubchv1_result,
                cntx);
                
            break;
        }
        case EVT_ID_CUI_PWD_GUARD_KEY:
            return mmi_bootup_sec_key_proc(evt);

        case EVT_ID_CUI_PWD_GUARD_AUX:
            return mmi_bootup_sec_confirm_emergency_call(evt);
            
        case EVT_ID_CUI_PWD_GUARD_PASSIVE_CLOSING:
            return CUI_PWD_DONT_CLOSE;
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_show_sim_ubchv1
 * DESCRIPTION
 *  Show screen sequence for UBCHV1.
 * PARAMETERS
 *  cntx                    [IN] Context
 *  sim                     [IN] Which SIM
 *  n_remaining_attempts    [IN] Number of remaining attempts of UBCHV1
 *  str_buffer              [IN] Buffer for temporary strings
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_show_sim_ubchv1(
                mmi_bootup_security_cntx_struct *cntx,
                mmi_sim_enum sim,
                S32 n_remaining_attempts,
                WCHAR *str_buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    WCHAR *guard_input_prompt_str;
    WCHAR *new_pwd_prompt_str;
    S16 guard_min_pwd_len, guard_max_pwd_len;
    S16 new_pwd_min_pwd_len, new_pwd_max_pwd_len;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    guard_input_prompt_str = mmi_malloc((MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 1) * 2 * sizeof(WCHAR));
    new_pwd_prompt_str = guard_input_prompt_str + MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 1;
    
    mmi_bootup_sec_get_input_prompt(
        sim,
        SRV_BOOTUP_VERI_UBCHV1,
        guard_input_prompt_str,
        MMI_BOOTUP_SEC_MAX_SIM_STR_LEN - 1);

#if (MMI_MAX_SIM_NUM >= 2)
    mmi_sim_ctrl_gen_str(
        new_pwd_prompt_str,
        MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
        STR_ID_BOOTUP_SIMX_NEW_CHV1,
        sim);
#else
    mmi_wcsncpy(new_pwd_prompt_str, get_string(STR_ID_BOOTUP_NEW_CHV1), MMI_BOOTUP_SEC_MAX_SIM_STR_LEN);
#endif

    mmi_bootup_sec_get_length_restriction(
        SRV_BOOTUP_VERI_UBCHV1,
        &(guard_min_pwd_len),
        &(guard_max_pwd_len));
    mmi_bootup_sec_get_length_restriction(
        SRV_BOOTUP_VERI_CHV1,
        &(new_pwd_min_pwd_len),
        &(new_pwd_max_pwd_len));
    mmi_bootup_sec_n_attempts_to_str(
        str_buffer,
        MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
        n_remaining_attempts);

    cntx->ubchv1_screen = cui_pwd_guard_create_dynamic_str(
        cntx->grp_id,
        guard_input_prompt_str,
        new_pwd_prompt_str,
        get_string(STR_ID_BOOTUP_CONFIRM_NEW_CHV1),
        guard_min_pwd_len,
        guard_max_pwd_len,
        new_pwd_min_pwd_len,
        new_pwd_max_pwd_len);
    cui_pwd_guard_set_guard_extra_msg(cntx->ubchv1_screen, str_buffer);
    cui_pwd_guard_set_action_str(cntx->ubchv1_screen, STR_GLOBAL_OK, 0);
    cui_pwd_guard_set_auxiliary(
        cntx->ubchv1_screen,
        CUI_PWD_AUXILIARY_GUARD_DEFAULT,
        MMI_TRUE,
        STR_ID_BOOTUP_SOFTKEY_EMERGENCY);
    cui_pwd_guard_set_diff_input_prompt(cntx->ubchv1_screen, STR_ID_BOOTUP_CHV1_NOT_MATCH);
    cui_pwd_guard_set_proc(cntx->ubchv1_screen, mmi_bootup_sec_verify_ubchv1_proc, cntx);

    cui_pwd_guard_run(cntx->ubchv1_screen);

    mmi_mfree(guard_input_prompt_str);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_do_verification_of_sim
 * DESCRIPTION
 *  Do verification of SIM.
 * PARAMETERS
 *  cntx            [IN] Context
 *  sim             [IN] Which SIM
 *  str_buffer      [IN] Buffer for temporary strings
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_do_verification_of_phone(
                mmi_bootup_security_cntx_struct *cntx,
                WCHAR *str_buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 min_pwd_len, max_pwd_len;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_BOOTUP_TRC_GROUP, TRC_MMI_BOOTUP_PHONE_VERIFICATION);

    cntx->veri_type = SRV_BOOTUP_VERI_PHONE_LOCK;
#ifdef MMI_BOOTUP_SEC_MAX_PHONE_PWD_ATTEMPTS
    cntx->n_remaining_attempts = MMI_BOOTUP_SEC_MAX_PHONE_PWD_ATTEMPTS;
#else
    cntx->n_remaining_attempts = SRV_BOOTUP_ATTEMPT_INFINITE_NUMBER;
#endif
    cntx->is_displaying_sim_screen = MMI_FALSE;
    cntx->sim_displaying = MMI_SIM_NONE;
    
    mmi_bootup_sec_n_attempts_to_str(
        str_buffer,
        MMI_BOOTUP_SEC_MAX_SIM_STR_LEN,
        cntx->n_remaining_attempts),

    mmi_bootup_sec_get_length_restriction(
        SRV_BOOTUP_VERI_PHONE_LOCK,
        &min_pwd_len,
        &max_pwd_len);

    cntx->password_screen = cui_pwd_basic_create(
        cntx->grp_id,
        STR_ID_BOOTUP_INPUT_PHONE_PASSWORD,
        min_pwd_len,
        max_pwd_len);
    cui_pwd_basic_set_extra_msg(cntx->password_screen, str_buffer);
    cui_pwd_basic_set_action_str(cntx->password_screen, STR_GLOBAL_OK, 0);
    cui_pwd_basic_set_auxiliary(
        cntx->password_screen,
        CUI_PWD_AUXILIARY_BASIC_DEFAULT,
        MMI_TRUE,
        STR_ID_BOOTUP_SOFTKEY_EMERGENCY);
    cui_pwd_basic_set_proc(cntx->password_screen, mmi_bootup_sec_verify_phone_lock_proc, cntx);
    
    cui_pwd_basic_run(cntx->password_screen);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_do_verification_of_sim
 * DESCRIPTION
 *  Do verification of SIM.
 * PARAMETERS
 *  cntx            [IN] Context
 *  sim             [IN] Which SIM
 *  str_buffer      [IN] Buffer for temporary strings
 * RETURNS
 *  MMI_TRUE if SIM verification is required
 *****************************************************************************/
static MMI_BOOL mmi_bootup_sec_do_verification_of_sim(
                    mmi_bootup_security_cntx_struct *cntx,
                    mmi_sim_enum sim,
                    WCHAR *str_buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 n_remaining_attempts;
    srv_bootup_verification_type_enum veri_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    veri_type = srv_bootup_get_verification_info(sim, &n_remaining_attempts);

    MMI_TRACE(MMI_BOOTUP_TRC_GROUP, TRC_MMI_BOOTUP_SIM_VERIFICATION, sim, veri_type);

    if (veri_type == SRV_BOOTUP_VERI_NONE)
    {
        return MMI_FALSE;
    }

    cntx->veri_type = veri_type;
    cntx->n_remaining_attempts = n_remaining_attempts;
    cntx->is_displaying_sim_screen = MMI_TRUE;
    cntx->sim_displaying = sim;

    switch (veri_type)
    {
        case SRV_BOOTUP_VERI_CHV1:
             mmi_bootup_sec_show_sim_password(
                cntx,
                sim,
                veri_type,
                n_remaining_attempts,
                str_buffer);
             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:
            mmi_bootup_sec_show_sim_password(
                cntx,
                sim,
                veri_type,
                n_remaining_attempts,
                str_buffer);
                
#ifdef __MMI_SML_UNLOCK_RETRY_TIMER__
            // TODO: Show waiting for power-off while previous counting-down
#endif

            break;
#endif /* __SIM_ME_LOCK__ */

        case SRV_BOOTUP_VERI_UBCHV1:
            mmi_bootup_sec_show_sim_ubchv1(
                cntx,
                sim,
                n_remaining_attempts,
                str_buffer);
            break;

        default:
            MMI_ASSERT(MMI_FALSE);
            break;
    }

    return MMI_TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_on_sim_status_changed
 * DESCRIPTION
 *  Handler for SIM status changed.
 *  If we are verifying SIM, displaying password screen and an fatal error
 *  occurred on SIM, we shall close the password screen and continue next
 *  valid verification.
 * PARAMETERS
 *  evt     [IN] srv_bootup_sim_status_changed_evt_struct
 * RETURNS
 *  MMI_RET_OK
 *****************************************************************************/
mmi_ret mmi_bootup_sec_on_sim_status_changed(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;
    srv_bootup_sim_status_changed_evt_struct *status_changed_evt;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(evt->evt_id == EVT_ID_SRV_BOOTUP_SIM_STATUS_CHANGED);

    status_changed_evt = (srv_bootup_sim_status_changed_evt_struct*)evt;
    cntx = &(g_mmi_bootup_security_cntx);

    if (cntx->is_displaying_sim_screen &&
        status_changed_evt->sim == cntx->sim_displaying)
    {
        if (status_changed_evt->fatal_error)
        {
#if defined(__SIM_HOT_SWAP_SUPPORT__) || defined(__SIM_RECOVERY_ENHANCEMENT__)
            if (srv_bootup_is_completed()) /* Verification request from SecSet */
            {
                srv_sim_ctrl_ua_cause_enum ua_cause;

                ua_cause = srv_sim_ctrl_get_unavailable_cause(status_changed_evt->sim);
                switch (ua_cause)
                {
                    case SRV_SIM_CTRL_UA_CAUSE_NOT_INSERTED:
                    case SRV_SIM_CTRL_UA_CAUSE_ACCESS_ERROR:
                    case SRV_SIM_CTRL_UA_CAUSE_BT_ACCESS_PROFILE:
                    case SRV_SIM_CTRL_UA_CAUSE_DISCONNECTED:
                    case SRV_SIM_CTRL_UA_CAUSE_RECOVERY:
                        /* Popup by SimCtrl app, we only need to go next step */
                        if (cntx->password_screen != MMI_BOOTUP_CUI_NULL)
                        {
                            mmi_bootup_close_password_screen_and_continue(cntx);
                        }
                        else if (cntx->ubchv1_screen != MMI_BOOTUP_CUI_NULL)
                        {
                            mmi_bootup_close_ubchv1_screen_and_continue(cntx);
                        }
                        return MMI_RET_OK;
                        
                    default:
                        /* Popup by ourselves*/
                        break;
                }
            }
#endif
        
            /* Close the verification screen and do another verification */
            if (cntx->password_screen != MMI_BOOTUP_CUI_NULL)
            {
                MMI_ASSERT(cntx->ubchv1_screen == MMI_BOOTUP_CUI_NULL);
                mmi_bootup_popup_sim_error_and_continue(
                    cntx,
                    status_changed_evt->sim,
                    mmi_bootup_close_password_screen_and_continue);
            }
            else if (cntx->ubchv1_screen != MMI_BOOTUP_CUI_NULL)
            {
                mmi_bootup_popup_sim_error_and_continue(
                    cntx,
                    status_changed_evt->sim,
                    mmi_bootup_close_ubchv1_screen_and_continue);
            }
        }
        else
        {
            srv_bootup_verify_result_struct veri_result;
            srv_bootup_verification_type_enum veri_type;
            S32 n_remaining_attempts;

            /*
             * [MAUI_02283184] Feature: verify CHV1/UBCHV1 via AT command
             * Generate a result for current verification, to trigger
             * following UI actions.
             */
            veri_type = srv_bootup_get_verification_info(
                cntx->sim_displaying,
                &n_remaining_attempts);
                
            veri_result.sim = cntx->sim_displaying;
            veri_result.type = cntx->veri_type;
            veri_result.success = MMI_FALSE;
            veri_result.fatal_error = status_changed_evt->fatal_error;
            veri_result.n_remaining_attempts = n_remaining_attempts;
            
            if (cntx->veri_type != veri_type)
            {
                if (veri_type == SRV_BOOTUP_VERI_NONE)
                {
                    veri_result.success = MMI_TRUE;
                }
                else if (veri_type == SRV_BOOTUP_VERI_UBCHV1 &&
                    cntx->veri_type == SRV_BOOTUP_VERI_CHV1)
                {
                    veri_result.success = MMI_FALSE;
                    veri_result.n_remaining_attempts = 0;
                }
                else
                {
                    /* Another veri_type is required */
                    veri_result.success = MMI_TRUE;
                }
            }
            else if (n_remaining_attempts < cntx->n_remaining_attempts)
            {
                veri_result.success = MMI_FALSE;
            }
            else if (n_remaining_attempts == cntx->n_remaining_attempts)
            {
                /* Do nothing */
                return MMI_RET_OK;
            }
            else
            {
                /* Impossible: veri_type is the same, but n_remaining_attempts increased */
                MMI_ASSERT(MMI_FALSE);
            }

            if (cntx->veri_type == SRV_BOOTUP_VERI_UBCHV1)
            {
                mmi_bootup_sec_verify_ubchv1_result(cntx, &veri_result);
            }
            else
            {
                mmi_bootup_sec_verify_result(cntx, &veri_result);
            }
        }
    }

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_next_verification
 * DESCRIPTION
 *  Check the verification status and show correspoinding password screen if
 *  necessary.
 * PARAMETERS
 *  cntx        [IN] Context
 * RETURN VALUES
 *  MMI_FRM_PROC_RESULT_NOTIFY_LATER : Verification is required and the 
 *                                     password screen has been shown.
 *  MMI_FRM_PROC_RESULT_COMPLETED : If no verification is required.
 *****************************************************************************/
static MMI_BOOL mmi_bootup_sec_next_verification(
                    mmi_bootup_security_cntx_struct *cntx)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    WCHAR *str_buffer;
    mmi_sim_enum sim;
    S32 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    str_buffer = (WCHAR*)OslMalloc((MMI_BOOTUP_SEC_MAX_SIM_STR_LEN + 20) * sizeof(WCHAR));

    cntx->veri_type = SRV_BOOTUP_VERI_NONE;
    cntx->n_remaining_attempts = 0;
    cntx->sim_displaying = MMI_SIM_NONE;
    cntx->is_displaying_sim_screen = MMI_FALSE;
    
    if (srv_bootup_phone_lock_is_required())
    {
        mmi_bootup_sec_do_verification_of_phone(
            cntx,
            str_buffer);

        OslMfree(str_buffer);
        return MMI_TRUE;
    }
#ifdef __MMI_TELEPHONY_SUPPORT__
    for (i = 0; i < SRV_SIM_CTRL_MAX_SIM_NUM; i++)
    {
        sim = mmi_frm_index_to_sim(i);

        if (!srv_bootup_veri_is_cancelled(sim))
        {
            if (mmi_bootup_sec_do_verification_of_sim(
                    cntx,
                    sim,
                    str_buffer))
            {
                OslMfree(str_buffer);
                return MMI_TRUE;
            }
        }
    }
#endif
    OslMfree(str_buffer);
    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_continue_verification
 * DESCRIPTION
 *  Continue next verification. If no verification is required, it calls
 *  mmi_frm_proc_notify_completed to continue the booting flow.
 * PARAMETERS
 *  cntx    [IN] Context
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_bootup_sec_continue_verification(
                    mmi_bootup_security_cntx_struct *cntx)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_veri_completed_evt_struct completed_evt;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!mmi_bootup_sec_next_verification(cntx))
    {
        MMI_FRM_INIT_EVENT(&completed_evt, EVT_ID_MMI_BOOTUP_VERIFICATION_COMPLETED);
        MMI_FRM_SEND_EVENT(&completed_evt, cntx->proc, cntx->user_data);
        cntx->grp_id = GRP_ID_INVALID;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_sec_do_verification
 * DESCRIPTION
 *  Do verificaitons via bootup security interface. This API is also provided
 *  for SIM reinit.
 * PARAMETERS
 *  grp_id      [IN] Group ID
 *  proc        [IN] Complete event proc. Will be invoked only if returns
 *                   MMI_TRUE.
 *  user_data   [IN] To be passed into proc
 * RETURNS
 *  MMI_TRUE if the verification screen is shown. If returns MMI_FALSE,
 *  no verification is required and the proc will never be invoked.
 *****************************************************************************/
MMI_BOOL mmi_bootup_sec_do_verification(
            mmi_id grp_id,
            mmi_proc_func proc,
            void *user_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_bootup_security_cntx_struct *cntx;
    MMI_BOOL displayed;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    cntx = &(g_mmi_bootup_security_cntx);

    /* Only one instance is available */
    MMI_EXT_ASSERT(cntx->grp_id == GRP_ID_INVALID, cntx->grp_id, grp_id, 0);
    
    cntx->grp_id = grp_id;
    cntx->password_screen = MMI_BOOTUP_CUI_NULL;
    cntx->ubchv1_screen = MMI_BOOTUP_CUI_NULL;
    cntx->is_displaying_sim_screen = MMI_FALSE;

    cntx->proc = proc;
    cntx->user_data = user_data;

    displayed = mmi_bootup_sec_next_verification(cntx);
    if (!displayed)
    {
        cntx->grp_id = GRP_ID_INVALID;
    }

    return displayed;
}



/*****************************************************************************
 * [Bootup flow - security procecure]
 *
 *
 *****************************************************************************/

typedef struct
{
    mmi_frm_proc_id_info_struct id_info;
} mmi_bootup_sec_flow_cntx_struct;

static mmi_bootup_sec_flow_cntx_struct g_mmi_bootup_sec_flow_cntx;


static mmi_ret mmi_bootup_flow_security_on_completed(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_frm_proc_notify_completed(g_mmi_bootup_sec_flow_cntx.id_info);

    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_bootup_flow_security
 * DESCRIPTION
 *  Enter security check phase of bootup.
 * PARAMETERS
 *  arg         [IN] Unused
 *  id_info     [IN] To be passed into mmi_frm_proc_notify_completed().
 * RETURNS
 *  mmi_frm_proc_result_enum
 *****************************************************************************/
mmi_frm_proc_result_enum mmi_bootup_flow_security(
                            void *arg,
                            const mmi_frm_proc_id_info_struct id_info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL displayed;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_BOOTUP_TRC_GROUP, TRC_MMI_BOOTUP_FLOW_SECURITY);
    
    g_mmi_bootup_sec_flow_cntx.id_info = id_info;

    displayed = mmi_bootup_sec_do_verification(
        GRP_ID_BOOTUP,
        mmi_bootup_flow_security_on_completed,
        &g_mmi_bootup_sec_flow_cntx);

    return (displayed ?
                MMI_FRM_PROC_RESULT_NOTIFY_LATER :
                MMI_FRM_PROC_RESULT_COMPLETED);
}