1 /* SPDX-License-Identifier: GPL-2.0-only */
12 #include "amdfwtool.h"
14 void write_or_fail(int fd
, void *ptr
, size_t size
)
18 written
= write_from_buf_to_file(fd
, ptr
, size
);
19 if (written
< 0 || (size_t)written
!= size
) {
20 fprintf(stderr
, "%s: Error writing %zu bytes - written %zd bytes\n",
21 __func__
, size
, written
);
26 ssize_t
read_from_file_to_buf(int fd
, void *buf
, size_t buf_size
)
29 size_t total_bytes
= 0;
32 bytes
= read(fd
, buf
+ total_bytes
, buf_size
- total_bytes
);
34 fprintf(stderr
, "Reached EOF probably\n");
38 if (bytes
< 0 && errno
== EAGAIN
)
42 fprintf(stderr
, "Read failure %s\n", strerror(errno
));
47 } while (total_bytes
< buf_size
);
49 if (total_bytes
!= buf_size
) {
50 fprintf(stderr
, "Read data size(%zu) != buffer size(%zu)\n",
51 total_bytes
, buf_size
);
57 ssize_t
write_from_buf_to_file(int fd
, const void *buf
, size_t buf_size
)
60 size_t total_bytes
= 0;
63 bytes
= write(fd
, buf
+ total_bytes
, buf_size
- total_bytes
);
64 if (bytes
< 0 && errno
== EAGAIN
)
68 fprintf(stderr
, "Write failure %s\n", strerror(errno
));
69 lseek(fd
, SEEK_CUR
, -total_bytes
);
74 } while (total_bytes
< buf_size
);
76 if (total_bytes
!= buf_size
) {
77 fprintf(stderr
, "Wrote more data(%zu) than buffer size(%zu)\n",
78 total_bytes
, buf_size
);
79 lseek(fd
, SEEK_CUR
, -total_bytes
);
86 ssize_t
write_body(char *output
, void *body_offset
, ssize_t body_size
)
88 char body_name
[PATH_MAX
], body_tmp_name
[PATH_MAX
];
93 /* Create a tmp file and rename it at the end so that make does not get confused
94 if amdfwtool is killed for some unexpected reasons. */
95 ret
= snprintf(body_tmp_name
, sizeof(body_tmp_name
), "%s%s%s",
96 output
, BODY_FILE_SUFFIX
, TMP_FILE_SUFFIX
);
98 fprintf(stderr
, "Error %s forming BODY tmp file name: %d\n",
99 strerror(errno
), ret
);
101 } else if ((unsigned int)ret
>= sizeof(body_tmp_name
)) {
102 fprintf(stderr
, "BODY File name %d > %zu\n", ret
, sizeof(body_tmp_name
));
106 fd
= open(body_tmp_name
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
108 fprintf(stderr
, "Error: Opening %s file: %s\n", body_tmp_name
, strerror(errno
));
112 bytes
= write_from_buf_to_file(fd
, body_offset
, body_size
);
113 if (bytes
!= body_size
) {
114 fprintf(stderr
, "Error: Writing to file %s failed\n", body_tmp_name
);
119 /* Rename the tmp file */
120 ret
= snprintf(body_name
, sizeof(body_name
), "%s%s", output
, BODY_FILE_SUFFIX
);
122 fprintf(stderr
, "Error %s forming BODY file name: %d\n", strerror(errno
), ret
);
126 if (rename(body_tmp_name
, body_name
)) {
127 fprintf(stderr
, "Error: renaming file %s to %s\n", body_tmp_name
, body_name
);
134 ssize_t
copy_blob(void *dest
, const char *src_file
, size_t room
)
140 fd
= open(src_file
, O_RDONLY
);
142 fprintf(stderr
, "Error opening file: %s: %s\n",
143 src_file
, strerror(errno
));
147 if (fstat(fd
, &fd_stat
)) {
148 fprintf(stderr
, "fstat error: %s\n", strerror(errno
));
153 if ((size_t)fd_stat
.st_size
> room
) {
154 fprintf(stderr
, "Error: %s will not fit. Exiting.\n", src_file
);
159 bytes
= read(fd
, dest
, (size_t)fd_stat
.st_size
);
161 if (bytes
!= (ssize_t
)fd_stat
.st_size
) {
162 fprintf(stderr
, "Error while reading %s\n", src_file
);