Add ota_update.

Change-Id: I42468c44555ac1ddcdc229664472c9dfc84d6bb4
diff --git a/mbtk/Makefile b/mbtk/Makefile
index 95ab04c..232ef54 100755
--- a/mbtk/Makefile
+++ b/mbtk/Makefile
@@ -9,7 +9,7 @@
 DIRS += libql_lib liblynq_lib
 
 # Build bin file.
-DIRS += device_info_generate mbtk_adbd mbtk_rild mbtk_logd mbtk_utils
+DIRS += mbtk_adbd mbtk_rild mbtk_logd mbtk_utils mbtk_utils_linux
 
 # Build test file.
 DIRS += test
diff --git a/mbtk/device_info_generate/Makefile b/mbtk/device_info_generate/Makefile
deleted file mode 100755
index e6d4a03..0000000
--- a/mbtk/device_info_generate/Makefile
+++ /dev/null
@@ -1,33 +0,0 @@
-BUILD_ROOT = $(shell pwd)/..
-include $(BUILD_ROOT)/Make.defines
-
-LOCAL_PATH=$(BUILD_ROOT)/device_info_generate
-
-INC_DIR += \
-		-I$(LOCAL_PATH)
-
-CC=gcc
-
-MY_FILES_PATH:=$(LOCAL_PATH)
-
-LOCAL_SRC_FILES = $(wildcard *.c)
-OBJS = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(LOCAL_SRC_FILES)))
-$(info OBJS = $(OBJS))
-
-dtarget := $(OUT_DIR)/bin/device_info_generate
-
-all: $(dtarget)
-
-$(dtarget): $(OBJS)
-	@echo "  BIN     $@"
-	$(CC) $(OBJS) -o $@
-
-%.o:%.c
-	$(CC) $(INC_DIR) -c $< -o $@
-
-%.o:%.cpp
-	$(CC) $(INC_DIR) -c $< -o $@
-
-clean:
-	rm -f $(OBJS) $(dtarget)
-
diff --git a/mbtk/mbtk_utils_linux/Makefile b/mbtk/mbtk_utils_linux/Makefile
new file mode 100755
index 0000000..61d3770
--- /dev/null
+++ b/mbtk/mbtk_utils_linux/Makefile
@@ -0,0 +1,32 @@
+BUILD_ROOT = $(shell pwd)/..
+include $(BUILD_ROOT)/Make.defines
+
+
+
+INC_DIR += \
+		-I$(LOCAL_PATH)
+
+CC=gcc
+
+LOCAL_SRC_FILES = $(wildcard *.c) $(wildcard *.cpp)
+
+$(info LOCAL_SRC_FILES = $(LOCAL_SRC_FILES))
+
+OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(LOCAL_SRC_FILES)))
+BINS = $(patsubst %.o,%,$(OBJS))
+
+all: $(BINS)
+
+$(BINS):$(OBJS)
+	@echo "  BIN     $@"
+	$(CC) $@.o -o $(OUT_DIR)/bin/$@
+
+%.o:%.c
+	$(CC) $(INC_DIR) -c $< -o $@
+
+%.o:%.cpp
+	$(CC) $(INC_DIR) -c $< -o $@
+
+clean:
+	rm -f $(OBJS) $(BINS)
+
diff --git a/mbtk/device_info_generate/main.c b/mbtk/mbtk_utils_linux/device_info_generate.c
similarity index 100%
rename from mbtk/device_info_generate/main.c
rename to mbtk/mbtk_utils_linux/device_info_generate.c
diff --git a/mbtk/mbtk_utils_linux/ota_update.c b/mbtk/mbtk_utils_linux/ota_update.c
new file mode 100755
index 0000000..50ef62a
--- /dev/null
+++ b/mbtk/mbtk_utils_linux/ota_update.c
@@ -0,0 +1,126 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "mbtk_device.h"
+
+/*
+* revision_out start from 0x1000.
+*/
+#define REVISION_OUT_ADDR 0x1000
+#define DEV_INFO_FILE_NAME "dev_info.bin"
+
+static void help()
+{
+    printf("ota_update -f [ota_bin] -v [new_revision_dir]\n");
+}
+
+/*
+*
+* ota_update -f [ota_bin] -v [new_revision_dir]
+*
+*/
+int main(int argc, char *argv[])
+{
+    int ch;
+    char ota_bin[128] = {0};
+    char dev_info_file[128] = {0};
+    while((ch = getopt(argc, argv, "f:v:"))!= -1)
+    {
+        switch(ch)
+        {
+            case 'f':
+                if(strlen(optarg) > 0)
+                    memcpy(ota_bin, optarg, strlen(optarg));
+                break;
+            case 'v':
+                if(strlen(optarg) > 0)
+                    memcpy(dev_info_file, optarg, strlen(optarg));
+                break;
+            default:
+                help();
+                return -1;
+        }
+    }
+    if(strlen(ota_bin) == 0 || strlen(dev_info_file) == 0)
+    {
+        help();
+        return -1;
+    }
+
+    printf("Ota Bin:%s, Revision Dir:%s\n", ota_bin, dev_info_file);
+
+    sprintf(dev_info_file + strlen(dev_info_file), "/%s", DEV_INFO_FILE_NAME);
+
+    if(access(ota_bin, F_OK))
+    {
+        printf("%s not exist.", ota_bin);
+        return -1;
+    }
+
+    if(access(dev_info_file, F_OK))
+    {
+        printf("%s not exist.", dev_info_file);
+        return -1;
+    }
+
+    int fd = open(dev_info_file, O_RDONLY);
+    if(fd < 0)
+    {
+        printf("Open(%s) fail:%d\n", dev_info_file, errno);
+        return -1;
+    }
+    mbtk_device_info_header_t info_header;
+    memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
+    if(read(fd, &info_header, sizeof(mbtk_device_info_header_t)) != sizeof(mbtk_device_info_header_t)) {
+        printf("Read mbtk_device_info_header_t fail:%d\n", errno);
+        goto fail;
+    }
+
+    if(lseek(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, SEEK_SET) < 0)
+    {
+        printf("seek failed\n");
+        goto fail;
+    }
+
+    mbtk_device_info_basic_t info_basic;
+    memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
+    if(read(fd, &info_basic, sizeof(mbtk_device_info_basic_t)) != sizeof(mbtk_device_info_basic_t)) {
+        printf("Read mbtk_device_info_basic_t fail:%d\n", errno);
+        goto fail;
+    }
+
+    printf("New Revision:%s\n", info_basic.revision_out);
+
+    close(fd);
+    fd = open(ota_bin, O_WRONLY, 0644);
+    if(fd < 0)
+    {
+        printf("Open(%s) fail:%d\n", ota_bin, errno);
+        return -1;
+    }
+
+    if(lseek(fd, REVISION_OUT_ADDR, SEEK_SET) < 0)
+    {
+        printf("seek failed\n");
+        goto fail;
+    }
+
+    if(write(fd, info_basic.revision_out, strlen(info_basic.revision_out)) != strlen(info_basic.revision_out)) {
+        printf("Write revision failed\n");
+        goto fail;
+    }
+
+    printf("%s revision update to:%s\n", ota_bin, info_basic.revision_out);
+    close(fd);
+    return 0;
+fail:
+    close(fd);
+    return -1;
+}
+