]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/macbless.c
tpm: Fix bug in GRUB2 TPM module
[grub2.git] / grub-core / commands / macbless.c
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
33 GRUB_MOD_LICENSE ("GPLv3+");
34
35 struct find_node_context
36 {
37 grub_uint64_t inode_found;
38 char *dirname;
39 enum
40 { FOUND_NONE, FOUND_FILE, FOUND_DIR } found;
41 };
42
43 static int
44 find_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 ? FOUND_DIR : FOUND_FILE;
57 }
58 return 0;
59 }
60
61 grub_err_t
62 grub_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 + ((grub_uint64_t) 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
131 grub_err_t
132 grub_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 == FOUND_DIR), intel);
179 }
180
181 static grub_err_t
182 grub_cmd_macbless (grub_command_t cmd, int argc, char **args)
183 {
184 char *device_name;
185 char *path = 0;
186 grub_device_t dev = 0;
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 || !dev)
201 {
202 if (dev)
203 grub_device_close (dev);
204
205 grub_free (device_name);
206
207 return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
208 }
209
210 err = grub_mac_bless_file (dev, path, cmd->name[3] == 't');
211
212 grub_device_close (dev);
213 grub_free (device_name);
214 return err;
215 }
216
217 static grub_command_t cmd, cmd_ppc;
218 \f
219 GRUB_MOD_INIT(macbless)
220 {
221 cmd = grub_register_command ("mactelbless", grub_cmd_macbless,
222 N_("FILE"),
223 N_
224 ("Bless FILE of HFS or HFS+ partition for intel macs."));
225 cmd_ppc =
226 grub_register_command ("macppcbless", grub_cmd_macbless, N_("DIR"),
227 N_
228 ("Bless DIR of HFS or HFS+ partition for PPC macs."));
229 }
230
231 GRUB_MOD_FINI(macbless)
232 {
233 grub_unregister_command (cmd);
234 grub_unregister_command (cmd_ppc);
235 }