summaryrefslogtreecommitdiffstats
path: root/utils/zenutils/source/firmware_extract/main.cpp
blob: 1f31cc86ebb7b8f5dd3614e795679108306d646d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* zenutils - Utilities for working with creative firmwares.
 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <iostream>
#include <ctime>
#include <getpot/GetPot>
#include <utils.h>
#include <firmware.h>


static const char VERSION[] = "0.1";


void print_version()
{
    std::cout
        << "firmware_extract - Extracts files from a Creative firmware."
        << std::endl
        << "Version " << VERSION << std::endl
        << "Copyright (c) 2007 Rasmus Ry" << std::endl;
}

void print_help()
{
    print_version();
    std::cout
        << "Usage: firmware_extract [command] [options]" << std::endl
        << std::endl
        << " Commands:" << std::endl
        << "  -h,--help" << std::endl
        << "      prints this message." << std::endl
        << "  -f,--firmware [file]" << std::endl
        << "      specifies the firmware arhive file name." << std::endl
        << std::endl
        << " Options:" << std::endl
        << "  -V,--verbose" << std::endl
        << "      prints verbose messages." << std::endl
        << "  -p,--prefix [prefix]" << std::endl
        << "      specifies a file name prefix for the extracted files." << std::endl
        << std::endl
        ;
}


struct save_entry_functor
{
    save_entry_functor(const std::string& fileprefix)
        : _fileprefix(fileprefix) {}

    bool operator()(const zen::firmware_entry& entry)
    {
        std::string filename = _fileprefix + entry.get_content_name();
        std::ofstream ofs;
        ofs.open(filename.c_str(), std::ios::binary);
        if (!ofs)
            false;

        size_t off = entry.get_content_offset();
        std::streamsize size = entry.get_bytes().size() - off;
        ofs.write((const char*)&entry.get_bytes()[off], size);

        return ofs.good();
    }

    const std::string& _fileprefix;
}; //struct save_entry_functor

struct print_entry_functor
{
    print_entry_functor(std::ostream& os, const std::string& fileprefix)
        : _os(os), _fileprefix(fileprefix), num(0) {}

    bool operator()(const zen::firmware_entry& entry)
    {
        std::string filename = _fileprefix + entry.get_content_name();
        if (!num)
            _os << "[./" << num++ << "]" << std::endl;
        else
            _os << "[../" << num++ << "]" << std::endl;
        _os << "tag = " << entry.get_name() << std::endl;

        if (entry.get_content_offset())
            _os << "name = " << entry.get_content_name() << std::endl;

        _os << "file = \'" << shared::double_quote(filename) << "\'"
            << std::endl;

        return _os.good();
    }

    std::ostream& _os;
    const std::string& _fileprefix;
    int num;
}; //struct print_entry_functor


int process_arguments(int argc, char* argv[])
{
    //--------------------------------------------------------------------
    // Parse input variables.
    //--------------------------------------------------------------------

    GetPot cl(argc, argv);
    if (cl.size() == 1 || cl.search(2, "-h", "--help"))
    {
        print_help();
        return 1;
    }

    std::string firmwarename;
    if (cl.search("-f") || cl.search("--firmware"))
        firmwarename = cl.next("");
    if (firmwarename.empty())
    {
        std::cerr << "Firmware archive must be specified." << std::endl;
        return 2;
    }

    bool verbose = false;
    if (cl.search("-V") || cl.search("--verbose"))
        verbose = true;

    std::string prefixname = shared::remove_extension(firmwarename) + "_";
    if (cl.search("-p") || cl.search("--prefix"))
        prefixname = cl.next(prefixname.c_str());


    //--------------------------------------------------------------------
    // Read the firmware archive.
    //--------------------------------------------------------------------

    if (verbose)
        std::cout << "[*] Reading firmware archive..." << std::endl;

    zen::firmware_archive archive(false);
    std::ifstream ifs;
    ifs.open(firmwarename.c_str(), std::ios::binary);
    if (!ifs)
    {
        std::cerr << "Failed to open the firmware archive." << std::endl;
        return 3;
    }

    if (!archive.read(ifs))
    {
        std::cerr << "Failed to read the firmware archive." << std::endl;
        return 4;
    }


    //--------------------------------------------------------------------
    // Generate a make file for the extracted firmware archive.
    //--------------------------------------------------------------------

    // Get make filename for the given input file.
    std::string makefile = shared::replace_extension(firmwarename, ".mk");

    if (verbose)
        std::cout << "[*] Producing make file..." << std::endl;


    // Produce make file for the given input file.
    std::ofstream ofs;
    ofs.open(makefile.c_str(), std::ios::binary);
    if (!ofs)
    {
        std::cerr << "Failed to create firmware archive make file."
                  << std::endl;
        return 5;
    }

    time_t timeval = time(NULL);
    ofs << "# Make file generated at: " << ctime(&timeval);
    ofs << "endian = " << (archive.is_big_endian() ? "big" : "little")
        << std::endl;
    ofs << "signed = " << (archive.is_signed() ? "true" : "false")
        << std::endl;

    ofs << "[children]" << std::endl;
    ofs << "count = " << archive.get_children().size() << std::endl;

    std::for_each(archive.get_children().begin(),
                  archive.get_children().end(),
                  print_entry_functor(ofs, prefixname));

    ofs << "[neighbours]" << std::endl;
    ofs << "count = " << archive.get_neighbours().size() << std::endl;
    std::for_each(archive.get_neighbours().begin(),
                  archive.get_neighbours().end(),
                  print_entry_functor(ofs, prefixname));


    //--------------------------------------------------------------------
    // Save firmware entries.
    //--------------------------------------------------------------------

    if (verbose)
        std::cout << "[*] Saving firmware entries..." << std::endl;

    std::for_each(archive.get_children().begin(),
                  archive.get_children().end(),
                  save_entry_functor(prefixname));

    std::for_each(archive.get_neighbours().begin(),
                  archive.get_neighbours().end(),
                  save_entry_functor(prefixname));

    return 0;
}

int main(int argc, char* argv[])
{
    try
    {
        return process_arguments(argc, argv);
    }
    catch (const std::exception& xcpt)
    {
        std::cerr << "Exception caught: " << xcpt.what() << std::endl;
        return -1;
    }
    catch (...)
    {
        std::cerr << "Unknown exception caught." << std::endl;
        return -2;
    }
    return -3;
}