1 /*
2  * Copyright (c) 2024 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 _LOGF(fmt, args...) \
13     printf("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__, ##args)
14 #define LOGF(x...) _LOGF(x)
15 
16 #if WITH_DEV_VIRTIO_9P
17 #include <lib/fs.h>
18 
19 #define V9FS_MOUNT_POINT  "/v9p"
20 #define V9FS_NAME         "9p"
21 #define V9P_BDEV_NAME     "v9p0"
22 
23 #define BUF_SIZE          1024
24 
v9fs_tests(int argc,const console_cmd_args * argv)25 int v9fs_tests(int argc, const console_cmd_args *argv) {
26     status_t status;
27     ssize_t readbytes;
28     filehandle *handle;
29     char buf[BUF_SIZE];
30 
31     status = fs_mount(V9FS_MOUNT_POINT, V9FS_NAME, V9P_BDEV_NAME);
32     if (status != NO_ERROR) {
33       LOGF("failed to mount v9p bdev (%s) onto mount point (%s): %d\n",
34            V9P_BDEV_NAME, V9FS_MOUNT_POINT, status);
35       return status;
36     }
37 
38     status = fs_open_file(V9FS_MOUNT_POINT "/LICENSE", &handle);
39     if (status != NO_ERROR) {
40         LOGF("failed to open the target file: %d\n", status);
41         return status;
42     }
43 
44     readbytes = fs_read_file(handle, buf, 0, BUF_SIZE);
45     if (readbytes < 0) {
46         LOGF("failed to read the target file: %ld\n", readbytes);
47         return status;
48     }
49 
50     hexdump8(buf, BUF_SIZE);
51 
52     status = fs_close_file(handle);
53     if (status != NO_ERROR) {
54         LOGF("failed to close the target file: %d\n", status);
55         return status;
56     }
57 
58     status = fs_unmount(V9FS_MOUNT_POINT);
59     if (status != NO_ERROR) {
60       LOGF("failed to unmount v9p on mount point (%s): %d\n",
61            V9FS_MOUNT_POINT, status);
62       return status;
63     }
64 
65     return NO_ERROR;
66 }
67 #else
v9fs_tests(int argc,const console_cmd_args * argv)68 int v9fs_tests(int argc, const console_cmd_args *argv) {
69     LOGF("platform didn't have dev/virtio/9p supported\n");
70     return ERR_NOT_SUPPORTED;
71 }
72 #endif // WITH_DEV_VIRTIO_9P
73