1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-26 22:52:34 +02:00
flashrom/processor_enable.c
Stefan Tauner 1668770c6f Rigorously check integrity of I/O stream data
Even if fwrite() succeeds the data is not necessarily out of the clib's buffers
and writing it eventually could fail. Even if the data is flushed out (explicitly by
fflush() or implicitly by fclose()) the kernel might still hold a buffer.

Previously we have ignored this to a large extent - even in important cases
like writing the flash contents to a file. The results can be truncated
images that would brick the respective machine if written back as is (though
flashrom would not allow that due to a size mismatch). flashrom would not
indicate the problem in any output - so far we only check the return value
of fwrite() that is not conclusive.

This patch checks the return values of all related system calls like fclose()
unless we only read the file and are not really interested in output errors.
In the latter case the return value is casted to void to document this fact.
Additionally, this patch explicitly calls fflush() and fsync() (on regular files only)
to do the best we can to guarantee the read image reaches the disk safely
and at least inform the user if it did not work.

Corresponding to flashrom svn r1902.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Urja Rannikko <urjaman@gmail.com>
2015-12-25 21:59:45 +00:00

89 lines
2.5 KiB
C

/*
* This file is part of the flashrom project.
*
* Copyright (C) 2010 Carl-Daniel Hailfinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Contains the processor specific flash enables and system settings.
*/
#include "flash.h"
#include "programmer.h"
#if defined (__MIPSEL__) && defined (__linux)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
static int is_loongson(void)
{
FILE *cpuinfo;
cpuinfo = fopen("/proc/cpuinfo", "rb");
if (!cpuinfo)
return 0;
while (!feof(cpuinfo)) {
char line[512], *ptr;
if (fgets(line, sizeof(line), cpuinfo) == NULL)
break;
ptr = line;
while (*ptr && isspace((unsigned char)*ptr))
ptr++;
/* "cpu" part appears only with some Linux versions. */
if (strncmp(ptr, "cpu", strlen("cpu")) == 0)
ptr += strlen("cpu");
while (*ptr && isspace((unsigned char)*ptr))
ptr++;
if (strncmp(ptr, "model", strlen("model")) != 0)
continue;
ptr += strlen("model");
while (*ptr && isspace((unsigned char)*ptr))
ptr++;
if (*ptr != ':')
continue;
ptr++;
while (*ptr && isspace((unsigned char)*ptr))
ptr++;
(void)fclose(cpuinfo);
return (strncmp(ptr, "ICT Loongson-2 V0.3", strlen("ICT Loongson-2 V0.3")) == 0) ||
(strncmp(ptr, "Godson2 V0.3 FPU V0.1", strlen("Godson2 V0.3 FPU V0.1")) == 0);
}
(void)fclose(cpuinfo);
return 0;
}
#endif
int processor_flash_enable(void)
{
/* Default to 1 to catch not implemented architectures. */
int ret = 1;
/* FIXME: detect loongson on FreeBSD and OpenBSD as well. */
#if defined (__MIPSEL__) && defined (__linux)
if (is_loongson()) {
flashbase = 0x1fc00000;
ret = 0;
}
#elif defined(__i386__) || defined(__x86_64__)
/* On x86, flash access is not processor specific except on
* AMD Elan SC520, AMD Geode and maybe other SoC-style CPUs.
* FIXME: Move enable_flash_cs5536 and get_flashbase_sc520 here.
*/
ret = 0;
#endif
return ret;
}