xref: /aosp_15_r20/external/flashrom/helpers_fileio.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2009-2010 Carl-Daniel Hailfinger
5  * Copyright (C) 2013 Stefan Tauner
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <ctype.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 
23 #ifndef __LIBPAYLOAD__
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #endif
27 
28 #include "flash.h"
29 
read_buf_from_file(unsigned char * buf,unsigned long size,const char * filename)30 int read_buf_from_file(unsigned char *buf, unsigned long size,
31 		       const char *filename)
32 {
33 #ifdef __LIBPAYLOAD__
34 	msg_gerr("Error: No file I/O support in libpayload\n");
35 	return 1;
36 #else
37 	int ret = 0;
38 
39 	FILE *image;
40 	if (!strcmp(filename, "-"))
41 		image = fdopen(fileno(stdin), "rb");
42 	else
43 		image = fopen(filename, "rb");
44 	if (image == NULL) {
45 		msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
46 		return 1;
47 	}
48 
49 	struct stat image_stat;
50 	if (fstat(fileno(image), &image_stat) != 0) {
51 		msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
52 		ret = 1;
53 		goto out;
54 	}
55 	if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
56 		msg_gerr("Error: Image size (%jd B) doesn't match the expected size (%lu B)!\n",
57 			 (intmax_t)image_stat.st_size, size);
58 		ret = 1;
59 		goto out;
60 	}
61 
62 	unsigned long numbytes = fread(buf, 1, size, image);
63 	if (numbytes != size) {
64 		msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
65 			 "wanted %ld!\n", numbytes, size);
66 		ret = 1;
67 	}
68 out:
69 	(void)fclose(image);
70 	return ret;
71 #endif
72 }
73 
74 /**
75  * @brief Writes passed data buffer into a file
76  *
77  * @param buf      Buffer with data to write
78  * @param size     Size of buffer
79  * @param filename File path to write to
80  * @return 0 on success
81  */
write_buf_to_file(const unsigned char * buf,unsigned long size,const char * filename)82 int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
83 {
84 #ifdef __LIBPAYLOAD__
85 	msg_gerr("Error: No file I/O support in libpayload\n");
86 	return 1;
87 #else
88 	FILE *image;
89 	int ret = 0;
90 
91 	if (!filename) {
92 		msg_gerr("No filename specified.\n");
93 		return 1;
94 	}
95 	if ((image = fopen(filename, "wb")) == NULL) {
96 		msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
97 		return 1;
98 	}
99 
100 	unsigned long numbytes = fwrite(buf, 1, size, image);
101 	if (numbytes != size) {
102 		msg_gerr("Error: file %s could not be written completely.\n", filename);
103 		ret = 1;
104 		goto out;
105 	}
106 	if (fflush(image)) {
107 		msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
108 		ret = 1;
109 	}
110 	// Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
111 #if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
112 	struct stat image_stat;
113 	if (fstat(fileno(image), &image_stat) != 0) {
114 		msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
115 		ret = 1;
116 		goto out;
117 	}
118 	if (S_ISREG(image_stat.st_mode)) {
119 		if (fsync(fileno(image))) {
120 			msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
121 			ret = 1;
122 		}
123 	}
124 #endif
125 out:
126 	if (fclose(image)) {
127 		msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
128 		ret = 1;
129 	}
130 	return ret;
131 #endif
132 }
133