source: grub-pc/trunk/fuentes/grub-core/commands/macbless.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

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