Bump version numbers for 3.13
[maemo-rb.git] / utils / jz4740_tools / IHFSsplit.c
blobad62d81daf4f7e38814b4b6aa06b8457289768eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by William Poetra Yoga Hadisoeseno
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
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>
32 void usage()
34 fprintf(stderr, "usage: IHFSsplit <ihfs_img> <output_dir>\n");
35 exit(1);
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;
49 #define MAX_FILES 2048
50 #define MAX_IHFS_PATH 56
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;
60 #define SECTOR_SIZE 512
62 int ihfs_sanity(const int ihfs_img)
64 struct stat statbuf;
65 ihfs_header_t ihfs_hdr;
67 printf("starting sanity check for IHFS image...\n");
69 lseek(ihfs_img, 0, SEEK_SET);
70 read(ihfs_img, &ihfs_hdr, sizeof (ihfs_hdr));
72 printf(" checking for IHFS signature...\n");
73 if (ihfs_hdr.signature != 0x49484653)
74 return 1;
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;
81 printf(" checking for unknown value 1...\n");
82 if (ihfs_hdr.unknown1 != 0x00000004)
83 return 1;
85 printf(" checking for unknown value 2...\n");
86 if (ihfs_hdr.unknown2 != 0xfffff000)
87 return 1;
89 printf(" checking for number of files...\n");
90 if (ihfs_hdr.numfiles > MAX_FILES)
91 return 1;
93 printf(" checking for marker...\n");
94 if (ihfs_hdr.marker != 0x55aa55aa)
95 return 1;
97 return 0;
100 void mkdir_p(const char *path)
102 char *dir;
104 dir = dirname(strdup(path));
105 if (strchr(dir, '/'))
106 mkdir_p(dir);
108 #ifdef _WIN32
109 mkdir(dir);
110 #else
111 mkdir(dir, 0755);
112 #endif
115 #define BUF_SIZE 4096
117 void outputfile(const char *outpath, const int ihfs_img, const int offset, const int length)
119 int outfd;
120 int i, rem;
121 char buf[BUF_SIZE];
123 lseek(ihfs_img, offset, SEEK_SET);
125 outfd = creat(outpath, 0644);
127 for (i = 0; i < length / BUF_SIZE; ++i) {
128 read(ihfs_img, buf, BUF_SIZE);
129 write(outfd, buf, BUF_SIZE);
131 rem = length - i * BUF_SIZE;
132 if (rem > 0) {
133 read(ihfs_img, buf, rem);
134 write(outfd, buf, rem);
137 close(outfd);
140 int main(int argc, char **argv)
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];
149 /* check the arguments */
151 if (argc != 3)
152 usage();
154 stat(argv[1], &statbuf);
155 if (!S_ISREG(statbuf.st_mode))
156 usage();
158 stat(argv[2], &statbuf);
159 if (!S_ISDIR(statbuf.st_mode))
160 usage();
162 /* check the file, then split */
164 ihfs_img = open(argv[1], O_RDONLY);
166 if (ihfs_sanity(ihfs_img)) {
167 printf("Non-IHFS format!\n");
168 return 1;
169 } else
170 printf("sanity check OK\n");
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));
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);
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] = '/';
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);
194 free(outpath);
196 close(ihfs_img);
198 return 0;