forked from hermanhermitage/videocore-disjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdis.html
395 lines (356 loc) · 12.6 KB
/
dis.html
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
//var remote_binary = 'https://raw.github.com/raspberrypi/firmware/blob/master/boot/bootcode.bin';
//var remote_arch = 'https://raw.github.com/hermanhermitage/videocoreiv/master/videocoreiv.arch';
var get = function(url, handler) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
// retrieve data unprocessed as a binary string
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onload = handler;
xhr.send();
}
var z32 = "00000000000000000000000000000000";
function as(i, base, width) {
var r = i.toString(base);
return z32.substring(32-width+r.length)+r;
}
function u8(a, i) {
return a.charCodeAt(i)&0xff;
}
function u16(a, i) {
return u8(a, i)|(u8(a, i+1)<<8);
}
function u32(a, i) {
//return u16(a, i)|((u16(a, i+2)<<16));
return (u16(a, i)|(u16(a, i+2) << 16))>>>0;
}
var logDiv;
var log = function(message){
logDiv = logDiv || document.getElementById('log');
if (logDiv) {
logDiv.innerHTML += message + '<br>';
}
}
var arch = {
instructions: [],
tables: {},
signed: {}
}
/*
get(remote_arch, function(){
var e = this.response;
log('Architecture file loaded.\n');
processArch(e);
for(var i=0; i<arch.instructions.length; i++){
var inst = arch.instructions[i];
if ((instPrepare(inst) & 7) != 0) {
log('Warning: instruction found that is not an exact multiple of bytes.');
}
}
get(remote_binary, function(){
log('Binary file loaded.\n');
var elem = document.getElementById('dump');
elem.innerHTML = "<pre>"+dis(this.response).join("\n")+"</pre>";
});
});
*/
function archDefineTable(def){
log('Table definition '+def);
var symbol = def[0];
var parts = def.split('[');
parts[1] = parts[1].split(']')[0];
var table = parts[1].replace(/\"/g, '').replace(/ /g,'').split(',');
parts[0] = parts[0].trim();//replace(/\ /g,'');
arch.tables[parts[0]] = table;
log('Table definition '+parts[0]+' is '+table.toString());
}
function archLookupTable(arch, symbol, index) {
return arch.tables[symbol][index];
}
function archDefineSigned(def){
log('Signed definition '+def);
arch.signed[def[0]] = true;
}
function archAddinst(pattern, action){
log('Added instruction '+pattern+' => '+action);
arch.instructions.push({pattern: pattern, action: action});
}
function instPrepare(inst) {
inst.bitlength = 0;
inst.masks=[]; inst.bits=[];
var bit = 8, bitvalue = 0, maskvalue = 0;
for(var i=0; i<inst.pattern.length; i++){
var c = inst.pattern[i];
if(c!=' ' && c!='\t') {
inst.bitlength++;
bitvalue = (bitvalue << 1) | (c=='1');
maskvalue = (maskvalue <<1) | (c=='0'||c=='1');
if (--bit == 0) {
inst.bits.push(bitvalue); inst.masks.push(maskvalue);
bitvalue = maskvalue = 0; bit = 8;
}
}
}
if (bit < 8) {
inst.bits.push(bitvalue << bit); inst.masks.push(maskvalue << bit);
}
inst.bytelength = (inst.bitlength+7)>>3;
return inst.bitlength;
}
function instMatch(inst, bytes) {
for(var i=0; i<inst.bytelength; i++) {
if ((bytes[i] & inst.masks[i]) != inst.bits[i])
return 0;
}
return 1;
}
//tmp hack
function is_signed(c) { return c=='i' || c=='o'; }
function instBind(inst, bytes) {
var bindings = {values:{}, lengths:{}};
var j=0, bit = 0, byte;
for (var i=0; i<inst.pattern.length; i++) {
var c=inst.pattern[i];
if (c!=' ' && c!='\t'){
bindings.values[c] = bindings.values[c] || 0;
bindings.lengths[c] = bindings.lengths[c] || 0;
byte = bit==0 ? bytes[j++] : byte;
if(bindings.lengths[c]==0 && is_signed(c) && (byte>>7))
bindings.values[c] = -1;
bindings.values[c] = (bindings.values[c]<<1) | (byte>>7);
bindings.lengths[c]++;
byte = (byte << 1)&0xff;
bit = (bit+1) & 7;
}
}
return bindings;
}
function is_printf_flag(c) { return (c=='+') || (c==' ') || (c=='#') || (c=='0'); }
function is_printf_digit(c) { return (c>='0') && (c<='9'); }
function is_printf_precision(c) { return (c=='.'); }
function is_printf_length(c) { return (c=='h') || (c=='l') || (c=='L') || (c=='z') || (c=='j') || (c=='t'); }
function instShow(inst, bindings, pc) {
var i;
var get_number = function() {
var n =0;
for (; is_printf_digit(inst.action[i]); i++)
n = n * 10 + inst.action.charCodeAt(i)-"0".charCodeAt(0);
return n;
}
var o=[];
var flags, width, precision, length, type;
for (var i=0; i<inst.action.length; i++) {
var c = inst.action[i];
if (c=='%') { //%[parameter][flags][width][.precision][length]type
flags = width = precision = length = type = null;
i++;
flags = is_printf_flag(inst.action[i]) ? inst.action[i++] : 0;
if (is_printf_digit(inst.action[i]))
width = get_number();
if (is_printf_precision(inst.action[i])) {
i++;
precision = get_number();
}
length = is_printf_length(inst.action[i]) ? inst.action[i++] : 0;
type = inst.action[i];
}
else if (c=='{') {
// evaluate expression up to }
// and then print according to fmt
var fmt="";
for (var j=++i;inst.action[i]!='}'; i++) {
/*
if(inst.action[i]>='a' && inst.action[i]<='z' || inst.action[i]=='$') {
//fmt += inst.action[i]+':'+bindings.values[inst.action[i]];
fmt += bindings.values[inst.action[i]];
} else {
fmt += inst.action[i];
}*/
}
fmt=inst.action.substring(j, i);
with(bindings.values) {
fmt=eval(fmt);
}
if (type=='x')
fmt=fmt>>>0;
o.push(fmt.toString(type=='x'?16:10));
}
else {
o.push(c);
}
}
return o.join('');
}
function archFindinst(arch, ws) {
for (var i=0; i<arch.instructions.length; i++) {
var inst = arch.instructions[i];
if (instMatch(inst, ws[inst.bytelength/2-1]))
return inst;
}
}
function ws(e, i) {
return [
[u8(e, i+1), u8(e, i+0)],
[u8(e, i+1), u8(e, i+0), u8(e, i+3), u8(e, i+2)],
[u8(e, i+1), u8(e, i+0), u8(e, i+5), u8(e, i+4), u8(e, i+3), u8(e, i+2)],
[u8(e, i+1), u8(e, i+0), u8(e, i+7), u8(e, i+6), u8(e, i+5), u8(e, i+4), u8(e, i+3), u8(e, i+2)],
[u8(e, i+1), u8(e, i+0), u8(e, i+9), u8(e, i+8), u8(e, i+7), u8(e, i+6), u8(e, i+5), u8(e, i+4), u8(e, i+3), u8(e, i+2)],
];
}
function processArch(arch){
var lines = arch.split('\n');
for (var i=0; i<lines.length; i++) {
var pattern;
for (var j=0; j<lines[i].length; j++) {
var c = lines[i][j];
if (c==' ' || c=='\t') {
continue;
}
else if (c=='#' || c=='\n' || c=='\r') {
break;
}
else if (lines[i].indexOf('(define-table', j)==j) {
archDefineTable(lines[i].substring(j+14));
break;
}
else if (lines[i].indexOf('(define-signed', j)==j) {
archDefineSigned(lines[i].substring(j+15));
break;
}
else if (c=='"') {
// Scan closing quote
var end = lines[i].indexOf('"', j+1);
if (end<0) {
log('Missing closing " on line '+(i+1)+'.');
break;
}
archAddinst(pattern, lines[i].substring(j+1, end));
break;
}
else {
var k=j;
for (; ; j++) {
c = lines[i][j];
if (c!=' ' && c!='\t' && (c<'a' || c>'z') && c!='0' && c!='1' && c!='!' && c!='?')
break;
}
pattern = lines[i].substring(k, j);
//log('pattern is '+pattern+' '+k+'..'+j);
j--;
}
}
}
}
function dc(c) {
return (c>=32 && c<=126) ? String.fromCharCode(c) : '.';
}
function dis2(arch, e, i, d){
var l=[];
l.push(as(i, 16, 8)+": "+as(u16(e, i), 2, 16)+": ");
var bytes = ws(e, i);
var inst = archFindinst(arch, bytes);
if (inst) {
for(var j=0; j<10; j++)
if (j<inst.bytelength)
l.push(dc(u8(e, i+j)));
else
l.push(' ');
l.push(" :");
for (var j=0; j<5; j++)
if (j<inst.bytelength/2)
l.push(as(u16(e, i+2*j), 16, 4)+' ');
else
l.push(' ');
var bindings = instBind(inst, bytes[inst.bytelength/2-1]);
for (var j=32; j<128; j++) {
var c = String.fromCharCode(j);
if (bindings.lengths[c]>0 && arch.tables[c]) {
bindings.values[c] = archLookupTable(arch, c, bindings.values[c]);
}
bindings.values['$'] = i;
}
l.push(instShow(inst, bindings, i));
}
d.push(l.join(''));
return inst.bytelength;
return inst.bytelength;
}
function dis(memory){
var d = [];
for (var a=0; a<memory.length; ){
a+=dis2(arch, memory, a, d);
}
return d;
}
var binary;
var architecture;
function loadBinary(jsonp) {
console.log('Binary loaded');
binary = window.atob(jsonp.data.content.replace(/\s/g,''));
log('Binary file loaded.\n');
var elem = document.getElementById('dump');
elem.innerText = dis(binary).join("\n");
}
function loadArchitecture(jsonp) {
console.log('Architecture loaded');
architecture = window.atob(jsonp.data.content.replace(/\s/g,''));
var e = architecture;
log('Architecture file loaded.\n');
processArch(e);
for(var i=0; i<arch.instructions.length; i++){
var inst = arch.instructions[i];
if ((instPrepare(inst) & 7) != 0) {
log('Warning: instruction found that is not an exact multiple of bytes.');
}
}
}
function handleFiles(files){
for (var i=0; i<files.length; i++)
var file = files[i];
var reader = new FileReader();
reader.onload = function(e) {
var contents = this.result;
document.getElementById('dump').innerText = dis(contents).join('\n');
}
reader.readAsBinaryString(file);
}
function loadScript(src){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= src;
head.appendChild(script);
}
function loadGithubJsonp(fileGithubPath, callbackFunctionName){
loadScript('https://api.github.com/repos/'+fileGithubPath+'?callback='+callbackFunctionName)
}
</script>
<!--
Instruction: https://api.github.com/repos/ <input type="text" size="60" name="archUrl" value="hermanhermitage/videocoreiv/contents/videocoreiv.arch"/> or <input type="file" id="input" onchange="handleFiles(this.files)"> <br/>
-->
<b>RaspberryPi VideoCore IV Disassembler</b> by <a href='mailto:[email protected]'>Herman Hermitage</a>.<br>
A simple disassembler for the Broadcom Cpu in the RaspberryPi.<br>
<br>
<b>Binary:</b><br>
<input type="submit" value="Load" onclick='loadGithubJsonp("raspberrypi/firmware/contents/boot/bootcode.bin", "loadBinary")'/> https://api.github.com/repos/ <input type="text" size="60" name="binaryUrl" value="raspberrypi/firmware/contents/boot/bootcode.bin"/>, or <input value="Local File" type="file" id="input" onchange="handleFiles(this.files)">
<script>
</script>
<div><pre id='file'></pre></div>
<b>Disassembly</b>
<div><textarea id='dump' style='font-family: monospace' rows=24 cols=132></textarea></div>
<b>Log</b>
<div id='log'></div>
<script src='https://api.github.com/repos/hermanhermitage/videocoreiv/contents/videocoreiv.arch?callback=loadArchitecture'>
</script>
<!--<script src='https://api.github.com/repos/raspberrypi/firmware/git/blobs/d6bbd12c3becb0517819c5afe138cffb62ad7d95?callback=loadBinary'>-->
<!--
<script src='https://api.github.com/repos/raspberrypi/firmware/contents/boot/bootcode.bin?callback=loadBinary'>
</script>
-->
</body>
</html>