Commit 030b4fdc authored by Marek Vasut's avatar Marek Vasut
Browse files

mxssb: Consistently use stream functions



Use fopen/fclose/fread/fwrite/fseek/ftell functions to operate with
files to avoid using fdopen() and such. This also requires some data
type adjustment, which in turn removes some warnings produced by
clang analyzer.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent 60a67d8f
......@@ -360,27 +360,27 @@ static int sb_write_image(struct sb_image_ctx *ictx)
struct sb_command *ccmd;
struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
int fd;
ssize_t ret;
FILE *fp;
unsigned long ret;
fd = open(ictx->output_filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0)
fp = fopen(ictx->output_filename, "w");
if (!fp)
return -EINVAL;
ret = write(fd, sb_header, sizeof(*sb_header));
ret = fwrite(sb_header, 1, sizeof(*sb_header), fp);
if (ret != sizeof(*sb_header))
goto err;
sctx = ictx->sect_head;
while (sctx) {
ret = write(fd, &sctx->payload, sizeof(struct sb_sections_header));
ret = fwrite(&sctx->payload, 1, sizeof(struct sb_sections_header), fp);
if (ret != sizeof(struct sb_sections_header))
goto err;
sctx = sctx->sect;
};
ret = write(fd, sb_dict_key, sizeof(*sb_dict_key));
ret = fwrite(sb_dict_key, 1, sizeof(*sb_dict_key), fp);
if (ret != sizeof(*sb_dict_key))
goto err;
......@@ -390,12 +390,12 @@ static int sb_write_image(struct sb_image_ctx *ictx)
while (cctx) {
ccmd = &cctx->payload;
ret = write(fd, &cctx->c_payload, sizeof(cctx->payload));
ret = fwrite(&cctx->c_payload, 1, sizeof(cctx->payload), fp);
if (ret != sizeof(cctx->payload))
goto err;
if (ccmd->header.tag == ROM_LOAD_CMD) {
ret = write(fd, cctx->data, cctx->length);
ret = fwrite(cctx->data, 1, cctx->length, fp);
if (ret != cctx->length)
goto err;
}
......@@ -406,57 +406,65 @@ static int sb_write_image(struct sb_image_ctx *ictx)
sctx = sctx->sect;
};
ret = write(fd, ictx->digest, sizeof(ictx->digest));
ret = fwrite(ictx->digest, 1, sizeof(ictx->digest), fp);
if (ret != sizeof(ictx->digest))
goto err;
return 0;
err:
fprintf(stderr, "Failed to write output file!\n");
close(fd);
fclose(fp);
return -EINVAL;
}
static int sb_load_file(struct sb_cmd_ctx *cctx, char *filename)
{
off_t real_size, roundup_size;
long real_size, roundup_size;
uint8_t *data;
int fd, ret;
long ret;
unsigned long size;
FILE *fp;
if (!filename) {
fprintf(stderr, "ERR: Missing filename!\n");
return -EINVAL;
}
fd = open(filename, O_RDONLY);
if (fd < 0)
fp = fopen(filename, "r");
if (!fp)
goto err_open;
real_size = lseek(fd, 0, SEEK_END);
ret = fseek(fp, 0, SEEK_END);
if (ret < 0)
goto err_file;
real_size = ftell(fp);
if (real_size < 0)
goto err_file;
lseek(fd, 0, SEEK_SET);
ret = fseek(fp, 0, SEEK_SET);
if (ret < 0)
goto err_file;
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)
size = fread(data, 1, real_size, fp);
if (size != (unsigned long)real_size)
goto err_alloc;
cctx->data = data;
cctx->length = roundup_size;
close(fd);
fclose(fp);
return 0;
err_alloc:
free(data);
err_file:
close(fd);
fclose(fp);
err_open:
fprintf(stderr, "ERR: Failed to load file \"%s\"\n", filename);
return -EINVAL;
......@@ -1254,17 +1262,13 @@ static int sb_parse_line(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
static int sb_load_cmdfile(struct sb_image_ctx *ictx)
{
struct sb_cmd_list cmd;
int fd, lineno = 1;
int lineno = 1;
FILE *fp;
char *line = NULL;
ssize_t rlen;
size_t len;
fd = open(ictx->cfg_filename, O_RDONLY);
if (fd < 0)
goto err_open;
fp = fdopen(fd, "r");
fp = fopen(ictx->cfg_filename, "r");
if (!fp)
goto err_file;
......@@ -1289,7 +1293,6 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx)
err_file:
fclose(fp);
err_open:
fprintf(stderr, "ERR: Failed to load file \"%s\"\n", ictx->cfg_filename);
return -EINVAL;
......@@ -1319,7 +1322,7 @@ static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx)
}
static int sb_verify_image_header(struct sb_image_ctx *ictx,
int fd, off_t fsize)
FILE *fp, long fsize)
{
/* Verify static fields in the image header. */
struct sb_boot_image_header *hdr = &ictx->payload;
......@@ -1328,7 +1331,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
int sz, ret = 0;
unsigned char digest[20];
EVP_MD_CTX md_ctx;
ssize_t size;
unsigned long size;
/* Start image-wide crypto. */
EVP_MD_CTX_init(&ictx->md_ctx);
......@@ -1336,7 +1339,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
fprintf(stdout, "---------- Verifying SB Image Header ----------\n");
size = read(fd, &ictx->payload, sizeof(ictx->payload));
size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp);
if (size != sizeof(ictx->payload)) {
fprintf(stderr, "ERR: SB image header too short!\n");
return -EINVAL;
......@@ -1474,10 +1477,11 @@ static void sb_decrypt_tag(struct sb_image_ctx *ictx,
}
static int sb_verify_command(struct sb_image_ctx *ictx,
struct sb_cmd_ctx *cctx, int fd, ssize_t *tsize)
struct sb_cmd_ctx *cctx, FILE *fp,
unsigned long *tsize)
{
struct sb_command *ccmd = &cctx->payload;
ssize_t size;
unsigned long size;
char *csum, *flag = "";
int ret;
unsigned int i;
......@@ -1511,7 +1515,7 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
if (!cctx->data)
return -ENOMEM;
size = read(fd, cctx->data, cctx->length);
size = fread(cctx->data, 1, cctx->length, fp);
if (size != cctx->length) {
fprintf(stderr,
"ERR: SB LOAD command payload too short!\n");
......@@ -1565,9 +1569,9 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
}
static int sb_verify_commands(struct sb_image_ctx *ictx,
struct sb_section_ctx *sctx, int fd)
struct sb_section_ctx *sctx, FILE *fp)
{
ssize_t size, tsize = 0;
unsigned long size, tsize = 0;
struct sb_cmd_ctx *cctx;
int ret;
......@@ -1585,7 +1589,7 @@ static int sb_verify_commands(struct sb_image_ctx *ictx,
sctx->cmd_tail = cctx;
}
size = read(fd, &cctx->c_payload, sizeof(cctx->c_payload));
size = fread(&cctx->c_payload, 1, sizeof(cctx->c_payload), fp);
if (size != sizeof(cctx->c_payload)) {
fprintf(stderr, "ERR: SB command header too short!\n");
return -EINVAL;
......@@ -1595,7 +1599,7 @@ static int sb_verify_commands(struct sb_image_ctx *ictx,
sb_decrypt_tag(ictx, cctx);
ret = sb_verify_command(ictx, cctx, fd, &tsize);
ret = sb_verify_command(ictx, cctx, fp, &tsize);
if (ret)
return -EINVAL;
}
......@@ -1603,14 +1607,14 @@ static int sb_verify_commands(struct sb_image_ctx *ictx,
return 0;
}
static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, int fd)
static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
{
struct sb_boot_image_header *hdr = &ictx->payload;
struct sb_sections_header *shdr;
unsigned int i;
int ret;
struct sb_section_ctx *sctx;
ssize_t size;
unsigned long size;
char *bootable;
fprintf(stdout, "----- Verifying SB Sections and Commands -----\n");
......@@ -1627,7 +1631,7 @@ static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, int fd)
ictx->sect_tail = sctx;
}
size = read(fd, &sctx->payload, sizeof(sctx->payload));
size = fread(&sctx->payload, 1, sizeof(sctx->payload), fp);
if (size != sizeof(sctx->payload)) {
fprintf(stderr, "ERR: SB section header too short!\n");
return -EINVAL;
......@@ -1637,7 +1641,7 @@ static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, int fd)
uint8_t sb_section_header_cbc_mac[sizeof(struct sb_key_dictionary_key)];
struct sb_key_dictionary_key key;
size = read(fd, &key, sizeof(key));
size = fread(&key, 1, sizeof(key), fp);
if (size != sizeof(key)) {
fprintf(stderr, "ERR: SB key dictionary too short!\n");
return -EINVAL;
......@@ -1676,7 +1680,7 @@ static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, int fd)
(hdr->first_boot_section_id != shdr->section_number))
fprintf(stderr, " WARN: Bootable section does ID not match image header ID!\n");
ret = sb_verify_commands(ictx, sctx, fd);
ret = sb_verify_commands(ictx, sctx, fp);
if (ret)
goto exit;
......@@ -1692,22 +1696,22 @@ exit:
return 0;
}
static int sb_verify_image_end(struct sb_image_ctx *ictx, int fd, off_t filesz)
static int sb_verify_image_end(struct sb_image_ctx *ictx, FILE *fp, off_t filesz)
{
uint8_t digest[32];
ssize_t size;
unsigned long size;
off_t pos;
int ret;
fprintf(stdout, "------------- Verifying image end -------------\n");
size = read(fd, digest, sizeof(digest));
size = fread(digest, 1, sizeof(digest), fp);
if (size != sizeof(digest)) {
fprintf(stderr, "ERR: SB key dictionary too short!\n");
return -EINVAL;
}
pos = lseek(fd, 0, SEEK_CUR);
pos = ftell(fp);
if (pos != filesz) {
fprintf(stderr, "ERR: Trailing data past the image!\n");
return -EINVAL;
......@@ -1733,23 +1737,30 @@ static int sb_verify_image_end(struct sb_image_ctx *ictx, int fd, off_t filesz)
static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
{
off_t filesize;
int fd, ret;
long filesize;
int ret;
FILE *fp;
if (!ictx->input_filename) {
fprintf(stderr, "ERR: Missing filename!\n");
return -EINVAL;
}
fd = open(ictx->input_filename, O_RDONLY);
if (fd < 0)
fp = fopen(ictx->input_filename, "r");
if (!fp)
goto err_open;
filesize = lseek(fd, 0, SEEK_END);
ret = fseek(fp, 0, SEEK_END);
if (ret < 0)
goto err_file;
filesize = ftell(fp);
if (filesize < 0)
goto err_file;
lseek(fd, 0, SEEK_SET);
ret = fseek(fp, 0, SEEK_SET);
if (ret < 0)
goto err_file;
if (filesize < (signed)sizeof(ictx->payload)) {
fprintf(stderr, "ERR: File too short!\n");
......@@ -1757,16 +1768,16 @@ static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
}
/* Load and verify image header */
ret = sb_verify_image_header(ictx, fd, filesize);
ret = sb_verify_image_header(ictx, fp, filesize);
if (ret)
goto err_verify;
/* Load and verify sections and commands */
ret = sb_verify_sections_cmds(ictx, fd);
ret = sb_verify_sections_cmds(ictx, fp);
if (ret)
goto err_verify;
ret = sb_verify_image_end(ictx, fd, filesize);
ret = sb_verify_image_end(ictx, fp, filesize);
if (ret)
goto err_verify;
......@@ -1779,11 +1790,11 @@ err_verify:
/* Stop the encryption session. */
sb_aes_deinit(&ictx->cipher_ctx);
close(fd);
fclose(fp);
return ret;
err_file:
close(fd);
fclose(fp);
err_open:
fprintf(stderr, "ERR: Failed to load file \"%s\"\n", ictx->input_filename);
return -EINVAL;
......
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