Skip to content

Commit 35c3954

Browse files
committed
examples/mtdpart:Register MTD devices using the mtd_register method
1. Due to the automatic wrapping of MTD devices during the open() process, the legacy registration methods ftl_initialize() and bchdev_register() are no longer required for MTD device registration. The new method is now changed to register_mtddriver 2. Some code for space release and deregistration has been added, and certain error handling methods have been updated.
1 parent 29c8835 commit 35c3954

File tree

1 file changed

+107
-104
lines changed

1 file changed

+107
-104
lines changed

examples/mtdpart/mtdpart_main.c

Lines changed: 107 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ int main(int argc, FAR char *argv[])
133133
FAR struct mtd_dev_s *part[CONFIG_EXAMPLES_MTDPART_NPARTITIONS + 1];
134134
FAR struct mtd_geometry_s geo;
135135
FAR uint32_t *buffer;
136-
char blockname[32];
137-
char charname[32];
136+
char mtdname[32];
138137
size_t partsize;
139138
ssize_t nbytes;
140139
off_t nblocks;
@@ -148,6 +147,7 @@ int main(int argc, FAR char *argv[])
148147
int j;
149148
int k;
150149
int ret;
150+
int status;
151151

152152
/* Create and initialize a RAM MTD FLASH driver instance */
153153

@@ -159,8 +159,8 @@ int main(int argc, FAR char *argv[])
159159
if (!master)
160160
{
161161
printf("ERROR: Failed to create RAM MTD instance\n");
162-
fflush(stdout);
163-
exit(1);
162+
status = 1;
163+
goto errout;
164164
}
165165

166166
/* Perform the IOCTL to erase the entire FLASH part */
@@ -179,22 +179,12 @@ int main(int argc, FAR char *argv[])
179179
* interesting.
180180
*/
181181

182-
ret = ftl_initialize(0, master);
182+
ret = register_mtddriver("/dev/mtd0", master, 0775, NULL);
183183
if (ret < 0)
184184
{
185-
printf("ERROR: ftl_initialize /dev/mtdblock0 failed: %d\n", ret);
186-
fflush(stdout);
187-
exit(2);
188-
}
189-
190-
/* Now create a character device on the block device */
191-
192-
ret = bchdev_register("/dev/mtdblock0", "/dev/mtd0", false);
193-
if (ret < 0)
194-
{
195-
printf("ERROR: bchdev_register /dev/mtd0 failed: %d\n", ret);
196-
fflush(stdout);
197-
exit(3);
185+
printf("ERROR: register_mtddriver /dev/mtd0 failed: %d\n", ret);
186+
status = 2;
187+
goto errout;
198188
}
199189

200190
/* Get the geometry of the FLASH device */
@@ -204,7 +194,9 @@ int main(int argc, FAR char *argv[])
204194
if (ret < 0)
205195
{
206196
ferr("ERROR: mtd->ioctl failed: %d\n", ret);
207-
exit(3);
197+
unregister_mtddriver("/dev/mtd0");
198+
status = 3;
199+
goto errout;
208200
}
209201

210202
printf("Flash Geometry:\n");
@@ -244,33 +236,18 @@ int main(int argc, FAR char *argv[])
244236
{
245237
printf("ERROR: mtd_partition failed. offset=%lu nblocks=%lu\n",
246238
(unsigned long)offset, (unsigned long)nblocks);
247-
fflush(stdout);
248-
exit(4);
249-
}
250-
251-
/* Initialize to provide an FTL block driver on the MTD FLASH
252-
* interface
253-
*/
254-
255-
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", i);
256-
snprintf(charname, sizeof(charname), "/dev/mtd%d", i);
257-
258-
ret = ftl_initialize(i, part[i]);
259-
if (ret < 0)
260-
{
261-
printf("ERROR: ftl_initialize %s failed: %d\n", blockname, ret);
262-
fflush(stdout);
263-
exit(5);
239+
status = 4;
240+
goto errout;
264241
}
265242

266-
/* Now create a character device on the block device */
243+
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i);
267244

268-
ret = bchdev_register(blockname, charname, false);
245+
ret = register_mtddriver(mtdname, part[i], 0775, NULL);
269246
if (ret < 0)
270247
{
271-
printf("ERROR: bchdev_register %s failed: %d\n", charname, ret);
272-
fflush(stdout);
273-
exit(6);
248+
printf("ERROR: register_mtddriver %s failed: %d\n", mtdname, ret);
249+
status = 5;
250+
goto errout;
274251
}
275252
}
276253

