Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / fs / hfsplus.c
blobe6493ce03b2c5168dbc017c1b8c6964ecfef0cba
1 /* hfsplus.c - HFS+ Filesystem. */
2 /*
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 */
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 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
81 grub_uint32_t next;
82 grub_uint32_t prev;
83 grub_int8_t type;
84 grub_uint8_t height;
85 grub_uint16_t count;
86 grub_uint16_t unused;
87 } __attribute__ ((packed));
89 /* The header of a HFS+ B+ Tree. */
90 struct grub_hfsplus_btheader
92 grub_uint16_t depth;
93 grub_uint32_t root;
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;
114 grub_uint8_t type;
115 grub_uint8_t unused;
116 grub_uint32_t fileid;
117 grub_uint32_t start;
118 } __attribute__ ((packed));
120 struct grub_hfsplus_key
122 union
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
132 grub_uint16_t type;
133 grub_uint16_t flags;
134 grub_uint32_t reserved;
135 grub_uint32_t fileid;
136 grub_uint8_t unused1[30];
137 grub_uint16_t mode;
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
165 int parent;
166 char *name;
169 /* Internal representation of an extent overflow key. */
170 struct grub_hfsplus_extkey_internal
172 grub_uint32_t fileid;
173 grub_uint32_t start;
176 struct grub_hfsplus_key_internal
178 union
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];
191 grub_uint64_t size;
192 grub_uint32_t fileid;
195 struct grub_hfsplus_btree
197 grub_uint32_t root;
198 int nodesize;
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;
208 grub_disk_t disk;
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). */
220 int embedded_offset;
223 #ifndef GRUB_UTIL
224 static grub_dl_t my_mod;
225 #endif
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;
248 unsigned int offset;
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. */
257 static int
258 grub_hfsplus_find_block (struct grub_hfsplus_extent *extent,
259 int *fileblock)
261 int i;
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;
273 return -1;
276 static grub_err_t
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];
295 while (1)
297 struct grub_hfsplus_extkey *key;
298 struct grub_hfsplus_extkey_internal extoverflow;
299 int blk;
300 int ptr;
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. */
307 grub_free (nnode);
308 nnode = 0;
310 if (blk != -1)
311 return (blk
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
317 bug... */
318 if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
320 grub_error (GRUB_ERR_READ_ERROR,
321 "extra extents found in an extend overflow file");
322 break;
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);
336 break;
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. */
348 grub_free (nnode);
350 /* Too bad, you lose. */
351 return -1;
355 /* Read LEN bytes from the file described by DATA starting with byte
356 POS. Return the amount of read bytes in READ. */
357 static grub_ssize_t
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,
365 node->size,
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;
375 union {
376 struct grub_hfs_sblock hfs;
377 struct grub_hfsplus_volheader hfsplus;
378 } volheader;
380 data = grub_malloc (sizeof (*data));
381 if (!data)
382 return 0;
384 data->disk = disk;
386 /* Read the bootblock. */
387 grub_disk_read (disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader),
388 (char *) &volheader);
389 if (grub_errno)
390 goto fail;
392 data->embedded_offset = 0;
393 if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC)
395 int extent_start;
396 int ablk_size;
397 int ablk_start;
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");
403 goto fail;
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
411 + extent_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);
416 if (grub_errno)
417 goto fail;
420 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
421 HFX? */
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");
426 goto fail;
429 grub_memcpy (&data->volheader, &volheader.hfsplus,
430 sizeof (volheader.hfsplus));
432 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data->volheader.blksize),
433 &data->log2blksize))
434 goto fail;
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))
459 goto fail;
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))
467 goto fail;
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))
473 goto fail;
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;
481 return data;
483 fail:
485 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
486 grub_error (GRUB_ERR_BAD_FS, "not a hfsplus filesystem");
488 grub_free (data);
489 return 0;
492 /* Compare the on disk catalog key KEYA with the catalog key we are
493 looking for (KEYB). */
494 static int
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;
500 char *filename;
501 int i;
502 int diff;
504 diff = grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent;
505 if (diff)
506 return diff;
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
525 whatever it was. */
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]);
528 return diff;
531 /* Compare the on disk extent overflow key KEYA with the extent
532 overflow key we are looking for (KEYB). */
533 static int
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;
539 int diff;
541 diff = grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid;
543 if (diff)
544 return diff;
546 diff = grub_be_to_cpu32 (extkey_a->start) - extkey_b->start;
547 return diff;
550 static char *
551 grub_hfsplus_read_symlink (grub_fshelp_node_t node)
553 char *symlink;
554 grub_ssize_t numread;
556 symlink = grub_malloc (node->size + 1);
557 if (!symlink)
558 return 0;
560 numread = grub_hfsplus_read_file (node, 0, 0, node->size, symlink);
561 if (numread != (grub_ssize_t) node->size)
563 grub_free (symlink);
564 return 0;
566 symlink[node->size] = '\0';
568 return symlink;
571 static int
572 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
573 struct grub_hfsplus_btnode *first_node,
574 int first_rec,
575 int (*hook) (void *record))
577 int rec;
579 for (;;)
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)))
587 return 1;
590 if (! first_node->next)
591 break;
593 if (! grub_hfsplus_read_file (&btree->file, 0,
594 (grub_be_to_cpu32 (first_node->next)
595 * btree->nodesize),
596 btree->nodesize, cnode))
597 return 1;
599 /* Don't skip any record in the next iteration. */
600 first_rec = 0;
603 return 0;
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. */
610 static grub_err_t
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;
618 char *node;
619 struct grub_hfsplus_btnode *nodedesc;
620 int rec;
622 node = grub_malloc (btree->nodesize);
623 if (! node)
624 return grub_errno;
626 currnode = btree->root;
627 while (1)
629 int match = 0;
631 /* Read a node. */
632 if (! grub_hfsplus_read_file (&btree->file, 0,
633 (long)currnode * (long)btree->nodesize,
634 btree->nodesize, (char *) node))
636 grub_free (node);
637 return grub_errno;
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
649 record. */
650 if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_LEAF
651 && compare_keys (currkey, key) == 0)
653 /* An exact match was found! */
655 *matchnode = nodedesc;
656 *keyoffset = rec;
658 return 0;
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)
668 break;
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)
676 + 2);
677 currnode = grub_be_to_cpu32 (*pointer);
678 match = 1;
682 /* No match is found, no record with this key exists in the
683 tree. */
684 if (! match)
686 *matchnode = 0;
687 grub_free (node);
688 return 1;
693 static int
694 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir,
695 int NESTED_FUNC_ATTR
696 (*hook) (const char *filename,
697 enum grub_fshelp_filetype filetype,
698 grub_fshelp_node_t node))
700 int ret = 0;
702 auto int list_nodes (void *record);
703 int list_nodes (void *record)
705 struct grub_hfsplus_catkey *catkey;
706 char *filename;
707 int i;
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;
714 fileinfo =
715 (struct grub_hfsplus_catfile *) ((char *) record
716 + grub_be_to_cpu16 (catkey->keylen)
717 + 2 + (grub_be_to_cpu16(catkey->keylen)
718 % 2));
720 /* Stop iterating when the last directory entry is found. */
721 if (grub_be_to_cpu32 (catkey->parent) != dir->fileid)
722 return 1;
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;
734 else
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)
741 return 0;
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)
750 return 0;
753 filename = grub_malloc (grub_be_to_cpu16 (catkey->namelen) + 1);
754 if (! filename)
755 return 0;
757 if (! grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
758 grub_be_to_cpu16 (catkey->namelen)))
760 grub_free (filename);
761 return 0;
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);
788 return ret;
791 struct grub_hfsplus_key_internal intern;
792 struct grub_hfsplus_btnode *node;
793 int ptr;
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))
802 return 0;
804 /* Iterate over all entries in this directory. */
805 grub_hfsplus_btree_iterate_node (&dir->data->catalog_tree, node, ptr,
806 list_nodes);
808 grub_free (node);
810 return ret;
813 /* Open a file named NAME and initialize FILE. */
814 static grub_err_t
815 grub_hfsplus_open (struct grub_file *file, const char *name)
817 struct grub_hfsplus_data *data;
818 struct grub_fshelp_node *fdiro = 0;
820 #ifndef GRUB_UTIL
821 grub_dl_ref (my_mod);
822 #endif
824 data = grub_hfsplus_mount (file->device->disk);
825 if (!data)
826 goto fail;
828 grub_fshelp_find_file (name, &data->dirroot, &fdiro,
829 grub_hfsplus_iterate_dir,
830 grub_hfsplus_read_symlink, GRUB_FSHELP_REG);
831 if (grub_errno)
832 goto fail;
834 file->size = fdiro->size;
835 data->opened_file = *fdiro;
836 grub_free (fdiro);
838 file->data = data;
839 file->offset = 0;
841 return 0;
843 fail:
844 if (data && fdiro != &data->dirroot)
845 grub_free (fdiro);
846 grub_free (data);
848 #ifndef GRUB_UTIL
849 grub_dl_unref (my_mod);
850 #endif
852 return grub_errno;
856 static grub_err_t
857 grub_hfsplus_close (grub_file_t file)
859 grub_free (file->data);
861 #ifndef GRUB_UTIL
862 grub_dl_unref (my_mod);
863 #endif
865 return GRUB_ERR_NONE;
869 /* Read LEN bytes data from FILE into BUF. */
870 static grub_ssize_t
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);
879 return size;
883 static grub_err_t
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)
898 grub_free (node);
900 if (filetype == GRUB_FSHELP_DIR)
901 return hook (filename, 1);
902 else
903 return hook (filename, 0);
905 return 0;
908 #ifndef GRUB_UTIL
909 grub_dl_ref (my_mod);
910 #endif
912 data = grub_hfsplus_mount (device->disk);
913 if (!data)
914 goto fail;
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);
920 if (grub_errno)
921 goto fail;
923 /* Iterate over all entries in this directory. */
924 grub_hfsplus_iterate_dir (fdiro, iterate);
926 fail:
927 if (data && fdiro != &data->dirroot)
928 grub_free (fdiro);
929 grub_free (data);
931 #ifndef GRUB_UTIL
932 grub_dl_unref (my_mod);
933 #endif
935 return grub_errno;
939 static grub_err_t
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 =
952 .name = "hfsplus",
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,
958 .next = 0
961 GRUB_MOD_INIT(hfsplus)
963 grub_fs_register (&grub_hfsplus_fs);
964 #ifndef GRUB_UTIL
965 my_mod = mod;
966 #endif
969 GRUB_MOD_FINI(hfsplus)
971 grub_fs_unregister (&grub_hfsplus_fs);