-
Notifications
You must be signed in to change notification settings - Fork 1
/
PocketBookUpdate.cpp
185 lines (165 loc) · 4.34 KB
/
PocketBookUpdate.cpp
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
#include "PocketBookUpdate.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
#include <sys/stat.h>
string convertInt(int number)
{
if( number == 0) return "";
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
PocketBookUpdate::PocketBookUpdate(const char*filename) : mFilename(filename)
{
// allocate buffer
int size_of_header = sizeof(PocketBookUpdateHeader);
buffer = new char[size_of_header];
// read buffer
fstream file;
file.open(filename);
file.read(buffer, size_of_header);
file.close();
// interpret buffer as header
header = reinterpret_cast<PocketBookUpdateHeader*>(buffer);
partsNames[SWUPDATE_TAR_GZ] = "swupdate.tar.gz";
partsNames[ELF_IMG] = "elf.img";
partsNames[APP_IMG] = "app.img";
partsNames[ROOTFS_IMG] = "rootfs.img";
partsNames[A_IMG] = "a.img";
for(int i = 0; i < sizeof(header->fwParts)/sizeof(header->fwParts[0]); ++i)
{
if(0 == header->fwParts[i].size ) continue;
string name;
// printName
map<PartitionType, const char*>::const_iterator partNameIter =
partsNames.find(static_cast<PartitionType>(header->fwParts[i].type));
if(partNameIter != partsNames.end())
{
name = partNameIter->second;
}
else
{
name = "unknownImg";
}
int nameCounter = 0;
string tempName;
map<string, FWPart>::iterator it = parts.end();
do {
tempName = name + convertInt(nameCounter);
it = parts.find(string(tempName));
nameCounter++;
}
while(it != parts.end());
name = tempName;
pair<map<string, FWPart>::iterator,bool> index =
parts.insert(pair<string, FWPart>(name, header->fwParts[i]));
index.first->second.offset += sizeof(*header);
}
}
PocketBookUpdate::~PocketBookUpdate()
{
delete[] buffer;
}
void PocketBookUpdate::extract(const char * outputDir)
{
struct stat st;
if(stat(outputDir,&st) != 0)
{
cout << "Directory " << outputDir << " doesn't exist" << endl;
return;
}
ifstream infile(mFilename.c_str(), ifstream::binary);
map<string, FWPart>::const_iterator it;
for ( it=parts.begin(); it != parts.end(); it++ )
{
infile.seekg(it->second.offset);
string outputFilename = string(outputDir) + string("/") + it->first;
ofstream outfile(outputFilename.c_str(), ofstream::binary);
long unsigned int size = it->second.size;
char*buf = new char[size];
infile.read(buf, size);
outfile.write(buf, size);
delete[] buf;
outfile.close();
}
infile.close();
}
int PocketBookUpdate::printField(unsigned offset, unsigned size, const char* name)
{
cout.width(10);
cout << showbase << hex;
cout << offset << " ";
cout.width(10);
cout << offset+size << "\t\t" << name << ":\t\t";
cout << endl;
}
int PocketBookUpdate::printField(unsigned i)
{
static const int fieldsNumber = 7;
if( i >= fieldsNumber ) return 0;
static const int sizes[] = {
sizeof(header->magic),
sizeof(header->model),
sizeof(header->unknownBuffer1),
sizeof(header->padding1),
sizeof(header->unknownUInt32),
sizeof(header->padding2),
sizeof(header->fwParts)
};
const char* fieldNames[] = {
"Magic",
"Model",
"Unknown buffer 1",
"Padding1",
"UnknownUInt32",
"Padding2",
"Partition table"
};
static int offset = 0;
printField(offset, sizes[i], fieldNames[i]);
offset += sizes[i];
return sizes[i];
}
void PocketBookUpdate::print()
{
cout << "Header part: " << endl;
unsigned int offset = 0;
unsigned int size = 0;
size = printField(0);
const char * tab = "\t\t\t\t\t\t\t";
cout << tab;
cout.write(header->magic, size);
cout << endl;
size = printField(1);
cout << tab;
cout.write(header->model, size);
cout << endl;
size = printField(2);
cout << tab;
cout << endl;
size = printField(3);
cout << tab;
cout << endl;
size = printField(4);
cout << tab;
cout << header->unknownUInt32;
cout << endl;
size = printField(5);
cout << tab;
cout << endl;
size = printField(6);
cout << tab;
cout << endl;
cout << "Firmware partitions: " << endl;
printParts();
}
int PocketBookUpdate::printParts()
{
map<string, FWPart>::const_iterator it;
for ( it=parts.begin() ; it != parts.end(); it++ )
{
printField(it->second.offset, it->second.size, it->first.c_str());
}
}