Skip to content

Commit

Permalink
rimage: fix file operations error checks
Browse files Browse the repository at this point in the history
This will fix file operations

Signed-off-by: Adrian Bonislawski <[email protected]>
  • Loading branch information
abonislawski committed Jul 10, 2024
1 parent 9d45332 commit d985247
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/ext_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const struct ext_man_header ext_man_template = {
static int ext_man_open_file(struct image *image)
{
/* open extended manifest outfile for writing */
sprintf(image->out_ext_man_file, "%s.xman", image->out_file);
snprintf(image->out_ext_man_file, sizeof(image->out_ext_man_file),
"%s.xman", image->out_file);
unlink(image->out_ext_man_file);

image->out_ext_man_fd = fopen(image->out_ext_man_file, "wb");
Expand Down
18 changes: 16 additions & 2 deletions src/file_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ static int simple_write_module(struct image *image, struct module *module)

/* Get the pointer of writing hdr */
ptr_hdr = ftell(image->out_fd);
if (ptr_hdr < 0) {
fprintf(stderr, "error: cant get file position\n");
return -errno;
}

count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
if (count != 1) {
fprintf(stderr, "error: failed to write section header %d\n",
Expand Down Expand Up @@ -180,7 +185,11 @@ static int simple_write_module(struct image *image, struct module *module)
-errno);
return -errno;
}
fseek(image->out_fd, ptr_cur, SEEK_SET);
err = fseek(image->out_fd, ptr_cur, SEEK_SET);
if (err) {
fprintf(stderr, "cant seek %d\n", -errno);
return -errno;
}

fprintf(stdout, "\n");
/* return padding size */
Expand Down Expand Up @@ -324,7 +333,12 @@ int simple_write_firmware(struct image *image)
hdr.file_size += ret;
}
/* overwrite hdr */
fseek(image->out_fd, 0, SEEK_SET);
ret = fseek(image->out_fd, 0, SEEK_SET);
if (ret) {
fprintf(stderr, "cant seek %d\n", -errno);
return -errno;
}

count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
if (count != 1)
return -errno;
Expand Down
17 changes: 10 additions & 7 deletions src/manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ static int man_open_rom_file(struct image *image)
{
uint32_t size;

sprintf(image->out_rom_file, "%s.rom", image->out_file);
snprintf(image->out_rom_file, sizeof(image->out_rom_file),
"%s.rom", image->out_file);
unlink(image->out_rom_file);

size = image->adsp->mem_zones[SOF_FW_BLK_TYPE_ROM].size;
Expand Down Expand Up @@ -67,7 +68,8 @@ static int man_open_unsigned_file(struct image *image)
static int man_open_manifest_file(struct image *image)
{
/* open manifest outfile for writing */
sprintf(image->out_man_file, "%s.met", image->out_file);
snprintf(image->out_man_file, sizeof(image->out_man_file),
"%s.met", image->out_file);
unlink(image->out_man_file);

image->out_man_fd = fopen(image->out_man_file, "wb");
Expand Down Expand Up @@ -1361,10 +1363,10 @@ int man_write_fw_v2_5(struct image *image)
int verify_image(struct image *image)
{
FILE *in_file;
int ret, i;
int ret = 0;
long size;
void *buffer;
size_t read;
size_t read, i;

/* is verify supported for target ? */
if (!image->adsp->verify_firmware) {
Expand Down Expand Up @@ -1432,15 +1434,16 @@ int verify_image(struct image *image)
image->verify_file);
out:
fclose(in_file);
return 0;
return ret;
}


int resign_image(struct image *image)
{
int key_size, key_file_size;
void *buffer = NULL;
size_t size, read;
size_t read;
int32_t size;
FILE *in_file;
int ret, i;

Expand Down Expand Up @@ -1487,7 +1490,7 @@ int resign_image(struct image *image)
/* read file into buffer */
read = fread(buffer, 1, size, in_file);
if (read != size) {
fprintf(stderr, "error: unable to read %zu bytes from %s err %d\n",
fprintf(stderr, "error: unable to read %d bytes from %s err %d\n",
size, image->in_file, errno);
ret = errno;
goto out;
Expand Down

0 comments on commit d985247

Please sign in to comment.