1#include <sys/types.h>
2#include <stdlib.h>
3#include <limits.h>
4#include <stdio.h>
5#include <fcntl.h>
6#include <string.h>
7#include <signal.h>
8#include <pwd.h>
9#include <sys/file.h>
10#include <sys/ioctl.h>
11#include <sys/mount.h>
12#include <sys/uio.h>
13#include <linux/version.h>
14#include <errno.h>
15#include <fcntl.h>
16
17#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
18#define OOPS_PAGE_SIZE 4096
19#define OOPS_PART_SIZE 64 * 1024
20#define DEBUG_PANIC_FILE "/tmp/Panic-log.txt"
21#define GOOD_MESSAGE "Good, No kernel panic found so far..."
22
23struct mtd_info_user
24{
25	unsigned char   type;
26	unsigned int    flags;
27	unsigned int    size;           /* Total size of the MTD */
28	unsigned int    erasesize;
29	unsigned int    oobblock;       /* Size of OOB blocks (e.g. 512) */
30	unsigned int    oobsize;        /* Amount of OOB data per block (e.g. 16) */
31	unsigned int    ecctype;
32	unsigned int    eccsize;
33};
34
35#define MEMGETINFO      _IOR('M', 1, struct mtd_info_user)
36
37struct mtd_info_user mtdInfo;
38
39static void dni_get_mtdoops_info(char *oops_mtd)
40{
41	int fd;
42
43	fd = open(oops_mtd, O_RDWR);
44	if (fd < 0) {
45		printf("open error \n");
46		return;
47	}
48
49	if (ioctl(fd, MEMGETINFO, &mtdInfo)){
50		printf("ioctl memgetinfo error \n");
51		return;
52	}
53ret:
54        close(fd);
55}
56
57int dni_read_mtdoops_data(char *oops_mtd, char *buf, int buf_len)
58{
59	int i;
60	FILE *fp;
61	int page, ret, total = 0;
62	unsigned int *count;
63	char buff[OOPS_PAGE_SIZE];
64
65	/* get the file all information */
66	if ((fp = fopen(oops_mtd, "r")) == NULL) {
67		printf("Open error!");
68		return -1;
69	}
70
71	/*read mtd file to buffer */
72	for (page = 0; page < (mtdInfo.size / OOPS_PAGE_SIZE); page++) {
73		ret = fread(buff, OOPS_PAGE_SIZE, 1, fp);
74
75		count = (unsigned int *)buff;
76
77		if(count[1] != MTDOOPS_KERNMSG_MAGIC)
78			break;
79
80		for(i = 0; i < (OOPS_PAGE_SIZE - 8); i++)
81			buf[total++] = buff[i + 8];
82
83		buf[total++] = '\n';
84	}
85	fclose(fp);
86
87	return total;
88}
89
90int main(int argc, char **argv)
91{
92	char buf[OOPS_PART_SIZE];
93	FILE *fp;
94	int i = 0, count = 0;
95	char *oops_mtd;
96
97	if(argc < 2){
98		printf("lack of param, exit..\n");
99		return 1;
100	}
101
102	oops_mtd = argv[1];
103
104	/* clear memory all 0x20 space */
105	memset(buf, 0x20, OOPS_PART_SIZE);
106
107	/* get the mtdoops size */
108	dni_get_mtdoops_info(oops_mtd);
109
110	count = dni_read_mtdoops_data(oops_mtd, buf, OOPS_PART_SIZE);
111//	printf("********** %d\n",count);
112	if (count == 0) {
113//		printf("####### there's no crash yet #######\n");
114		strcpy(buf, GOOD_MESSAGE);
115		count = strlen(GOOD_MESSAGE);
116
117	}
118
119	if (( fp = fopen(DEBUG_PANIC_FILE, "w")) == NULL) {
120		printf("Open error!\n");
121		return -1;
122	}
123
124	do {
125		//printf("###### have got crash ########\n");
126
127		/* skip the loglevel in kernel*/
128//		printf("******** some errors ********\n");
129		if (buf[i + 0] == '<' && buf[i + 1] >= '0' &&
130				buf[i + 1] <= '7' && buf[i + 2] == '>') {
131                        i += 3;
132			continue;
133		}
134
135		fputc(buf[i++], fp);
136
137	} while( i < count);
138	for(i = 0; i < 4096; i++)
139		 printf("%c",buf[i]);
140//	printf("\n\n\n\n\n\n\n\n\\n");
141	fclose(fp);
142
143	return 0;
144}
145