blob: 0b0f0dcf0d8fe2fba2d0e45ed69660b9364ace37 [file] [log] [blame]
l.yangb8fdece2024-10-10 14:56:17 +08001/**
2 * @file oss_ramdump_osa.c
3 * @brief Implementation of Ramdump os adapt
4 *
5 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
6 * @author Qing Wang <wang.qing@sanechips.com.cn>
7 * @ingroup si_ap_oss_ramdump_id
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 */
24
25/*******************************************************************************
26 * Include header files *
27 ******************************************************************************/
28#include "ramdump.h"
29#include <linux/lzo.h>
30#include "ramdump_compress.h"
31#ifdef CONFIG_RAMDUMP_EMMC
32#include "ramdump_emmc.h"
33#endif
34#ifdef CONFIG_MTD_SPI_NAND
35#include "ramdump_spinand.h"
36#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/*******************************************************************************
43* Extern function declarations *
44*******************************************************************************/
45extern unsigned char *ramdump_phy_to_vir(unsigned long phy, unsigned long size);
46extern int dump_printk_text(char *buffer, unsigned long len);
47
48/*******************************************************************************
49* Extern variable declarations *
50*******************************************************************************/
51extern unsigned int ramdump_compress_flag;
52extern unsigned char *ramdump_log_buf;
53extern unsigned int ramdump_export_mode;
xf.li6b423c72025-03-14 00:07:42 -070054extern unsigned int ramdump_emmc_size;
55extern unsigned int ramdump_spinand_size;
l.yangb8fdece2024-10-10 14:56:17 +080056
57/*******************************************************************************
58 * Macro definitions *
59 ******************************************************************************/
60/*Ö¸ÁîÖ¡³¤¶È */
61#define RAMDUMP_INTERACTIVE_DATA_LEN 40
62#define RAMDUMP_INTERACTIVE_ARRAY_LEN 10
63
64/* ramdump ºÍ ¹²ÏíÄÚ´æ½»»¥ÃüÁîÔ¼¶¨ */
65/*ͬ²½ÇëÇó*/
66#define RAMDUMP_PC_INTERACTIVE_REQ 1
67/*ͬ²½ÇëÇóÓ¦´ð,´«ÊäramdumpµÄÎļþÊýÄ¿*/
68#define RAMDUMP_TRANS_SERVER_INTERACTIVE_RSP 2
69/*ÇëÇó´«µÝÖ¸¶¨Îļþ±àºÅµÄÎļþÐÅÏ¢*/
70#define RAMDUMP_PC_FILE_INFO_READ_REQ 3
71/*ÇëÇó´«µÝÖ¸¶¨Îļþ±àºÅµÄÎļþÐÅÏ¢µÄÓ¦´ð£¬´«ÊäÎļþÃû¼°´óС*/
72#define RAMDUMP_TRANS_SERVER_FILE_INFO_READ_RSP 4
73/*ÇëÇó¶Áȡָ¶¨Îļþ±àºÅµÄÎļþÄÚÈÝ*/
74#define RAMDUMP_PC_FILE_DATA_TRANS_REQ 5
75/*ÇëÇó¶Áȡָ¶¨Îļþ±àºÅµÄÎļþÄÚÈݵÄÓ¦´ð£¬´«ÊäÎļþÄÚÈÝ*/
76#define RAMDUMP_TRANS_SERVER_FILE_DATA_TRANS_RSP 6
77/*´«Êä½áÊø*/
78#define RAMDUMP_PC_FILE_TRANS_DONE_REQ 7
79/*´«Êä½áÊøÓ¦´ð*/
80#define RAMDUMP_TRANS_SERVER_FILE_TRANS_DONE_RSP 8
81
82/* ´íÎóÀàÐÍ */
83/*Ö¸Áî´íÎó*/
84#define RAMDUMP_INTERACTIVE_CMD_ERROR 9
85/*ÇëÇó´«µÝÖ¸¶¨Îļþ±àºÅ´í*/
86#define RAMDUMP_FILE_NUMBER_ERROR 10
87/*ÇëÇó´«µÝÖ¸¶¨ÎļþλÖôóС´í*/
88#define RAMDUMP_FILE_SIZE_ERROR 11
89
90#define RAMDUMP_DELAY_MS_COUNT (2500)
91
92/*******************************************************************************
93 * Type definitions *
94 ******************************************************************************/
95/*
96 * struct TRANS WITH AP
97 */
98
99/* trans_server rsp pc, interactive msg struct */
100typedef struct
101{
102 unsigned int cmd;
103 unsigned int file_num;
104} ramdump_trans_server_interactive_req;
105
106/* trans_server rsp pc, file info msg struct */
107typedef struct
108{
109 unsigned int cmd;
110 char file_name[RAMDUMP_RAMCONF_FILENAME_MAXLEN];
111 unsigned int file_size;
112} ramdump_trans_server_file_info_req;
113
114/* pc req trans_server, file info msg struct */
115typedef struct
116{
117 unsigned int cmd;
118 unsigned int file_id;
119} ramdump_pc_file_info_rsp;
120
121/* trans_server rsp pc, trans data msg struct */
122typedef struct
123{
124 unsigned int cmd;
125 unsigned int buf_addr;
126 unsigned int buf_left_size;
127} ramdump_trans_server_data_trans_req;
128
129/* pc req trans_server, trans data msg struct */
130typedef struct
131{
132 unsigned int cmd;
133 unsigned int file_id; /* Îļþ±àºÅ */
134 unsigned int offset; /* offsetΪÊý¾ÝÆ«ÒÆ */
135 unsigned int length; /* lengthΪÊý¾Ý³¤¶È */
136} ramdump_pc_trans_data_rsp;
137
138/*******************************************************************************
139 * Local function declarations *
140 ******************************************************************************/
141
142/*******************************************************************************
143 * Local variable definitions *
144 ******************************************************************************/
145char *ramdump_log_buf_region = NULL;
146unsigned int ramdump_log_buf_len = 0;
147
148/*******************************************************************************
149 * Global variable definitions *
150 ******************************************************************************/
151unsigned char *ramdump_shared_mem_base = NULL;
152unsigned char *ramdump_export_flag_base = NULL;
153int ramdump_file_num = 0;
154ramdump_file_t ramdump_device_fp = {0};
155ramdump_file_t ramdump_spinand_fp = {0};
156ramdump_file_t *g_ramdump_dev_fp;
157unsigned int ramdump_device_file_cnt = 0;
158unsigned char *ramdump_log_buf = NULL; /* ¸´ÓÃramdump´æ´¢µÄ128KB(Æ«ÒÆ16KB) */
159
160/*******************************************************************************
161 * Inline function implementations *
162 ******************************************************************************/
163static inline void ramdump_wait_delay( unsigned long ms)
164{
165 volatile int j = 0;
166 for (j = 0; j < 10000; j++);
167}
168/*******************************************************************************
169 * extern function implementations *
170 ******************************************************************************/
171
172/*******************************************************************************
173* ¹¦ÄÜÃèÊö: ramdump_oss_data_trans_write
174* ²ÎÊý˵Ã÷:
175* (´«Èë²ÎÊý) void
176* (´«³ö²ÎÊý) void
177* ·µ »Ø Öµ: void
178* ÆäËü˵Ã÷: This function is used for ramdump to trans dump data to PC
179*******************************************************************************/
180int ramdump_oss_data_trans_write(unsigned char *buffer, unsigned int size)
181{
182 int ret;
183 ramdump_shmem_t *msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
184
185 if (size > (RAMDUMP_SHARED_MEM_LEN- roundup(sizeof(ramdump_shmem_t), RAMDUMP_SHMEM_ALIGN_SIZE)));
186 ret = -1;
187
188 while(1){
189 if ((msg->core_flag == 1) && (msg->rw_flag == 1)){
190 memcpy(msg->buf, buffer, size);
191 msg->size = size;
192 msg->core_flag = 0;
193 msg->rw_flag = 2;
194 ret = size;
195 break;
196 }
197 else
198 ramdump_wait_delay(0);
199 }
200 return ret;
201}
202
203/*******************************************************************************
204* ¹¦ÄÜÃèÊö: ramdump_oss_data_trans_read
205* ²ÎÊý˵Ã÷:
206* (´«Èë²ÎÊý) void
207* (´«³ö²ÎÊý) void
208* ·µ »Ø Öµ: void
209* ÆäËü˵Ã÷: This function is used for ramdump to trans dump data to PC
210*******************************************************************************/
211int ramdump_oss_data_trans_read(unsigned char *buffer, unsigned int size)
212{
213 int ret;
214 ramdump_shmem_t *msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
215
216 if (size > (RAMDUMP_SHARED_MEM_LEN - roundup(sizeof(ramdump_shmem_t), RAMDUMP_SHMEM_ALIGN_SIZE)))
217 ret = -1;
218
219 while(1){
220 if ((msg->core_flag == 1) && (msg->rw_flag == 2)){
221 if (size < msg->size)
222 return -1;
223 memcpy(buffer, msg->buf, msg->size);
224 msg->size = size;
225 msg->core_flag = 1;
226 msg->rw_flag = 1;
227 ret = size;
228 break;
229 }
230 else
231 ramdump_wait_delay(0);
232 }
233 return ret;
234}
235
236/*******************************************************************************
237* ¹¦ÄÜÃèÊö: ramdump_oss_data_trans_init
238* ²ÎÊý˵Ã÷:
239* (´«Èë²ÎÊý) void
240* (´«³ö²ÎÊý) void
241* ·µ »Ø Öµ: void
242* ÆäËü˵Ã÷: This function is used for map ramdump_shared_mem_base
243*******************************************************************************/
244void ramdump_oss_data_trans_init(void)
245{
246 ramdump_shmem_t *msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
247
248 memset(msg, 0, sizeof(ramdump_shmem_t));
249 msg->core_flag = 1;
250 msg->rw_flag = 1;
251}
252
253/*******************************************************************************
254* ¹¦ÄÜÃèÊö: ramdump_device_init
255* ²ÎÊý˵Ã÷:
256* (´«Èë²ÎÊý) void
257* (´«³ö²ÎÊý) void
258* ·µ »Ø Öµ: void
259* ÆäËü˵Ã÷: This function is used for init fp head
260*******************************************************************************/
261int ramdump_device_init(void)
262{
263 int ret = 0;
264
265 ramdump_lzo_init();
266 if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
267 {
268#ifdef CONFIG_RAMDUMP_EMMC
269 ret = ramdump_emmc_init(&ramdump_device_fp);
270#endif
271 g_ramdump_dev_fp = &ramdump_device_fp;
272 }
273 else if(ramdump_export_mode == RAMDUMP_MODE_SPINAND)
274 {
275#ifdef CONFIG_MTD_SPI_NAND
276 ret = ramdump_spinand_init(&ramdump_spinand_fp);
277#endif
278 g_ramdump_dev_fp = &ramdump_spinand_fp;
279 }
280 return ret;
281}
282
283/*******************************************************************************
284* ¹¦ÄÜÃèÊö: ramdump_device_close
285* ²ÎÊý˵Ã÷:
286* (´«Èë²ÎÊý) void
287* (´«³ö²ÎÊý) void
288* ·µ »Ø Öµ: void
289* ÆäËü˵Ã÷: This function is used for print close msg
290*******************************************************************************/
291void ramdump_device_close(void)
292{
xf.li6b423c72025-03-14 00:07:42 -0700293 g_ramdump_dev_fp->file_num = ramdump_device_file_cnt;
294
l.yangb8fdece2024-10-10 14:56:17 +0800295 if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
296 {
297#ifdef CONFIG_RAMDUMP_EMMC
298 ramdump_emmc_close(&ramdump_device_fp);
299#endif
300 }
301 else if (ramdump_export_mode == RAMDUMP_MODE_SPINAND)
302 {
303#ifdef CONFIG_MTD_SPI_NAND
304 ramdump_spinand_close(&ramdump_spinand_fp);
305#endif
306 }
307}
308
309/*******************************************************************************
310* ¹¦ÄÜÃèÊö: ramdump_fill_header
311* ²ÎÊý˵Ã÷:
312* (´«Èë²ÎÊý)
313* (´«³ö²ÎÊý) void
314* ·µ »Ø Öµ: void
315* ÆäËü˵Ã÷: This function is used for ramdump file header
316*******************************************************************************/
317int ramdump_fill_header(char *file_name, unsigned int file_size, ramdump_file_t *fp, unsigned int offset)
318{
319 if (ramdump_device_file_cnt >= RAMDUMP_FILE_NUM_MAX)
320 return -1;
321
322 fp->file_fp[ramdump_device_file_cnt].magic = 0x3A3A3A3A;
323 strncpy(fp->file_fp[ramdump_device_file_cnt].file_name, file_name, RAMDUMP_RAMCONF_FILENAME_MAXLEN - 1);
324 fp->file_fp[ramdump_device_file_cnt].offset = offset;
325 fp->file_fp[ramdump_device_file_cnt].size = file_size;
326 return 0;
327}
328
329/*******************************************************************************
330* ¹¦ÄÜÃèÊö: ramdump_device_write_file
331* ²ÎÊý˵Ã÷:
332* (´«Èë²ÎÊý) void
333* (´«³ö²ÎÊý) void
334* ·µ »Ø Öµ: void
335* ÆäËü˵Ã÷: This function is used for write file infomation
336*******************************************************************************/
337int ramdump_device_write_file(ramdump_trans_server_file_info_req *server_to_cap)
338{
339 int ret = -1;
xf.li6b423c72025-03-14 00:07:42 -0700340 unsigned int file_size = 0;
341
342 /* Started by AICoder, pid:wcfb91c2aa35add146d90b5530cd112845133621 */
343 file_size = server_to_cap->file_size;
l.yangb8fdece2024-10-10 14:56:17 +0800344
345 if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
346 {
347#ifdef CONFIG_RAMDUMP_EMMC
xf.li6b423c72025-03-14 00:07:42 -0700348 if ((ramdump_emmc_offset >= RAMDUMP_TRANS_EMMC_LEN)
349 || ((ramdump_emmc_offset + file_size) > ramdump_emmc_size))
l.yangb8fdece2024-10-10 14:56:17 +0800350 return -1;
351
352 ret = ramdump_fill_header(server_to_cap->file_name,
353 server_to_cap->file_size,
354 &ramdump_device_fp,
355 ramdump_emmc_offset);
356#endif
357 }
358 else if(ramdump_export_mode == RAMDUMP_MODE_SPINAND)
359 {
360#ifdef CONFIG_MTD_SPI_NAND
xf.li6b423c72025-03-14 00:07:42 -0700361 if ((ramdump_spinand_offset >= RAMDUMP_SPINAND_LEN)
362 || ((ramdump_spinand_offset + file_size) > ramdump_spinand_size))
l.yangb8fdece2024-10-10 14:56:17 +0800363 return -1;
xf.li6b423c72025-03-14 00:07:42 -0700364 /* Ended by AICoder, pid:wcfb91c2aa35add146d90b5530cd112845133621 */
365
l.yangb8fdece2024-10-10 14:56:17 +0800366 ret = ramdump_fill_header(server_to_cap->file_name,
367 server_to_cap->file_size,
368 &ramdump_spinand_fp,
369 ramdump_spinand_offset);
370#endif
371 }
372 return ret;
373}
374
375/*******************************************************************************
376* ¹¦ÄÜÃèÊö: ramdump_device_write_file
377* ²ÎÊý˵Ã÷:
378* (´«Èë²ÎÊý) fp£º Îļþ¾ä±ú
379* (´«³ö²ÎÊý) file_size Îļþ´óС
380* ·µ »Ø Öµ: ³É¹¦·µ»Ø0£¬Ê§°Ü·µ»Ø-1
381* ÆäËü˵Ã÷: This function is used for write file infomation
382*******************************************************************************/
383int ramdump_device_modify_file_size(ssize_t file_size)
384{
385 int ret = -1;
386 ramdump_file_t *fp = g_ramdump_dev_fp;
387
388 if(fp)
389 {
390 fp->file_fp[ramdump_device_file_cnt].size = file_size;
391 return 0;
392 }
393 return ret;
394}
395
396/*******************************************************************************
397* ¹¦ÄÜÃèÊö: ramdump_device_write_file_head
398* ²ÎÊý˵Ã÷:
399* (´«Èë²ÎÊý) void
400* (´«³ö²ÎÊý) void
401* ·µ »Ø Öµ: void
402* ÆäËü˵Ã÷: This function is used for write file head
403*******************************************************************************/
404int ramdump_device_write_file_head(void)
405{
406 int ret = -1;
407
408 if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
409 {
410#ifdef CONFIG_RAMDUMP_EMMC
411 ret = ramdump_emmc_write_file_head(&ramdump_device_fp);
412#endif
413 }
414 else if(ramdump_export_mode == RAMDUMP_MODE_SPINAND)
415 {
416#ifdef CONFIG_MTD_SPI_NAND
417 ret = ramdump_spinand_write_file_head(&ramdump_spinand_fp);
418#endif
419 }
420 return ret;
421}
422
423int ramdump_do_write_log_txt(ramdump_file_t *fp)
424{
425 int ret = -1;
426 size_t dst_len = 0;
427 size_t send_len = 0;
428 ramdump_shmem_t *msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
429 char *buf = NULL;
430
431 memset(ramdump_log_buf, 0, RAMDUMP_LOG_BUF);
432 ret = dump_printk_text(ramdump_log_buf, RAMDUMP_LOG_BUF);
433 if(ret < 0){
434 printk("ramdump printk log buf failed!!\n");
435 return ret;
436 }
437 if (ramdump_compress_flag == 1){
438 ret = ramdump_lzo_compress(ramdump_log_buf, RAMDUMP_LOG_BUF, msg->buf, &dst_len);
439 buf = msg->buf;
440 }
441 if (ret != LZO_E_OK){
442 dst_len = RAMDUMP_LOG_BUF;
443 buf = ramdump_log_buf;
444 }
445 fp->file_num += 1;
446 fp->file_fp[ramdump_device_file_cnt].magic = 0x3A3A3A3A;
447 strncpy(fp->file_fp[ramdump_device_file_cnt].file_name, "cap_log_buf.txt", RAMDUMP_RAMCONF_FILENAME_MAXLEN - 1);
448
449 if (fp == &ramdump_device_fp)
450 {
451#ifdef CONFIG_RAMDUMP_EMMC
452 fp->file_fp[ramdump_device_file_cnt].size = roundup(dst_len, RAMDUMP_EMMC_ALIGN_SIZE);
453 fp->file_fp[ramdump_device_file_cnt].offset = ramdump_emmc_offset;
454 ret = mmc_bwrite(RAMDUMP_EMMC_ADDR + ramdump_emmc_offset, dst_len, buf);
455 ramdump_emmc_write_file_head(fp);
456 ramdump_emmc_offset = ramdump_emmc_offset + roundup(dst_len, RAMDUMP_EMMC_ALIGN_SIZE);
457#endif
458 }
459 else if (fp == &ramdump_spinand_fp)
460 {
461#ifdef CONFIG_MTD_SPI_NAND
462 send_len = roundup(dst_len, RAMDUMP_FLASH_ALIGN_SIZE);
463 fp->file_fp[ramdump_device_file_cnt].size = send_len;
464 fp->file_fp[ramdump_device_file_cnt].offset = ramdump_spinand_offset;
465 ret = write_data(RAMDUMP_SPINAND_ADDR + ramdump_spinand_offset, send_len, buf);
466 ramdump_spinand_offset = ramdump_spinand_offset + send_len;
467#endif
468 }
469 else
470 {
471 printk("ramdump_do_write_logbuf error fp!\n");
472 return -1;
473 }
474 ramdump_device_file_cnt += 1;
475 return ret;
476}
477
478int ramdump_do_write_logbuf(ramdump_file_t *fp)
479{
480 char *buf = NULL;
481 int ret = -1;
482 size_t dst_len = 0;
483 size_t send_len = 0;
484 ramdump_shmem_t *msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
485
486 if(!fp)
487 {
488 printk("ramdump_do_write_logbuf error: fp is Null\n");
489 return -1;
490 }
491
492 ramdump_log_buf_region = log_buf_addr_get();
493 ramdump_log_buf_len = log_buf_len_get();
494
495 if (ramdump_compress_flag == 1){
496 ret = ramdump_lzo_compress(ramdump_log_buf_region, ramdump_log_buf_len, msg->buf, &dst_len);
497 buf = msg->buf;
498 }
499 if (ret != LZO_E_OK){
500 dst_len = ramdump_log_buf_len;
501 buf = ramdump_log_buf_region;
502 }
503
504 fp->file_num += 1;
505 fp->file_fp[ramdump_device_file_cnt].magic = 0x3A3A3A3A;
506 strncpy(fp->file_fp[ramdump_device_file_cnt].file_name, "cap_log_buf.bin", RAMDUMP_RAMCONF_FILENAME_MAXLEN - 1);
507
508 if (fp == &ramdump_device_fp)
509 {
510#ifdef CONFIG_RAMDUMP_EMMC
511 fp->file_fp[ramdump_device_file_cnt].size = roundup(dst_len, RAMDUMP_EMMC_ALIGN_SIZE);
512 ret = mmc_bwrite(RAMDUMP_EMMC_ADDR + ramdump_emmc_offset, dst_len, buf);
513 fp->file_fp[ramdump_device_file_cnt].offset = ramdump_emmc_offset;
514 ramdump_emmc_write_file_head(fp);
515 ramdump_emmc_offset = ramdump_emmc_offset + roundup(dst_len, RAMDUMP_EMMC_ALIGN_SIZE);
516#endif
517 }
518 else if (fp == &ramdump_spinand_fp)
519 {
520#ifdef CONFIG_MTD_SPI_NAND
521 send_len = roundup(dst_len, RAMDUMP_FLASH_ALIGN_SIZE);
522 fp->file_fp[ramdump_device_file_cnt].size = send_len;
523 fp->file_fp[ramdump_device_file_cnt].offset = ramdump_spinand_offset;
524 ret = write_data(RAMDUMP_SPINAND_ADDR + ramdump_spinand_offset, send_len, buf);
525 ramdump_spinand_offset = ramdump_spinand_offset + send_len;
526#endif
527 }
528 else
529 {
530 printk("ramdump_do_write_logbuf error fp!\n");
531 return -1;
532 }
533
534 ramdump_device_file_cnt += 1;
535 ramdump_do_write_log_txt(fp);
536
537 return ret;
538}
539
540/*******************************************************************************
541* ¹¦ÄÜÃèÊö: ramdump_device_write_logbuf
542* ²ÎÊý˵Ã÷:
543* (´«Èë²ÎÊý) void
544* (´«³ö²ÎÊý) void
545* ·µ »Ø Öµ: void
546* ÆäËü˵Ã÷: This function is used for write cap logbuf
547*******************************************************************************/
548int ramdump_device_write_logbuf(void)
549{
550 int ret = -1;
551
552 ret = ramdump_do_write_logbuf(g_ramdump_dev_fp);
553 if (ret < 0)
554 ramdump_printf("device memory trans file:cap_log_buf error!!!\n");
555 else
556 ramdump_printf("device memory trans file:cap_log_buf success!!!\n");
557 return ret;
558}
559
560/*******************************************************************************
561* ¹¦ÄÜÃèÊö: ramdump_device_write_data
562* ²ÎÊý˵Ã÷:
563* (´«Èë²ÎÊý) void
564* (´«³ö²ÎÊý) void
565* ·µ »Ø Öµ: void
566* ÆäËü˵Ã÷: This function is used for write data
567*******************************************************************************/
568int ramdump_device_write_data(ramdump_shmem_t *msg, unsigned int size, ssize_t *dstlen)
569{
570 int ret = 0;
xf.li6b423c72025-03-14 00:07:42 -0700571
l.yangb8fdece2024-10-10 14:56:17 +0800572 if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
xf.li6b423c72025-03-14 00:07:42 -0700573 {
l.yangb8fdece2024-10-10 14:56:17 +0800574#ifdef CONFIG_RAMDUMP_EMMC
575 ret = ramdump_emmc_write_data(msg, &ramdump_device_fp, size);
576 if(ret < 0)
577 *dstlen = 0;
578 else
579 *dstlen += roundup(ret, RAMDUMP_EMMC_ALIGN_SIZE);
580#endif
581 }
582 else if(ramdump_export_mode == RAMDUMP_MODE_SPINAND)
583 {
584#ifdef CONFIG_MTD_SPI_NAND
585 ret = ramdump_spinand_write_data(msg, &ramdump_spinand_fp, size);
586 if(ret < 0)
587 *dstlen = 0;
588 else
589 *dstlen += ret;
590#endif
591 }
592 else
593 return 0;
594 return ret;
595}
596
597/*******************************************************************************
598 * Global function implementations *
599 ******************************************************************************/
600void ramdump_shared_mem_init(void)
601{
602 ramdump_shared_mem_base = ramdump_phy_to_vir((unsigned long)RAMDUMP_SHARED_MEM_BASE, (unsigned long)RAMDUMP_MEM_LEN);
603 ramdump_export_flag_base = ramdump_phy_to_vir((unsigned long)IRAM_BASE_ADDR_RAMDUMP_MODE, sizeof(unsigned long));
604 ramdump_log_buf = ramdump_shared_mem_base + 0x4000;
605 ramdump_flash_alloc_transbuf();
606}
607
608/*******************************************************************************
609* ¹¦ÄÜÃèÊö: ramdump_data_transfer_to_device
610* ²ÎÊý˵Ã÷:
611* (´«Èë²ÎÊý) void
612* (´«³ö²ÎÊý) void
613* ·µ »Ø Öµ: void
614* ÆäËü˵Ã÷: This function is used for ramdump to trans dump data to ap
615*******************************************************************************/
616void ramdump_data_transfer_to_device(void)
617{
618 int data_trans_max;
619 int file_cnt = 0;
620 int file_size = 0;
621 int file_offset = 0;
622 int file_left_size = 0;
623 int file_trans_size = 0;
624 int error_cmd = 0;
625 int ret = 0;
626 ssize_t file_dstlen = 0;
627
628 unsigned int req_buf[RAMDUMP_INTERACTIVE_ARRAY_LEN] = {0};
629 ramdump_trans_server_interactive_req cap_to_server_msg = {0};
630
631 /* interactive begin */
632 if(ramdump_device_init() < 0)
633 return;
l.yangafee7ee2024-10-10 15:01:10 +0800634
635 ramdump_device_write_logbuf();
l.yangb8fdece2024-10-10 14:56:17 +0800636 data_trans_max = RAMDUMP_SHARED_MEM_LEN - roundup(sizeof(ramdump_shmem_t), RAMDUMP_SHMEM_ALIGN_SIZE) - RAMDUMP_COMPRESS_OUT_LEN;
637 cap_to_server_msg.cmd = RAMDUMP_PC_INTERACTIVE_REQ;
638 ramdump_oss_data_trans_write((unsigned char*)(&cap_to_server_msg), sizeof(cap_to_server_msg));
639
640 for(;;)
641 {
642 ramdump_oss_data_trans_read((unsigned char *)req_buf, RAMDUMP_INTERACTIVE_DATA_LEN);
643 switch (*(unsigned int *)req_buf)
644 {
645 case RAMDUMP_TRANS_SERVER_INTERACTIVE_RSP:
646 {
647 ramdump_pc_file_info_rsp cap_to_server_msg ={0};
648 ramdump_trans_server_interactive_req *server_to_cap_msg = (ramdump_trans_server_interactive_req *)req_buf;
649 /* data from server to cap */
650 ramdump_file_num = server_to_cap_msg->file_num;
l.yangb8fdece2024-10-10 14:56:17 +0800651
652 /* data from cap to server */
653 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_INFO_READ_REQ;
654 cap_to_server_msg.file_id = file_cnt;
655
656 ramdump_oss_data_trans_write(
657 (unsigned char*)(&cap_to_server_msg),
658 sizeof(cap_to_server_msg));
659
660 break;
661 }
662 case RAMDUMP_TRANS_SERVER_FILE_INFO_READ_RSP:
663 {
664 ramdump_pc_trans_data_rsp cap_to_server_msg = {0};
665 ramdump_trans_server_file_info_req *server_to_cap_msg = (ramdump_trans_server_file_info_req *)req_buf;
666 /* data from server to cap */
667 /*device memory file create*/
668 if(ramdump_device_write_file(server_to_cap_msg) == -1){
669 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_TRANS_DONE_REQ;
xf.li6b423c72025-03-14 00:07:42 -0700670 /* Started by AICoder, pid:ddd3ag3c37x6798145ec08ac1067150b58735197 */
671 ramdump_oss_data_trans_write(
672 (unsigned char*)(&cap_to_server_msg),
673 sizeof(cap_to_server_msg));
674 break;
675 /* Ended by AICoder, pid:ddd3ag3c37x6798145ec08ac1067150b58735197 */
l.yangb8fdece2024-10-10 14:56:17 +0800676 }
677 file_size = server_to_cap_msg->file_size;
678 file_offset = 0;
679 file_left_size = file_size;
680 /* data from cap to server */
681 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_DATA_TRANS_REQ;
682 cap_to_server_msg.file_id = file_cnt;
683 cap_to_server_msg.offset = file_offset;
684 if (file_size >= data_trans_max)
685 cap_to_server_msg.length = data_trans_max;
686 else
687 cap_to_server_msg.length = file_size;
688 file_trans_size = cap_to_server_msg.length;
689 file_left_size = file_left_size - cap_to_server_msg.length;
690 file_offset = file_offset + cap_to_server_msg.length;
xf.li6b423c72025-03-14 00:07:42 -0700691
692 printk("device memory trans file:%-30s size %9d, offset %9d!!!\n", server_to_cap_msg->file_name, file_size, ramdump_emmc_offset);
l.yangb8fdece2024-10-10 14:56:17 +0800693 /* interactive data trans */
694 ramdump_oss_data_trans_write(
695 (unsigned char*)(&cap_to_server_msg),
696 sizeof(cap_to_server_msg));
697
698 break;
699 }
700 case RAMDUMP_TRANS_SERVER_FILE_DATA_TRANS_RSP:
701 {
702 int write_len = 0;
703 ramdump_pc_trans_data_rsp cap_to_server_msg = {0};
704 /* data from server to cap */
705 ramdump_shmem_t *server_to_cap_msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
706 server_to_cap_msg->core_flag = 0;
xf.li6b423c72025-03-14 00:07:42 -0700707
l.yangb8fdece2024-10-10 14:56:17 +0800708 /*data from cap to emmc*/
l.yangb8fdece2024-10-10 14:56:17 +0800709 write_len = ramdump_device_write_data(server_to_cap_msg, file_left_size, &file_dstlen);
xf.li6b423c72025-03-14 00:07:42 -0700710 if(write_len < 0 )
l.yangb8fdece2024-10-10 14:56:17 +0800711 {
xf.li6b423c72025-03-14 00:07:42 -0700712 /* Started by AICoder, pid:u5befs8483y615f142ce0bda306d660bed685275 */
713 if(write_len == -RAMDUMP_NO_FREE_SPACE)
714 {
715 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_TRANS_DONE_REQ;
716 ramdump_oss_data_trans_write(
717 (unsigned char*)(&cap_to_server_msg),
718 sizeof(cap_to_server_msg));
719 break;
720 }
721 else
722 ramdump_printf("ramdump write emmc data error!\n");
723 /* Ended by AICoder, pid:u5befs8483y615f142ce0bda306d660bed685275 */
l.yangb8fdece2024-10-10 14:56:17 +0800724 }
l.yangb8fdece2024-10-10 14:56:17 +0800725 /*ÅжÏÊ£Óà´óС*/
726 if (file_left_size == 0)
727 {
728 file_cnt++;
729 if (file_cnt == ramdump_file_num)
730 {
731 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_TRANS_DONE_REQ;
732 }
733 else
734 {
735 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_INFO_READ_REQ;
736 cap_to_server_msg.file_id = file_cnt;
737 }
738 ramdump_device_modify_file_size(file_dstlen);
739 file_dstlen = 0;
740 ramdump_device_file_cnt++;
741 }
742 else
743 {
744 /* data from cap to server */
745 if (file_left_size >= data_trans_max)
746 cap_to_server_msg.length = data_trans_max;
747 else
748 cap_to_server_msg.length = file_left_size;
749 cap_to_server_msg.cmd = RAMDUMP_PC_FILE_DATA_TRANS_REQ;
750 cap_to_server_msg.file_id = file_cnt;
751 cap_to_server_msg.offset = file_offset;
752 file_left_size = file_left_size - cap_to_server_msg.length;
753 file_offset= file_offset + cap_to_server_msg.length;
754 }
755
756 ramdump_oss_data_trans_write((unsigned char *)(&cap_to_server_msg), sizeof(cap_to_server_msg));
757 continue;
758 }
759 case RAMDUMP_TRANS_SERVER_FILE_TRANS_DONE_RSP:
760 {
l.yangb8fdece2024-10-10 14:56:17 +0800761 ramdump_device_close();
762 return;
763 }
764 default:
765 {
766 error_cmd = RAMDUMP_INTERACTIVE_CMD_ERROR;
767 ramdump_printf("ramdump trans emmc error:%d!\n", error_cmd);
768 /* interactive data trans */
769 break;
770 }
771 }
772 }
773}
774
775#ifdef __cplusplus
776}
777#endif
778