@@ -104,16 +104,12 @@ static void sahara_hello(struct qdl_device *qdl, struct sahara_pkt *pkt)
104
104
qdl_write (qdl , & resp , resp .length );
105
105
}
106
106
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 )
108
108
{
109
- int progfd ;
110
109
ssize_t n ;
111
110
void * buf ;
112
111
int ret = 0 ;
113
112
114
- progfd = open (mbn , O_RDONLY );
115
- if (progfd < 0 )
116
- return - errno ;
117
113
118
114
buf = malloc (len );
119
115
if (!buf )
@@ -131,13 +127,12 @@ static int sahara_read_common(struct qdl_device *qdl, const char *mbn, off_t off
131
127
err (1 , "failed to write %zu bytes to sahara" , len );
132
128
133
129
free (buf );
134
- close (progfd );
135
130
136
131
out :
137
132
return ret ;
138
133
}
139
134
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 )
141
136
{
142
137
int ret ;
143
138
@@ -146,12 +141,12 @@ static void sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const ch
146
141
printf ("READ image: %d offset: 0x%x length: 0x%x\n" ,
147
142
pkt -> read_req .image , pkt -> read_req .offset , pkt -> read_req .length );
148
143
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 );
150
145
if (ret < 0 )
151
146
errx (1 , "failed to read image chunk to sahara" );
152
147
}
153
148
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 )
155
150
{
156
151
int ret ;
157
152
@@ -160,7 +155,7 @@ static void sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const
160
155
printf ("READ64 image: %" PRId64 " offset: 0x%" PRIx64 " length: 0x%" PRIx64 "\n" ,
161
156
pkt -> read64_req .image , pkt -> read64_req .offset , pkt -> read64_req .length );
162
157
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 );
164
159
if (ret < 0 )
165
160
errx (1 , "failed to read image chunk to sahara" );
166
161
}
@@ -199,6 +194,13 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
199
194
char tmp [32 ];
200
195
bool done = false;
201
196
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
+ }
202
204
203
205
while (!done ) {
204
206
n = qdl_read (qdl , buf , sizeof (buf ), 1000 );
@@ -216,7 +218,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
216
218
sahara_hello (qdl , pkt );
217
219
break ;
218
220
case 3 :
219
- sahara_read (qdl , pkt , prog_mbn );
221
+ sahara_read (qdl , pkt , prog_fd );
220
222
break ;
221
223
case 4 :
222
224
sahara_eoi (qdl , pkt );
@@ -226,7 +228,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
226
228
done = true;
227
229
break ;
228
230
case 0x12 :
229
- sahara_read64 (qdl , pkt , prog_mbn );
231
+ sahara_read64 (qdl , pkt , prog_fd );
230
232
break ;
231
233
default :
232
234
sprintf (tmp , "CMD%x" , pkt -> cmd );
@@ -235,5 +237,7 @@ int sahara_run(struct qdl_device *qdl, char *prog_mbn)
235
237
}
236
238
}
237
239
240
+ close (prog_fd );
241
+
238
242
return done ? 0 : -1 ;
239
243
}
0 commit comments