@@ -280,8 +257,8 @@ int main(int argc, FAR char *argv[])
280257
if (!buffer)
281258
{
282259
printf("ERROR: failed to allocate a sector buffer\n");
283-
fflush(stdout);
284-
exit(7);
260+
status = 6;
261+
goto errout;
285262
}
286263

287264
/* Open the master MTD FLASH character driver for writing */
@@ -290,8 +267,8 @@ int main(int argc, FAR char *argv[])
290267
if (fd < 0)
291268
{
292269
printf("ERROR: open /dev/mtd0 failed: %d\n", errno);
293-
fflush(stdout);
294-
exit(8);
270+
status = 7;
271+
goto errout;
295272
}
296273

297274
/* Now write the offset into every block */
@@ -317,8 +294,9 @@ int main(int argc, FAR char *argv[])
317294
if (nbytes < 0)
318295
{
319296
printf("ERROR: write to /dev/mtd0 failed: %d\n", errno);
320-
fflush(stdout);
321-
exit(9);
297+
close(fd);
298+
status = 8;
299+
goto errout;
322300
}
323301
}
324302
}
@@ -338,13 +316,13 @@ int main(int argc, FAR char *argv[])
338316

339317
/* Open the master MTD partition character driver for writing */
340318

341-
snprintf(charname, sizeof(charname), "/dev/mtd%d", i);
342-
fd = open(charname, O_RDWR);
319+
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i);
320+
fd = open(mtdname, O_RDWR);
343321
if (fd < 0)
344322
{
345-
printf("ERROR: open %s failed: %d\n", charname, errno);
346-
fflush(stdout);
347-
exit(10);
323+
printf("ERROR: open %s failed: %d\n", mtdname, errno);
324+
status = 9;
325+
goto errout;
348326
}
349327

350328
/* Now verify the offset in every block */
@@ -364,31 +342,20 @@ int main(int argc, FAR char *argv[])
364342
{
365343
printf("ERROR: lseek to offset %ld failed: %d\n",
366344
(unsigned long)sectoff, errno);
367-
fflush(stdout);
368-
exit(11);
345+
close(fd);
346+
status = 10;
347+
goto errout;
369348
}
370349

371350
/* Read the next block into memory */
372351

373352
nbytes = read(fd, buffer, geo.blocksize);
374353
if (nbytes < 0)
375354
{
376-
printf("ERROR: read from %s failed: %d\n", charname, errno);
377-
fflush(stdout);
378-
exit(12);
379-
}
380-
else if (nbytes == 0)
381-
{
382-
printf("ERROR: Unexpected end-of file in %s\n", charname);
383-
fflush(stdout);
384-
exit(13);
385-
}
386-
else if (nbytes != geo.blocksize)
387-
{
388-
printf("ERROR: Unexpected read size from %s: %ld\n",
389-
charname, (unsigned long)nbytes);
390-
fflush(stdout);
391-
exit(14);
355+
printf("ERROR: read from %s failed: %d\n", mtdname, errno);
356+
close(fd);
357+
status = 11;
358+
goto errout;
392359
}
393360

394361
/* Since we forced the size of the partition to be an even number
@@ -398,19 +365,21 @@ int main(int argc, FAR char *argv[])
398365

399366
else if (nbytes == 0)
400367
{
401-
printf("ERROR: Unexpected end of file on %s\n", charname);
402-
fflush(stdout);
403-
exit(15);
368+
printf("ERROR: Unexpected end of file on %s\n", mtdname);
369+
close(fd);
370+
status = 12;
371+
goto errout;
404372
}
405373

406374
/* This is not expected at all */
407375

408376
else if (nbytes != geo.blocksize)
409377
{
410-
printf("ERROR: Short read from %s failed: %lu\n",
411-
charname, (unsigned long)nbytes);
412-
fflush(stdout);
413-
exit(16);
378+
printf("ERROR: Short read from %s failed: %lu\n",
379+
mtdname, (unsigned long)nbytes);
380+
close(fd);
381+
status = 13;
382+
goto errout;
414383
}
415384

416385
/* Verify the offsets in the block */
@@ -421,8 +390,9 @@ int main(int argc, FAR char *argv[])
421390
{
422391
printf("ERROR: Bad offset %lu, expected %lu\n",
423392
(long)buffer[k], (long)check);
424-
fflush(stdout);
425-
exit(17);
393+
close(fd);
394+
status = 14;
395+
goto errout;
426396
}
427397

