环境
内核版本:linux-2.6,当发生内核崩溃时,重启之前把panic打印信息保存在flash裸设备中保存;
代码
kernel/panic.c ++
@@ -48,6 +48,10 @@
long (*panic_blink)(int state);
EXPORT_SYMBOL(panic_blink);
+extern void xd_store_crash(void);
/**
* panic - halt the system
* @fmt: The text string to print
@@ -114,6 +118,9 @@
bust_spinlocks(0);
+ xd_store_crash();
if (!panic_blink)
panic_blink = no_blink;
kernel/printk.c ++
char *get_logbuf (void)
{
printk("CRASH LOG %d %d %d\n",log_buf_len,log_start,log_end);
return (log_buf);
}
int get_logsize (void)
{
return (log_buf_len);
}
int get_logstart(void)
{
return log_start;
}
int get_logend(void)
{
return log_end;
}
crashlog.c +add
#include <linux/module.h>
#include <linux/bootmem.h>
#include <linux/debugfs.h>
#include <linux/crashlog.h>
#include <linux/kmsg_dump.h>
#include <linux/module.h>
#include <linux/pfn.h>
#include <asm/io.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <linux/slab.h>
#define CRASHLOG_SIZE 16*1024
struct crashlog{
unsigned int len;
unsigned char *buf;
};
struct logbuffer{
int buf_len;
char *buffer;
};
static void erase_callback(struct erase_info *done)
{
wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
wake_up(wait_q);
}
static int erase_write(struct mtd_info *mtd, unsigned long pos,int len, const char *buf)
{
struct erase_info erase;
DECLARE_WAITQUEUE(wait, current);
wait_queue_head_t wait_q;
size_t retlen;
int ret;
init_waitqueue_head(&wait_q);
erase.mtd = mtd;
erase.callback = erase_callback;
erase.addr = pos;
erase.len = len;
erase.priv = (u_long)&wait_q;
set_current_state(TASK_UNINTERRUPTIBLE);
add_wait_queue(&wait_q, &wait);
ret = mtd->erase(mtd, &erase);
if (ret) {
remove_wait_queue(&wait_q, &wait);
return ret;
}
remove_wait_queue(&wait_q, &wait);
ret = mtd->write(mtd, pos, len, &retlen, buf);
if (ret)
return ret;
if (retlen != len)
return -EIO;
return 0;
}
static int get_logbuffer(struct logbuffer *log)
{
int log_end, log_size;
extern char *get_logbuf(void);
extern int get_logsize(void);
//extern int get_logstart(void);
extern int get_logend(void);
char *buffer;
int idx,i = 0;
buffer = (char *)get_logbuf();
//log_start = get_logstart();
log_end = get_logend();
log_size = get_logsize();//Get syslog buffer cache, this system is 128K
log->buf_len = CRASHLOG_SIZE;// We save last 16K log
if(log_end - CRASHLOG_SIZE > 0){
idx = log_end - CRASHLOG_SIZE;
log->buf_len = CRASHLOG_SIZE;
}else{
log->buf_len = log_end;
idx = 0;
}
log->buffer = (char *)kzalloc(log->buf_len,GFP_KERNEL);
if(log->buffer == NULL){
printk("%s:Can't kmalloc, fail\n",__func__);
return -2;
}
for(;idx != log_end;idx++)
log->buffer[i++] = buffer[idx%log_size];
return 0;
}
static void put_logbuffer(struct logbuffer *log)
{
kfree(log->buffer);
}
static int init_cache(struct mtd_info *mtd, struct crashlog *cache)
{
int ret = 0;
unsigned int *tmp = NULL;
int len = 0;
unsigned char *buf;
buf = (char *)kmalloc(mtd->size,GFP_KERNEL);
if(buf == NULL){
printk("%s:Can't kmalloc, fail\n",__func__);
return -2;
}
for (len = 0; len < mtd->size; len++){
buf[len] = 0xff;
}
mtd->read(mtd, 0, mtd->size, &len, buf);
for (len = 0; len < 12; len++){// the 12c is 0xff
if (buf[len] != 0xff) {
break;
}
}
if(len == 12) {//partition is clean
cache->len = sizeof(cache->len);
} else {
tmp = (unsigned int *)buf;
cache->len = *tmp;
}
if(cache->len >= mtd->size){//partition is full
printk("Crash is full, Please clean it first\n");
kfree(buf);
ret = -1;
}
cache->buf = buf;
return ret;
}
static void release_cache(struct crashlog *cache)
{
kfree(cache->buf);
}
static void fill_buffer(struct crashlog *cache,struct logbuffer *log, int maxsize)
{
int len = 0;
unsigned int *tmp = NULL;
int write_size, start, ofs;
len = cache->len + log->buf_len;
start = cache->len;
if (len >= maxsize){
write_size = maxsize-cache->len;
ofs = log->buf_len-write_size;
memcpy(&cache->buf[start],&log->buffer[ofs],write_size);
cache->len = maxsize;
}else{
write_size = log->buf_len;
memcpy(&cache->buf[start],log->buffer,write_size);
cache->len += log->buf_len;
}
if(cache->len < maxsize)
cache->buf[cache->len++] = '\n';
tmp = (unsigned int *)cache->buf;
*tmp = cache->len;
}
void xd_store_crash(void)
{
struct mtd_info *mtd = NULL;
struct logbuffer log;
struct crashlog cache;
int len;
mtd = get_mtd_device_nm("crash");
printk("Trying to store to crash(size:%lld)\n",mtd->size);
if(get_logbuffer(&log) < 0)
return;
if (!IS_ERR(mtd)) {
if(init_cache(mtd, &cache) < 0){
goto out;
}
fill_buffer(&cache,&log, mtd->size);
#if 0
printk("Dump:\n");
for(len = 0; len < 20;len++){
printk("0x%02X ",cache.buf[len]);
}
printk("\n");
#endif
len = erase_write(mtd, 0,mtd->size, cache.buf);
release_cache(&cache);
if (len == 0){
printk("Crash Saved to %d\n",cache.len);
}
} else {
printk("Could not find crash partition\n");
}
out:
put_logbuffer(&log);
}
EXPORT_SYMBOL(xd_store_crash);
MODULE_DESCRIPTION("CRASHLOG DRIVER");
MODULE_AUTHOR("xxxx@qq.com");