Commit c289ba8d authored by Marek Vasut's avatar Marek Vasut
Browse files

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 {
* | |-CMD
* : :
*/
struct sb_cmd_list {
struct sb_cmd_list *next;
char *cmd;
size_t len;
};
struct sb_cmd_ctx {
unsigned int ivt:1;
......@@ -74,9 +80,8 @@ struct sb_image_ctx {
EVP_MD_CTX md_ctx;
int section_count;
uint8_t digest[32];
char **sb_input;
unsigned int sb_insts;
unsigned int sb_insts_pos;
struct sb_cmd_list *sb_cmd_list;
struct sb_cmd_list *sb_cmd_pos;
char *spl_filename;
char *uboot_filename;
......@@ -647,8 +652,8 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx)
int ret;
char *line;
for (; ictx->sb_insts_pos < ictx->sb_insts; ictx->sb_insts_pos++) {
line = strdup(ictx->sb_input[ictx->sb_insts_pos]);
while (ictx->sb_cmd_pos) {
line = strdup(ictx->sb_cmd_pos->cmd);
if (!line)
goto err;
......@@ -672,6 +677,7 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx)
prev->cmd = cctx;
prev = cctx;
free(line);
ictx->sb_cmd_pos = ictx->sb_cmd_pos->next;
}
sctx->cmd = head;
......@@ -702,15 +708,15 @@ static int sb_parse_sections(struct sb_image_ctx *ictx)
long id;
char *tok, *line;
while (ictx->sb_insts_pos < ictx->sb_insts) {
line = strdup(ictx->sb_input[ictx->sb_insts_pos]);
while (ictx->sb_cmd_pos) {
line = strdup(ictx->sb_cmd_pos->cmd);
if (!line)
goto fail;
tok = strtok(line, " ");
if (!tok || (tok[0] != 'S')) { /* SECTION */
fprintf(stderr, "ERR: Not a section, skipping!\n");
ictx->sb_insts_pos++;
ictx->sb_cmd_pos = ictx->sb_cmd_pos->next;
free(line);
continue;
}
......@@ -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
* 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);
if (ret)
goto fail;
......@@ -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)
{
char **table;
unsigned int i;
size_t flen, llen;
if (ictx->target_cpu == CPU_MX23) {
ictx->sb_insts = sizeof(sb_input_mx23)/sizeof(sb_input_mx23[0]);
ictx->sb_insts_pos = 0;
} else if (ictx->target_cpu == CPU_MX28) {
ictx->sb_insts = sizeof(sb_input_mx28)/sizeof(sb_input_mx28[0]);
ictx->sb_insts_pos = 0;
}
unsigned int i, sb_insts;
struct sb_cmd_list *head = NULL, *prev = NULL, *curr;
if (ictx->target_cpu == CPU_MX23)
sb_insts = sizeof(sb_input_mx23)/sizeof(sb_input_mx23[0]);
else if (ictx->target_cpu == CPU_MX28)
sb_insts = sizeof(sb_input_mx28)/sizeof(sb_input_mx28[0]);
for (i = 0; i < sb_insts; i++) {
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 */
if (ictx->target_cpu == CPU_MX23) {
curr->len = strlen(sb_input_mx23[i]);
if (i == 3) {
llen = strlen(sb_input_mx23[i]);
flen = strlen(ictx->spl_filename);
table[i] = calloc(1, llen + flen + 1);
if (!table[i])
return -ENOMEM;
snprintf(table[i], llen + flen + 1, "%s%s",
curr->len += strlen(ictx->spl_filename) + 1;
curr->cmd = calloc(1, curr->len);
if (!curr->cmd)
goto err;
snprintf(curr->cmd, curr->len, "%s%s",
sb_input_mx23[i], ictx->spl_filename);
} else if (i == 5) {
llen = strlen(sb_input_mx23[i]);
flen = strlen(ictx->uboot_filename);
table[i] = calloc(1, llen + flen + 1);
if (!table[i])
return -ENOMEM;
snprintf(table[i], llen + flen + 1, "%s%s",
curr->len += strlen(ictx->uboot_filename) + 1;
curr->cmd = calloc(1, curr->len);
if (!curr->cmd)
goto err;
snprintf(curr->cmd, curr->len, "%s%s",
sb_input_mx23[i], ictx->uboot_filename);
} else {
table[i] = strdup(sb_input_mx23[i]);
curr->cmd = strdup(sb_input_mx23[i]);
}
} else {
curr->len = strlen(sb_input_mx28[i]);
if (i == 3) {
llen = strlen(sb_input_mx28[i]);
flen = strlen(ictx->spl_filename);
table[i] = calloc(1, llen + flen + 1);
if (!table[i])
return -ENOMEM;
snprintf(table[i], llen + flen + 1, "%s%s",
curr->len += strlen(ictx->spl_filename) + 1;
curr->cmd = calloc(1, curr->len);
if (!curr->cmd)
goto err;
snprintf(curr->cmd, curr->len, "%s%s",
sb_input_mx28[i], ictx->spl_filename);
} else if (i == 6) {
llen = strlen(sb_input_mx28[i]);
flen = strlen(ictx->uboot_filename);
table[i] = calloc(1, llen + flen + 1);
if (!table[i])
return -ENOMEM;
snprintf(table[i], llen + flen + 1, "%s%s",
curr->len += strlen(ictx->uboot_filename) + 1;
curr->cmd = calloc(1, curr->len);
if (!curr->cmd)
goto err;
snprintf(curr->cmd, curr->len, "%s%s",
sb_input_mx28[i], ictx->uboot_filename);
} 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;
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)
......@@ -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_cmd_ctx *cctx, *c_head;
unsigned int i;
struct sb_cmd_list *cl;
while (sctx) {
s_head = sctx;
......@@ -1048,13 +1067,13 @@ static void sb_free_image(struct sb_image_ctx *ictx)
free(s_head);
}
/* Free the instruction table. */
if (ictx->sb_input) {
for (i = 0; i < ictx->sb_insts; i++) {
if (ictx->sb_input[i])
free(ictx->sb_input[i]);
}
free(ictx->sb_input);
/* Free the instruction list. */
while (ictx->sb_cmd_list) {
cl = ictx->sb_cmd_list;
ictx->sb_cmd_list = ictx->sb_cmd_list->next;
if (cl->cmd)
free(cl->cmd);
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