1 | /* |
---|
2 | * Shell Command Implmentation |
---|
3 | * |
---|
4 | * Author: Fernando RUIZ CASAS |
---|
5 | * Work: fernando.ruiz@ctv.es |
---|
6 | * Home: correo@fernando-ruiz.com |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <string.h> |
---|
22 | #include <errno.h> |
---|
23 | |
---|
24 | #include <rtems.h> |
---|
25 | #include <rtems/shell.h> |
---|
26 | #include <rtems/shellconfig.h> |
---|
27 | #include <rtems/dosfs.h> |
---|
28 | #include <rtems/fsmount.h> |
---|
29 | #include "internal.h" |
---|
30 | |
---|
31 | static rtems_chain_control filesystems; |
---|
32 | static bool fs_init; |
---|
33 | |
---|
34 | static void rtems_shell_mount_fsys_init(void) |
---|
35 | { |
---|
36 | if (!fs_init) |
---|
37 | { |
---|
38 | rtems_chain_initialize_empty (&filesystems); |
---|
39 | fs_init = true; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | void rtems_shell_mount_add_fsys(rtems_shell_filesystems_t* fs) |
---|
44 | { |
---|
45 | rtems_shell_mount_fsys_init(); |
---|
46 | rtems_chain_append (&filesystems, &fs->link); |
---|
47 | } |
---|
48 | |
---|
49 | void rtems_shell_mount_del_fsys(rtems_shell_filesystems_t* fs) |
---|
50 | { |
---|
51 | if (fs_init) |
---|
52 | rtems_chain_extract (&fs->link); |
---|
53 | } |
---|
54 | |
---|
55 | int rtems_shell_libc_mounter( |
---|
56 | const char* driver, |
---|
57 | const char* path, |
---|
58 | rtems_shell_filesystems_t* fs, |
---|
59 | rtems_filesystem_options_t options) |
---|
60 | { |
---|
61 | rtems_filesystem_mount_table_entry_t* mt_entry; |
---|
62 | /* |
---|
63 | * Mount the disk. |
---|
64 | */ |
---|
65 | |
---|
66 | if (mount (&mt_entry, fs->fs_ops, options, (char*) driver, (char*) path) < 0) |
---|
67 | { |
---|
68 | fprintf (stderr, "mount: mount failed: %s\n", strerror (errno)); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | #define NUMOF(_i) (sizeof (_i) / sizeof (_i[0])) |
---|
76 | |
---|
77 | int rtems_shell_main_mount( |
---|
78 | int argc, |
---|
79 | char *argv[] |
---|
80 | ) |
---|
81 | { |
---|
82 | rtems_filesystem_options_t options = RTEMS_FILESYSTEM_READ_WRITE; |
---|
83 | rtems_shell_filesystems_t* fs = NULL; |
---|
84 | char* driver = NULL; |
---|
85 | char* mount_point = NULL; |
---|
86 | int arg; |
---|
87 | |
---|
88 | rtems_shell_mount_fsys_init(); |
---|
89 | |
---|
90 | for (arg = 1; arg < argc; arg++) { |
---|
91 | if (argv[arg][0] == '-') { |
---|
92 | if (argv[arg][1] == 't') { |
---|
93 | rtems_shell_filesystems_t** a; |
---|
94 | |
---|
95 | arg++; |
---|
96 | if (arg == argc) { |
---|
97 | fprintf( |
---|
98 | stderr, |
---|
99 | "%s: -t needs a type of file-system;; see -L.\n", |
---|
100 | argv[0] |
---|
101 | ); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | for (a = rtems_shell_Mount_filesystems; *a; a++) { |
---|
106 | if (strcmp (argv[arg], (*a)->name) == 0) { |
---|
107 | fs = *a; |
---|
108 | break; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | if (!fs && !rtems_chain_is_empty(&filesystems)) { |
---|
113 | rtems_chain_node* node = filesystems.first; |
---|
114 | while (!rtems_chain_is_tail (&filesystems, node)) { |
---|
115 | rtems_shell_filesystems_t* f = (rtems_shell_filesystems_t*)node; |
---|
116 | if (strcmp (argv[arg], f->name) == 0) { |
---|
117 | fs = f; |
---|
118 | break; |
---|
119 | } |
---|
120 | node = node->next; |
---|
121 | } |
---|
122 | } |
---|
123 | } else if (argv[arg][1] == 'r') { |
---|
124 | options = RTEMS_FILESYSTEM_READ_ONLY; |
---|
125 | } else if (argv[arg][1] == 'L') { |
---|
126 | rtems_shell_filesystems_t** a; |
---|
127 | fprintf (stderr, "File systems: "); |
---|
128 | for (a = rtems_shell_Mount_filesystems; *a; a++) |
---|
129 | if (*a) |
---|
130 | fprintf (stderr, "%s ", (*a)->name); |
---|
131 | if (!rtems_chain_is_empty(&filesystems)) { |
---|
132 | rtems_chain_node* node = filesystems.first; |
---|
133 | while (!rtems_chain_is_tail (&filesystems, node)) { |
---|
134 | rtems_shell_filesystems_t* f = (rtems_shell_filesystems_t*)node; |
---|
135 | fprintf (stderr, "%s ", f->name); |
---|
136 | node = node->next; |
---|
137 | } |
---|
138 | } |
---|
139 | fprintf (stderr, "\n"); |
---|
140 | return 1; |
---|
141 | } else { |
---|
142 | fprintf (stderr, "unknown option: %s\n", argv[arg]); |
---|
143 | return 1; |
---|
144 | } |
---|
145 | } else { |
---|
146 | if (!driver) |
---|
147 | driver = argv[arg]; |
---|
148 | else if (!mount_point) |
---|
149 | mount_point = argv[arg]; |
---|
150 | else { |
---|
151 | fprintf ( |
---|
152 | stderr, "mount: driver and mount only require: %s\n", argv[arg]); |
---|
153 | return 1; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | if (fs == NULL) { |
---|
159 | fprintf (stderr, "mount: no file-system; see the -L option\n"); |
---|
160 | return 1; |
---|
161 | } |
---|
162 | |
---|
163 | if (fs->driver_needed && !driver) { |
---|
164 | fprintf (stderr, "mount: no driver\n"); |
---|
165 | return 1; |
---|
166 | } |
---|
167 | |
---|
168 | if (!mount_point) { |
---|
169 | fprintf (stderr, "mount: no mount point\n"); |
---|
170 | return 1; |
---|
171 | } |
---|
172 | |
---|
173 | /* |
---|
174 | * Mount the disk. |
---|
175 | */ |
---|
176 | |
---|
177 | if (fs->mounter (driver, mount_point, fs, options)) |
---|
178 | return 1; |
---|
179 | |
---|
180 | printf ("mounted %s -> %s\n", driver, mount_point); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | rtems_shell_cmd_t rtems_shell_MOUNT_Command = { |
---|
186 | "mount", /* name */ |
---|
187 | "mount [-t fstype] [-r] [-L] device path # mount disk", /* usage */ |
---|
188 | "files", /* topic */ |
---|
189 | rtems_shell_main_mount, /* command */ |
---|
190 | NULL, /* alias */ |
---|
191 | NULL /* next */ |
---|
192 | }; |
---|