summaryrefslogtreecommitdiff
path: root/utils/imxtools/sbtools/sb.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/imxtools/sbtools/sb.c')
-rw-r--r--utils/imxtools/sbtools/sb.c86
1 files changed, 45 insertions, 41 deletions
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c
index 218ea5a6f7..33961d41bc 100644
--- a/utils/imxtools/sbtools/sb.c
+++ b/utils/imxtools/sbtools/sb.c
@@ -45,8 +45,9 @@ static void fill_gaps(struct sb_file_t *sb)
45 } 45 }
46} 46}
47 47
48static void compute_sb_offsets(struct sb_file_t *sb) 48static void compute_sb_offsets(struct sb_file_t *sb, void *u, sb_color_printf cprintf)
49{ 49{
50 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
50 sb->image_size = 0; 51 sb->image_size = 0;
51 /* sb header */ 52 /* sb header */
52 sb->image_size += sizeof(struct sb_header_t) / BLOCK_SIZE; 53 sb->image_size += sizeof(struct sb_header_t) / BLOCK_SIZE;
@@ -68,14 +69,14 @@ static void compute_sb_offsets(struct sb_file_t *sb)
68 struct sb_section_t *sec = &sb->sections[i]; 69 struct sb_section_t *sec = &sb->sections[i];
69 sec->sec_size = 0; 70 sec->sec_size = 0;
70 71
71 if(g_debug) 72 char name[5];
72 { 73 sb_fill_section_name(name, sec->identifier);
73 printf("%s section 0x%08x", sec->is_data ? "Data" : "Boot", 74 printf(BLUE, "%s", sec->is_data ? "Data" : "Boot");
74 sec->identifier); 75 printf(GREEN, " Section");
75 if(sec->is_cleartext) 76 printf(YELLOW, "'%s'", name);
76 printf(" (cleartext)"); 77 if(sec->is_cleartext)
77 printf("\n"); 78 printf(RED, " (cleartext)");
78 } 79 printf(OFF, "\n");
79 80
80 sec->file_offset = sb->image_size; 81 sec->file_offset = sb->image_size;
81 for(int j = 0; j < sec->nr_insts; j++) 82 for(int j = 0; j < sec->nr_insts; j++)
@@ -83,24 +84,26 @@ static void compute_sb_offsets(struct sb_file_t *sb)
83 struct sb_inst_t *inst = &sec->insts[j]; 84 struct sb_inst_t *inst = &sec->insts[j];
84 if(inst->inst == SB_INST_CALL || inst->inst == SB_INST_JUMP) 85 if(inst->inst == SB_INST_CALL || inst->inst == SB_INST_JUMP)
85 { 86 {
86 if(g_debug) 87 printf(RED, " %s", inst->inst == SB_INST_CALL ? "CALL" : "JUMP");
87 printf(" %s | addr=0x%08x | arg=0x%08x\n", 88 printf(OFF, " | "); printf(BLUE, "addr=0x%08x", inst->addr);
88 inst->inst == SB_INST_CALL ? "CALL" : "JUMP", inst->addr, inst->argument); 89 printf(OFF, " | "); printf(GREEN, "arg=0x%08x\n", inst->argument);
89 sb->image_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE; 90 sb->image_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE;
90 sec->sec_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE; 91 sec->sec_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE;
91 } 92 }
92 else if(inst->inst == SB_INST_FILL) 93 else if(inst->inst == SB_INST_FILL)
93 { 94 {
94 if(g_debug) 95 printf(RED, " FILL");
95 printf(" FILL | addr=0x%08x | len=0x%08x | pattern=0x%08x\n", 96 printf(OFF, " | "); printf(BLUE, "addr=0x%08x", inst->addr);
96 inst->addr, inst->size, inst->pattern); 97 printf(OFF, " | "); printf(GREEN, "len=0x%08x", inst->size);
98 printf(OFF, " | "); printf(YELLOW, "pattern=0x%08x\n", inst->pattern);
97 sb->image_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE; 99 sb->image_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE;
98 sec->sec_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE; 100 sec->sec_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE;
99 } 101 }
100 else if(inst->inst == SB_INST_LOAD) 102 else if(inst->inst == SB_INST_LOAD)
101 { 103 {
102 if(g_debug) 104 printf(RED, " LOAD");
103 printf(" LOAD | addr=0x%08x | len=0x%08x\n", inst->addr, inst->size); 105 printf(OFF, " | "); printf(BLUE, "addr=0x%08x", inst->addr);
106 printf(OFF, " | "); printf(GREEN, "len=0x%08x\n", inst->size);
104 /* load header */ 107 /* load header */
105 sb->image_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE; 108 sb->image_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE;
106 sec->sec_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE; 109 sec->sec_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE;
@@ -110,29 +113,27 @@ static void compute_sb_offsets(struct sb_file_t *sb)
110 } 113 }
111 else if(inst->inst == SB_INST_MODE) 114 else if(inst->inst == SB_INST_MODE)
112 { 115 {
113 if(g_debug) 116 printf(RED, " MODE");
114 printf(" MODE | mod=0x%08x\n", inst->addr); 117 printf(OFF, " | "); printf(BLUE, "mod=0x%08x\n", inst->addr);
115 sb->image_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE; 118 sb->image_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE;
116 sec->sec_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE; 119 sec->sec_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE;
117 } 120 }
118 else if(inst->inst == SB_INST_DATA) 121 else if(inst->inst == SB_INST_DATA)
119 { 122 {
120 if(g_debug) 123 printf(RED, " DATA");
121 printf(" DATA | size=0x%08x\n", inst->size); 124 printf(OFF, " | "); printf(BLUE, "size=0x%08x\n", inst->size);
122 sb->image_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE; 125 sb->image_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE;
123 sec->sec_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE; 126 sec->sec_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE;
124 } 127 }
125 else if(inst->inst == SB_INST_NOP) 128 else if(inst->inst == SB_INST_NOP)
126 { 129 {
127 if(g_debug) 130 printf(RED, " NOOP\n");
128 printf(" NOOP\n");
129 sb->image_size += sizeof(struct sb_instruction_nop_t) / BLOCK_SIZE; 131 sb->image_size += sizeof(struct sb_instruction_nop_t) / BLOCK_SIZE;
130 sec->sec_size += sizeof(struct sb_instruction_nop_t) / BLOCK_SIZE; 132 sec->sec_size += sizeof(struct sb_instruction_nop_t) / BLOCK_SIZE;
131 } 133 }
132 else 134 else
133 { 135 {
134 if(g_debug) 136 cprintf(u, true, GREY, "die on inst %d\n", inst->inst);
135 printf("die on inst %d\n", inst->inst);
136 } 137 }
137 } 138 }
138 /* we need to make sure next section starts on the right alignment. 139 /* we need to make sure next section starts on the right alignment.
@@ -155,8 +156,8 @@ static void compute_sb_offsets(struct sb_file_t *sb)
155 aug_insts[0].size = missing_sz * BLOCK_SIZE; 156 aug_insts[0].size = missing_sz * BLOCK_SIZE;
156 aug_insts[0].data = xmalloc(missing_sz * BLOCK_SIZE); 157 aug_insts[0].data = xmalloc(missing_sz * BLOCK_SIZE);
157 generate_random_data(aug_insts[0].data, missing_sz * BLOCK_SIZE); 158 generate_random_data(aug_insts[0].data, missing_sz * BLOCK_SIZE);
158 if(g_debug) 159 printf(RED, " DATA");
159 printf(" DATA | size=0x%08x\n", aug_insts[0].size); 160 printf(OFF, " | "); printf(BLUE, "size=0x%08x\n", aug_insts[0].size);
160 } 161 }
161 else 162 else
162 { 163 {
@@ -166,8 +167,7 @@ static void compute_sb_offsets(struct sb_file_t *sb)
166 for(int j = 0; j < nr_aug_insts; j++) 167 for(int j = 0; j < nr_aug_insts; j++)
167 { 168 {
168 aug_insts[j].inst = SB_INST_NOP; 169 aug_insts[j].inst = SB_INST_NOP;
169 if(g_debug) 170 printf(RED, " NOOP\n");
170 printf(" NOOP\n");
171 } 171 }
172 } 172 }
173 173
@@ -183,6 +183,7 @@ static void compute_sb_offsets(struct sb_file_t *sb)
183 } 183 }
184 /* final signature */ 184 /* final signature */
185 sb->image_size += 2; 185 sb->image_size += 2;
186 #undef printf
186} 187}
187 188
188static uint64_t generate_timestamp() 189static uint64_t generate_timestamp()
@@ -282,7 +283,7 @@ static void produce_section_tag_cmd(struct sb_section_t *sec,
282} 283}
283 284
284void produce_sb_instruction(struct sb_inst_t *inst, 285void produce_sb_instruction(struct sb_inst_t *inst,
285 struct sb_instruction_common_t *cmd) 286 struct sb_instruction_common_t *cmd, void *u, sb_color_printf cprintf)
286{ 287{
287 memset(cmd, 0, sizeof(struct sb_instruction_common_t)); 288 memset(cmd, 0, sizeof(struct sb_instruction_common_t));
288 cmd->hdr.opcode = inst->inst; 289 cmd->hdr.opcode = inst->inst;
@@ -311,13 +312,15 @@ void produce_sb_instruction(struct sb_inst_t *inst,
311 break; 312 break;
312 default: 313 default:
313 if(g_debug) 314 if(g_debug)
314 printf("die on invalid inst %d\n", inst->inst); 315 cprintf(u, true, GREY, "die on invalid inst %d\n", inst->inst);
315 } 316 }
316 cmd->hdr.checksum = instruction_checksum(&cmd->hdr); 317 cmd->hdr.checksum = instruction_checksum(&cmd->hdr);
317} 318}
318 319
319enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename) 320enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *u,
321 sb_color_printf cprintf)
320{ 322{
323 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
321 struct crypto_key_t real_key; 324 struct crypto_key_t real_key;
322 real_key.method = CRYPTO_KEY; 325 real_key.method = CRYPTO_KEY;
323 byte crypto_iv[16]; 326 byte crypto_iv[16];
@@ -327,7 +330,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename)
327 memset(cbc_macs[i], 0, 16); 330 memset(cbc_macs[i], 0, 16);
328 331
329 fill_gaps(sb); 332 fill_gaps(sb);
330 compute_sb_offsets(sb); 333 compute_sb_offsets(sb, u, cprintf);
331 334
332 generate_random_data(real_key.u.key, 16); 335 generate_random_data(real_key.u.key, 16);
333 336
@@ -387,14 +390,14 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename)
387 /* KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH */ 390 /* KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH KCAH */
388 if(g_debug) 391 if(g_debug)
389 { 392 {
390 printf("Real key: "); 393 printf(GREEN, "Real key: ");
391 for(int j = 0; j < 16; j++) 394 for(int j = 0; j < 16; j++)
392 printf("%02x", real_key.u.key[j]); 395 printf(YELLOW, "%02x", real_key.u.key[j]);
393 printf("\n"); 396 printf(OFF, "\n");
394 printf("IV : "); 397 printf(GREEN, "IV : ");
395 for(int j = 0; j < 16; j++) 398 for(int j = 0; j < 16; j++)
396 printf("%02x", crypto_iv[j]); 399 printf(YELLOW, "%02x", crypto_iv[j]);
397 printf("\n"); 400 printf(OFF, "\n");
398 } 401 }
399 /* produce sections data */ 402 /* produce sections data */
400 for(int i = 0; i< sb_hdr.nr_sections; i++) 403 for(int i = 0; i< sb_hdr.nr_sections; i++)
@@ -417,7 +420,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename)
417 if(inst->inst != SB_INST_DATA) 420 if(inst->inst != SB_INST_DATA)
418 { 421 {
419 struct sb_instruction_common_t cmd; 422 struct sb_instruction_common_t cmd;
420 produce_sb_instruction(inst, &cmd); 423 produce_sb_instruction(inst, &cmd, u, cprintf);
421 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) 424 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
422 crypto_cbc((byte *)&cmd, (byte *)&cmd, sizeof(cmd) / BLOCK_SIZE, 425 crypto_cbc((byte *)&cmd, (byte *)&cmd, sizeof(cmd) / BLOCK_SIZE,
423 &real_key, cur_cbc_mac, &cur_cbc_mac, 1); 426 &real_key, cur_cbc_mac, &cur_cbc_mac, 1);
@@ -452,7 +455,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename)
452 if(buf_p - buf != sb_hdr.image_size * BLOCK_SIZE) 455 if(buf_p - buf != sb_hdr.image_size * BLOCK_SIZE)
453 { 456 {
454 if(g_debug) 457 if(g_debug)
455 printf("SB image buffer was not entirely filled !\n"); 458 printf(GREY, u, true, "SB image buffer was not entirely filled !\n");
456 return SB_ERROR; 459 return SB_ERROR;
457 } 460 }
458 461
@@ -468,6 +471,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename)
468 free(buf); 471 free(buf);
469 472
470 return SB_SUCCESS; 473 return SB_SUCCESS;
474 #undef printf
471} 475}
472 476
473static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, 477static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf,