Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-mount.c
blob4626e54ed45cc8e53d4e47c7838c3297cbaac40d
1 /* grub-mount.c - FUSE driver for filesystems that GRUB understands */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #define FUSE_USE_VERSION 26
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/misc.h>
25 #include <grub/device.h>
26 #include <grub/disk.h>
27 #include <grub/file.h>
28 #include <grub/fs.h>
29 #include <grub/env.h>
30 #include <grub/term.h>
31 #include <grub/mm.h>
32 #include <grub/lib/hexdump.h>
33 #include <grub/crypto.h>
34 #include <grub/command.h>
35 #include <grub/zfs/zfs.h>
36 #include <grub/i18n.h>
37 #include <fuse/fuse.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <stdlib.h>
44 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
45 #pragma GCC diagnostic ignored "-Wmissing-declarations"
46 #include <argp.h>
47 #pragma GCC diagnostic error "-Wmissing-prototypes"
48 #pragma GCC diagnostic error "-Wmissing-declarations"
50 #include "progname.h"
52 static const char *root = NULL;
53 grub_device_t dev = NULL;
54 grub_fs_t fs = NULL;
55 static char **images = NULL;
56 static char *debug_str = NULL;
57 static char **fuse_args = NULL;
58 static int fuse_argc = 0;
59 static int num_disks = 0;
60 static int mount_crypt = 0;
62 static grub_err_t
63 execute_command (const char *name, int n, char **args)
65 grub_command_t cmd;
67 cmd = grub_command_find (name);
68 if (! cmd)
69 grub_util_error (_("can't find command `%s'"), name);
71 return (cmd->func) (cmd, n, args);
74 /* Translate GRUB error numbers into OS error numbers. Print any unexpected
75 errors. */
76 static int
77 translate_error (void)
79 int ret;
81 switch (grub_errno)
83 case GRUB_ERR_NONE:
84 ret = 0;
85 break;
87 case GRUB_ERR_OUT_OF_MEMORY:
88 grub_print_error ();
89 ret = -ENOMEM;
90 break;
92 case GRUB_ERR_BAD_FILE_TYPE:
93 /* This could also be EISDIR. Take a guess. */
94 ret = -ENOTDIR;
95 break;
97 case GRUB_ERR_FILE_NOT_FOUND:
98 ret = -ENOENT;
99 break;
101 case GRUB_ERR_FILE_READ_ERROR:
102 case GRUB_ERR_READ_ERROR:
103 case GRUB_ERR_IO:
104 grub_print_error ();
105 ret = -EIO;
106 break;
108 case GRUB_ERR_SYMLINK_LOOP:
109 ret = -ELOOP;
110 break;
112 default:
113 grub_print_error ();
114 ret = -EINVAL;
115 break;
118 /* Any previous errors were handled. */
119 grub_errno = GRUB_ERR_NONE;
121 return ret;
124 /* Context for fuse_getattr. */
125 struct fuse_getattr_ctx
127 char *filename;
128 struct grub_dirhook_info file_info;
129 int file_exists;
132 /* A hook for iterating directories. */
133 static int
134 fuse_getattr_find_file (const char *cur_filename,
135 const struct grub_dirhook_info *info, void *data)
137 struct fuse_getattr_ctx *ctx = data;
139 if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
140 : grub_strcmp (cur_filename, ctx->filename)) == 0)
142 ctx->file_info = *info;
143 ctx->file_exists = 1;
144 return 1;
146 return 0;
149 static int
150 fuse_getattr (const char *path, struct stat *st)
152 struct fuse_getattr_ctx ctx;
153 char *pathname, *path2;
154 const char *pathname_t;
156 if (path[0] == '/' && path[1] == 0)
158 st->st_dev = 0;
159 st->st_ino = 0;
160 st->st_mode = 0555 | S_IFDIR;
161 st->st_uid = 0;
162 st->st_gid = 0;
163 st->st_rdev = 0;
164 st->st_size = 0;
165 st->st_blksize = 512;
166 st->st_blocks = (st->st_blksize + 511) >> 9;
167 st->st_atime = st->st_mtime = st->st_ctime = 0;
168 return 0;
171 ctx.file_exists = 0;
173 pathname_t = grub_strchr (path, ')');
174 if (! pathname_t)
175 pathname_t = path;
176 else
177 pathname_t++;
178 pathname = xstrdup (pathname_t);
180 /* Remove trailing '/'. */
181 while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
182 pathname[grub_strlen (pathname) - 1] = 0;
184 /* Split into path and filename. */
185 ctx.filename = grub_strrchr (pathname, '/');
186 if (! ctx.filename)
188 path2 = grub_strdup ("/");
189 ctx.filename = pathname;
191 else
193 ctx.filename++;
194 path2 = grub_strdup (pathname);
195 path2[ctx.filename - pathname] = 0;
198 /* It's the whole device. */
199 (fs->dir) (dev, path2, fuse_getattr_find_file, &ctx);
201 grub_free (path2);
202 if (!ctx.file_exists)
204 grub_errno = GRUB_ERR_NONE;
205 return -ENOENT;
207 st->st_dev = 0;
208 st->st_ino = 0;
209 st->st_mode = ctx.file_info.dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
210 st->st_uid = 0;
211 st->st_gid = 0;
212 st->st_rdev = 0;
213 st->st_size = 0;
214 if (!ctx.file_info.dir)
216 grub_file_t file;
217 file = grub_file_open (path);
218 if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
220 grub_errno = GRUB_ERR_NONE;
221 st->st_mode = (0555 | S_IFDIR);
223 else if (! file)
224 return translate_error ();
225 else
227 st->st_size = file->size;
228 grub_file_close (file);
231 st->st_blksize = 512;
232 st->st_blocks = (st->st_size + 511) >> 9;
233 st->st_atime = st->st_mtime = st->st_ctime = ctx.file_info.mtimeset
234 ? ctx.file_info.mtime : 0;
235 grub_errno = GRUB_ERR_NONE;
236 return 0;
239 static int
240 fuse_opendir (const char *path, struct fuse_file_info *fi)
242 return 0;
245 /* FIXME */
246 static grub_file_t files[65536];
247 static int first_fd = 1;
249 static int
250 fuse_open (const char *path, struct fuse_file_info *fi __attribute__ ((unused)))
252 grub_file_t file;
253 file = grub_file_open (path);
254 if (! file)
255 return translate_error ();
256 files[first_fd++] = file;
257 fi->fh = first_fd;
258 files[first_fd++] = file;
259 grub_errno = GRUB_ERR_NONE;
260 return 0;
263 static int
264 fuse_read (const char *path, char *buf, size_t sz, off_t off,
265 struct fuse_file_info *fi)
267 grub_file_t file = files[fi->fh];
268 grub_ssize_t size;
270 if (off > file->size)
271 return -EINVAL;
273 file->offset = off;
275 size = grub_file_read (file, buf, sz);
276 if (size < 0)
277 return translate_error ();
278 else
280 grub_errno = GRUB_ERR_NONE;
281 return size;
285 static int
286 fuse_release (const char *path, struct fuse_file_info *fi)
288 grub_file_close (files[fi->fh]);
289 files[fi->fh] = NULL;
290 grub_errno = GRUB_ERR_NONE;
291 return 0;
294 /* Context for fuse_readdir. */
295 struct fuse_readdir_ctx
297 const char *path;
298 void *buf;
299 fuse_fill_dir_t fill;
302 /* Helper for fuse_readdir. */
303 static int
304 fuse_readdir_call_fill (const char *filename,
305 const struct grub_dirhook_info *info, void *data)
307 struct fuse_readdir_ctx *ctx = data;
308 struct stat st;
310 grub_memset (&st, 0, sizeof (st));
311 st.st_mode = info->dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
312 if (!info->dir)
314 grub_file_t file;
315 char *tmp;
316 tmp = xasprintf ("%s/%s", ctx->path, filename);
317 file = grub_file_open (tmp);
318 free (tmp);
319 /* Symlink to directory. */
320 if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
322 grub_errno = GRUB_ERR_NONE;
323 st.st_mode = (0555 | S_IFDIR);
325 else if (!file)
327 grub_errno = GRUB_ERR_NONE;
329 else
331 st.st_size = file->size;
332 grub_file_close (file);
335 st.st_blksize = 512;
336 st.st_blocks = (st.st_size + 511) >> 9;
337 st.st_atime = st.st_mtime = st.st_ctime
338 = info->mtimeset ? info->mtime : 0;
339 ctx->fill (ctx->buf, filename, &st, 0);
340 return 0;
343 static int
344 fuse_readdir (const char *path, void *buf,
345 fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi)
347 struct fuse_readdir_ctx ctx = {
348 .path = path,
349 .buf = buf,
350 .fill = fill
352 char *pathname;
354 pathname = xstrdup (path);
356 /* Remove trailing '/'. */
357 while (pathname [0] && pathname[1]
358 && pathname[grub_strlen (pathname) - 1] == '/')
359 pathname[grub_strlen (pathname) - 1] = 0;
361 (fs->dir) (dev, pathname, fuse_readdir_call_fill, &ctx);
362 free (pathname);
363 grub_errno = GRUB_ERR_NONE;
364 return 0;
367 struct fuse_operations grub_opers = {
368 .getattr = fuse_getattr,
369 .open = fuse_open,
370 .release = fuse_release,
371 .opendir = fuse_opendir,
372 .readdir = fuse_readdir,
373 .read = fuse_read
376 static grub_err_t
377 fuse_init (void)
379 int i;
381 for (i = 0; i < num_disks; i++)
383 char *argv[2];
384 char *host_file;
385 char *loop_name;
386 loop_name = grub_xasprintf ("loop%d", i);
387 if (!loop_name)
388 grub_util_error ("%s", grub_errmsg);
390 host_file = grub_xasprintf ("(host)%s", images[i]);
391 if (!host_file)
392 grub_util_error ("%s", grub_errmsg);
394 argv[0] = loop_name;
395 argv[1] = host_file;
397 if (execute_command ("loopback", 2, argv))
398 grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
400 grub_free (loop_name);
401 grub_free (host_file);
404 if (mount_crypt)
406 char *argv[2] = { xstrdup ("-a"), NULL};
407 if (execute_command ("cryptomount", 1, argv))
408 grub_util_error (_("`cryptomount' command fails: %s"),
409 grub_errmsg);
410 free (argv[0]);
413 grub_lvm_fini ();
414 grub_mdraid09_fini ();
415 grub_mdraid1x_fini ();
416 grub_diskfilter_fini ();
417 grub_diskfilter_init ();
418 grub_mdraid09_init ();
419 grub_mdraid1x_init ();
420 grub_lvm_init ();
422 dev = grub_device_open (0);
423 if (! dev)
424 return grub_errno;
426 fs = grub_fs_probe (dev);
427 if (! fs)
429 grub_device_close (dev);
430 return grub_errno;
433 if (fuse_main (fuse_argc, fuse_args, &grub_opers, NULL))
434 grub_error (GRUB_ERR_IO, "fuse_main failed");
436 for (i = 0; i < num_disks; i++)
438 char *argv[2];
439 char *loop_name;
441 loop_name = grub_xasprintf ("loop%d", i);
442 if (!loop_name)
443 grub_util_error ("%s", grub_errmsg);
445 argv[0] = xstrdup ("-d");
446 argv[1] = loop_name;
448 execute_command ("loopback", 2, argv);
450 grub_free (argv[0]);
451 grub_free (loop_name);
454 return grub_errno;
457 static struct argp_option options[] = {
458 {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
459 {"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
460 {"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
461 {"zfs-key", 'K',
462 /* TRANSLATORS: "prompt" is a keyword. */
463 N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
464 {"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
465 {0, 0, 0, 0, 0, 0}
468 /* Print the version information. */
469 static void
470 print_version (FILE *stream, struct argp_state *state)
472 fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
474 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
476 static error_t
477 argp_parser (int key, char *arg, struct argp_state *state)
479 switch (key)
481 case 'r':
482 root = arg;
483 return 0;
485 case 'K':
486 if (strcmp (arg, "prompt") == 0)
488 char buf[1024];
489 grub_printf ("%s", _("Enter ZFS password: "));
490 if (grub_password_get (buf, 1023))
492 grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
495 else
497 FILE *f;
498 ssize_t real_size;
499 grub_uint8_t buf[1024];
500 f = grub_util_fopen (arg, "rb");
501 if (!f)
503 printf (_("%s: error:"), program_name);
504 printf (_("cannot open `%s': %s"), arg, strerror (errno));
505 printf ("\n");
506 return 0;
508 real_size = fread (buf, 1, 1024, f);
509 if (real_size < 0)
511 printf (_("%s: error:"), program_name);
512 printf (_("cannot read `%s': %s"), arg,
513 strerror (errno));
514 printf ("\n");
515 fclose (f);
516 return 0;
518 grub_zfs_add_key (buf, real_size, 0);
519 fclose (f);
521 return 0;
523 case 'C':
524 mount_crypt = 1;
525 return 0;
527 case 'd':
528 debug_str = arg;
529 return 0;
531 case 'v':
532 verbosity++;
533 return 0;
535 case ARGP_KEY_ARG:
536 if (arg[0] != '-')
537 break;
539 default:
540 if (!arg)
541 return 0;
543 fuse_args = xrealloc (fuse_args, (fuse_argc + 1) * sizeof (fuse_args[0]));
544 fuse_args[fuse_argc] = xstrdup (arg);
545 fuse_argc++;
546 return 0;
549 images = xrealloc (images, (num_disks + 1) * sizeof (images[0]));
550 images[num_disks] = canonicalize_file_name (arg);
551 num_disks++;
553 return 0;
556 struct argp argp = {
557 options, argp_parser, N_("IMAGE1 [IMAGE2 ...] MOUNTPOINT"),
558 N_("Debug tool for filesystem driver."),
559 NULL, NULL, NULL
563 main (int argc, char *argv[])
565 const char *default_root;
566 char *alloc_root;
568 grub_util_host_init (&argc, &argv);
570 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
571 fuse_args[fuse_argc] = xstrdup (argv[0]);
572 fuse_argc++;
573 /* Run single-threaded. */
574 fuse_args[fuse_argc] = xstrdup ("-s");
575 fuse_argc++;
577 argp_parse (&argp, argc, argv, 0, 0, 0);
579 if (num_disks < 2)
580 grub_util_error ("%s", _("need an image and mountpoint"));
581 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
582 fuse_args[fuse_argc] = images[num_disks - 1];
583 fuse_argc++;
584 num_disks--;
585 fuse_args[fuse_argc] = NULL;
587 /* Initialize all modules. */
588 grub_init_all ();
590 if (debug_str)
591 grub_env_set ("debug", debug_str);
593 default_root = (num_disks == 1) ? "loop0" : "md0";
594 alloc_root = 0;
595 if (root)
597 if ((*root >= '0') && (*root <= '9'))
599 alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
601 sprintf (alloc_root, "%s,%s", default_root, root);
602 root = alloc_root;
605 else
606 root = default_root;
608 grub_env_set ("root", root);
610 if (alloc_root)
611 free (alloc_root);
613 /* Do it. */
614 fuse_init ();
615 if (grub_errno)
617 grub_print_error ();
618 return 1;
621 /* Free resources. */
622 grub_fini_all ();
624 return 0;