Use file_error function to print file related errors

Added error handling where it was missing. According to the documentation,
the fseek function returns a non-zero value on error. The conditions for
checking this value have been corrected.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
This commit is contained in:
Adrian Warecki 2023-03-07 13:37:43 +01:00 committed by pjdobrowolski
parent 32a5388c10
commit 4aacac3460
6 changed files with 110 additions and 129 deletions

View File

@ -15,6 +15,7 @@
#include "rimage/cse.h"
#include "rimage/css.h"
#include "rimage/toml_utils.h"
#include "rimage/file_utils.h"
#include "toml.h"
#include <stdbool.h>
#include <stdint.h>
@ -2348,7 +2349,8 @@ int adsp_parse_config(const char *file, struct image *image)
fd = fopen(file, "r");
if (!fd)
return log_err(-EIO, "error: can't open '%s' file\n", file);
return file_error("unable to open file for reading", file);
ret = adsp_parse_config_fd(fd, image);
fclose(fd);
return ret;

View File

@ -35,11 +35,8 @@ static int ext_man_open_file(struct image *image)
/* open extended manifest outfile for writing */
image->out_ext_man_fd = fopen(image->out_ext_man_file, "wb");
if (!image->out_ext_man_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_ext_man_file, errno);
return errno;
}
if (!image->out_ext_man_fd)
return file_error("unable to open file for writing", image->out_ext_man_file);
return 0;
}
@ -169,10 +166,7 @@ int ext_man_write(struct image *image)
count = fwrite(ext_man, 1, ext_man->full_size, image->out_ext_man_fd);
if (count != ext_man->full_size) {
fprintf(stderr,
"error: can't write extended manifest to file %d\n",
-errno);
ret = -errno;
ret = file_error("can't write extended manifest", image->out_ext_man_file);
goto out;
}
@ -195,6 +189,7 @@ int ext_man_write_cavs_25(struct image *image)
int pin_count;
int count, i;
int ret;
size_t write_ret;
ret = ext_man_open_file(image);
if (ret)
@ -213,17 +208,38 @@ int ext_man_write_cavs_25(struct image *image)
fwrite(&header, 1, sizeof(header), image->out_ext_man_fd);
for (i = 0; i < count; i++) {
fwrite(&mod_ext->ext_mod_config_array[i].header, 1,
sizeof(struct fw_ext_mod_config_header), image->out_ext_man_fd);
write_ret = fwrite(&mod_ext->ext_mod_config_array[i].header,
sizeof(struct fw_ext_mod_config_header), 1,
image->out_ext_man_fd);
if (write_ret != 1) {
ret = file_error("can't write fw_ext_mod_config_header",
image->out_ext_man_file);
goto out;
}
if (mod_ext->ext_mod_config_array[i].header.num_scheduling_capabilities)
fwrite(&mod_ext->ext_mod_config_array[i].sched_caps, 1,
sizeof(struct mod_scheduling_caps), image->out_ext_man_fd);
if (mod_ext->ext_mod_config_array[i].header.num_scheduling_capabilities) {
write_ret = fwrite(&mod_ext->ext_mod_config_array[i].sched_caps,
sizeof(struct mod_scheduling_caps), 1,
image->out_ext_man_fd);
if (write_ret != 1) {
ret = file_error("can't write mod_scheduling_caps",
image->out_ext_man_file);
goto out;
}
}
pin_count = mod_ext->ext_mod_config_array[i].header.num_pin_entries;
if (pin_count)
fwrite(mod_ext->ext_mod_config_array[i].pin_desc, pin_count,
sizeof(struct fw_pin_description), image->out_ext_man_fd);
if (pin_count) {
write_ret = fwrite(mod_ext->ext_mod_config_array[i].pin_desc,
sizeof(struct fw_pin_description), pin_count,
image->out_ext_man_fd);
if (write_ret != pin_count) {
ret = file_error("can't write fw_pin_description",
image->out_ext_man_file);
goto out;
}
}
}
out:

View File

