blob: 1ab4e01eeff53dbf4d6a660ea1e1e93e3fa857d5 [file] [log] [blame]
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <pthread.h>
#include "mtk_device_wrap.h"
#include "lynq_nand.h"
#if 1
#define NAND_LOG_PATCH "/dev/mtd19"
#else
#define NAND_LOG_PATCH "/dev/mtd19/lynq_log.txt"
#endif
#define NAND_PRIMITIVE_OFFSET 2*1024
#define NAND_TOTAL_SIZE 20*1024*1024
#define NAND_CURRENT_LEN 2*1024
#define NAND_OLD_CURRENT_LEN 0
static int lynq_nand_offset_global = 0;
static pthread_mutex_t nand_mutex = PTHREAD_MUTEX_INITIALIZER;
char nand_count = 0;
void lynq_store_nand_offset_data(char *store_nand_offset_data);
int lynq_read_nand_offset_data(char *read_nand_offset_data);
int lynq_nand_store_log_message(char *log_message,size_t log_message_len)
{
pthread_mutex_lock(&nand_mutex);
int fd;
int err = 0;
char nand_current_buf[NAND_CURRENT_LEN] = {0};
unsigned int nand_page = 0;
int nand_offset_count = 0;
char nand_offset_buf[8] = {0};
char read_nand_buf[8] = {0};
if(log_message_len == 0)
{
return -1;
}
fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDWR);
if(fd < 0)
{
perror("open nand log message error1!!!!");
return fd;
}
if(lynq_read_nand_offset_data(nand_offset_buf) == 0)
{
nand_offset_count = atoi(nand_offset_buf);
lynq_nand_offset_global = nand_offset_count;
}
((log_message_len % 2048) == 0 ) ? (nand_page = log_message_len / 2048) : (nand_page = (log_message_len / 2048)+1);
//printf("nand_page:%d\n",nand_page);
while(1)
{
err = mtk_device_wrap_seek(fd, lynq_nand_offset_global, SEEK_SET);
if(err == -1)
{
perror("seek nand log message error8!!!!");
return err;
}
mtk_device_wrap_read(fd, read_nand_buf, 8);
if(((read_nand_buf[0] == 0xff) && (read_nand_buf[1] == 0xff) && (read_nand_buf[2] == 0xff) &&(read_nand_buf[3] == 0xff)) ||\
((lynq_nand_offset_global % (128 *1024) ) == 0))
{
for(int i = 0; i < nand_page; i++)
{
memset(nand_current_buf,0,NAND_CURRENT_LEN);
err = mtk_device_wrap_seek(fd, lynq_nand_offset_global, SEEK_SET);
if(err == -1)
{
perror("seek nand log message error8!!!!");
return err;
}
if((i+1) == nand_page)
{
memcpy(nand_current_buf,&log_message[i*NAND_CURRENT_LEN],(log_message_len-(i*2048)));
// printf("(i+1) == nand_page---%d\n",(log_message_len-(i*2048)));
}
else
{
memcpy(nand_current_buf,&log_message[i*NAND_CURRENT_LEN],NAND_CURRENT_LEN);
printf("(i+1) != nand_page\n");
}
err = mtk_device_wrap_write(fd, nand_current_buf, NAND_CURRENT_LEN);
if(err == -1)
{
perror("write nand log message error9!!!!");
return err;
}
lynq_nand_offset_global += NAND_CURRENT_LEN;
if(lynq_nand_offset_global > NAND_TOTAL_SIZE)
{
lynq_nand_offset_global = 0;
}
}
break;
}
//printf("lynq_nand_offset_global:%d---nand_offset_count:%d\n",lynq_nand_offset_global,nand_offset_count);
lynq_nand_offset_global += NAND_CURRENT_LEN;
if(lynq_nand_offset_global > NAND_TOTAL_SIZE)
{
lynq_nand_offset_global = 0;
}
}
if(nand_count >= 64)
{
nand_count = 0;
sprintf(nand_offset_buf,"%d",lynq_nand_offset_global);
lynq_store_nand_offset_data(nand_offset_buf);
}
err = mtk_device_wrap_close(fd);
pthread_mutex_unlock(&nand_mutex);
return 0;
}
int lynq_nand_get_log_message(char *log_message,size_t log_message_len,off_t offset)
{
int fd;
int err = 0;
pthread_mutex_lock(&nand_mutex);
unsigned int lynq_nand_offset_part = 0;
fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDONLY);
if(fd < 0)
{
perror("open nand log message error!!!!");
return fd;
}
lynq_nand_offset_part = NAND_PRIMITIVE_OFFSET + offset;
//printf("lynq_nand_offset_part 13:%d\n",lynq_nand_offset_part);
err = mtk_device_wrap_seek(fd, lynq_nand_offset_part, SEEK_SET);
//printf("lynq_nand_offset_part 14:%d----nand seek 4:%d\n",lynq_nand_offset_part,err);
if(err == -1)
{
perror("seek nand log message error 15!!!!");
return err;
}
mtk_device_wrap_read(fd, log_message, log_message_len);
err = mtk_device_wrap_close(fd);
pthread_mutex_unlock(&nand_mutex);
return err;
}
int lynq_nand_read_data_test(void)
{
int fd;
FILE *file_fd = NULL;
int err;
char log_data_buf[6*1024] = {0};
char log_nand_buf[2048] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99};
static unsigned int nand_offset_test = 0;
char *filePath = "/usr/local/lnand_log.txt";
system("touch /usr//local/lnand_log.txt");
system("chmod 777 /usr//local/lnand_log.txt");
fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDWR);
if(fd < 0)
{
perror("open nand log message error!!!!");
return fd;
}
file_fd = fopen(filePath,"rt+");
if(NULL == file_fd)
{
perror("file_fd");
return -1;
}
for(int i = 0 ;i < 100; i++)
{
memset(log_data_buf,0,6*1024);
err = mtk_device_wrap_seek(fd, nand_offset_test, SEEK_SET);
if(err == -1)
{
perror("seek nand log message error!!!!");
return err;
}
fseek(file_fd,nand_offset_test,SEEK_SET);
mtk_device_wrap_read(fd, log_data_buf, 6*1024);
nand_offset_test += (6*1024);
fwrite(&log_data_buf,sizeof(log_data_buf), 1, file_fd);
}
fclose(file_fd);
mtk_device_wrap_close(fd);
}
void lynq_store_nand_offset_data(char *store_nand_offset_data)
{
char *log_level_writefile = "persist.store_nand_data";
property_set(log_level_writefile,(char *)store_nand_offset_data);
return ;
}
int lynq_read_nand_offset_data(char *read_nand_offset_data)
{
char *log_level_readfile = "persist.store_nand_data";
if(property_get(log_level_readfile,(char *)read_nand_offset_data,NULL) > 0)
{
// printf("read log level deploy success!!!\n");
return 0;
}
else
{
// printf("get file error!!!\n");
return -1;
}
}