1 /* hfsplus.c - HFS+ Filesystem. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 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 http://developer.apple.com/technotes/tn/tn1150.html */
23 #include <grub/file.h>
25 #include <grub/misc.h>
26 #include <grub/disk.h>
28 #include <grub/types.h>
29 #include <grub/fshelp.h>
32 #define GRUB_HFSPLUS_MAGIC 0x482B
33 #define GRUB_HFSPLUSX_MAGIC 0x4858
34 #define GRUB_HFSPLUS_SBLOCK 2
37 struct grub_hfsplus_extent
39 /* The first block of a file on disk. */
41 /* The amount of blocks described by this extent. */
43 } __attribute__ ((packed
));
45 /* The descriptor of a fork. */
46 struct grub_hfsplus_forkdata
49 grub_uint32_t clumpsize
;
51 struct grub_hfsplus_extent extents
[8];
52 } __attribute__ ((packed
));
54 /* The HFS+ Volume Header. */
55 struct grub_hfsplus_volheader
58 grub_uint16_t version
;
59 grub_uint32_t attributes
;
60 grub_uint8_t unused1
[12];
62 grub_uint8_t unused2
[16];
63 grub_uint32_t blksize
;
64 grub_uint8_t unused3
[60];
65 grub_uint64_t num_serial
;
66 struct grub_hfsplus_forkdata allocations_file
;
67 struct grub_hfsplus_forkdata extents_file
;
68 struct grub_hfsplus_forkdata catalog_file
;
69 struct grub_hfsplus_forkdata attrib_file
;
70 struct grub_hfsplus_forkdata startup_file
;
71 } __attribute__ ((packed
));
73 /* The type of node. */
74 enum grub_hfsplus_btnode_type
76 GRUB_HFSPLUS_BTNODE_TYPE_LEAF
= -1,
77 GRUB_HFSPLUS_BTNODE_TYPE_INDEX
= 0,
78 GRUB_HFSPLUS_BTNODE_TYPE_HEADER
= 1,
79 GRUB_HFSPLUS_BTNODE_TYPE_MAP
= 2,
82 struct grub_hfsplus_btnode
90 } __attribute__ ((packed
));
92 /* The header of a HFS+ B+ Tree. */
93 struct grub_hfsplus_btheader
97 grub_uint32_t leaf_records
;
98 grub_uint32_t first_leaf_node
;
99 grub_uint32_t last_leaf_node
;
100 grub_uint16_t nodesize
;
101 grub_uint16_t keysize
;
102 } __attribute__ ((packed
));
104 /* The on disk layout of a catalog key. */
105 struct grub_hfsplus_catkey
107 grub_uint16_t keylen
;
108 grub_uint32_t parent
;
109 grub_uint16_t namelen
;
110 grub_uint16_t name
[30];
111 } __attribute__ ((packed
));
113 /* The on disk layout of an extent overflow file key. */
114 struct grub_hfsplus_extkey
116 grub_uint16_t keylen
;
119 grub_uint32_t fileid
;
121 } __attribute__ ((packed
));
123 struct grub_hfsplus_key
127 struct grub_hfsplus_extkey extkey
;
128 struct grub_hfsplus_catkey catkey
;
129 grub_uint16_t keylen
;
131 } __attribute__ ((packed
));
133 struct grub_hfsplus_catfile
137 grub_uint32_t reserved
;
138 grub_uint32_t fileid
;
139 grub_uint8_t unused1
[4];
141 grub_uint8_t unused2
[22];
143 grub_uint8_t unused3
[44];
144 struct grub_hfsplus_forkdata data
;
145 struct grub_hfsplus_forkdata resource
;
146 } __attribute__ ((packed
));
148 /* Filetype information as used in inodes. */
149 #define GRUB_HFSPLUS_FILEMODE_MASK 0170000
150 #define GRUB_HFSPLUS_FILEMODE_REG 0100000
151 #define GRUB_HFSPLUS_FILEMODE_DIRECTORY 0040000
152 #define GRUB_HFSPLUS_FILEMODE_SYMLINK 0120000
154 /* Some pre-defined file IDs. */
155 #define GRUB_HFSPLUS_FILEID_ROOTDIR 2
156 #define GRUB_HFSPLUS_FILEID_OVERFLOW 3
157 #define GRUB_HFSPLUS_FILEID_CATALOG 4
159 enum grub_hfsplus_filetype
161 GRUB_HFSPLUS_FILETYPE_DIR
= 1,
162 GRUB_HFSPLUS_FILETYPE_REG
= 2,
163 GRUB_HFSPLUS_FILETYPE_DIR_THREAD
= 3,
164 GRUB_HFSPLUS_FILETYPE_REG_THREAD
= 4
167 /* Internal representation of a catalog key. */
168 struct grub_hfsplus_catkey_internal
174 /* Internal representation of an extent overflow key. */
175 struct grub_hfsplus_extkey_internal
177 grub_uint32_t fileid
;
181 struct grub_hfsplus_key_internal
185 struct grub_hfsplus_extkey_internal extkey
;
186 struct grub_hfsplus_catkey_internal catkey
;
192 struct grub_fshelp_node
194 struct grub_hfsplus_data
*data
;
195 struct grub_hfsplus_extent extents
[8];
197 grub_uint32_t fileid
;
201 struct grub_hfsplus_btree
206 /* Catalog file node. */
207 struct grub_fshelp_node file
;
210 /* Information about a "mounted" HFS+ filesystem. */
211 struct grub_hfsplus_data
213 struct grub_hfsplus_volheader volheader
;
216 unsigned int log2blksize
;
218 struct grub_hfsplus_btree catalog_tree
;
219 struct grub_hfsplus_btree extoverflow_tree
;
221 struct grub_fshelp_node dirroot
;
222 struct grub_fshelp_node opened_file
;
224 /* This is the offset into the physical disk for an embedded HFS+
225 filesystem (one inside a plain HFS wrapper). */
230 static grub_dl_t my_mod
;
234 /* Return the offset of the record with the index INDEX, in the node
235 NODE which is part of the B+ tree BTREE. */
236 static inline unsigned int
237 grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree
*btree
,
238 struct grub_hfsplus_btnode
*node
, int index
)
240 char *cnode
= (char *) node
;
241 grub_uint16_t
*recptr
;
242 recptr
= (grub_uint16_t
*) (&cnode
[btree
->nodesize
243 - index
* sizeof (grub_uint16_t
) - 2]);
244 return grub_be_to_cpu16 (*recptr
);
247 /* Return a pointer to the record with the index INDEX, in the node
248 NODE which is part of the B+ tree BTREE. */
249 static inline struct grub_hfsplus_key
*
250 grub_hfsplus_btree_recptr (struct grub_hfsplus_btree
*btree
,
251 struct grub_hfsplus_btnode
*node
, int index
)
253 char *cnode
= (char *) node
;
255 offset
= grub_hfsplus_btree_recoffset (btree
, node
, index
);
256 return (struct grub_hfsplus_key
*) &cnode
[offset
];
260 /* Find the extent that points to FILEBLOCK. If it is not in one of
261 the 8 extents described by EXTENT, return -1. In that case set
262 FILEBLOCK to the next block. */
264 grub_hfsplus_find_block (struct grub_hfsplus_extent
*extent
,
268 grub_size_t blksleft
= *fileblock
;
270 /* First lookup the file in the given extents. */
271 for (i
= 0; i
< 8; i
++)
273 if (blksleft
< grub_be_to_cpu32 (extent
[i
].count
))
274 return grub_be_to_cpu32 (extent
[i
].start
) + blksleft
;
275 blksleft
-= grub_be_to_cpu32 (extent
[i
].count
);
278 *fileblock
= blksleft
;
283 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
284 struct grub_hfsplus_key_internal
*key
,
285 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
286 struct grub_hfsplus_key_internal
*keyb
),
287 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
);
289 static int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
290 struct grub_hfsplus_key_internal
*keyb
);
292 /* Search for the block FILEBLOCK inside the file NODE. Return the
293 blocknumber of this block on disk. */
294 static grub_disk_addr_t
295 grub_hfsplus_read_block (grub_fshelp_node_t node
, grub_disk_addr_t fileblock
)
297 struct grub_hfsplus_btnode
*nnode
= 0;
298 int blksleft
= fileblock
;
299 struct grub_hfsplus_extent
*extents
= &node
->extents
[0];
303 struct grub_hfsplus_extkey
*key
;
304 struct grub_hfsplus_extkey_internal extoverflow
;
308 /* Try to find this block in the current set of extents. */
309 blk
= grub_hfsplus_find_block (extents
, &blksleft
);
311 /* The previous iteration of this loop allocated memory. The
312 code above used this memory, it can be freed now. */
318 + (node
->data
->embedded_offset
>> (node
->data
->log2blksize
319 - GRUB_DISK_SECTOR_BITS
)));
321 /* For the extent overflow file, extra extents can't be found in
322 the extent overflow file. If this happens, you found a
324 if (node
->fileid
== GRUB_HFSPLUS_FILEID_OVERFLOW
)
326 grub_error (GRUB_ERR_READ_ERROR
,
327 "extra extents found in an extend overflow file");
331 /* Set up the key to look for in the extent overflow file. */
332 extoverflow
.fileid
= node
->fileid
;
333 extoverflow
.start
= fileblock
- blksleft
;
335 if (grub_hfsplus_btree_search (&node
->data
->extoverflow_tree
,
336 (struct grub_hfsplus_key_internal
*) &extoverflow
,
337 grub_hfsplus_cmp_extkey
, &nnode
, &ptr
))
339 grub_error (GRUB_ERR_READ_ERROR
,
340 "no block found for the file id 0x%x and the block offset 0x%x",
341 node
->fileid
, fileblock
);
345 /* The extent overflow file has 8 extents right after the key. */
346 key
= (struct grub_hfsplus_extkey
*)
347 grub_hfsplus_btree_recptr (&node
->data
->extoverflow_tree
, nnode
, ptr
);
348 extents
= (struct grub_hfsplus_extent
*) (key
+ 1);
350 /* The block wasn't found. Perhaps the next iteration will find
351 it. The last block we found is stored in BLKSLEFT now. */
356 /* Too bad, you lose. */
361 /* Read LEN bytes from the file described by DATA starting with byte
362 POS. Return the amount of read bytes in READ. */
364 grub_hfsplus_read_file (grub_fshelp_node_t node
,
365 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
366 unsigned offset
, unsigned length
),
367 int pos
, grub_size_t len
, char *buf
)
369 return grub_fshelp_read_file (node
->data
->disk
, node
, read_hook
,
370 pos
, len
, buf
, grub_hfsplus_read_block
,
372 node
->data
->log2blksize
- GRUB_DISK_SECTOR_BITS
);
375 static struct grub_hfsplus_data
*
376 grub_hfsplus_mount (grub_disk_t disk
)
378 struct grub_hfsplus_data
*data
;
379 struct grub_hfsplus_btheader header
;
380 struct grub_hfsplus_btnode node
;
382 struct grub_hfs_sblock hfs
;
383 struct grub_hfsplus_volheader hfsplus
;
386 data
= grub_malloc (sizeof (*data
));
392 /* Read the bootblock. */
393 grub_disk_read (disk
, GRUB_HFSPLUS_SBLOCK
, 0, sizeof (volheader
),
394 (char *) &volheader
);
398 data
->embedded_offset
= 0;
399 if (grub_be_to_cpu16 (volheader
.hfs
.magic
) == GRUB_HFS_MAGIC
)
405 /* See if there's an embedded HFS+ filesystem. */
406 if (grub_be_to_cpu16 (volheader
.hfs
.embed_sig
) != GRUB_HFSPLUS_MAGIC
)
408 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
412 /* Calculate the offset needed to translate HFS+ sector numbers. */
413 extent_start
= grub_be_to_cpu16 (volheader
.hfs
.embed_extent
.first_block
);
414 ablk_size
= grub_be_to_cpu32 (volheader
.hfs
.blksz
);
415 ablk_start
= grub_be_to_cpu16 (volheader
.hfs
.first_block
);
416 data
->embedded_offset
= (ablk_start
418 * (ablk_size
>> GRUB_DISK_SECTOR_BITS
));
420 grub_disk_read (disk
, data
->embedded_offset
+ GRUB_HFSPLUS_SBLOCK
, 0,
421 sizeof (volheader
), (char *) &volheader
);
426 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
428 if ((grub_be_to_cpu16 (volheader
.hfsplus
.magic
) != GRUB_HFSPLUS_MAGIC
)
429 && (grub_be_to_cpu16 (volheader
.hfsplus
.magic
) != GRUB_HFSPLUSX_MAGIC
))
431 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
435 grub_memcpy (&data
->volheader
, &volheader
.hfsplus
,
436 sizeof (volheader
.hfsplus
));
438 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data
->volheader
.blksize
),
442 /* Make a new node for the catalog tree. */
443 data
->catalog_tree
.file
.data
= data
;
444 data
->catalog_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_CATALOG
;
445 grub_memcpy (&data
->catalog_tree
.file
.extents
,
446 data
->volheader
.catalog_file
.extents
,
447 sizeof data
->volheader
.catalog_file
.extents
);
448 data
->catalog_tree
.file
.size
=
449 grub_be_to_cpu64 (data
->volheader
.catalog_file
.size
);
451 /* Make a new node for the extent overflow file. */
452 data
->extoverflow_tree
.file
.data
= data
;
453 data
->extoverflow_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_OVERFLOW
;
454 grub_memcpy (&data
->extoverflow_tree
.file
.extents
,
455 data
->volheader
.extents_file
.extents
,
456 sizeof data
->volheader
.catalog_file
.extents
);
458 data
->extoverflow_tree
.file
.size
=
459 grub_be_to_cpu64 (data
->volheader
.extents_file
.size
);
461 /* Read the essential information about the trees. */
462 if (! grub_hfsplus_read_file (&data
->catalog_tree
.file
, 0,
463 sizeof (struct grub_hfsplus_btnode
),
464 sizeof (header
), (char *) &header
))
467 data
->catalog_tree
.root
= grub_be_to_cpu32 (header
.root
);
468 data
->catalog_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
470 if (! grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0,
471 sizeof (struct grub_hfsplus_btnode
),
472 sizeof (header
), (char *) &header
))
475 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
477 if (! grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0, 0,
478 sizeof (node
), (char *) &node
))
481 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
482 data
->extoverflow_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
484 data
->dirroot
.data
= data
;
485 data
->dirroot
.fileid
= GRUB_HFSPLUS_FILEID_ROOTDIR
;
491 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
492 grub_error (GRUB_ERR_BAD_FS
, "not a hfsplus filesystem");
498 /* Compare the on disk catalog key KEYA with the catalog key we are
499 looking for (KEYB). */
501 grub_hfsplus_cmp_catkey (struct grub_hfsplus_key
*keya
,
502 struct grub_hfsplus_key_internal
*keyb
)
504 struct grub_hfsplus_catkey
*catkey_a
= &keya
->catkey
;
505 struct grub_hfsplus_catkey_internal
*catkey_b
= &keyb
->catkey
;
510 diff
= grub_be_to_cpu32 (catkey_a
->parent
) - catkey_b
->parent
;
514 /* Change the filename in keya so the endianness is correct. */
515 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
516 catkey_a
->name
[i
] = grub_be_to_cpu16 (catkey_a
->name
[i
]);
518 filename
= grub_malloc (grub_be_to_cpu16 (catkey_a
->namelen
) + 1);
520 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey_a
->name
,
521 grub_be_to_cpu16 (catkey_a
->namelen
)))
522 return -1; /* XXX: This error never occurs, but in case it happens
523 just skip this entry. */
525 diff
= grub_strncmp (filename
, catkey_b
->name
,
526 grub_be_to_cpu16 (catkey_a
->namelen
));
528 grub_free (filename
);
530 /* The endianness was changed to host format, change it back to
532 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
533 catkey_a
->name
[i
] = grub_cpu_to_be16 (catkey_a
->name
[i
]);
537 /* Compare the on disk extent overflow key KEYA with the extent
538 overflow key we are looking for (KEYB). */
540 grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
541 struct grub_hfsplus_key_internal
*keyb
)
543 struct grub_hfsplus_extkey
*extkey_a
= &keya
->extkey
;
544 struct grub_hfsplus_extkey_internal
*extkey_b
= &keyb
->extkey
;
547 diff
= grub_be_to_cpu32 (extkey_a
->fileid
) - extkey_b
->fileid
;
552 diff
= grub_be_to_cpu32 (extkey_a
->start
) - extkey_b
->start
;
557 grub_hfsplus_read_symlink (grub_fshelp_node_t node
)
560 grub_ssize_t numread
;
562 symlink
= grub_malloc (node
->size
+ 1);
566 numread
= grub_hfsplus_read_file (node
, 0, 0, node
->size
, symlink
);
567 if (numread
!= (grub_ssize_t
) node
->size
)
572 symlink
[node
->size
] = '\0';
578 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree
*btree
,
579 struct grub_hfsplus_btnode
*first_node
,
581 int (*hook
) (void *record
))
587 char *cnode
= (char *) first_node
;
589 /* Iterate over all records in this node. */
590 for (rec
= first_rec
; rec
< grub_be_to_cpu16 (first_node
->count
); rec
++)
592 if (hook (grub_hfsplus_btree_recptr (btree
, first_node
, rec
)))
596 if (! first_node
->next
)
599 if (! grub_hfsplus_read_file (&btree
->file
, 0,
600 (grub_be_to_cpu32 (first_node
->next
)
602 btree
->nodesize
, cnode
))
605 /* Don't skip any record in the next iteration. */
612 /* Lookup the node described by KEY in the B+ Tree BTREE. Compare
613 keys using the function COMPARE_KEYS. When a match is found,
614 return the node in MATCHNODE and a pointer to the data in this node
615 in KEYOFFSET. MATCHNODE should be freed by the caller. */
617 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
618 struct grub_hfsplus_key_internal
*key
,
619 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
620 struct grub_hfsplus_key_internal
*keyb
),
621 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
)
623 grub_uint64_t currnode
;
625 struct grub_hfsplus_btnode
*nodedesc
;
628 node
= grub_malloc (btree
->nodesize
);
632 currnode
= btree
->root
;
638 if (! grub_hfsplus_read_file (&btree
->file
, 0,
639 (long)currnode
* (long)btree
->nodesize
,
640 btree
->nodesize
, (char *) node
))
646 nodedesc
= (struct grub_hfsplus_btnode
*) node
;
648 /* Find the record in this tree. */
649 for (rec
= 0; rec
< grub_be_to_cpu16 (nodedesc
->count
); rec
++)
651 struct grub_hfsplus_key
*currkey
;
652 currkey
= grub_hfsplus_btree_recptr (btree
, nodedesc
, rec
);
654 /* The action that has to be taken depend on the type of
656 if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_LEAF
657 && compare_keys (currkey
, key
) == 0)
659 /* An exact match was found! */
661 *matchnode
= nodedesc
;
666 else if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_INDEX
)
668 grub_uint32_t
*pointer
;
670 /* The place where the key could have been found didn't
671 contain the key. This means that the previous match
672 is the one that should be followed. */
673 if (compare_keys (currkey
, key
) > 0)
676 /* Mark the last key which is lower or equal to the key
677 that we are looking for. The last match that is
678 found will be used to locate the child which can
679 contain the record. */
680 pointer
= (grub_uint32_t
*) ((char *) currkey
681 + grub_be_to_cpu16 (currkey
->keylen
)
683 currnode
= grub_be_to_cpu32 (*pointer
);
688 /* No match is found, no record with this key exists in the
700 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir
,
702 (*hook
) (const char *filename
,
703 enum grub_fshelp_filetype filetype
,
704 grub_fshelp_node_t node
))
708 auto int list_nodes (void *record
);
709 int list_nodes (void *record
)
711 struct grub_hfsplus_catkey
*catkey
;
714 struct grub_fshelp_node
*node
;
715 struct grub_hfsplus_catfile
*fileinfo
;
716 enum grub_fshelp_filetype type
= GRUB_FSHELP_UNKNOWN
;
718 catkey
= (struct grub_hfsplus_catkey
*) record
;
721 (struct grub_hfsplus_catfile
*) ((char *) record
722 + grub_be_to_cpu16 (catkey
->keylen
)
723 + 2 + (grub_be_to_cpu16(catkey
->keylen
)
726 /* Stop iterating when the last directory entry is found. */
727 if (grub_be_to_cpu32 (catkey
->parent
) != dir
->fileid
)
730 /* Determine the type of the node that is found. */
731 if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_REG
)
733 int mode
= (grub_be_to_cpu16 (fileinfo
->mode
)
734 & GRUB_HFSPLUS_FILEMODE_MASK
);
736 if (mode
== GRUB_HFSPLUS_FILEMODE_REG
)
737 type
= GRUB_FSHELP_REG
;
738 else if (mode
== GRUB_HFSPLUS_FILEMODE_SYMLINK
)
739 type
= GRUB_FSHELP_SYMLINK
;
741 type
= GRUB_FSHELP_UNKNOWN
;
743 else if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_DIR
)
744 type
= GRUB_FSHELP_DIR
;
746 if (type
== GRUB_FSHELP_UNKNOWN
)
749 /* Make sure the byte order of the UTF16 string is correct. */
750 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
752 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
754 /* If the name is obviously invalid, skip this node. */
755 if (catkey
->name
[i
] == 0)
759 filename
= grub_malloc (grub_be_to_cpu16 (catkey
->namelen
) + 1);
763 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey
->name
,
764 grub_be_to_cpu16 (catkey
->namelen
)))
766 grub_free (filename
);
770 filename
[grub_be_to_cpu16 (catkey
->namelen
)] = '\0';
772 /* Restore the byte order to what it was previously. */
773 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
774 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
776 /* hfs+ is case insensitive. */
777 type
|= GRUB_FSHELP_CASE_INSENSITIVE
;
779 /* Only accept valid nodes. */
780 if (grub_strlen (filename
) == grub_be_to_cpu16 (catkey
->namelen
))
782 /* A valid node is found; setup the node and call the
783 callback function. */
784 node
= grub_malloc (sizeof (*node
));
785 node
->data
= dir
->data
;
787 grub_memcpy (node
->extents
, fileinfo
->data
.extents
,
788 sizeof (node
->extents
));
789 node
->mtime
= grub_be_to_cpu32 (fileinfo
->mtime
) - 2082844800;
790 node
->size
= grub_be_to_cpu64 (fileinfo
->data
.size
);
791 node
->fileid
= grub_be_to_cpu32 (fileinfo
->fileid
);
793 ret
= hook (filename
, type
, node
);
796 grub_free (filename
);
801 struct grub_hfsplus_key_internal intern
;
802 struct grub_hfsplus_btnode
*node
;
805 /* Create a key that points to the first entry in the directory. */
806 intern
.catkey
.parent
= dir
->fileid
;
807 intern
.catkey
.name
= "";
809 /* First lookup the first entry. */
810 if (grub_hfsplus_btree_search (&dir
->data
->catalog_tree
, &intern
,
811 grub_hfsplus_cmp_catkey
, &node
, &ptr
))
814 /* Iterate over all entries in this directory. */
815 grub_hfsplus_btree_iterate_node (&dir
->data
->catalog_tree
, node
, ptr
,
823 /* Open a file named NAME and initialize FILE. */
825 grub_hfsplus_open (struct grub_file
*file
, const char *name
)
827 struct grub_hfsplus_data
*data
;
828 struct grub_fshelp_node
*fdiro
= 0;
831 grub_dl_ref (my_mod
);
834 data
= grub_hfsplus_mount (file
->device
->disk
);
838 grub_fshelp_find_file (name
, &data
->dirroot
, &fdiro
,
839 grub_hfsplus_iterate_dir
,
840 grub_hfsplus_read_symlink
, GRUB_FSHELP_REG
);
844 file
->size
= fdiro
->size
;
845 data
->opened_file
= *fdiro
;
854 if (data
&& fdiro
!= &data
->dirroot
)
859 grub_dl_unref (my_mod
);
867 grub_hfsplus_close (grub_file_t file
)
869 grub_free (file
->data
);
872 grub_dl_unref (my_mod
);
875 return GRUB_ERR_NONE
;
879 /* Read LEN bytes data from FILE into BUF. */
881 grub_hfsplus_read (grub_file_t file
, char *buf
, grub_size_t len
)
883 struct grub_hfsplus_data
*data
=
884 (struct grub_hfsplus_data
*) file
->data
;
886 int size
= grub_hfsplus_read_file (&data
->opened_file
, file
->read_hook
,
887 file
->offset
, len
, buf
);
894 grub_hfsplus_dir (grub_device_t device
, const char *path
,
895 int (*hook
) (const char *filename
,
896 const struct grub_dirhook_info
*info
))
898 struct grub_hfsplus_data
*data
= 0;
899 struct grub_fshelp_node
*fdiro
= 0;
901 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
902 enum grub_fshelp_filetype filetype
,
903 grub_fshelp_node_t node
);
905 int NESTED_FUNC_ATTR
iterate (const char *filename
,
906 enum grub_fshelp_filetype filetype
,
907 grub_fshelp_node_t node
)
909 struct grub_dirhook_info info
;
910 grub_memset (&info
, 0, sizeof (info
));
911 info
.dir
= ((filetype
& GRUB_FSHELP_TYPE_MASK
) == GRUB_FSHELP_DIR
);
913 info
.mtime
= node
->mtime
;
914 info
.case_insensitive
= !! (filetype
& GRUB_FSHELP_CASE_INSENSITIVE
);
916 return hook (filename
, &info
);
920 grub_dl_ref (my_mod
);
923 data
= grub_hfsplus_mount (device
->disk
);
927 /* Find the directory that should be opened. */
928 grub_fshelp_find_file (path
, &data
->dirroot
, &fdiro
,
929 grub_hfsplus_iterate_dir
,
930 grub_hfsplus_read_symlink
, GRUB_FSHELP_DIR
);
934 /* Iterate over all entries in this directory. */
935 grub_hfsplus_iterate_dir (fdiro
, iterate
);
938 if (data
&& fdiro
!= &data
->dirroot
)
943 grub_dl_unref (my_mod
);
951 grub_hfsplus_label (grub_device_t device
__attribute__((unused
))
952 , char **label
__attribute__((unused
)))
954 /* XXX: It's not documented how to read a label. */
955 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
956 "reading the label of a HFS+ "
957 "partition is not implemented");
962 grub_hfsplus_mtime (grub_device_t device
, grub_int32_t
*tm
)
964 struct grub_hfsplus_data
*data
;
965 grub_disk_t disk
= device
->disk
;
968 grub_dl_ref (my_mod
);
971 data
= grub_hfsplus_mount (disk
);
975 *tm
= grub_be_to_cpu32 (data
->volheader
.utime
) - 2082844800;
978 grub_dl_unref (my_mod
);
988 grub_hfsplus_uuid (grub_device_t device
, char **uuid
)
990 struct grub_hfsplus_data
*data
;
991 grub_disk_t disk
= device
->disk
;
994 grub_dl_ref (my_mod
);
997 data
= grub_hfsplus_mount (disk
);
1000 *uuid
= grub_malloc (16 + sizeof ('\0'));
1001 grub_sprintf (*uuid
, "%016llx",
1002 (unsigned long long)
1003 grub_be_to_cpu64 (data
->volheader
.num_serial
));
1009 grub_dl_unref (my_mod
);
1019 static struct grub_fs grub_hfsplus_fs
=
1022 .dir
= grub_hfsplus_dir
,
1023 .open
= grub_hfsplus_open
,
1024 .read
= grub_hfsplus_read
,
1025 .close
= grub_hfsplus_close
,
1026 .label
= grub_hfsplus_label
,
1027 .mtime
= grub_hfsplus_mtime
,
1028 .uuid
= grub_hfsplus_uuid
,
1032 GRUB_MOD_INIT(hfsplus
)
1034 grub_fs_register (&grub_hfsplus_fs
);
1040 GRUB_MOD_FINI(hfsplus
)
1042 grub_fs_unregister (&grub_hfsplus_fs
);