2009-07-19 Yves BLUSSEAU <yves.grub-devel@zetam.org>
[grub2/phcoder.git] / fs / hfsplus.c
blob31bb54014b6afa0f6365b95d36da6c3547130615
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 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;
124 grub_uint8_t type;
125 grub_uint8_t unused;
126 grub_uint32_t fileid;
127 grub_uint32_t start;
128 } __attribute__ ((packed));
130 struct grub_hfsplus_key
132 union
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
142 grub_uint16_t type;
143 grub_uint16_t flags;
144 grub_uint32_t reserved;
145 grub_uint32_t fileid;
146 grub_uint8_t unused1[4];
147 grub_uint32_t mtime;
148 grub_uint8_t unused2[22];
149 grub_uint16_t mode;
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
180 int parent;
181 char *name;
184 /* Internal representation of an extent overflow key. */
185 struct grub_hfsplus_extkey_internal
187 grub_uint32_t fileid;
188 grub_uint32_t start;
191 struct grub_hfsplus_key_internal
193 union
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];
206 grub_uint64_t size;
207 grub_uint32_t fileid;
208 grub_int32_t mtime;
211 struct grub_hfsplus_btree
213 grub_uint32_t root;
214 int nodesize;
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;
224 grub_disk_t disk;
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). */
236 int embedded_offset;
237 int case_sensitive;
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;
263 unsigned int offset;
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. */
272 static int
273 grub_hfsplus_find_block (struct grub_hfsplus_extent *extent,
274 int *fileblock)
276 int i;
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;
288 return -1;
291 static grub_err_t
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];
310 while (1)
312 struct grub_hfsplus_extkey *key;
313 struct grub_hfsplus_extkey_internal extoverflow;
314 int blk;
315 int ptr;
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. */
322 grub_free (nnode);
323 nnode = 0;
325 if (blk != -1)
326 return (blk
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
332 bug... */
333 if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
335 grub_error (GRUB_ERR_READ_ERROR,
336 "extra extents found in an extend overflow file");
337 break;
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);
351 break;
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. */
363 grub_free (nnode);
365 /* Too bad, you lose. */
366 return -1;
370 /* Read LEN bytes from the file described by DATA starting with byte
371 POS. Return the amount of read bytes in READ. */
372 static grub_ssize_t
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,
380 node->size,
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;
390 grub_uint16_t magic;
391 union {
392 struct grub_hfs_sblock hfs;
393 struct grub_hfsplus_volheader hfsplus;
394 } volheader;
396 data = grub_malloc (sizeof (*data));
397 if (!data)
398 return 0;
400 data->disk = disk;
402 /* Read the bootblock. */
403 grub_disk_read (disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader),
404 &volheader);
405 if (grub_errno)
406 goto fail;
408 data->embedded_offset = 0;
409 if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC)
411 int extent_start;
412 int ablk_size;
413 int ablk_start;
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");
419 goto fail;
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
427 + extent_start
428 * (ablk_size >> GRUB_DISK_SECTOR_BITS));
430 grub_disk_read (disk, data->embedded_offset + GRUB_HFSPLUS_SBLOCK, 0,
431 sizeof (volheader), &volheader);
432 if (grub_errno)
433 goto fail;
436 /* Make sure this is an HFS+ filesystem. XXX: Do we really support
437 HFX? */
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");
442 goto fail;
445 grub_memcpy (&data->volheader, &volheader.hfsplus,
446 sizeof (volheader.hfsplus));
448 if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data->volheader.blksize),
449 &data->log2blksize))
450 goto fail;
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))
475 goto fail;
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))
485 goto fail;
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))
491 goto fail;
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;
499 return data;
501 fail:
503 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
504 grub_error (GRUB_ERR_BAD_FS, "not a hfsplus filesystem");
506 grub_free (data);
507 return 0;
510 /* Compare the on disk catalog key KEYA with the catalog key we are
511 looking for (KEYB). */
512 static int
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;
518 char *filename;
519 int i;
520 int diff;
522 diff = grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent;
523 if (diff)
524 return diff;
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
543 whatever it was. */
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]);
546 return diff;
549 /* Compare the on disk extent overflow key KEYA with the extent
550 overflow key we are looking for (KEYB). */
551 static int
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;
557 int diff;
559 diff = grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid;
561 if (diff)
562 return diff;
564 diff = grub_be_to_cpu32 (extkey_a->start) - extkey_b->start;
565 return diff;
568 static char *
569 grub_hfsplus_read_symlink (grub_fshelp_node_t node)
571 char *symlink;
572 grub_ssize_t numread;
574 symlink = grub_malloc (node->size + 1);
575 if (!symlink)
576 return 0;
578 numread = grub_hfsplus_read_file (node, 0, 0, node->size, symlink);
579 if (numread != (grub_ssize_t) node->size)
581 grub_free (symlink);
582 return 0;
584 symlink[node->size] = '\0';
586 return symlink;
589 static int
590 grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
591 struct grub_hfsplus_btnode *first_node,
592 int first_rec,
593 int (*hook) (void *record))
595 int rec;
597 for (;;)
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)))
605 return 1;
608 if (! first_node->next)
609 break;
611 if (! grub_hfsplus_read_file (&btree->file, 0,
612 (grub_be_to_cpu32 (first_node->next)
613 * btree->nodesize),
614 btree->nodesize, cnode))
615 return 1;
617 /* Don't skip any record in the next iteration. */
618 first_rec = 0;
621 return 0;
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. */
628 static grub_err_t
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;
636 char *node;
637 struct grub_hfsplus_btnode *nodedesc;
638 int rec;
640 node = grub_malloc (btree->nodesize);
641 if (! node)
642 return grub_errno;
644 currnode = btree->root;
645 while (1)
647 int match = 0;
649 /* Read a node. */
650 if (! grub_hfsplus_read_file (&btree->file, 0,
651 (long)currnode * (long)btree->nodesize,
652 btree->nodesize, (char *) node))
654 grub_free (node);
655 return grub_errno;
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
667 record. */
668 if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_LEAF
669 && compare_keys (currkey, key) == 0)
671 /* An exact match was found! */
673 *matchnode = nodedesc;
674 *keyoffset = rec;
676 return 0;
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)
686 break;
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)
694 + 2);
695 currnode = grub_be_to_cpu32 (*pointer);
696 match = 1;
700 /* No match is found, no record with this key exists in the
701 tree. */
702 if (! match)
704 *matchnode = 0;
705 grub_free (node);
706 return 1;
711 static int
712 grub_hfsplus_iterate_dir (grub_fshelp_node_t dir,
713 int NESTED_FUNC_ATTR
714 (*hook) (const char *filename,
715 enum grub_fshelp_filetype filetype,
716 grub_fshelp_node_t node))
718 int ret = 0;
720 auto int list_nodes (void *record);
721 int list_nodes (void *record)
723 struct grub_hfsplus_catkey *catkey;
724 char *filename;
725 int i;
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;
732 fileinfo =
733 (struct grub_hfsplus_catfile *) ((char *) record
734 + grub_be_to_cpu16 (catkey->keylen)
735 + 2 + (grub_be_to_cpu16(catkey->keylen)
736 % 2));
738 /* Stop iterating when the last directory entry is found. */
739 if (grub_be_to_cpu32 (catkey->parent) != dir->fileid)
740 return 1;
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;
752 else
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)
759 return 0;
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)
768 return 0;
771 filename = grub_malloc (grub_be_to_cpu16 (catkey->namelen) + 1);
772 if (! filename)
773 return 0;
775 if (! grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
776 grub_be_to_cpu16 (catkey->namelen)))
778 grub_free (filename);
779 return 0;
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);
811 return ret;
814 struct grub_hfsplus_key_internal intern;
815 struct grub_hfsplus_btnode *node;
816 int ptr;
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))
825 return 0;
827 /* Iterate over all entries in this directory. */
828 grub_hfsplus_btree_iterate_node (&dir->data->catalog_tree, node, ptr,
829 list_nodes);
831 grub_free (node);
833 return ret;
836 /* Open a file named NAME and initialize FILE. */
837 static grub_err_t
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);
846 if (!data)
847 goto fail;
849 grub_fshelp_find_file (name, &data->dirroot, &fdiro,
850 grub_hfsplus_iterate_dir,
851 grub_hfsplus_read_symlink, GRUB_FSHELP_REG);
852 if (grub_errno)
853 goto fail;
855 file->size = fdiro->size;
856 data->opened_file = *fdiro;
857 grub_free (fdiro);
859 file->data = data;
860 file->offset = 0;
862 return 0;
864 fail:
865 if (data && fdiro != &data->dirroot)
866 grub_free (fdiro);
867 grub_free (data);
869 grub_dl_unref (my_mod);
871 return grub_errno;
875 static grub_err_t
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. */
887 static grub_ssize_t
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);
896 return size;
900 static grub_err_t
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);
919 info.mtimeset = 1;
920 info.mtime = node->mtime;
921 info.case_insensitive = !! (filetype & GRUB_FSHELP_CASE_INSENSITIVE);
922 grub_free (node);
923 return hook (filename, &info);
926 grub_dl_ref (my_mod);
928 data = grub_hfsplus_mount (device->disk);
929 if (!data)
930 goto fail;
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);
936 if (grub_errno)
937 goto fail;
939 /* Iterate over all entries in this directory. */
940 grub_hfsplus_iterate_dir (fdiro, iterate);
942 fail:
943 if (data && fdiro != &data->dirroot)
944 grub_free (fdiro);
945 grub_free (data);
947 grub_dl_unref (my_mod);
949 return grub_errno;
953 static grub_err_t
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");
963 /* Get mtime. */
964 static grub_err_t
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);
973 if (!data)
974 *tm = 0;
975 else
976 *tm = grub_be_to_cpu32 (data->volheader.utime) - 2082844800;
978 grub_dl_unref (my_mod);
980 grub_free (data);
982 return grub_errno;
986 static grub_err_t
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);
995 if (data)
997 *uuid = grub_malloc (16 + sizeof ('\0'));
998 grub_sprintf (*uuid, "%016llx",
999 (unsigned long long)
1000 grub_be_to_cpu64 (data->volheader.num_serial));
1002 else
1003 *uuid = NULL;
1005 grub_dl_unref (my_mod);
1007 grub_free (data);
1009 return grub_errno;
1014 static struct grub_fs grub_hfsplus_fs =
1016 .name = "hfsplus",
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,
1024 .next = 0
1027 GRUB_MOD_INIT(hfsplus)
1029 grub_fs_register (&grub_hfsplus_fs);
1030 my_mod = mod;
1033 GRUB_MOD_FINI(hfsplus)
1035 grub_fs_unregister (&grub_hfsplus_fs);