[3bd76491] | 1 | /*
|
---|
[566c401] | 2 | * Copyright (c) 2011 Martin Sucha
|
---|
[3bd76491] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext2
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef LIBEXT2_LIBEXT2_H_
|
---|
| 37 | #define LIBEXT2_LIBEXT2_H_
|
---|
| 38 |
|
---|
[d5e2763] | 39 | #include <byteorder.h>
|
---|
[36bca8eb] | 40 | #include <libblock.h>
|
---|
[3bd76491] | 41 |
|
---|
[d5e2763] | 42 | typedef struct ext2_superblock {
|
---|
[36bca8eb] | 43 | uint8_t unused[20];
|
---|
| 44 | uint32_t first_block; // Block containing the superblock (either 0 or 1)
|
---|
| 45 | uint32_t block_size_log2; // log_2(block_size)
|
---|
| 46 | int32_t fragment_size_log2; // log_2(fragment size)
|
---|
| 47 | uint32_t blocks_per_group; // Number of blocks in one block group
|
---|
| 48 | uint32_t fragments_per_group; // Number of fragments per block group
|
---|
| 49 | uint32_t inodes_per_group; // Number of inodes per block group
|
---|
| 50 | uint8_t unused2[12];
|
---|
| 51 | uint16_t magic; // Magic value
|
---|
[c00e729] | 52 | uint16_t state; // State (mounted/unmounted)
|
---|
| 53 | uint16_t rev_minor; // Minor revision level
|
---|
| 54 | uint8_t unused3[12];
|
---|
| 55 | uint32_t creator_os;
|
---|
| 56 | uint32_t rev_major; // Major revision level
|
---|
| 57 | uint8_t unused4[8];
|
---|
| 58 |
|
---|
| 59 | // Following is for ext2 revision 1 only
|
---|
| 60 | uint32_t first_inode;
|
---|
| 61 | uint16_t inode_size;
|
---|
| 62 | uint8_t unused5[14];
|
---|
| 63 | uint8_t uuid[16]; // UUID TODO: Create a library for UUIDs
|
---|
| 64 | uint8_t volume_name[16];
|
---|
[566c401] | 65 |
|
---|
[36bca8eb] | 66 | // TODO: add __attribute__((aligned(...)) for better performance?
|
---|
| 67 | // (it is necessary to ensure the superblock is correctly aligned then
|
---|
| 68 | // though)
|
---|
[566c401] | 69 | } __attribute__ ((packed)) ext2_superblock_t;
|
---|
| 70 |
|
---|
[d5e2763] | 71 |
|
---|
[36bca8eb] | 72 | typedef struct ext2_filesystem {
|
---|
| 73 | devmap_handle_t device;
|
---|
| 74 | ext2_superblock_t * superblock;
|
---|
| 75 | } ext2_filesystem_t;
|
---|
[d5e2763] | 76 |
|
---|
[36bca8eb] | 77 | #define EXT2_SUPERBLOCK_MAGIC 0xEF53
|
---|
| 78 | #define EXT2_SUPERBLOCK_SIZE 1024
|
---|
| 79 | #define EXT2_SUPERBLOCK_OFFSET 1024
|
---|
| 80 | #define EXT2_SUPERBLOCK_LAST_BYTE (EXT2_SUPERBLOCK_OFFSET + \
|
---|
| 81 | EXT2_SUPERBLOCK_SIZE -1)
|
---|
[8bd5dad] | 82 | // allow maximum this block size
|
---|
| 83 | #define EXT2_MAX_BLOCK_SIZE 8096
|
---|
[c00e729] | 84 | #define EXT2_REV0_FIRST_INODE 11
|
---|
| 85 | #define EXT2_REV0_INODE_SIZE 128
|
---|
[36bca8eb] | 86 |
|
---|
[566c401] | 87 | inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *);
|
---|
| 88 | inline uint32_t ext2_superblock_get_first_block(ext2_superblock_t *);
|
---|
| 89 | inline uint32_t ext2_superblock_get_block_size_log2(ext2_superblock_t *);
|
---|
| 90 | inline uint32_t ext2_superblock_get_block_size(ext2_superblock_t *);
|
---|
| 91 | inline int32_t ext2_superblock_get_fragment_size_log2(ext2_superblock_t *);
|
---|
| 92 | inline uint32_t ext2_superblock_get_fragment_size(ext2_superblock_t *);
|
---|
| 93 | inline uint32_t ext2_superblock_get_blocks_per_group(ext2_superblock_t *);
|
---|
| 94 | inline uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *);
|
---|
[c00e729] | 95 | inline uint16_t ext2_superblock_get_state(ext2_superblock_t *);
|
---|
| 96 | inline uint16_t ext2_superblock_get_rev_minor(ext2_superblock_t *);
|
---|
| 97 | inline uint32_t ext2_superblock_get_rev_major(ext2_superblock_t *);
|
---|
| 98 | inline uint32_t ext2_superblock_get_creator_os(ext2_superblock_t *);
|
---|
| 99 | inline uint32_t ext2_superblock_get_first_inode(ext2_superblock_t *);
|
---|
| 100 | inline uint16_t ext2_superblock_get_inode_size(ext2_superblock_t *);
|
---|
[36bca8eb] | 101 |
|
---|
[566c401] | 102 | extern int ext2_superblock_read_direct(devmap_handle_t, ext2_superblock_t **);
|
---|
[36bca8eb] | 103 |
|
---|
[566c401] | 104 | extern int ext2_filesystem_init(ext2_filesystem_t *, devmap_handle_t);
|
---|
| 105 | extern void ext2_filesystem_fini(ext2_filesystem_t *);
|
---|
[3bd76491] | 106 |
|
---|
| 107 | #endif
|
---|
| 108 |
|
---|
| 109 | /** @}
|
---|
| 110 | */
|
---|