1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include "aos/init.h"
10 #include "board.h"
11 #include <k_api.h>
12 #include "aos/cli.h"
13 #define SDTEST_FILE_NAME "/sdcard/sdtest.test"
14 #define SDTEST_FILE_LEN (1024 * 1024)
15 #define SDTEST_WORDS "sdtest\r\n"
16
17 static FILE *testfd = NULL;
18
sdtest_write(void)19 static int sdtest_write(void)
20 {
21 int i = 0;
22 int j = 0;
23 int ret = 0;
24 char *data = NULL;
25 int readbytes = 0;
26 int writebytes = 0;
27 int words_len = strlen(SDTEST_WORDS);
28
29 printf("\n====sd write test start ... ===\n");
30
31 testfd = fopen(SDTEST_FILE_NAME, "w");
32 if (testfd == NULL) {
33 printf("open /sdcard/test fail !!!\n");
34 printf("====Result: sd write test FAIL !!!===\n");
35 return -1;
36 }
37
38 printf("begin write zero test\n");
39 data = (char *) malloc(words_len);
40 for (i = 0; i < SDTEST_FILE_LEN / words_len; i++) {
41 memset(data, 0, words_len);
42 writebytes = fwrite(data, 1, words_len, testfd);
43 if (writebytes != words_len) {
44 printf("sdtest write %d bytes to %s failed, return %d\n", words_len, SDTEST_FILE_NAME, writebytes);
45 printf("====Result: sd write test FAIL !!!===\n");
46 free(data);
47 fclose(testfd);
48 return -1;
49 }
50 }
51 fclose(testfd);
52 free(data);
53
54 testfd = fopen(SDTEST_FILE_NAME, "r");
55 if (testfd == NULL) {
56 printf("open /sdcard/test fail !!!\n");
57 printf("====Result: sd write test FAIL !!!===\n");
58 return -1;
59 }
60
61 data = (char *) malloc(SDTEST_FILE_LEN);
62 memset(data, 0, SDTEST_FILE_LEN);
63 readbytes = fread(data, 1, SDTEST_FILE_LEN, testfd);
64 if (readbytes != SDTEST_FILE_LEN) {
65 printf("sdtest read %d bytes form %s failed, return %d\n", SDTEST_FILE_LEN, SDTEST_FILE_NAME, readbytes);
66 printf("====Result: sd write test FAIL !!!===\n");
67 free(data);
68 fclose(testfd);
69 return -1;
70 }
71
72 for (i = 0; i < SDTEST_FILE_LEN; i++) {
73 if (data[i] != 0) {
74 printf("sdtest read %d bytes form %s failed, data[%d] %d\n", SDTEST_FILE_LEN, SDTEST_FILE_NAME, i, (int)data[i]);
75 printf("====Result: sd write test FAIL !!!===\n");
76 free(data);
77 fclose(testfd);
78 return -1;
79 }
80 }
81 fclose(testfd);
82
83 testfd = fopen(SDTEST_FILE_NAME, "w");
84 if (testfd == NULL) {
85 printf("open /sdcard/test fail !!!\n");
86 printf("====Result: sd write test FAIL !!!===\n");
87 free(data);
88 return -1;
89 }
90
91 for (i = 0; i < SDTEST_FILE_LEN / strlen(SDTEST_WORDS); i++) {
92 memcpy(data + i * strlen(SDTEST_WORDS), SDTEST_WORDS, strlen(SDTEST_WORDS));
93 }
94
95 printf("begin write data test\n");
96 writebytes = fwrite(data, 1, SDTEST_FILE_LEN, testfd);
97 if (writebytes != SDTEST_FILE_LEN) {
98 printf("write data SDTEST_FILE_LEN bytes to file /sdcard/test fail !!!\n");
99 printf("====Result: sd write test FAIL !!!===\n");
100 free(data);
101 fclose(testfd);
102 return -1;
103 }
104
105 printf("====Result: sd write test PASS !!!===\n");
106 free(data);
107 fclose(testfd);
108
109 return 0;
110 }
111
sdtest_read(void)112 static int sdtest_read(void)
113 {
114 int i = 0;
115 int j = 0;
116 int ret = 0;
117 char *data = NULL;
118 int readbytes = 0;
119 int words_len = strlen(SDTEST_WORDS);
120
121 printf("\r\n====sd read test start ... ===\n");
122
123 testfd = fopen(SDTEST_FILE_NAME, "r");
124 if (testfd == NULL) {
125 printf("open /sdcard/test fail !!!\n");
126 printf("====Result: sd read test FAIL !!!===\n");
127 return -1;
128 }
129
130 data = (char *) malloc(words_len);
131 for (i = 0; i < SDTEST_FILE_LEN / words_len; i++) {
132 memset(data, 0, words_len);
133 readbytes = fread(data, 1, words_len, testfd);
134 if (readbytes != words_len) {
135 printf("sdtest read %d bytes form %s failed, return %d\n", words_len, SDTEST_FILE_NAME, readbytes);
136 ret = -1;
137 goto test_err;
138 }
139
140 if (0 != strncmp(data, SDTEST_WORDS, words_len)) {
141 printf("sdtest read %d bytes form %s, data error\n", words_len, SDTEST_FILE_NAME);
142 printf("read ");
143 for (j = 0; j < words_len; j++) {
144 printf("0x%x", (uint32_t)data[j]);
145 }
146 printf(", ");
147 printf("actual %s\n", SDTEST_WORDS);
148 ret = -1;
149 goto test_err;
150 }
151 }
152
153 printf("====Result: sd read test PASS !!!===\n");
154 test_err:
155 free(data);
156 fclose(testfd);
157 if (ret != 0) {
158 printf("====Result: sd read test FAIL !!!===\n\n");
159 }
160 return ret;
161 }
162
handle_sdtest_cmd(char * pwbuf,int blen,int argc,char ** argv)163 static void handle_sdtest_cmd(char *pwbuf, int blen, int argc, char **argv)
164 {
165 if (argc < 2) {
166 printf("Usage: sdtest read/write\n");
167 return;
168 }
169
170 if (strcmp(argv[1], "read") == 0) {
171 sdtest_read();
172 } else if (strcmp(argv[1], "write") == 0) {
173 sdtest_write();
174 } else {
175 printf("Usage: sdtest read/write\n");
176 return;
177 }
178 }
179
180 static struct cli_command sdtest_cmd = {
181 .name = "sdtest",
182 .help = "sdtest [read/write]",
183 .function = handle_sdtest_cmd
184 };
185
sdcard_test(void)186 int sdcard_test(void)
187 {
188 int i = 0;
189 int ret = 0;
190 char *data = NULL;
191
192 printf("\r\n\r\n");
193 printf("***************************************************************\r\n");
194 printf("*********************** sdcard Test ***************************\r\n");
195 printf("** How to test: pls format sdcard and insert it to socket *****\r\n");
196 printf("***************************************************************\r\n");
197 printf("=====sdcard test : Start=====\r\n");
198
199 aos_cli_register_command(&sdtest_cmd);
200
201 aos_unlink(SDTEST_FILE_NAME);
202 aos_rmdir(SDTEST_FILE_NAME);
203
204 testfd = fopen(SDTEST_FILE_NAME, "w");
205 if (testfd == NULL) {
206 printf("create /sdcard/sdtest.test fail !!!\n");
207 return;
208 }
209
210 printf("create /sdcard/sdtest.test success !!!\n");
211 data = (char *)malloc(SDTEST_FILE_LEN);
212 for (i = 0; i < SDTEST_FILE_LEN / strlen(SDTEST_WORDS); i++) {
213 memcpy(data + i * strlen(SDTEST_WORDS), SDTEST_WORDS, strlen(SDTEST_WORDS));
214 }
215
216 printf("begin init /sdcard/sdtest.test ...\n");
217 ret = fwrite(data, 1, SDTEST_FILE_LEN, testfd);
218 if (ret != SDTEST_FILE_LEN) {
219 printf("write data SDTEST_FILE_LEN bytes to file /sdcard/sdtest.test fail !!!\n");
220 }
221 fsync(testfd);
222
223 printf("write init data to /sdcard/sdtest.test success !!!\n");
224 fclose(testfd);
225 free(data);
226
227 aos_msleep(1000);
228
229 ret = sdtest_read();
230 ret += sdtest_write();
231
232 aos_unlink(SDTEST_FILE_NAME);
233
234 return ret;
235 }
236