macbless.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* hfspbless.c - set the hfs+ boot directory. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2007,2008,2009,2012,2013 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 <grub/command.h>
  20. #include <grub/fs.h>
  21. #include <grub/misc.h>
  22. #include <grub/dl.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/hfsplus.h>
  26. #include <grub/hfs.h>
  27. #include <grub/partition.h>
  28. #include <grub/file.h>
  29. #include <grub/mm.h>
  30. #include <grub/err.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. struct find_node_context
  33. {
  34. grub_uint64_t inode_found;
  35. char *dirname;
  36. enum
  37. { FOUND_NONE, FOUND_FILE, FOUND_DIR } found;
  38. };
  39. static int
  40. find_inode (const char *filename,
  41. const struct grub_dirhook_info *info, void *data)
  42. {
  43. struct find_node_context *ctx = data;
  44. if (!info->inodeset)
  45. return 0;
  46. if ((grub_strcmp (ctx->dirname, filename) == 0
  47. || (info->case_insensitive
  48. && grub_strcasecmp (ctx->dirname, filename) == 0)))
  49. {
  50. ctx->inode_found = info->inode;
  51. ctx->found = info->dir ? FOUND_DIR : FOUND_FILE;
  52. }
  53. return 0;
  54. }
  55. grub_err_t
  56. grub_mac_bless_inode (grub_device_t dev, grub_uint32_t inode, int is_dir,
  57. int intel)
  58. {
  59. grub_err_t err;
  60. union
  61. {
  62. struct grub_hfs_sblock hfs;
  63. struct grub_hfsplus_volheader hfsplus;
  64. } volheader;
  65. grub_disk_addr_t embedded_offset;
  66. if (intel && is_dir)
  67. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  68. "can't bless a directory for mactel");
  69. if (!intel && !is_dir)
  70. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  71. "can't bless a file for mac PPC");
  72. /* Read the bootblock. */
  73. err = grub_disk_read (dev->disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader),
  74. (char *) &volheader);
  75. if (err)
  76. return err;
  77. embedded_offset = 0;
  78. if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC)
  79. {
  80. int extent_start;
  81. int ablk_size;
  82. int ablk_start;
  83. /* See if there's an embedded HFS+ filesystem. */
  84. if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
  85. {
  86. if (intel)
  87. volheader.hfs.intel_bootfile = grub_be_to_cpu32 (inode);
  88. else
  89. volheader.hfs.ppc_bootdir = grub_be_to_cpu32 (inode);
  90. return GRUB_ERR_NONE;
  91. }
  92. /* Calculate the offset needed to translate HFS+ sector numbers. */
  93. extent_start =
  94. grub_be_to_cpu16 (volheader.hfs.embed_extent.first_block);
  95. ablk_size = grub_be_to_cpu32 (volheader.hfs.blksz);
  96. ablk_start = grub_be_to_cpu16 (volheader.hfs.first_block);
  97. embedded_offset = (ablk_start
  98. + ((grub_uint64_t) extent_start)
  99. * (ablk_size >> GRUB_DISK_SECTOR_BITS));
  100. err =
  101. grub_disk_read (dev->disk, embedded_offset + GRUB_HFSPLUS_SBLOCK, 0,
  102. sizeof (volheader), (char *) &volheader);
  103. if (err)
  104. return err;
  105. }
  106. if ((grub_be_to_cpu16 (volheader.hfsplus.magic) != GRUB_HFSPLUS_MAGIC)
  107. && (grub_be_to_cpu16 (volheader.hfsplus.magic) != GRUB_HFSPLUSX_MAGIC))
  108. return grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
  109. if (intel)
  110. volheader.hfsplus.intel_bootfile = grub_be_to_cpu32 (inode);
  111. else
  112. volheader.hfsplus.ppc_bootdir = grub_be_to_cpu32 (inode);
  113. return grub_disk_write (dev->disk, embedded_offset + GRUB_HFSPLUS_SBLOCK, 0,
  114. sizeof (volheader), (char *) &volheader);
  115. }
  116. grub_err_t
  117. grub_mac_bless_file (grub_device_t dev, const char *path_in, int intel)
  118. {
  119. grub_fs_t fs;
  120. char *path, *tail;
  121. struct find_node_context ctx;
  122. fs = grub_fs_probe (dev);
  123. if (!fs || (grub_strcmp (fs->name, "hfsplus") != 0
  124. && grub_strcmp (fs->name, "hfs") != 0))
  125. return grub_error (GRUB_ERR_BAD_FS, "no suitable FS found");
  126. path = grub_strdup (path_in);
  127. if (!path)
  128. return grub_errno;
  129. tail = path + grub_strlen (path) - 1;
  130. /* Remove trailing '/'. */
  131. while (tail != path && *tail == '/')
  132. *(tail--) = 0;
  133. tail = grub_strrchr (path, '/');
  134. ctx.found = 0;
  135. if (tail)
  136. {
  137. *tail = 0;
  138. ctx.dirname = tail + 1;
  139. (fs->dir) (dev, *path == 0 ? "/" : path, find_inode, &ctx);
  140. }
  141. else
  142. {
  143. ctx.dirname = path + 1;
  144. (fs->dir) (dev, "/", find_inode, &ctx);
  145. }
  146. if (!ctx.found)
  147. {
  148. grub_free (path);
  149. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
  150. path_in);
  151. }
  152. grub_free (path);
  153. return grub_mac_bless_inode (dev, (grub_uint32_t) ctx.inode_found,
  154. (ctx.found == FOUND_DIR), intel);
  155. }
  156. static grub_err_t
  157. grub_cmd_macbless (grub_command_t cmd, int argc, char **args)
  158. {
  159. char *device_name;
  160. char *path = 0;
  161. grub_device_t dev = 0;
  162. grub_err_t err;
  163. if (argc != 1)
  164. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  165. device_name = grub_file_get_device_name (args[0]);
  166. dev = grub_device_open (device_name);
  167. path = grub_strchr (args[0], ')');
  168. if (!path)
  169. path = args[0];
  170. else
  171. path = path + 1;
  172. if (!path || *path == 0 || !dev)
  173. {
  174. if (dev)
  175. grub_device_close (dev);
  176. grub_free (device_name);
  177. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
  178. }
  179. err = grub_mac_bless_file (dev, path, cmd->name[3] == 't');
  180. grub_device_close (dev);
  181. grub_free (device_name);
  182. return err;
  183. }
  184. static grub_command_t cmd, cmd_ppc;
  185. GRUB_MOD_INIT(macbless)
  186. {
  187. cmd = grub_register_command ("mactelbless", grub_cmd_macbless,
  188. N_("FILE"),
  189. N_
  190. ("Bless FILE of HFS or HFS+ partition for intel macs."));
  191. cmd_ppc =
  192. grub_register_command ("macppcbless", grub_cmd_macbless, N_("DIR"),
  193. N_
  194. ("Bless DIR of HFS or HFS+ partition for PPC macs."));
  195. }
  196. GRUB_MOD_FINI(macbless)
  197. {
  198. grub_unregister_command (cmd);
  199. grub_unregister_command (cmd_ppc);
  200. }