tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / util / grub-mount.c
blobbf4c8b891be5c20b909e0c58aec043430a1a30b6
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 #include <config.h>
20 #include <grub/types.h>
21 #include <grub/emu/misc.h>
22 #include <grub/util/misc.h>
23 #include <grub/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/file.h>
27 #include <grub/fs.h>
28 #include <grub/env.h>
29 #include <grub/term.h>
30 #include <grub/mm.h>
31 #include <grub/lib/hexdump.h>
32 #include <grub/crypto.h>
33 #include <grub/command.h>
34 #include <grub/zfs/zfs.h>
35 #include <grub/i18n.h>
36 #include <fuse.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <stdlib.h>
43 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
44 #pragma GCC diagnostic ignored "-Wmissing-declarations"
45 #include <argp.h>
46 #pragma GCC diagnostic error "-Wmissing-prototypes"
47 #pragma GCC diagnostic error "-Wmissing-declarations"
49 #include "progname.h"
51 static const char *root = NULL;
52 grub_device_t dev = NULL;
53 grub_fs_t fs = NULL;
54 static char **images = NULL;
55 static char *debug_str = NULL;
56 static char **fuse_args = NULL;
57 static int fuse_argc = 0;
58 static int num_disks = 0;
59 static int mount_crypt = 0;
61 static grub_err_t
62 execute_command (const char *name, int n, char **args)
64 grub_command_t cmd;
66 cmd = grub_command_find (name);
67 if (! cmd)
68 grub_util_error (_("can't find command `%s'"), name);
70 return (cmd->func) (cmd, n, args);
73 /* Translate GRUB error numbers into OS error numbers. Print any unexpected
74 errors. */
75 static int
76 translate_error (void)
78 int ret;
80 switch (grub_errno)
82 case GRUB_ERR_NONE:
83 ret = 0;
84 break;
86 case GRUB_ERR_OUT_OF_MEMORY:
87 grub_print_error ();
88 ret = -ENOMEM;
89 break;
91 case GRUB_ERR_BAD_FILE_TYPE:
92 /* This could also be EISDIR. Take a guess. */
93 ret = -ENOTDIR;
94 break;
96 case GRUB_ERR_FILE_NOT_FOUND:
97 ret = -ENOENT;
98 break;
100 case GRUB_ERR_FILE_READ_ERROR:
101 case GRUB_ERR_READ_ERROR:
102 case GRUB_ERR_IO:
103 grub_print_error ();
104 ret = -EIO;
105 break;
107 case GRUB_ERR_SYMLINK_LOOP:
108 ret = -ELOOP;
109 break;
111 default:
112 grub_print_error ();
113 ret = -EINVAL;
114 break;
117 /* Any previous errors were handled. */
118 grub_errno = GRUB_ERR_NONE;
120 return ret;
123 /* Context for fuse_getattr. */
124 struct fuse_getattr_ctx
126 char *filename;
127 struct grub_dirhook_info file_info;
128 int file_exists;
131 /* A hook for iterating directories. */
132 static int
133 fuse_getattr_find_file (const char *cur_filename,
134 const struct grub_dirhook_info *info, void *data)
136 struct fuse_getattr_ctx *ctx = data;
138 if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
139 : grub_strcmp (cur_filename, ctx->filename)) == 0)
141 ctx->file_info = *info;
142 ctx->file_exists = 1;
143 return 1;
145 return 0;
148 #if FUSE_USE_VERSION < 30
149 static int
150 fuse_getattr (const char *path, struct stat *st)
151 #else
152 static int
153 fuse_getattr (const char *path, struct stat *st,
154 struct fuse_file_info *fi __attribute__ ((unused)))
155 #endif
157 struct fuse_getattr_ctx ctx;
158 char *pathname, *path2;
160 if (path[0] == '/' && path[1] == 0)
162 st->st_dev = 0;
163 st->st_ino = 0;
164 st->st_mode = 0555 | S_IFDIR;
165 st->st_uid = 0;
166 st->st_gid = 0;
167 st->st_rdev = 0;
168 st->st_size = 0;
169 st->st_blksize = 512;
170 st->st_blocks = (st->st_blksize + 511) >> 9;
171 st->st_atime = st->st_mtime = st->st_ctime = 0;
172 return 0;
175 ctx.file_exists = 0;
177 pathname = xstrdup (path);
179 /* Remove trailing '/'. */
180 while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
181 pathname[grub_strlen (pathname) - 1] = 0;
183 /* Split into path and filename. */
184 ctx.filename = grub_strrchr (pathname, '/');
185 if (! ctx.filename)
187 path2 = grub_strdup ("/");
188 ctx.filename = pathname;
190 else
192 ctx.filename++;
193 path2 = grub_strdup (pathname);
194 path2[ctx.filename - pathname] = 0;
197 /* It's the whole device. */
198 (fs->fs_dir) (dev, path2, fuse_getattr_find_file, &ctx);
200 grub_free (path2);
201 free (pathname);
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, GRUB_FILE_TYPE_GET_SIZE);
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)
252 if ((fi->flags & O_ACCMODE) != O_RDONLY)
253 return -EROFS;
255 grub_file_t file;
256 file = grub_file_open (path, GRUB_FILE_TYPE_MOUNT);
257 if (! file)
258 return translate_error ();
259 files[first_fd++] = file;
260 fi->fh = first_fd;
261 files[first_fd++] = file;
262 grub_errno = GRUB_ERR_NONE;
263 return 0;
266 static int
267 fuse_read (const char *path, char *buf, size_t sz, off_t off,
268 struct fuse_file_info *fi)
270 grub_file_t file = files[fi->fh];
271 grub_ssize_t size;
273 if (off > file->size)
274 return -EINVAL;
276 file->offset = off;
278 size = grub_file_read (file, buf, sz);
279 if (size < 0)
280 return translate_error ();
281 else
283 grub_errno = GRUB_ERR_NONE;
284 return size;
288 static int
289 fuse_release (const char *path, struct fuse_file_info *fi)
291 grub_file_close (files[fi->fh]);
292 files[fi->fh] = NULL;
293 grub_errno = GRUB_ERR_NONE;
294 return 0;
297 /* Context for fuse_readdir. */
298 struct fuse_readdir_ctx
300 const char *path;
301 void *buf;
302 fuse_fill_dir_t fill;
305 /* Helper for fuse_readdir. */
306 static int
307 fuse_readdir_call_fill (const char *filename,
308 const struct grub_dirhook_info *info, void *data)
310 struct fuse_readdir_ctx *ctx = data;
311 struct stat st;
313 grub_memset (&st, 0, sizeof (st));
314 st.st_mode = info->dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
315 if (!info->dir)
317 grub_file_t file;
318 char *tmp;
319 tmp = xasprintf ("%s/%s", ctx->path, filename);
320 file = grub_file_open (tmp, GRUB_FILE_TYPE_GET_SIZE);
321 free (tmp);
322 /* Symlink to directory. */
323 if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
325 grub_errno = GRUB_ERR_NONE;
326 st.st_mode = (0555 | S_IFDIR);
328 else if (!file)
330 grub_errno = GRUB_ERR_NONE;
332 else
334 st.st_size = file->size;
335 grub_file_close (file);
338 st.st_blksize = 512;
339 st.st_blocks = (st.st_size + 511) >> 9;
340 st.st_atime = st.st_mtime = st.st_ctime
341 = info->mtimeset ? info->mtime : 0;
342 #if FUSE_USE_VERSION < 30
343 ctx->fill (ctx->buf, filename, &st, 0);
344 #else
345 ctx->fill (ctx->buf, filename, &st, 0, 0);
346 #endif
347 return 0;
350 #if FUSE_USE_VERSION < 30
351 static int
352 fuse_readdir (const char *path, void *buf,
353 fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi)
354 #else
355 static int
356 fuse_readdir (const char *path, void *buf,
357 fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi,
358 enum fuse_readdir_flags flags __attribute__ ((unused)))
359 #endif
361 struct fuse_readdir_ctx ctx = {
362 .path = path,
363 .buf = buf,
364 .fill = fill
366 char *pathname;
368 pathname = xstrdup (path);
370 /* Remove trailing '/'. */
371 while (pathname [0] && pathname[1]
372 && pathname[grub_strlen (pathname) - 1] == '/')
373 pathname[grub_strlen (pathname) - 1] = 0;
375 (fs->fs_dir) (dev, pathname, fuse_readdir_call_fill, &ctx);
376 free (pathname);
377 grub_errno = GRUB_ERR_NONE;
378 return 0;
381 struct fuse_operations grub_opers = {
382 .getattr = fuse_getattr,
383 .open = fuse_open,
384 .release = fuse_release,
385 .opendir = fuse_opendir,
386 .readdir = fuse_readdir,
387 .read = fuse_read
390 static grub_err_t
391 fuse_init (void)
393 int i;
395 for (i = 0; i < num_disks; i++)
397 char *argv[2];
398 char *host_file;
399 char *loop_name;
400 loop_name = grub_xasprintf ("loop%d", i);
401 if (!loop_name)
402 grub_util_error ("%s", grub_errmsg);
404 host_file = grub_xasprintf ("(host)%s", images[i]);
405 if (!host_file)
406 grub_util_error ("%s", grub_errmsg);
408 argv[0] = loop_name;
409 argv[1] = host_file;
411 if (execute_command ("loopback", 2, argv))
412 grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
414 grub_free (loop_name);
415 grub_free (host_file);
418 if (mount_crypt)
420 char *argv[2] = { xstrdup ("-a"), NULL};
421 if (execute_command ("cryptomount", 1, argv))
422 grub_util_error (_("`cryptomount' command fails: %s"),
423 grub_errmsg);
424 free (argv[0]);
427 grub_lvm_fini ();
428 grub_mdraid09_fini ();
429 grub_mdraid1x_fini ();
430 grub_diskfilter_fini ();
431 grub_diskfilter_init ();
432 grub_mdraid09_init ();
433 grub_mdraid1x_init ();
434 grub_lvm_init ();
436 dev = grub_device_open (0);
437 if (! dev)
438 return grub_errno;
440 fs = grub_fs_probe (dev);
441 if (! fs)
443 grub_device_close (dev);
444 return grub_errno;
447 if (fuse_main (fuse_argc, fuse_args, &grub_opers, NULL))
448 grub_error (GRUB_ERR_IO, "fuse_main failed");
450 for (i = 0; i < num_disks; i++)
452 char *argv[2];
453 char *loop_name;
455 loop_name = grub_xasprintf ("loop%d", i);
456 if (!loop_name)
457 grub_util_error ("%s", grub_errmsg);
459 argv[0] = xstrdup ("-d");
460 argv[1] = loop_name;
462 execute_command ("loopback", 2, argv);
464 grub_free (argv[0]);
465 grub_free (loop_name);
468 return grub_errno;
471 static struct argp_option options[] = {
472 {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
473 {"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
474 {"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
475 {"zfs-key", 'K',
476 /* TRANSLATORS: "prompt" is a keyword. */
477 N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
478 {"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
479 {0, 0, 0, 0, 0, 0}
482 /* Print the version information. */
483 static void
484 print_version (FILE *stream, struct argp_state *state)
486 fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
488 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
490 static error_t
491 argp_parser (int key, char *arg, struct argp_state *state)
493 switch (key)
495 case 'r':
496 root = arg;
497 return 0;
499 case 'K':
500 if (strcmp (arg, "prompt") == 0)
502 char buf[1024];
503 grub_printf ("%s", _("Enter ZFS password: "));
504 if (grub_password_get (buf, 1023))
506 grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
509 else
511 FILE *f;
512 ssize_t real_size;
513 grub_uint8_t buf[1024];
514 f = grub_util_fopen (arg, "rb");
515 if (!f)
517 printf (_("%s: error:"), program_name);
518 printf (_("cannot open `%s': %s"), arg, strerror (errno));
519 printf ("\n");
520 return 0;
522 real_size = fread (buf, 1, 1024, f);
523 if (real_size < 0)
525 printf (_("%s: error:"), program_name);
526 printf (_("cannot read `%s': %s"), arg,
527 strerror (errno));
528 printf ("\n");
529 fclose (f);
530 return 0;
532 grub_zfs_add_key (buf, real_size, 0);
533 fclose (f);
535 return 0;
537 case 'C':
538 mount_crypt = 1;
539 return 0;
541 case 'd':
542 debug_str = arg;
543 return 0;
545 case 'v':
546 verbosity++;
547 return 0;
549 case ARGP_KEY_ARG:
550 if (arg[0] != '-')
551 break;
553 /* FALLTHROUGH */
554 default:
555 if (!arg)
556 return 0;
558 fuse_args = xrealloc (fuse_args, (fuse_argc + 1) * sizeof (fuse_args[0]));
559 fuse_args[fuse_argc] = xstrdup (arg);
560 fuse_argc++;
561 return 0;
564 images = xrealloc (images, (num_disks + 1) * sizeof (images[0]));
565 images[num_disks] = grub_canonicalize_file_name (arg);
566 if (images[num_disks] == NULL)
567 grub_util_error (_("cannot find `%s': %s"), arg, strerror (errno));
568 num_disks++;
570 return 0;
573 struct argp argp = {
574 options, argp_parser, N_("IMAGE1 [IMAGE2 ...] MOUNTPOINT"),
575 N_("Debug tool for filesystem driver."),
576 NULL, NULL, NULL
580 main (int argc, char *argv[])
582 const char *default_root;
583 char *alloc_root;
585 grub_util_host_init (&argc, &argv);
587 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
588 fuse_args[fuse_argc] = xstrdup (argv[0]);
589 fuse_argc++;
590 /* Run single-threaded. */
591 fuse_args[fuse_argc] = xstrdup ("-s");
592 fuse_argc++;
594 argp_parse (&argp, argc, argv, 0, 0, 0);
596 if (num_disks < 2)
597 grub_util_error ("%s", _("need an image and mountpoint"));
598 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
599 fuse_args[fuse_argc] = images[num_disks - 1];
600 fuse_argc++;
601 num_disks--;
602 fuse_args[fuse_argc] = NULL;
604 /* Initialize all modules. */
605 grub_init_all ();
607 if (debug_str)
608 grub_env_set ("debug", debug_str);
610 default_root = (num_disks == 1) ? "loop0" : "md0";
611 alloc_root = 0;
612 if (root)
614 if ((*root >= '0') && (*root <= '9'))
616 alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
618 sprintf (alloc_root, "%s,%s", default_root, root);
619 root = alloc_root;
622 else
623 root = default_root;
625 grub_env_set ("root", root);
627 if (alloc_root)
628 free (alloc_root);
630 /* Do it. */
631 fuse_init ();
632 if (grub_errno)
634 grub_print_error ();
635 return 1;
638 /* Free resources. */
639 grub_fini_all ();
641 return 0;