Commit 88395e16 authored by Marek Vasut's avatar Marek Vasut

mxssb: Pull out the instruction filling

Implement a function that allocates space for an instructions
and fills in the fields on a per-command basis. Note that some
commands are still not implemented.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent 29c4e9ae
......@@ -376,44 +376,78 @@ static uint8_t sb_command_checksum(struct sb_command *inst)
return csum;
}
static void sb_encrypt_tag(
EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *md_ctx,
struct sb_source_entry *src,
uint8_t result[sizeof(struct sb_command)])
static int sb_tag_to_command(struct sb_source_entry *src,
struct sb_command **rcmd)
{
struct sb_command inst = {
.header = {
.checksum = 0x5a,
.tag = src->tag,
.flags = src->flags,
},
};
uint8_t *inst_ptr = (uint8_t *)&inst;
struct sb_command *cmd;
cmd = calloc(1, sizeof(*cmd));
if (!cmd)
return -ENOMEM;
cmd->header.checksum = 0x5a;
cmd->header.tag = src->tag;
cmd->header.flags = src->flags;
switch (src->tag) {
case ROM_NOP_CMD: /* UNSUPPORTED */
goto unsupp;
case ROM_TAG_CMD:
inst.tag.section_number = 0;
inst.tag.section_length = sb_get_sections_block_size() - 1;
inst.tag.section_flags = SB_SECTION_FLAG_BOOTABLE;
/* FIXME ... support multiple sections */
cmd->tag.section_number = 0;
/* FIXME ... copy this from section header! */
cmd->tag.section_length = sb_get_sections_block_size() - 1;
/* FIXME ... copy this from section header! */
cmd->tag.section_flags = SB_SECTION_FLAG_BOOTABLE;
break;
case ROM_LOAD_CMD:
inst.load.address = src->address;
inst.load.count = src->length;
inst.load.crc32 = crc32(src->payload, src->length);
cmd->load.address = src->address;
cmd->load.count = src->length;
cmd->load.crc32 = crc32(src->payload, src->length);
break;
case ROM_FILL_CMD: /* UNSUPPORTED */
goto unsupp;
case ROM_JUMP_CMD: /* UNSUPPORTED */
goto unsupp;
case ROM_CALL_CMD:
inst.call.address = src->address;
cmd->call.address = src->address;
/* FIXME ... make this configurable */
cmd->jump.argument = 0x00000000;
break;
default:
printf("Error\n");
case ROM_MODE_CMD: /* UNSUPPORTED */
goto unsupp;
}
*rcmd = cmd;
return 0;
unsupp:
free(cmd);
return -ENOTSUP;
}
static void sb_encrypt_tag(
EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *md_ctx,
struct sb_source_entry *src,
uint8_t result[sizeof(struct sb_command)])
{
struct sb_command *cmd;
int ret;
ret = sb_tag_to_command(src, &cmd);
if (ret) {
fprintf(stderr, "Failed to generate instruction!\n");
exit(ret);
}
inst.header.checksum = sb_command_checksum(&inst);
cmd->header.checksum = sb_command_checksum(cmd);
sb_aes_encrypt(cipher_ctx, (uint8_t *)cmd, (uint8_t *)cmd, sizeof(*cmd));
EVP_DigestUpdate(md_ctx, cmd, sizeof(*cmd));
sb_aes_encrypt(cipher_ctx, inst_ptr, inst_ptr, sizeof(inst));
EVP_DigestUpdate(md_ctx, inst_ptr, sizeof(inst));
memcpy(result, cmd, sizeof(*cmd));
memcpy(result, inst_ptr, sizeof(inst));
free(cmd);
}
static void sb_emit_data(uint8_t *image, size_t *offset, void *data, size_t len)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment