1 /*
2  * Copyright (c) 2009 Travis Geiselbrecht
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 <lk/debug.h>
9 #include <string.h>
10 #include <lk/console_cmd.h>
11 #include <lib/fs.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <platform.h>
15 #include <lk/err.h>
16 
17 #if LK_DEBUGLEVEL > 1
18 static int cmd_fs(int argc, const console_cmd_args *argv);
19 
20 STATIC_COMMAND_START
21 STATIC_COMMAND("fs", "fs debug commands", &cmd_fs)
22 STATIC_COMMAND_END(fs);
23 
24 extern int fs_mount_type(const char *path, const char *device, const char *name);
25 
cmd_fs_ioctl(int argc,const console_cmd_args * argv)26 static int cmd_fs_ioctl(int argc, const console_cmd_args *argv) {
27     if (argc < 3) {
28         printf("not enough arguments\n");
29         return ERR_INVALID_ARGS;
30     }
31 
32     int request = argv[2].u;
33 
34     switch (request) {
35         case FS_IOCTL_GET_FILE_ADDR: {
36             if (argc < 4) {
37                 printf("%s %s %lu <path>\n", argv[0].str, argv[1].str,
38                        argv[2].u);
39                 return ERR_INVALID_ARGS;
40             }
41 
42             int err;
43             filehandle *handle;
44             err = fs_open_file(argv[3].str, &handle);
45             if (err != NO_ERROR) {
46                 printf("error %d opening file\n", err);
47                 return err;
48             }
49 
50             void *file_addr;
51             err = fs_file_ioctl(handle, request, &file_addr);
52             if (err != NO_ERROR) {
53                 fs_close_file(handle);
54                 return err;
55             }
56 
57             printf("%s is mapped at %p\n", argv[3].str, file_addr);
58 
59             return fs_close_file(handle);
60             break;
61         }
62         case FS_IOCTL_IS_LINEAR: {
63             if (argc < 4) {
64                 printf("%s %s %lu <path>\n", argv[0].str, argv[1].str,
65                        argv[2].u);
66                 return ERR_INVALID_ARGS;
67             }
68 
69             int err;
70             filehandle *handle;
71             err = fs_open_file(argv[3].str, &handle);
72             if (err != NO_ERROR) {
73                 printf("error %d opening file\n", err);
74                 return err;
75             }
76 
77             bool is_mapped;
78             err = fs_file_ioctl(handle, request, (void **)&is_mapped);
79             if (err != NO_ERROR) {
80                 fs_close_file(handle);
81                 return err;
82             }
83 
84             if (is_mapped) {
85                 printf("file at %s is memory mapped\n", argv[3].str);
86             } else {
87                 printf("file at %s is not memory mapped\n", argv[3].str);
88             }
89 
90             return fs_close_file(handle);
91             break;
92         }
93         default: {
94             printf("error, unsupported ioctl: %d\n", request);
95         }
96     }
97 
98     return ERR_NOT_SUPPORTED;
99 }
100 
cmd_fs(int argc,const console_cmd_args * argv)101 static int cmd_fs(int argc, const console_cmd_args *argv) {
102     int rc = 0;
103 
104     if (argc < 2) {
105 notenoughargs:
106         printf("not enough arguments:\n");
107 usage:
108         printf("%s mount <path> <type> [device]\n", argv[0].str);
109         printf("%s unmount <path>\n", argv[0].str);
110         printf("%s write <path> <string> [<offset>]\n", argv[0].str);
111         printf("%s format <type> [device]\n", argv[0].str);
112         printf("%s stat <path>\n", argv[0].str);
113         printf("%s ioctl <request> [args...]\n", argv[0].str);
114         printf("%s list\n", argv[0].str);
115         return -1;
116     }
117 
118     if (!strcmp(argv[1].str, "mount")) {
119         int err;
120 
121         if (argc < 4)
122             goto notenoughargs;
123 
124         err = fs_mount(argv[2].str, argv[3].str,
125                        (argc >= 5) ? argv[4].str : NULL);
126 
127         if (err < 0) {
128             printf("error %d mounting device\n", err);
129             return err;
130         }
131     } else if (!strcmp(argv[1].str, "unmount")) {
132         int err;
133 
134         if (argc < 3)
135             goto notenoughargs;
136 
137         err = fs_unmount(argv[2].str);
138         if (err < 0) {
139             printf("error %d unmounting device\n", err);
140             return err;
141         }
142     } else if (!strcmp(argv[1].str, "format")) {
143         int err;
144 
145         if (argc < 3) {
146             goto notenoughargs;
147         }
148 
149         err = fs_format_device(
150                   argv[2].str,
151                   (argc >= 4) ? argv[3].str : NULL,
152                   NULL
153               );
154 
155         if (err != NO_ERROR) {
156             printf("error %d formatting device\n", err);
157             return err;
158         }
159 
160     } else if (!strcmp(argv[1].str, "stat")) {
161         int err;
162 
163         if (argc < 3) {
164             goto notenoughargs;
165         }
166 
167         struct fs_stat stat;
168         err = fs_stat_fs(argv[2].str, &stat);
169 
170         if (err != NO_ERROR) {
171             printf("error %d statting filesystem\n", err);
172             return err;
173         }
174 
175         printf("\ttotal bytes: %llu\n", stat.total_space);
176         printf("\tfree bytes: %llu\n", stat.free_space);
177         printf("\n");
178         printf("\ttotal inodes: %d\n", stat.total_inodes);
179         printf("\tfree inodes: %d\n", stat.free_inodes);
180 
181     } else if (!strcmp(argv[1].str, "ioctl")) {
182         return cmd_fs_ioctl(argc, argv);
183     } else if (!strcmp(argv[1].str, "write")) {
184         int err;
185         off_t off;
186         filehandle *handle;
187         struct file_stat stat;
188 
189         if (argc < 3)
190             goto notenoughargs;
191 
192         err = fs_open_file(argv[2].str, &handle);
193         if (err < 0) {
194             printf("error %d opening file\n", err);
195             return err;
196         }
197 
198         err = fs_stat_file(handle, &stat);
199         if (err < 0) {
200             printf("error %d stat'ing file\n", err);
201             fs_close_file(handle);
202             return err;
203         }
204 
205         if (argc < 5)
206             off = stat.size;
207         else
208             off = argv[4].u;
209 
210         err = fs_write_file(handle, argv[3].str, off, strlen(argv[3].str));
211         if (err < 0) {
212             printf("error %d writing file\n", err);
213             fs_close_file(handle);
214             return err;
215         }
216 
217         fs_close_file(handle);
218     } else if (!strcmp(argv[1].str, "list")) {
219         printf("Implemented file systems:\n");
220         fs_dump_list();
221     } else {
222         printf("unrecognized subcommand\n");
223         goto usage;
224     }
225 
226     return rc;
227 }
228 
229 #endif
230