1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <getpot/GetPot>
26 static const char VERSION
[] = "0.1";
32 << "firmware_extract - Extracts files from a Creative firmware."
34 << "Version " << VERSION
<< std::endl
35 << "Copyright (c) 2007 Rasmus Ry" << std::endl
;
42 << "Usage: firmware_extract [command] [options]" << std::endl
44 << " Commands:" << std::endl
45 << " -h,--help" << std::endl
46 << " prints this message." << std::endl
47 << " -f,--firmware [file]" << std::endl
48 << " specifies the firmware arhive file name." << std::endl
50 << " Options:" << std::endl
51 << " -V,--verbose" << std::endl
52 << " prints verbose messages." << std::endl
53 << " -p,--prefix [prefix]" << std::endl
54 << " specifies a file name prefix for the extracted files." << std::endl
60 struct save_entry_functor
62 save_entry_functor(const std::string
& fileprefix
)
63 : _fileprefix(fileprefix
) {}
65 bool operator()(const zen::firmware_entry
& entry
)
67 std::string filename
= _fileprefix
+ entry
.get_content_name();
69 ofs
.open(filename
.c_str(), std::ios::binary
);
73 size_t off
= entry
.get_content_offset();
74 std::streamsize size
= entry
.get_bytes().size() - off
;
75 ofs
.write((const char*)&entry
.get_bytes()[off
], size
);
80 const std::string
& _fileprefix
;
81 }; //struct save_entry_functor
83 struct print_entry_functor
85 print_entry_functor(std::ostream
& os
, const std::string
& fileprefix
)
86 : _os(os
), _fileprefix(fileprefix
), num(0) {}
88 bool operator()(const zen::firmware_entry
& entry
)
90 std::string filename
= _fileprefix
+ entry
.get_content_name();
92 _os
<< "[./" << num
++ << "]" << std::endl
;
94 _os
<< "[../" << num
++ << "]" << std::endl
;
95 _os
<< "tag = " << entry
.get_name() << std::endl
;
97 if (entry
.get_content_offset())
98 _os
<< "name = " << entry
.get_content_name() << std::endl
;
100 _os
<< "file = \'" << shared::double_quote(filename
) << "\'"
107 const std::string
& _fileprefix
;
109 }; //struct print_entry_functor
112 int process_arguments(int argc
, char* argv
[])
114 //--------------------------------------------------------------------
115 // Parse input variables.
116 //--------------------------------------------------------------------
118 GetPot
cl(argc
, argv
);
119 if (cl
.size() == 1 || cl
.search(2, "-h", "--help"))
125 std::string firmwarename
;
126 if (cl
.search("-f") || cl
.search("--firmware"))
127 firmwarename
= cl
.next("");
128 if (firmwarename
.empty())
130 std::cerr
<< "Firmware archive must be specified." << std::endl
;
134 bool verbose
= false;
135 if (cl
.search("-V") || cl
.search("--verbose"))
138 std::string prefixname
= shared::remove_extension(firmwarename
) + "_";
139 if (cl
.search("-p") || cl
.search("--prefix"))
140 prefixname
= cl
.next(prefixname
.c_str());
143 //--------------------------------------------------------------------
144 // Read the firmware archive.
145 //--------------------------------------------------------------------
148 std::cout
<< "[*] Reading firmware archive..." << std::endl
;
150 zen::firmware_archive
archive(false);
152 ifs
.open(firmwarename
.c_str(), std::ios::binary
);
155 std::cerr
<< "Failed to open the firmware archive." << std::endl
;
159 if (!archive
.read(ifs
))
161 std::cerr
<< "Failed to read the firmware archive." << std::endl
;
166 //--------------------------------------------------------------------
167 // Generate a make file for the extracted firmware archive.
168 //--------------------------------------------------------------------
170 // Get make filename for the given input file.
171 std::string makefile
= shared::replace_extension(firmwarename
, ".mk");
174 std::cout
<< "[*] Producing make file..." << std::endl
;
177 // Produce make file for the given input file.
179 ofs
.open(makefile
.c_str(), std::ios::binary
);
182 std::cerr
<< "Failed to create firmware archive make file."
187 time_t timeval
= time(NULL
);
188 ofs
<< "# Make file generated at: " << ctime(&timeval
);
189 ofs
<< "endian = " << (archive
.is_big_endian() ? "big" : "little")
191 ofs
<< "signed = " << (archive
.is_signed() ? "true" : "false")
194 ofs
<< "[children]" << std::endl
;
195 ofs
<< "count = " << archive
.get_children().size() << std::endl
;
197 std::for_each(archive
.get_children().begin(),
198 archive
.get_children().end(),
199 print_entry_functor(ofs
, prefixname
));
201 ofs
<< "[neighbours]" << std::endl
;
202 ofs
<< "count = " << archive
.get_neighbours().size() << std::endl
;
203 std::for_each(archive
.get_neighbours().begin(),
204 archive
.get_neighbours().end(),
205 print_entry_functor(ofs
, prefixname
));
208 //--------------------------------------------------------------------
209 // Save firmware entries.
210 //--------------------------------------------------------------------
213 std::cout
<< "[*] Saving firmware entries..." << std::endl
;
215 std::for_each(archive
.get_children().begin(),
216 archive
.get_children().end(),
217 save_entry_functor(prefixname
));
219 std::for_each(archive
.get_neighbours().begin(),
220 archive
.get_neighbours().end(),
221 save_entry_functor(prefixname
));
226 int main(int argc
, char* argv
[])
230 return process_arguments(argc
, argv
);
232 catch (const std::exception
& xcpt
)
234 std::cerr
<< "Exception caught: " << xcpt
.what() << std::endl
;
239 std::cerr
<< "Unknown exception caught." << std::endl
;