Commit 0044795e authored by Marek Vasut's avatar Marek Vasut

mxssb: Load file while preparing the SB image header

This patch loads a file associated with a LOAD command and puts
it correctly into the tree-like structure representing the SB image.
Note that the loading operation is now done twice, but this will be
fixed shortly.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent f5fbc7c8
......@@ -42,6 +42,8 @@ enum {
* : :
*/
struct sb_cmd_ctx {
unsigned int ivt:1;
struct sb_cmd_ctx *cmd;
uint8_t *data;
......@@ -558,6 +560,7 @@ static int sb_load_binary(const char *filename, const unsigned int spl)
lst[slot].payload = data;
lst[slot].length = roundup_size;
lst[slot].filename = (char *)filename;
close(fd);
return 0;
......@@ -628,6 +631,51 @@ static int set_cpu(const char *pn, const char *cpu)
return -1;
}
static int sb_load_file(struct sb_cmd_ctx *cctx, struct sb_source_entry *src)
{
off_t real_size, roundup_size;
uint8_t *data;
int fd, ret;
if (!src->filename) {
fprintf(stderr, "ERR: Missing filename!\n");
return -EINVAL;
}
fd = open(src->filename, O_RDONLY);
if (fd < 0)
goto err_open;
real_size = lseek(fd, 0, SEEK_END);
if (real_size < 0)
goto err_file;
lseek(fd, 0, SEEK_SET);
roundup_size = roundup(real_size, SB_BLOCK_SIZE);
data = calloc(1, roundup_size);
if (!data)
goto err_file;
ret = read(fd, data, real_size);
if (ret != real_size)
goto err_alloc;
cctx->data = data;
cctx->length = roundup_size;
close(fd);
return 0;
err_alloc:
free(data);
err_file:
close(fd);
err_open:
fprintf(stderr, "ERR: Failed to load file \"%s\"\n", src->filename);
return -EINVAL;
}
static int sb_parse_cmds(struct sb_section_ctx *sctx)
{
struct sb_cmd_ctx *cctx, *prev = NULL, *head = NULL;
......@@ -653,8 +701,15 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx)
/* FIXME -- this is where the file loading will happen. */
if (src->tag == ROM_LOAD_CMD) {
cctx->data = src->payload;
cctx->length = src->length;
if (src->filename) {
ret = sb_load_file(cctx, src);
if (ret)
goto err;
} else { /* IVT -- FIXME */
cctx->ivt = 1;
cctx->data = src->payload;
cctx->length = src->length;
}
}
if (prev)
......@@ -853,6 +908,8 @@ static void sb_free_image(struct sb_image_ctx *ictx)
while (c_head) {
cctx = c_head;
c_head = c_head->cmd;
if (cctx->data && !cctx->ivt)
free(cctx->data);
free(cctx);
}
......@@ -863,7 +920,7 @@ static void sb_free_image(struct sb_image_ctx *ictx)
int main(int argc, char **argv)
{
uint8_t *image;
uint8_t *image = NULL;
int image_size;
int ret;
int opt;
......@@ -940,7 +997,8 @@ fail:
sb_unload_binary(1);
sb_unload_binary(0);
free(image);
if (image)
free(image);
return ret;
}
......@@ -175,6 +175,7 @@ struct sb_source_entry {
uint8_t tag;
uint32_t address;
uint32_t flags;
char *filename;
uint8_t *payload;
uint32_t length;
};
......
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