diff options
Diffstat (limited to 'util/amdfwtool/handle_file.c')
-rw-r--r-- | util/amdfwtool/handle_file.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/util/amdfwtool/handle_file.c b/util/amdfwtool/handle_file.c index 884f1ba709..c37bb2c8eb 100644 --- a/util/amdfwtool/handle_file.c +++ b/util/amdfwtool/handle_file.c @@ -82,3 +82,86 @@ ssize_t write_from_buf_to_file(int fd, const void *buf, size_t buf_size) return buf_size; } + +ssize_t write_body(char *output, void *body_offset, ssize_t body_size) +{ + char body_name[PATH_MAX], body_tmp_name[PATH_MAX]; + int ret; + int fd; + ssize_t bytes = -1; + + /* Create a tmp file and rename it at the end so that make does not get confused + if amdfwtool is killed for some unexpected reasons. */ + ret = snprintf(body_tmp_name, sizeof(body_tmp_name), "%s%s%s", + output, BODY_FILE_SUFFIX, TMP_FILE_SUFFIX); + if (ret < 0) { + fprintf(stderr, "Error %s forming BODY tmp file name: %d\n", + strerror(errno), ret); + return -1; + } else if ((unsigned int)ret >= sizeof(body_tmp_name)) { + fprintf(stderr, "BODY File name %d > %zu\n", ret, sizeof(body_tmp_name)); + return -1; + } + + fd = open(body_tmp_name, O_RDWR | O_CREAT | O_TRUNC, 0666); + if (fd < 0) { + fprintf(stderr, "Error: Opening %s file: %s\n", body_tmp_name, strerror(errno)); + return -1; + } + + bytes = write_from_buf_to_file(fd, body_offset, body_size); + if (bytes != body_size) { + fprintf(stderr, "Error: Writing to file %s failed\n", body_tmp_name); + return -1; + } + close(fd); + + /* Rename the tmp file */ + ret = snprintf(body_name, sizeof(body_name), "%s%s", output, BODY_FILE_SUFFIX); + if (ret < 0) { + fprintf(stderr, "Error %s forming BODY file name: %d\n", strerror(errno), ret); + return -1; + } + + if (rename(body_tmp_name, body_name)) { + fprintf(stderr, "Error: renaming file %s to %s\n", body_tmp_name, body_name); + return -1; + } + + return bytes; +} + +ssize_t copy_blob(void *dest, const char *src_file, size_t room) +{ + int fd; + struct stat fd_stat; + ssize_t bytes; + + fd = open(src_file, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Error opening file: %s: %s\n", + src_file, strerror(errno)); + return -1; + } + + if (fstat(fd, &fd_stat)) { + fprintf(stderr, "fstat error: %s\n", strerror(errno)); + close(fd); + return -2; + } + + if ((size_t)fd_stat.st_size > room) { + fprintf(stderr, "Error: %s will not fit. Exiting.\n", src_file); + close(fd); + return -3; + } + + bytes = read(fd, dest, (size_t)fd_stat.st_size); + close(fd); + if (bytes != (ssize_t)fd_stat.st_size) { + fprintf(stderr, "Error while reading %s\n", src_file); + return -4; + } + + return bytes; +} |