make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / fs / sfs.c
blobab31874baa04410d74e1b73485894dc852986018
1 /* sfs.c - Amiga Smart FileSystem. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007 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 #include <grub/err.h>
21 #include <grub/file.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/dl.h>
26 #include <grub/types.h>
27 #include <grub/fshelp.h>
29 /* The common header for a block. */
30 struct grub_sfs_bheader
32 grub_uint8_t magic[4];
33 grub_uint32_t chksum;
34 grub_uint32_t ipointtomyself;
35 } __attribute__ ((packed));
37 /* The sfs rootblock. */
38 struct grub_sfs_rblock
40 struct grub_sfs_bheader header;
41 grub_uint32_t version;
42 grub_uint8_t unused1[36];
43 grub_uint32_t blocksize;
44 grub_uint8_t unused2[40];
45 grub_uint8_t unused3[8];
46 grub_uint32_t rootobject;
47 grub_uint32_t btree;
48 } __attribute__ ((packed));
50 /* A SFS object container. */
51 struct grub_sfs_obj
53 grub_uint8_t unused1[4];
54 grub_uint32_t nodeid;
55 grub_uint8_t unused2[4];
56 union
58 struct
60 grub_uint32_t first_block;
61 grub_uint32_t size;
62 } file __attribute__ ((packed));
63 struct
65 grub_uint32_t hashtable;
66 grub_uint32_t dir_objc;
67 } dir __attribute__ ((packed));
68 } file_dir;
69 grub_uint8_t unused3[4];
70 grub_uint8_t type;
71 grub_uint8_t filename[1];
72 grub_uint8_t comment[1];
73 } __attribute__ ((packed));
75 #define GRUB_SFS_TYPE_DELETED 32
76 #define GRUB_SFS_TYPE_SYMLINK 64
77 #define GRUB_SFS_TYPE_DIR 128
79 /* A SFS object container. */
80 struct grub_sfs_objc
82 struct grub_sfs_bheader header;
83 grub_uint32_t parent;
84 grub_uint32_t next;
85 grub_uint32_t prev;
86 /* The amount of objects depends on the blocksize. */
87 struct grub_sfs_obj objects[1];
88 } __attribute__ ((packed));
90 struct grub_sfs_btree_node
92 grub_uint32_t key;
93 grub_uint32_t data;
94 } __attribute__ ((packed));
96 struct grub_sfs_btree_extent
98 grub_uint32_t key;
99 grub_uint32_t next;
100 grub_uint32_t prev;
101 grub_uint16_t size;
102 } __attribute__ ((packed));
104 struct grub_sfs_btree
106 struct grub_sfs_bheader header;
107 grub_uint16_t nodes;
108 grub_uint8_t leaf;
109 grub_uint8_t nodesize;
110 /* Normally this can be kind of node, but just extents are
111 supported. */
112 struct grub_sfs_btree_node node[1];
113 } __attribute__ ((packed));
117 struct grub_fshelp_node
119 struct grub_sfs_data *data;
120 int block;
121 int size;
124 /* Information about a "mounted" sfs filesystem. */
125 struct grub_sfs_data
127 struct grub_sfs_rblock rblock;
128 struct grub_fshelp_node diropen;
129 grub_disk_t disk;
131 /* Blocksize in sectors. */
132 unsigned int blocksize;
134 /* Label of the filesystem. */
135 char *label;
138 #ifndef GRUB_UTIL
139 static grub_dl_t my_mod;
140 #endif
143 /* Lookup the extent starting with BLOCK in the filesystem described
144 by DATA. Return the extent size in SIZE and the following extent
145 in NEXTEXT. */
146 static grub_err_t
147 grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
148 int *size, int *nextext)
150 char *treeblock;
151 struct grub_sfs_btree *tree;
152 int i;
153 int next;
154 int prev;
156 treeblock = grub_malloc (data->blocksize);
157 if (!block)
158 return 0;
160 next = grub_be_to_cpu32 (data->rblock.btree);
161 tree = (struct grub_sfs_btree *) treeblock;
163 /* Handle this level in the btree. */
166 prev = 0;
168 grub_disk_read (data->disk, next, 0, data->blocksize, treeblock);
169 if (grub_errno)
171 grub_free (treeblock);
172 return grub_errno;
175 grub_uint16_t nodescount = grub_be_to_cpu16(tree->nodes);
176 for (i = 0; i < nodescount; i++)
179 #define EXTNODE(tree, index) \
180 ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
181 + (index) * (tree)->nodesize))
183 /* Follow the tree down to the leaf level. */
184 if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) >= block)
185 && !tree->leaf)
187 next = grub_be_to_cpu32 (EXTNODE (tree, i - 1)->data);
188 break;
191 /* In case the last node is reached just use that one, it is
192 the right match. */
193 if (i + 1 == nodescount && !tree->leaf)
195 next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
196 break;
199 /* If the leaf level is reached, just find the correct extent. */
200 if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
202 struct grub_sfs_btree_extent *extent;
203 extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
205 /* We found a correct leaf. */
206 *size = grub_be_to_cpu16 (extent->size);
207 *nextext = grub_be_to_cpu32 (extent->next);
209 grub_free (treeblock);
210 return 0;
213 #undef EXTNODE
216 } while (!tree->leaf);
218 grub_free (treeblock);
220 return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
223 static int
224 grub_sfs_read_block (grub_fshelp_node_t node, int fileblock)
226 int blk = node->block;
227 int size = 0;
228 int next = 0;
230 while (blk)
232 grub_err_t err;
234 /* In case of the first block we don't have to lookup the
235 extent, the minimum size is always 1. */
236 if (fileblock == 0)
237 return blk;
239 err = grub_sfs_read_extent (node->data, blk, &size, &next);
240 if (err)
241 return 0;
243 if (fileblock < size)
244 return fileblock + blk;
246 fileblock -= size;
248 blk = next;
251 grub_error (GRUB_ERR_FILE_READ_ERROR,
252 "reading a SFS block outside the extent");
254 return 0;
258 /* Read LEN bytes from the file described by DATA starting with byte
259 POS. Return the amount of read bytes in READ. */
260 static grub_ssize_t
261 grub_sfs_read_file (grub_fshelp_node_t node,
262 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
263 unsigned offset, unsigned length),
264 int pos, grub_size_t len, char *buf)
266 return grub_fshelp_read_file (node->data->disk, node, read_hook,
267 pos, len, buf, grub_sfs_read_block,
268 node->size, 0);
272 static struct grub_sfs_data *
273 grub_sfs_mount (grub_disk_t disk)
275 struct grub_sfs_data *data;
276 struct grub_sfs_objc *rootobjc;
277 char *rootobjc_data = 0;
278 unsigned int blk;
280 data = grub_malloc (sizeof (*data));
281 if (!data)
282 return 0;
284 /* Read the rootblock. */
285 grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
286 (char *) &data->rblock);
287 if (grub_errno)
288 goto fail;
290 /* Make sure this is a sfs filesystem. */
291 if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4))
293 grub_error (GRUB_ERR_BAD_FS, "not a sfs filesystem");
294 goto fail;
297 data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize);
298 rootobjc_data = grub_malloc (data->blocksize);
299 if (! rootobjc_data)
300 goto fail;
302 /* Read the root object container. */
303 grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0,
304 data->blocksize, rootobjc_data);
305 if (grub_errno)
306 goto fail;
308 rootobjc = (struct grub_sfs_objc *) rootobjc_data;
310 blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
311 data->diropen.size = 0;
312 data->diropen.block = blk;
313 data->diropen.data = data;
314 data->disk = disk;
315 data->label = grub_strdup ((char *) (rootobjc->objects[0].filename));
317 return data;
319 fail:
320 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
321 grub_error (GRUB_ERR_BAD_FS, "not an sfs filesystem");
323 grub_free (data);
324 grub_free (rootobjc_data);
325 return 0;
329 static char *
330 grub_sfs_read_symlink (grub_fshelp_node_t node)
332 struct grub_sfs_data *data = node->data;
333 char *symlink;
334 char *block;
336 block = grub_malloc (data->blocksize);
337 if (!block)
338 return 0;
340 grub_disk_read (data->disk, node->block, 0, data->blocksize, block);
341 if (grub_errno)
343 grub_free (block);
344 return 0;
347 /* This is just a wild guess, but it always worked for me. How the
348 SLNK block looks like is not documented in the SFS docs. */
349 symlink = grub_strdup (&block[24]);
350 grub_free (block);
351 if (!symlink)
352 return 0;
354 return symlink;
357 static int
358 grub_sfs_iterate_dir (grub_fshelp_node_t dir,
359 int NESTED_FUNC_ATTR
360 (*hook) (const char *filename,
361 enum grub_fshelp_filetype filetype,
362 grub_fshelp_node_t node))
364 struct grub_fshelp_node *node = 0;
365 struct grub_sfs_data *data = dir->data;
366 char *objc_data;
367 struct grub_sfs_objc *objc;
368 unsigned int next = dir->block;
369 int pos;
371 auto int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
372 int size, int type);
374 int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
375 int size, int type)
377 node = grub_malloc (sizeof (*node));
378 if (!node)
379 return 1;
381 node->data = data;
382 node->size = size;
383 node->block = block;
385 return hook (name, type, node);
388 objc_data = grub_malloc (data->blocksize);
389 if (!objc_data)
390 goto fail;
392 /* The Object container can consist of multiple blocks, iterate over
393 every block. */
394 while (next)
396 grub_disk_read (data->disk, next, 0, data->blocksize, objc_data);
397 if (grub_errno)
398 goto fail;
400 objc = (struct grub_sfs_objc *) objc_data;
402 pos = (char *) &objc->objects[0] - (char *) objc;
404 /* Iterate over all entries in this block. */
405 while (pos + sizeof (struct grub_sfs_obj) < data->blocksize)
407 struct grub_sfs_obj *obj;
408 obj = (struct grub_sfs_obj *) ((char *) objc + pos);
409 char *filename = (char *) (obj->filename);
410 int len;
411 enum grub_fshelp_filetype type;
412 unsigned int block;
414 /* The filename and comment dynamically increase the size of
415 the object. */
416 len = grub_strlen (filename);
417 len += grub_strlen (filename + len + 1);
419 pos += sizeof (*obj) + len;
420 /* Round up to a multiple of two bytes. */
421 pos = ((pos + 1) >> 1) << 1;
423 if (grub_strlen (filename) == 0)
424 continue;
426 /* First check if the file was not deleted. */
427 if (obj->type & GRUB_SFS_TYPE_DELETED)
428 continue;
429 else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
430 type = GRUB_FSHELP_SYMLINK;
431 else if (obj->type & GRUB_SFS_TYPE_DIR)
432 type = GRUB_FSHELP_DIR;
433 else
434 type = GRUB_FSHELP_REG;
436 if (type == GRUB_FSHELP_DIR)
437 block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
438 else
439 block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
441 if (grub_sfs_create_node (filename, block,
442 grub_be_to_cpu32 (obj->file_dir.file.size),
443 type))
445 grub_free (objc_data);
446 return 1;
450 next = grub_be_to_cpu32 (objc->next);
453 fail:
454 grub_free (objc_data);
455 return 1;
459 /* Open a file named NAME and initialize FILE. */
460 static grub_err_t
461 grub_sfs_open (struct grub_file *file, const char *name)
463 struct grub_sfs_data *data;
464 struct grub_fshelp_node *fdiro = 0;
466 #ifndef GRUB_UTIL
467 grub_dl_ref (my_mod);
468 #endif
470 data = grub_sfs_mount (file->device->disk);
471 if (!data)
472 goto fail;
474 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir,
475 grub_sfs_read_symlink, GRUB_FSHELP_REG);
476 if (grub_errno)
477 goto fail;
479 file->size = fdiro->size;
480 data->diropen = *fdiro;
481 grub_free (fdiro);
483 file->data = data;
484 file->offset = 0;
486 return 0;
488 fail:
489 if (data && fdiro != &data->diropen)
490 grub_free (fdiro);
491 if (data)
492 grub_free (data->label);
493 grub_free (data);
495 #ifndef GRUB_UTIL
496 grub_dl_unref (my_mod);
497 #endif
499 return grub_errno;
503 static grub_err_t
504 grub_sfs_close (grub_file_t file)
506 grub_free (file->data);
508 #ifndef GRUB_UTIL
509 grub_dl_unref (my_mod);
510 #endif
512 return GRUB_ERR_NONE;
516 /* Read LEN bytes data from FILE into BUF. */
517 static grub_ssize_t
518 grub_sfs_read (grub_file_t file, char *buf, grub_size_t len)
520 struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
522 int size = grub_sfs_read_file (&data->diropen, file->read_hook,
523 file->offset, len, buf);
525 return size;
529 static grub_err_t
530 grub_sfs_dir (grub_device_t device, const char *path,
531 int (*hook) (const char *filename, int dir))
533 struct grub_sfs_data *data = 0;
534 struct grub_fshelp_node *fdiro = 0;
536 auto int NESTED_FUNC_ATTR iterate (const char *filename,
537 enum grub_fshelp_filetype filetype,
538 grub_fshelp_node_t node);
540 int NESTED_FUNC_ATTR iterate (const char *filename,
541 enum grub_fshelp_filetype filetype,
542 grub_fshelp_node_t node)
544 grub_free (node);
546 if (filetype == GRUB_FSHELP_DIR)
547 return hook (filename, 1);
548 else
549 return hook (filename, 0);
551 return 0;
554 #ifndef GRUB_UTIL
555 grub_dl_ref (my_mod);
556 #endif
558 data = grub_sfs_mount (device->disk);
559 if (!data)
560 goto fail;
562 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir,
563 grub_sfs_read_symlink, GRUB_FSHELP_DIR);
564 if (grub_errno)
565 goto fail;
567 grub_sfs_iterate_dir (fdiro, iterate);
569 fail:
570 if (data && fdiro != &data->diropen)
571 grub_free (fdiro);
572 if (data)
573 grub_free (data->label);
574 grub_free (data);
576 #ifndef GRUB_UTIL
577 grub_dl_unref (my_mod);
578 #endif
580 return grub_errno;
584 static grub_err_t
585 grub_sfs_label (grub_device_t device, char **label)
587 struct grub_sfs_data *data;
588 grub_disk_t disk = device->disk;
590 data = grub_sfs_mount (disk);
591 if (data)
592 *label = data->label;
594 grub_free (data);
596 return grub_errno;
600 static struct grub_fs grub_sfs_fs =
602 .name = "sfs",
603 .dir = grub_sfs_dir,
604 .open = grub_sfs_open,
605 .read = grub_sfs_read,
606 .close = grub_sfs_close,
607 .label = grub_sfs_label,
608 .next = 0
611 GRUB_MOD_INIT(sfs)
613 grub_fs_register (&grub_sfs_fs);
614 #ifndef GRUB_UTIL
615 my_mod = mod;
616 #endif
619 GRUB_MOD_FINI(sfs)
621 grub_fs_unregister (&grub_sfs_fs);