blob: 86744a6bbb1bebe5f3803b59db57b9bd4cdabc38 [file] [log] [blame]
/*
* mbtk_device_info.c
*
* MBTK device partition information utils.
*
*/
/******************************************************************************
EDIT HISTORY FOR FILE
WHEN WHO WHAT,WHERE,WHY
-------- -------- -------------------------------------------------------
2024/2/27 LiuBin Initial version
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "mbtk_device.h"
#include "mbtk_log.h"
#include "mbtk_str.h"
#include "mbtk_mtd.h"
/*
* mbtk_device_info_basic_t
* mbtk_device_info_fota_t
*/
int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
{
int fd, len, i;
mbtk_device_info_header_t info_header;
memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
switch(item_type) {
case MBTK_DEVICE_INFO_ITEM_BASIC:
{
if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
return -1;
}
break;
}
case MBTK_DEVICE_INFO_ITEM_FOTA:
{
if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
return -1;
}
break;
}
case MBTK_DEVICE_INFO_ITEM_MODEM:
{
if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
return -1;
}
break;
}
case MBTK_DEVICE_INFO_ITEM_LOG:
{
if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
return -1;
}
break;
}
default:
{
LOGE("Item type[%d] error.", item_type);
return -1;
}
}
mbtk_partition_info_t *partition_info = mbtk_partition_get();
if(partition_info == NULL) {
LOGE("mbtk_partition_get() fail.");
return -1;
}
i = 0;
char dev[32] = {0};
while(i < MBTK_PARTITION_NUM_MAX) {
if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
LOGD("%s -> %s", strcmp(partition_info[i].name, dev);
break;
}
i++;
}
if(strlen(dev) == 0) {
LOGE("DEV is null.");
return -1;
}
fd = open(dev, O_RDONLY);
if (fd < 0) {
LOGE("Fatal error: can't open device_info %s\n", dev);
return -1;
}
len = read(fd, &info_header, sizeof(mbtk_device_info_header_t));
if (len != sizeof(mbtk_device_info_header_t)) {
LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t));
close(fd);
goto fail;
}
if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) {
LOGE("TAG error : %08x", info_header.tag);
goto fail;
}
if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) {
LOGE("Version error : %d", info_header.version);
goto fail;
}
LOGD("Item count:%d", info_header.item_count);
if(info_header.item_header[item_type].addr == 0) {
LOGE("No found item : %d", item_type);
goto fail;
}
lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
if (read(fd, item_ptr, item_size) != item_size) {
LOGE("Read fail:%d", errno);
goto fail;
}
close(fd);
return 0;
fail:
close(fd);
return -1;
}