Add cust info partition config.

Change-Id: Ic376d338ef4ff8b3cb8943f25f0fa87ff4be93b7
diff --git a/mbtk/mbtk_ril/src/mbtk_info_server.c b/mbtk/mbtk_ril/src/mbtk_info_server.c
index 3fc1a86..6a3454d 100755
--- a/mbtk/mbtk_ril/src/mbtk_info_server.c
+++ b/mbtk/mbtk_ril/src/mbtk_info_server.c
@@ -20,6 +20,7 @@
 #include "mbtk_ifc.h"
 #include "info_data.h"
 #include "mbtk_led.h"
+#include "cust_info.h"
 
 static int sock_listen_fd = -1;
 static int epoll_fd = -1;
@@ -318,11 +319,8 @@
     band_support.gsm_band = (uint16)79;
     band_support.umts_band = (uint16)147;
     band_support.tdlte_band = (uint32)482;
-#if MBTK_LTE_B28_SUPPORT
     band_support.fddlte_band = (uint32)134742231;
-#else
-    band_support.fddlte_band = (uint32)524503;
-#endif
+    //band_support.fddlte_band = (uint32)524503;
     band_support.net_pref = (uint8)0;
 #else
     ATResponse *response = NULL;
@@ -2493,15 +2491,15 @@
         {
 
         }
-        else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE ???§³??
+        else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE
         {
 
         }
-        else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE ??§³??
+        else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE
         {
 
         }
-        else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE RAT§³?????
+        else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE
         {
 
         }
@@ -2515,11 +2513,11 @@
         {
 
         }
-        else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA???§³??
+        else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA
         {
 
         }
-        else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA RAT§³?????
+        else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA
         {
 
         }
@@ -2533,7 +2531,7 @@
         {
 
         }
-        else if(strStartsWith(line, "+EEMGINFOPS:")) // PS???
+        else if(strStartsWith(line, "+EEMGINFOPS:")) // PS
         {
 
         }
@@ -6301,17 +6299,123 @@
 }
 
 /*
+root@OpenWrt:/usrdata# cat /proc/mtd
+dev:    size   erasesize  name
+mtd0: 00040000 00020000 "bootloader"
+mtd1: 00020000 00020000 "cp_reliabledata"
+mtd2: 00020000 00020000 "ap_reliabledata"
+mtd3: 00020000 00020000 "cp_reliabledata_backup"
+mtd4: 00020000 00020000 "ap_reliabledata_backup"
+mtd5: 00020000 00020000 "mep-ota"
+mtd6: 00020000 00020000 "mep-ota_backup"
+mtd7: 00040000 00020000 "dtim"
+mtd8: 00f40000 00020000 "cpimage"
+mtd9: 000c0000 00020000 "u-boot"
+mtd10: 00500000 00020000 "kernel"
+mtd11: 00100000 00020000 "asr_flag"
+mtd12: 01400000 00020000 "rootfs"
+mtd13: 01400000 00020000 "oem_data"
+mtd14: 01e00000 00020000 "OTA"
+mtd15: 01400000 00020000 "rootfs_data"
+mtd16: 081a0000 00020000 "user_data"
+mtd17: 00d20000 00020000 "MRVL_BBM"
+*/
+static int partition_name_2_dev(const char *name, char *dev) {
+    if(name == NULL || dev == NULL) {
+        LOGE("ARG error.");
+		return -1;
+    }
+
+    int fp = fopen("/proc/mtd", "r");
+	if (fp == NULL) {
+		LOGE("Open MTD failed!");
+		return -1;
+	}
+
+    char buf[1024];
+	while (fgets(buf, 1024, fp) != NULL) {
+		if(strstr(buf, name)) {
+            int index = atoi(buf + 3);
+            sprintf(dev, "/dev/mtdblock%d", index);
+            LOGD("%s -> %s", name, dev);
+            return 0;
+		}
+	}
+
+    return -1;
+}
+
+static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
+{
+    int fd = 0, ret = 0;
+    unsigned int step = 0, offset = 0, flaglen = 0;
+    flaglen = sizeof(mbtk_cust_info_t);
+
+    char mtd_path[50] = {0};
+    if(partition_name_2_dev(name, mtd_path)) {
+        LOGE("partition_name_2_dev() failed!");
+        return -1;
+    }
+
+    fd = open(mtd_path, O_RDONLY);
+    if (fd < 0) {
+        LOGE("Fatal error: can't open cust info %s\n", mtd_path);
+        return -1;
+    }
+
+    if (read(fd, cust_info, flaglen) < 0)
+        goto error;
+    if (cust_info->header != CUST_INFO_HEADER) {
+        LOGE("Cust info partition error.");
+        goto error;
+    } else {
+        if(cust_info->band_type == 1) { // CN
+            LOGD("Band : CN");
+        } else if(cust_info->band_type == 2) { // EU
+            LOGD("Band : EU");
+        } else {
+            LOGE("Unknown band type:%d", cust_info->band_type);
+            goto error;
+        }
+    }
+    close(fd);
+    return 0;
+error:
+    close(fd);
+    return -1;
+}
+
+/*
 AT*BAND=15,78,147,482,134742231
 
 OK
 */
-static int lte_b28_set()
+static int band_config()
 {
     int err = -1;
     ATResponse *p_response = NULL;
-    char *line;
-    char *ipv4 = NULL, *ipv6 = NULL;
-    err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
+
+    mbtk_cust_info_t cust_info;
+    memset(&cust_info, 0x0, sizeof(mbtk_cust_info_t));
+    if(!custom_partition_read("cust_info", &cust_info)) {
+        if(cust_info.band_type == 1) { // CN
+            // GSM : B3/B8
+            // WCDMA : B1/B5/B8
+            // TDD-LTE : B34/B38/B39/B40/B41
+            // FDD-LTE : B1/B3/B5/B8
+            err = at_send_command("AT*BAND=15,11,145,482,149", &p_response);
+        } else if(cust_info.band_type == 2) { // EU
+            // GSM : B3/B8
+            // WCDMA : B1/B5/B8
+            // TDD-LTE : B38/B40/B41
+            // FDD-LTE : B1/B3/B5/B7/B8/B20/B28
+            err = at_send_command("AT*BAND=15,11,145,416,134742229", &p_response);
+        } else {
+            err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
+        }
+    } else {
+        err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
+    }
     if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
     {
         LOGE("*BAND exec error.");
@@ -6319,7 +6423,7 @@
         goto error;
     }
 
-    LOGD("Set B28 Success.");
+    LOGD("Set Band Success.");
     err = 0;
 
 error:
@@ -6502,18 +6606,16 @@
         LOGE("SIM NOT READY!");
     }
 
-    // Set B28
+    // Set Band
     // AT*BAND=15,78,147,482,134742231
-#if MBTK_LTE_B28_SUPPORT
-    char b28_config[10];
-    memset(b28_config, 0, 10);
-    property_get("persist.mbtk.b28_config", b28_config, "0");
-    if(atoi(b28_config) == 0) {
-        if(!lte_b28_set()) { // Set B28 success.
-            property_set("persist.mbtk.b28_config", "1");
+    char buff[10];
+    memset(buff, 0, 10);
+    property_get("persist.mbtk.band_config", buff, "0");
+    if(atoi(buff) == 0) {
+        if(!band_config()) { // Set band success.
+            property_set("persist.mbtk.band_config", "1");
         }
     }
-#endif
 }
 
 int mbtk_info_server_start()