aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Baryshkov <dmitry.baryshkov@linaro.org>2021-04-15 21:17:23 +0300
committerBjorn Andersson <bjorn@kryo.se>2021-04-18 21:05:17 -0700
commit3c0405c03d427d7adfc70ae25abdcfdd4107bec8 (patch)
tree85411e318697f223f8dedcfe4a11edf765e0c415
parentec6c8a034e54070a5c1c36e8d2ea08b22443ff4e (diff)
qdl: bail out with the sensible error if prog.mbn can not be opened
Rather than failing with the cryptic message, return early if prog.mbn can not be found. Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
-rw-r--r--sahara.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/sahara.c b/sahara.c
index adb03c6..27082a2 100644
--- a/sahara.c
+++ b/sahara.c
@@ -104,16 +104,12 @@ static void sahara_hello(struct qdl_device *qdl, struct sahara_pkt *pkt)
qdl_write(qdl, &resp, resp.length);
}
-static int sahara_read_common(struct qdl_device *qdl, const char *mbn, off_t offset, size_t len)
+static int sahara_read_common(struct qdl_device *qdl, int progfd, off_t offset, size_t len)
{
- int progfd;
ssize_t n;
void *buf;
int ret = 0;
- progfd = open(mbn, O_RDONLY);
- if (progfd < 0)
- return -errno;
buf = malloc(len);
if (!buf)
@@ -131,13 +127,12 @@ static int sahara_read_common(struct qdl_device *qdl, const char *mbn, off_t off
err(1, "failed to write %zu bytes to sahara", len);
free(buf);
- close(progfd);
out:
return ret;
}
-static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const char *mbn)
+static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, int prog_fd)
{
int ret;
@@ -146,12 +141,12 @@ static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const ch
printf("READ image: %d offset: 0x%x length: 0x%x\n",
pkt->read_req.image, pkt->read_req.offset, pkt->read_req.length);
- ret = sahara_read_common(qdl, mbn, pkt->read_req.offset, pkt->read_req.length);
+ ret = sahara_read_common(qdl, prog_fd, pkt->read_req.offset, pkt->read_req.length);
if (ret < 0)
errx(1, "failed to read image chunk to sahara");
}
-static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const char *mbn)
+static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, int prog_fd)
{
int ret;
@@ -160,7 +155,7 @@ static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const
printf("READ64 image: %" PRId64 " offset: 0x%" PRIx64 " length: 0x%" PRIx64 "\n",
pkt->read64_req.image, pkt->read64_req.offset, pkt->read64_req.length);
- ret = sahara_read_common(qdl, mbn, pkt->read64_req.offset, pkt->read64_req.length);
+ ret = sahara_read_common(qdl, prog_fd, pkt->read64_req.offset, pkt->read64_req.length);
if (ret < 0)
errx(1, "failed to read image chunk to sahara");
}
@@ -199,6 +194,13 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
char tmp[32];
bool done = false;
int n;
+ int prog_fd;
+
+ prog_fd = open(prog_mbn, O_RDONLY);
+ if (prog_fd < 0) {
+ fprintf(stderr, "Can not open %s: %s\n", prog_mbn, strerror(errno));
+ return -1;
+ }
while (!done) {
n = qdl_read(qdl, buf, sizeof(buf), 1000);
@@ -216,7 +218,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
sahara_hello(qdl, pkt);
break;
case 3:
- sahara_read(qdl, pkt, prog_mbn);
+ sahara_read(qdl, pkt, prog_fd);
break;
case 4:
sahara_eoi(qdl, pkt);
@@ -226,7 +228,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
done = true;
break;
case 0x12:
- sahara_read64(qdl, pkt, prog_mbn);
+ sahara_read64(qdl, pkt, prog_fd);
break;
default:
sprintf(tmp, "CMD%x", pkt->cmd);
@@ -235,5 +237,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
}
}
+ close(prog_fd);
+
return done ? 0 : -1;
}