CRC32使用實例,字符串操作,SD卡讀
/* monitor the /mnt/external_sd/rk_lcd_parameters. */
/* if the parameters has been changed, and then update the lcdparamers. */
/* the parameters will work after reboot. */
/* [email protected] */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <cutils/properties.h>
#include <sys/utsname.h>
#include <cutils/list.h>
#include <cutils/log.h>
#include <cutils/sockets.h>
#include <sys/reboot.h>
#include <cutils/iosched_policy.h>
#define LOG_TAG "LcdParamService"
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef unsigned char uint8;
#define LCD_PARAMETE_FILE_PATH "/mnt/external_sd/rk_lcd_parameters"
#define RKNAND_GET_VENDOR_SECTOR0 _IOW('v', 16, unsigned int)
#define RKNAND_STORE_VENDOR_SECTOR0 _IOW('v', 17, unsigned int)
#define RKNAND_SYS_STORGAE_DATA_LEN 512
#define VENDOR_SECTOR_OP_TAG 0x444E4556 // "VEND"
#define POLY 0xEDB88320L //CRC stand
#define CONFIG_NUM 24
char *key[CONFIG_NUM] =
{
"screen_type",
"screen_lvds_format",
"screen_face",
"screen_x_res",
"screen_y_res",
"screen_width",
"screen_height",
"screen_lcdc_aclk",
"screen_pixclock",
"screen_left_margin",
"screen_right_margin",
"screen_hsync_len",
"screen_upper_margin",
"screen_lower_margin",
"screen_vsync_len",
"screen_pin_hsync",
"screen_pin_vsync",
"screen_pin_den",
"screen_pin_dclk",
"screen_swap_rb",
"screen_swap_rg",
"screen_swap_gb",
"screen_swap_delta",
"screen_swap_dumy",
};
typedef struct tagRKNAND_SYS_STORGAE
{
unsigned long tag;
unsigned long len;
unsigned char data[RKNAND_SYS_STORGAE_DATA_LEN];
}RKNAND_SYS_STORGAE;
static uint32 crc32_tab[256];
static uint32 nand_crc = 0;
void rknand_print_hex_data(uint8 *s,uint32 * buf,uint32 len)
{
uint32 i,j,count;
SLOGE("%s",s);
for(i=0;i<len;i+=4)
{
SLOGE("%x %x %x %x",buf[i],buf[i+1],buf[i+2],buf[i+3]);
}
}
void init_crc32_tab(void)
{
int i = 0;
int j = 0;
uint32 crc = 0;
for(i = 0; i < 256; i++)
{
crc = (uint32)i;
for(j = 0; j < 8; j++)
{
if(crc & 0x00000001L)
crc = (crc >> 1) ^ POLY;
else
crc = crc >> 1;
}
crc32_tab[i] = crc;
}
}
uint32 get_crc32(uint32 crc_init, uint8 *crc_buf, uint32 buf_size)
{
uint32 crc = crc_init ^ 0xffffffff;
init_crc32_tab();
while(buf_size--)
crc = (crc >> 8) ^ crc32_tab[(crc & 0xff) ^ *crc_buf++];
return crc ^ 0xfffffff;
}
uint32 getfile_crc(FILE *fp)
{
uint32 size = 4 *1024;
uint8 crc_buf[size];
uint32 readln = 0;
uint crc = 0;
while((readln = fread(crc_buf, sizeof(uint8), size, fp)) > 0)
crc = get_crc32(crc, crc_buf, readln);
return crc;
}
/*
*去除字符串左端空格
*/
char *strtriml(char *pstr)
{
int i = 0,j;
j = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i <= j))
i++;
if (0<i)
strcpy(pstr, &pstr[i]);
return pstr;
}
/*
*去除字符串右端空格
*/
char *strtrimr(char *pstr)
{
int i;
i = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i >= 0))
pstr[i--] = '\0';
return pstr;
}
/*
*去除字符串兩端空格
*/
char *strtrim(char *pstr)
{
char *p;
p = strtrimr(pstr);
return strtriml(p);
}
char *strdelchr(char *pstr, int chr)
{
int i = 0;
int l = 0;
int ll = 0;
ll = l = strlen(pstr);
while(i < l)
{
if(pstr[i] == chr)
{
memmove((pstr + i), (pstr + i + 1), (ll - i -1));
pstr[ll - 1] = '\0';
ll--;
}
i++;
}
/* for(i = 0; i < l; i++) */
/* { */
/* if(pstr[i] == chr) */
/* { */
/* memmove((pstr + i), (pstr + i + 1), (l - i -1)); */
/* pstr[l - 1] = '\0'; */
/* break; */
/* } */
/* } */
return pstr;
}
void strrmspace(char * str)
{
char *p1, *p2;
char ch;
p1 = str; //first pointer
p2 = str; // second pointer to the remaining string
if (p1==NULL) return;
while (*p1)
{
if (*p1 != ' ')
{
ch = *p1;
*p2 = ch;
p1++;
p2++;
}
else
{
p1++;
}
}
*p2 = '\0';
}
uint32 getfile_crc_from_nand(void)
{
int ret = 0;
uint32 crc = 0;
RKNAND_SYS_STORGAE sysData;
int sys_fd = open("/dev/rknand_sys_storage",O_RDWR,0);
if(sys_fd < 0){
SLOGE("rknand_sys_storage open fail\n");
return -1;
}
sysData.tag = VENDOR_SECTOR_OP_TAG;
sysData.len = RKNAND_SYS_STORGAE_DATA_LEN-8;
ret = ioctl(sys_fd, RKNAND_GET_VENDOR_SECTOR0, &sysData);
/* rknand_print_hex_data("getfile_crc_from_nand data:",(uint32*)sysData.data,32); */
/* crc = sysData.data[24 * 4]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 1]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 2]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 3]; */
/* SLOGE("-luoxt- crc data = 0x%02x%02x%02x%02x", sysData.data[24 * 4], */
/* sysData.data[24 * 4 + 1], */
/* sysData.data[24 * 4 + 2], */
/* sysData.data[24 * 4 + 3]); */
crc = sysData.data[CONFIG_NUM * 4];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 1];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 2];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 3];
SLOGE("-luoxt- nand crc data = 0X%02X%02X%02X%02X", sysData.data[CONFIG_NUM * 4],
sysData.data[CONFIG_NUM * 4 + 1],
sysData.data[CONFIG_NUM * 4 + 2],
sysData.data[CONFIG_NUM * 4 + 3]);
close(sys_fd);
return crc;
}
int rk_update_lcd_parameters_from_sdcard(void)
{
int ret = 0;
FILE *fp = 0;
int n = 0;
char line[1024] = {0};
int i = 0;
RKNAND_SYS_STORGAE sysData;
int sys_fd = 0;
static uint32 file_crc = 0; //file lcdparameter crc data
static int updated = 0; // had store the param into the nand
static char got_crc = 0; //get file crc flag
/* SLOGE("---luoxt--- rk_update_lcd_parameters_from_sdcard\n"); */
while(access(LCD_PARAMETE_FILE_PATH, 0))
{
if(updated)
updated = 0;
if(got_crc)
got_crc = 0;
return -1;
}
if(!updated)
{
if (!got_crc)
{
fp = fopen(LCD_PARAMETE_FILE_PATH, "r");
if(fp == NULL)
{
SLOGE("open %s failed", LCD_PARAMETE_FILE_PATH);
return -1;
}
else
{
SLOGE("success open file %s", LCD_PARAMETE_FILE_PATH);
}
file_crc = getfile_crc(fp);
got_crc = 1;
SLOGE(" file crc is 0X%08X nand_crc is 0X%08X", file_crc, nand_crc );
fclose(fp);
}
if((nand_crc != file_crc))
{
fp = fopen(LCD_PARAMETE_FILE_PATH, "r");
if(fp == NULL)
{
SLOGE("open %s failed", LCD_PARAMETE_FILE_PATH);
return -1;
}
else
{
SLOGE("success open file %s", LCD_PARAMETE_FILE_PATH);
}
sys_fd = open("/dev/rknand_sys_storage",O_RDWR,0);
if(sys_fd < 0){
SLOGE("rknand_sys_storage open fail\n");
return -1;
}
sysData.tag = VENDOR_SECTOR_OP_TAG;
sysData.len = RKNAND_SYS_STORGAE_DATA_LEN - 8;
while(fgets(line, 1023, fp))
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0)
{
continue;
}
else if(p[0]=='#')
{
continue;
}
else if( (!(strstr(p, "="))) && (!(strstr(p, ";"))) && (!(strstr(p, "screen_"))))
{
continue;
}
else
{
// get key and value string like "screen_lvds_format = 1" spilt by ";"
char *key_val_str = strtok(p, ";");
char *value = strchr(key_val_str, '=');
if(value == NULL)
continue;
for(i = 0; i < CONFIG_NUM; i++)
{
if(strstr(p, key[i]))
{
char *val = strdelchr(value, '=');
strrmspace(val);
uint32 config1 = atoi(val);
sysData.data[i * 4] =(uint8) (config1 >> 24);
sysData.data[i * 4 + 1] =(uint8) (config1 >> 16);
sysData.data[i * 4 + 2] =(uint8) (config1 >> 8);
sysData.data[i * 4 + 3] =(uint8) (config1 >> 0);
SLOGE("-luoxt- %s=%d val %s", key[i], config1, val);
break;
}
}
}
}
// file crc data
sysData.data[CONFIG_NUM * 4] = (uint8) (file_crc >> 24);
sysData.data[CONFIG_NUM * 4 + 1] = (uint8) (file_crc >> 16);
sysData.data[CONFIG_NUM * 4 + 2] = (uint8) (file_crc >> 8);
sysData.data[CONFIG_NUM * 4 + 3] = (uint8) (file_crc >> 0);
SLOGE("-luoxt- crc32 = 0X%02X%02X%02X%02X", sysData.data[CONFIG_NUM * 4],
sysData.data[CONFIG_NUM * 4 + 1],
sysData.data[CONFIG_NUM * 4 + 2],
sysData.data[CONFIG_NUM * 4 + 3]);
/* rknand_print_hex_data("lcd paramters save:",(uint32*)sysData.data,32); */
ret = ioctl(sys_fd, RKNAND_STORE_VENDOR_SECTOR0, &sysData);
close(sys_fd);
if(ret){
SLOGE("save lcdparam error\n");
}
else
{
updated = 1;
nand_crc = file_crc;
fclose(fp);
sync();
reboot(RB_AUTOBOOT);
}
fclose(fp);
return ret;
}
}
return ret;
}
int main(void)
{
int ret = 0;
nand_crc = getfile_crc_from_nand();
while(1)
{
rk_update_lcd_parameters_from_sdcard();
usleep(100000);
}
return 0;
}
/* if the parameters has been changed, and then update the lcdparamers. */
/* the parameters will work after reboot. */
/* [email protected] */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <cutils/properties.h>
#include <sys/utsname.h>
#include <cutils/list.h>
#include <cutils/log.h>
#include <cutils/sockets.h>
#include <sys/reboot.h>
#include <cutils/iosched_policy.h>
#define LOG_TAG "LcdParamService"
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef unsigned char uint8;
#define LCD_PARAMETE_FILE_PATH "/mnt/external_sd/rk_lcd_parameters"
#define RKNAND_GET_VENDOR_SECTOR0 _IOW('v', 16, unsigned int)
#define RKNAND_STORE_VENDOR_SECTOR0 _IOW('v', 17, unsigned int)
#define RKNAND_SYS_STORGAE_DATA_LEN 512
#define VENDOR_SECTOR_OP_TAG 0x444E4556 // "VEND"
#define POLY 0xEDB88320L //CRC stand
#define CONFIG_NUM 24
char *key[CONFIG_NUM] =
{
"screen_type",
"screen_lvds_format",
"screen_face",
"screen_x_res",
"screen_y_res",
"screen_width",
"screen_height",
"screen_lcdc_aclk",
"screen_pixclock",
"screen_left_margin",
"screen_right_margin",
"screen_hsync_len",
"screen_upper_margin",
"screen_lower_margin",
"screen_vsync_len",
"screen_pin_hsync",
"screen_pin_vsync",
"screen_pin_den",
"screen_pin_dclk",
"screen_swap_rb",
"screen_swap_rg",
"screen_swap_gb",
"screen_swap_delta",
"screen_swap_dumy",
};
typedef struct tagRKNAND_SYS_STORGAE
{
unsigned long tag;
unsigned long len;
unsigned char data[RKNAND_SYS_STORGAE_DATA_LEN];
}RKNAND_SYS_STORGAE;
static uint32 crc32_tab[256];
static uint32 nand_crc = 0;
void rknand_print_hex_data(uint8 *s,uint32 * buf,uint32 len)
{
uint32 i,j,count;
SLOGE("%s",s);
for(i=0;i<len;i+=4)
{
SLOGE("%x %x %x %x",buf[i],buf[i+1],buf[i+2],buf[i+3]);
}
}
void init_crc32_tab(void)
{
int i = 0;
int j = 0;
uint32 crc = 0;
for(i = 0; i < 256; i++)
{
crc = (uint32)i;
for(j = 0; j < 8; j++)
{
if(crc & 0x00000001L)
crc = (crc >> 1) ^ POLY;
else
crc = crc >> 1;
}
crc32_tab[i] = crc;
}
}
uint32 get_crc32(uint32 crc_init, uint8 *crc_buf, uint32 buf_size)
{
uint32 crc = crc_init ^ 0xffffffff;
init_crc32_tab();
while(buf_size--)
crc = (crc >> 8) ^ crc32_tab[(crc & 0xff) ^ *crc_buf++];
return crc ^ 0xfffffff;
}
uint32 getfile_crc(FILE *fp)
{
uint32 size = 4 *1024;
uint8 crc_buf[size];
uint32 readln = 0;
uint crc = 0;
while((readln = fread(crc_buf, sizeof(uint8), size, fp)) > 0)
crc = get_crc32(crc, crc_buf, readln);
return crc;
}
/*
*去除字符串左端空格
*/
char *strtriml(char *pstr)
{
int i = 0,j;
j = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i <= j))
i++;
if (0<i)
strcpy(pstr, &pstr[i]);
return pstr;
}
/*
*去除字符串右端空格
*/
char *strtrimr(char *pstr)
{
int i;
i = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i >= 0))
pstr[i--] = '\0';
return pstr;
}
/*
*去除字符串兩端空格
*/
char *strtrim(char *pstr)
{
char *p;
p = strtrimr(pstr);
return strtriml(p);
}
char *strdelchr(char *pstr, int chr)
{
int i = 0;
int l = 0;
int ll = 0;
ll = l = strlen(pstr);
while(i < l)
{
if(pstr[i] == chr)
{
memmove((pstr + i), (pstr + i + 1), (ll - i -1));
pstr[ll - 1] = '\0';
ll--;
}
i++;
}
/* for(i = 0; i < l; i++) */
/* { */
/* if(pstr[i] == chr) */
/* { */
/* memmove((pstr + i), (pstr + i + 1), (l - i -1)); */
/* pstr[l - 1] = '\0'; */
/* break; */
/* } */
/* } */
return pstr;
}
void strrmspace(char * str)
{
char *p1, *p2;
char ch;
p1 = str; //first pointer
p2 = str; // second pointer to the remaining string
if (p1==NULL) return;
while (*p1)
{
if (*p1 != ' ')
{
ch = *p1;
*p2 = ch;
p1++;
p2++;
}
else
{
p1++;
}
}
*p2 = '\0';
}
uint32 getfile_crc_from_nand(void)
{
int ret = 0;
uint32 crc = 0;
RKNAND_SYS_STORGAE sysData;
int sys_fd = open("/dev/rknand_sys_storage",O_RDWR,0);
if(sys_fd < 0){
SLOGE("rknand_sys_storage open fail\n");
return -1;
}
sysData.tag = VENDOR_SECTOR_OP_TAG;
sysData.len = RKNAND_SYS_STORGAE_DATA_LEN-8;
ret = ioctl(sys_fd, RKNAND_GET_VENDOR_SECTOR0, &sysData);
/* rknand_print_hex_data("getfile_crc_from_nand data:",(uint32*)sysData.data,32); */
/* crc = sysData.data[24 * 4]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 1]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 2]; */
/* crc = (crc << 8) + sysData.data[24 * 4 + 3]; */
/* SLOGE("-luoxt- crc data = 0x%02x%02x%02x%02x", sysData.data[24 * 4], */
/* sysData.data[24 * 4 + 1], */
/* sysData.data[24 * 4 + 2], */
/* sysData.data[24 * 4 + 3]); */
crc = sysData.data[CONFIG_NUM * 4];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 1];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 2];
crc = (crc << 8) + sysData.data[CONFIG_NUM * 4 + 3];
SLOGE("-luoxt- nand crc data = 0X%02X%02X%02X%02X", sysData.data[CONFIG_NUM * 4],
sysData.data[CONFIG_NUM * 4 + 1],
sysData.data[CONFIG_NUM * 4 + 2],
sysData.data[CONFIG_NUM * 4 + 3]);
close(sys_fd);
return crc;
}
int rk_update_lcd_parameters_from_sdcard(void)
{
int ret = 0;
FILE *fp = 0;
int n = 0;
char line[1024] = {0};
int i = 0;
RKNAND_SYS_STORGAE sysData;
int sys_fd = 0;
static uint32 file_crc = 0; //file lcdparameter crc data
static int updated = 0; // had store the param into the nand
static char got_crc = 0; //get file crc flag
/* SLOGE("---luoxt--- rk_update_lcd_parameters_from_sdcard\n"); */
while(access(LCD_PARAMETE_FILE_PATH, 0))
{
if(updated)
updated = 0;
if(got_crc)
got_crc = 0;
return -1;
}
if(!updated)
{
if (!got_crc)
{
fp = fopen(LCD_PARAMETE_FILE_PATH, "r");
if(fp == NULL)
{
SLOGE("open %s failed", LCD_PARAMETE_FILE_PATH);
return -1;
}
else
{
SLOGE("success open file %s", LCD_PARAMETE_FILE_PATH);
}
file_crc = getfile_crc(fp);
got_crc = 1;
SLOGE(" file crc is 0X%08X nand_crc is 0X%08X", file_crc, nand_crc );
fclose(fp);
}
if((nand_crc != file_crc))
{
fp = fopen(LCD_PARAMETE_FILE_PATH, "r");
if(fp == NULL)
{
SLOGE("open %s failed", LCD_PARAMETE_FILE_PATH);
return -1;
}
else
{
SLOGE("success open file %s", LCD_PARAMETE_FILE_PATH);
}
sys_fd = open("/dev/rknand_sys_storage",O_RDWR,0);
if(sys_fd < 0){
SLOGE("rknand_sys_storage open fail\n");
return -1;
}
sysData.tag = VENDOR_SECTOR_OP_TAG;
sysData.len = RKNAND_SYS_STORGAE_DATA_LEN - 8;
while(fgets(line, 1023, fp))
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0)
{
continue;
}
else if(p[0]=='#')
{
continue;
}
else if( (!(strstr(p, "="))) && (!(strstr(p, ";"))) && (!(strstr(p, "screen_"))))
{
continue;
}
else
{
// get key and value string like "screen_lvds_format = 1" spilt by ";"
char *key_val_str = strtok(p, ";");
char *value = strchr(key_val_str, '=');
if(value == NULL)
continue;
for(i = 0; i < CONFIG_NUM; i++)
{
if(strstr(p, key[i]))
{
char *val = strdelchr(value, '=');
strrmspace(val);
uint32 config1 = atoi(val);
sysData.data[i * 4] =(uint8) (config1 >> 24);
sysData.data[i * 4 + 1] =(uint8) (config1 >> 16);
sysData.data[i * 4 + 2] =(uint8) (config1 >> 8);
sysData.data[i * 4 + 3] =(uint8) (config1 >> 0);
SLOGE("-luoxt- %s=%d val %s", key[i], config1, val);
break;
}
}
}
}
// file crc data
sysData.data[CONFIG_NUM * 4] = (uint8) (file_crc >> 24);
sysData.data[CONFIG_NUM * 4 + 1] = (uint8) (file_crc >> 16);
sysData.data[CONFIG_NUM * 4 + 2] = (uint8) (file_crc >> 8);
sysData.data[CONFIG_NUM * 4 + 3] = (uint8) (file_crc >> 0);
SLOGE("-luoxt- crc32 = 0X%02X%02X%02X%02X", sysData.data[CONFIG_NUM * 4],
sysData.data[CONFIG_NUM * 4 + 1],
sysData.data[CONFIG_NUM * 4 + 2],
sysData.data[CONFIG_NUM * 4 + 3]);
/* rknand_print_hex_data("lcd paramters save:",(uint32*)sysData.data,32); */
ret = ioctl(sys_fd, RKNAND_STORE_VENDOR_SECTOR0, &sysData);
close(sys_fd);
if(ret){
SLOGE("save lcdparam error\n");
}
else
{
updated = 1;
nand_crc = file_crc;
fclose(fp);
sync();
reboot(RB_AUTOBOOT);
}
fclose(fp);
return ret;
}
}
return ret;
}
int main(void)
{
int ret = 0;
nand_crc = getfile_crc_from_nand();
while(1)
{
rk_update_lcd_parameters_from_sdcard();
usleep(100000);
}
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.