Skip to content

Commit 3c0405c

Browse files
lumagandersson
authored andcommitted
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 <[email protected]>
1 parent ec6c8a0 commit 3c0405c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

sahara.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,12 @@ static void sahara_hello(struct qdl_device *qdl, struct sahara_pkt *pkt)
104104
qdl_write(qdl, &resp, resp.length);
105105
}
106106

107-
static int sahara_read_common(struct qdl_device *qdl, const char *mbn, off_t offset, size_t len)
107+
static int sahara_read_common(struct qdl_device *qdl, int progfd, off_t offset, size_t len)
108108
{
109-
int progfd;
110109
ssize_t n;
111110
void *buf;
112111
int ret = 0;
113112

114-
progfd = open(mbn, O_RDONLY);
115-
if (progfd < 0)
116-
return -errno;
117113

118114
buf = malloc(len);
119115
if (!buf)
@@ -131,13 +127,12 @@ static int sahara_read_common(struct qdl_device *qdl, const char *mbn, off_t off
131127
err(1, "failed to write %zu bytes to sahara", len);
132128

133129
free(buf);
134-
close(progfd);
135130

136131
out:
137132
return ret;
138133
}
139134

140-
static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const char *mbn)
135+
static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, int prog_fd)
141136
{
142137
int ret;
143138

@@ -146,12 +141,12 @@ static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const ch
146141
printf("READ image: %d offset: 0x%x length: 0x%x\n",
147142
pkt->read_req.image, pkt->read_req.offset, pkt->read_req.length);
148143

149-
ret = sahara_read_common(qdl, mbn, pkt->read_req.offset, pkt->read_req.length);
144+
ret = sahara_read_common(qdl, prog_fd, pkt->read_req.offset, pkt->read_req.length);
150145
if (ret < 0)
151146
errx(1, "failed to read image chunk to sahara");
152147
}
153148

154-
static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const char *mbn)
149+
static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, int prog_fd)
155150
{
156151
int ret;
157152

@@ -160,7 +155,7 @@ static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const
160155
printf("READ64 image: %" PRId64 " offset: 0x%" PRIx64 " length: 0x%" PRIx64 "\n",
161156
pkt->read64_req.image, pkt->read64_req.offset, pkt->read64_req.length);
162157

163-
ret = sahara_read_common(qdl, mbn, pkt->read64_req.offset, pkt->read64_req.length);
158+
ret = sahara_read_common(qdl, prog_fd, pkt->read64_req.offset, pkt->read64_req.length);
164159
if (ret < 0)
165160
errx(1, "failed to read image chunk to sahara");
166161
}
@@ -199,6 +194,13 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
199194
char tmp[32];
200195
bool done = false;
201196
int n;
197+
int prog_fd;
198+
199+
prog_fd = open(prog_mbn, O_RDONLY);
200+
if (prog_fd < 0) {
201+
fprintf(stderr, "Can not open %s: %s\n", prog_mbn, strerror(errno));
202+
return -1;
203+
}
202204

203205
while (!done) {
204206
n = qdl_read(qdl, buf, sizeof(buf), 1000);
@@ -216,7 +218,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
216218
sahara_hello(qdl, pkt);
217219
break;
218220
case 3:
219-
sahara_read(qdl, pkt, prog_mbn);
221+
sahara_read(qdl, pkt, prog_fd);
220222
break;
221223
case 4:
222224
sahara_eoi(qdl, pkt);
@@ -226,7 +228,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
226228
done = true;
227229
break;
228230
case 0x12:
229-
sahara_read64(qdl, pkt, prog_mbn);
231+
sahara_read64(qdl, pkt, prog_fd);
230232
break;
231233
default:
232234
sprintf(tmp, "CMD%x", pkt->cmd);
@@ -235,5 +237,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
235237
}
236238
}
237239

240+
close(prog_fd);
241+
238242
return done ? 0 : -1;
239243
}

0 commit comments

Comments
 (0)