3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,2007,2008,2009 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/>.
20 /* HFS is documented at
21 http://developer.apple.com/documentation/mac/Files/Files-2.html */
24 #include <grub/file.h>
26 #include <grub/misc.h>
27 #include <grub/disk.h>
29 #include <grub/types.h>
31 #include <grub/i18n.h>
33 GRUB_MOD_LICENSE ("GPLv3+");
35 #define GRUB_HFS_SBLOCK 2
36 #define GRUB_HFS_EMBED_HFSPLUS_SIG 0x482B
38 #define GRUB_HFS_BLKS (data->blksz >> 9)
40 #define GRUB_HFS_NODE_LEAF 0xFF
42 /* The two supported filesystems a record can have. */
45 GRUB_HFS_FILETYPE_DIR
= 1,
46 GRUB_HFS_FILETYPE_FILE
= 2
49 /* Catalog node ID (CNID). */
50 enum grub_hfs_cnid_type
52 GRUB_HFS_CNID_ROOT_PARENT
= 1,
53 GRUB_HFS_CNID_ROOT
= 2,
54 GRUB_HFS_CNID_EXT
= 3,
55 GRUB_HFS_CNID_CAT
= 4,
59 /* A node descriptor. This is the header of every node. */
68 } __attribute__ ((packed
));
70 /* The head of the B*-Tree. */
71 struct grub_hfs_treeheader
73 grub_uint16_t tree_depth
;
74 /* The number of the first node. */
75 grub_uint32_t root_node
;
77 grub_uint32_t first_leaf
;
78 grub_uint32_t last_leaf
;
79 grub_uint16_t node_size
;
80 grub_uint16_t key_size
;
82 grub_uint32_t free_nodes
;
83 grub_uint8_t unused
[76];
84 } __attribute__ ((packed
));
86 /* The state of a mounted HFS filesystem. */
89 struct grub_hfs_sblock sblock
;
91 grub_hfs_datarecord_t extents
;
103 /* The key as used on disk in a catalog tree. This is used to lookup
104 file/directory nodes by parent directory ID and filename. */
105 struct grub_hfs_catalog_key
108 grub_uint32_t parent_dir
;
110 /* Filename length. */
114 grub_uint8_t str
[31];
115 } __attribute__ ((packed
));
117 /* The key as used on disk in a extent overflow tree. Using this key
118 the extents can be looked up using a fileid and logical start block
120 struct grub_hfs_extent_key
122 /* The kind of fork. This is used to store meta information like
123 icons, attributes, etc. We will only use the datafork, which is
125 grub_uint8_t forktype
;
126 grub_uint32_t fileid
;
127 grub_uint16_t first_block
;
128 } __attribute__ ((packed
));
130 /* A directory record. This is used to find out the directory ID. */
131 struct grub_hfs_dirrec
133 /* For a directory, type == 1. */
135 grub_uint8_t unused
[5];
139 } __attribute__ ((packed
));
141 /* Information about a file. */
142 struct grub_hfs_filerec
144 /* For a file, type == 2. */
146 grub_uint8_t unused
[19];
147 grub_uint32_t fileid
;
148 grub_uint8_t unused2
[2];
150 grub_uint8_t unused3
[18];
152 grub_uint8_t unused4
[22];
154 /* The first 3 extents of the file. The other extents can be found
155 in the extent overflow file. */
156 grub_hfs_datarecord_t extents
;
157 } __attribute__ ((packed
));
159 /* A record descriptor, both key and data, used to pass to call back
161 struct grub_hfs_record
169 static grub_dl_t my_mod
;
171 static int grub_hfs_find_node (struct grub_hfs_data
*, char *,
172 grub_uint32_t
, int, char *, int);
174 /* Find block BLOCK of the file FILE in the mounted UFS filesystem
175 DATA. The first 3 extents are described by DAT. If cache is set,
176 using caching to improve non-random reads. */
178 grub_hfs_block (struct grub_hfs_data
*data
, grub_hfs_datarecord_t dat
,
179 int file
, int block
, int cache
)
181 grub_hfs_datarecord_t dr
;
183 struct grub_hfs_extent_key key
;
186 static int cache_file
= 0;
187 static int cache_pos
= 0;
188 static grub_hfs_datarecord_t cache_dr
;
190 grub_memcpy (dr
, dat
, sizeof (dr
));
193 key
.fileid
= grub_cpu_to_be32 (file
);
195 if (cache
&& cache_file
== file
&& block
> cache_pos
)
198 key
.first_block
= grub_cpu_to_be16 (pos
);
199 grub_memcpy (dr
, cache_dr
, sizeof (cache_dr
));
206 /* Try all 3 extents. */
207 for (i
= 0; i
< 3; i
++)
209 /* Check if the block is stored in this extent. */
210 if (grub_be_to_cpu16 (dr
[i
].count
) + pos
> block
)
212 int first
= grub_be_to_cpu16 (dr
[i
].first_block
);
214 /* If the cache is enabled, store the current position
220 grub_memcpy (cache_dr
, dr
, sizeof (cache_dr
));
223 return (grub_be_to_cpu16 (data
->sblock
.first_block
)
224 + (first
+ block
- pos
) * GRUB_HFS_BLKS
);
227 /* Try the next extent. */
228 pos
+= grub_be_to_cpu16 (dr
[i
].count
);
231 /* Lookup the block in the extent overflow file. */
232 key
.first_block
= grub_cpu_to_be16 (pos
);
234 grub_hfs_find_node (data
, (char *) &key
, data
->ext_root
,
235 1, (char *) &dr
, sizeof (dr
));
242 /* Read LEN bytes from the file described by DATA starting with byte
243 POS. Return the amount of read bytes in READ. */
245 grub_hfs_read_file (struct grub_hfs_data
*data
,
246 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
247 unsigned offset
, unsigned length
),
248 grub_off_t pos
, grub_size_t len
, char *buf
)
253 blockcnt
= grub_divmod64 (((len
+ pos
)
254 + data
->blksz
- 1), data
->blksz
, 0);
256 for (i
= grub_divmod64 (pos
, data
->blksz
, 0); i
< blockcnt
; i
++)
258 grub_disk_addr_t blknr
;
260 grub_off_t blockend
= data
->blksz
;
264 grub_divmod64 (pos
, data
->blksz
, &blockoff
);
266 blknr
= grub_hfs_block (data
, data
->extents
, data
->fileid
, i
, 1);
271 if (i
== blockcnt
- 1)
273 grub_divmod64 ((len
+ pos
), data
->blksz
, &blockend
);
275 /* The last portion is exactly EXT2_BLOCK_SIZE (data). */
277 blockend
= data
->blksz
;
281 if (i
== grub_divmod64 (pos
, data
->blksz
, 0))
283 skipfirst
= blockoff
;
284 blockend
-= skipfirst
;
287 /* If the block number is 0 this block is not stored on disk but
288 is zero filled instead. */
291 data
->disk
->read_hook
= read_hook
;
292 grub_disk_read (data
->disk
, blknr
, skipfirst
,
294 data
->disk
->read_hook
= 0;
299 buf
+= data
->blksz
- skipfirst
;
306 /* Mount the filesystem on the disk DISK. */
307 static struct grub_hfs_data
*
308 grub_hfs_mount (grub_disk_t disk
)
310 struct grub_hfs_data
*data
;
311 struct grub_hfs_catalog_key key
;
312 struct grub_hfs_dirrec dir
;
317 struct grub_hfs_node node
;
318 struct grub_hfs_treeheader head
;
321 data
= grub_malloc (sizeof (struct grub_hfs_data
));
325 /* Read the superblock. */
326 if (grub_disk_read (disk
, GRUB_HFS_SBLOCK
, 0,
327 sizeof (struct grub_hfs_sblock
), &data
->sblock
))
330 /* Check if this is a HFS filesystem. */
331 if (grub_be_to_cpu16 (data
->sblock
.magic
) != GRUB_HFS_MAGIC
332 || (data
->sblock
.blksz
& grub_cpu_to_be32_compile_time (0xc00001ff)))
334 grub_error (GRUB_ERR_BAD_FS
, "not an HFS filesystem");
338 /* Check if this is an embedded HFS+ filesystem. */
339 if (grub_be_to_cpu16 (data
->sblock
.embed_sig
) == GRUB_HFS_EMBED_HFSPLUS_SIG
)
341 grub_error (GRUB_ERR_BAD_FS
, "embedded HFS+ filesystem");
345 data
->blksz
= grub_be_to_cpu32 (data
->sblock
.blksz
);
348 /* Lookup the root node of the extent overflow tree. */
349 first_block
= ((grub_be_to_cpu16 (data
->sblock
.extent_recs
[0].first_block
)
351 + grub_be_to_cpu16 (data
->sblock
.first_block
));
353 if (grub_disk_read (data
->disk
, first_block
, 0,
354 sizeof (treehead
), &treehead
))
356 data
->ext_root
= grub_be_to_cpu32 (treehead
.head
.root_node
);
357 data
->ext_size
= grub_be_to_cpu16 (treehead
.head
.node_size
);
359 /* Lookup the root node of the catalog tree. */
360 first_block
= ((grub_be_to_cpu16 (data
->sblock
.catalog_recs
[0].first_block
)
362 + grub_be_to_cpu16 (data
->sblock
.first_block
));
363 if (grub_disk_read (data
->disk
, first_block
, 0,
364 sizeof (treehead
), &treehead
))
366 data
->cat_root
= grub_be_to_cpu32 (treehead
.head
.root_node
);
367 data
->cat_size
= grub_be_to_cpu16 (treehead
.head
.node_size
);
369 /* Lookup the root directory node in the catalog tree using the
371 key
.parent_dir
= grub_cpu_to_be32 (1);
372 key
.strlen
= data
->sblock
.volname
[0];
373 grub_strcpy ((char *) key
.str
, (char *) (data
->sblock
.volname
+ 1));
375 if (grub_hfs_find_node (data
, (char *) &key
, data
->cat_root
,
376 0, (char *) &dir
, sizeof (dir
)) == 0)
378 grub_error (GRUB_ERR_BAD_FS
, "cannot find the HFS root directory");
385 data
->rootdir
= grub_be_to_cpu32 (dir
.dirid
);
391 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
392 grub_error (GRUB_ERR_BAD_FS
, "not a HFS filesystem");
397 /* Compare the K1 and K2 catalog file keys using HFS character ordering. */
399 grub_hfs_cmp_catkeys (struct grub_hfs_catalog_key
*k1
,
400 struct grub_hfs_catalog_key
*k2
)
402 /* Taken from hfsutils 3.2.6 and converted to a readable form */
403 static const unsigned char hfs_charorder
[256] = {
436 [' '] = 32, [0xCA] = 32,
475 ['A'] = 71, ['a'] = 71,
476 [0x88] = 72, [0xCB] = 72,
477 [0x80] = 73, [0x8A] = 73,
478 [0x8B] = 74, [0xCC] = 74,
479 [0x81] = 75, [0x8C] = 75,
480 [0xAE] = 76, [0xBE] = 76,
485 ['B'] = 81, ['b'] = 81,
486 ['C'] = 82, ['c'] = 82,
487 [0x82] = 83, [0x8D] = 83,
488 ['D'] = 84, ['d'] = 84,
489 ['E'] = 85, ['e'] = 85,
490 [0x83] = 86, [0x8E] = 86,
494 ['F'] = 90, ['f'] = 90,
495 ['G'] = 91, ['g'] = 91,
496 ['H'] = 92, ['h'] = 92,
497 ['I'] = 93, ['i'] = 93,
502 ['J'] = 98, ['j'] = 98,
503 ['K'] = 99, ['k'] = 99,
504 ['L'] = 100, ['l'] = 100,
505 ['M'] = 101, ['m'] = 101,
506 ['N'] = 102, ['n'] = 102,
507 [0x84] = 103, [0x96] = 103,
508 ['O'] = 104, ['o'] = 104,
509 [0x85] = 105, [0x9A] = 105,
510 [0x9B] = 106, [0xCD] = 106,
511 [0xAF] = 107, [0xBF] = 107,
512 [0xCE] = 108, [0xCF] = 108,
517 ['P'] = 113, ['p'] = 113,
518 ['Q'] = 114, ['q'] = 114,
519 ['R'] = 115, ['r'] = 115,
520 ['S'] = 116, ['s'] = 116,
522 ['T'] = 118, ['t'] = 118,
523 ['U'] = 119, ['u'] = 119,
524 [0x86] = 120, [0x9F] = 120,
528 ['V'] = 124, ['v'] = 124,
529 ['W'] = 125, ['w'] = 125,
530 ['X'] = 126, ['x'] = 126,
531 ['Y'] = 127, ['y'] = 127,
533 ['Z'] = 129, ['z'] = 129,
623 int minlen
= (k1
->strlen
< k2
->strlen
) ? k1
->strlen
: k2
->strlen
;
625 cmp
= (grub_be_to_cpu32 (k1
->parent_dir
) - grub_be_to_cpu32 (k2
->parent_dir
));
629 for (i
= 0; i
< minlen
; i
++)
631 cmp
= (hfs_charorder
[k1
->str
[i
]] - hfs_charorder
[k2
->str
[i
]]);
636 /* Shorter strings precede long ones. */
637 return (k1
->strlen
- k2
->strlen
);
641 /* Compare the K1 and K2 extent overflow file keys. */
643 grub_hfs_cmp_extkeys (struct grub_hfs_extent_key
*k1
,
644 struct grub_hfs_extent_key
*k2
)
646 int cmp
= k1
->forktype
- k2
->forktype
;
648 cmp
= grub_be_to_cpu32 (k1
->fileid
) - grub_be_to_cpu32 (k2
->fileid
);
650 cmp
= (grub_be_to_cpu16 (k1
->first_block
)
651 - grub_be_to_cpu16 (k2
->first_block
));
656 /* Iterate the records in the node with index IDX in the mounted HFS
657 filesystem DATA. This node holds data of the type TYPE (0 =
658 catalog node, 1 = extent overflow node). If this is set, continue
659 iterating to the next node. For every records, call NODE_HOOK. */
661 grub_hfs_iterate_records (struct grub_hfs_data
*data
, int type
, int idx
,
662 int this, int (*node_hook
) (struct grub_hfs_node
*hnd
,
663 struct grub_hfs_record
*))
665 int nodesize
= type
== 0 ? data
->cat_size
: data
->ext_size
;
669 struct grub_hfs_node node
;
670 char rawnode
[nodesize
];
671 grub_uint16_t offsets
[nodesize
/ 2];
677 struct grub_hfs_extent
*dat
;
680 dat
= (struct grub_hfs_extent
*) (type
== 0
681 ? (&data
->sblock
.catalog_recs
)
682 : (&data
->sblock
.extent_recs
));
684 /* Read the node into memory. */
685 blk
= grub_hfs_block (data
, dat
,
686 (type
== 0) ? GRUB_HFS_CNID_CAT
: GRUB_HFS_CNID_EXT
,
687 idx
/ (data
->blksz
/ nodesize
), 0);
688 blk
+= (idx
% (data
->blksz
/ nodesize
));
692 if (grub_disk_read (data
->disk
, blk
, 0,
693 sizeof (node
), &node
))
696 /* Iterate over all records in this node. */
697 for (i
= 0; i
< grub_be_to_cpu16 (node
.node
.reccnt
); i
++)
699 int pos
= (nodesize
>> 1) - 1 - i
;
704 } __attribute__ ((packed
)) *pnt
;
705 pnt
= (struct pointer
*) (grub_be_to_cpu16 (node
.offsets
[pos
])
708 struct grub_hfs_record rec
=
712 &pnt
->key
+ pnt
->keylen
+(pnt
->keylen
+ 1) % 2,
713 nodesize
- grub_be_to_cpu16 (node
.offsets
[pos
])
717 if (node_hook (&node
.node
, &rec
))
721 idx
= grub_be_to_cpu32 (node
.node
.next
);
722 } while (idx
&& this);
728 /* Lookup a record in the mounted filesystem DATA using the key KEY.
729 The index of the node on top of the tree is IDX. The tree is of
730 the type TYPE (0 = catalog node, 1 = extent overflow node). Return
731 the data in DATAR with a maximum length of DATALEN. */
733 grub_hfs_find_node (struct grub_hfs_data
*data
, char *key
,
734 grub_uint32_t idx
, int type
, char *datar
, int datalen
)
740 auto int node_found (struct grub_hfs_node
*, struct grub_hfs_record
*);
742 int node_found (struct grub_hfs_node
*hnd
, struct grub_hfs_record
*rec
)
747 cmp
= grub_hfs_cmp_catkeys (rec
->key
, (void *) key
);
749 cmp
= grub_hfs_cmp_extkeys (rec
->key
, (void *) key
);
751 /* If the key is smaller or equal to the current node, mark the
752 entry. In case of a non-leaf mode it will be used to lookup
753 the rest of the tree. */
755 found
= grub_be_to_cpu32 (grub_get_unaligned32 (rec
->data
));
756 else /* The key can not be found in the tree. */
759 /* Check if this node is a leaf node. */
760 if (hnd
->type
== GRUB_HFS_NODE_LEAF
)
769 grub_memcpy (datar
, rec
->data
,
770 rec
->datalen
< datalen
? rec
->datalen
: datalen
);
782 if (grub_hfs_iterate_records (data
, type
, idx
, 0, node_found
))
795 /* Iterate over the directory with the id DIR. The tree is searched
796 starting with the node ROOT_IDX. For every entry in this directory
799 grub_hfs_iterate_dir (struct grub_hfs_data
*data
, grub_uint32_t root_idx
,
800 unsigned int dir
, int (*hook
) (struct grub_hfs_record
*))
806 /* The lowest key possible with DIR as root directory. */
807 struct grub_hfs_catalog_key key
= {0, grub_cpu_to_be32 (dir
), 0, ""};
809 auto int node_found (struct grub_hfs_node
*, struct grub_hfs_record
*);
810 auto int it_dir (struct grub_hfs_node
* __attribute ((unused
)),
811 struct grub_hfs_record
*);
814 int node_found (struct grub_hfs_node
*hnd
, struct grub_hfs_record
*rec
)
816 struct grub_hfs_catalog_key
*ckey
= rec
->key
;
818 if (grub_hfs_cmp_catkeys (rec
->key
, (void *) &key
) <= 0)
819 found
= grub_be_to_cpu32 (grub_get_unaligned32 (rec
->data
));
821 if (hnd
->type
== 0xFF && ckey
->strlen
> 0)
824 next
= grub_be_to_cpu32 (hnd
->next
);
826 /* An entry was found. */
827 if (grub_be_to_cpu32 (ckey
->parent_dir
) == dir
)
834 int it_dir (struct grub_hfs_node
*hnd
__attribute ((unused
)),
835 struct grub_hfs_record
*rec
)
837 struct grub_hfs_catalog_key
*ckey
= rec
->key
;
838 struct grub_hfs_catalog_key
*origkey
= &key
;
840 /* Stop when the entries do not match anymore. */
841 if (grub_be_to_cpu32 (ckey
->parent_dir
)
842 != grub_be_to_cpu32 ((origkey
)->parent_dir
))
852 if (grub_hfs_iterate_records (data
, 0, root_idx
, 0, node_found
))
861 /* If there was a matching record in this leaf node, continue the
862 iteration until the last record was found. */
863 grub_hfs_iterate_records (data
, 0, next
, 1, it_dir
);
867 #define MAX_UTF8_PER_MAC_ROMAN 3
869 static const char macroman
[0x80][MAX_UTF8_PER_MAC_ROMAN
+ 1] =
903 /* A0 */ "\xe2\x80\xa0",
908 /* A5 */ "\xe2\x80\xa2",
913 /* AA */ "\xe2\x84\xa2",
916 /* AD */ "\xe2\x89\xa0",
919 /* B0 */ "\xe2\x88\x9e",
921 /* B2 */ "\xe2\x89\xa4",
922 /* B3 */ "\xe2\x89\xa5",
925 /* B6 */ "\xe2\x88\x82",
926 /* B7 */ "\xe2\x88\x91",
927 /* B8 */ "\xe2\x88\x8f",
929 /* BA */ "\xe2\x88\xab",
938 /* C3 */ "\xe2\x88\x9a",
940 /* C5 */ "\xe2\x89\x88",
941 /* C6 */ "\xe2\x88\x86",
944 /* C9 */ "\xe2\x80\xa6",
951 /* D0 */ "\xe2\x80\x93",
952 /* D1 */ "\xe2\x80\x94",
953 /* D2 */ "\xe2\x80\x9c",
954 /* D3 */ "\xe2\x80\x9d",
955 /* D4 */ "\xe2\x80\x98",
956 /* D5 */ "\xe2\x80\x99",
958 /* D7 */ "\xe2\x97\x8a",
961 /* DA */ "\xe2\x81\x84",
962 /* DB */ "\xe2\x82\xac",
963 /* DC */ "\xe2\x80\xb9",
964 /* DD */ "\xe2\x80\xba",
965 /* DE */ "\xef\xac\x81",
966 /* DF */ "\xef\xac\x82",
967 /* E0 */ "\xe2\x80\xa1",
969 /* E2 */ "\xe2\x80\x9a",
970 /* E3 */ "\xe2\x80\x9e",
971 /* E4 */ "\xe2\x80\xb0",
983 /* F0 */ "\xef\xa3\xbf",
1002 macroman_to_utf8 (char *to
, const grub_uint8_t
*from
, grub_size_t len
,
1003 int translate_slash
)
1006 const grub_uint8_t
*iptr
;
1008 for (iptr
= from
; iptr
< from
+ len
&& *iptr
; iptr
++)
1010 /* Translate '/' to ':' as per HFS spec. */
1011 if (*iptr
== '/' && translate_slash
)
1016 if (!(*iptr
& 0x80))
1021 optr
= grub_stpcpy (optr
, macroman
[*iptr
& 0x7f]);
1027 utf8_to_macroman (grub_uint8_t
*to
, const char *from
)
1029 grub_uint8_t
*end
= to
+ 31;
1030 grub_uint8_t
*optr
= to
;
1031 const char *iptr
= from
;
1033 while (*iptr
&& optr
< end
)
1036 /* Translate ':' to '/' as per HFS spec. */
1043 if (!(*iptr
& 0x80))
1049 if ((*iptr
& 0xf0) == 0xe0)
1051 for (i
= 0; i
< 0x80; i
++)
1052 if (grub_memcmp (macroman
[i
], iptr
, clen
) == 0)
1059 /* Too long or not encodable. */
1066 /* Find a file or directory with the pathname PATH in the filesystem
1067 DATA. Return the file record in RETDATA when it is non-zero.
1068 Return the directory number in RETINODE when it is non-zero. */
1070 grub_hfs_find_dir (struct grub_hfs_data
*data
, const char *path
,
1071 struct grub_hfs_filerec
*retdata
, int *retinode
)
1073 int inode
= data
->rootdir
;
1077 struct grub_hfs_filerec frec
;
1078 struct grub_hfs_dirrec dir
;
1081 fdrec
.frec
.type
= GRUB_HFS_FILETYPE_DIR
;
1085 grub_error (GRUB_ERR_BAD_FILENAME
, N_("invalid file name `%s'"), path
);
1089 origpath
= grub_strdup (path
);
1094 while (*path
== '/')
1097 while (path
&& grub_strlen (path
))
1100 if (fdrec
.frec
.type
!= GRUB_HFS_FILETYPE_DIR
)
1102 grub_error (GRUB_ERR_BAD_FILE_TYPE
, N_("not a directory"));
1106 /* Isolate a part of the path. */
1107 next
= grub_strchr (path
, '/');
1110 while (*next
== '/')
1114 struct grub_hfs_catalog_key key
;
1116 key
.parent_dir
= grub_cpu_to_be32 (inode
);
1117 slen
= utf8_to_macroman (key
.str
, path
);
1120 grub_error (GRUB_ERR_FILE_NOT_FOUND
, N_("file `%s' not found"), path
);
1125 /* Lookup this node. */
1126 if (! grub_hfs_find_node (data
, (char *) &key
, data
->cat_root
,
1127 0, (char *) &fdrec
.frec
, sizeof (fdrec
.frec
)))
1129 grub_error (GRUB_ERR_FILE_NOT_FOUND
, N_("file `%s' not found"), origpath
);
1136 inode
= grub_be_to_cpu32 (fdrec
.dir
.dirid
);
1141 grub_memcpy (retdata
, &fdrec
.frec
, sizeof (fdrec
.frec
));
1147 grub_free (origpath
);
1154 grub_hfs_dir (grub_device_t device
, const char *path
,
1155 int (*hook
) (const char *filename
,
1156 const struct grub_dirhook_info
*info
))
1160 auto int dir_hook (struct grub_hfs_record
*rec
);
1162 int dir_hook (struct grub_hfs_record
*rec
)
1164 struct grub_hfs_dirrec
*drec
= rec
->data
;
1165 struct grub_hfs_filerec
*frec
= rec
->data
;
1166 struct grub_hfs_catalog_key
*ckey
= rec
->key
;
1167 char fname
[sizeof (ckey
->str
) * MAX_UTF8_PER_MAC_ROMAN
+ 1];
1168 struct grub_dirhook_info info
;
1171 grub_memset (fname
, 0, sizeof (fname
));
1173 grub_memset (&info
, 0, sizeof (info
));
1176 if (len
> sizeof (ckey
->str
))
1177 len
= sizeof (ckey
->str
);
1178 macroman_to_utf8 (fname
, ckey
->str
, len
, 1);
1180 info
.case_insensitive
= 1;
1182 if (drec
->type
== GRUB_HFS_FILETYPE_DIR
)
1186 info
.mtime
= grub_be_to_cpu32 (drec
->mtime
) - 2082844800;
1187 return hook (fname
, &info
);
1189 if (frec
->type
== GRUB_HFS_FILETYPE_FILE
)
1193 info
.mtime
= grub_be_to_cpu32 (frec
->mtime
) - 2082844800;
1194 return hook (fname
, &info
);
1200 struct grub_hfs_data
*data
;
1201 struct grub_hfs_filerec frec
;
1203 grub_dl_ref (my_mod
);
1205 data
= grub_hfs_mount (device
->disk
);
1209 /* First the directory ID for the directory. */
1210 if (grub_hfs_find_dir (data
, path
, &frec
, &inode
))
1213 if (frec
.type
!= GRUB_HFS_FILETYPE_DIR
)
1215 grub_error (GRUB_ERR_BAD_FILE_TYPE
, N_("not a directory"));
1219 grub_hfs_iterate_dir (data
, data
->cat_root
, inode
, dir_hook
);
1224 grub_dl_unref (my_mod
);
1230 /* Open a file named NAME and initialize FILE. */
1232 grub_hfs_open (struct grub_file
*file
, const char *name
)
1234 struct grub_hfs_data
*data
;
1235 struct grub_hfs_filerec frec
;
1237 grub_dl_ref (my_mod
);
1239 data
= grub_hfs_mount (file
->device
->disk
);
1241 if (grub_hfs_find_dir (data
, name
, &frec
, 0))
1244 grub_dl_unref (my_mod
);
1248 if (frec
.type
!= GRUB_HFS_FILETYPE_FILE
)
1251 grub_error (GRUB_ERR_BAD_FILE_TYPE
, N_("not a regular file"));
1252 grub_dl_unref (my_mod
);
1256 grub_memcpy (data
->extents
, frec
.extents
, sizeof (grub_hfs_datarecord_t
));
1257 file
->size
= grub_be_to_cpu32 (frec
.size
);
1258 data
->size
= grub_be_to_cpu32 (frec
.size
);
1259 data
->fileid
= grub_be_to_cpu32 (frec
.fileid
);
1268 grub_hfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
1270 struct grub_hfs_data
*data
=
1271 (struct grub_hfs_data
*) file
->data
;
1273 return grub_hfs_read_file (data
, file
->read_hook
, file
->offset
, len
, buf
);
1278 grub_hfs_close (grub_file_t file
)
1280 grub_free (file
->data
);
1282 grub_dl_unref (my_mod
);
1289 grub_hfs_label (grub_device_t device
, char **label
)
1291 struct grub_hfs_data
*data
;
1293 data
= grub_hfs_mount (device
->disk
);
1297 grub_size_t len
= data
->sblock
.volname
[0];
1298 if (len
> sizeof (data
->sblock
.volname
) - 1)
1299 len
= sizeof (data
->sblock
.volname
) - 1;
1300 *label
= grub_malloc (len
* MAX_UTF8_PER_MAC_ROMAN
+ 1);
1302 macroman_to_utf8 (*label
, data
->sblock
.volname
+ 1,
1313 grub_hfs_mtime (grub_device_t device
, grub_int32_t
*tm
)
1315 struct grub_hfs_data
*data
;
1317 data
= grub_hfs_mount (device
->disk
);
1320 *tm
= grub_be_to_cpu32 (data
->sblock
.mtime
) - 2082844800;
1329 grub_hfs_uuid (grub_device_t device
, char **uuid
)
1331 struct grub_hfs_data
*data
;
1333 grub_dl_ref (my_mod
);
1335 data
= grub_hfs_mount (device
->disk
);
1336 if (data
&& data
->sblock
.num_serial
!= 0)
1338 *uuid
= grub_xasprintf ("%016llx",
1339 (unsigned long long)
1340 grub_be_to_cpu64 (data
->sblock
.num_serial
));
1345 grub_dl_unref (my_mod
);
1354 static struct grub_fs grub_hfs_fs
=
1357 .dir
= grub_hfs_dir
,
1358 .open
= grub_hfs_open
,
1359 .read
= grub_hfs_read
,
1360 .close
= grub_hfs_close
,
1361 .label
= grub_hfs_label
,
1362 .uuid
= grub_hfs_uuid
,
1363 .mtime
= grub_hfs_mtime
,
1365 .reserved_first_sector
= 1,
1366 .blocklist_install
= 1,
1373 grub_fs_register (&grub_hfs_fs
);
1379 grub_fs_unregister (&grub_hfs_fs
);