diff options
-rw-r--r-- | utils/imxtools/sbtools/sb1.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/utils/imxtools/sbtools/sb1.c b/utils/imxtools/sbtools/sb1.c index ff14de0786..e4eb470e99 100644 --- a/utils/imxtools/sbtools/sb1.c +++ b/utils/imxtools/sbtools/sb1.c | |||
@@ -318,7 +318,7 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | |||
318 | 318 | ||
319 | if(header->image_size % SECTOR_SIZE) | 319 | if(header->image_size % SECTOR_SIZE) |
320 | { | 320 | { |
321 | if(g_force) | 321 | if(!g_force) |
322 | printf(GREY, "Image size is not a multiple of sector size\n"); | 322 | printf(GREY, "Image size is not a multiple of sector size\n"); |
323 | else | 323 | else |
324 | fatal(SB1_FORMAT_ERROR, "Image size is not a multiple of sector size\n"); | 324 | fatal(SB1_FORMAT_ERROR, "Image size is not a multiple of sector size\n"); |
@@ -326,6 +326,7 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | |||
326 | 326 | ||
327 | /* find key */ | 327 | /* find key */ |
328 | union xorcrypt_key_t key[2]; | 328 | union xorcrypt_key_t key[2]; |
329 | memset(key, 0, sizeof(key)); | ||
329 | bool valid_key = false; | 330 | bool valid_key = false; |
330 | uint8_t sector[SECTOR_SIZE]; | 331 | uint8_t sector[SECTOR_SIZE]; |
331 | 332 | ||
@@ -338,15 +339,22 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | |||
338 | memcpy(sector, header + 1, SECTOR_SIZE - header->header_size); | 339 | memcpy(sector, header + 1, SECTOR_SIZE - header->header_size); |
339 | /* try to decrypt the first sector */ | 340 | /* try to decrypt the first sector */ |
340 | uint32_t mark = xor_decrypt(key, sector, SECTOR_SIZE - 4 - header->header_size); | 341 | uint32_t mark = xor_decrypt(key, sector, SECTOR_SIZE - 4 - header->header_size); |
342 | /* copy key again it's modified by the crypto code */ | ||
343 | memcpy(key, g_key_array[i].u.xor_key, sizeof(key)); | ||
341 | if(mark != *(uint32_t *)§or[SECTOR_SIZE - 4 - header->header_size]) | 344 | if(mark != *(uint32_t *)§or[SECTOR_SIZE - 4 - header->header_size]) |
342 | continue; | 345 | continue; |
343 | /* found ! */ | 346 | /* found ! */ |
344 | valid_key = true; | 347 | valid_key = true; |
345 | /* copy key again it's modified by the crypto code */ | ||
346 | memcpy(key, g_key_array[i].u.xor_key, sizeof(key)); | ||
347 | break; | 348 | break; |
348 | } | 349 | } |
349 | 350 | ||
351 | if(!valid_key) | ||
352 | { | ||
353 | if(!g_force) | ||
354 | fatal(SB1_NO_VALID_KEY, "No valid key found\n"); | ||
355 | printf(GREY, "No valid key found: forced to continue but this will fail\n"); | ||
356 | } | ||
357 | |||
350 | printf(BLUE, "Crypto\n"); | 358 | printf(BLUE, "Crypto\n"); |
351 | for(int i = 0; i < 2; i++) | 359 | for(int i = 0; i < 2; i++) |
352 | { | 360 | { |
@@ -365,9 +373,6 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | |||
365 | } | 373 | } |
366 | 374 | ||
367 | memcpy(file->key.u.xor_key, key, sizeof(key)); | 375 | memcpy(file->key.u.xor_key, key, sizeof(key)); |
368 | |||
369 | if(!valid_key) | ||
370 | fatal(SB1_NO_VALID_KEY, "No valid key found\n"); | ||
371 | 376 | ||
372 | /* decrypt image in-place (and removing crypto markers) */ | 377 | /* decrypt image in-place (and removing crypto markers) */ |
373 | void *ptr = header + 1; | 378 | void *ptr = header + 1; |
@@ -377,7 +382,7 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u, | |||
377 | { | 382 | { |
378 | int size = SECTOR_SIZE - 4 - offset; | 383 | int size = SECTOR_SIZE - 4 - offset; |
379 | uint32_t mark = xor_decrypt(key, ptr, size); | 384 | uint32_t mark = xor_decrypt(key, ptr, size); |
380 | if(mark != *(uint32_t *)(ptr + size)) | 385 | if(mark != *(uint32_t *)(ptr + size) && !g_force) |
381 | fatal(SB1_CHECKSUM_ERROR, "Crypto mark mismatch\n"); | 386 | fatal(SB1_CHECKSUM_ERROR, "Crypto mark mismatch\n"); |
382 | memmove(copy_ptr, ptr, size); | 387 | memmove(copy_ptr, ptr, size); |
383 | 388 | ||