1 /*
2 * emmc_test.c -- emmc test application
3 *
4 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5 * Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include "unistd.h"
28
29 #include "emmc_test.h"
30
31 #define LOG_TAG "emmc_test"
32 #include "common.h"
33
34
35 #ifdef PCBA_PX3SE
36 #define EMMCPATH "/sys/bus/mmc/devices/mmc0:1234/block/mmcblk0/size"
37 #elif defined PCBA_356X
38 #define EMMCPATH "/sys/bus/mmc/devices/mmc2:0001/block/mmcblk2/size"
39 #else
40 #define EMMCPATH "/sys/bus/mmc/devices/mmc0:0001/block/mmcblk0/size"
41 #endif
42
43 #define READ_DDR_COMMAND "cat /proc/zoneinfo | busybox grep present | \
44 busybox awk 'BEGIN{a=0}{a+=$2}END{print a}'"
45
46 #define RKNAND_TEST_CMD "cat /proc/rknand > %s"
47 #define RKNAND_TEST_FILE "/tmp/rknand_test.txt"
48
49
50 /* for emmc */
readFromFile(const char * path,char * emmcsize_char,size_t size)51 static int readFromFile(const char *path, char *emmcsize_char, size_t size)
52 {
53 if (!path)
54 return -1;
55
56 int fd = open(path, O_RDONLY|O_NOCTTY|O_NDELAY, 0);
57 if (fd == -1) {
58 printf("open '%s' failed!\n", path);
59 return -1;
60 }
61 ssize_t count = read(fd, emmcsize_char, size);
62
63 if (count > 0) {
64 while (count > 0 && emmcsize_char[count-1] == '\n')
65 count--;
66 emmcsize_char[count] = '\0';
67 } else {
68 emmcsize_char[0] = '\0';
69 }
70 close(fd);
71 return count;
72 }
73
get_emmc_size(char * size_data)74 int get_emmc_size(char *size_data)
75 {
76 int count;
77 int emmc_size=1;
78 double size = (double)(atoi(size_data))/2/1024/1024;
79
80 if (size > 0 && size <= 1) /*1 GB */
81 return 1;
82 for (count = 1; count < 10; count++)
83 {
84 if ((size > emmc_size) && (size <= emmc_size*2)) /*2 - 512 GB*/
85 return emmc_size*2;
86
87 emmc_size *=2;
88 }
89 return -1;
90 }
91
92 #ifdef PCBA_PX3SE//emmc
emmc_test(void * argv)93 void *emmc_test(void *argv)
94 {
95 int emmc_ret = 0;
96 char emmcsize_char[20];
97 int emmc_size = 0;
98
99 int ddr_ret = 0;
100 char ddrsize_char[20];
101 int ddr_size = 0;
102 char cmd[128];
103
104 printf("======= emmc test starting ========\n");
105 //sprintf(cmd,"aplay %s/emmc_test_start.wav",AUDIO_PATH);
106 //system(cmd);
107 //system("aplay /data/test/emmc_test_start.wav");
108 /* For emmc */
109 memset(emmcsize_char, 0, sizeof(emmcsize_char));
110 emmc_ret = readFromFile(EMMCPATH, emmcsize_char, sizeof(emmcsize_char));
111
112 printf("readFromFile effective bytes is %d \n",emmc_ret);
113 if (emmc_ret >= 0) { /*read back normal*/
114 emmc_size = get_emmc_size(emmcsize_char);
115 if(emmc_size < 0){
116 emmc_ret = -1;
117 goto fail;
118 }
119 printf("======= emmc_size is: %d GB ========\n",emmc_size);
120 if (EMMC_CAPACITY != emmc_size)
121 {
122 goto fail;
123 }
124 }
125 else
126 {
127 goto fail;
128 }
129 printf("======= emmc_test success ========\n");
130
131 return (void*)emmc_ret;
132 fail:
133 printf("======= emmc_test failed ========\n");
134
135 return (void*)emmc_ret;
136 }
137 #endif
138
139 #ifdef PCBA_3308
emmc_test(void * argv)140 void *emmc_test(void *argv)
141 {
142 char cmd[128];
143 char buf[128];
144 FILE *fp;
145 int emmc_ret = -1;
146
147 printf("======= emmc test starting ========\n");
148
149 sprintf(cmd,RKNAND_TEST_CMD,RKNAND_TEST_FILE);
150 system(cmd);
151 printf("cmd is: %s.\n",cmd);
152 sleep(1);
153
154 fp = fopen(RKNAND_TEST_FILE, "r");
155 if (!fp)
156 {
157 printf("%s fopen err:%s\n",__func__,strerror(errno));
158 return (void*)-1;
159 }
160
161 while(!feof(fp))
162 {
163 fgets(buf,sizeof(buf),fp);
164 if(strstr(buf,"Device Capacity: 256 MB")!=NULL)
165 {
166 emmc_ret = 0;
167 break;
168 }
169 }
170 fclose(fp);
171 remove(RKNAND_TEST_FILE);
172
173 printf("\n=================== function :%s finish======================\n",__func__);
174 return (void*)emmc_ret;
175 }
176 #endif
177
178 #ifdef PCBA_1808
179 // TODO: add 1808 emmc test code here.
emmc_test(void * argv)180 void *emmc_test(void *argv)
181 {
182 return 0;
183 }
184
185 #endif
186
187 #ifdef PCBA_3326
188 // TODO: add 3326 emmc test code here.
emmc_test(void * argv)189 void *emmc_test(void *argv)
190 {
191 return 0;
192 }
193
194 #endif
195
196 #ifdef PCBA_PX30
197 // TODO: add PX30 emmc test code here.
emmc_test(void * argv)198 void *emmc_test(void *argv)
199 {
200 return 0;
201 }
202 #endif
203
204 #ifdef PCBA_3288
205 // TODO: add 3288 emmc test code here.
emmc_test(void * argv)206 void *emmc_test(void *argv)
207 {
208 return 0;
209 }
210 #endif
211
212 #ifdef PCBA_3328
213 // TODO: add 3328 emmc test code here.
emmc_test(void * argv)214 void *emmc_test(void *argv)
215 {
216 return 0;
217 }
218 #endif
219
220 #ifdef PCBA_3399
221 // TODO: add 3399 emmc test code here.
emmc_test(void * argv)222 void *emmc_test(void *argv)
223 {
224 return 0;
225 }
226
227 #endif
228
229 #ifdef PCBA_3399PRO
230 // TODO: add 3399PRO emmc test code here.
emmc_test(void * argv)231 void *emmc_test(void *argv)
232 {
233 return 0;
234 }
235 #endif
236
237 #ifdef PCBA_1126_1109
238 // TODO: add rv1126/1109 emmc test code here.
emmc_test(void * argv)239 void *emmc_test(void *argv)
240 {
241 return 0;
242 }
243 #endif
244
245 #ifdef PCBA_356X
246 // TODO: add 356X emmc test code here.
emmc_test(void * argv)247 void *emmc_test(void *argv)
248 {
249 int emmc_ret = 0;
250 char emmcsize_char[20];
251 int emmc_size = 0;
252
253 int ddr_ret = 0;
254 char ddrsize_char[20];
255 int ddr_size = 0;
256 char cmd[128];
257
258 printf("======= emmc test starting ========\n");
259 //sprintf(cmd,"aplay %s/emmc_test_start.wav",AUDIO_PATH);
260 //system(cmd);
261 //system("aplay /data/test/emmc_test_start.wav");
262 /* For emmc */
263 memset(emmcsize_char, 0, sizeof(emmcsize_char));
264 emmc_ret = readFromFile(EMMCPATH, emmcsize_char, sizeof(emmcsize_char));
265
266 printf("readFromFile effective bytes is %d \n",emmc_ret);
267 if (emmc_ret >= 0) { /*read back normal*/
268 emmc_size = get_emmc_size(emmcsize_char);
269 if(emmc_size < 0){
270 emmc_ret = -1;
271 goto fail;
272 }
273 printf("======= emmc_size is: %d GB ========\n",emmc_size);
274 if (EMMC_CAPACITY != emmc_size)
275 {
276 goto fail;
277 }
278 }
279 else
280 {
281 goto fail;
282 }
283 printf("======= emmc_test success ========\n");
284
285 return (void*)emmc_ret;
286 fail:
287 printf("======= emmc_test failed ========\n");
288
289 return (void*)emmc_ret;
290 }
291 #endif
292
293 #ifdef PCBA_3588
294 // TODO: add 3588 emmc test code here.
emmc_test(void * argv)295 void *emmc_test(void *argv)
296 {
297 return 0;
298 }
299 #endif
300
main(int argc,char * argv[])301 int main(int argc, char *argv[])
302 {
303 int test_flag = 0,err_code = 0;
304 char buf[COMMAND_VALUESIZE] = "emmc_test";
305 char result[COMMAND_VALUESIZE] = RESULT_PASS;
306 test_flag = (int)emmc_test(argv[0]);
307 if(test_flag < 0)
308 {
309 strcpy(result,RESULT_FAIL);
310 err_code = EMMC_PROC_ERR;
311 }
312 send_msg_to_server(buf, result, err_code);
313 }
314