blob: 07356c5aaa6a7bcf2295f8b743b29f166f219a9f [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <time.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <sys/ioctl.h>
8#include <mtd/mtd-user.h>
9#include <errno.h>
10
11#include "gsw_oem_rw_interface.h"
12
13
14#define FLASH_DEVICE "/dev/mtd32"
15
16
17
18#define TEST_BLOCK_ID 1 // 测试使用的block ID
19
20void print_usage(void)
21{
22 printf("Usage:\n");
23 printf(" 1: Write test data to block\n");
24 printf(" 2: Read data from block\n");
25 printf(" 0: Exit\n");
26 printf("Please input your choice: ");
27}
28
29int write_test(void)
30{
31 int ret;
32 struct mtd_info_user mtd_info;
33 int fd;
34 char *write_buf = NULL;
35 unsigned int max_write_size;
36
37
38 fd = open(FLASH_DEVICE, O_RDONLY);
39 if (fd == -1) {
40 printf("Error opening flash device: %s\n", strerror(errno));
41 return GSW_HAL_NORMAL_FAIL;
42 }
43
44 if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
45 printf("Error getting MTD info: %s\n", strerror(errno));
46 close(fd);
47 return GSW_HAL_NORMAL_FAIL;
48 }
49
50 close(fd);
51
52 max_write_size = mtd_info.erasesize;
53 printf("MTD erase size: %u bytes\n", max_write_size);
54
55
56 write_buf = (char *)malloc(max_write_size);
57 if (write_buf == NULL)
58 {
59 printf("Failed to allocate write buffer\n");
60 return -1;
61 }
62
63 int block_id = -1;
64 printf("enter block id \n");
65 ret = scanf("%d",&block_id);
66 memset(write_buf, 0, max_write_size);
67 snprintf(write_buf, max_write_size, "Test data for block %d, timestamp: %d",
68 block_id, (int)time(NULL));
69 printf("Write data: %s\n", write_buf);
70 printf("Writing data to block %d...\n", block_id);
71 ret = gsw_oem_write_data_ext(block_id, write_buf, max_write_size);
72 if (ret != GSW_HAL_SUCCESS)
73 {
74 printf("Write failed with error: %d\n", ret);
75 }
76 else
77 {
78 printf("Write successful\n");
79 }
80
81 free(write_buf);
82 return ret;
83}
84
85int read_test(void)
86{
87 int ret;
88 unsigned int read_len = 0;
89 struct mtd_info_user mtd_info;
90 int fd;
91 char *read_buf = NULL;
92 unsigned int max_read_size;
93
94
95 fd = open(FLASH_DEVICE, O_RDONLY);
96 if (fd == -1) {
97 printf("Error opening flash device: %s\n", strerror(errno));
98 return GSW_HAL_NORMAL_FAIL;
99 }
100
101 if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
102 printf("Error getting MTD info: %s\n", strerror(errno));
103 close(fd);
104 return GSW_HAL_NORMAL_FAIL;
105 }
106
107 close(fd);
108
109 max_read_size = mtd_info.erasesize;
110 printf("MTD erase size: %u bytes\n", max_read_size);
111
112
113 read_buf = (char *)malloc(max_read_size);
114 if (read_buf == NULL)
115 {
116 printf("Failed to allocate read buffer\n");
117 return -1;
118 }
119
120 memset(read_buf, 0, max_read_size);
121
122 int block_id = -1;
123 printf("enter block id \n");
124 ret = scanf("%d",&block_id);
125 printf("Reading data from block %d...\n", block_id);
126
127 ret = gsw_oem_read_data_ext(block_id, read_buf, &read_len);
128 if (ret != GSW_HAL_SUCCESS)
129 {
130 printf("Read failed with error: %d\n", ret);
131 }
132 else
133 {
134 printf("Read %u bytes\n", read_len);
hj.shao340f2a82025-08-05 01:15:41 -0700135 if (read_buf != NULL) {
136 int pos = -1;
137 for (int i = 0; i < read_len; i++) {
138 unsigned char c = ((unsigned char*)read_buf)[i];
139 if (c < 32 || c > 126) {
140 pos = i;
141 ((unsigned char*)read_buf)[i] = '.';
142 break;
143 }
144 }
145 printf("Read data: %s\n", (char*)read_buf);
146 }
147 else
148 {
149 printf("Read data: \n");
150 }
b.liu68a94c92025-05-24 12:53:41 +0800151 }
152 free(read_buf);
153 return ret;
154}
155
156int main()
157{
158 int choice;
159 int ret = 0;
160
161 while (1)
162 {
163 print_usage();
164 if (scanf("%d", &choice) != 1)
165 {
166 printf("Invalid input\n");
167
168 while (getchar() != '\n');
169 continue;
170 }
171
172
173 while (getchar() != '\n');
174
175 switch (choice)
176 {
177 case 0:
178 printf("Exit\n");
179 return 0;
180 case 1:
181 ret = write_test();
182 break;
183 case 2:
184 ret = read_test();
185 break;
186 default:
187 printf("Invalid choice\n");
188 break;
189 }
190
191 printf("\n");
192 }
193
194 return ret;
195}
196