Add mbtk_reboot
Change-Id: I16682b505016b15c9f69819bd100da7e7fd75ae4
diff --git a/mbtk/libmbtk_lib/src/mbtk_device_info.c b/mbtk/libmbtk_lib/src/mbtk_device_info.c
index 4aa87dc..31d9523 100755
--- a/mbtk/libmbtk_lib/src/mbtk_device_info.c
+++ b/mbtk/libmbtk_lib/src/mbtk_device_info.c
@@ -20,7 +20,10 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <mtd/mtd-user.h>
+#include "mbtk_type.h"
#include "mbtk_device.h"
#include "mbtk_log.h"
#include "mbtk_str.h"
@@ -143,3 +146,117 @@
return -1;
}
+int mbtk_dev_info_write(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
+{
+ if(item_ptr == NULL) {
+ LOGE("ARG error.");
+ return -1;
+ }
+
+ switch(item_type) {
+ case MBTK_DEVICE_INFO_ITEM_BASIC:
+ {
+ if(item_size != sizeof(mbtk_device_info_basic_t)) {
+ LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
+ return -1;
+ }
+ break;
+ }
+ case MBTK_DEVICE_INFO_ITEM_FOTA:
+ {
+ if(item_size != sizeof(mbtk_device_info_fota_t)) {
+ LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
+ return -1;
+ }
+ break;
+ }
+ case MBTK_DEVICE_INFO_ITEM_MODEM:
+ {
+ if(item_size != sizeof(mbtk_device_info_modem_t)) {
+ LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
+ return -1;
+ }
+ break;
+ }
+ case MBTK_DEVICE_INFO_ITEM_LOG:
+ {
+ if(item_size != sizeof(mbtk_device_info_log_t)) {
+ LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
+ return -1;
+ }
+ break;
+ }
+ default:
+ {
+ LOGE("Item type[%d] error.\n\r", item_type);
+ return -1;
+ }
+ }
+
+ mbtk_partition_info_t info;
+ memset(&info, 0x0, sizeof(mbtk_partition_info_t));
+ if(mbtk_partition_get_by_name("device_info", &info)) {
+ LOGE("mbtk_partition_get_by_name() fail.");
+ return -1;
+ }
+
+ LOGD("device_info name : %s, dev : %s, addr : %08x, size : %08x, erase_size : %08x", info.name,
+ info.dev, info.partition_start, info.partition_size, info.erase_size);
+
+ if(info.erase_size <= 0 || info.partition_size <= 0) {
+ LOGE("partition info error.");
+ return -1;
+ }
+
+ int fd = open(info.dev, O_RDWR);
+ if (fd < 0) {
+ LOGE("Fatal error: can't open device_info %s\n", info.dev);
+ return -1;
+ }
+
+ char *mtd_buff = (char*)malloc(info.erase_size);
+ if(mtd_buff == NULL) {
+ LOGE("malloc() failed\n");
+ return -1;
+ }
+ memset(mtd_buff, 0xFF, info.erase_size);
+ int len = read(fd, mtd_buff, info.erase_size);
+ if (len != info.erase_size) {
+ LOGE("Fatal error: read %d[%d] bytes(expect %d)\n", len, errno, info.erase_size);
+ goto fail;
+ }
+
+ struct erase_info_user mtdEraseInfo;
+ if (lseek(fd, 0, SEEK_SET) < 0)
+ {
+ LOGE("seek failed\n");
+ return -1;
+ }
+
+ mtdEraseInfo.length = info.partition_size;
+ mtdEraseInfo.start = 0;
+ ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
+ ioctl(fd, MEMERASE, &mtdEraseInfo);
+
+ mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff;
+ memcpy(mtd_buff + info_header->item_header[item_type].addr, item_ptr, item_size);
+
+ lseek(fd, 0, SEEK_SET);
+ if (write(fd, mtd_buff, info.erase_size) != info.erase_size) {
+ LOGE("Write fail:%d", errno);
+ goto fail;
+ }
+
+ if(mtd_buff) {
+ free(mtd_buff);
+ }
+
+ close(fd);
+ return 0;
+
+fail:
+ close(fd);
+ return -1;
+}
+
+
diff --git a/mbtk/libmbtk_lib/src/mbtk_mtd.c b/mbtk/libmbtk_lib/src/mbtk_mtd.c
index 4f1c2be..6129bc1 100755
--- a/mbtk/libmbtk_lib/src/mbtk_mtd.c
+++ b/mbtk/libmbtk_lib/src/mbtk_mtd.c
@@ -42,12 +42,14 @@
int index = 0;
char size_str[16];
char name[32];
+ char erase_size_str[16];
memset(buff, 0x0, 64);
while(fgets(buff, 64, fp)) {
if(str_startwith(buff, "mtd")) {
memset(size_str, 0x0, 16);
+ memset(erase_size_str, 0x0, sizeof(erase_size_str));
memset(name, 0x0, 32);
- if(3 == sscanf(buff, "%s %s %*s %s", partition_list[index].dev, size_str, name)) {
+ if(4 == sscanf(buff, "%s %s %s %s", partition_list[index].dev, size_str, erase_size_str, name)) {
if(name[0] == '\"' && name[strlen(name) - 1] == '\"') {
memcpy(partition_list[index].name, name + 1, strlen(name) - 2); // No copy ""
} else {
@@ -60,6 +62,7 @@
}
partition_list[index].partition_size = (uint32)strtoul(size_str, NULL, 16);
+ partition_list[index].erase_size = (uint32)strtoul(erase_size_str, NULL, 16);
if(index > 0) {
partition_list[index].partition_start = partition_list[index - 1].partition_start + partition_list[index - 1].partition_size;
}
@@ -87,4 +90,51 @@
return partition_list;
}
+int mbtk_partition_get_by_name(char *partition_name, mbtk_partition_info_t *info)
+{
+ FILE *fp = fopen("/proc/mtd", "r");
+ if(fp == NULL) {
+ LOGE("fopen(/proc/mtd) fail:%d", errno);
+ return -1;
+ }
+
+ char buff[64];
+ char size_str[16];
+ char erase_size_str[16];
+ char name[32];
+ memset(buff, 0x0, sizeof(buff));
+ while(fgets(buff, sizeof(buff), fp)) {
+ if(strstr(buff, partition_name)) {
+ memset(size_str, 0x0, sizeof(size_str));
+ memset(erase_size_str, 0x0, sizeof(erase_size_str));
+ memset(name, 0x0, sizeof(name));
+ memcpy(info->dev, "/dev/", 5);
+ if(4 == sscanf(buff, "%s %s %s %s", info->dev + 5, size_str, erase_size_str, name)) {
+ if(name[0] == '\"' && name[strlen(name) - 1] == '\"') {
+ memcpy(info->name, name + 1, strlen(name) - 2); // No copy ""
+ } else {
+ LOGE("partition(%s) name error.", buff);
+ return -1;
+ }
+
+ if(info->dev[strlen(info->dev) - 1] == ':') {
+ info->dev[strlen(info->dev) - 1] = '\0';
+ }
+
+ info->partition_size = (uint32)strtoul(size_str, NULL, 16);
+ info->erase_size = (uint32)strtoul(erase_size_str, NULL, 16);
+ //info->partition_start += info->partition_size;
+ break;
+ } else {
+ LOGE("sscanf(%s) fail:%d", buff, errno);
+ return -1;
+ }
+ }
+ memset(buff, 0x0, sizeof(buff));
+ }
+ fclose(fp);
+
+ return 0;
+}
+