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 11, 2024
1 parent 9d45332 commit d287016
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 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
29 changes: 26 additions & 3 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 @@ -172,15 +177,28 @@ static int simple_write_module(struct image *image, struct module *module)
hdr.size += padding;
/* Record current pointer, will set it back after overwriting hdr */
ptr_cur = ftell(image->out_fd);
if (ptr_cur < 0) {
fprintf(stderr, "error: cant get file position\n");
return -errno;
}
/* overwrite hdr */
fseek(image->out_fd, ptr_hdr, SEEK_SET);
err = fseek(image->out_fd, ptr_hdr, SEEK_SET);
if (err) {
fprintf(stderr, "cant seek %d\n", -errno);
return -errno;
}

count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
if (count != 1) {
fprintf(stderr, "error: failed to write section header %d\n",
-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 +342,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
20 changes: 12 additions & 8 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 All @@ -50,7 +51,8 @@ static int man_open_rom_file(struct image *image)

static int man_open_unsigned_file(struct image *image)
{
sprintf(image->out_unsigned_file, "%s.uns", image->out_file);
snprintf(image->out_unsigned_file, sizeof(image->out_unsigned_file),
"%s.uns", image->out_file);
unlink(image->out_unsigned_file);

/* open unsigned FW outfile for writing */
Expand All @@ -67,7 +69,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 +1364,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 +1435,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 +1491,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
3 changes: 2 additions & 1 deletion src/rimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ int main(int argc, char *argv[])

if (image.in_file) {
fprintf(stdout, "going to re-sign\n");
return resign_image(&image);
ret = resign_image(&image);
goto out;
}

/* set IMR Type in found machine definition */
Expand Down

0 comments on commit d287016

Please sign in to comment.