From patchwork Mon Sep 23 23:14:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [6/6, WIP] layout: Bailout early in process_include_args() if region file is inaccessible. Date: Mon, 23 Sep 2013 23:14:06 -0000 From: Stefan Tauner X-Patchwork-Id: 4071 Message-Id: <1379978046-32403-7-git-send-email-stefan.tauner@student.tuwien.ac.at> To: flashrom@flashrom.org Signed-off-by: Stefan Tauner --- layout.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/layout.c b/layout.c index d522348..66b7d5b 100644 --- a/layout.c +++ b/layout.c @@ -19,6 +19,11 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifndef __LIBPAYLOAD__ +#include +#include +#include +#endif #include #include #include @@ -443,6 +448,25 @@ int process_include_args(void) msg_gerr("Error: No file I/O support in libpayload\n"); return 1; #else + if (access(file, R_OK) != 0) { + msg_gerr("Region file name \"%s\" seems to be unreadable: %s\n", + file, strerror(errno)); + return 1; + } + + struct stat file_stat; + if (stat((file), &file_stat) != 0) { + msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", + file, strerror(errno)); + return 1; + } + chipoff_t size = rom_entries[idx].end - rom_entries[idx].start + 1; + if (file_stat.st_size != size) { + msg_gerr("Error: Region file size (%jd B) doesn't match the region's size (%" + PRIuCHIPSIZE " B)!\n", (intmax_t)file_stat.st_size, size); + return 1; + } + file = strdup(file); if (file == NULL) { msg_gerr("Out of memory!\n");