blob: e3a4993a32f2aac921ff86b7722102fdd6b9f987 [file] [log] [blame]
Edward O'Callaghan4c76c732022-08-12 11:03:00 +10001/*
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__
Edward O'Callaghanee3fbd72022-08-15 10:53:32 +100024#include <fcntl.h>
Edward O'Callaghan4c76c732022-08-12 11:03:00 +100025#include <sys/stat.h>
26#endif
27
28#include "flash.h"
29
30int 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 flash chip's 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 }
68out:
69 (void)fclose(image);
70 return ret;
71#endif
72}
Edward O'Callaghanee3fbd72022-08-15 10:53:32 +100073
74int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
75{
76#ifdef __LIBPAYLOAD__
77 msg_gerr("Error: No file I/O support in libpayload\n");
78 return 1;
79#else
80 FILE *image;
81 int ret = 0;
82
83 if (!filename) {
84 msg_gerr("No filename specified.\n");
85 return 1;
86 }
87 if ((image = fopen(filename, "wb")) == NULL) {
88 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
89 return 1;
90 }
91
92 unsigned long numbytes = fwrite(buf, 1, size, image);
93 if (numbytes != size) {
94 msg_gerr("Error: file %s could not be written completely.\n", filename);
95 ret = 1;
96 goto out;
97 }
98 if (fflush(image)) {
99 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
100 ret = 1;
101 }
102 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
103#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
104 struct stat image_stat;
105 if (fstat(fileno(image), &image_stat) != 0) {
106 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
107 ret = 1;
108 goto out;
109 }
110 if (S_ISREG(image_stat.st_mode)) {
111 if (fsync(fileno(image))) {
112 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
113 ret = 1;
114 }
115 }
116#endif
117out:
118 if (fclose(image)) {
119 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
120 ret = 1;
121 }
122 return ret;
123#endif
124}