1 /* hfsplus.c - HFS+ Filesystem. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008 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 unused
[32];
61 grub_uint32_t blksize
;
62 grub_uint8_t unused2
[68];
63 struct grub_hfsplus_forkdata allocations_file
;
64 struct grub_hfsplus_forkdata extents_file
;
65 struct grub_hfsplus_forkdata catalog_file
;
66 struct grub_hfsplus_forkdata attrib_file
;
67 struct grub_hfsplus_forkdata startup_file
;
68 } __attribute__ ((packed
));
70 /* The type of node. */
71 enum grub_hfsplus_btnode_type
73 GRUB_HFSPLUS_BTNODE_TYPE_LEAF
= -1,
74 GRUB_HFSPLUS_BTNODE_TYPE_INDEX
= 0,
75 GRUB_HFSPLUS_BTNODE_TYPE_HEADER
= 1,
76 GRUB_HFSPLUS_BTNODE_TYPE_MAP
= 2,
79 struct grub_hfsplus_btnode
87 } __attribute__ ((packed
));
89 /* The header of a HFS+ B+ Tree. */
90 struct grub_hfsplus_btheader
94 grub_uint32_t leaf_records
;
95 grub_uint32_t first_leaf_node
;
96 grub_uint32_t last_leaf_node
;
97 grub_uint16_t nodesize
;
98 grub_uint16_t keysize
;
99 } __attribute__ ((packed
));
101 /* The on disk layout of a catalog key. */
102 struct grub_hfsplus_catkey
104 grub_uint16_t keylen
;
105 grub_uint32_t parent
;
106 grub_uint16_t namelen
;
107 grub_uint16_t name
[30];
108 } __attribute__ ((packed
));
110 /* The on disk layout of an extent overflow file key. */
111 struct grub_hfsplus_extkey
113 grub_uint16_t keylen
;
116 grub_uint32_t fileid
;
118 } __attribute__ ((packed
));
120 struct grub_hfsplus_key
124 struct grub_hfsplus_extkey extkey
;
125 struct grub_hfsplus_catkey catkey
;
126 grub_uint16_t keylen
;
128 } __attribute__ ((packed
));
130 struct grub_hfsplus_catfile
134 grub_uint32_t reserved
;
135 grub_uint32_t fileid
;
136 grub_uint8_t unused1
[30];
138 grub_uint8_t unused2
[44];
139 struct grub_hfsplus_forkdata data
;
140 struct grub_hfsplus_forkdata resource
;
141 } __attribute__ ((packed
));
143 /* Filetype information as used in inodes. */
144 #define GRUB_HFSPLUS_FILEMODE_MASK 0170000
145 #define GRUB_HFSPLUS_FILEMODE_REG 0100000
146 #define GRUB_HFSPLUS_FILEMODE_DIRECTORY 0040000
147 #define GRUB_HFSPLUS_FILEMODE_SYMLINK 0120000
149 /* Some pre-defined file IDs. */
150 #define GRUB_HFSPLUS_FILEID_ROOTDIR 2
151 #define GRUB_HFSPLUS_FILEID_OVERFLOW 3
152 #define GRUB_HFSPLUS_FILEID_CATALOG 4
154 enum grub_hfsplus_filetype
156 GRUB_HFSPLUS_FILETYPE_DIR
= 1,
157 GRUB_HFSPLUS_FILETYPE_REG
= 2,
158 GRUB_HFSPLUS_FILETYPE_DIR_THREAD
= 3,
159 GRUB_HFSPLUS_FILETYPE_REG_THREAD
= 4
162 /* Internal representation of a catalog key. */
163 struct grub_hfsplus_catkey_internal
169 /* Internal representation of an extent overflow key. */
170 struct grub_hfsplus_extkey_internal
172 grub_uint32_t fileid
;
176 struct grub_hfsplus_key_internal
180 struct grub_hfsplus_extkey_internal extkey
;
181 struct grub_hfsplus_catkey_internal catkey
;
187 struct grub_fshelp_node
189 struct grub_hfsplus_data
*data
;
190 struct grub_hfsplus_extent extents
[8];
192 grub_uint32_t fileid
;
195 struct grub_hfsplus_btree
200 /* Catalog file node. */
201 struct grub_fshelp_node file
;
204 /* Information about a "mounted" HFS+ filesystem. */
205 struct grub_hfsplus_data
207 struct grub_hfsplus_volheader volheader
;
210 unsigned int log2blksize
;
212 struct grub_hfsplus_btree catalog_tree
;
213 struct grub_hfsplus_btree extoverflow_tree
;
215 struct grub_fshelp_node dirroot
;
216 struct grub_fshelp_node opened_file
;
218 /* This is the offset into the physical disk for an embedded HFS+
219 filesystem (one inside a plain HFS wrapper). */
224 static grub_dl_t my_mod
;
228 /* Return the offset of the record with the index INDEX, in the node
229 NODE which is part of the B+ tree BTREE. */
230 static inline unsigned int
231 grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree
*btree
,
232 struct grub_hfsplus_btnode
*node
, int index
)
234 char *cnode
= (char *) node
;
235 grub_uint16_t
*recptr
;
236 recptr
= (grub_uint16_t
*) (&cnode
[btree
->nodesize
237 - index
* sizeof (grub_uint16_t
) - 2]);
238 return grub_be_to_cpu16 (*recptr
);
241 /* Return a pointer to the record with the index INDEX, in the node
242 NODE which is part of the B+ tree BTREE. */
243 static inline struct grub_hfsplus_key
*
244 grub_hfsplus_btree_recptr (struct grub_hfsplus_btree
*btree
,
245 struct grub_hfsplus_btnode
*node
, int index
)
247 char *cnode
= (char *) node
;
249 offset
= grub_hfsplus_btree_recoffset (btree
, node
, index
);
250 return (struct grub_hfsplus_key
*) &cnode
[offset
];
254 /* Find the extent that points to FILEBLOCK. If it is not in one of
255 the 8 extents described by EXTENT, return -1. In that case set
256 FILEBLOCK to the next block. */
258 grub_hfsplus_find_block (struct grub_hfsplus_extent
*extent
,
262 grub_size_t blksleft
= *fileblock
;
264 /* First lookup the file in the given extents. */
265 for (i
= 0; i
< 8; i
++)
267 if (blksleft
< grub_be_to_cpu32 (extent
[i
].count
))
268 return grub_be_to_cpu32 (extent
[i
].start
) + blksleft
;
269 blksleft
-= grub_be_to_cpu32 (extent
[i
].count
);
272 *fileblock
= blksleft
;
277 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
278 struct grub_hfsplus_key_internal
*key
,
279 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
280 struct grub_hfsplus_key_internal
*keyb
),
281 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
);
283 static int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
284 struct grub_hfsplus_key_internal
*keyb
);
286 /* Search for the block FILEBLOCK inside the file NODE. Return the
287 blocknumber of this block on disk. */
288 static grub_disk_addr_t
289 grub_hfsplus_read_block (grub_fshelp_node_t node
, grub_disk_addr_t fileblock
)
291 struct grub_hfsplus_btnode
*nnode
= 0;
292 int blksleft
= fileblock
;
293 struct grub_hfsplus_extent
*extents
= &node
->extents
[0];
297 struct grub_hfsplus_extkey
*key
;
298 struct grub_hfsplus_extkey_internal extoverflow
;
302 /* Try to find this block in the current set of extents. */
303 blk
= grub_hfsplus_find_block (extents
, &blksleft
);
305 /* The previous iteration of this loop allocated memory. The
306 code above used this memory, it can be freed now. */
312 + (node
->data
->embedded_offset
>> (node
->data
->log2blksize
313 - GRUB_DISK_SECTOR_BITS
)));
315 /* For the extent overflow file, extra extents can't be found in
316 the extent overflow file. If this happens, you found a
318 if (node
->fileid
== GRUB_HFSPLUS_FILEID_OVERFLOW
)
320 grub_error (GRUB_ERR_READ_ERROR
,
321 "extra extents found in an extend overflow file");
325 /* Set up the key to look for in the extent overflow file. */
326 extoverflow
.fileid
= node
->fileid
;
327 extoverflow
.start
= fileblock
- blksleft
;
329 if (grub_hfsplus_btree_search (&node
->data
->extoverflow_tree
,
330 (struct grub_hfsplus_key_internal
*) &extoverflow
,
331 grub_hfsplus_cmp_extkey
, &nnode
, &ptr
))
333 grub_error (GRUB_ERR_READ_ERROR
,
334 "no block found for the file id 0x%x and the block offset 0x%x",
335 node
->fileid
, fileblock
);
339 /* The extent overflow file has 8 extents right after the key. */
340 key
= (struct grub_hfsplus_extkey
*)
341 grub_hfsplus_btree_recptr (&node
->data
->extoverflow_tree
, nnode
, ptr
);
342 extents
= (struct grub_hfsplus_extent
*) (key
+ 1);
344 /* The block wasn't found. Perhaps the next iteration will find
345 it. The last block we found is stored in BLKSLEFT now. */
350 /* Too bad, you lose. */
355 /* Read LEN bytes from the file described by DATA starting with byte
356 POS. Return the amount of read bytes in READ. */
358 grub_hfsplus_read_file (grub_fshelp_node_t node
,
359 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
360 unsigned offset
, unsigned length
),
361 int pos
, grub_size_t len
, char *buf
)
363 return grub_fshelp_read_file (node
->data
->disk
, node
, read_hook
,
364 pos
, len
, buf
, grub_hfsplus_read_block
,
366 node
->data
->log2blksize
- GRUB_DISK_SECTOR_BITS
);
369 static struct grub_hfsplus_data
*
370 grub_hfsplus_mount (grub_disk_t disk
)
372 struct grub_hfsplus_data
*data
;
373 struct grub_hfsplus_btheader header
;
374 struct grub_hfsplus_btnode node
;
376 struct grub_hfs_sblock hfs
;
377 struct grub_hfsplus_volheader hfsplus
;
380 data
= grub_malloc (sizeof (*data
));
386 /* Read the bootblock. */
387 grub_disk_read (disk
, GRUB_HFSPLUS_SBLOCK
, 0, sizeof (volheader
),
388 (char *) &volheader
);
392 data
->embedded_offset
= 0;
393 if (grub_be_to_cpu16 (volheader
.hfs
.magic
) == GRUB_HFS_MAGIC
)
399 /* See if there's an embedded HFS+ filesystem. */
400 if (grub_be_to_cpu16 (volheader
.hfs
.embed_sig
) != GRUB_HFSPLUS_MAGIC
)
402 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
406 /* Calculate the offset needed to translate HFS+ sector numbers. */
407 extent_start
= grub_be_to_cpu16 (volheader
.hfs
.embed_extent
.first_block
);
408 ablk_size
= grub_be_to_cpu32 (volheader
.hfs
.blksz
);
409 ablk_start
= grub_be_to_cpu16 (volheader
.hfs
.first_block
);
410 data
->embedded_offset
= (ablk_start
412 * (ablk_size
>> GRUB_DISK_SECTOR_BITS
));
414 grub_disk_read (disk
, data
->embedded_offset
+ GRUB_HFSPLUS_SBLOCK
, 0,
415 sizeof (volheader
), (char *) &volheader
);
420 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
422 if ((grub_be_to_cpu16 (volheader
.hfsplus
.magic
) != GRUB_HFSPLUS_MAGIC
)
423 && (grub_be_to_cpu16 (volheader
.hfsplus
.magic
) != GRUB_HFSPLUSX_MAGIC
))
425 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
429 grub_memcpy (&data
->volheader
, &volheader
.hfsplus
,
430 sizeof (volheader
.hfsplus
));
432 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data
->volheader
.blksize
),
436 /* Make a new node for the catalog tree. */
437 data
->catalog_tree
.file
.data
= data
;
438 data
->catalog_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_CATALOG
;
439 grub_memcpy (&data
->catalog_tree
.file
.extents
,
440 data
->volheader
.catalog_file
.extents
,
441 sizeof data
->volheader
.catalog_file
.extents
);
442 data
->catalog_tree
.file
.size
=
443 grub_be_to_cpu64 (data
->volheader
.catalog_file
.size
);
445 /* Make a new node for the extent overflow file. */
446 data
->extoverflow_tree
.file
.data
= data
;
447 data
->extoverflow_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_OVERFLOW
;
448 grub_memcpy (&data
->extoverflow_tree
.file
.extents
,
449 data
->volheader
.extents_file
.extents
,
450 sizeof data
->volheader
.catalog_file
.extents
);
452 data
->extoverflow_tree
.file
.size
=
453 grub_be_to_cpu64 (data
->volheader
.extents_file
.size
);
455 /* Read the essential information about the trees. */
456 if (! grub_hfsplus_read_file (&data
->catalog_tree
.file
, 0,
457 sizeof (struct grub_hfsplus_btnode
),
458 sizeof (header
), (char *) &header
))
461 data
->catalog_tree
.root
= grub_be_to_cpu32 (header
.root
);
462 data
->catalog_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
464 if (! grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0,
465 sizeof (struct grub_hfsplus_btnode
),
466 sizeof (header
), (char *) &header
))
469 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
471 if (! grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0, 0,
472 sizeof (node
), (char *) &node
))
475 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
476 data
->extoverflow_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
478 data
->dirroot
.data
= data
;
479 data
->dirroot
.fileid
= GRUB_HFSPLUS_FILEID_ROOTDIR
;
485 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
486 grub_error (GRUB_ERR_BAD_FS
, "not a hfsplus filesystem");
492 /* Compare the on disk catalog key KEYA with the catalog key we are
493 looking for (KEYB). */
495 grub_hfsplus_cmp_catkey (struct grub_hfsplus_key
*keya
,
496 struct grub_hfsplus_key_internal
*keyb
)
498 struct grub_hfsplus_catkey
*catkey_a
= &keya
->catkey
;
499 struct grub_hfsplus_catkey_internal
*catkey_b
= &keyb
->catkey
;
504 diff
= grub_be_to_cpu32 (catkey_a
->parent
) - catkey_b
->parent
;
508 /* Change the filename in keya so the endianness is correct. */
509 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
510 catkey_a
->name
[i
] = grub_be_to_cpu16 (catkey_a
->name
[i
]);
512 filename
= grub_malloc (grub_be_to_cpu16 (catkey_a
->namelen
) + 1);
514 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey_a
->name
,
515 grub_be_to_cpu16 (catkey_a
->namelen
)))
516 return -1; /* XXX: This error never occurs, but in case it happens
517 just skip this entry. */
519 diff
= grub_strncmp (filename
, catkey_b
->name
,
520 grub_be_to_cpu16 (catkey_a
->namelen
));
522 grub_free (filename
);
524 /* The endianness was changed to host format, change it back to
526 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
527 catkey_a
->name
[i
] = grub_cpu_to_be16 (catkey_a
->name
[i
]);
531 /* Compare the on disk extent overflow key KEYA with the extent
532 overflow key we are looking for (KEYB). */
534 grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
535 struct grub_hfsplus_key_internal
*keyb
)
537 struct grub_hfsplus_extkey
*extkey_a
= &keya
->extkey
;
538 struct grub_hfsplus_extkey_internal
*extkey_b
= &keyb
->extkey
;
541 diff
= grub_be_to_cpu32 (extkey_a
->fileid
) - extkey_b
->fileid
;
546 diff
= grub_be_to_cpu32 (extkey_a
->start
) - extkey_b
->start
;
551 grub_hfsplus_read_symlink (grub_fshelp_node_t node
)
554 grub_ssize_t numread
;
556 symlink
= grub_malloc (node
->size
+ 1);
560 numread
= grub_hfsplus_read_file (node
, 0, 0, node
->size
, symlink
);
561 if (numread
!= (grub_ssize_t
) node
->size
)
566 symlink
[node
->size
] = '\0';
572 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree
*btree
,
573 struct grub_hfsplus_btnode
*first_node
,
575 int (*hook
) (void *record
))
581 char *cnode
= (char *) first_node
;
583 /* Iterate over all records in this node. */
584 for (rec
= first_rec
; rec
< grub_be_to_cpu16 (first_node
->count
); rec
++)
586 if (hook (grub_hfsplus_btree_recptr (btree
, first_node
, rec
)))
590 if (! first_node
->next
)
593 if (! grub_hfsplus_read_file (&btree
->file
, 0,
594 (grub_be_to_cpu32 (first_node
->next
)
596 btree
->nodesize
, cnode
))
599 /* Don't skip any record in the next iteration. */
606 /* Lookup the node described by KEY in the B+ Tree BTREE. Compare
607 keys using the function COMPARE_KEYS. When a match is found,
608 return the node in MATCHNODE and a pointer to the data in this node
609 in KEYOFFSET. MATCHNODE should be freed by the caller. */
611 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
612 struct grub_hfsplus_key_internal
*key
,
613 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
614 struct grub_hfsplus_key_internal
*keyb
),
615 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
)
617 grub_uint64_t currnode
;
619 struct grub_hfsplus_btnode
*nodedesc
;
622 node
= grub_malloc (btree
->nodesize
);
626 currnode
= btree
->root
;
632 if (! grub_hfsplus_read_file (&btree
->file
, 0,
633 (long)currnode
* (long)btree
->nodesize
,
634 btree
->nodesize
, (char *) node
))
640 nodedesc
= (struct grub_hfsplus_btnode
*) node
;
642 /* Find the record in this tree. */
643 for (rec
= 0; rec
< grub_be_to_cpu16 (nodedesc
->count
); rec
++)
645 struct grub_hfsplus_key
*currkey
;
646 currkey
= grub_hfsplus_btree_recptr (btree
, nodedesc
, rec
);
648 /* The action that has to be taken depend on the type of
650 if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_LEAF
651 && compare_keys (currkey
, key
) == 0)
653 /* An exact match was found! */
655 *matchnode
= nodedesc
;
660 else if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_INDEX
)
662 grub_uint32_t
*pointer
;
664 /* The place where the key could have been found didn't
665 contain the key. This means that the previous match
666 is the one that should be followed. */
667 if (compare_keys (currkey
, key
) > 0)
670 /* Mark the last key which is lower or equal to the key
671 that we are looking for. The last match that is
672 found will be used to locate the child which can
673 contain the record. */
674 pointer
= (grub_uint32_t
*) ((char *) currkey
675 + grub_be_to_cpu16 (currkey
->keylen
)
677 currnode
= grub_be_to_cpu32 (*pointer
);
682 /* No match is found, no record with this key exists in the
694 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir
,
696 (*hook
) (const char *filename
,
697 enum grub_fshelp_filetype filetype
,
698 grub_fshelp_node_t node
))
702 auto int list_nodes (void *record
);
703 int list_nodes (void *record
)
705 struct grub_hfsplus_catkey
*catkey
;
708 struct grub_fshelp_node
*node
;
709 struct grub_hfsplus_catfile
*fileinfo
;
710 enum grub_fshelp_filetype type
= GRUB_FSHELP_UNKNOWN
;
712 catkey
= (struct grub_hfsplus_catkey
*) record
;
715 (struct grub_hfsplus_catfile
*) ((char *) record
716 + grub_be_to_cpu16 (catkey
->keylen
)
717 + 2 + (grub_be_to_cpu16(catkey
->keylen
)
720 /* Stop iterating when the last directory entry is found. */
721 if (grub_be_to_cpu32 (catkey
->parent
) != dir
->fileid
)
724 /* Determine the type of the node that is found. */
725 if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_REG
)
727 int mode
= (grub_be_to_cpu16 (fileinfo
->mode
)
728 & GRUB_HFSPLUS_FILEMODE_MASK
);
730 if (mode
== GRUB_HFSPLUS_FILEMODE_REG
)
731 type
= GRUB_FSHELP_REG
;
732 else if (mode
== GRUB_HFSPLUS_FILEMODE_SYMLINK
)
733 type
= GRUB_FSHELP_SYMLINK
;
735 type
= GRUB_FSHELP_UNKNOWN
;
737 else if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_DIR
)
738 type
= GRUB_FSHELP_DIR
;
740 if (type
== GRUB_FSHELP_UNKNOWN
)
743 /* Make sure the byte order of the UTF16 string is correct. */
744 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
746 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
748 /* If the name is obviously invalid, skip this node. */
749 if (catkey
->name
[i
] == 0)
753 filename
= grub_malloc (grub_be_to_cpu16 (catkey
->namelen
) + 1);
757 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey
->name
,
758 grub_be_to_cpu16 (catkey
->namelen
)))
760 grub_free (filename
);
764 filename
[grub_be_to_cpu16 (catkey
->namelen
)] = '\0';
766 /* Restore the byte order to what it was previously. */
767 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
768 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
770 /* Only accept valid nodes. */
771 if (grub_strlen (filename
) == grub_be_to_cpu16 (catkey
->namelen
))
773 /* A valid node is found; setup the node and call the
774 callback function. */
775 node
= grub_malloc (sizeof (*node
));
776 node
->data
= dir
->data
;
778 grub_memcpy (node
->extents
, fileinfo
->data
.extents
,
779 sizeof (node
->extents
));
780 node
->size
= grub_be_to_cpu64 (fileinfo
->data
.size
);
781 node
->fileid
= grub_be_to_cpu32 (fileinfo
->fileid
);
783 ret
= hook (filename
, type
, node
);
786 grub_free (filename
);
791 struct grub_hfsplus_key_internal intern
;
792 struct grub_hfsplus_btnode
*node
;
795 /* Create a key that points to the first entry in the directory. */
796 intern
.catkey
.parent
= dir
->fileid
;
797 intern
.catkey
.name
= "";
799 /* First lookup the first entry. */
800 if (grub_hfsplus_btree_search (&dir
->data
->catalog_tree
, &intern
,
801 grub_hfsplus_cmp_catkey
, &node
, &ptr
))
804 /* Iterate over all entries in this directory. */
805 grub_hfsplus_btree_iterate_node (&dir
->data
->catalog_tree
, node
, ptr
,
813 /* Open a file named NAME and initialize FILE. */
815 grub_hfsplus_open (struct grub_file
*file
, const char *name
)
817 struct grub_hfsplus_data
*data
;
818 struct grub_fshelp_node
*fdiro
= 0;
821 grub_dl_ref (my_mod
);
824 data
= grub_hfsplus_mount (file
->device
->disk
);
828 grub_fshelp_find_file (name
, &data
->dirroot
, &fdiro
,
829 grub_hfsplus_iterate_dir
,
830 grub_hfsplus_read_symlink
, GRUB_FSHELP_REG
);
834 file
->size
= fdiro
->size
;
835 data
->opened_file
= *fdiro
;
844 if (data
&& fdiro
!= &data
->dirroot
)
849 grub_dl_unref (my_mod
);
857 grub_hfsplus_close (grub_file_t file
)
859 grub_free (file
->data
);
862 grub_dl_unref (my_mod
);
865 return GRUB_ERR_NONE
;
869 /* Read LEN bytes data from FILE into BUF. */
871 grub_hfsplus_read (grub_file_t file
, char *buf
, grub_size_t len
)
873 struct grub_hfsplus_data
*data
=
874 (struct grub_hfsplus_data
*) file
->data
;
876 int size
= grub_hfsplus_read_file (&data
->opened_file
, file
->read_hook
,
877 file
->offset
, len
, buf
);
884 grub_hfsplus_dir (grub_device_t device
, const char *path
,
885 int (*hook
) (const char *filename
, int dir
))
887 struct grub_hfsplus_data
*data
= 0;
888 struct grub_fshelp_node
*fdiro
= 0;
890 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
891 enum grub_fshelp_filetype filetype
,
892 grub_fshelp_node_t node
);
894 int NESTED_FUNC_ATTR
iterate (const char *filename
,
895 enum grub_fshelp_filetype filetype
,
896 grub_fshelp_node_t node
)
900 if (filetype
== GRUB_FSHELP_DIR
)
901 return hook (filename
, 1);
903 return hook (filename
, 0);
909 grub_dl_ref (my_mod
);
912 data
= grub_hfsplus_mount (device
->disk
);
916 /* Find the directory that should be opened. */
917 grub_fshelp_find_file (path
, &data
->dirroot
, &fdiro
,
918 grub_hfsplus_iterate_dir
,
919 grub_hfsplus_read_symlink
, GRUB_FSHELP_DIR
);
923 /* Iterate over all entries in this directory. */
924 grub_hfsplus_iterate_dir (fdiro
, iterate
);
927 if (data
&& fdiro
!= &data
->dirroot
)
932 grub_dl_unref (my_mod
);
940 grub_hfsplus_label (grub_device_t device
__attribute__((unused
))
941 , char **label
__attribute__((unused
)))
943 /* XXX: It's not documented how to read a label. */
944 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
945 "reading the label of a HFS+ "
946 "partition is not implemented");
950 static struct grub_fs grub_hfsplus_fs
=
953 .dir
= grub_hfsplus_dir
,
954 .open
= grub_hfsplus_open
,
955 .read
= grub_hfsplus_read
,
956 .close
= grub_hfsplus_close
,
957 .label
= grub_hfsplus_label
,
961 GRUB_MOD_INIT(hfsplus
)
963 grub_fs_register (&grub_hfsplus_fs
);
969 GRUB_MOD_FINI(hfsplus
)
971 grub_fs_unregister (&grub_hfsplus_fs
);