| 48 |
48 |
|
| 49 |
49 |
int init_flash_dev(struct mbox_context *context) |
| 50 |
50 |
{ |
| 51 |
|
char *filename = get_dev_mtd(); |
|
51 |
const char *filename = context->filename; |
| 52 |
52 |
int fd, rc = 0; |
|
53 |
int mtd = 0; |
|
54 |
|
|
55 |
if(!filename) |
|
56 |
{ |
|
57 |
filename = get_dev_mtd(); |
|
58 |
mtd = 1; |
|
59 |
} |
| 53 |
60 |
|
| 54 |
61 |
if (!filename) { |
| 55 |
62 |
MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n"); |
| … |
| 69 |
76 |
context->fds[MTD_FD].fd = fd; |
| 70 |
77 |
|
| 71 |
78 |
/* Read the Flash Info */ |
| 72 |
|
if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) { |
| 73 |
|
MSG_ERR("Couldn't get information about MTD: %s\n", |
| 74 |
|
strerror(errno)); |
| 75 |
|
rc = -1; |
| 76 |
|
goto out; |
|
79 |
if (mtd) |
|
80 |
{ |
|
81 |
if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) { |
|
82 |
MSG_ERR("Couldn't get information about MTD: %s\n", |
|
83 |
strerror(errno)); |
|
84 |
rc = -1; |
|
85 |
goto out; |
|
86 |
} |
|
87 |
} |
|
88 |
else |
|
89 |
{ |
|
90 |
context->mtd_info.size = context->flash_size; |
|
91 |
/* Assume a 4K sector size. */ |
|
92 |
context->mtd_info.erasesize = 4 * 1024; |
| 77 |
93 |
} |
| 78 |
94 |
|
| 79 |
95 |
if (context->flash_size == 0) { |
| … |
| 109 |
125 |
MSG_DBG("Flash erase size: 0x%.8x\n", context->mtd_info.erasesize); |
| 110 |
126 |
|
| 111 |
127 |
out: |
| 112 |
|
free(filename); |
|
128 |
if (mtd) { |
|
129 |
free((void*)filename); |
|
130 |
} |
| 113 |
131 |
return rc; |
| 114 |
132 |
} |
| 115 |
133 |
|
| … |
| 197 |
215 |
/* Erase the previous run which just ended */ |
| 198 |
216 |
MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n", |
| 199 |
217 |
erase_info.start, erase_info.length); |
| 200 |
|
rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, |
| 201 |
|
&erase_info); |
|
218 |
|
|
219 |
|
|
220 |
if(!context->filename) |
|
221 |
{ |
|
222 |
/* MTD Device */ |
|
223 |
rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, |
|
224 |
&erase_info); |
|
225 |
} |
|
226 |
else |
|
227 |
{ |
|
228 |
uint8_t* erase_buf = (uint8_t*)malloc(erase_size); |
|
229 |
if (!erase_buf) { |
|
230 |
MSG_ERR("Couldn't malloc erase buffer. %s\n", strerror(errno)); |
|
231 |
return -MBOX_R_SYSTEM_ERROR; |
|
232 |
} |
|
233 |
|
|
234 |
memset(erase_buf, 0xFF, erase_size); |
|
235 |
rc = pwrite(context->fds[MTD_FD].fd, erase_buf, erase_info.length, erase_info.start); |
|
236 |
free(erase_buf); |
|
237 |
} |
|
238 |
|
| 202 |
239 |
if (rc < 0) { |
| 203 |
240 |
MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 204 |
241 |
erase_info.start); |
| … |
| 218 |
255 |
if (erase_info.length) { |
| 219 |
256 |
MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n", |
| 220 |
257 |
erase_info.start, erase_info.length); |
| 221 |
|
rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info); |
|
258 |
|
|
259 |
|
|
260 |
if(!context->filename) |
|
261 |
{ |
|
262 |
/* MTD Device */ |
|
263 |
rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, |
|
264 |
&erase_info); |
|
265 |
} |
|
266 |
else |
|
267 |
{ |
|
268 |
uint8_t* erase_buf = (uint8_t*)malloc(erase_size); |
|
269 |
if (!erase_buf) { |
|
270 |
MSG_ERR("Couldn't malloc erase buffer. %s\n", strerror(errno)); |
|
271 |
return -MBOX_R_SYSTEM_ERROR; |
|
272 |
} |
|
273 |
|
|
274 |
memset(erase_buf, 0xFF, erase_size); |
|
275 |
rc = pwrite(context->fds[MTD_FD].fd, erase_buf, erase_info.length, erase_info.start); |
|
276 |
free(erase_buf); |
|
277 |
} |
|
278 |
|
|
279 |
|
| 222 |
280 |
if (rc < 0) { |
| 223 |
281 |
MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 224 |
282 |
erase_info.start); |