[d5e2763] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
---|
| 3 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup fs
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file ext2info.c
|
---|
| 36 | * @brief Tool for displaying information about ext2 filesystem
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <libblock.h>
|
---|
| 43 | #include <mem.h>
|
---|
| 44 | #include <devmap.h>
|
---|
| 45 | #include <byteorder.h>
|
---|
| 46 | #include <sys/types.h>
|
---|
| 47 | #include <sys/typefmt.h>
|
---|
| 48 | #include <inttypes.h>
|
---|
| 49 | #include <errno.h>
|
---|
| 50 | #include <libext2.h>
|
---|
| 51 |
|
---|
| 52 | #define NAME "ext2info"
|
---|
| 53 |
|
---|
| 54 | static void syntax_print(void);
|
---|
[d241aae] | 55 | static void print_superblock(ext2_superblock_t *);
|
---|
| 56 | static void print_block_groups(ext2_filesystem_t *);
|
---|
| 57 | static void print_block_group(ext2_block_group_t *);
|
---|
[d5e2763] | 58 |
|
---|
| 59 | int main(int argc, char **argv)
|
---|
| 60 | {
|
---|
| 61 |
|
---|
| 62 | int rc;
|
---|
| 63 | char *dev_path;
|
---|
| 64 | devmap_handle_t handle;
|
---|
[8bd5dad] | 65 | ext2_filesystem_t filesystem;
|
---|
[d241aae] | 66 | bool strict_check;
|
---|
[d5e2763] | 67 |
|
---|
| 68 | if (argc < 2) {
|
---|
| 69 | printf(NAME ": Error, argument missing.\n");
|
---|
| 70 | syntax_print();
|
---|
| 71 | return 1;
|
---|
| 72 | }
|
---|
[d241aae] | 73 |
|
---|
| 74 | strict_check = false;
|
---|
| 75 | if (str_cmp(*argv, "--strict-check") == 0) {
|
---|
| 76 | --argc; ++argv;
|
---|
| 77 | strict_check = true;
|
---|
| 78 | }
|
---|
[d5e2763] | 79 |
|
---|
| 80 | --argc; ++argv;
|
---|
| 81 |
|
---|
| 82 | if (argc != 1) {
|
---|
| 83 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 84 | syntax_print();
|
---|
| 85 | return 1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | dev_path = *argv;
|
---|
| 89 |
|
---|
| 90 | rc = devmap_device_get_handle(dev_path, &handle, 0);
|
---|
| 91 | if (rc != EOK) {
|
---|
| 92 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
| 93 | return 2;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[8bd5dad] | 96 | rc = ext2_filesystem_init(&filesystem, handle);
|
---|
[d5e2763] | 97 | if (rc != EOK) {
|
---|
[8bd5dad] | 98 | printf(NAME ": Error initializing libext2.\n");
|
---|
[d5e2763] | 99 | return 3;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1d6f507] | 102 | rc = ext2_filesystem_check_sanity(&filesystem);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | printf(NAME ": Filesystem did not pass sanity check.\n");
|
---|
[d241aae] | 105 | if (strict_check) {
|
---|
| 106 | return 3;
|
---|
| 107 | }
|
---|
[1d6f507] | 108 | }
|
---|
| 109 |
|
---|
[c00e729] | 110 | print_superblock(filesystem.superblock);
|
---|
[d241aae] | 111 | print_block_groups(&filesystem);
|
---|
[d5e2763] | 112 |
|
---|
[8bd5dad] | 113 | ext2_filesystem_fini(&filesystem);
|
---|
[d5e2763] | 114 |
|
---|
| 115 | return 0;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | static void syntax_print(void)
|
---|
| 120 | {
|
---|
[c00e729] | 121 | printf("syntax: ext2info <device_name>\n");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static void print_superblock(ext2_superblock_t *superblock)
|
---|
| 125 | {
|
---|
| 126 | uint16_t magic;
|
---|
| 127 | uint32_t first_block;
|
---|
| 128 | uint32_t block_size;
|
---|
| 129 | uint32_t fragment_size;
|
---|
| 130 | uint32_t blocks_per_group;
|
---|
| 131 | uint32_t fragments_per_group;
|
---|
| 132 | uint32_t rev_major;
|
---|
| 133 | uint16_t rev_minor;
|
---|
| 134 | uint16_t state;
|
---|
| 135 | uint32_t first_inode;
|
---|
| 136 | uint16_t inode_size;
|
---|
[f6fa2c2] | 137 | uint32_t total_blocks;
|
---|
| 138 | uint32_t reserved_blocks;
|
---|
| 139 | uint32_t free_blocks;
|
---|
| 140 | uint32_t total_inodes;
|
---|
| 141 | uint32_t free_inodes;
|
---|
| 142 | uint32_t os;
|
---|
[c00e729] | 143 |
|
---|
| 144 | int pos;
|
---|
| 145 | unsigned char c;
|
---|
| 146 |
|
---|
| 147 | magic = ext2_superblock_get_magic(superblock);
|
---|
| 148 | first_block = ext2_superblock_get_first_block(superblock);
|
---|
| 149 | block_size = ext2_superblock_get_block_size(superblock);
|
---|
| 150 | fragment_size = ext2_superblock_get_fragment_size(superblock);
|
---|
| 151 | blocks_per_group = ext2_superblock_get_blocks_per_group(superblock);
|
---|
| 152 | fragments_per_group = ext2_superblock_get_fragments_per_group(superblock);
|
---|
| 153 | rev_major = ext2_superblock_get_rev_major(superblock);
|
---|
| 154 | rev_minor = ext2_superblock_get_rev_minor(superblock);
|
---|
| 155 | state = ext2_superblock_get_state(superblock);
|
---|
| 156 | first_inode = ext2_superblock_get_first_inode(superblock);
|
---|
| 157 | inode_size = ext2_superblock_get_inode_size(superblock);
|
---|
[f6fa2c2] | 158 | total_blocks = ext2_superblock_get_total_block_count(superblock);
|
---|
| 159 | reserved_blocks = ext2_superblock_get_reserved_block_count(superblock);
|
---|
| 160 | free_blocks = ext2_superblock_get_free_block_count(superblock);
|
---|
| 161 | total_inodes = ext2_superblock_get_total_inode_count(superblock);
|
---|
| 162 | free_inodes = ext2_superblock_get_free_inode_count(superblock);
|
---|
| 163 | os = ext2_superblock_get_os(superblock);
|
---|
[c00e729] | 164 |
|
---|
| 165 | printf("Superblock:\n");
|
---|
| 166 |
|
---|
| 167 | if (magic == EXT2_SUPERBLOCK_MAGIC) {
|
---|
| 168 | printf(" Magic value: %X (correct)\n", magic);
|
---|
| 169 | }
|
---|
| 170 | else {
|
---|
| 171 | printf(" Magic value: %X (incorrect)\n", magic);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | printf(" Revision: %u.%hu\n", rev_major, rev_minor);
|
---|
| 175 | printf(" State: %hu\n", state);
|
---|
[f6fa2c2] | 176 | printf(" Creator OS: %u\n", os);
|
---|
[c00e729] | 177 | printf(" First block: %u\n", first_block);
|
---|
| 178 | printf(" Block size: %u bytes (%u KiB)\n", block_size, block_size/1024);
|
---|
| 179 | printf(" Blocks per group: %u\n", blocks_per_group);
|
---|
[f6fa2c2] | 180 | printf(" Total blocks: %u\n", total_blocks);
|
---|
| 181 | printf(" Reserved blocks: %u\n", reserved_blocks);
|
---|
| 182 | printf(" Free blocks: %u\n", free_blocks);
|
---|
[c00e729] | 183 | printf(" Fragment size: %u bytes (%u KiB)\n", fragment_size,
|
---|
| 184 | fragment_size/1024);
|
---|
| 185 | printf(" Fragments per group: %u\n", fragments_per_group);
|
---|
| 186 | printf(" First inode: %u\n", first_inode);
|
---|
| 187 | printf(" Inode size: %hu bytes\n", inode_size);
|
---|
[f6fa2c2] | 188 | printf(" Total inodes: %u\n", total_inodes);
|
---|
| 189 | printf(" Free inodes: %u\n", free_inodes);
|
---|
[c00e729] | 190 |
|
---|
| 191 |
|
---|
[f6fa2c2] | 192 | if (rev_major == 1) {
|
---|
| 193 | printf(" UUID: ");
|
---|
| 194 | for (pos = 0; pos < 16; pos++) {
|
---|
| 195 | printf("%02x", superblock->uuid[pos]);
|
---|
[c00e729] | 196 | }
|
---|
[f6fa2c2] | 197 | printf("\n");
|
---|
| 198 |
|
---|
| 199 | printf(" Volume label: ");
|
---|
| 200 | for (pos = 0; pos < 16; pos++) {
|
---|
| 201 | c = superblock->volume_name[pos];
|
---|
| 202 | if (c >= 32 && c < 128) {
|
---|
| 203 | putchar(c);
|
---|
| 204 | }
|
---|
| 205 | else {
|
---|
| 206 | putchar(' ');
|
---|
| 207 | }
|
---|
[c00e729] | 208 | }
|
---|
[f6fa2c2] | 209 | printf("\n");
|
---|
[c00e729] | 210 | }
|
---|
| 211 |
|
---|
[d5e2763] | 212 | }
|
---|
| 213 |
|
---|
[d241aae] | 214 | void print_block_groups(ext2_filesystem_t *filesystem) {
|
---|
| 215 | uint32_t block_group_count;
|
---|
| 216 | uint32_t i;
|
---|
| 217 | ext2_block_group_ref_t *block_group_ref;
|
---|
| 218 | int rc;
|
---|
| 219 |
|
---|
| 220 | printf("Block groups:\n");
|
---|
| 221 |
|
---|
| 222 | block_group_count = ext2_superblock_get_block_group_count(
|
---|
| 223 | filesystem->superblock);
|
---|
| 224 |
|
---|
| 225 | for (i = 0; i < block_group_count; i++) {
|
---|
| 226 | printf(" Block group %u\n", i);
|
---|
| 227 | rc = ext2_filesystem_get_block_group_ref(filesystem, i, &block_group_ref);
|
---|
| 228 | if (rc != EOK) {
|
---|
| 229 | printf(" Failed reading block group\n");
|
---|
| 230 | continue;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | print_block_group(block_group_ref->block_group);
|
---|
| 234 |
|
---|
| 235 | rc = ext2_filesystem_put_block_group_ref(block_group_ref);
|
---|
| 236 | if (rc != EOK) {
|
---|
| 237 | printf(" Failed freeing block group\n");
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void print_block_group(ext2_block_group_t *bg) {
|
---|
| 244 | uint32_t block_bitmap_block;
|
---|
| 245 | uint32_t inode_bitmap_block;
|
---|
| 246 | uint32_t inode_table_first_block;
|
---|
| 247 | uint16_t free_block_count;
|
---|
| 248 | uint16_t free_inode_count;
|
---|
| 249 | uint16_t directory_inode_count;
|
---|
| 250 |
|
---|
| 251 | block_bitmap_block = ext2_block_group_get_block_bitmap_block(bg);
|
---|
| 252 | inode_bitmap_block = ext2_block_group_get_inode_bitmap_block(bg);
|
---|
| 253 | inode_table_first_block = ext2_block_group_get_inode_table_first_block(bg);
|
---|
| 254 | free_block_count = ext2_block_group_get_free_block_count(bg);
|
---|
| 255 | free_inode_count = ext2_block_group_get_free_inode_count(bg);
|
---|
| 256 | directory_inode_count = ext2_block_group_get_directory_inode_count(bg);
|
---|
| 257 |
|
---|
| 258 | printf(" Block bitmap block: %u\n", block_bitmap_block);
|
---|
| 259 | printf(" Inode bitmap block: %u\n", inode_bitmap_block);
|
---|
| 260 | printf(" Inode table's first block: %u\n", inode_table_first_block);
|
---|
| 261 | printf(" Free blocks: %u\n", free_block_count);
|
---|
| 262 | printf(" Free inodes: %u\n", free_inode_count);
|
---|
| 263 | printf(" Directory inodes: %u\n", directory_inode_count);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[d5e2763] | 266 | /**
|
---|
| 267 | * @}
|
---|
| 268 | */
|
---|