-
Notifications
You must be signed in to change notification settings - Fork 0
/
otra.js
executable file
·364 lines (314 loc) · 11.8 KB
/
otra.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env node
const fs = require("fs");
const path = require('node:path');
const Otra = require("./otra_reader/Otra");
const OtraSpl = require("./otra_reader/OtraSpl");
const OtraSirius = require("./otra_reader/OtraSirius");
const KaitaiStream = require('kaitai-struct/KaitaiStream');
const lzo = require('lzo');
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers');
const { parse } = require("path");
const { parsed } = require("yargs");
yargs(hideBin(process.argv))
.command('extract <image> [folder]', 'extract the OTRA image', (yargs) => {
return yargs
.positional('image', {
describe: 'image to process'
})
.positional('folder', {
describe: 'folder to extract to'
})
}, (argv) => {
const fileContent = fs.readFileSync(argv.image);
const type = getOtraType(fileContent);
if(!argv.folder) {
argv.folder = "./"+path.basename(argv.image).replace(/.img$/, '');
}
if(!fs.existsSync(argv.folder))
fs.mkdirSync(argv.folder)
if(type === "normal") {
const parsedOtra = new Otra(new KaitaiStream(fileContent));
if(parsedOtra.header.headerVersion > 1) {
parsedOtra.partInfo0 = mapPartsToSegments(parsedOtra.partInfo0, parsedOtra.segmentInfo0, parsedOtra.segments0);
parsedOtra.partInfo1 = mapPartsToSegments(parsedOtra.partInfo1, parsedOtra.segmentInfo1, parsedOtra.segments1);
}
console.log(`extracting image file: ${argv.image}`);
function writeFile(file, data, decompressedSize, append) {
console.log("writing: "+argv.folder+"/"+file + (append ? " +" : ""))
const buffer = Buffer.from(data)
fs.writeFileSync(argv.folder+"/"+file, (decompressedSize && parsedOtra.header.compressed ? lzo.decompress(buffer, decompressedSize) : buffer),
append ? {
flag: "a+"
} : null)
}
if(parsedOtra.header.romSize > 0) {
writeFile("rom.img", parsedOtra.rom)
}
if(parsedOtra.header.loaderSize > 0) {
writeFile("loader.img", parsedOtra.loader)
}
if(parsedOtra.header.envSize > 0) {
writeFile("env.img", parsedOtra.env)
}
function writePart(part) {
part.segments.forEach((segment, index) => {
writeFile(part.name+".img", segment.data.body, segment.decompressedSize, (index !== 0))
})
}
if(parsedOtra.header.headerVersion > 1) {
parsedOtra.partInfo0.forEach(writePart);
parsedOtra.partInfo1.forEach(writePart);
}
/*parsedOtra.segmentInfo0.forEach((segment, index) => {
const partitions = parsedOtra.partInfo0.filter((part)=>{ return part.flashOffset === segment.flashOffset}).map((part) => { return part.name })
const partition = partitions.length ? partitions.pop() : segment.flashOffset
writeFile(partition+".bin", parsedOtra.segments0[index].body, segment.decompressedSize)
})
*/
}
else if(type === "spl") {
function writeFile(file, data) {
console.log("writing: "+argv.folder+"/"+file)
const buffer = Buffer.from(data)
fs.writeFileSync(argv.folder+"/"+file, buffer);
}
const parsedOtra = new OtraSpl(new KaitaiStream(fileContent));
if(parsedOtra.header.splLen > 0) {
writeFile("spl.img", parsedOtra.spl);
}
if(parsedOtra.header.trootLen > 0) {
writeFile("troot.img", parsedOtra.troot);
}
if(parsedOtra.header.signatureLen > 0) {
writeFile("signature.img", parsedOtra.signature);
}
}
else if(type === "sirius") {
function writeFile(file, data) {
console.log("writing: "+argv.folder+"/"+file)
const buffer = Buffer.from(data)
fs.writeFileSync(argv.folder+"/"+file, buffer);
}
const parsedOtra = new OtraSirius(new KaitaiStream(fileContent));
if(parsedOtra.header.imgLen > 0) {
writeFile("rom.img", parsedOtra.image);
}
}
else {
throw "Unknown image type";
}
})
.command('info <image>', 'show info on the OTRA image', (yargs) => {
return yargs
.positional('image', {
describe: 'image to process'
})
}, (argv) => {
const fileContent = fs.readFileSync(argv.image);
const type = getOtraType(fileContent);
if(type === "normal") {
const parsedOtra = new Otra(new KaitaiStream(fileContent));
if(parsedOtra.header.headerVersion > 1) {
parsedOtra.partInfo0 = mapPartsToSegments(parsedOtra.partInfo0, parsedOtra.segmentInfo0, parsedOtra.segments0);
parsedOtra.partInfo1 = mapPartsToSegments(parsedOtra.partInfo1, parsedOtra.segmentInfo1, parsedOtra.segments1);
}
console.log(`image file: ${argv.image}
header version: ${parsedOtra.header.headerVersion}
hash size:${parsedOtra.header.hashSize}
signature size:${parsedOtra.header.sigSize}
hash: ${Buffer.from(parsedOtra.header.hash).toString('hex')}
signature: ${Buffer.from(parsedOtra.header.signature).toString('hex')}
image size:${parsedOtra.header.imgSize}
rom size:${parsedOtra.header.romSize}
loader size:${parsedOtra.header.loaderSize}
env size:${parsedOtra.header.envSize}
version size:${parsedOtra.header.headerVersionSize}
`);
if(parsedOtra.header.headerVersion > 3) {
console.log(`sdk version: ${parsedOtra.headerExt.sdkVersion}`)
}
if(parsedOtra.header.headerVersion > 1) {
console.log(`compressed:${parsedOtra.header.compressed}
partitions0 count:${parsedOtra.header.partitions0}
partitions1 count:${parsedOtra.header.partitions1}
segmenst0 count:${parsedOtra.header.segments0}
segments1 count:${parsedOtra.header.segments1}
partitions0:`);
console.table(parsedOtra.partInfo0.map((part)=>{
return {
name: part.name,
offset: part.flashOffset,
size: humanFileSize(part.length),
upgrade: Boolean(part.isUpgrade),
segments: part.segments.length
}
}))
if(parsedOtra.header.partitions1) {
console.log("partitions1:")
console.table(parsedOtra.partInfo1.map((part)=>{
return {
name: part.name,
offset: part.flashOffset,
size: humanFileSize(part.length),
upgrade: Boolean(part.isUpgrade),
segments: part.segments.length
}
}))
}
console.log("segments0:");
console.table(parsedOtra.segmentInfo0.map((segment)=>{
return {
imgOffset: segment.imgOffset,
flashOffset: segment.flashOffset,
compressedSize: humanFileSize(segment.compressedSize),
decompressedSize: humanFileSize(segment.decompressedSize),
partition: parsedOtra.partInfo0.filter((part)=>{
return segment.flashOffset >= part.flashOffset && segment.flashOffset < part.flashOffset + part.length
}).map((part) => { return part.name }).pop()
}
}))
if(parsedOtra.header.segments1) {
console.log("segments1:")
console.table(parsedOtra.segmentInfo1.map((segment)=>{
return {
imgOffset: segment.imgOffset,
flashOffset: segment.flashOffset,
compressedSize: humanFileSize(segment.compressedSize),
decompressedSize: humanFileSize(segment.decompressedSize),
partition: parsedOtra.partInfo1.filter((part)=>{
return segment.flashOffset >= part.flashOffset && segment.flashOffset < part.flashOffset + part.length
}).map((part) => { return part.name })
}
}))
}
}
}
else if(type === "spl") {
const parsedOtra = new OtraSpl(new KaitaiStream(fileContent));
console.log(`spl image file: ${argv.image}
version: ${parsedOtra.header.version}
image type: ${parsedOtra.header.imgType}
checksum: 0x${parsedOtra.header.checksum.toString(16)}
spl load address: 0x${parsedOtra.header.splLoadAddr.toString(16)}
spl length: ${parsedOtra.header.splLen}
troot load address: 0x${parsedOtra.header.trootLoadAddr.toString(16)}
troot length: ${parsedOtra.header.trootLen}
signature load address: 0x${parsedOtra.header.signatureLoadAddr.toString(16)}
signature length: ${parsedOtra.header.signatureLen}
`);
}
else if(type === "sirius") {
const parsedOtra = new OtraSirius(new KaitaiStream(fileContent));
const ourCheck = checksum(parsedOtra.image);
if(ourCheck !== parsedOtra.header.checksum) {
console.log("checksum mismatch! "+ourCheck.toString(16)+"!=="+parsedOtra.header.checksum.toString(16))
}
console.log(`sirius image file: ${argv.image}
version: ${parsedOtra.header.version}
image type: ${parsedOtra.header.imgType}
checksum: 0x${parsedOtra.header.checksum.toString(16)}
hash length: ${parsedOtra.header.hashSize}
signature length: ${parsedOtra.header.sigLen}
hash: ${parsedOtra.hash.toString(16)}
signature: ${parsedOtra.sig.toString(16)}
load address: 0x${parsedOtra.header.imgLoadAddr.toString(16)}
length: ${parsedOtra.header.imgLen}
`);
}
else {
throw "Unknown image type";
}
})
.command('pack <type> <loadaddress> <input> <output>', 'pack a sirius OTRA image', (yargs) => {
return yargs
.positional('type', {
describe: 'must be "sirius"'
})
.positional('loadaddress', {
describe: 'image load address"'
})
.positional('input', {
describe: 'input raw image'
})
.positional('output', {
describe: 'output sirius OTRA image'
})
}, (argv) => {
if(argv.type !== "sirius") {
throw "unsupported image type"
}
const image = fs.readFileSync(argv.input);
const sum = checksum(image);
const otra = Buffer.concat([Buffer.from("OTRA", "ascii"), Buffer.alloc(60), image])
otra.writeUint32LE(parseInt(argv.loadaddress), 0xc);
otra.writeUint32LE(image.length, 0x10);
otra.writeUint32LE(sum, 0x20);
fs.writeFileSync(argv.output, otra)
})
.strictCommands()
.demandCommand(1)
.parse()
function isZero(buf, start, len) {
const target = buf.slice(start, start+len)
for (var i = 0; i < len; i++) {
if (target.readUInt8(i) !==0) {
return false;
}
}
return true;
}
function getOtraType(buf) {
const magic = buf.toString("ascii", 0, 4);
if(magic !== "OTRA") {
return "unknown";
}
if(isZero(buf, 4, 8) && !isZero(buf, 36, 4) && isZero(buf, 40, 4*6)) {
return "spl"
}
else if(isZero(buf, 4, 8) && !isZero(buf, 32, 4) && isZero(buf, 36, 4*7)) {
return "sirius"
}
else {
return "normal"
}
}
// i don't think this can be trivially done with kaitai but would be happy to be proven wrong
function mapPartsToSegments(parts, segments, datas) {
segments = segments.map((segment, index)=>{ return {...segment, data:datas[index]} })
return parts.map((part)=>{
return {
...part,
segments: segments.filter((segment)=>{
return segment.flashOffset >= part.flashOffset && segment.flashOffset < part.flashOffset + part.length
})
}
})
}
function humanFileSize(bytes, si=false, dp=1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
}
function checksum(data) {
//just sum up all the bytes
const buf = Buffer.from(data);
let result = 0;
const len = buf.length;
for (let i = 0; i < len; i++) {
result += buf.readUInt8(i);
result = result & 0xffffffff;
}
return result;
}