grub-macbless.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* grub-probe.c - probe device information for a given path */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/file.h>
  26. #include <grub/fs.h>
  27. #include <grub/partition.h>
  28. #include <grub/msdos_partition.h>
  29. #include <grub/emu/hostdisk.h>
  30. #include <grub/emu/getroot.h>
  31. #include <grub/term.h>
  32. #include <grub/env.h>
  33. #include <grub/diskfilter.h>
  34. #include <grub/i18n.h>
  35. #include <grub/crypto.h>
  36. #include <grub/cryptodisk.h>
  37. #include <grub/hfsplus.h>
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include <sys/stat.h>
  44. #define _GNU_SOURCE 1
  45. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  46. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  47. #include <argp.h>
  48. #pragma GCC diagnostic error "-Wmissing-prototypes"
  49. #pragma GCC diagnostic error "-Wmissing-declarations"
  50. #include "progname.h"
  51. static void
  52. bless (const char *path, int x86)
  53. {
  54. char *drive_name = NULL;
  55. char **devices;
  56. char *grub_path = NULL;
  57. char *filebuf_via_grub = NULL, *filebuf_via_sys = NULL;
  58. grub_device_t dev = NULL;
  59. grub_err_t err;
  60. struct stat st;
  61. grub_path = grub_canonicalize_file_name (path);
  62. if (stat (grub_path, &st) < 0)
  63. grub_util_error (N_("cannot stat `%s': %s"),
  64. grub_path, strerror (errno));
  65. devices = grub_guess_root_devices (grub_path);
  66. if (! devices || !devices[0])
  67. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), path);
  68. drive_name = grub_util_get_grub_dev (devices[0]);
  69. if (! drive_name)
  70. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  71. devices[0]);
  72. grub_util_info ("opening %s", drive_name);
  73. dev = grub_device_open (drive_name);
  74. if (! dev)
  75. grub_util_error ("%s", grub_errmsg);
  76. err = grub_mac_bless_inode (dev, st.st_ino, S_ISDIR (st.st_mode), x86);
  77. if (err)
  78. grub_util_error ("%s", grub_errmsg);
  79. free (grub_path);
  80. free (filebuf_via_grub);
  81. free (filebuf_via_sys);
  82. free (drive_name);
  83. free (devices);
  84. grub_device_close (dev);
  85. }
  86. static struct argp_option options[] = {
  87. {"x86", 'x', 0, 0,
  88. N_("bless for x86-based macs"), 0},
  89. {"ppc", 'p', 0, 0,
  90. N_("bless for ppc-based macs"), 0},
  91. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  92. { 0, 0, 0, 0, 0, 0 }
  93. };
  94. struct arguments
  95. {
  96. char *arg;
  97. int ppc;
  98. };
  99. static error_t
  100. argp_parser (int key, char *arg, struct argp_state *state)
  101. {
  102. /* Get the input argument from argp_parse, which we
  103. know is a pointer to our arguments structure. */
  104. struct arguments *arguments = state->input;
  105. switch (key)
  106. {
  107. case 'v':
  108. verbosity++;
  109. break;
  110. case 'x':
  111. arguments->ppc = 0;
  112. break;
  113. case 'p':
  114. arguments->ppc = 1;
  115. break;
  116. case ARGP_KEY_NO_ARGS:
  117. fprintf (stderr, "%s", _("No path or device is specified.\n"));
  118. argp_usage (state);
  119. break;
  120. case ARGP_KEY_ARG:
  121. if (arguments->arg)
  122. {
  123. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  124. fprintf (stderr, "\n");
  125. return ARGP_ERR_UNKNOWN;
  126. }
  127. arguments->arg = xstrdup (arg);
  128. break;
  129. default:
  130. return ARGP_ERR_UNKNOWN;
  131. }
  132. return 0;
  133. }
  134. static struct argp argp = {
  135. options, argp_parser, N_("--ppc PATH|--x86 FILE"),
  136. N_("Mac-style bless on HFS or HFS+"),
  137. NULL, NULL, NULL
  138. };
  139. int
  140. main (int argc, char *argv[])
  141. {
  142. struct arguments arguments;
  143. grub_util_host_init (&argc, &argv);
  144. /* Check for options. */
  145. memset (&arguments, 0, sizeof (struct arguments));
  146. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  147. {
  148. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  149. exit(1);
  150. }
  151. if (verbosity > 1)
  152. grub_env_set ("debug", "all");
  153. /* Initialize the emulated biosdisk driver. */
  154. grub_util_biosdisk_init (NULL);
  155. /* Initialize all modules. */
  156. grub_init_all ();
  157. grub_gcry_init_all ();
  158. grub_lvm_fini ();
  159. grub_mdraid09_fini ();
  160. grub_mdraid1x_fini ();
  161. grub_diskfilter_fini ();
  162. grub_diskfilter_init ();
  163. grub_mdraid09_init ();
  164. grub_mdraid1x_init ();
  165. grub_lvm_init ();
  166. /* Do it. */
  167. bless (arguments.arg, !arguments.ppc);
  168. /* Free resources. */
  169. grub_gcry_fini_all ();
  170. grub_fini_all ();
  171. grub_util_biosdisk_fini ();
  172. return 0;
  173. }