Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx> Acked-by: Juergen Beisert <jbe@xxxxxxxxxxxxxx> --- common/environment.c | 34 ++++++++++++++++++++++++++++++++++ include/environment.h | 1 + 2 files changed, 35 insertions(+), 0 deletions(-) diff --git a/common/environment.c b/common/environment.c index eb8278f..3270a08 100644 --- a/common/environment.c +++ b/common/environment.c @@ -93,6 +93,40 @@ out: return 1; } +#ifdef __BAREBOX__ +int file_check_and_erase(char *filename) +{ + int fd; + int ret; + + fd = open(filename, O_WRONLY | O_CREAT); + if (fd < 0) { + printf("could not open %s: %s\n", filename, errno_str()); + return errno; + } + + ret = protect(fd, ~0, 0, 0); + + /* ENOSYS is no error here, many devices do not need it */ + if (ret && errno != -ENOSYS) { + printf("could not unprotect %s: %s\n", filename, errno_str()); + return errno; + } + + ret = erase(fd, ~0, 0); + + /* ENOSYS is no error here, many devices do not need it */ + if (ret && errno != -ENOSYS) { + printf("could not erase %s: %s\n", filename, errno_str()); + close(fd); + return errno; + } + + close(fd); + return 0; +} +#endif /* __BAREBOX__ */ + /** * Make the current environment persistent * @param[in] filename where to store diff --git a/include/environment.h b/include/environment.h index 2b1f5d9..b2b95e0 100644 --- a/include/environment.h +++ b/include/environment.h @@ -58,6 +58,7 @@ int export(const char *); struct stat; int file_size_action(const char *, struct stat *, void *, int); int file_save_action(const char *, struct stat *, void *, int); +int file_check_and_erase(char *filename); #endif /* __BAREBOX__ */ -- 1.7.2.3 _______________________________________________ barebox mailing list barebox@xxxxxxxxxxxxxxxxxxx http://lists.infradead.org/mailman/listinfo/barebox