Fix FS#12824 : Malfunctioning FFT plugin in Sansa Clip Zip
[maemo-rb.git] / utils / imxtools / sbtools / sb1.c
blobdeb09a51e17a2a5261f128ccb5fca878072b6cbc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2012 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <time.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include "misc.h"
26 #include "crypto.h"
27 #include "sb1.h"
29 static int sdram_size_table[] = {2, 8, 16, 32, 64};
31 #define NR_SDRAM_ENTRIES (int)(sizeof(sdram_size_table) / sizeof(sdram_size_table[0]))
33 int sb1_sdram_size_by_index(int index)
35 if(index < 0 || index >= NR_SDRAM_ENTRIES)
36 return -1;
37 return sdram_size_table[index];
40 int sb1_sdram_index_by_size(int size)
42 for(int i = 0; i < NR_SDRAM_ENTRIES; i++)
43 if(sdram_size_table[i] == size)
44 return i;
45 return -1;
48 static uint16_t swap16(uint16_t t)
50 return (t << 8) | (t >> 8);
53 static void fix_version(struct sb1_version_t *ver)
55 ver->major = swap16(ver->major);
56 ver->minor = swap16(ver->minor);
57 ver->revision = swap16(ver->revision);
60 enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
62 if(sb->key.method != CRYPTO_XOR_KEY)
63 return SB1_NO_VALID_KEY;
64 /* compute image size (without userdata) */
65 uint32_t image_size = 0;
66 image_size += sizeof(struct sb1_header_t);
67 for(int i = 0; i < sb->nr_insts; i++)
69 switch(sb->insts[i].cmd)
71 case SB1_INST_LOAD:
72 image_size += 8 + ROUND_UP(sb->insts[i].size, 4);
73 break;
74 case SB1_INST_FILL:
75 case SB1_INST_JUMP:
76 case SB1_INST_CALL:
77 image_size += 12;
78 break;
79 case SB1_INST_MODE:
80 case SB1_INST_SDRAM:
81 image_size += 8;
82 break;
83 default:
84 bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd);
87 // now take crypto marks and sector size into account:
88 // there is one crypto mark per sector, ie 4 bytes for 508 = 512 (sector)
89 image_size += 4 * ((image_size + SECTOR_SIZE - 5) / (SECTOR_SIZE - 4));
90 image_size = ROUND_UP(image_size, SECTOR_SIZE);
92 /* allocate buffer and fill it (ignoring crypto for now) */
93 void *buf = xmalloc(image_size);
94 struct sb1_header_t *header = buf;
95 memset(buf, 0, image_size);
96 header->rom_version = sb->rom_version;
97 header->image_size = image_size + sb->userdata_size;
98 header->header_size = sizeof(struct sb1_header_t);
99 header->userdata_offset = sb->userdata ? image_size : 0;
100 memcpy(&header->product_ver, &sb->product_ver, sizeof(sb->product_ver));
101 fix_version(&header->product_ver);
102 memcpy(&header->component_ver, &sb->component_ver, sizeof(sb->component_ver));
103 fix_version(&header->component_ver);
104 header->drive_tag = sb->drive_tag;
105 strncpy((void *)header->signature, "STMP", 4);
107 struct sb1_cmd_header_t *cmd = (void *)(header + 1);
108 for(int i = 0; i < sb->nr_insts; i++)
110 int bytes = 0;
111 int size = 0;
112 switch(sb->insts[i].cmd)
114 case SB1_INST_LOAD:
115 bytes = sb->insts[i].size;
116 cmd->addr = sb->insts[i].addr;
117 memcpy(cmd + 1, sb->insts[i].data, sb->insts[i].size);
118 memset((void *)(cmd + 1) + sb->insts[i].size, 0,
119 bytes - sb->insts[i].size);
120 break;
121 case SB1_INST_FILL:
122 bytes = sb->insts[i].size;
123 size = 2;
124 memcpy(cmd + 1, &sb->insts[i].pattern, 4);
125 cmd->addr = sb->insts[i].addr;
126 break;
127 case SB1_INST_JUMP:
128 case SB1_INST_CALL:
129 bytes = 4;
130 cmd->addr = sb->insts[i].addr;
131 memcpy(cmd + 1, &sb->insts[i].argument, 4);
132 break;
133 case SB1_INST_MODE:
134 bytes = 0;
135 cmd->addr = sb->insts[i].mode;
136 break;
137 case SB1_INST_SDRAM:
138 bytes = 0;
139 cmd->addr = SB1_MK_ADDR_SDRAM(sb->insts[i].sdram.chip_select,
140 sb->insts[i].sdram.size_index);
141 break;
142 default:
143 bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd);
146 /* handle most common cases */
147 if(size == 0)
148 size = ROUND_UP(bytes, 4) / 4 + 1;
150 cmd->cmd = SB1_MK_CMD(sb->insts[i].cmd, sb->insts[i].datatype,
151 bytes, sb->insts[i].critical,
152 size);
154 cmd = (void *)cmd + 4 + size * 4;
157 /* move everything to prepare crypto marks (start at the end !) */
158 for(int i = image_size / SECTOR_SIZE - 1; i >= 0; i--)
159 memmove(buf + i * SECTOR_SIZE, buf + i * (SECTOR_SIZE - 4), SECTOR_SIZE - 4);
161 union xorcrypt_key_t key[2];
162 memcpy(key, sb->key.u.xor_key, sizeof(sb->key));
163 void *ptr = header + 1;
164 int offset = header->header_size;
165 for(unsigned i = 0; i < image_size / SECTOR_SIZE; i++)
167 int size = SECTOR_SIZE - 4 - offset;
168 uint32_t mark = xor_encrypt(key, ptr, size);
169 *(uint32_t *)(ptr + size) = mark;
171 ptr += size + 4;
172 offset = 0;
175 FILE *fd = fopen(filename, "wb");
176 if(fd == NULL)
177 return SB1_OPEN_ERROR;
178 if(fwrite(buf, image_size, 1, fd) != 1)
180 free(buf);
181 return SB1_WRITE_ERROR;
183 free(buf);
184 if(sb->userdata)
185 fwrite(sb->userdata, sb->userdata_size, 1, fd);
186 fclose(fd);
188 return SB1_SUCCESS;
191 struct sb1_file_t *sb1_read_file(const char *filename, void *u,
192 sb1_color_printf cprintf, enum sb1_error_t *err)
194 return sb1_read_file_ex(filename, 0, -1, u, cprintf, err);
197 struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size, void *u,
198 sb1_color_printf cprintf, enum sb1_error_t *err)
200 #define fatal(e, ...) \
201 do { if(err) *err = e; \
202 cprintf(u, true, GREY, __VA_ARGS__); \
203 free(buf); \
204 return NULL; } while(0)
206 FILE *f = fopen(filename, "rb");
207 void *buf = NULL;
208 if(f == NULL)
209 fatal(SB1_OPEN_ERROR, "Cannot open file for reading\n");
210 fseek(f, 0, SEEK_END);
211 size_t read_size = ftell(f);
212 fseek(f, offset, SEEK_SET);
213 if(size != (size_t)-1)
214 read_size = size;
215 buf = xmalloc(read_size);
216 if(fread(buf, read_size, 1, f) != 1)
218 fclose(f);
219 fatal(SB1_READ_ERROR, "Cannot read file\n");
221 fclose(f);
223 struct sb1_file_t *ret = sb1_read_memory(buf, read_size, u, cprintf, err);
224 free(buf);
225 return ret;
227 #undef fatal
230 static const char *sb1_cmd_name(int cmd)
232 switch(cmd)
234 case SB1_INST_LOAD: return "load";
235 case SB1_INST_FILL: return "fill";
236 case SB1_INST_JUMP: return "jump";
237 case SB1_INST_CALL: return "call";
238 case SB1_INST_MODE: return "mode";
239 case SB1_INST_SDRAM: return "sdram";
240 default: return "unknown";
244 static const char *sb1_datatype_name(int cmd)
246 switch(cmd)
248 case SB1_DATATYPE_UINT32: return "uint32";
249 case SB1_DATATYPE_UINT16: return "uint16";
250 case SB1_DATATYPE_UINT8: return "uint8";
251 default: return "unknown";
255 bool sb1_is_key_valid_fast(void *buffer, size_t size, union xorcrypt_key_t _key[2])
257 struct sb1_header_t *header = (struct sb1_header_t *)buffer;
259 union xorcrypt_key_t key[2];
261 uint8_t sector[SECTOR_SIZE];
262 /* copy key and data because it's modified by the crypto code */
263 memcpy(key, _key, sizeof(key));
264 memcpy(sector, header + 1, SECTOR_SIZE - header->header_size);
265 /* try to decrypt the first sector */
266 uint32_t mark = xor_decrypt(key, sector, SECTOR_SIZE - 4 - header->header_size);
267 /* copy key again it's modified by the crypto code */
268 return mark == *(uint32_t *)&sector[SECTOR_SIZE - 4 - header->header_size];
271 bool sb1_brute_force(const char *filename, void *u, sb1_color_printf cprintf,
272 enum sb1_error_t *err, struct crypto_key_t *key)
274 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
275 uint8_t sector[SECTOR_SIZE];
276 FILE *f = fopen(filename, "rb");
277 if(f == NULL)
279 printf("Cannot open file '%s' for reading: %m\n", filename);
280 *err = SB1_OPEN_ERROR;
281 return false;
283 if(fread(sector, sizeof(sector), 1, f) != 1)
285 printf("Cannot read file '%s': %m\n", filename);
286 *err = SB1_READ_ERROR;
287 fclose(f);
288 return false;
290 fclose(f);
292 printf(BLUE, "Brute forcing key...\n");
293 time_t start_time = time(NULL);
294 uint32_t laserfuse[3] = {0, 0, 0};
295 unsigned last_print = 0;
298 for(int i = 0; i < 0x10000; i++)
300 laserfuse[2] = (i & 0xff00) << 8 | (i & 0xff);
301 xor_generate_key(laserfuse, key->u.xor_key);
302 if(g_debug)
304 printf(GREEN, "Trying key");
305 printf(GREEN, "[");
306 printf(RED, "%08x", laserfuse[0]);
307 printf(GREEN, ",");
308 printf(RED, "%08x", laserfuse[1]);
309 printf(GREEN, ",");
310 printf(RED, "%08x", laserfuse[2]);
311 printf(GREEN, "]:");
312 for(int j = 0; j < 32; j++)
313 printf(YELLOW, " %08x", key->u.xor_key[j / 16].k[j % 16]);
315 if(sb1_is_key_valid_fast(sector, SECTOR_SIZE, key->u.xor_key))
317 if(g_debug)
318 printf(RED, " Ok\n");
319 return true;
321 else
323 if(g_debug)
324 printf(RED, " No\n");
327 laserfuse[0]++;
329 if(laserfuse[0] / 1000 != last_print)
331 time_t cur_time = time(NULL);
332 float key_per_sec = laserfuse[0] / (float)(cur_time - start_time);
333 float tot = 0x1000000LL / key_per_sec;
334 time_t eta_time = start_time + tot;
336 printf(YELLOW, "%llu", laserfuse[0] * 0x10000LL);
337 printf(GREEN, " out of ");
338 printf(BLUE, "%llu", 0x1000000LL * 0x10000LL);
339 printf(GREEN, " tested (");
340 printf(RED, "%f%%", laserfuse[0] / (float)0x1000000LL * 100.0);
341 printf(GREEN, "), ");
342 printf(YELLOW, "%d", cur_time - start_time);
343 printf(GREEN, " seconds elapsed, ");
344 printf(BLUE, "%d", eta_time - cur_time);
345 printf(GREEN, " seconds remaining, [");
346 printf(RED, "%f", key_per_sec);
347 printf(GREEN, " keys/s], ETA ");
348 printf(YELLOW, "%s", ctime(&eta_time));
349 last_print = laserfuse[0] / 1000;
351 }while(laserfuse[0] != 0);
353 *err = SB1_NO_VALID_KEY;
354 return false;
355 #undef printf
358 struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u,
359 sb1_color_printf cprintf, enum sb1_error_t *err)
361 struct sb1_file_t *file = NULL;
362 uint8_t *buf = _buf;
364 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
365 #define fatal(e, ...) \
366 do { if(err) *err = e; \
367 cprintf(u, true, GREY, __VA_ARGS__); \
368 sb1_free(file); \
369 return NULL; } while(0)
370 #define print_hex(c, p, len, nl) \
371 do { printf(c, ""); print_hex(p, len, nl); } while(0)
373 file = xmalloc(sizeof(struct sb1_file_t));
374 memset(file, 0, sizeof(struct sb1_file_t));
375 struct sb1_header_t *header = (struct sb1_header_t *)buf;
377 if(memcmp(header->signature, "STMP", 4) != 0)
378 fatal(SB1_FORMAT_ERROR, "Bad signature\n");
379 if(header->image_size > filesize)
380 fatal(SB1_FORMAT_ERROR, "File too small (should be at least %d bytes)\n",
381 header->image_size);
382 if(header->header_size != sizeof(struct sb1_header_t))
383 fatal(SB1_FORMAT_ERROR, "Bad header size\n");
385 printf(BLUE, "Basic info:\n");
386 printf(GREEN, " ROM version: ");
387 printf(YELLOW, "%x\n", header->rom_version);
388 printf(GREEN, " Userdata offset: ");
389 printf(YELLOW, "%x\n", header->userdata_offset);
390 printf(GREEN, " Pad: ");
391 printf(YELLOW, "%x\n", header->pad2);
393 struct sb1_version_t product_ver = header->product_ver;
394 fix_version(&product_ver);
395 struct sb1_version_t component_ver = header->component_ver;
396 fix_version(&component_ver);
398 printf(GREEN, " Product version: ");
399 printf(YELLOW, "%X.%X.%X\n", product_ver.major, product_ver.minor, product_ver.revision);
400 printf(GREEN, " Component version: ");
401 printf(YELLOW, "%X.%X.%X\n", component_ver.major, component_ver.minor, component_ver.revision);
403 printf(GREEN, " Drive tag: ");
404 printf(YELLOW, "%x\n", header->drive_tag);
406 /* copy rom version, padding and drive tag */
407 /* copy versions */
408 memcpy(&file->product_ver, &product_ver, sizeof(product_ver));
409 memcpy(&file->component_ver, &component_ver, sizeof(component_ver));
410 file->rom_version = header->rom_version;
411 file->pad2 = header->pad2;
412 file->drive_tag = header->drive_tag;
414 /* reduce size w.r.t to userdata part */
415 uint32_t userdata_size = 0;
416 if(header->userdata_offset != 0)
418 userdata_size = header->image_size - header->userdata_offset;
419 header->image_size -= userdata_size;
422 if(header->image_size % SECTOR_SIZE)
424 if(!g_force)
425 printf(GREY, "Image size is not a multiple of sector size\n");
426 else
427 fatal(SB1_FORMAT_ERROR, "Image size is not a multiple of sector size\n");
430 /* find key */
431 union xorcrypt_key_t key[2];
432 memset(key, 0, sizeof(key));
433 bool valid_key = false;
434 uint8_t sector[SECTOR_SIZE];
436 for(int i = 0; i < g_nr_keys; i++)
438 if(!g_key_array[i].method == CRYPTO_XOR_KEY)
439 continue;
440 /* copy key and data because it's modified by the crypto code */
441 memcpy(key, g_key_array[i].u.xor_key, sizeof(key));
442 memcpy(sector, header + 1, SECTOR_SIZE - header->header_size);
443 /* try to decrypt the first sector */
444 uint32_t mark = xor_decrypt(key, sector, SECTOR_SIZE - 4 - header->header_size);
445 /* copy key again it's modified by the crypto code */
446 memcpy(key, g_key_array[i].u.xor_key, sizeof(key));
447 if(mark != *(uint32_t *)&sector[SECTOR_SIZE - 4 - header->header_size])
448 continue;
449 /* found ! */
450 valid_key = true;
451 break;
454 if(!valid_key)
456 if(!g_force)
457 fatal(SB1_NO_VALID_KEY, "No valid key found\n");
458 printf(GREY, "No valid key found: forced to continue but this will fail\n");
461 printf(BLUE, "Crypto\n");
462 for(int i = 0; i < 2; i++)
464 printf(RED, " Key %d\n", i);
465 printf(OFF, " ");
466 for(int j = 0; j < 64; j++)
468 printf(YELLOW, "%02x ", key[i].key[j]);
469 if((j + 1) % 16 == 0)
471 printf(OFF, "\n");
472 if(j + 1 != 64)
473 printf(OFF, " ");
478 memcpy(file->key.u.xor_key, key, sizeof(key));
480 /* decrypt image in-place (and removing crypto markers) */
481 void *ptr = header + 1;
482 void *copy_ptr = header + 1;
483 int offset = header->header_size;
484 for(unsigned i = 0; i < header->image_size / SECTOR_SIZE; i++)
486 int size = SECTOR_SIZE - 4 - offset;
487 uint32_t mark = xor_decrypt(key, ptr, size);
488 if(mark != *(uint32_t *)(ptr + size) && !g_force)
489 fatal(SB1_CHECKSUM_ERROR, "Crypto mark mismatch\n");
490 memmove(copy_ptr, ptr, size);
492 ptr += size + 4;
493 copy_ptr += size;
494 offset = 0;
497 /* reduce image size given the removed marks */
498 header->image_size -= header->image_size / SECTOR_SIZE;
500 printf(BLUE, "Commands\n");
501 struct sb1_cmd_header_t *cmd = (void *)(header + 1);
502 while((void *)cmd < (void *)header + header->image_size)
504 printf(GREEN, " Command");
505 printf(YELLOW, " %#x\n", cmd->cmd);
506 printf(YELLOW, " Size:");
507 printf(RED, " %#x\n", SB1_CMD_SIZE(cmd->cmd));
508 printf(YELLOW, " Critical:");
509 printf(RED, " %d\n", SB1_CMD_CRITICAL(cmd->cmd));
510 printf(YELLOW, " Data Type:");
511 printf(RED, " %#x ", SB1_CMD_DATATYPE(cmd->cmd));
512 printf(GREEN, "(%s)\n", sb1_datatype_name(SB1_CMD_DATATYPE(cmd->cmd)));
513 printf(YELLOW, " Bytes:");
514 printf(RED, " %#x\n", SB1_CMD_BYTES(cmd->cmd));
515 printf(YELLOW, " Boot:");
516 printf(RED, " %#x ", SB1_CMD_BOOT(cmd->cmd));
517 printf(GREEN, "(%s)\n", sb1_cmd_name(SB1_CMD_BOOT(cmd->cmd)));
519 /* copy command */
520 struct sb1_inst_t inst;
521 memset(&inst, 0, sizeof(inst));
522 inst.cmd = SB1_CMD_BOOT(cmd->cmd);
523 inst.critical = SB1_CMD_CRITICAL(cmd->cmd);
524 inst.datatype = SB1_CMD_DATATYPE(cmd->cmd);
525 inst.size = SB1_CMD_BYTES(cmd->cmd);
527 switch(SB1_CMD_BOOT(cmd->cmd))
529 case SB1_INST_SDRAM:
530 inst.sdram.chip_select = SB1_ADDR_SDRAM_CS(cmd->addr);
531 inst.sdram.size_index = SB1_ADDR_SDRAM_SZ(cmd->addr);
532 printf(YELLOW, " Ram:");
533 printf(RED, " %#x", inst.addr);
534 printf(GREEN, " (Chip Select=%d, Size=%d)\n", SB1_ADDR_SDRAM_CS(cmd->addr),
535 sb1_sdram_size_by_index(SB1_ADDR_SDRAM_SZ(cmd->addr)));
536 break;
537 case SB1_INST_MODE:
538 inst.mode = cmd->addr;
539 printf(YELLOW, " Mode:");
540 printf(RED, " %#x\n", inst.mode);
541 break;
542 case SB1_INST_LOAD:
543 inst.data = malloc(inst.size);
544 memcpy(inst.data, cmd + 1, inst.size);
545 inst.addr = cmd->addr;
546 printf(YELLOW, " Addr:");
547 printf(RED, " %#x\n", inst.addr);
548 break;
549 case SB1_INST_FILL:
550 inst.addr = cmd->addr;
551 inst.pattern = *(uint32_t *)(cmd + 1);
552 printf(YELLOW, " Addr:");
553 printf(RED, " %#x\n", cmd->addr);
554 printf(YELLOW, " Pattern:");
555 printf(RED, " %#x\n", inst.pattern);
556 break;
557 case SB1_INST_CALL:
558 case SB1_INST_JUMP:
559 inst.addr = cmd->addr;
560 inst.argument = *(uint32_t *)(cmd + 1);
561 printf(YELLOW, " Addr:");
562 printf(RED, " %#x\n", cmd->addr);
563 printf(YELLOW, " Argument:");
564 printf(RED, " %#x\n", inst.pattern);
565 break;
566 default:
567 printf(GREY, "WARNING: unknown SB command !\n");
568 break;
571 file->insts = augment_array(file->insts, sizeof(inst), file->nr_insts, &inst, 1);
572 file->nr_insts++;
574 /* last instruction ? */
575 if(SB1_CMD_BOOT(cmd->cmd) == SB1_INST_JUMP ||
576 SB1_CMD_BOOT(cmd->cmd) == SB1_INST_MODE)
577 break;
579 cmd = (void *)cmd + 4 + 4 * SB1_CMD_SIZE(cmd->cmd);
582 /* copy userdata */
583 file->userdata_size = userdata_size;
584 if(userdata_size > 0)
586 file->userdata = malloc(userdata_size);
587 memcpy(file->userdata, (void *)header + header->userdata_offset, userdata_size);
590 return file;
591 #undef printf
592 #undef fatal
593 #undef print_hex
596 void sb1_free(struct sb1_file_t *file)
598 if(!file) return;
600 for(int i = 0; i < file->nr_insts; i++)
601 free(file->insts[i].data);
602 free(file->insts);
603 free(file->userdata);
604 free(file);
607 void sb1_dump(struct sb1_file_t *file, void *u, sb1_color_printf cprintf)
609 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
610 #define print_hex(c, p, len, nl) \
611 do { printf(c, ""); print_hex(p, len, nl); } while(0)
613 #define TREE RED
614 #define HEADER GREEN
615 #define TEXT YELLOW
616 #define TEXT2 BLUE
617 #define TEXT3 RED
618 #define SEP OFF
620 printf(BLUE, "SB1 File\n");
621 printf(TREE, "+-");
622 printf(HEADER, "Rom Ver: ");
623 printf(TEXT, "%x\n", file->rom_version);
624 printf(TREE, "+-");
625 printf(HEADER, "Pad: ");
626 printf(TEXT, "%x\n", file->pad2);
627 printf(TREE, "+-");
628 printf(HEADER, "Drive Tag: ");
629 printf(TEXT, "%x\n", file->drive_tag);
630 printf(TREE, "+-");
631 printf(HEADER, "Product Version: ");
632 printf(TEXT, "%X.%X.%X\n", file->product_ver.major, file->product_ver.minor,
633 file->product_ver.revision);
634 printf(TREE, "+-");
635 printf(HEADER, "Component Version: ");
636 printf(TEXT, "%X.%X.%X\n", file->component_ver.major, file->component_ver.minor,
637 file->component_ver.revision);
639 for(int j = 0; j < file->nr_insts; j++)
641 struct sb1_inst_t *inst = &file->insts[j];
642 printf(TREE, "+-");
643 printf(HEADER, "Command\n");
644 printf(TREE, "| +-");
645 switch(inst->cmd)
647 case SB1_INST_CALL:
648 case SB1_INST_JUMP:
649 printf(HEADER, "%s", inst->cmd == SB1_INST_CALL ? "CALL" : "JUMP");
650 printf(SEP, " | ");
651 printf(TEXT3, "crit=%d", inst->critical);
652 printf(SEP, " | ");
653 printf(TEXT, "addr=0x%08x\n", inst->addr);
654 break;
655 case SB1_INST_LOAD:
656 printf(HEADER, "LOAD");
657 printf(SEP, " | ");
658 printf(TEXT3, "crit=%d", inst->critical);
659 printf(SEP, " | ");
660 printf(TEXT, "addr=0x%08x", inst->addr);
661 printf(SEP, " | ");
662 printf(TEXT2, "len=0x%08x\n", inst->size);
663 break;
664 case SB1_INST_FILL:
665 printf(HEADER, "FILL");
666 printf(SEP, " | ");
667 printf(TEXT3, "crit=%d", inst->critical);
668 printf(SEP, " | ");
669 printf(TEXT, "addr=0x%08x", inst->addr);
670 printf(SEP, " | ");
671 printf(TEXT2, "len=0x%08x", inst->size);
672 printf(SEP, " | ");
673 printf(TEXT2, "pattern=0x%08x\n", inst->pattern);
674 break;
675 case SB1_INST_MODE:
676 printf(HEADER, "MODE");
677 printf(SEP, " | ");
678 printf(TEXT3, "crit=%d", inst->critical);
679 printf(SEP, " | ");
680 printf(TEXT, "mode=0x%08x\n", inst->addr);
681 break;
682 case SB1_INST_SDRAM:
683 printf(HEADER, "SRAM");
684 printf(SEP, " | ");
685 printf(TEXT3, "crit=%d", inst->critical);
686 printf(SEP, " | ");
687 printf(TEXT, "chip_select=%d", inst->sdram.chip_select);
688 printf(SEP, " | ");
689 printf(TEXT2, "chip_size=%d\n", sb1_sdram_size_by_index(inst->sdram.size_index));
690 break;
691 default:
692 printf(GREY, "[Unknown instruction %x]\n", inst->cmd);
693 break;
697 #undef printf
698 #undef print_hex
701 static struct crypto_key_t g_default_xor_key =
703 .method = CRYPTO_XOR_KEY,
704 .u.xor_key =
706 {.k = {0x67ECAEF6, 0xB31FB961, 0x118A9F4C, 0xA32A97DA,
707 0x6CC39617, 0x5BC00314, 0x9D430685, 0x4D7DB502,
708 0xA347685E, 0x3C87E86C, 0x8987AAA0, 0x24B78EF1,
709 0x893B9605, 0x9BB8C2BE, 0x6D9544E2, 0x375B525C}},
710 {.k = {0x3F424704, 0x53B5A331, 0x6AD345A5, 0x20DCEC51,
711 0x743C8D3B, 0x444B3792, 0x0AF429569, 0xB7EE1111,
712 0x583BF768, 0x9683BF9A, 0x0B032D799, 0xFE4E78ED,
713 0xF20D08C2, 0xFA0BE4A2, 0x4D89C317, 0x887B2D6F}}
717 void sb1_get_default_key(struct crypto_key_t *key)
719 memcpy(key, &g_default_xor_key, sizeof(g_default_xor_key));
720 /* decrypt the xor key which is xor'ed */
721 for(int i = 0; i < 2; i++)
722 for(int j = 0; j < 16; j++)
723 key->u.xor_key[i].k[j] ^= 0xaa55aa55;