428398
/* Invert the value to indicate that we have verified
@@ -440,25 +410,28 @@ int main(int argc, FAR char *argv[])
440410
{
441411
printf("ERROR: lseek to offset %ld failed: %d\n",
442412
(unsigned long)sectoff, errno);
443-
fflush(stdout);
444-
exit(18);
413+
close(fd);
414+
status = 15;
415+
goto errout;
445416
}
446417

447418
/* Now write the block back to FLASH with the modified value */
448419

449420
nbytes = write(fd, buffer, geo.blocksize);
450421
if (nbytes < 0)
451422
{
452-
printf("ERROR: write to %s failed: %d\n", charname, errno);
453-
fflush(stdout);
454-
exit(19);
423+
printf("ERROR: write to %s failed: %d\n", mtdname, errno);
424+
close(fd);
425+
status = 16;
426+
goto errout;
455427
}
456428
else if (nbytes != geo.blocksize)
457429
{
458430
printf("ERROR: Unexpected write size to %s: %ld\n",
459-
charname, (unsigned long)nbytes);
460-
fflush(stdout);
461-
exit(20);
431+
mtdname, (unsigned long)nbytes);
432+
close(fd);
433+
status = 17;
434+
goto errout;
462435
}
463436

464437
/* Get the offset to the next block */
@@ -472,9 +445,10 @@ int main(int argc, FAR char *argv[])
472445
if (nbytes != 0)
473446
{
474447
printf("ERROR: Expected end-of-file from %s failed: %zd %d\n",
475-
charname, nbytes, errno);
476-
fflush(stdout);
477-
exit(22);
448+
mtdname, nbytes, errno);
449+
close(fd);
450+
status = 18;
451+
goto errout;
478452
}
479453

480454
close(fd);
@@ -490,8 +464,8 @@ int main(int argc, FAR char *argv[])
490464
if (fd < 0)
491465
{
492466
printf("ERROR: open /dev/mtd0 failed: %d\n", errno);
493-
fflush(stdout);
494-
exit(23);
467+
status = 19;
468+
goto errout;
495469
}
496470

497471
offset = 0;
@@ -504,22 +478,25 @@ int main(int argc, FAR char *argv[])
504478
nbytes = read(fd, buffer, geo.blocksize);
505479
if (nbytes < 0)
506480
{
507-
printf("ERROR: read from %s failed: %d\n", charname, errno);
508-
fflush(stdout);
509-
exit(24);
481+
printf("ERROR: read from %s failed: %d\n", mtdname, errno);
482+
close(fd);
483+
status = 20;
484+
goto errout;
510485
}
511486
else if (nbytes == 0)
512487
{
513-
printf("ERROR: Unexpected end-of file in %s\n", charname);
514-
fflush(stdout);
515-
exit(25);
488+
printf("ERROR: Unexpected end-of file in %s\n", mtdname);
489+
close(fd);
490+
status = 21;
491+
goto errout;
516492
}
517493
else if (nbytes != geo.blocksize)
518494
{
519495
printf("ERROR: Unexpected read size from %s: %ld\n",
520-
charname, (unsigned long)nbytes);
521-
fflush(stdout);
522-
exit(26);
496+
mtdname, (unsigned long)nbytes);
497+
close(fd);
498+
status = 22;
499+
goto errout;
523500
}
524501

525502
/* Verify the values in the block */
@@ -530,19 +507,45 @@ int main(int argc, FAR char *argv[])
530507
{
531508
printf("ERROR: Bad value %lu, expected %lu\n",
532509
(long)buffer[k], (long)(~check));
533-
fflush(stdout);
534-
exit(27);
510+
close(fd);
511+
status = 23;
512+
goto errout;
535513
}
536514

537515
check += sizeof(uint32_t);
538516
}
539517
}
540518

541519
close(fd);
520+
status = 0;
542521

543522
/* And exit without bothering to clean up */
544523

545524
printf("PASS: Everything looks good\n");
525+
526+
errout:
527+
528+
printf("ERROR: error status %d\n", status);
529+
530+
if (buffer)
531+
{
532+
free(buffer);
533+
}
534+
535+
if (master)
536+
{
537+
rammtd_uninitialize(master);
538+
}
539+
540+
unregister_mtddriver("/dev/mtd0");
541+
542+
for (int i = 1; i <= CONFIG_EXAMPLES_MTDPART_NPARTITIONS; i++)
543+
{
544+
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i);
545+
unregister_mtddriver(mtdname);
546+
}
547+
548+
546549
fflush(stdout);
547-
return 0;
550+
exit(status);
548551
}

0 commit comments

Comments
 (0)