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 grub_uint32_t total_nodes
;
103 grub_uint32_t free_nodes
;
104 grub_uint16_t reserved1
;
105 grub_uint32_t clump_size
; /* ignored */
106 grub_uint8_t btree_type
;
107 grub_uint8_t key_compare
;
108 grub_uint32_t attributes
;
109 } __attribute__ ((packed
));
111 /* The on disk layout of a catalog key. */
112 struct grub_hfsplus_catkey
114 grub_uint16_t keylen
;
115 grub_uint32_t parent
;
116 grub_uint16_t namelen
;
117 grub_uint16_t name
[30];
118 } __attribute__ ((packed
));
120 /* The on disk layout of an extent overflow file key. */
121 struct grub_hfsplus_extkey
123 grub_uint16_t keylen
;
126 grub_uint32_t fileid
;
128 } __attribute__ ((packed
));
130 struct grub_hfsplus_key
134 struct grub_hfsplus_extkey extkey
;
135 struct grub_hfsplus_catkey catkey
;
136 grub_uint16_t keylen
;
138 } __attribute__ ((packed
));
140 struct grub_hfsplus_catfile
144 grub_uint32_t reserved
;
145 grub_uint32_t fileid
;
146 grub_uint8_t unused1
[4];
148 grub_uint8_t unused2
[22];
150 grub_uint8_t unused3
[44];
151 struct grub_hfsplus_forkdata data
;
152 struct grub_hfsplus_forkdata resource
;
153 } __attribute__ ((packed
));
155 /* Filetype information as used in inodes. */
156 #define GRUB_HFSPLUS_FILEMODE_MASK 0170000
157 #define GRUB_HFSPLUS_FILEMODE_REG 0100000
158 #define GRUB_HFSPLUS_FILEMODE_DIRECTORY 0040000
159 #define GRUB_HFSPLUS_FILEMODE_SYMLINK 0120000
161 /* Some pre-defined file IDs. */
162 #define GRUB_HFSPLUS_FILEID_ROOTDIR 2
163 #define GRUB_HFSPLUS_FILEID_OVERFLOW 3
164 #define GRUB_HFSPLUS_FILEID_CATALOG 4
166 enum grub_hfsplus_filetype
168 GRUB_HFSPLUS_FILETYPE_DIR
= 1,
169 GRUB_HFSPLUS_FILETYPE_REG
= 2,
170 GRUB_HFSPLUS_FILETYPE_DIR_THREAD
= 3,
171 GRUB_HFSPLUS_FILETYPE_REG_THREAD
= 4
174 #define GRUB_HFSPLUSX_BINARYCOMPARE 0xBC
175 #define GRUB_HFSPLUSX_CASEFOLDING 0xCF
177 /* Internal representation of a catalog key. */
178 struct grub_hfsplus_catkey_internal
184 /* Internal representation of an extent overflow key. */
185 struct grub_hfsplus_extkey_internal
187 grub_uint32_t fileid
;
191 struct grub_hfsplus_key_internal
195 struct grub_hfsplus_extkey_internal extkey
;
196 struct grub_hfsplus_catkey_internal catkey
;
202 struct grub_fshelp_node
204 struct grub_hfsplus_data
*data
;
205 struct grub_hfsplus_extent extents
[8];
207 grub_uint32_t fileid
;
211 struct grub_hfsplus_btree
216 /* Catalog file node. */
217 struct grub_fshelp_node file
;
220 /* Information about a "mounted" HFS+ filesystem. */
221 struct grub_hfsplus_data
223 struct grub_hfsplus_volheader volheader
;
226 unsigned int log2blksize
;
228 struct grub_hfsplus_btree catalog_tree
;
229 struct grub_hfsplus_btree extoverflow_tree
;
231 struct grub_fshelp_node dirroot
;
232 struct grub_fshelp_node opened_file
;
234 /* This is the offset into the physical disk for an embedded HFS+
235 filesystem (one inside a plain HFS wrapper). */
240 static grub_dl_t my_mod
;
243 /* Return the offset of the record with the index INDEX, in the node
244 NODE which is part of the B+ tree BTREE. */
245 static inline unsigned int
246 grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree
*btree
,
247 struct grub_hfsplus_btnode
*node
, int index
)
249 char *cnode
= (char *) node
;
250 grub_uint16_t
*recptr
;
251 recptr
= (grub_uint16_t
*) (&cnode
[btree
->nodesize
252 - index
* sizeof (grub_uint16_t
) - 2]);
253 return grub_be_to_cpu16 (*recptr
);
256 /* Return a pointer to the record with the index INDEX, in the node
257 NODE which is part of the B+ tree BTREE. */
258 static inline struct grub_hfsplus_key
*
259 grub_hfsplus_btree_recptr (struct grub_hfsplus_btree
*btree
,
260 struct grub_hfsplus_btnode
*node
, int index
)
262 char *cnode
= (char *) node
;
264 offset
= grub_hfsplus_btree_recoffset (btree
, node
, index
);
265 return (struct grub_hfsplus_key
*) &cnode
[offset
];
269 /* Find the extent that points to FILEBLOCK. If it is not in one of
270 the 8 extents described by EXTENT, return -1. In that case set
271 FILEBLOCK to the next block. */
273 grub_hfsplus_find_block (struct grub_hfsplus_extent
*extent
,
277 grub_size_t blksleft
= *fileblock
;
279 /* First lookup the file in the given extents. */
280 for (i
= 0; i
< 8; i
++)
282 if (blksleft
< grub_be_to_cpu32 (extent
[i
].count
))
283 return grub_be_to_cpu32 (extent
[i
].start
) + blksleft
;
284 blksleft
-= grub_be_to_cpu32 (extent
[i
].count
);
287 *fileblock
= blksleft
;
292 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
293 struct grub_hfsplus_key_internal
*key
,
294 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
295 struct grub_hfsplus_key_internal
*keyb
),
296 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
);
298 static int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
299 struct grub_hfsplus_key_internal
*keyb
);
301 /* Search for the block FILEBLOCK inside the file NODE. Return the
302 blocknumber of this block on disk. */
303 static grub_disk_addr_t
304 grub_hfsplus_read_block (grub_fshelp_node_t node
, grub_disk_addr_t fileblock
)
306 struct grub_hfsplus_btnode
*nnode
= 0;
307 int blksleft
= fileblock
;
308 struct grub_hfsplus_extent
*extents
= &node
->extents
[0];
312 struct grub_hfsplus_extkey
*key
;
313 struct grub_hfsplus_extkey_internal extoverflow
;
317 /* Try to find this block in the current set of extents. */
318 blk
= grub_hfsplus_find_block (extents
, &blksleft
);
320 /* The previous iteration of this loop allocated memory. The
321 code above used this memory, it can be freed now. */
327 + (node
->data
->embedded_offset
>> (node
->data
->log2blksize
328 - GRUB_DISK_SECTOR_BITS
)));
330 /* For the extent overflow file, extra extents can't be found in
331 the extent overflow file. If this happens, you found a
333 if (node
->fileid
== GRUB_HFSPLUS_FILEID_OVERFLOW
)
335 grub_error (GRUB_ERR_READ_ERROR
,
336 "extra extents found in an extend overflow file");
340 /* Set up the key to look for in the extent overflow file. */
341 extoverflow
.fileid
= node
->fileid
;
342 extoverflow
.start
= fileblock
- blksleft
;
344 if (grub_hfsplus_btree_search (&node
->data
->extoverflow_tree
,
345 (struct grub_hfsplus_key_internal
*) &extoverflow
,
346 grub_hfsplus_cmp_extkey
, &nnode
, &ptr
))
348 grub_error (GRUB_ERR_READ_ERROR
,
349 "no block found for the file id 0x%x and the block offset 0x%x",
350 node
->fileid
, fileblock
);
354 /* The extent overflow file has 8 extents right after the key. */
355 key
= (struct grub_hfsplus_extkey
*)
356 grub_hfsplus_btree_recptr (&node
->data
->extoverflow_tree
, nnode
, ptr
);
357 extents
= (struct grub_hfsplus_extent
*) (key
+ 1);
359 /* The block wasn't found. Perhaps the next iteration will find
360 it. The last block we found is stored in BLKSLEFT now. */
365 /* Too bad, you lose. */
370 /* Read LEN bytes from the file described by DATA starting with byte
371 POS. Return the amount of read bytes in READ. */
373 grub_hfsplus_read_file (grub_fshelp_node_t node
,
374 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
375 unsigned offset
, unsigned length
),
376 int pos
, grub_size_t len
, char *buf
)
378 return grub_fshelp_read_file (node
->data
->disk
, node
, read_hook
,
379 pos
, len
, buf
, grub_hfsplus_read_block
,
381 node
->data
->log2blksize
- GRUB_DISK_SECTOR_BITS
);
384 static struct grub_hfsplus_data
*
385 grub_hfsplus_mount (grub_disk_t disk
)
387 struct grub_hfsplus_data
*data
;
388 struct grub_hfsplus_btheader header
;
389 struct grub_hfsplus_btnode node
;
392 struct grub_hfs_sblock hfs
;
393 struct grub_hfsplus_volheader hfsplus
;
396 data
= grub_malloc (sizeof (*data
));
402 /* Read the bootblock. */
403 grub_disk_read (disk
, GRUB_HFSPLUS_SBLOCK
, 0, sizeof (volheader
),
408 data
->embedded_offset
= 0;
409 if (grub_be_to_cpu16 (volheader
.hfs
.magic
) == GRUB_HFS_MAGIC
)
415 /* See if there's an embedded HFS+ filesystem. */
416 if (grub_be_to_cpu16 (volheader
.hfs
.embed_sig
) != GRUB_HFSPLUS_MAGIC
)
418 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
422 /* Calculate the offset needed to translate HFS+ sector numbers. */
423 extent_start
= grub_be_to_cpu16 (volheader
.hfs
.embed_extent
.first_block
);
424 ablk_size
= grub_be_to_cpu32 (volheader
.hfs
.blksz
);
425 ablk_start
= grub_be_to_cpu16 (volheader
.hfs
.first_block
);
426 data
->embedded_offset
= (ablk_start
428 * (ablk_size
>> GRUB_DISK_SECTOR_BITS
));
430 grub_disk_read (disk
, data
->embedded_offset
+ GRUB_HFSPLUS_SBLOCK
, 0,
431 sizeof (volheader
), &volheader
);
436 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
438 magic
= grub_be_to_cpu16 (volheader
.hfsplus
.magic
);
439 if ((magic
!= GRUB_HFSPLUS_MAGIC
) && (magic
!= GRUB_HFSPLUSX_MAGIC
))
441 grub_error (GRUB_ERR_BAD_FS
, "not a HFS+ filesystem");
445 grub_memcpy (&data
->volheader
, &volheader
.hfsplus
,
446 sizeof (volheader
.hfsplus
));
448 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data
->volheader
.blksize
),
452 /* Make a new node for the catalog tree. */
453 data
->catalog_tree
.file
.data
= data
;
454 data
->catalog_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_CATALOG
;
455 grub_memcpy (&data
->catalog_tree
.file
.extents
,
456 data
->volheader
.catalog_file
.extents
,
457 sizeof data
->volheader
.catalog_file
.extents
);
458 data
->catalog_tree
.file
.size
=
459 grub_be_to_cpu64 (data
->volheader
.catalog_file
.size
);
461 /* Make a new node for the extent overflow file. */
462 data
->extoverflow_tree
.file
.data
= data
;
463 data
->extoverflow_tree
.file
.fileid
= GRUB_HFSPLUS_FILEID_OVERFLOW
;
464 grub_memcpy (&data
->extoverflow_tree
.file
.extents
,
465 data
->volheader
.extents_file
.extents
,
466 sizeof data
->volheader
.catalog_file
.extents
);
468 data
->extoverflow_tree
.file
.size
=
469 grub_be_to_cpu64 (data
->volheader
.extents_file
.size
);
471 /* Read the essential information about the trees. */
472 if (grub_hfsplus_read_file (&data
->catalog_tree
.file
, 0,
473 sizeof (struct grub_hfsplus_btnode
),
474 sizeof (header
), (char *) &header
) <= 0)
477 data
->catalog_tree
.root
= grub_be_to_cpu32 (header
.root
);
478 data
->catalog_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
479 data
->case_sensitive
= ((magic
== GRUB_HFSPLUSX_MAGIC
) &&
480 (header
.key_compare
== GRUB_HFSPLUSX_BINARYCOMPARE
));
482 if (grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0,
483 sizeof (struct grub_hfsplus_btnode
),
484 sizeof (header
), (char *) &header
) <= 0)
487 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
489 if (grub_hfsplus_read_file (&data
->extoverflow_tree
.file
, 0, 0,
490 sizeof (node
), (char *) &node
) <= 0)
493 data
->extoverflow_tree
.root
= grub_be_to_cpu32 (header
.root
);
494 data
->extoverflow_tree
.nodesize
= grub_be_to_cpu16 (header
.nodesize
);
496 data
->dirroot
.data
= data
;
497 data
->dirroot
.fileid
= GRUB_HFSPLUS_FILEID_ROOTDIR
;
503 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
504 grub_error (GRUB_ERR_BAD_FS
, "not a hfsplus filesystem");
510 /* Compare the on disk catalog key KEYA with the catalog key we are
511 looking for (KEYB). */
513 grub_hfsplus_cmp_catkey (struct grub_hfsplus_key
*keya
,
514 struct grub_hfsplus_key_internal
*keyb
)
516 struct grub_hfsplus_catkey
*catkey_a
= &keya
->catkey
;
517 struct grub_hfsplus_catkey_internal
*catkey_b
= &keyb
->catkey
;
522 diff
= grub_be_to_cpu32 (catkey_a
->parent
) - catkey_b
->parent
;
526 /* Change the filename in keya so the endianness is correct. */
527 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
528 catkey_a
->name
[i
] = grub_be_to_cpu16 (catkey_a
->name
[i
]);
530 filename
= grub_malloc (grub_be_to_cpu16 (catkey_a
->namelen
) + 1);
532 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey_a
->name
,
533 grub_be_to_cpu16 (catkey_a
->namelen
)))
534 return -1; /* XXX: This error never occurs, but in case it happens
535 just skip this entry. */
537 diff
= grub_strncmp (filename
, catkey_b
->name
,
538 grub_be_to_cpu16 (catkey_a
->namelen
));
540 grub_free (filename
);
542 /* The endianness was changed to host format, change it back to
544 for (i
= 0; i
< grub_be_to_cpu16 (catkey_a
->namelen
); i
++)
545 catkey_a
->name
[i
] = grub_cpu_to_be16 (catkey_a
->name
[i
]);
549 /* Compare the on disk extent overflow key KEYA with the extent
550 overflow key we are looking for (KEYB). */
552 grub_hfsplus_cmp_extkey (struct grub_hfsplus_key
*keya
,
553 struct grub_hfsplus_key_internal
*keyb
)
555 struct grub_hfsplus_extkey
*extkey_a
= &keya
->extkey
;
556 struct grub_hfsplus_extkey_internal
*extkey_b
= &keyb
->extkey
;
559 diff
= grub_be_to_cpu32 (extkey_a
->fileid
) - extkey_b
->fileid
;
564 diff
= grub_be_to_cpu32 (extkey_a
->start
) - extkey_b
->start
;
569 grub_hfsplus_read_symlink (grub_fshelp_node_t node
)
572 grub_ssize_t numread
;
574 symlink
= grub_malloc (node
->size
+ 1);
578 numread
= grub_hfsplus_read_file (node
, 0, 0, node
->size
, symlink
);
579 if (numread
!= (grub_ssize_t
) node
->size
)
584 symlink
[node
->size
] = '\0';
590 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree
*btree
,
591 struct grub_hfsplus_btnode
*first_node
,
593 int (*hook
) (void *record
))
599 char *cnode
= (char *) first_node
;
601 /* Iterate over all records in this node. */
602 for (rec
= first_rec
; rec
< grub_be_to_cpu16 (first_node
->count
); rec
++)
604 if (hook (grub_hfsplus_btree_recptr (btree
, first_node
, rec
)))
608 if (! first_node
->next
)
611 if (grub_hfsplus_read_file (&btree
->file
, 0,
612 (grub_be_to_cpu32 (first_node
->next
)
614 btree
->nodesize
, cnode
) <= 0)
617 /* Don't skip any record in the next iteration. */
624 /* Lookup the node described by KEY in the B+ Tree BTREE. Compare
625 keys using the function COMPARE_KEYS. When a match is found,
626 return the node in MATCHNODE and a pointer to the data in this node
627 in KEYOFFSET. MATCHNODE should be freed by the caller. */
629 grub_hfsplus_btree_search (struct grub_hfsplus_btree
*btree
,
630 struct grub_hfsplus_key_internal
*key
,
631 int (*compare_keys
) (struct grub_hfsplus_key
*keya
,
632 struct grub_hfsplus_key_internal
*keyb
),
633 struct grub_hfsplus_btnode
**matchnode
, int *keyoffset
)
635 grub_uint64_t currnode
;
637 struct grub_hfsplus_btnode
*nodedesc
;
640 node
= grub_malloc (btree
->nodesize
);
644 currnode
= btree
->root
;
650 if (grub_hfsplus_read_file (&btree
->file
, 0,
651 (long)currnode
* (long)btree
->nodesize
,
652 btree
->nodesize
, (char *) node
) <= 0)
655 return grub_error (GRUB_ERR_BAD_FS
, "Couldn't read i-node.");
658 nodedesc
= (struct grub_hfsplus_btnode
*) node
;
660 /* Find the record in this tree. */
661 for (rec
= 0; rec
< grub_be_to_cpu16 (nodedesc
->count
); rec
++)
663 struct grub_hfsplus_key
*currkey
;
664 currkey
= grub_hfsplus_btree_recptr (btree
, nodedesc
, rec
);
666 /* The action that has to be taken depend on the type of
668 if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_LEAF
669 && compare_keys (currkey
, key
) == 0)
671 /* An exact match was found! */
673 *matchnode
= nodedesc
;
678 else if (nodedesc
->type
== GRUB_HFSPLUS_BTNODE_TYPE_INDEX
)
680 grub_uint32_t
*pointer
;
682 /* The place where the key could have been found didn't
683 contain the key. This means that the previous match
684 is the one that should be followed. */
685 if (compare_keys (currkey
, key
) > 0)
688 /* Mark the last key which is lower or equal to the key
689 that we are looking for. The last match that is
690 found will be used to locate the child which can
691 contain the record. */
692 pointer
= (grub_uint32_t
*) ((char *) currkey
693 + grub_be_to_cpu16 (currkey
->keylen
)
695 currnode
= grub_be_to_cpu32 (*pointer
);
700 /* No match is found, no record with this key exists in the
712 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir
,
714 (*hook
) (const char *filename
,
715 enum grub_fshelp_filetype filetype
,
716 grub_fshelp_node_t node
))
720 auto int list_nodes (void *record
);
721 int list_nodes (void *record
)
723 struct grub_hfsplus_catkey
*catkey
;
726 struct grub_fshelp_node
*node
;
727 struct grub_hfsplus_catfile
*fileinfo
;
728 enum grub_fshelp_filetype type
= GRUB_FSHELP_UNKNOWN
;
730 catkey
= (struct grub_hfsplus_catkey
*) record
;
733 (struct grub_hfsplus_catfile
*) ((char *) record
734 + grub_be_to_cpu16 (catkey
->keylen
)
735 + 2 + (grub_be_to_cpu16(catkey
->keylen
)
738 /* Stop iterating when the last directory entry is found. */
739 if (grub_be_to_cpu32 (catkey
->parent
) != dir
->fileid
)
742 /* Determine the type of the node that is found. */
743 if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_REG
)
745 int mode
= (grub_be_to_cpu16 (fileinfo
->mode
)
746 & GRUB_HFSPLUS_FILEMODE_MASK
);
748 if (mode
== GRUB_HFSPLUS_FILEMODE_REG
)
749 type
= GRUB_FSHELP_REG
;
750 else if (mode
== GRUB_HFSPLUS_FILEMODE_SYMLINK
)
751 type
= GRUB_FSHELP_SYMLINK
;
753 type
= GRUB_FSHELP_UNKNOWN
;
755 else if (grub_be_to_cpu16 (fileinfo
->type
) == GRUB_HFSPLUS_FILETYPE_DIR
)
756 type
= GRUB_FSHELP_DIR
;
758 if (type
== GRUB_FSHELP_UNKNOWN
)
761 /* Make sure the byte order of the UTF16 string is correct. */
762 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
764 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
766 /* If the name is obviously invalid, skip this node. */
767 if (catkey
->name
[i
] == 0)
771 filename
= grub_malloc (grub_be_to_cpu16 (catkey
->namelen
) + 1);
775 if (! grub_utf16_to_utf8 ((grub_uint8_t
*) filename
, catkey
->name
,
776 grub_be_to_cpu16 (catkey
->namelen
)))
778 grub_free (filename
);
782 filename
[grub_be_to_cpu16 (catkey
->namelen
)] = '\0';
784 /* Restore the byte order to what it was previously. */
785 for (i
= 0; i
< grub_be_to_cpu16 (catkey
->namelen
); i
++)
786 catkey
->name
[i
] = grub_be_to_cpu16 (catkey
->name
[i
]);
788 /* hfs+ is case insensitive. */
789 if (! dir
->data
->case_sensitive
)
790 type
|= GRUB_FSHELP_CASE_INSENSITIVE
;
792 /* Only accept valid nodes. */
793 if (grub_strlen (filename
) == grub_be_to_cpu16 (catkey
->namelen
))
795 /* A valid node is found; setup the node and call the
796 callback function. */
797 node
= grub_malloc (sizeof (*node
));
798 node
->data
= dir
->data
;
800 grub_memcpy (node
->extents
, fileinfo
->data
.extents
,
801 sizeof (node
->extents
));
802 node
->mtime
= grub_be_to_cpu32 (fileinfo
->mtime
) - 2082844800;
803 node
->size
= grub_be_to_cpu64 (fileinfo
->data
.size
);
804 node
->fileid
= grub_be_to_cpu32 (fileinfo
->fileid
);
806 ret
= hook (filename
, type
, node
);
809 grub_free (filename
);
814 struct grub_hfsplus_key_internal intern
;
815 struct grub_hfsplus_btnode
*node
;
818 /* Create a key that points to the first entry in the directory. */
819 intern
.catkey
.parent
= dir
->fileid
;
820 intern
.catkey
.name
= "";
822 /* First lookup the first entry. */
823 if (grub_hfsplus_btree_search (&dir
->data
->catalog_tree
, &intern
,
824 grub_hfsplus_cmp_catkey
, &node
, &ptr
))
827 /* Iterate over all entries in this directory. */
828 grub_hfsplus_btree_iterate_node (&dir
->data
->catalog_tree
, node
, ptr
,
836 /* Open a file named NAME and initialize FILE. */
838 grub_hfsplus_open (struct grub_file
*file
, const char *name
)
840 struct grub_hfsplus_data
*data
;
841 struct grub_fshelp_node
*fdiro
= 0;
843 grub_dl_ref (my_mod
);
845 data
= grub_hfsplus_mount (file
->device
->disk
);
849 grub_fshelp_find_file (name
, &data
->dirroot
, &fdiro
,
850 grub_hfsplus_iterate_dir
,
851 grub_hfsplus_read_symlink
, GRUB_FSHELP_REG
);
855 file
->size
= fdiro
->size
;
856 data
->opened_file
= *fdiro
;
865 if (data
&& fdiro
!= &data
->dirroot
)
869 grub_dl_unref (my_mod
);
876 grub_hfsplus_close (grub_file_t file
)
878 grub_free (file
->data
);
880 grub_dl_unref (my_mod
);
882 return GRUB_ERR_NONE
;
886 /* Read LEN bytes data from FILE into BUF. */
888 grub_hfsplus_read (grub_file_t file
, char *buf
, grub_size_t len
)
890 struct grub_hfsplus_data
*data
=
891 (struct grub_hfsplus_data
*) file
->data
;
893 int size
= grub_hfsplus_read_file (&data
->opened_file
, file
->read_hook
,
894 file
->offset
, len
, buf
);
901 grub_hfsplus_dir (grub_device_t device
, const char *path
,
902 int (*hook
) (const char *filename
,
903 const struct grub_dirhook_info
*info
))
905 struct grub_hfsplus_data
*data
= 0;
906 struct grub_fshelp_node
*fdiro
= 0;
908 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
909 enum grub_fshelp_filetype filetype
,
910 grub_fshelp_node_t node
);
912 int NESTED_FUNC_ATTR
iterate (const char *filename
,
913 enum grub_fshelp_filetype filetype
,
914 grub_fshelp_node_t node
)
916 struct grub_dirhook_info info
;
917 grub_memset (&info
, 0, sizeof (info
));
918 info
.dir
= ((filetype
& GRUB_FSHELP_TYPE_MASK
) == GRUB_FSHELP_DIR
);
920 info
.mtime
= node
->mtime
;
921 info
.case_insensitive
= !! (filetype
& GRUB_FSHELP_CASE_INSENSITIVE
);
923 return hook (filename
, &info
);
926 grub_dl_ref (my_mod
);
928 data
= grub_hfsplus_mount (device
->disk
);
932 /* Find the directory that should be opened. */
933 grub_fshelp_find_file (path
, &data
->dirroot
, &fdiro
,
934 grub_hfsplus_iterate_dir
,
935 grub_hfsplus_read_symlink
, GRUB_FSHELP_DIR
);
939 /* Iterate over all entries in this directory. */
940 grub_hfsplus_iterate_dir (fdiro
, iterate
);
943 if (data
&& fdiro
!= &data
->dirroot
)
947 grub_dl_unref (my_mod
);
954 grub_hfsplus_label (grub_device_t device
__attribute__((unused
))
955 , char **label
__attribute__((unused
)))
957 /* XXX: It's not documented how to read a label. */
958 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
959 "reading the label of a HFS+ "
960 "partition is not implemented");
965 grub_hfsplus_mtime (grub_device_t device
, grub_int32_t
*tm
)
967 struct grub_hfsplus_data
*data
;
968 grub_disk_t disk
= device
->disk
;
970 grub_dl_ref (my_mod
);
972 data
= grub_hfsplus_mount (disk
);
976 *tm
= grub_be_to_cpu32 (data
->volheader
.utime
) - 2082844800;
978 grub_dl_unref (my_mod
);
987 grub_hfsplus_uuid (grub_device_t device
, char **uuid
)
989 struct grub_hfsplus_data
*data
;
990 grub_disk_t disk
= device
->disk
;
992 grub_dl_ref (my_mod
);
994 data
= grub_hfsplus_mount (disk
);
997 *uuid
= grub_malloc (16 + sizeof ('\0'));
998 grub_sprintf (*uuid
, "%016llx",
1000 grub_be_to_cpu64 (data
->volheader
.num_serial
));
1005 grub_dl_unref (my_mod
);
1014 static struct grub_fs grub_hfsplus_fs
=
1017 .dir
= grub_hfsplus_dir
,
1018 .open
= grub_hfsplus_open
,
1019 .read
= grub_hfsplus_read
,
1020 .close
= grub_hfsplus_close
,
1021 .label
= grub_hfsplus_label
,
1022 .mtime
= grub_hfsplus_mtime
,
1023 .uuid
= grub_hfsplus_uuid
,
1025 .reserved_first_sector
= 1,
1030 GRUB_MOD_INIT(hfsplus
)
1032 grub_fs_register (&grub_hfsplus_fs
);
1036 GRUB_MOD_FINI(hfsplus
)
1038 grub_fs_unregister (&grub_hfsplus_fs
);