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) ...@@ -420,13 +420,9 @@ static int sb_create_image(struct sb_image_ctx *ictx)
return 0; return 0;
} }
static void sb_emit_data(uint8_t *image, size_t *offset, void *data, size_t len) char *output_filename = NULL;
{
memcpy(image + *offset, data, len);
*offset += len;
}
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_boot_image_header *sb_header = &ictx->payload;
struct sb_section_ctx *sctx; struct sb_section_ctx *sctx;
...@@ -434,60 +430,61 @@ static int sb_assemble_image(struct sb_image_ctx *ictx, uint8_t **image) ...@@ -434,60 +430,61 @@ static int sb_assemble_image(struct sb_image_ctx *ictx, uint8_t **image)
struct sb_command *ccmd; struct sb_command *ccmd;
struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key; struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
/* The currect offset in the SB image. */ int fd;
size_t image_offset = 0; ssize_t ret;
/* Allocate the resulting SB image. */ fd = open(output_filename, O_CREAT | O_WRONLY, 0644);
*image = malloc(sb_header->image_blocks * SB_BLOCK_SIZE); if (fd < 0)
if (!*image) return -EINVAL;
return -ENOMEM;
sb_emit_data(*image, &image_offset, sb_header, sizeof(*sb_header));
sctx = ictx->sect; ret = write(fd, sb_header, sizeof(*sb_header));
if (ret != sizeof(*sb_header))
goto err;
sctx = ictx->sect;
while (sctx) { while (sctx) {
sb_emit_data(*image, &image_offset, &sctx->payload, ret = write(fd, &sctx->payload, sizeof(struct sb_sections_header));
sizeof(struct sb_sections_header)); if (ret != sizeof(struct sb_sections_header))
goto err;
sctx = sctx->sect; 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; sctx = ictx->sect;
while (sctx) { while (sctx) {
cctx = sctx->cmd; cctx = sctx->cmd;
while (cctx) { while (cctx) {
ccmd = &cctx->payload; ccmd = &cctx->payload;
sb_emit_data(*image, &image_offset, &cctx->c_payload, ret = write(fd, &cctx->c_payload, sizeof(cctx->payload));
sizeof(cctx->payload)); if (ret != sizeof(cctx->payload))
if (ccmd->header.tag == ROM_LOAD_CMD) goto err;
sb_emit_data(*image, &image_offset, cctx->data, cctx->length);
if (ccmd->header.tag == ROM_LOAD_CMD) {
ret = write(fd, cctx->data, cctx->length);
if (ret != cctx->length)
goto err;
}
cctx = cctx->cmd; cctx = cctx->cmd;
} }
sctx = sctx->sect; sctx = sctx->sect;
}; };
sb_emit_data(*image, &image_offset, ictx->digest, sizeof(ictx->digest));
return 0; ret = write(fd, ictx->digest, sizeof(ictx->digest));
} if (ret != sizeof(ictx->digest))
goto err;
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, image, len);
if (ret != len) {
close(fd);
return -EINVAL;
}
close(fd);
return 0; return 0;
err:
fprintf(stderr, "Failed to write output file!\n");
close(fd);
return -EINVAL;
} }
static int sb_load_file(struct sb_cmd_ctx *cctx, struct sb_source_entry *src) 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) ...@@ -956,11 +953,8 @@ static int set_cpu(const char *pn, const char *cpu)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
uint8_t *image = NULL;
int image_size;
int ret; int ret;
int opt; int opt;
char *output_filename = NULL;
char *spl_filename = NULL; char *spl_filename = NULL;
char *uboot_filename = NULL; char *uboot_filename = NULL;
...@@ -1016,21 +1010,11 @@ int main(int argc, char **argv) ...@@ -1016,21 +1010,11 @@ int main(int argc, char **argv)
goto fail; goto fail;
ret = sb_create_image(&ctx); ret = sb_create_image(&ctx);
if (ret) if (!ret)
return ret; ret = sb_assemble_image(&ctx);
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);
fail: fail:
sb_free_image(&ctx); sb_free_image(&ctx);
if (image)
free(image);
return ret; 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