@ -10,6 +10,7 @@
#include <rimage/rimage.h>
#include <rimage/manifest.h>
#include <rimage/file_utils.h>
static int get_mem_zone_type(struct image *image, Elf32_Shdr *section)
{
@ -68,7 +69,7 @@ static int write_block(struct image *image, struct module *module,
/* write header */
count = fwrite(&block, sizeof(block), 1, image->out_fd);
if (count != 1)
return -errno;
return file_error("Write header failed", image->out_file);
/* alloc data data */
buffer = calloc(1, section->size);
@ -77,24 +78,22 @@ static int write_block(struct image *image, struct module *module,
/* read in section data */
ret = fseek(module->fd, section->off, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "error: cant seek to section %d\n", ret);
if (ret) {
ret = file_error("seek to section failed", module->elf_file);
goto out;
}
count = fread(buffer, 1, section->size, module->fd);
if (count != section->size) {
fprintf(stderr, "error: cant read section %d\n", -errno);
ret = -errno;
ret = file_error("cant read section", module->elf_file);
goto out;
}
/* write out section data */
count = fwrite(buffer, 1, block.size, image->out_fd);
if (count != block.size) {
fprintf(stderr, "error: cant write section %d\n", -errno);
ret = file_error("cant write section", image->out_file);
fprintf(stderr, " foffset %d size 0x%x mem addr 0x%x\n",
section->off, section->size, section->vaddr);
ret = -errno;
goto out;
}
@ -128,12 +127,12 @@ 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)
return file_error("cant get file position", image->out_file);
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;
}
if (count != 1)
return file_error("failed to write section header", image->out_file);
fprintf(stdout, "\n\tTotals\tStart\t\tEnd\t\tSize");
@ -172,15 +171,21 @@ 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)
return file_error("cant get file position", image->out_file);
/* overwrite hdr */
fseek(image->out_fd, ptr_hdr, SEEK_SET);
err = fseek(image->out_fd, ptr_hdr, SEEK_SET);
if (err)
return file_error("cant seek to header", image->out_file);
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);
if (count != 1)
return file_error("failed to write section header", image->out_file);
err = fseek(image->out_fd, ptr_cur, SEEK_SET);
if (err)
return file_error("cant seek", image->out_file);
fprintf(stdout, "\n");
/* return padding size */
@ -201,7 +206,7 @@ static int write_block_reloc(struct image *image, struct module *module)
/* write header */
count = fwrite(&block, sizeof(block), 1, image->out_fd);
if (count != 1)
return -errno;
return file_error("cant write header", image->out_file);
/* alloc data data */
buffer = calloc(1, module->file_size);
@ -210,22 +215,20 @@ static int write_block_reloc(struct image *image, struct module *module)
/* read in section data */
ret = fseek(module->fd, 0, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "error: can't seek to section %d\n", ret);
if (ret) {
ret = file_error("can't seek to section", module->elf_file);
goto out;
}
count = fread(buffer, 1, module->file_size, module->fd);
if (count != module->file_size) {
fprintf(stderr, "error: can't read section %d\n", -errno);
ret = -errno;
ret = file_error("can't read section", module->elf_file);
goto out;
}
/* write out section data */
count = fwrite(buffer, 1, module->file_size, image->out_fd);
if (count != module->file_size) {
fprintf(stderr, "error: can't write section %d\n", -errno);
ret = -errno;
ret = file_error("can't write section", image->out_file);
goto out;
}
@ -249,11 +252,8 @@ static int simple_write_module_reloc(struct image *image, struct module *module)
hdr.type = SOF_FW_BASE; // module
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;
}
if (count != 1)
return file_error("failed to write section header", image->out_file);
fprintf(stdout, "\n\tTotals\tStart\t\tEnd\t\tSize");
@ -304,7 +304,7 @@ int simple_write_firmware(struct image *image)
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
if (count != 1)
return -errno;
return file_error("failed to write header", image->out_file);
for (i = 0; i < image->num_modules; i++) {
module = &image->module[i];
@ -324,10 +324,14 @@ 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)
return file_error("can't seek set", image->out_file);
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
if (count != 1)
return -errno;
return file_error("failed to write header", image->out_file);
fprintf(stdout, "firmware: image size %ld (0x%lx) bytes %d modules\n\n",
(long)(hdr.file_size + sizeof(hdr)),

View File

@ -45,11 +45,8 @@ static int man_open_rom_file(struct image *image)
/* open ROM outfile for writing */
image->out_rom_fd = fopen(image->out_rom_file, "wb");
if (!image->out_rom_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_rom_file, errno);
return -errno;
}
if (!image->out_rom_fd)
return file_error("unable to open file for writing", image->out_rom_file);
return 0;
}
@ -65,11 +62,8 @@ static int man_open_unsigned_file(struct image *image)
/* open unsigned FW outfile for writing */
image->out_unsigned_fd = fopen(image->out_unsigned_file, "wb");
if (!image->out_unsigned_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_unsigned_file, errno);
return -errno;
}
if (!image->out_unsigned_fd)
return file_error("unable to open file for writing", image->out_unsigned_file);
return 0;
}
@ -85,11 +79,8 @@ static int man_open_manifest_file(struct image *image)
/* open manifest outfile for writing */
image->out_man_fd = fopen(image->out_man_file, "wb");
if (!image->out_man_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_man_file, errno);
return -errno;
}
if (!image->out_man_fd)
return file_error("unable to open file for writing", image->out_man_file);
return 0;
}
@ -221,10 +212,8 @@ static int man_copy_sram(struct image *image, Elf32_Shdr *section,
man_module->segment[seg_type].file_offset = offset;
count = fread(buffer, 1, section->size, module->fd);
if (count != section->size) {
fprintf(stderr, "error: cant read section %d\n", -errno);
return -errno;
}
if (count != section->size)
return file_error("cant read section", module->elf_file);
/* get module end offset ? */
if (end > image->image_end)
@ -245,10 +234,8 @@ static int man_copy_elf_section(struct image *image, Elf32_Shdr *section,
/* seek to ELF section */
ret = fseek(module->fd, section->off, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "error: can't seek to section %d\n", ret);
return ret;
}
if (ret)
return file_error("can't seek to section", module->elf_file);
/* write data to DRAM or ROM image */
if (!elf_is_rom(image, section))
@ -286,10 +273,8 @@ static int man_get_module_manifest(struct image *image, struct module *module,
}
count = fread(&sof_mod, 1, sizeof(sof_mod), module->fd);
if (count != sizeof(sof_mod)) {
fprintf(stderr, "error: can't read section %d\n", -errno);
return -errno;
}
if (count != sizeof(sof_mod))
return file_error("can't read section", module->elf_file);
/* configure man_module with sofmod data */
memcpy(man_module->struct_id, "$AME", 4);
@ -576,16 +561,12 @@ static int man_module_create_reloc(struct image *image, struct module *module,
/* seek to beginning of file */
err = fseek(module->fd, 0, SEEK_SET);
if (err < 0) {
fprintf(stderr, "error: can't seek to section %d\n", err);
return err;
}
if (err)
return file_error("can't seek to section", module->elf_file);
count = fread(buffer, 1, module->file_size, module->fd);
if (count != module->file_size) {
fprintf(stderr, "error: can't read section %d\n", -errno);
return -errno;
}
if (count != module->file_size)
return file_error("can't read section", module->elf_file);
fprintf(stdout, "\t%d\t0x%8.8x\t0x%8.8zx\t0x%x\t%s\n", 0,
0, module->file_size, 0, "DATA");
@ -618,11 +599,9 @@ static int man_write_unsigned_mod(struct image *image, int meta_start_offset,
image->out_man_fd);
/* did the metadata/manifest write succeed ? */
if (count != 1) {
fprintf(stderr, "error: failed to write meta %s %d\n",
image->out_man_file, -errno);
return -errno;
}
if (count != 1)
return file_error("failed to write meta", image->out_man_file);
fclose(image->out_man_fd);
/* now prepare the unsigned rimage */
@ -631,11 +610,8 @@ static int man_write_unsigned_mod(struct image *image, int meta_start_offset,
1, image->out_unsigned_fd);
/* did the unsigned FW write succeed ? */
if (count != 1) {
fprintf(stderr, "error: failed to write firmware %s %d\n",
image->out_unsigned_file, -errno);
return -errno;
}
if (count != 1)
return file_error("failed to write firmware", image->out_unsigned_file);
fclose(image->out_unsigned_fd);
return 0;
@ -649,11 +625,8 @@ static int man_write_fw_mod(struct image *image)
count = fwrite(image->fw_image, image->image_end, 1, image->out_fd);
/* did the image write succeed ? */
if (count != 1) {
fprintf(stderr, "error: failed to write signed firmware %s %d\n",
image->out_file, -errno);
return -errno;
}
if (count != 1)
return file_error("failed to write signed firmware", image->out_file);
return 0;
}
@ -1567,11 +1540,8 @@ int verify_image(struct image *image)
/* open image for reading */
in_file = fopen(image->verify_file, "rb");
if (!in_file) {
fprintf(stderr, "error: unable to open %s for reading %d\n",
image->verify_file, errno);
return -errno;
}
if (!in_file)
return file_error("unable to open file for reading", image->verify_file);
/* get file size */
ret = get_file_size(in_file, image->verify_file, &size);
@ -1589,9 +1559,7 @@ int verify_image(struct image *image)
/* find start of fw image and verify */
read = fread(buffer, 1, size, in_file);
if (read != size) {
fprintf(stderr, "error: unable to read %ld bytes from %s err %d\n",
size, image->verify_file, errno);
ret = errno;
ret = file_error("unable to read whole file", image->verify_file);
goto out;
}
for (i = 0; i < size; i += sizeof(uint32_t)) {
@ -1622,11 +1590,8 @@ int resign_image(struct image *image)
/* open image for reading */
in_file = fopen(image->in_file, "rb");
if (!in_file) {
fprintf(stderr, "error: unable to open %s for reading %d\n",
image->in_file, errno);
return -errno;
}
if (!in_file)
return file_error("unable to open file for reading", image->in_file);
/* get file size */
ret = get_file_size(in_file, image->in_file, &size);
@ -1644,9 +1609,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",
size, image->in_file, errno);
ret = errno;
ret = file_error("unable to read whole file", image->in_file);
goto out;
}
@ -1703,9 +1666,7 @@ int resign_image(struct image *image)
unlink(image->out_file);
image->out_fd = fopen(image->out_file, "wb");
if (!image->out_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_file, errno);
ret = -EINVAL;
ret = file_error("unable to open file for writting", image->out_file);
goto out;
}

View File

@ -24,6 +24,7 @@
#include <rimage/css.h>
#include <rimage/manifest.h>
#include <rimage/misc_utils.h>
#include <rimage/file_utils.h>
#include <rimage/hash.h>
#define DEBUG_PKCS 0
@ -57,11 +58,9 @@ static int rimage_read_key(EVP_PKEY **privkey, struct image *image)
fprintf(stdout, " %s: read key '%s'\n", __func__, path);
fp = fopen(path, "rb");
if (!fp) {
fprintf(stderr, "error: can't open file %s %d\n",
path, -errno);
return -errno;
}
if (!fp)
return file_error("unable to open file for reading", path);
PEM_read_PrivateKey(fp, privkey, NULL, NULL);
fclose(fp);

View File

@ -14,6 +14,7 @@
#include <rimage/ext_manifest_gen.h>
#include <rimage/rimage.h>
#include <rimage/manifest.h>
#include <rimage/file_utils.h>
static void usage(char *name)
@ -218,9 +219,7 @@ int main(int argc, char *argv[])
unlink(image.out_file);
image.out_fd = fopen(image.out_file, "wb");
if (!image.out_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image.out_file, errno);
ret = -EINVAL;
ret = file_error("unable to open file for writing", image.out_file);
goto out;
}