blob: 723c0c6081f5cd03c8f1971efaea119ae19ee79b [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);
135 printf("Read data: %s\n", read_buf);
136 }
137 free(read_buf);
138 return ret;
139}
140
141int main()
142{
143 int choice;
144 int ret = 0;
145
146 while (1)
147 {
148 print_usage();
149 if (scanf("%d", &choice) != 1)
150 {
151 printf("Invalid input\n");
152
153 while (getchar() != '\n');
154 continue;
155 }
156
157
158 while (getchar() != '\n');
159
160 switch (choice)
161 {
162 case 0:
163 printf("Exit\n");
164 return 0;
165 case 1:
166 ret = write_test();
167 break;
168 case 2:
169 ret = read_test();
170 break;
171 default:
172 printf("Invalid choice\n");
173 break;
174 }
175
176 printf("\n");
177 }
178
179 return ret;
180}
181