From 72b9bd614faf5f7fbc674a246721b7bfd976958e Mon Sep 17 00:00:00 2001 From: Marek Vasut Date: Mon, 15 Jul 2013 00:44:11 +0200 Subject: [PATCH] mxssb: Move the command tokenizing fully into sb_tag_to_command() Move the line tokenization fully into the sb_tag_to_command(). Moreover, improve error return and reporting from this function. Signed-off-by: Marek Vasut --- mxssb.c | 121 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 48 deletions(-) diff --git a/mxssb.c b/mxssb.c index 9cb6c6d..f7711c6 100644 --- a/mxssb.c +++ b/mxssb.c @@ -477,34 +477,44 @@ static long sb_token_to_long(char *tok) return id; } -static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) +static int sb_tag_to_command(struct sb_cmd_ctx *cctx, struct sb_cmd_list *lst) { struct sb_command *cmd = &cctx->payload; - int ret; + int ret = 0; ssize_t len; char *tok; long dest; + char *line; + + line = strdup(lst->cmd); + if (!line) + return -ENOMEM; tok = strtok(line, " "); if (!tok) { /* No command on this line. */ fprintf(stderr, "WARN: No command on this line, ignoring!\n"); - return 0; + ret = 0; + goto err; } len = strlen(tok); /* Next section, return. */ - if ((len == 7) && !strncmp(tok, "SECTION", 7)) - return -ENOMSG; + if ((len == 7) && !strncmp(tok, "SECTION", 7)) { + ret = -ENOMSG; + goto err; + } if ((len != 3) && (len != 4)) { fprintf(stderr, "WARN: Unknown command!\n"); - return -EINVAL; + ret = -EINVAL; + goto err; } if ((len == 3) && (strncmp(tok, "TAG", 3) && strncmp(tok, "NOP", 3))) { fprintf(stderr, "ERR: Unknown command!\n"); - return -EINVAL; + ret = -EINVAL; + goto err; } if ((len == 4) && @@ -512,10 +522,10 @@ static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) strncmp(tok, "JUMP", 4) && strncmp(tok, "CALL", 4) && strncmp(tok, "MODE", 4))) { fprintf(stderr, "ERR: Unknown command!\n"); - return -EINVAL; + ret = -EINVAL; + goto err; } - cmd->header.checksum = 0x5a; switch (tok[0]) { @@ -536,33 +546,45 @@ static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) break; case 'L': /* ROM_LOAD_CMD */ tok = strtok(NULL, " "); - if (!tok) - return -EINVAL; + if (!tok) { + ret = -EINVAL; + goto err; + } /* Check for "IVT" flag. */ if ((strlen(tok) == 3) && !strncmp(tok, "IVT", 3)) { cctx->ivt = 1; tok = strtok(NULL, " "); - if (!tok) - return -EINVAL; + if (!tok) { + ret = -EINVAL; + goto err; + } } /* Read load destination address. */ dest = sb_token_to_long(tok); - if (dest < 0) - return -EINVAL; + if (dest < 0) { + ret = -EINVAL; + goto err; + } /* Read filename or IVT entrypoint. */ tok = strtok(NULL, " "); - if (!tok) - return -EINVAL; + if (!tok) { + ret = -EINVAL; + goto err; + } if (cctx->ivt) { struct sb_ivt_header *ivt; long ivtep = sb_token_to_long(tok); - if (ivtep < 0) - return -EINVAL; + if (ivtep < 0) { + ret = -EINVAL; + goto err; + } ivt = calloc(1, sizeof(*ivt)); - if (!ivt) - return -ENOMEM; + if (!ivt) { + ret = -ENOMEM; + goto err; + } ivt->header = SB_HAB_IVT_HEADER(sizeof(*ivt)), ivt->entry = ivtep, @@ -572,8 +594,10 @@ static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) cctx->length = sizeof(*ivt); } else { ret = sb_load_file(cctx, tok); - if (ret) - return ret; + if (ret) { + ret = ret; + goto err; + } } if (cctx->length & (SB_BLOCK_SIZE - 1)) @@ -588,32 +612,42 @@ static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) break; case 'F': /* ROM_FILL_CMD ; UNSUPPORTED */ cmd->header.tag = ROM_FILL_CMD; - goto unsupp; + ret = -ENOTSUP; + goto err; case 'J': /* ROM_JUMP_CMD ; UNSUPPORTED */ cmd->header.tag = ROM_JUMP_CMD; - goto unsupp; + ret = -ENOTSUP; + goto err; case 'C': /* ROM_CALL_CMD */ tok = strtok(NULL, " "); - if (!tok) - return -EINVAL; + if (!tok) { + ret = -EINVAL; + goto err; + } /* Check for "HAB" flag. */ if ((strlen(tok) == 3) && !strncmp(tok, "HAB", 3)) { cmd->header.flags = ROM_CALL_CMD_FLAG_HAB; tok = strtok(NULL, " "); - if (!tok) - return -EINVAL; + if (!tok) { + ret = -EINVAL; + goto err; + } } /* Read load destination address. */ dest = sb_token_to_long(tok); - if (dest < 0) - return -EINVAL; + if (dest < 0) { + ret = -EINVAL; + goto err; + } long arg = 0x0; tok = strtok(NULL, " "); if (tok) { arg = sb_token_to_long(tok); - if (arg < 0) - return -EINVAL; + if (arg < 0) { + ret = -EINVAL; + goto err; + } } cmd->header.tag = ROM_CALL_CMD; @@ -622,13 +656,13 @@ static int sb_tag_to_command(struct sb_cmd_ctx *cctx, char *line) break; case 'M': /* ROM_MODE_CMD ; UNSUPPORTED */ cmd->header.tag = ROM_MODE_CMD; - goto unsupp; + ret = -ENOTSUP; + goto err; } - return 0; - -unsupp: - return -ENOTSUP; +err: + free(line); + return ret; } static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) @@ -636,13 +670,8 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) struct sb_cmd_ctx *cctx, *prev = NULL, *head = NULL; uint32_t len = 0; int ret; - char *line; while (ictx->sb_cmd_pos) { - line = strdup(ictx->sb_cmd_pos->cmd); - if (!line) - goto err; - cctx = calloc(1, sizeof(*cctx)); if (!cctx) goto err; @@ -650,9 +679,8 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) if (!head) head = cctx; - ret = sb_tag_to_command(cctx, line); + ret = sb_tag_to_command(cctx, ictx->sb_cmd_pos); if (ret == -ENOMSG) { - free(line); free(cctx); ret = 0; break; @@ -667,7 +695,6 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) if (prev) prev->cmd = cctx; prev = cctx; - free(line); ictx->sb_cmd_pos = ictx->sb_cmd_pos->next; } @@ -676,8 +703,6 @@ static int sb_parse_cmds(struct sb_section_ctx *sctx, struct sb_image_ctx *ictx) return 0; err: - if (line) - free(line); if (cctx) free(cctx); while (head) { -- GitLab