blob: e292ff460747461a1578ebaaad23031544410133 [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>
Elliott Hughes19269002025-11-12 20:34:01 +000022#include <unistd.h>
Edward O'Callaghan4c76c732022-08-12 11:03:00 +100023
24#ifndef __LIBPAYLOAD__
Edward O'Callaghanee3fbd72022-08-15 10:53:32 +100025#include <fcntl.h>
Edward O'Callaghan4c76c732022-08-12 11:03:00 +100026#include <sys/stat.h>
27#endif
28
29#include "flash.h"
30
31int read_buf_from_file(unsigned char *buf, unsigned long size,
32 const char *filename)
33{
34#ifdef __LIBPAYLOAD__
35 msg_gerr("Error: No file I/O support in libpayload\n");
36 return 1;
37#else
38 int ret = 0;
39
40 FILE *image;
41 if (!strcmp(filename, "-"))
42 image = fdopen(fileno(stdin), "rb");
43 else
44 image = fopen(filename, "rb");
45 if (image == NULL) {
46 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
47 return 1;
48 }
49
50 struct stat image_stat;
51 if (fstat(fileno(image), &image_stat) != 0) {
52 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
53 ret = 1;
54 goto out;
55 }
56 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
57 msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
58 (intmax_t)image_stat.st_size, size);
59 ret = 1;
60 goto out;
61 }
62
63 unsigned long numbytes = fread(buf, 1, size, image);
64 if (numbytes != size) {
65 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
66 "wanted %ld!\n", numbytes, size);
67 ret = 1;
68 }
69out:
70 (void)fclose(image);
71 return ret;
72#endif
73}
Edward O'Callaghanee3fbd72022-08-15 10:53:32 +100074
75int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
76{
77#ifdef __LIBPAYLOAD__
78 msg_gerr("Error: No file I/O support in libpayload\n");
79 return 1;
80#else
81 FILE *image;
82 int ret = 0;
83
84 if (!filename) {
85 msg_gerr("No filename specified.\n");
86 return 1;
87 }
88 if ((image = fopen(filename, "wb")) == NULL) {
89 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
90 return 1;
91 }
92
93 unsigned long numbytes = fwrite(buf, 1, size, image);
94 if (numbytes != size) {
95 msg_gerr("Error: file %s could not be written completely.\n", filename);
96 ret = 1;
97 goto out;
98 }
99 if (fflush(image)) {
100 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
101 ret = 1;
102 }
103 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
104#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
105 struct stat image_stat;
106 if (fstat(fileno(image), &image_stat) != 0) {
107 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
108 ret = 1;
109 goto out;
110 }
111 if (S_ISREG(image_stat.st_mode)) {
112 if (fsync(fileno(image))) {
113 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
114 ret = 1;
115 }
116 }
117#endif
118out:
119 if (fclose(image)) {
120 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
121 ret = 1;
122 }
123 return ret;
124#endif
125}