/src/barebox/commands/mount.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | /* mount.c - mount devices */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <command.h> |
8 | | #include <fs.h> |
9 | | #include <errno.h> |
10 | | #include <getopt.h> |
11 | | #include <linux/err.h> |
12 | | #include <stringlist.h> |
13 | | |
14 | | static int do_mount(int argc, char *argv[]) |
15 | 0 | { |
16 | 0 | int ret = 0, opt, verbose = 0; |
17 | 0 | struct driver *drv; |
18 | 0 | const char *type = NULL; |
19 | 0 | const char *mountpoint, *devstr; |
20 | 0 | struct string_list fsoption_list; |
21 | 0 | char *fsoptions = NULL; |
22 | |
|
23 | 0 | string_list_init(&fsoption_list); |
24 | |
|
25 | 0 | while ((opt = getopt(argc, argv, "ao:t:v")) > 0) { |
26 | 0 | switch (opt) { |
27 | 0 | case 'a': |
28 | 0 | mount_all(); |
29 | 0 | break; |
30 | 0 | case 't': |
31 | 0 | type = optarg; |
32 | 0 | break; |
33 | 0 | case 'o': |
34 | 0 | string_list_add(&fsoption_list, optarg); |
35 | 0 | break; |
36 | 0 | case 'v': |
37 | 0 | verbose++; |
38 | 0 | break; |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | argv += optind; |
43 | 0 | argc -= optind; |
44 | |
|
45 | 0 | if (argc == 0) { |
46 | 0 | struct fs_device *fsdev; |
47 | |
|
48 | 0 | for_each_fs_device(fsdev) { |
49 | 0 | printf("%s on %s type %s\n", |
50 | 0 | fsdev->backingstore ? fsdev->backingstore : "none", |
51 | 0 | fsdev->path, |
52 | 0 | fsdev->dev.name); |
53 | 0 | } |
54 | |
|
55 | 0 | if (verbose) { |
56 | 0 | printf("\nSupported filesystems:\n\n"); |
57 | 0 | bus_for_each_driver(&fs_bus, drv) { |
58 | 0 | struct fs_driver * fsdrv = drv_to_fs_driver(drv); |
59 | 0 | printf("%s\n", fsdrv->drv.name); |
60 | 0 | } |
61 | 0 | } |
62 | |
|
63 | 0 | goto out; |
64 | 0 | } |
65 | | |
66 | 0 | if (argc == 0) { |
67 | 0 | ret = COMMAND_ERROR_USAGE; |
68 | 0 | goto out; |
69 | 0 | } |
70 | | |
71 | 0 | devstr = argv[0]; |
72 | |
|
73 | 0 | fsoptions = string_list_join(&fsoption_list, " "); |
74 | 0 | if (!fsoptions) { |
75 | 0 | ret = -ENOMEM; |
76 | 0 | goto out; |
77 | 0 | } |
78 | | |
79 | 0 | if (argc == 1) { |
80 | 0 | struct cdev *cdev; |
81 | 0 | const char *path; |
82 | |
|
83 | 0 | devstr = devpath_to_name(devstr); |
84 | |
|
85 | 0 | device_detect_by_name(devstr); |
86 | |
|
87 | 0 | cdev = cdev_by_name(devstr); |
88 | 0 | if (!cdev) { |
89 | 0 | ret = -ENOENT; |
90 | 0 | goto out; |
91 | 0 | } |
92 | | |
93 | 0 | path = cdev_mount_default(cdev, fsoptions); |
94 | 0 | if (IS_ERR(path)) |
95 | 0 | return PTR_ERR(path); |
96 | | |
97 | 0 | printf("mounted /dev/%s on %s\n", devstr, path); |
98 | |
|
99 | 0 | goto out; |
100 | 0 | } |
101 | | |
102 | 0 | if (argc == 3) { |
103 | | /* |
104 | | * Old behaviour: mount <dev> <type> <mountpoint> |
105 | | */ |
106 | 0 | type = argv[1]; |
107 | 0 | mountpoint = argv[2]; |
108 | 0 | } else { |
109 | 0 | mountpoint = argv[1]; |
110 | 0 | } |
111 | |
|
112 | 0 | ret = mount(devstr, type, mountpoint, fsoptions); |
113 | |
|
114 | 0 | out: |
115 | 0 | free(fsoptions); |
116 | 0 | string_list_free(&fsoption_list); |
117 | 0 | return ret; |
118 | 0 | } |
119 | | |
120 | | BAREBOX_CMD_HELP_START(mount) |
121 | | BAREBOX_CMD_HELP_TEXT("If no argument is given, list mounted filesystems.") |
122 | | BAREBOX_CMD_HELP_TEXT("If no FSTYPE is specified, try to detect it automatically.") |
123 | | BAREBOX_CMD_HELP_TEXT("With -a the mount command mounts all block devices whose filesystem") |
124 | | BAREBOX_CMD_HELP_TEXT("can be detected automatically to /mnt/PARTNAME") |
125 | | BAREBOX_CMD_HELP_TEXT("If mountpoint is not given, a standard mountpoint of /mnt/DEVICE") |
126 | | BAREBOX_CMD_HELP_TEXT("is used. This directory is created automatically if necessary.") |
127 | | BAREBOX_CMD_HELP_TEXT("With -o loop the mount command mounts a file instead of a device.") |
128 | | BAREBOX_CMD_HELP_TEXT("") |
129 | | BAREBOX_CMD_HELP_TEXT("Options:") |
130 | | BAREBOX_CMD_HELP_OPT("-a\t", "mount all blockdevices") |
131 | | BAREBOX_CMD_HELP_OPT("-t FSTYPE", "specify filesystem type") |
132 | | BAREBOX_CMD_HELP_OPT("-o OPTIONS", "set file system OPTIONS") |
133 | | BAREBOX_CMD_HELP_OPT("-v\t", "verbose") |
134 | | BAREBOX_CMD_HELP_END |
135 | | |
136 | | BAREBOX_CMD_START(mount) |
137 | | .cmd = do_mount, |
138 | | BAREBOX_CMD_DESC("mount a filesystem or list mounted filesystems") |
139 | | BAREBOX_CMD_OPTS("[[-atov] [DEVICE] [MOUNTPOINT]]") |
140 | | BAREBOX_CMD_GROUP(CMD_GRP_PART) |
141 | | BAREBOX_CMD_HELP(cmd_mount_help) |
142 | | BAREBOX_CMD_END |