2009-05-04 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / fs / hfsplus.c
blob0a7bedde75a7e1934d848119c9b844c7955e8246
1 /* hfsplus.c - HFS+ Filesystem. */
2 /*
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 */
22 #include <grub/err.h>
23 #include <grub/file.h>
24 #include <grub/mm.h>
25 #include <grub/misc.h>
26 #include <grub/disk.h>
27 #include <grub/dl.h>
28 #include <grub/types.h>
29 #include <grub/fshelp.h>
30 #include <grub/hfs.h>
32 #define GRUB_HFSPLUS_MAGIC 0x482B
33 #define GRUB_HFSPLUSX_MAGIC 0x4858
34 #define GRUB_HFSPLUS_SBLOCK 2
36 /* A HFS+ extent. */
37 struct grub_hfsplus_extent
39 /* The first block of a file on disk. */
40 grub_uint32_t start;
41 /* The amount of blocks described by this extent. */
42 grub_uint32_t count;
43 } __attribute__ ((packed));
45 /* The descriptor of a fork. */
46 struct grub_hfsplus_forkdata
48 grub_uint64_t size;
49 grub_uint32_t clumpsize;
50 grub_uint32_t blocks;
51 struct grub_hfsplus_extent extents[8];
52 } __attribute__ ((packed));
54 /* The HFS+ Volume Header. */
55 struct grub_hfsplus_volheader
57 grub_uint16_t magic;
58 grub_uint16_t version;
59 grub_uint32_t attributes;
60 grub_uint8_t unused1[12];
61 grub_uint32_t utime;
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
84 grub_uint32_t next;
85 grub_uint32_t prev;
86 grub_int8_t type;
87 grub_uint8_t height;
88 grub_uint16_t count;
89 grub_uint16_t unused;
90 } __attribute__ ((packed));
92 /* The header of a HFS+ B+ Tree. */
93 struct grub_hfsplus_btheader
95 grub_uint16_t depth;
96 grub_uint32_t root;
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;
117 grub_uint8_t type;
118 grub_uint8_t unused;
119 grub_uint32_t fileid;
120 grub_uint32_t start;
121 } __attribute__ ((packed));
123 struct grub_hfsplus_key
125 union
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
135 grub_uint16_t type;
136 grub_uint16_t flags;
137 grub_uint32_t reserved;
138 grub_uint32_t fileid;
139 grub_uint8_t unused1[4];
140 grub_uint32_t mtime;
141 grub_uint8_t unused2[22];
142 grub_uint16_t mode;
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
170 int parent;
171 char *name;
174 /* Internal representation of an extent overflow key. */
175 struct grub_hfsplus_extkey_internal
177 grub_uint32_t fileid;
178 grub_uint32_t start;
181 struct grub_hfsplus_key_internal
183 union
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];
196 grub_uint64_t size;
197 grub_uint32_t fileid;
198 grub_int32_t mtime;
201 struct grub_hfsplus_btree
203 grub_uint32_t root;
204 int nodesize;
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;
214 grub_disk_t disk;
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). */
226 int embedded_offset;
229 #ifndef GRUB_UTIL
230 static grub_dl_t my_mod;
231 #endif
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;
254 unsigned int offset;
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. */
263 static int
264 grub_hfsplus_find_block (struct grub_hfsplus_extent *extent,
265 int *fileblock)
267 int i;
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;
279 return -1;
282 static grub_err_t
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];
301 while (1)
303 struct grub_hfsplus_extkey *key;
304 struct grub_hfsplus_extkey_internal extoverflow;
305 int blk;
306 int ptr;
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. */
313 grub_free (nnode);
314 nnode = 0;
316 if (blk != -1)
317 return (blk
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
323 bug... */
324 if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
326 grub_error (GRUB_ERR_READ_ERROR,
327 "extra extents found in an extend overflow file");
328 break;
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);
342 break;
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. */
354 grub_free (nnode);
356 /* Too bad, you lose. */
357 return -1;
361 /* Read LEN bytes from the file described by DATA starting with byte
362 POS. Return the amount of read bytes in READ. */
363 static grub_ssize_t
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,
371 node->size,
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;
381 union {
382 struct grub_hfs_sblock hfs;
383 struct grub_hfsplus_volheader hfsplus;
384 } volheader;
386 data = grub_malloc (sizeof (*data));
387 if (!data)
388 return 0;
390 data->disk = disk;
392 /* Read the bootblock. */
393 grub_disk_read (disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader),
394 (char *) &volheader);
395 if (grub_errno)
396 goto fail;
398 data->embedded_offset = 0;
399 if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC)
401 int extent_start;
402 int ablk_size;
403 int ablk_start;
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");
409 goto fail;
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
417 + extent_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);
422 if (grub_errno)
423 goto fail;
426 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
427 HFX? */
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");
432 goto fail;
435 grub_memcpy (&data->volheader, &volheader.hfsplus,
436 sizeof (volheader.hfsplus));
438 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data->volheader.blksize),
439 &data->log2blksize))
440 goto fail;
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))
465 goto fail;
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))
473 goto fail;
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))
479 goto fail;
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;
487 return data;
489 fail:
491 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
492 grub_error (GRUB_ERR_BAD_FS, "not a hfsplus filesystem");
494 grub_free (data);
495 return 0;
498 /* Compare the on disk catalog key KEYA with the catalog key we are
499 looking for (KEYB). */
500 static int
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;
506 char *filename;
507 int i;
508 int diff;
510 diff = grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent;
511 if (diff)
512 return diff;
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
531 whatever it was. */
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]);
534 return diff;
537 /* Compare the on disk extent overflow key KEYA with the extent
538 overflow key we are looking for (KEYB). */
539 static int
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;
545 int diff;
547 diff = grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid;
549 if (diff)
550 return diff;
552 diff = grub_be_to_cpu32 (extkey_a->start) - extkey_b->start;
553 return diff;
556 static char *
557 grub_hfsplus_read_symlink (grub_fshelp_node_t node)
559 char *symlink;
560 grub_ssize_t numread;
562 symlink = grub_malloc (node->size + 1);
563 if (!symlink)
564 return 0;
566 numread = grub_hfsplus_read_file (node, 0, 0, node->size, symlink);
567 if (numread != (grub_ssize_t) node->size)
569 grub_free (symlink);
570 return 0;
572 symlink[node->size] = '\0';
574 return symlink;
577 static int
578 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
579 struct grub_hfsplus_btnode *first_node,
580 int first_rec,
581 int (*hook) (void *record))
583 int rec;
585 for (;;)
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)))
593 return 1;
596 if (! first_node->next)
597 break;
599 if (! grub_hfsplus_read_file (&btree->file, 0,
600 (grub_be_to_cpu32 (first_node->next)
601 * btree->nodesize),
602 btree->nodesize, cnode))
603 return 1;
605 /* Don't skip any record in the next iteration. */
606 first_rec = 0;
609 return 0;
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. */
616 static grub_err_t
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;
624 char *node;
625 struct grub_hfsplus_btnode *nodedesc;
626 int rec;
628 node = grub_malloc (btree->nodesize);
629 if (! node)
630 return grub_errno;
632 currnode = btree->root;
633 while (1)
635 int match = 0;
637 /* Read a node. */
638 if (! grub_hfsplus_read_file (&btree->file, 0,
639 (long)currnode * (long)btree->nodesize,
640 btree->nodesize, (char *) node))
642 grub_free (node);
643 return grub_errno;
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
655 record. */
656 if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_LEAF
657 && compare_keys (currkey, key) == 0)
659 /* An exact match was found! */
661 *matchnode = nodedesc;
662 *keyoffset = rec;
664 return 0;
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)
674 break;
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)
682 + 2);
683 currnode = grub_be_to_cpu32 (*pointer);
684 match = 1;
688 /* No match is found, no record with this key exists in the
689 tree. */
690 if (! match)
692 *matchnode = 0;
693 grub_free (node);
694 return 1;
699 static int
700 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir,
701 int NESTED_FUNC_ATTR
702 (*hook) (const char *filename,
703 enum grub_fshelp_filetype filetype,
704 grub_fshelp_node_t node))
706 int ret = 0;
708 auto int list_nodes (void *record);
709 int list_nodes (void *record)
711 struct grub_hfsplus_catkey *catkey;
712 char *filename;
713 int i;
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;
720 fileinfo =
721 (struct grub_hfsplus_catfile *) ((char *) record
722 + grub_be_to_cpu16 (catkey->keylen)
723 + 2 + (grub_be_to_cpu16(catkey->keylen)
724 % 2));
726 /* Stop iterating when the last directory entry is found. */
727 if (grub_be_to_cpu32 (catkey->parent) != dir->fileid)
728 return 1;
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;
740 else
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)
747 return 0;
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)
756 return 0;
759 filename = grub_malloc (grub_be_to_cpu16 (catkey->namelen) + 1);
760 if (! filename)
761 return 0;
763 if (! grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
764 grub_be_to_cpu16 (catkey->namelen)))
766 grub_free (filename);
767 return 0;
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);
798 return ret;
801 struct grub_hfsplus_key_internal intern;
802 struct grub_hfsplus_btnode *node;
803 int ptr;
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))
812 return 0;
814 /* Iterate over all entries in this directory. */
815 grub_hfsplus_btree_iterate_node (&dir->data->catalog_tree, node, ptr,
816 list_nodes);
818 grub_free (node);
820 return ret;
823 /* Open a file named NAME and initialize FILE. */
824 static grub_err_t
825 grub_hfsplus_open (struct grub_file *file, const char *name)
827 struct grub_hfsplus_data *data;
828 struct grub_fshelp_node *fdiro = 0;
830 #ifndef GRUB_UTIL
831 grub_dl_ref (my_mod);
832 #endif
834 data = grub_hfsplus_mount (file->device->disk);
835 if (!data)
836 goto fail;
838 grub_fshelp_find_file (name, &data->dirroot, &fdiro,
839 grub_hfsplus_iterate_dir,
840 grub_hfsplus_read_symlink, GRUB_FSHELP_REG);
841 if (grub_errno)
842 goto fail;
844 file->size = fdiro->size;
845 data->opened_file = *fdiro;
846 grub_free (fdiro);
848 file->data = data;
849 file->offset = 0;
851 return 0;
853 fail:
854 if (data && fdiro != &data->dirroot)
855 grub_free (fdiro);
856 grub_free (data);
858 #ifndef GRUB_UTIL
859 grub_dl_unref (my_mod);
860 #endif
862 return grub_errno;
866 static grub_err_t
867 grub_hfsplus_close (grub_file_t file)
869 grub_free (file->data);
871 #ifndef GRUB_UTIL
872 grub_dl_unref (my_mod);
873 #endif
875 return GRUB_ERR_NONE;
879 /* Read LEN bytes data from FILE into BUF. */
880 static grub_ssize_t
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);
889 return size;
893 static grub_err_t
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);
912 info.mtimeset = 1;
913 info.mtime = node->mtime;
914 info.case_insensitive = !! (filetype & GRUB_FSHELP_CASE_INSENSITIVE);
915 grub_free (node);
916 return hook (filename, &info);
919 #ifndef GRUB_UTIL
920 grub_dl_ref (my_mod);
921 #endif
923 data = grub_hfsplus_mount (device->disk);
924 if (!data)
925 goto fail;
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);
931 if (grub_errno)
932 goto fail;
934 /* Iterate over all entries in this directory. */
935 grub_hfsplus_iterate_dir (fdiro, iterate);
937 fail:
938 if (data && fdiro != &data->dirroot)
939 grub_free (fdiro);
940 grub_free (data);
942 #ifndef GRUB_UTIL
943 grub_dl_unref (my_mod);
944 #endif
946 return grub_errno;
950 static grub_err_t
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");
960 /* Get mtime. */
961 static grub_err_t
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;
967 #ifndef GRUB_UTIL
968 grub_dl_ref (my_mod);
969 #endif
971 data = grub_hfsplus_mount (disk);
972 if (!data)
973 *tm = 0;
974 else
975 *tm = grub_be_to_cpu32 (data->volheader.utime) - 2082844800;
977 #ifndef GRUB_UTIL
978 grub_dl_unref (my_mod);
979 #endif
981 grub_free (data);
983 return grub_errno;
987 static grub_err_t
988 grub_hfsplus_uuid (grub_device_t device, char **uuid)
990 struct grub_hfsplus_data *data;
991 grub_disk_t disk = device->disk;
993 #ifndef GRUB_UTIL
994 grub_dl_ref (my_mod);
995 #endif
997 data = grub_hfsplus_mount (disk);
998 if (data)
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));
1005 else
1006 *uuid = NULL;
1008 #ifndef GRUB_UTIL
1009 grub_dl_unref (my_mod);
1010 #endif
1012 grub_free (data);
1014 return grub_errno;
1019 static struct grub_fs grub_hfsplus_fs =
1021 .name = "hfsplus",
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,
1029 .next = 0
1032 GRUB_MOD_INIT(hfsplus)
1034 grub_fs_register (&grub_hfsplus_fs);
1035 #ifndef GRUB_UTIL
1036 my_mod = mod;
1037 #endif
1040 GRUB_MOD_FINI(hfsplus)
1042 grub_fs_unregister (&grub_hfsplus_fs);