2015-11-09 18:45:44 +01:00
|
|
|
/****************************************************************************
|
2021-07-23 06:03:12 +02:00
|
|
|
* apps/examples/media/media_main.c
|
2015-11-09 18:45:44 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-11-09 18:45:44 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-09 18:45:44 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-11-09 18:45:44 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2020-11-13 11:58:14 +01:00
|
|
|
#include <stdint.h>
|
2015-11-09 18:45:44 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
#define START_VALUE 0x20
|
|
|
|
#define END_VALUE 0x7f
|
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct media_info_s
|
|
|
|
{
|
|
|
|
off_t blocksize;
|
|
|
|
off_t nblocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* get_blocksize
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void get_blocksize(int fd, FAR struct media_info_s *info)
|
|
|
|
{
|
|
|
|
struct mtd_geometry_s mtdgeo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* If the underlying driver is an MTD driver, then we should able to get
|
|
|
|
* the erase block size from an MTD ioctl call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ioctl(fd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&mtdgeo));
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2015-11-13 18:30:52 +01:00
|
|
|
/* Its an MTD device. Use its geometry */
|
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
printf("MTD Geometry:\n");
|
|
|
|
printf(" blocksize: %u\n", (unsigned int)mtdgeo.blocksize);
|
|
|
|
printf(" erasesize: %lu\n", (unsigned long)mtdgeo.erasesize);
|
|
|
|
printf(" neraseblocks: %lu\n", (unsigned long)mtdgeo.neraseblocks);
|
|
|
|
|
|
|
|
info->blocksize = mtdgeo.erasesize;
|
|
|
|
info->nblocks = mtdgeo.neraseblocks;
|
2015-11-13 18:30:52 +01:00
|
|
|
|
|
|
|
/* Attempt to erase the entire MTD device */
|
|
|
|
|
|
|
|
ret = ioctl(fd, MTDIOC_BULKERASE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed erase the MTD device\n");
|
|
|
|
}
|
2015-11-09 18:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, use the configured default. We have no idea of the size
|
|
|
|
* of the media in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->blocksize = CONFIG_EXAMPLES_MEDIA_BLOCKSIZE;
|
|
|
|
info->nblocks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* media_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2015-11-09 20:10:00 +01:00
|
|
|
FAR uint8_t *txbuffer;
|
|
|
|
FAR uint8_t *rxbuffer;
|
2016-06-01 16:15:10 +02:00
|
|
|
FAR char *devpath;
|
2015-11-09 18:45:44 +01:00
|
|
|
struct media_info_s info;
|
2015-11-09 20:10:00 +01:00
|
|
|
ssize_t nwritten;
|
|
|
|
ssize_t nread;
|
|
|
|
off_t blockno;
|
|
|
|
off_t pos;
|
|
|
|
off_t seekpos;
|
|
|
|
off_t i;
|
|
|
|
uint32_t nerrors;
|
|
|
|
uint8_t value;
|
2015-11-09 18:45:44 +01:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Open the character driver that wraps the media */
|
|
|
|
|
2016-06-01 16:15:10 +02:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
devpath = CONFIG_EXAMPLES_MEDIA_DEVPATH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
devpath = argv[1];
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:10:26 +02:00
|
|
|
fd = open(devpath, O_RDWR);
|
2016-06-01 16:15:10 +02:00
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: failed to open %s: %d\n",
|
2016-06-01 16:15:10 +02:00
|
|
|
devpath, errno);
|
2015-11-09 18:45:44 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2020-11-13 11:54:28 +01:00
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
/* Get the block size to use */
|
|
|
|
|
|
|
|
get_blocksize(fd, &info);
|
2015-11-10 20:35:10 +01:00
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
printf("Using:\n");
|
2021-08-02 07:57:50 +02:00
|
|
|
printf(" blocksize: %ju\n", (uintmax_t)info.blocksize);
|
|
|
|
printf(" nblocks: %ju\n", (uintmax_t)info.nblocks);
|
2015-11-09 18:45:44 +01:00
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
/* Allocate I/O buffers of the correct block size */
|
2015-11-09 18:45:44 +01:00
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
txbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize);
|
|
|
|
if (txbuffer == NULL)
|
2015-11-09 18:45:44 +01:00
|
|
|
{
|
2020-11-13 11:54:28 +01:00
|
|
|
fprintf(stderr,
|
2021-08-02 07:57:50 +02:00
|
|
|
"ERROR: failed to allocate TX I/O buffer of size %ju\n",
|
|
|
|
(uintmax_t)info.blocksize);
|
2015-11-09 18:45:44 +01:00
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
rxbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize);
|
|
|
|
if (rxbuffer == NULL)
|
|
|
|
{
|
2020-11-13 11:54:28 +01:00
|
|
|
fprintf(stderr,
|
2021-08-02 07:57:50 +02:00
|
|
|
"ERROR: failed to allocate IRX /O buffer of size %ju\n",
|
|
|
|
(uintmax_t)info.blocksize);
|
2015-11-09 20:10:00 +01:00
|
|
|
free(txbuffer);
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write and verify each sector */
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
nerrors = 0;
|
|
|
|
value = START_VALUE;
|
|
|
|
|
|
|
|
for (blockno = 0; info.nblocks == 0 || blockno < info.nblocks; blockno++)
|
|
|
|
{
|
2021-07-23 06:03:12 +02:00
|
|
|
printf("Write/Verify: Block %ju\n", (uintmax_t)blockno);
|
2015-11-10 20:35:10 +01:00
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
/* Fill buffer with a (possibly) unique pattern */
|
|
|
|
|
2016-04-21 03:23:01 +02:00
|
|
|
for (i = 0; i < info.blocksize; i++)
|
2015-11-09 20:10:00 +01:00
|
|
|
{
|
|
|
|
txbuffer[i] = value;
|
|
|
|
if (++value >= END_VALUE)
|
|
|
|
{
|
|
|
|
value = START_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seekpos = lseek(fd, pos, SEEK_SET);
|
|
|
|
if (seekpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
2020-11-13 11:58:14 +01:00
|
|
|
fprintf(stderr, "ERROR: lseek to %ju failed: %d\n",
|
|
|
|
(uintmax_t)pos, errcode);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (seekpos != pos)
|
|
|
|
{
|
2020-11-13 11:58:14 +01:00
|
|
|
fprintf(stderr, "ERROR: lseek failed: %ju vs %ju\n",
|
|
|
|
(uintmax_t)seekpos, (uintmax_t) pos);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nwritten = write(fd, txbuffer, info.blocksize);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: write failed: %d\n", errcode);
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nwritten != info.blocksize)
|
|
|
|
{
|
2021-08-02 07:57:50 +02:00
|
|
|
fprintf(stderr, "ERROR: Unexpected write size: %zu vs. %ju\n",
|
|
|
|
nwritten, (uintmax_t)info.blocksize);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
seekpos = lseek(fd, pos, SEEK_SET);
|
|
|
|
if (seekpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
2020-11-13 11:58:14 +01:00
|
|
|
fprintf(stderr, "ERROR: lseek to %ju failed: %d\n",
|
|
|
|
(uintmax_t)pos, errcode);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (seekpos != pos)
|
|
|
|
{
|
2020-11-13 11:58:14 +01:00
|
|
|
fprintf(stderr, "ERROR: lseek failed: %ju vs %ju\n",
|
|
|
|
(uintmax_t)seekpos, (uintmax_t)pos);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = read(fd, rxbuffer, info.blocksize);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: read failed: %d\n", errcode);
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nread != info.blocksize)
|
|
|
|
{
|
2021-08-02 07:57:50 +02:00
|
|
|
fprintf(stderr, "ERROR: Unexpected read size: %zu vs. %ju\n",
|
|
|
|
nread, (uintmax_t)info.blocksize);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-10 18:34:33 +01:00
|
|
|
for (i = 0; i < info.blocksize && nerrors <= 100; i++)
|
2015-11-09 20:10:00 +01:00
|
|
|
{
|
|
|
|
if (txbuffer[i] != rxbuffer[i])
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2021-08-02 07:57:50 +02:00
|
|
|
"ERROR: block=%ju offset=%ju. "
|
2020-11-13 11:54:28 +01:00
|
|
|
"Unexpected value: %02x vs. %02x\n",
|
2021-08-02 07:57:50 +02:00
|
|
|
(uintmax_t)blockno, (uintmax_t)i,
|
|
|
|
rxbuffer[i], txbuffer[i]);
|
2015-11-10 18:34:33 +01:00
|
|
|
nerrors++;
|
2015-11-09 20:10:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 18:34:33 +01:00
|
|
|
|
|
|
|
if (nerrors > 100)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Too many errors\n");
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-10 18:34:33 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-09 20:10:00 +01:00
|
|
|
}
|
2015-11-10 18:34:33 +01:00
|
|
|
|
|
|
|
pos += info.blocksize;
|
2015-11-09 20:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the number of blocks if it was unknown before */
|
|
|
|
|
|
|
|
if (info.nblocks == 0)
|
|
|
|
{
|
|
|
|
info.nblocks = blockno;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Seek to the beginning of the file */
|
2015-11-09 20:33:37 +01:00
|
|
|
|
|
|
|
seekpos = lseek(fd, 0, SEEK_SET);
|
|
|
|
if (seekpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: lseek to 0 failed: %d\n", errcode);
|
|
|
|
}
|
|
|
|
else if (seekpos != 0)
|
|
|
|
{
|
2020-11-13 11:58:14 +01:00
|
|
|
fprintf(stderr, "ERROR: lseek to 0 failed: %ju\n",
|
|
|
|
(uintmax_t)seekpos);
|
2015-11-09 20:33:37 +01:00
|
|
|
}
|
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
/* Re-read and verify each sector */
|
|
|
|
|
|
|
|
value = START_VALUE;
|
|
|
|
|
|
|
|
for (blockno = 0; blockno < info.nblocks; blockno++)
|
|
|
|
{
|
2021-07-23 06:03:12 +02:00
|
|
|
printf("Re-read/Verify: Block %ju\n", (uintmax_t)blockno);
|
2015-11-10 20:35:10 +01:00
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
/* Fill buffer with a (possibly) unique pattern */
|
|
|
|
|
2016-04-21 03:23:01 +02:00
|
|
|
for (i = 0; i < info.blocksize; i++)
|
2015-11-09 20:10:00 +01:00
|
|
|
{
|
|
|
|
txbuffer[i] = value;
|
|
|
|
if (++value >= END_VALUE)
|
|
|
|
{
|
|
|
|
value = START_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = read(fd, rxbuffer, info.blocksize);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: read failed: %d\n", errcode);
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nread != info.blocksize)
|
|
|
|
{
|
2021-08-02 07:57:50 +02:00
|
|
|
fprintf(stderr, "ERROR: Unexpected read size: %zu vs. %ju\n",
|
|
|
|
nread, (uintmax_t)info.blocksize);
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-09 20:10:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-10 18:34:33 +01:00
|
|
|
for (i = 0; i < info.blocksize && nerrors <= 100; i++)
|
2015-11-09 20:10:00 +01:00
|
|
|
{
|
|
|
|
if (txbuffer[i] != rxbuffer[i])
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2021-08-02 07:57:50 +02:00
|
|
|
"ERROR: block=%ju offset=%ju. "
|
2020-11-13 11:54:28 +01:00
|
|
|
"Unexpected value: %02x vs. %02x\n",
|
2021-08-02 07:57:50 +02:00
|
|
|
(uintmax_t)blockno, (uintmax_t)i,
|
|
|
|
rxbuffer[i], txbuffer[i]);
|
2015-11-10 18:34:33 +01:00
|
|
|
nerrors++;
|
2015-11-09 20:10:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 18:34:33 +01:00
|
|
|
|
|
|
|
if (nerrors > 100)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Too many errors\n");
|
2021-07-23 06:03:12 +02:00
|
|
|
fprintf(stderr, "ERROR: Aborting at block: %ju\n",
|
|
|
|
(uintmax_t)blockno);
|
2015-11-10 18:34:33 +01:00
|
|
|
info.nblocks = blockno;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-09 20:10:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 18:45:44 +01:00
|
|
|
/* Clean-up and exit */
|
|
|
|
|
2015-11-09 20:10:00 +01:00
|
|
|
free(rxbuffer);
|
|
|
|
free(txbuffer);
|
2015-11-09 18:45:44 +01:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|