blob: 1ab4e01eeff53dbf4d6a660ea1e1e93e3fa857d5 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#include <stdlib.h>
2#include <stdio.h>
3#include <stdint.h>
4#include <fcntl.h>
5#include <pthread.h>
6
7#include "mtk_device_wrap.h"
8#include "lynq_nand.h"
9
10#if 1
11#define NAND_LOG_PATCH "/dev/mtd19"
12#else
13#define NAND_LOG_PATCH "/dev/mtd19/lynq_log.txt"
14#endif
15
16#define NAND_PRIMITIVE_OFFSET 2*1024
17#define NAND_TOTAL_SIZE 20*1024*1024
18#define NAND_CURRENT_LEN 2*1024
19#define NAND_OLD_CURRENT_LEN 0
20
21static int lynq_nand_offset_global = 0;
22static pthread_mutex_t nand_mutex = PTHREAD_MUTEX_INITIALIZER;
23char nand_count = 0;
24
25void lynq_store_nand_offset_data(char *store_nand_offset_data);
26int lynq_read_nand_offset_data(char *read_nand_offset_data);
27
28
29int lynq_nand_store_log_message(char *log_message,size_t log_message_len)
30{
31 pthread_mutex_lock(&nand_mutex);
32 int fd;
33 int err = 0;
34 char nand_current_buf[NAND_CURRENT_LEN] = {0};
35 unsigned int nand_page = 0;
36 int nand_offset_count = 0;
37 char nand_offset_buf[8] = {0};
38 char read_nand_buf[8] = {0};
39
40 if(log_message_len == 0)
41 {
42 return -1;
43 }
44
45 fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDWR);
46 if(fd < 0)
47 {
48 perror("open nand log message error1!!!!");
49 return fd;
50 }
51
52 if(lynq_read_nand_offset_data(nand_offset_buf) == 0)
53 {
54 nand_offset_count = atoi(nand_offset_buf);
55 lynq_nand_offset_global = nand_offset_count;
56 }
57
58 ((log_message_len % 2048) == 0 ) ? (nand_page = log_message_len / 2048) : (nand_page = (log_message_len / 2048)+1);
59//printf("nand_page:%d\n",nand_page);
60 while(1)
61 {
62 err = mtk_device_wrap_seek(fd, lynq_nand_offset_global, SEEK_SET);
63 if(err == -1)
64 {
65 perror("seek nand log message error8!!!!");
66 return err;
67 }
68 mtk_device_wrap_read(fd, read_nand_buf, 8);
69 if(((read_nand_buf[0] == 0xff) && (read_nand_buf[1] == 0xff) && (read_nand_buf[2] == 0xff) &&(read_nand_buf[3] == 0xff)) ||\
70 ((lynq_nand_offset_global % (128 *1024) ) == 0))
71 {
72 for(int i = 0; i < nand_page; i++)
73 {
74 memset(nand_current_buf,0,NAND_CURRENT_LEN);
75 err = mtk_device_wrap_seek(fd, lynq_nand_offset_global, SEEK_SET);
76 if(err == -1)
77 {
78 perror("seek nand log message error8!!!!");
79 return err;
80 }
81 if((i+1) == nand_page)
82 {
83 memcpy(nand_current_buf,&log_message[i*NAND_CURRENT_LEN],(log_message_len-(i*2048)));
84// printf("(i+1) == nand_page---%d\n",(log_message_len-(i*2048)));
85 }
86 else
87 {
88 memcpy(nand_current_buf,&log_message[i*NAND_CURRENT_LEN],NAND_CURRENT_LEN);
89 printf("(i+1) != nand_page\n");
90 }
91
92 err = mtk_device_wrap_write(fd, nand_current_buf, NAND_CURRENT_LEN);
93 if(err == -1)
94 {
95 perror("write nand log message error9!!!!");
96 return err;
97 }
98 lynq_nand_offset_global += NAND_CURRENT_LEN;
99 if(lynq_nand_offset_global > NAND_TOTAL_SIZE)
100 {
101 lynq_nand_offset_global = 0;
102 }
103 }
104 break;
105 }
106//printf("lynq_nand_offset_global:%d---nand_offset_count:%d\n",lynq_nand_offset_global,nand_offset_count);
107 lynq_nand_offset_global += NAND_CURRENT_LEN;
108 if(lynq_nand_offset_global > NAND_TOTAL_SIZE)
109 {
110 lynq_nand_offset_global = 0;
111 }
112 }
113
114 if(nand_count >= 64)
115 {
116 nand_count = 0;
117 sprintf(nand_offset_buf,"%d",lynq_nand_offset_global);
118 lynq_store_nand_offset_data(nand_offset_buf);
119 }
120
121 err = mtk_device_wrap_close(fd);
122
123 pthread_mutex_unlock(&nand_mutex);
124 return 0;
125}
126
127
128int lynq_nand_get_log_message(char *log_message,size_t log_message_len,off_t offset)
129{
130 int fd;
131 int err = 0;
132
133 pthread_mutex_lock(&nand_mutex);
134
135 unsigned int lynq_nand_offset_part = 0;
136
137 fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDONLY);
138 if(fd < 0)
139 {
140 perror("open nand log message error!!!!");
141 return fd;
142 }
143
144 lynq_nand_offset_part = NAND_PRIMITIVE_OFFSET + offset;
145//printf("lynq_nand_offset_part 13:%d\n",lynq_nand_offset_part);
146 err = mtk_device_wrap_seek(fd, lynq_nand_offset_part, SEEK_SET);
147//printf("lynq_nand_offset_part 14:%d----nand seek 4:%d\n",lynq_nand_offset_part,err);
148 if(err == -1)
149 {
150 perror("seek nand log message error 15!!!!");
151 return err;
152 }
153
154 mtk_device_wrap_read(fd, log_message, log_message_len);
155
156 err = mtk_device_wrap_close(fd);
157
158 pthread_mutex_unlock(&nand_mutex);
159 return err;
160}
161
162
163int lynq_nand_read_data_test(void)
164{
165 int fd;
166 FILE *file_fd = NULL;
167 int err;
168 char log_data_buf[6*1024] = {0};
169 char log_nand_buf[2048] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99};
170 static unsigned int nand_offset_test = 0;
171 char *filePath = "/usr/local/lnand_log.txt";
172
173 system("touch /usr//local/lnand_log.txt");
174 system("chmod 777 /usr//local/lnand_log.txt");
175
176 fd=mtk_device_wrap_open(NAND_LOG_PATCH,O_RDWR);
177
178 if(fd < 0)
179 {
180 perror("open nand log message error!!!!");
181 return fd;
182 }
183
184 file_fd = fopen(filePath,"rt+");
185 if(NULL == file_fd)
186 {
187 perror("file_fd");
188 return -1;
189 }
190
191 for(int i = 0 ;i < 100; i++)
192 {
193 memset(log_data_buf,0,6*1024);
194 err = mtk_device_wrap_seek(fd, nand_offset_test, SEEK_SET);
195
196
197 if(err == -1)
198 {
199 perror("seek nand log message error!!!!");
200 return err;
201 }
202
203 fseek(file_fd,nand_offset_test,SEEK_SET);
204
205 mtk_device_wrap_read(fd, log_data_buf, 6*1024);
206 nand_offset_test += (6*1024);
207
208 fwrite(&log_data_buf,sizeof(log_data_buf), 1, file_fd);
209
210 }
211
212 fclose(file_fd);
213 mtk_device_wrap_close(fd);
214
215}
216
217
218
219void lynq_store_nand_offset_data(char *store_nand_offset_data)
220{
221 char *log_level_writefile = "persist.store_nand_data";
222 property_set(log_level_writefile,(char *)store_nand_offset_data);
223 return ;
224}
225
226int lynq_read_nand_offset_data(char *read_nand_offset_data)
227{
228 char *log_level_readfile = "persist.store_nand_data";
229
230 if(property_get(log_level_readfile,(char *)read_nand_offset_data,NULL) > 0)
231 {
232// printf("read log level deploy success!!!\n");
233 return 0;
234 }
235 else
236 {
237// printf("get file error!!!\n");
238 return -1;
239 }
240}