Maurus Cuelenaere | c8bdcbf | 2008-08-07 10:24:11 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by William Poetra Yoga Hadisoeseno |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdint.h> |
| 26 | #include <libgen.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <unistd.h> |
| 30 | #include <fcntl.h> |
| 31 | |
| 32 | void usage() |
| 33 | { |
| 34 | fprintf(stderr, "usage: IHFSsplit <ihfs_img> <output_dir>\n"); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | typedef struct { |
| 39 | uint32_t signature; |
| 40 | uint32_t fslen; |
| 41 | uint32_t unknown1; |
| 42 | uint32_t unknown2; |
| 43 | char timestamp[12]; |
| 44 | uint32_t numfiles; |
| 45 | char zeros[476]; |
| 46 | uint32_t marker; |
| 47 | } ihfs_header_t; |
| 48 | |
| 49 | #define MAX_FILES 2048 |
| 50 | #define MAX_IHFS_PATH 56 |
| 51 | |
| 52 | typedef struct { |
| 53 | struct { |
| 54 | char fullpath[MAX_IHFS_PATH]; |
| 55 | uint32_t sector; |
| 56 | uint32_t length; |
| 57 | } files[MAX_FILES]; |
| 58 | } ihfs_file_table_t; |
| 59 | |
| 60 | #define SECTOR_SIZE 512 |
| 61 | |
| 62 | int ihfs_sanity(const int ihfs_img) |
| 63 | { |
| 64 | struct stat statbuf; |
| 65 | ihfs_header_t ihfs_hdr; |
| 66 | |
| 67 | printf("starting sanity check for IHFS image...\n"); |
| 68 | |
| 69 | lseek(ihfs_img, 0, SEEK_SET); |
| 70 | read(ihfs_img, &ihfs_hdr, sizeof (ihfs_hdr)); |
| 71 | |
| 72 | printf(" checking for IHFS signature...\n"); |
| 73 | if (ihfs_hdr.signature != 0x49484653) |
| 74 | return 1; |
| 75 | |
| 76 | printf(" checking for FS length...\n"); |
| 77 | fstat(ihfs_img, &statbuf); |
| 78 | if (ihfs_hdr.fslen * SECTOR_SIZE != statbuf.st_size) |
| 79 | return 1; |
| 80 | |
| 81 | printf(" checking for unknown value 1...\n"); |
| 82 | if (ihfs_hdr.unknown1 != 0x00000004) |
| 83 | return 1; |
| 84 | |
| 85 | printf(" checking for unknown value 2...\n"); |
| 86 | if (ihfs_hdr.unknown2 != 0xfffff000) |
| 87 | return 1; |
| 88 | |
| 89 | printf(" checking for number of files...\n"); |
| 90 | if (ihfs_hdr.numfiles > MAX_FILES) |
| 91 | return 1; |
| 92 | |
| 93 | printf(" checking for marker...\n"); |
| 94 | if (ihfs_hdr.marker != 0x55aa55aa) |
| 95 | return 1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void mkdir_p(const char *path) |
| 101 | { |
| 102 | char *dir; |
| 103 | |
| 104 | dir = dirname(strdup(path)); |
| 105 | if (strchr(dir, '/')) |
| 106 | mkdir_p(dir); |
| 107 | |
Maurus Cuelenaere | aefbd63 | 2008-09-03 12:42:14 +0000 | [diff] [blame] | 108 | #ifdef _WIN32 |
| 109 | mkdir(dir); |
| 110 | #else |
Maurus Cuelenaere | c8bdcbf | 2008-08-07 10:24:11 +0000 | [diff] [blame] | 111 | mkdir(dir, 0755); |
Maurus Cuelenaere | aefbd63 | 2008-09-03 12:42:14 +0000 | [diff] [blame] | 112 | #endif |
Maurus Cuelenaere | c8bdcbf | 2008-08-07 10:24:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #define BUF_SIZE 4096 |
| 116 | |
| 117 | void outputfile(const char *outpath, const int ihfs_img, const int offset, const int length) |
| 118 | { |
| 119 | int outfd; |
| 120 | int i, rem; |
| 121 | char buf[BUF_SIZE]; |
| 122 | |
| 123 | lseek(ihfs_img, offset, SEEK_SET); |
| 124 | |
| 125 | outfd = creat(outpath, 0644); |
| 126 | |
| 127 | for (i = 0; i < length / BUF_SIZE; ++i) { |
| 128 | read(ihfs_img, buf, BUF_SIZE); |
| 129 | write(outfd, buf, BUF_SIZE); |
| 130 | } |
| 131 | rem = length - i * BUF_SIZE; |
| 132 | if (rem > 0) { |
| 133 | read(ihfs_img, buf, rem); |
| 134 | write(outfd, buf, rem); |
| 135 | } |
| 136 | |
| 137 | close(outfd); |
| 138 | } |
| 139 | |
| 140 | int main(int argc, char **argv) |
| 141 | { |
| 142 | struct stat statbuf; |
| 143 | int ihfs_img; |
| 144 | ihfs_header_t ihfs_hdr; |
| 145 | ihfs_file_table_t ihfs_ftbl; |
| 146 | int i, j; |
| 147 | char *outpath, *base_path, ihfs_path[MAX_IHFS_PATH+1]; |
| 148 | |
| 149 | /* check the arguments */ |
| 150 | |
| 151 | if (argc != 3) |
| 152 | usage(); |
| 153 | |
| 154 | stat(argv[1], &statbuf); |
| 155 | if (!S_ISREG(statbuf.st_mode)) |
| 156 | usage(); |
| 157 | |
| 158 | stat(argv[2], &statbuf); |
| 159 | if (!S_ISDIR(statbuf.st_mode)) |
| 160 | usage(); |
| 161 | |
| 162 | /* check the file, then split */ |
| 163 | |
| 164 | ihfs_img = open(argv[1], O_RDONLY); |
| 165 | |
| 166 | if (ihfs_sanity(ihfs_img)) { |
| 167 | printf("Non-IHFS format!\n"); |
| 168 | return 1; |
| 169 | } else |
| 170 | printf("sanity check OK\n"); |
| 171 | |
| 172 | lseek(ihfs_img, 0, SEEK_SET); |
| 173 | read(ihfs_img, &ihfs_hdr, sizeof (ihfs_hdr)); |
| 174 | lseek(ihfs_img, 4 * SECTOR_SIZE, SEEK_SET); |
| 175 | read(ihfs_img, &ihfs_ftbl, sizeof (ihfs_ftbl)); |
| 176 | |
| 177 | base_path = strdup(argv[2]); |
| 178 | outpath = malloc(strlen(base_path) + 1 + MAX_IHFS_PATH + 1); |
| 179 | for (i = 0; i < ihfs_hdr.numfiles; ++i) { |
| 180 | printf("\n"); |
| 181 | printf("pathname: %s\n", ihfs_ftbl.files[i].fullpath); |
| 182 | printf("starts at sector %d, length is %d bytes\n", ihfs_ftbl.files[i].sector, ihfs_ftbl.files[i].length); |
| 183 | |
| 184 | strncpy(ihfs_path, ihfs_ftbl.files[i].fullpath, MAX_IHFS_PATH); |
| 185 | ihfs_path[MAX_IHFS_PATH] = '\0'; |
| 186 | for (j = 0; j < strlen(ihfs_path); ++j) |
| 187 | if (ihfs_path[j] == '\\') |
| 188 | ihfs_path[j] = '/'; |
| 189 | |
| 190 | sprintf(outpath, "%s/%s", base_path, ihfs_path); |
| 191 | mkdir_p(outpath); |
| 192 | outputfile(outpath, ihfs_img, ihfs_ftbl.files[i].sector * SECTOR_SIZE, ihfs_ftbl.files[i].length); |
| 193 | } |
| 194 | free(outpath); |
| 195 | |
| 196 | close(ihfs_img); |
| 197 | |
| 198 | return 0; |
Maurus Cuelenaere | 72bc4e3 | 2008-08-12 16:55:59 +0000 | [diff] [blame] | 199 | } |