1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 
9 #include <epoll.h>
10 
11 #include <poll.h>
12 #include <errno.h>
13 
14 #include <string.h>
15 #include <sys/ioctl.h>
16 #include <aos/vfs.h>
17 #include <aos/hal/flash.h>
18 #include <vfsdev/flash_dev.h>
19 #include <drivers/char/u_device.h>
20 #include <drivers/u_ld.h>
21 
22 #if AOS_COMP_CLI
23 #include "aos/cli.h"
24 #endif
25 
vfs_flash_test(unsigned int flash_id)26 int vfs_flash_test(unsigned int flash_id)
27 {
28     char buffer[16];
29     int ret = 0;
30     int fd = -1;
31     char dev_str[16];
32 
33     int epfd = epoll_create(10 + 1);
34     hal_logic_partition_t part_info;
35 
36     snprintf(dev_str,16-1,"/dev/flash%d", flash_id);
37 
38     fd = open(dev_str, 0);
39     ddkc_info("open flash %s %s, fd:%d, epfd:%d\r\n", dev_str, fd >= 0 ? "success" : "fail", fd, epfd);
40 
41     if (fd >= 0) {
42 
43         int n = 0;
44         int nfds = 0;
45         struct epoll_event ev;
46         struct epoll_event events[10 + 1];
47 
48         ev.data.fd = fd;
49         ev.events = EPOLLIN | EPOLLOUT;
50         ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
51         ddkc_info("epoll_ctl return %d\r\n", ret);
52 
53         ret = lseek(fd, 0, SEEK_SET);
54         ddkc_info("lseek of flash return %d\r\n", ret);
55         ret = ioctl(fd, IOC_FLASH_ERASE_FLASH, strlen("test"));
56         ddkc_info("ioctl of flash return %d\r\n", ret);
57 
58         ret = lseek(fd, 0, SEEK_SET);
59         ddkc_info("lseek of flash return %d\r\n", ret);
60         ret = write(fd, "test", strlen("test"));
61         ddkc_info("write to flash return %d\r\n", ret);
62         if (ret != strlen("test")) {
63             ddkc_err("write to flash failed, ret:%d, target len:%d\r\n", ret, strlen("test"));
64             goto err;
65         }
66 
67         ret = lseek(fd,0,SEEK_SET);
68         ddkc_info("lseek of flash return %d\r\n", ret);
69         ret = read(fd, buffer, sizeof(buffer));
70         ddkc_info("read from flash return %d\r\n", ret);
71         if(ret != sizeof(buffer)){
72             ddkc_err("read from flash failed, ret:%d, target size:%d\r\n", ret, sizeof(buffer));
73             goto err;
74         }
75         buffer[15] = '\0';
76         ret = strncmp("test", buffer, strlen("test"));
77         if(ret){
78             ddkc_err("flash read failed, buffer:%s\r\n", buffer);
79             goto err;
80         }
81 
82         nfds = epoll_wait(epfd, events, 10, 1000);
83         if (nfds > 0) {
84             if (events[n].events & EPOLLIN) {
85                 ddkc_info("EPOLLIN received\r\n");
86                 ret = read(fd, buffer, sizeof(buffer));
87                 ddkc_info("read from ttyUART return %d\r\n", ret);
88 
89             } else if (events[n].events & EPOLLOUT) {
90                 ddkc_info("EPOLLOUT received\r\n");
91             }
92         }
93 
94         ddkc_info("ioctl with IOC_FLASH_INFO_GET\r\n");
95         ioctl(fd, IOC_FLASH_INFO_GET, (unsigned long)&part_info);
96         ddkc_info("ioctl on flash get partition info, description:%s\r\n", part_info.partition_description ? part_info.partition_description : "NULL");
97         ddkc_info("ioctl on flash get partition info, start_addr:0x%x\r\n", part_info.partition_start_addr);
98         ddkc_info("ioctl on flash get partition info, length:0x%x\r\n", part_info.partition_length);
99         ddkc_info("ioctl with IOC_FLASH_GET_BOOT_TYPE\r\n");
100         ret = ioctl(fd, IOC_FLASH_GET_BOOT_TYPE, 0);
101         ddkc_info("ioctl with IOC_FLASH_GET_BOOT_TYPE ret:%d\r\n", ret);
102 
103         close(epfd);
104         close(fd);
105         fd = -1;
106         epfd = -1;
107 
108         return 0;
109     } else {
110         ddkc_err("flash open failed, buffer:%s\r\n", buffer);
111         ret = -EIO;
112     }
113 
114 err:
115     if (fd >= 0)
116         close(fd);
117 
118     if (epfd >= 0)
119         close(epfd);
120 
121     return ret;
122 }
123 
flash_unit_test(int argc,char ** argv)124 static void flash_unit_test(int argc, char **argv)
125 {
126     int ret = 0;
127     unsigned int loops = 1;
128     unsigned int flash_id = HAL_PARTITION_OTA_TEMP;
129 
130     if (argc <= 1) {
131         ddkc_err("usage: flash_example <loops>\r\n");
132         return;
133     }
134 
135     flash_id = atoi(argv[1]);
136     loops = argc > 2 ? atoi(argv[2]) : 1;
137 
138     ddkc_info("flash_id:%d, loops:%d\r\n", flash_id, loops);
139 
140     while (loops--) {
141         ret = vfs_flash_test(flash_id);
142         if (ret) {
143             ddkc_err("vfs_flash_test return %d\r\n", ret);
144         } else {
145             ddkc_info("vfs_flash_test success!\r\n");
146         }
147     }
148 
149     return;
150 }
151 
152 #if AOS_COMP_CLI
153 /* reg args: fun, cmd, description*/
154 ALIOS_CLI_CMD_REGISTER(flash_unit_test, flash_example, flash vfs example)
155 #endif
156