-
Notifications
You must be signed in to change notification settings - Fork 656
examples/mtdpart:Register MTD devices using the mtd_register method #3130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -133,8 +133,7 @@ int main(int argc, FAR char *argv[]) | |||||
FAR struct mtd_dev_s *part[CONFIG_EXAMPLES_MTDPART_NPARTITIONS + 1]; | ||||||
FAR struct mtd_geometry_s geo; | ||||||
FAR uint32_t *buffer; | ||||||
char blockname[32]; | ||||||
char charname[32]; | ||||||
char mtdname[32]; | ||||||
size_t partsize; | ||||||
ssize_t nbytes; | ||||||
off_t nblocks; | ||||||
|
@@ -148,6 +147,7 @@ int main(int argc, FAR char *argv[]) | |||||
int j; | ||||||
int k; | ||||||
int ret; | ||||||
int status; | ||||||
|
||||||
/* Create and initialize a RAM MTD FLASH driver instance */ | ||||||
|
||||||
|
@@ -159,8 +159,8 @@ int main(int argc, FAR char *argv[]) | |||||
if (!master) | ||||||
{ | ||||||
printf("ERROR: Failed to create RAM MTD instance\n"); | ||||||
fflush(stdout); | ||||||
exit(1); | ||||||
status = 1; | ||||||
goto errout; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/* Perform the IOCTL to erase the entire FLASH part */ | ||||||
|
@@ -179,22 +179,12 @@ int main(int argc, FAR char *argv[]) | |||||
* interesting. | ||||||
*/ | ||||||
|
||||||
ret = ftl_initialize(0, master); | ||||||
ret = register_mtddriver("/dev/mtd0", master, 0775, NULL); | ||||||
if (ret < 0) | ||||||
{ | ||||||
printf("ERROR: ftl_initialize /dev/mtdblock0 failed: %d\n", ret); | ||||||
fflush(stdout); | ||||||
exit(2); | ||||||
} | ||||||
|
||||||
/* Now create a character device on the block device */ | ||||||
|
||||||
ret = bchdev_register("/dev/mtdblock0", "/dev/mtd0", false); | ||||||
if (ret < 0) | ||||||
{ | ||||||
printf("ERROR: bchdev_register /dev/mtd0 failed: %d\n", ret); | ||||||
fflush(stdout); | ||||||
exit(3); | ||||||
printf("ERROR: register_mtddriver /dev/mtd0 failed: %d\n", ret); | ||||||
status = 2; | ||||||
goto errout; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. goto err_mtd |
||||||
} | ||||||
|
||||||
/* Get the geometry of the FLASH device */ | ||||||
|
@@ -204,7 +194,9 @@ int main(int argc, FAR char *argv[]) | |||||
if (ret < 0) | ||||||
{ | ||||||
ferr("ERROR: mtd->ioctl failed: %d\n", ret); | ||||||
exit(3); | ||||||
unregister_mtddriver("/dev/mtd0"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to the end and add label err_mtd |
||||||
status = 3; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
printf("Flash Geometry:\n"); | ||||||
|
@@ -244,33 +236,18 @@ int main(int argc, FAR char *argv[]) | |||||
{ | ||||||
printf("ERROR: mtd_partition failed. offset=%lu nblocks=%lu\n", | ||||||
(unsigned long)offset, (unsigned long)nblocks); | ||||||
fflush(stdout); | ||||||
exit(4); | ||||||
} | ||||||
|
||||||
/* Initialize to provide an FTL block driver on the MTD FLASH | ||||||
* interface | ||||||
*/ | ||||||
|
||||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", i); | ||||||
snprintf(charname, sizeof(charname), "/dev/mtd%d", i); | ||||||
|
||||||
ret = ftl_initialize(i, part[i]); | ||||||
if (ret < 0) | ||||||
{ | ||||||
printf("ERROR: ftl_initialize %s failed: %d\n", blockname, ret); | ||||||
fflush(stdout); | ||||||
exit(5); | ||||||
status = 4; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Now create a character device on the block device */ | ||||||
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i); | ||||||
|
||||||
ret = bchdev_register(blockname, charname, false); | ||||||
ret = register_mtddriver(mtdname, part[i], 0775, NULL); | ||||||
if (ret < 0) | ||||||
{ | ||||||
printf("ERROR: bchdev_register %s failed: %d\n", charname, ret); | ||||||
fflush(stdout); | ||||||
exit(6); | ||||||
printf("ERROR: register_mtddriver %s failed: %d\n", mtdname, ret); | ||||||
status = 5; | ||||||
goto errout; | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -280,8 +257,8 @@ int main(int argc, FAR char *argv[]) | |||||
if (!buffer) | ||||||
{ | ||||||
printf("ERROR: failed to allocate a sector buffer\n"); | ||||||
fflush(stdout); | ||||||
exit(7); | ||||||
status = 6; | ||||||
goto errout; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. goto err_buf |
||||||
} | ||||||
|
||||||
/* Open the master MTD FLASH character driver for writing */ | ||||||
|
@@ -290,8 +267,8 @@ int main(int argc, FAR char *argv[]) | |||||
if (fd < 0) | ||||||
{ | ||||||
printf("ERROR: open /dev/mtd0 failed: %d\n", errno); | ||||||
fflush(stdout); | ||||||
exit(8); | ||||||
status = 7; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Now write the offset into every block */ | ||||||
|
@@ -317,8 +294,9 @@ int main(int argc, FAR char *argv[]) | |||||
if (nbytes < 0) | ||||||
{ | ||||||
printf("ERROR: write to /dev/mtd0 failed: %d\n", errno); | ||||||
fflush(stdout); | ||||||
exit(9); | ||||||
close(fd); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move close to the end and add label err_fd |
||||||
status = 8; | ||||||
goto errout; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -338,13 +316,13 @@ int main(int argc, FAR char *argv[]) | |||||
|
||||||
/* Open the master MTD partition character driver for writing */ | ||||||
|
||||||
snprintf(charname, sizeof(charname), "/dev/mtd%d", i); | ||||||
fd = open(charname, O_RDWR); | ||||||
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i); | ||||||
fd = open(mtdname, O_RDWR); | ||||||
if (fd < 0) | ||||||
{ | ||||||
printf("ERROR: open %s failed: %d\n", charname, errno); | ||||||
fflush(stdout); | ||||||
exit(10); | ||||||
printf("ERROR: open %s failed: %d\n", mtdname, errno); | ||||||
status = 9; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Now verify the offset in every block */ | ||||||
|
@@ -364,31 +342,20 @@ int main(int argc, FAR char *argv[]) | |||||
{ | ||||||
printf("ERROR: lseek to offset %ld failed: %d\n", | ||||||
(unsigned long)sectoff, errno); | ||||||
fflush(stdout); | ||||||
exit(11); | ||||||
close(fd); | ||||||
status = 10; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Read the next block into memory */ | ||||||
|
||||||
nbytes = read(fd, buffer, geo.blocksize); | ||||||
if (nbytes < 0) | ||||||
{ | ||||||
printf("ERROR: read from %s failed: %d\n", charname, errno); | ||||||
fflush(stdout); | ||||||
exit(12); | ||||||
} | ||||||
else if (nbytes == 0) | ||||||
jingfei195887 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
printf("ERROR: Unexpected end-of file in %s\n", charname); | ||||||
fflush(stdout); | ||||||
exit(13); | ||||||
} | ||||||
else if (nbytes != geo.blocksize) | ||||||
{ | ||||||
printf("ERROR: Unexpected read size from %s: %ld\n", | ||||||
charname, (unsigned long)nbytes); | ||||||
fflush(stdout); | ||||||
exit(14); | ||||||
printf("ERROR: read from %s failed: %d\n", mtdname, errno); | ||||||
close(fd); | ||||||
status = 11; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Since we forced the size of the partition to be an even number | ||||||
|
@@ -398,19 +365,21 @@ int main(int argc, FAR char *argv[]) | |||||
|
||||||
else if (nbytes == 0) | ||||||
{ | ||||||
printf("ERROR: Unexpected end of file on %s\n", charname); | ||||||
fflush(stdout); | ||||||
exit(15); | ||||||
printf("ERROR: Unexpected end of file on %s\n", mtdname); | ||||||
close(fd); | ||||||
status = 12; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* This is not expected at all */ | ||||||
|
||||||
else if (nbytes != geo.blocksize) | ||||||
{ | ||||||
printf("ERROR: Short read from %s failed: %lu\n", | ||||||
charname, (unsigned long)nbytes); | ||||||
fflush(stdout); | ||||||
exit(16); | ||||||
printf("ERROR: Short read from %s failed: %lu\n", | ||||||
mtdname, (unsigned long)nbytes); | ||||||
close(fd); | ||||||
status = 13; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Verify the offsets in the block */ | ||||||
|
@@ -421,8 +390,9 @@ int main(int argc, FAR char *argv[]) | |||||
{ | ||||||
printf("ERROR: Bad offset %lu, expected %lu\n", | ||||||
(long)buffer[k], (long)check); | ||||||
fflush(stdout); | ||||||
exit(17); | ||||||
close(fd); | ||||||
status = 14; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Invert the value to indicate that we have verified | ||||||
|
@@ -440,25 +410,28 @@ int main(int argc, FAR char *argv[]) | |||||
{ | ||||||
printf("ERROR: lseek to offset %ld failed: %d\n", | ||||||
(unsigned long)sectoff, errno); | ||||||
fflush(stdout); | ||||||
exit(18); | ||||||
close(fd); | ||||||
status = 15; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Now write the block back to FLASH with the modified value */ | ||||||
|
||||||
nbytes = write(fd, buffer, geo.blocksize); | ||||||
if (nbytes < 0) | ||||||
{ | ||||||
printf("ERROR: write to %s failed: %d\n", charname, errno); | ||||||
fflush(stdout); | ||||||
exit(19); | ||||||
printf("ERROR: write to %s failed: %d\n", mtdname, errno); | ||||||
close(fd); | ||||||
status = 16; | ||||||
goto errout; | ||||||
} | ||||||
else if (nbytes != geo.blocksize) | ||||||
{ | ||||||
printf("ERROR: Unexpected write size to %s: %ld\n", | ||||||
charname, (unsigned long)nbytes); | ||||||
fflush(stdout); | ||||||
exit(20); | ||||||
mtdname, (unsigned long)nbytes); | ||||||
close(fd); | ||||||
status = 17; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Get the offset to the next block */ | ||||||
|
@@ -472,15 +445,16 @@ int main(int argc, FAR char *argv[]) | |||||
if (nbytes != 0) | ||||||
{ | ||||||
printf("ERROR: Expected end-of-file from %s failed: %zd %d\n", | ||||||
charname, nbytes, errno); | ||||||
fflush(stdout); | ||||||
exit(22); | ||||||
mtdname, nbytes, errno); | ||||||
close(fd); | ||||||
status = 18; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
close(fd); | ||||||
} | ||||||
|
||||||
/* Now verify that all of the verifed blocks appear where we thing they | ||||||
/* Now verify that all of the verified blocks appear where we thing they | ||||||
* should on the device. | ||||||
*/ | ||||||
|
||||||
|
@@ -490,8 +464,8 @@ int main(int argc, FAR char *argv[]) | |||||
if (fd < 0) | ||||||
{ | ||||||
printf("ERROR: open /dev/mtd0 failed: %d\n", errno); | ||||||
fflush(stdout); | ||||||
exit(23); | ||||||
status = 19; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
offset = 0; | ||||||
|
@@ -504,22 +478,25 @@ int main(int argc, FAR char *argv[]) | |||||
nbytes = read(fd, buffer, geo.blocksize); | ||||||
if (nbytes < 0) | ||||||
{ | ||||||
printf("ERROR: read from %s failed: %d\n", charname, errno); | ||||||
fflush(stdout); | ||||||
exit(24); | ||||||
printf("ERROR: read from %s failed: %d\n", mtdname, errno); | ||||||
close(fd); | ||||||
status = 20; | ||||||
goto errout; | ||||||
} | ||||||
else if (nbytes == 0) | ||||||
{ | ||||||
printf("ERROR: Unexpected end-of file in %s\n", charname); | ||||||
fflush(stdout); | ||||||
exit(25); | ||||||
printf("ERROR: Unexpected end-of file in %s\n", mtdname); | ||||||
close(fd); | ||||||
status = 21; | ||||||
goto errout; | ||||||
} | ||||||
else if (nbytes != geo.blocksize) | ||||||
{ | ||||||
printf("ERROR: Unexpected read size from %s: %ld\n", | ||||||
charname, (unsigned long)nbytes); | ||||||
fflush(stdout); | ||||||
exit(26); | ||||||
mtdname, (unsigned long)nbytes); | ||||||
close(fd); | ||||||
status = 22; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
/* Verify the values in the block */ | ||||||
|
@@ -530,19 +507,47 @@ int main(int argc, FAR char *argv[]) | |||||
{ | ||||||
printf("ERROR: Bad value %lu, expected %lu\n", | ||||||
(long)buffer[k], (long)(~check)); | ||||||
fflush(stdout); | ||||||
exit(27); | ||||||
close(fd); | ||||||
status = 23; | ||||||
goto errout; | ||||||
} | ||||||
|
||||||
check += sizeof(uint32_t); | ||||||
} | ||||||
} | ||||||
|
||||||
close(fd); | ||||||
status = 0; | ||||||
|
||||||
/* And exit without bothering to clean up */ | ||||||
|
||||||
printf("PASS: Everything looks good\n"); | ||||||
|
||||||
errout: | ||||||
|
||||||
if (status) | ||||||
{ | ||||||
printf("ERROR: error status %d\n", status); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the unnecessary log |
||||||
} | ||||||
|
||||||
if (buffer) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the check |
||||||
{ | ||||||
free(buffer); | ||||||
} | ||||||
|
||||||
if (master) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need zero at line 132, please fix ALL warning before submit your change. |
||||||
{ | ||||||
rammtd_uninitialize(master); | ||||||
} | ||||||
|
||||||
unregister_mtddriver("/dev/mtd0"); | ||||||
|
||||||
for (i = 1; i <= CONFIG_EXAMPLES_MTDPART_NPARTITIONS; i++) | ||||||
{ | ||||||
snprintf(mtdname, sizeof(mtdname), "/dev/mtd%d", i); | ||||||
unregister_mtddriver(mtdname); | ||||||
} | ||||||
|
||||||
fflush(stdout); | ||||||
return 0; | ||||||
exit(status); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need zero