summaryrefslogtreecommitdiffstats
path: root/common/environment.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2014-04-08 17:32:05 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2014-04-09 17:00:37 +0200
commit55fb660e0b772553e3303fb48469a32185dc9888 (patch)
tree156a2308977b525a5c2329f7cdf7d186cc596c4e /common/environment.c
parentaa49d86b2d80687c36897c3b9f91954d8f1ec9b0 (diff)
downloadbarebox-55fb660e0b772553e3303fb48469a32185dc9888.tar.gz
barebox-55fb660e0b772553e3303fb48469a32185dc9888.tar.xz
loadenv: detect truncated environment files
Properly detect when an environment file is truncated. This can happen when a previous saveenv failed because the environment partition is too small. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/environment.c')
-rw-r--r--common/environment.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/common/environment.c b/common/environment.c
index c834902727..f2ac17d5d6 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -388,10 +388,10 @@ int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags)
int envfs_load(const char *filename, const char *dir, unsigned flags)
{
struct envfs_super super;
- void *buf = NULL;
+ void *buf = NULL, *rbuf;
int envfd;
int ret = 0;
- size_t size;
+ size_t size, rsize;
envfd = open(filename, O_RDONLY);
if (envfd < 0) {
@@ -412,11 +412,28 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
goto out;
buf = xmalloc(size);
- ret = read(envfd, buf, size);
- if (ret < size) {
- perror("read");
- ret = -errno;
- goto out;
+
+ rbuf = buf;
+ rsize = size;
+
+ while (rsize) {
+ ssize_t now;
+
+ now = read(envfd, rbuf, rsize);
+ if (now < 0) {
+ perror("read");
+ ret = -errno;
+ goto out;
+ }
+
+ if (!now) {
+ printf("%s: premature end of file\n", filename);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ rbuf += now;
+ rsize -= now;
}
ret = envfs_check_data(&super, buf, size);