1 /*
2  * Copyright (c) 2023 Cody Wong
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <app/tests.h>
9 #include <lk/err.h>
10 #include <lk/debug.h>
11 
12 #define FID_ROOT 1
13 #define FID_RO 2
14 
15 #define _LOGF(fmt, args...) \
16     printf("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__, ##args)
17 #define LOGF(x...) _LOGF(x)
18 
19 #if WITH_DEV_VIRTIO_9P
20 #include <dev/virtio/9p.h>
21 #include <dev/virtio.h>
22 
v9p_tests(int argc,const console_cmd_args * argv)23 int v9p_tests(int argc, const console_cmd_args *argv) {
24     struct virtio_device *dev = virtio_get_9p_device(0);
25     status_t status;
26 
27     if (dev == NULL) {
28         LOGF("v9p device doesn't exist\n");
29         return ERR_NOT_FOUND;
30     }
31 
32     virtio_9p_msg_t tatt = {
33         .msg_type = P9_TATTACH,
34         .tag = P9_TAG_DEFAULT,
35         .msg.tattach = {
36             .fid = FID_ROOT,
37             .afid = P9_FID_NOFID,
38             .uname = "root",
39             .aname = V9P_MOUNT_ANAME,
40             .n_uname = P9_UNAME_NONUNAME
41         }
42     };
43     virtio_9p_msg_t ratt = {};
44 
45     status = virtio_9p_rpc(dev, &tatt, &ratt);
46     if (status != NO_ERROR) {
47         LOGF("failed to attach to the host shared folder: %d\n", status);
48         return status;
49     }
50 
51     virtio_9p_msg_t twalk = {
52         .msg_type = P9_TWALK,
53         .tag = P9_TAG_DEFAULT,
54         .msg.twalk = {
55             .fid = FID_ROOT, .newfid = FID_RO, .nwname = 1,
56             .wname = {"LICENSE"}
57         }
58     };
59     virtio_9p_msg_t rwalk = {};
60 
61     status = virtio_9p_rpc(dev, &twalk, &rwalk);
62     if (status != NO_ERROR) {
63         LOGF("failed to walk to the target file: %d\n", status);
64         return status;
65     }
66 
67     virtio_9p_msg_t tlopen = {
68         .msg_type= P9_TLOPEN,
69         .tag = P9_TAG_DEFAULT,
70         .msg.tlopen = {
71             .fid = FID_RO, .flags = O_RDWR,
72         }
73     };
74     virtio_9p_msg_t rlopen = {};
75 
76     status = virtio_9p_rpc(dev, &tlopen, &rlopen);
77     if (status != NO_ERROR) {
78         LOGF("failed to open the target file: %d\n", status);
79         return status;
80     }
81 
82     virtio_9p_msg_t tread = {
83         .msg_type= P9_TREAD,
84         .tag = P9_TAG_DEFAULT,
85         .msg.tread = {
86             .fid = FID_RO, .offset = 0, .count = 1024
87         }
88     };
89     virtio_9p_msg_t rread = {};
90 
91     status = virtio_9p_rpc(dev, &tread, &rread);
92     if (status != NO_ERROR) {
93         LOGF("failed to read the target file: %d\n", status);
94         return status;
95     }
96 
97     hexdump8(rread.msg.rread.data, rread.msg.rread.count);
98 
99     return NO_ERROR;
100 }
101 #else
v9p_tests(int argc,const console_cmd_args * argv)102 int v9p_tests(int argc, const console_cmd_args *argv) {
103     LOGF("platform didn't have dev/virtio/9p supported\n");
104     return ERR_NOT_SUPPORTED;
105 }
106 #endif // WITH_DEV_VIRTIO_9P
107