-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdis.html
466 lines (428 loc) · 16.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<!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){
// def: T [ "i0", "i1", ..., "in" ]
def = def.slice(0,-1);
log('Table definition '+def);
// Table name is the first char
var symbol = def[0];
// Get array of quoted string elements
var quoted_items = def.match(/"[^"]*"/g);
// Strip the quotes off each entry
var table = quoted_items.map(function(x){return x.substring(1,x.length-1)});
arch.tables[symbol] = table;
log('Table definition '+symbol+' is '+table.toString());
}
function archLookupTable(arch, symbol, index) {
return arch.tables[symbol][index];
}
function archDefineSigned(def){
def = def.slice(0,-1);
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+3), u8(e, i+2), u8(e, i+5), u8(e, i+4), u8(e, i+7), u8(e, i+6)],
[u8(e, i+1), u8(e, i+0), u8(e, i+3), u8(e, i+2), u8(e, i+5), u8(e, i+4), u8(e, i+7), u8(e, i+6), u8(e, i+9), u8(e, i+8)],
];
}
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 (lines[i].indexOf('(set-byte-order', j)==j) {
//todo: implement custom byte orders
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 (; ;) {
c = lines[i][j];
if (c==' ' || c=='\t') {
j++;
}
else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c=='0' || c=='1' || c=='!' || c=='?') {
pattern = pattern + c;
j++;
}
else if (c == ':') {
j++;
var num = 0;
while (lines[i][j] >= '0' && lines[i][j] <= '9') {
num = (num * 10) + (lines[i][j] - '0');
j++;
}
c = pattern[pattern.length - 1];
while (num-- > 1) {
pattern = pattern + c;
}
}
else {
break;
}
}
//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(''));
if (inst) {
return inst.bytelength;
}
return 2;
}
function dis(memory){
var d = [];
for (var a=0; a<memory.length; ){
a+=dis2(arch, memory, a, d);
}
return d;
}
function loadBinary(jsonp) {
console.log('Binary loaded');
var binary = window.atob(jsonp.data.content.replace(/\s/g,''));
log('Binary file loaded.\n');
var elem = document.getElementById('dump');
elem.value = dis(binary).join("\n");
}
function loadArchitecture(jsonp) {
log('Architecture loaded');
var 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').value = 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){
var confirmed = true;
if (fileGithubPath.indexOf('raspberrypi/firmware')==0) {
var message = [
'The file you wish to diassemble is contained in the raspberrypi github repo at '+
'https://github.com/raspberrypi.',
'Since May 10 2012, these have been accompanied by a LICENSE.broadcom file at '+
'https://github.com/raspberrypi/firmware/blob/master/boot/LICENCE.broadcom.',
'Before you proceed please read and understand the Broadcom license.',
'You acknowledge that you understand a Github API will be used to retrieve a '+
'transformed version of the file in non binary form (JSONP) from the raspberrypi repo.',
'You acknowledge that you understand that javascript code running in your browser will then '+
'translate the JSONP encoded binary into a further modified ("diassembled") form.',
'You acknowledge that you understand the Github user raspberrypi '+
'placed the files on Github, where they would be subject to Githubs '+
'redistribution terms and transformative APIs as required for the normal functioning of Github services.',
'You acknowledge you are not using this tool for any activities relating to "circumvention" (in the DMCA sense), '+
'but are using it for the purpose of "interoperability".',
'Finally you acknowledge that '+
'the author of this program is in no way responsible for your actions and you will personally indemnify '+
'the author from any and all damages arising from or relating to your use of this tool.',
'By clicking OK below you acknowledge you have read, understood and agree to this agreement. '+
'If you do not agree or do not understand, do NOT proceed. Click "Cancel" (or the equivalent declining '+
'button in your browser) and close the related browser window.'
];
//message = ['We apologize but the ability to disassemble the chosen binary has been temporarily disabled.'];
confirmed = confirm(message.join('\n\n'));
}
if (confirmed)
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 (only tested in Google Chrome).<br>
<!-- See <a href='https://github.com/hermanhermitage/videocoreiv'>https://github.com/hermanhermitage/videocoreiv</a> for additional information on the processor.<br>-->
<br>
<b>Binary:</b><br>
<input type="submit" value="Load" onclick='loadGithubJsonp(document.getElementById("binaryUrl").value, "loadBinary")'/>
https://api.github.com/repos/
<select id='binaryUrl'>
<option value="hermanhermitage/videocoreiv/contents/blinker01/blinker01.bin">hermanhermitage/videocoreiv/contents/blinker01/blinker01.bin</option>
<option value="hermanhermitage/videocoreiv/contents/uart01/uart01.bin">hermanhermitage/videocoreiv/contents/master/uart01/uart01.bin</option>
<option value="hermanhermitage/videocoreiv/contents/uart02/uart02.bin">hermanhermitage/videocoreiv/contents/master/uart02/uart02.bin</option>
<option value="raspberrypi/firmware/contents/boot/bootcode.bin">raspberrypi/firmware/contents/boot/bootcode.bin</option>
<option value="raspberrypi/firmware/contents/boot/start_cd.elf">raspberrypi/firmware/contents/boot/start_cd.elf</option>
<option value="raspberrypi/firmware/blob/5dd9b4962eb5ae9b9cd9f4a1cbfada360521fee4/boot/start_cd.elf">raspberrypi/firmware/contents/boot/start_cd.elf current tracing</option>
<!-- <option value="raspberrypi/firmware/contents/boot/loader.bin">raspberrypi/firmware/contents/boot/loader.bin</option> -->
</select>
, 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>
<code>
<div id='log'></div>
</code>
<!-- swap this line with the one below to try o:10 format LHS's
<script src='https://api.github.com/repos/mm120/binutils-vc4/contents/libvc4/videocoreiv.arch?ref=vc4;callback=loadArchitecture'>
-->
<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>