imxtools/sbtools: fix some instruction handling & crypto

Change-Id: I6530bdf27896d8325dec4e2ba31c7e6a0131a286
This commit is contained in:
Amaury Pouly 2012-12-16 21:07:31 +01:00
parent f4f600fc52
commit 212cfdf771
2 changed files with 17 additions and 8 deletions

View file

@ -59,6 +59,8 @@ static void fix_version(struct sb1_version_t *ver)
enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename) enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
{ {
if(sb->key.method != CRYPTO_XOR_KEY)
return SB1_NO_VALID_KEY;
/* compute image size (without userdata) */ /* compute image size (without userdata) */
uint32_t image_size = 0; uint32_t image_size = 0;
image_size += sizeof(struct sb1_header_t); image_size += sizeof(struct sb1_header_t);
@ -104,6 +106,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
for(int i = 0; i < sb->nr_insts; i++) for(int i = 0; i < sb->nr_insts; i++)
{ {
int bytes = 0; int bytes = 0;
int size = 0;
switch(sb->insts[i].cmd) switch(sb->insts[i].cmd)
{ {
case SB1_INST_LOAD: case SB1_INST_LOAD:
@ -114,7 +117,8 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
bytes - sb->insts[i].size); bytes - sb->insts[i].size);
break; break;
case SB1_INST_FILL: case SB1_INST_FILL:
bytes = 4; bytes = sb->insts[i].size;
size = 2;
memcpy(cmd + 1, &sb->insts[i].pattern, 4); memcpy(cmd + 1, &sb->insts[i].pattern, 4);
cmd->addr = sb->insts[i].addr; cmd->addr = sb->insts[i].addr;
break; break;
@ -125,7 +129,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
memcpy(cmd + 1, &sb->insts[i].argument, 4); memcpy(cmd + 1, &sb->insts[i].argument, 4);
break; break;
case SB1_INST_MODE: case SB1_INST_MODE:
bytes = 4; bytes = 0;
cmd->addr = sb->insts[i].mode; cmd->addr = sb->insts[i].mode;
break; break;
case SB1_INST_SDRAM: case SB1_INST_SDRAM:
@ -137,11 +141,15 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd); bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd);
} }
/* handle most common cases */
if(size == 0)
size = ROUND_UP(bytes, 4) / 4 + 1;
cmd->cmd = SB1_MK_CMD(sb->insts[i].cmd, sb->insts[i].datatype, cmd->cmd = SB1_MK_CMD(sb->insts[i].cmd, sb->insts[i].datatype,
bytes, sb->insts[i].critical, bytes, sb->insts[i].critical,
ROUND_UP(bytes, 4) / 4 + 1); size);
cmd = (void *)cmd + 8 + ROUND_UP(bytes, 4); cmd = (void *)cmd + 4 + size * 4;
} }
/* move everything to prepare crypto marks (start at the end !) */ /* move everything to prepare crypto marks (start at the end !) */
@ -149,7 +157,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
memmove(buf + i * SECTOR_SIZE, buf + i * (SECTOR_SIZE - 4), SECTOR_SIZE - 4); memmove(buf + i * SECTOR_SIZE, buf + i * (SECTOR_SIZE - 4), SECTOR_SIZE - 4);
union xorcrypt_key_t key[2]; union xorcrypt_key_t key[2];
memcpy(key, sb->key, sizeof(sb->key)); memcpy(key, sb->key.u.xor_key, sizeof(sb->key));
void *ptr = header + 1; void *ptr = header + 1;
int offset = header->header_size; int offset = header->header_size;
for(unsigned i = 0; i < image_size / SECTOR_SIZE; i++) for(unsigned i = 0; i < image_size / SECTOR_SIZE; i++)
@ -354,7 +362,7 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u,
} }
} }
memcpy(file->key, key, sizeof(key)); memcpy(file->key.u.xor_key, key, sizeof(key));
if(!valid_key) if(!valid_key)
fatal(SB1_NO_VALID_KEY, "No valid key found\n"); fatal(SB1_NO_VALID_KEY, "No valid key found\n");

View file

@ -58,7 +58,8 @@ struct sb1_cmd_header_t
uint32_t addr; uint32_t addr;
} __attribute__((packed)); } __attribute__((packed));
#define SB1_CMD_MAX_SIZE 0x1ff8 #define SB1_CMD_MAX_LOAD_SIZE 0x1ff8
#define SB1_CMD_MAX_FILL_SIZE 0x3fff
#define SB1_CMD_SIZE(cmd) ((cmd) >> 21) #define SB1_CMD_SIZE(cmd) ((cmd) >> 21)
#define SB1_CMD_CRITICAL(cmd) !!(cmd & (1 << 20)) #define SB1_CMD_CRITICAL(cmd) !!(cmd & (1 << 20))
@ -125,7 +126,7 @@ struct sb1_file_t
struct sb1_inst_t *insts; struct sb1_inst_t *insts;
void *userdata; void *userdata;
int userdata_size; int userdata_size;
union xorcrypt_key_t key[2]; struct crypto_key_t key;
}; };
enum sb1_error_t enum sb1_error_t