textual
[RRG-proxmark3.git] / tools / pm3_amii_bin2eml.pl
blob83e9f195f7f9aea84fe15abd11d0371ff0af5c57
1 #!/usr/bin/env perl
3 # Read Amiibo data, decrypt, and produce EML file
4 # Convert proxmark MFU (MIFARE Ultralight) .bin to .eml format
5 # for proxmark3 loading and simulation
7 # -samy kamkar 05/28/2017
9 # hf mf eload --ul FILENAME_MINUS_EML
10 # hf 14a sim -t 7 -u UID
12 # perl -lne 'chomp; s/\s+(\S+)$//;$f=$1;if($f=~s/-(\S+)//){$g=hex($1);}else{$g=hex($f)}$f=hex($f); for$m($f..$g){print "0x" . substr(unpack("H4",pack("n",$m)),1) ." => \"$_\","}' /tmp/game >> game2
13 # perl -lne 'if(/^(\S.*?)\s+\w?\w\w\w\w(\s*-\s*\w?\w\w\w\w)?\s*$/){$l=$1} s/(\w{4,5}\s*-\s*)?(\w{4,5})$//; $a=$1;$b=$2; $b=hex($b); $a=$a?hex($a):$b; for$m($a..$b){print "0x" . substr(unpack("H4",pack("n",$m)),0) ." => \"$l\","}' /tmp/g2
15 my $UIDLOC = -540; # UID is 540 bytes from the end
16 my $BLOCKSIZE = 4; # in bytes
17 my $AMIITOOL = '../client/deps/amiitool/amiitool'; # path to amiitool (unless in $PATH)
18 my $KEYFILE = '../client/resources/key_retail.bin'; # path to retail key file
19 my $ADDHDR = 1; # add 56 byte header?
20 my $FIXPWD = 1; # recalculate PWD if dump value is 0
21 my $FIXACK = 1; # set ACK if dump value is 0
22 my $DECRYPT = 0; # auto-decrypt
24 my %game = (
25 0x000 => "Mario",
26 0x001 => "Mario",
27 0x008 => "Yoshi's Woolly World",
28 0x010 => "The Legend of Zelda",
29 0x014 => "Breath of the Wild",
30 0x018 => "Animal Crossing",
31 0x019 => "Animal Crossing",
32 0x01a => "Animal Crossing",
33 0x01b => "Animal Crossing",
34 0x01c => "Animal Crossing",
35 0x01d => "Animal Crossing",
36 0x01e => "Animal Crossing",
37 0x01f => "Animal Crossing",
38 0x020 => "Animal Crossing",
39 0x021 => "Animal Crossing",
40 0x022 => "Animal Crossing",
41 0x023 => "Animal Crossing",
42 0x024 => "Animal Crossing",
43 0x025 => "Animal Crossing",
44 0x026 => "Animal Crossing",
45 0x027 => "Animal Crossing",
46 0x028 => "Animal Crossing",
47 0x029 => "Animal Crossing",
48 0x02a => "Animal Crossing",
49 0x02b => "Animal Crossing",
50 0x02c => "Animal Crossing",
51 0x02d => "Animal Crossing",
52 0x02e => "Animal Crossing",
53 0x02f => "Animal Crossing",
54 0x030 => "Animal Crossing",
55 0x031 => "Animal Crossing",
56 0x032 => "Animal Crossing",
57 0x033 => "Animal Crossing",
58 0x034 => "Animal Crossing",
59 0x035 => "Animal Crossing",
60 0x036 => "Animal Crossing",
61 0x037 => "Animal Crossing",
62 0x038 => "Animal Crossing",
63 0x039 => "Animal Crossing",
64 0x03a => "Animal Crossing",
65 0x03b => "Animal Crossing",
66 0x03c => "Animal Crossing",
67 0x03d => "Animal Crossing",
68 0x03e => "Animal Crossing",
69 0x03f => "Animal Crossing",
70 0x040 => "Animal Crossing",
71 0x041 => "Animal Crossing",
72 0x042 => "Animal Crossing",
73 0x043 => "Animal Crossing",
74 0x044 => "Animal Crossing",
75 0x045 => "Animal Crossing",
76 0x046 => "Animal Crossing",
77 0x047 => "Animal Crossing",
78 0x048 => "Animal Crossing",
79 0x049 => "Animal Crossing",
80 0x04a => "Animal Crossing",
81 0x04b => "Animal Crossing",
82 0x04c => "Animal Crossing",
83 0x04d => "Animal Crossing",
84 0x04e => "Animal Crossing",
85 0x04f => "Animal Crossing",
86 0x050 => "Animal Crossing",
87 0x051 => "Animal Crossing",
88 0x058 => "Star Fox",
89 0x05c => "Metroid",
90 0x060 => "F-Zero",
91 0x064 => "Pikmin",
92 0x06c => "Punch Out",
93 0x070 => "Wii Fit",
94 0x074 => "Kid Icarus",
95 0x078 => "Classic Nintendo",
96 0x07c => "Mii",
97 0x080 => "Splatoon",
98 0x09c => "Mario Sports Superstars",
99 0x09d => "Mario Sports Superstars",
100 0x190 => "Pokemon",
101 0x191 => "Pokemon",
102 0x192 => "Pokemon",
103 0x193 => "Pokemon",
104 0x194 => "Pokemon",
105 0x195 => "Pokemon",
106 0x196 => "Pokemon",
107 0x197 => "Pokemon",
108 0x198 => "Pokemon",
109 0x199 => "Pokemon",
110 0x19a => "Pokemon",
111 0x19b => "Pokemon",
112 0x19c => "Pokemon",
113 0x19d => "Pokemon",
114 0x19e => "Pokemon",
115 0x19f => "Pokemon",
116 0x1a0 => "Pokemon",
117 0x1a1 => "Pokemon",
118 0x1a2 => "Pokemon",
119 0x1a3 => "Pokemon",
120 0x1a4 => "Pokemon",
121 0x1a5 => "Pokemon",
122 0x1a6 => "Pokemon",
123 0x1a7 => "Pokemon",
124 0x1a8 => "Pokemon",
125 0x1a9 => "Pokemon",
126 0x1aa => "Pokemon",
127 0x1ab => "Pokemon",
128 0x1ac => "Pokemon",
129 0x1ad => "Pokemon",
130 0x1ae => "Pokemon",
131 0x1af => "Pokemon",
132 0x1b0 => "Pokemon",
133 0x1b1 => "Pokemon",
134 0x1b2 => "Pokemon",
135 0x1b3 => "Pokemon",
136 0x1b4 => "Pokemon",
137 0x1b5 => "Pokemon",
138 0x1b6 => "Pokemon",
139 0x1b7 => "Pokemon",
140 0x1b8 => "Pokemon",
141 0x1b9 => "Pokemon",
142 0x1ba => "Pokemon",
143 0x1bb => "Pokemon",
144 0x1bc => "Pokemon",
145 0x1bd => "Pokemon",
146 0x1d0 => "Pokken",
147 0x1f0 => "Kirby",
148 0x1f4 => "BoxBoy!",
149 0x210 => "Fire Emblem",
150 0x224 => "Xenoblade",
151 0x228 => "Earthbound",
152 0x22c => "Chibi Robo",
153 0x320 => "Sonic",
154 0x334 => "Pac-man",
155 0x348 => "Megaman",
156 0x34c => "Street fighter",
157 0x350 => "Monster Hunter",
158 0x35c => "Shovel Knight",
161 my %type = (
162 0x00 => "Figure",
163 0x01 => "Card",
164 0x02 => "Yarn",
167 my %amiibo = (
168 0x0000 => "Super Smash Bros.",
169 0x0001 => "Super Smash Bros.",
170 0x0002 => "Super Smash Bros.",
171 0x0003 => "Super Smash Bros.",
172 0x0004 => "Super Smash Bros.",
173 0x0005 => "Super Smash Bros.",
174 0x0006 => "Super Smash Bros.",
175 0x0007 => "Super Smash Bros.",
176 0x0008 => "Super Smash Bros.",
177 0x0009 => "Super Smash Bros.",
178 0x000a => "Super Smash Bros.",
179 0x000b => "Super Smash Bros.",
180 0x000c => "Super Smash Bros.",
181 0x000d => "Super Smash Bros.",
182 0x000e => "Super Smash Bros.",
183 0x000f => "Super Smash Bros.",
184 0x0010 => "Super Smash Bros.",
185 0x0011 => "Super Smash Bros.",
186 0x0012 => "Super Smash Bros.",
187 0x0013 => "Super Smash Bros.",
188 0x0014 => "Super Smash Bros.",
189 0x0015 => "Super Smash Bros.",
190 0x0016 => "Super Smash Bros.",
191 0x0017 => "Super Smash Bros.",
192 0x0018 => "Super Smash Bros.",
193 0x0019 => "Super Smash Bros.",
194 0x001a => "Super Smash Bros.",
195 0x001b => "Super Smash Bros.",
196 0x001c => "Super Smash Bros.",
197 0x001d => "Super Smash Bros.",
198 0x001e => "Super Smash Bros.",
199 0x001f => "Super Smash Bros.",
200 0x0020 => "Super Smash Bros.",
201 0x0021 => "Super Smash Bros.",
202 0x0022 => "Super Smash Bros.",
203 0x0023 => "Super Smash Bros.",
204 0x0024 => "Super Smash Bros.",
205 0x0025 => "Super Smash Bros.",
206 0x0026 => "Super Smash Bros.",
207 0x0027 => "Super Smash Bros.",
208 0x0028 => "Super Smash Bros.",
209 0x0029 => "Super Smash Bros.",
210 0x002a => "Super Smash Bros.",
211 0x002b => "Super Smash Bros.",
212 0x002c => "Super Smash Bros.",
213 0x002d => "Super Smash Bros.",
214 0x002e => "Super Smash Bros.",
215 0x002f => "Super Smash Bros.",
216 0x0030 => "Super Smash Bros.",
217 0x0031 => "Super Smash Bros.",
218 0x0032 => "Super Smash Bros.",
219 0x0033 => "Super Smash Bros.",
220 0x023d => "Super Smash Bros.",
221 0x0251 => "Super Smash Bros.",
222 0x0252 => "Super Smash Bros.",
223 0x0253 => "Super Smash Bros.",
224 0x0258 => "Super Smash Bros.",
225 0x0034 => "Super Mario",
226 0x0035 => "Super Mario",
227 0x0036 => "Super Mario",
228 0x0037 => "Super Mario",
229 0x0038 => "Super Mario",
230 0x0039 => "Super Mario",
231 0x0262 => "Super Mario",
232 0x0263 => "Super Mario",
233 0x0028 => "Super Mario",
234 0x003c => "Super Mario",
235 0x003d => "Super Mario",
236 0x003a => "Chibi Robo",
237 0x003e => "Splatoon",
238 0x003f => "Splatoon",
239 0x0040 => "Splatoon",
240 0x025d => "Splatoon",
241 0x025e => "Splatoon",
242 0x025f => "Splatoon",
243 0x0260 => "Splatoon",
244 0x0261 => "Splatoon",
245 0x0044 => "Animal Crossing Cards",
246 0x0045 => "Animal Crossing Cards",
247 0x0046 => "Animal Crossing Cards",
248 0x0047 => "Animal Crossing Cards",
249 0x0048 => "Animal Crossing Cards",
250 0x0049 => "Animal Crossing Cards",
251 0x004a => "Animal Crossing Cards",
252 0x004b => "Animal Crossing Cards",
253 0x004c => "Animal Crossing Cards",
254 0x004d => "Animal Crossing Cards",
255 0x004e => "Animal Crossing Cards",
256 0x004f => "Animal Crossing Cards",
257 0x0050 => "Animal Crossing Cards",
258 0x0051 => "Animal Crossing Cards",
259 0x0052 => "Animal Crossing Cards",
260 0x0053 => "Animal Crossing Cards",
261 0x0054 => "Animal Crossing Cards",
262 0x0055 => "Animal Crossing Cards",
263 0x0056 => "Animal Crossing Cards",
264 0x0057 => "Animal Crossing Cards",
265 0x0058 => "Animal Crossing Cards",
266 0x0059 => "Animal Crossing Cards",
267 0x005a => "Animal Crossing Cards",
268 0x005b => "Animal Crossing Cards",
269 0x005c => "Animal Crossing Cards",
270 0x005d => "Animal Crossing Cards",
271 0x005e => "Animal Crossing Cards",
272 0x005f => "Animal Crossing Cards",
273 0x0060 => "Animal Crossing Cards",
274 0x0061 => "Animal Crossing Cards",
275 0x0062 => "Animal Crossing Cards",
276 0x0063 => "Animal Crossing Cards",
277 0x0064 => "Animal Crossing Cards",
278 0x0065 => "Animal Crossing Cards",
279 0x0066 => "Animal Crossing Cards",
280 0x0067 => "Animal Crossing Cards",
281 0x0068 => "Animal Crossing Cards",
282 0x0069 => "Animal Crossing Cards",
283 0x006a => "Animal Crossing Cards",
284 0x006b => "Animal Crossing Cards",
285 0x006c => "Animal Crossing Cards",
286 0x006d => "Animal Crossing Cards",
287 0x006e => "Animal Crossing Cards",
288 0x006f => "Animal Crossing Cards",
289 0x0070 => "Animal Crossing Cards",
290 0x0071 => "Animal Crossing Cards",
291 0x0072 => "Animal Crossing Cards",
292 0x0073 => "Animal Crossing Cards",
293 0x0074 => "Animal Crossing Cards",
294 0x0075 => "Animal Crossing Cards",
295 0x0076 => "Animal Crossing Cards",
296 0x0077 => "Animal Crossing Cards",
297 0x0078 => "Animal Crossing Cards",
298 0x0079 => "Animal Crossing Cards",
299 0x007a => "Animal Crossing Cards",
300 0x007b => "Animal Crossing Cards",
301 0x007c => "Animal Crossing Cards",
302 0x007d => "Animal Crossing Cards",
303 0x007e => "Animal Crossing Cards",
304 0x007f => "Animal Crossing Cards",
305 0x0080 => "Animal Crossing Cards",
306 0x0081 => "Animal Crossing Cards",
307 0x0082 => "Animal Crossing Cards",
308 0x0083 => "Animal Crossing Cards",
309 0x0084 => "Animal Crossing Cards",
310 0x0085 => "Animal Crossing Cards",
311 0x0086 => "Animal Crossing Cards",
312 0x0087 => "Animal Crossing Cards",
313 0x0088 => "Animal Crossing Cards",
314 0x0089 => "Animal Crossing Cards",
315 0x008a => "Animal Crossing Cards",
316 0x008b => "Animal Crossing Cards",
317 0x008c => "Animal Crossing Cards",
318 0x008d => "Animal Crossing Cards",
319 0x008e => "Animal Crossing Cards",
320 0x008f => "Animal Crossing Cards",
321 0x0090 => "Animal Crossing Cards",
322 0x0091 => "Animal Crossing Cards",
323 0x0092 => "Animal Crossing Cards",
324 0x0093 => "Animal Crossing Cards",
325 0x0094 => "Animal Crossing Cards",
326 0x0095 => "Animal Crossing Cards",
327 0x0096 => "Animal Crossing Cards",
328 0x0097 => "Animal Crossing Cards",
329 0x0098 => "Animal Crossing Cards",
330 0x0099 => "Animal Crossing Cards",
331 0x009a => "Animal Crossing Cards",
332 0x009b => "Animal Crossing Cards",
333 0x009c => "Animal Crossing Cards",
334 0x009d => "Animal Crossing Cards",
335 0x009e => "Animal Crossing Cards",
336 0x009f => "Animal Crossing Cards",
337 0x00a0 => "Animal Crossing Cards",
338 0x00a1 => "Animal Crossing Cards",
339 0x00a2 => "Animal Crossing Cards",
340 0x00a3 => "Animal Crossing Cards",
341 0x00a4 => "Animal Crossing Cards",
342 0x00a5 => "Animal Crossing Cards",
343 0x00a6 => "Animal Crossing Cards",
344 0x00a7 => "Animal Crossing Cards",
345 0x00a8 => "Animal Crossing Cards",
346 0x00a9 => "Animal Crossing Cards",
347 0x00aa => "Animal Crossing Cards",
348 0x00ab => "Animal Crossing Cards",
349 0x00ac => "Animal Crossing Cards",
350 0x00ad => "Animal Crossing Cards",
351 0x00ae => "Animal Crossing Cards",
352 0x00af => "Animal Crossing Cards",
353 0x00b0 => "Animal Crossing Cards",
354 0x00b1 => "Animal Crossing Cards",
355 0x00b2 => "Animal Crossing Cards",
356 0x00b3 => "Animal Crossing Cards",
357 0x00b4 => "Animal Crossing Cards",
358 0x00b5 => "Animal Crossing Cards",
359 0x00b6 => "Animal Crossing Cards",
360 0x00b7 => "Animal Crossing Cards",
361 0x00b8 => "Animal Crossing Cards",
362 0x00b9 => "Animal Crossing Cards",
363 0x00ba => "Animal Crossing Cards",
364 0x00bb => "Animal Crossing Cards",
365 0x00bc => "Animal Crossing Cards",
366 0x00bd => "Animal Crossing Cards",
367 0x00be => "Animal Crossing Cards",
368 0x00bf => "Animal Crossing Cards",
369 0x00c0 => "Animal Crossing Cards",
370 0x00c1 => "Animal Crossing Cards",
371 0x00c2 => "Animal Crossing Cards",
372 0x00c3 => "Animal Crossing Cards",
373 0x00c4 => "Animal Crossing Cards",
374 0x00c5 => "Animal Crossing Cards",
375 0x00c6 => "Animal Crossing Cards",
376 0x00c7 => "Animal Crossing Cards",
377 0x00c8 => "Animal Crossing Cards",
378 0x00c9 => "Animal Crossing Cards",
379 0x00ca => "Animal Crossing Cards",
380 0x00cb => "Animal Crossing Cards",
381 0x00cc => "Animal Crossing Cards",
382 0x00cd => "Animal Crossing Cards",
383 0x00ce => "Animal Crossing Cards",
384 0x00cf => "Animal Crossing Cards",
385 0x00d0 => "Animal Crossing Cards",
386 0x00d1 => "Animal Crossing Cards",
387 0x00d2 => "Animal Crossing Cards",
388 0x00d3 => "Animal Crossing Cards",
389 0x00d4 => "Animal Crossing Cards",
390 0x00d5 => "Animal Crossing Cards",
391 0x00d6 => "Animal Crossing Cards",
392 0x00d7 => "Animal Crossing Cards",
393 0x00d8 => "Animal Crossing Cards",
394 0x00d9 => "Animal Crossing Cards",
395 0x00da => "Animal Crossing Cards",
396 0x00db => "Animal Crossing Cards",
397 0x00dc => "Animal Crossing Cards",
398 0x00dd => "Animal Crossing Cards",
399 0x00de => "Animal Crossing Cards",
400 0x00df => "Animal Crossing Cards",
401 0x00e0 => "Animal Crossing Cards",
402 0x00e1 => "Animal Crossing Cards",
403 0x00e2 => "Animal Crossing Cards",
404 0x00e3 => "Animal Crossing Cards",
405 0x00e4 => "Animal Crossing Cards",
406 0x00e5 => "Animal Crossing Cards",
407 0x00e6 => "Animal Crossing Cards",
408 0x00e7 => "Animal Crossing Cards",
409 0x00e8 => "Animal Crossing Cards",
410 0x00e9 => "Animal Crossing Cards",
411 0x00ea => "Animal Crossing Cards",
412 0x00eb => "Animal Crossing Cards",
413 0x00ec => "Animal Crossing Cards",
414 0x00ed => "Animal Crossing Cards",
415 0x00ee => "Animal Crossing Cards",
416 0x00ef => "Animal Crossing Cards",
417 0x00f0 => "Animal Crossing Cards",
418 0x00f1 => "Animal Crossing Cards",
419 0x00f2 => "Animal Crossing Cards",
420 0x00f3 => "Animal Crossing Cards",
421 0x00f4 => "Animal Crossing Cards",
422 0x00f5 => "Animal Crossing Cards",
423 0x00f6 => "Animal Crossing Cards",
424 0x00f7 => "Animal Crossing Cards",
425 0x00f8 => "Animal Crossing Cards",
426 0x00f9 => "Animal Crossing Cards",
427 0x00fa => "Animal Crossing Cards",
428 0x00fb => "Animal Crossing Cards",
429 0x00fc => "Animal Crossing Cards",
430 0x00fd => "Animal Crossing Cards",
431 0x00fe => "Animal Crossing Cards",
432 0x00ff => "Animal Crossing Cards",
433 0x0100 => "Animal Crossing Cards",
434 0x0101 => "Animal Crossing Cards",
435 0x0102 => "Animal Crossing Cards",
436 0x0103 => "Animal Crossing Cards",
437 0x0104 => "Animal Crossing Cards",
438 0x0105 => "Animal Crossing Cards",
439 0x0106 => "Animal Crossing Cards",
440 0x0107 => "Animal Crossing Cards",
441 0x0108 => "Animal Crossing Cards",
442 0x0109 => "Animal Crossing Cards",
443 0x010a => "Animal Crossing Cards",
444 0x010b => "Animal Crossing Cards",
445 0x01d4 => "Animal Crossing Cards",
446 0x01d5 => "Animal Crossing Cards",
447 0x01d6 => "Animal Crossing Cards",
448 0x01d7 => "Animal Crossing Cards",
449 0x01d8 => "Animal Crossing Cards",
450 0x0041 => "Yoshi's Woolly World",
451 0x0042 => "Yoshi's Woolly World",
452 0x0043 => "Yoshi's Woolly World",
453 0x023e => "Yoshi's Woolly World",
454 0x035d => "Yoshi's Woolly World",
455 0x0238 => "8 - Bit Mario",
456 0x0239 => "8 - Bit Mario",
457 0x023a => "Skylanders",
458 0x023b => "Skylanders",
459 0x023f => "Animal Crossing Figures",
460 0x0240 => "Animal Crossing Figures",
461 0x0241 => "Animal Crossing Figures",
462 0x0242 => "Animal Crossing Figures",
463 0x0243 => "Animal Crossing Figures",
464 0x0244 => "Animal Crossing Figures",
465 0x0245 => "Animal Crossing Figures",
466 0x0246 => "Animal Crossing Figures",
467 0x0247 => "Animal Crossing Figures",
468 0x0248 => "Animal Crossing Figures",
469 0x0249 => "Animal Crossing Figures",
470 0x024a => "Animal Crossing Figures",
471 0x024f => "The Legend of Zelda",
472 0x034b => "The Legend of Zelda",
473 0x034c => "The Legend of Zelda",
474 0x034d => "The Legend of Zelda",
475 0x034e => "The Legend of Zelda",
476 0x034f => "The Legend of Zelda",
477 0x0350 => "The Legend of Zelda",
478 0x0351 => "The Legend of Zelda",
479 0x0352 => "The Legend of Zelda",
480 0x0353 => "The Legend of Zelda",
481 0x0354 => "The Legend of Zelda",
482 0x0355 => "The Legend of Zelda",
483 0x0356 => "The Legend of Zelda",
484 0x0357 => "The Legend of Zelda",
485 0x0358 => "The Legend of Zelda",
486 0x0359 => "The Legend of Zelda",
487 0x035a => "The Legend of Zelda",
488 0x035b => "The Legend of Zelda",
489 0x035c => "The Legend of Zelda",
490 0x0250 => "Shovel Knight",
491 0x0254 => "Kirby",
492 0x0255 => "Kirby",
493 0x0256 => "Kirby",
494 0x0257 => "Kirby",
495 0x025c => "Pokken",
496 0x02e1 => "Monster Hunter Stories",
497 0x02e2 => "Monster Hunter Stories",
498 0x02e3 => "Monster Hunter Stories",
499 0x0319 => "Animal Crossing Sanrio",
500 0x031a => "Animal Crossing Sanrio",
501 0x031b => "Animal Crossing Sanrio",
502 0x031c => "Animal Crossing Sanrio",
503 0x031d => "Animal Crossing Sanrio",
504 0x031e => "Animal Crossing Sanrio",
505 0x035e => "BoxBoy!",
506 0x0360 => "Fire Emblem",
507 0x0361 => "Fire Emblem",
510 my %amiiboseries = (
511 0x00 => "Super Smash Bros.",
512 0x01 => "Super Mario",
513 0x02 => "Chibi-Robo",
514 0x03 => "Yoshi's Woolly World",
515 0x04 => "Splatoon",
516 0x05 => "Animal Crossing",
517 0x06 => "8 - Bit Mario",
518 0x07 => "Skylanders",
519 0x08 => "???",
520 0x09 => "The Legend Of Zelda",
521 0x0A => "Shovel Knight",
522 0x0B => "??? (Pikmin?)",
523 0x0C => "Kirby",
524 0x0D => "Pokken",
525 0x0E => "Mario Sports Superstars",
526 0x0F => "Monster Hunter",
527 0x10 => "BoxBoy!",
528 0x11 => "???",
529 0x12 => "Fire Emblem",
532 use strict;
533 my @err;
534 sub err { push @err, @_ }
536 die "usage: $0 <input .bin>\n" unless @ARGV == 1;
537 my $input = shift;
539 open(IN, "<$input") || die "Can't read $input$!";
540 my $file = join "", <IN>;
541 close(IN);
543 sub bytes { return substr($file, ((length($file) + $UIDLOC) + $_[0]), $_[1] || 1) }
545 # check for crypto
546 #my $dec_check = substr($file, ((length($file) + $UIDLOC) + 3 ), 1) eq "\xE0";
547 #my $enc_check = substr($file, ((length($file) + $UIDLOC) + 3 + 8), 1) eq "\xE0";
548 my $dec_check = bytes(3 + 0, 1) eq "\xE0";
549 my $enc_check = bytes(3 + 8, 1) eq "\xE0";
551 my $game = ( unpack("H3", bytes(84 + 0, 2)));
552 my $char = (substr(unpack("H2", bytes(84 + 1)), 1));
553 my $cvar = ( unpack("H2", bytes(84 + 2)));
554 my $type = ( unpack("H2", bytes(84 + 3)));
555 my $amii = ( unpack("H4", bytes(84 + 4, 2)));
556 my $amis = ( unpack("H2", bytes(84 + 6)));
557 my $last = ( unpack("H2", bytes(84 + 7)));
558 err "Character / info: " . join(" ", map { unpack("H2", $_) } split(//, bytes(84, 8)));
559 err "Game : $game $game{hex($game)}";
560 err "Character: $char --";
561 err "Variation: $cvar --";
562 err "Type : $type $type{hex($type)}";
563 err "Amiibo : $amii $amiibo{hex($amii)}";
564 err "Series : $amis $amiiboseries{hex($amis)}";
565 err "Last : $last (should be 02)";
566 err "";
568 # looks like encrypted file
569 my $run;
570 if ($enc_check && !$dec_check)
572 if ($DECRYPT)
574 $run = "'$AMIITOOL' -d -k '$KEYFILE' -i '$input'";
575 err "Looks like encrypted file, decrypting";
576 err "Running: $run";
577 $file = `$run`;
579 else
581 err "Looks like encrypted file but setting preventing us from decrypting";
584 elsif ($enc_check && $dec_check)
586 $run = "'$AMIITOOL' -d -k '$KEYFILE' -i '$input'";
587 err "Looks like encrypted AND decrypted file, will try decrypting first";
588 err "Running: $run";
589 my $tmp = `$run`;
590 if (!$tmp)
592 err "Decryption failed, assuming file is already decrypted";
594 else
596 err "Decryption succeeded, loading decrypted contents";
597 $file = $tmp;
600 elsif ($dec_check && !$enc_check)
602 err "Looks like decrypted file, great!";
604 elsif (!$dec_check && !$enc_check)
606 die "Does not look like proper file format! Exiting.\n";
609 my @blocks = ();
610 my $uid = unpack("H14",
611 substr($file, length($file) + $UIDLOC, 3) .
612 substr($file, (length($file) + $UIDLOC) + 4, 4));
613 my $pwd = unpack("H8", substr($file, length($file) - 8, 4));
614 my $ack = unpack("H8", substr($file, length($file) - 4, 4));
616 my $fixedpwd = 0;
617 if ($FIXPWD && hex($pwd) == 0) {
618 # calculate correct amiibo password according to UID
619 err "PWD is blank, recalculating";
620 my $uid_a = hex(substr $uid, 2, 8);
621 my $uid_b = hex(substr $uid, 6, 8);
622 $pwd = sprintf("%08x", $uid_a ^ $uid_b ^ 0xaa55aa55);
624 $fixedpwd = 1;
627 my $fixedack = 0;
628 if ($FIXACK && hex($ack) == 0) {
629 # this is the command to be sent back to the Switch if
630 # the Switch sends the correct PWD
631 err "ACK is blank, fixing";
632 $ack = "80808080";
634 $fixedack = 1;
637 # file does not contain our 56 byte header, let's add it
638 my $addedhdr = 0;
639 if ($ADDHDR && length($file) == -1 * $UIDLOC)
641 err "Does not contain header, adding";
642 while (<DATA>)
644 chomp; # there may not be a newline so chomp and add below
645 push @blocks, $_;
648 $addedhdr = 1;
651 my $pages = 0;
652 while (length($file))
654 my $out = substr($file, 0, $BLOCKSIZE, ""); # was 16
655 $out = unpack("H*", $out);
656 push @blocks, $out;
658 $pages++;
661 if ($fixedpwd) {
662 @blocks[-2] = $pwd;
665 if ($fixedack) {
666 @blocks[-1] = $ack;
669 if ($addedhdr) {
670 @blocks[2] .= sprintf "%02X", ($pages - 1);
673 # finally, output the data
674 foreach(@blocks) {
675 print "$_\n";
678 print STDERR "\n";
679 print STDERR "$_\n" for @err;
680 print STDERR "UID: $uid\n";
681 print STDERR "PWD: $pwd\n";
682 print STDERR "ACK: $ack\n";
683 print STDERR "\n";
684 $uid = uc $uid;
685 #print STDERR "amiitool -d -k ../client/amiitool/key_retail.bin -i $input -o $input.decrypted\n";
686 $input =~ s/\....$//;
687 print STDERR "hf mfu eload --u $input\n";
688 print STDERR "hf 14a sim -t 7 -u $uid\n";
691 __DATA__
692 00040402
693 01001103
694 010000
695 92580B4C
696 45A9C42F
697 A90145CE
698 5E5F9C43
699 09A43D47
700 D232A3D1
701 68CBADE6
702 7F8185C6
703 00000000
704 00000000
705 00000000