spacepaste

a/mbox.h
b/mbox.h
151 151 bool current_is_write;
152 152
153 153 /* Memory & Flash State */
154 /* Backing file */
155 const char* filename;
154 156 /* Reserved Memory Region */
155 157 void *mem;
156 158 /* Reserved Mem Size (bytes) */
a/mboxd.c
b/mboxd.c
57 57 "\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n" \
58 58 "\t\t[-n | --window-num <num>]\n" \
59 59 "\t\t[-w | --window-size <size>M]\n" \
60 "\t\t-f | --flash <size>[K|M]\n\n" \
60 "\t\t-f | --flash <size>[K|M]\n" \
61 "\t\t-i | --file <path>\n\n" \
61 62 "\t-v | --verbose\t\tBe [more] verbose\n" \
62 63 "\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n" \
63 64 "\t-n | --window-num\tThe number of windows\n" \
64 65 "\t\t\t\t(default: fill the reserved memory region)\n" \
65 66 "\t-w | --window-size\tThe window size (power of 2) in MB\n" \
66 67 "\t\t\t\t(default: 1MB)\n" \
67 "\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n"
68 "\t-f | --flash\t\tSize of flash in [K|M] bytes\n" \
69 "\t-i | --file\t\tEmulate the flash device with the specified file\n\n"
68 70
69 71 static int poll_loop(struct mbox_context *context)
70 72 {
191 193
192 194 static const struct option long_options[] = {
193 195 { "flash", required_argument, 0, 'f' },
196 { "file", required_argument, 0, 'i' },
194 197 { "window-size", optional_argument, 0, 'w' },
195 198 { "window-num", optional_argument, 0, 'n' },
196 199 { "verbose", no_argument, 0, 'v' },
205 208
206 209 context->current = NULL; /* No current window */
207 210
208 while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL))
211 while ((opt = getopt_long(argc, argv, "f:i:w::n::vsVh", long_options, NULL))
209 212 != -1) {
210 213 switch (opt) {
211 214 case 0:
230 233 return false;
231 234 }
232 235 break;
236 case 'i':
237 context->filename = strdup(optarg);
238 break;
233 239 case 'n':
234 240 context->windows.num = strtol(argv[optind], &endptr,
235 241 10);
278 284 }
279 285
280 286 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size);
287 if (context->filename) {
288 MSG_INFO("Backing file: %s\n", context->filename);
289 }
281 290
282 291 if (verbosity) {
283 292 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
375 384 #ifdef VIRTUAL_PNOR_ENABLED
376 385 destroy_vpnor(context);
377 386 #endif
387 if(context->filename) {
388 free((void*)context->filename);
389 }
378 390 free(context);
379 391
380 392 return rc;
a/mboxd_flash_physical.c
b/mboxd_flash_physical.c
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);