Commit a289a072 authored by Marek Vasut's avatar Marek Vasut

mxssb: Write the output file in chunks

Write the output file directly instead of creating a block in memory
and then writing this whole block into a file. This saves some memory
and is a bit easier.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent e86c0e8c
......@@ -420,13 +420,9 @@ static int sb_create_image(struct sb_image_ctx *ictx)
return 0;
}
static void sb_emit_data(uint8_t *image, size_t *offset, void *data, size_t len)
{
memcpy(image + *offset, data, len);
*offset += len;
}
char *output_filename = NULL;
static int sb_assemble_image(struct sb_image_ctx *ictx, uint8_t **image)
static int sb_assemble_image(struct sb_image_ctx *ictx)
{
struct sb_boot_image_header *sb_header = &ictx->payload;
struct sb_section_ctx *sctx;
......@@ -434,60 +430,61 @@ static int sb_assemble_image(struct sb_image_ctx *ictx, uint8_t **image)
struct sb_command *ccmd;
struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
/* The currect offset in the SB image. */
size_t image_offset = 0;
int fd;
ssize_t ret;
/* Allocate the resulting SB image. */
*image = malloc(sb_header->image_blocks * SB_BLOCK_SIZE);
if (!*image)
return -ENOMEM;
sb_emit_data(*image, &image_offset, sb_header, sizeof(*sb_header));
sctx = ictx->sect;
fd = open(output_filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0)
return -EINVAL;
ret = write(fd, sb_header, sizeof(*sb_header));
if (ret != sizeof(*sb_header))
goto err;
sctx = ictx->sect;
while (sctx) {
sb_emit_data(*image, &image_offset, &sctx->payload,
sizeof(struct sb_sections_header));
ret = write(fd, &sctx->payload, sizeof(struct sb_sections_header));
if (ret != sizeof(struct sb_sections_header))
goto err;
sctx = sctx->sect;
};
sb_emit_data(*image, &image_offset, sb_dict_key, sizeof(*sb_dict_key));
ret = write(fd, sb_dict_key, sizeof(*sb_dict_key));
if (ret != sizeof(*sb_dict_key))
goto err;
sctx = ictx->sect;
while (sctx) {
cctx = sctx->cmd;
while (cctx) {
ccmd = &cctx->payload;
sb_emit_data(*image, &image_offset, &cctx->c_payload,
sizeof(cctx->payload));
if (ccmd->header.tag == ROM_LOAD_CMD)
sb_emit_data(*image, &image_offset, cctx->data, cctx->length);
ret = write(fd, &cctx->c_payload, sizeof(cctx->payload));
if (ret != sizeof(cctx->payload))
goto err;
if (ccmd->header.tag == ROM_LOAD_CMD) {
ret = write(fd, cctx->data, cctx->length);
if (ret != cctx->length)
goto err;
}
cctx = cctx->cmd;
}
sctx = sctx->sect;
};
sb_emit_data(*image, &image_offset, ictx->digest, sizeof(ictx->digest));
return 0;
}
static int sb_save_image(const char *filename, uint8_t *image, int len)
{
int fd;
ssize_t ret;
fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0)
return -EINVAL;
ret = write(fd, ictx->digest, sizeof(ictx->digest));
if (ret != sizeof(ictx->digest))
goto err;
ret = write(fd, image, len);
if (ret != len) {
return 0;
err:
fprintf(stderr, "Failed to write output file!\n");
close(fd);
return -EINVAL;
}
close(fd);
return 0;
}
static int sb_load_file(struct sb_cmd_ctx *cctx, struct sb_source_entry *src)
......@@ -956,11 +953,8 @@ static int set_cpu(const char *pn, const char *cpu)
int main(int argc, char **argv)
{
uint8_t *image = NULL;
int image_size;
int ret;
int opt;
char *output_filename = NULL;
char *spl_filename = NULL;
char *uboot_filename = NULL;
......@@ -1016,21 +1010,11 @@ int main(int argc, char **argv)
goto fail;
ret = sb_create_image(&ctx);
if (ret)
return ret;
ret = sb_assemble_image(&ctx, &image);
if (ret)
return ret;
image_size = ctx.payload.image_blocks * SB_BLOCK_SIZE;
ret = sb_save_image(output_filename, image, image_size);
if (!ret)
ret = sb_assemble_image(&ctx);
fail:
sb_free_image(&ctx);
if (image)
free(image);
return ret;
}
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