Commit c289ba8d authored by Marek Vasut's avatar Marek Vasut

mxssb: Convert the command table into linked list

When loading the command table, it is much more beneficial to
load it as a linked list, especially since we will be soon
loading the table from a file.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent 90a1203d
...@@ -43,6 +43,12 @@ enum sb_target_cpu { ...@@ -43,6 +43,12 @@ enum sb_target_cpu {
* | |-CMD * | |-CMD
* : : * : :
*/ */
struct sb_cmd_list {
struct sb_cmd_list *next;
char *cmd;
size_t len;
};
struct sb_cmd_ctx { struct sb_cmd_ctx {
unsigned int ivt:1; unsigned int ivt:1;
...@@ -74,9 +80,8 @@ struct sb_image_ctx { ...@@ -74,9 +80,8 @@ struct sb_image_ctx {
EVP_MD_CTX md_ctx; EVP_MD_CTX md_ctx;
int section_count; int section_count;
uint8_t digest[32]; uint8_t digest[32];
char **sb_input; struct sb_cmd_list *sb_cmd_list;
unsigned int sb_insts; struct sb_cmd_list *sb_cmd_pos;
unsigned int sb_insts_pos;
char *spl_filename; char *spl_filename;
char *uboot_filename; char *uboot_filename;
...@@ -647,8 +652,8 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) ...@@ -647,8 +652,8 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx)
int ret; int ret;
char *line; char *line;
for (; ictx->sb_insts_pos < ictx->sb_insts; ictx->sb_insts_pos++) { while (ictx->sb_cmd_pos) {
line = strdup(ictx->sb_input[ictx->sb_insts_pos]); line = strdup(ictx->sb_cmd_pos->cmd);
if (!line) if (!line)
goto err; goto err;
...@@ -672,6 +677,7 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) ...@@ -672,6 +677,7 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx)
prev->cmd = cctx; prev->cmd = cctx;
prev = cctx; prev = cctx;
free(line); free(line);
ictx->sb_cmd_pos = ictx->sb_cmd_pos->next;
} }
sctx->cmd = head; sctx->cmd = head;
...@@ -702,15 +708,15 @@ static int sb_parse_sections(struct sb_image_ctx *ictx) ...@@ -702,15 +708,15 @@ static int sb_parse_sections(struct sb_image_ctx *ictx)
long id; long id;
char *tok, *line; char *tok, *line;
while (ictx->sb_insts_pos < ictx->sb_insts) { while (ictx->sb_cmd_pos) {
line = strdup(ictx->sb_input[ictx->sb_insts_pos]); line = strdup(ictx->sb_cmd_pos->cmd);
if (!line) if (!line)
goto fail; goto fail;
tok = strtok(line, " "); tok = strtok(line, " ");
if (!tok || (tok[0] != 'S')) { /* SECTION */ if (!tok || (tok[0] != 'S')) { /* SECTION */
fprintf(stderr, "ERR: Not a section, skipping!\n"); fprintf(stderr, "ERR: Not a section, skipping!\n");
ictx->sb_insts_pos++; ictx->sb_cmd_pos = ictx->sb_cmd_pos->next;
free(line); free(line);
continue; continue;
} }
...@@ -735,7 +741,7 @@ static int sb_parse_sections(struct sb_image_ctx *ictx) ...@@ -735,7 +741,7 @@ static int sb_parse_sections(struct sb_image_ctx *ictx)
* Preincrement it here so the sb_parse_cmd() starts with the * Preincrement it here so the sb_parse_cmd() starts with the
* first command entry in the section. * first command entry in the section.
*/ */
ictx->sb_insts_pos++; ictx->sb_cmd_pos = ictx->sb_cmd_pos->next;
ret = sb_parse_cmds(sctx, ictx); ret = sb_parse_cmds(sctx, ictx);
if (ret) if (ret)
goto fail; goto fail;
...@@ -933,69 +939,82 @@ static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx) ...@@ -933,69 +939,82 @@ static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx)
static int sb_load_cmdfile(struct sb_image_ctx *ictx) static int sb_load_cmdfile(struct sb_image_ctx *ictx)
{ {
char **table; unsigned int i, sb_insts;
unsigned int i; struct sb_cmd_list *head = NULL, *prev = NULL, *curr;
size_t flen, llen;
if (ictx->target_cpu == CPU_MX23)
if (ictx->target_cpu == CPU_MX23) { sb_insts = sizeof(sb_input_mx23)/sizeof(sb_input_mx23[0]);
ictx->sb_insts = sizeof(sb_input_mx23)/sizeof(sb_input_mx23[0]); else if (ictx->target_cpu == CPU_MX28)
ictx->sb_insts_pos = 0; sb_insts = sizeof(sb_input_mx28)/sizeof(sb_input_mx28[0]);
} else if (ictx->target_cpu == CPU_MX28) {
ictx->sb_insts = sizeof(sb_input_mx28)/sizeof(sb_input_mx28[0]); for (i = 0; i < sb_insts; i++) {
ictx->sb_insts_pos = 0; curr = calloc(1, sizeof(*curr));
} if (!curr)
goto err;
if (!head)
head = curr;
table = calloc(1, ictx->sb_insts * sizeof(*table));
if (!table)
return -ENOMEM;
for (i = 0; i < ictx->sb_insts; i++) {
/* Compat -- patch in the file names */ /* Compat -- patch in the file names */
if (ictx->target_cpu == CPU_MX23) { if (ictx->target_cpu == CPU_MX23) {
curr->len = strlen(sb_input_mx23[i]);
if (i == 3) { if (i == 3) {
llen = strlen(sb_input_mx23[i]); curr->len += strlen(ictx->spl_filename) + 1;
flen = strlen(ictx->spl_filename); curr->cmd = calloc(1, curr->len);
table[i] = calloc(1, llen + flen + 1); if (!curr->cmd)
if (!table[i]) goto err;
return -ENOMEM; snprintf(curr->cmd, curr->len, "%s%s",
snprintf(table[i], llen + flen + 1, "%s%s",
sb_input_mx23[i], ictx->spl_filename); sb_input_mx23[i], ictx->spl_filename);
} else if (i == 5) { } else if (i == 5) {
llen = strlen(sb_input_mx23[i]); curr->len += strlen(ictx->uboot_filename) + 1;
flen = strlen(ictx->uboot_filename); curr->cmd = calloc(1, curr->len);
table[i] = calloc(1, llen + flen + 1); if (!curr->cmd)
if (!table[i]) goto err;
return -ENOMEM; snprintf(curr->cmd, curr->len, "%s%s",
snprintf(table[i], llen + flen + 1, "%s%s",
sb_input_mx23[i], ictx->uboot_filename); sb_input_mx23[i], ictx->uboot_filename);
} else { } else {
table[i] = strdup(sb_input_mx23[i]); curr->cmd = strdup(sb_input_mx23[i]);
} }
} else { } else {
curr->len = strlen(sb_input_mx28[i]);
if (i == 3) { if (i == 3) {
llen = strlen(sb_input_mx28[i]); curr->len += strlen(ictx->spl_filename) + 1;
flen = strlen(ictx->spl_filename); curr->cmd = calloc(1, curr->len);
table[i] = calloc(1, llen + flen + 1); if (!curr->cmd)
if (!table[i]) goto err;
return -ENOMEM; snprintf(curr->cmd, curr->len, "%s%s",
snprintf(table[i], llen + flen + 1, "%s%s",
sb_input_mx28[i], ictx->spl_filename); sb_input_mx28[i], ictx->spl_filename);
} else if (i == 6) { } else if (i == 6) {
llen = strlen(sb_input_mx28[i]); curr->len += strlen(ictx->uboot_filename) + 1;
flen = strlen(ictx->uboot_filename); curr->cmd = calloc(1, curr->len);
table[i] = calloc(1, llen + flen + 1); if (!curr->cmd)
if (!table[i]) goto err;
return -ENOMEM; snprintf(curr->cmd, curr->len, "%s%s",
snprintf(table[i], llen + flen + 1, "%s%s",
sb_input_mx28[i], ictx->uboot_filename); sb_input_mx28[i], ictx->uboot_filename);
} else { } else {
table[i] = strdup(sb_input_mx28[i]); curr->cmd = strdup(sb_input_mx28[i]);
} }
} }
if (prev)
prev->next = curr;
prev = curr;
} }
ictx->sb_input = table; ictx->sb_cmd_list = head;
ictx->sb_cmd_pos = ictx->sb_cmd_list;
return 0; return 0;
err:
while (head) {
curr = head;
head = head->next;
if (curr->cmd)
free(curr->cmd);
free(curr);
}
return -ENOMEM;
} }
static int sb_parse_image(struct sb_image_ctx *ictx) static int sb_parse_image(struct sb_image_ctx *ictx)
...@@ -1030,7 +1049,7 @@ static void sb_free_image(struct sb_image_ctx *ictx) ...@@ -1030,7 +1049,7 @@ static void sb_free_image(struct sb_image_ctx *ictx)
{ {
struct sb_section_ctx *sctx = ictx->sect, *s_head; struct sb_section_ctx *sctx = ictx->sect, *s_head;
struct sb_cmd_ctx *cctx, *c_head; struct sb_cmd_ctx *cctx, *c_head;
unsigned int i; struct sb_cmd_list *cl;
while (sctx) { while (sctx) {
s_head = sctx; s_head = sctx;
...@@ -1048,13 +1067,13 @@ static void sb_free_image(struct sb_image_ctx *ictx) ...@@ -1048,13 +1067,13 @@ static void sb_free_image(struct sb_image_ctx *ictx)
free(s_head); free(s_head);
} }
/* Free the instruction table. */ /* Free the instruction list. */
if (ictx->sb_input) { while (ictx->sb_cmd_list) {
for (i = 0; i < ictx->sb_insts; i++) { cl = ictx->sb_cmd_list;
if (ictx->sb_input[i]) ictx->sb_cmd_list = ictx->sb_cmd_list->next;
free(ictx->sb_input[i]); if (cl->cmd)
} free(cl->cmd);
free(ictx->sb_input); free(cl);
} }
} }
......
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