2013-05-01 16:09:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/flash_test/flash_test.c
|
|
|
|
*
|
2021-06-16 08:28:53 +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
|
2013-05-01 16:09:20 +02:00
|
|
|
*
|
2021-06-16 08:28:53 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-05-01 16:09:20 +02:00
|
|
|
*
|
2021-06-16 08:28:53 +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.
|
2013-05-01 16:09:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2018-10-19 19:44:41 +02:00
|
|
|
#include <sys/stat.h>
|
2013-05-01 16:09:20 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-10-19 19:44:41 +02:00
|
|
|
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2013-12-10 16:38:48 +01:00
|
|
|
#include <nuttx/fs/smart.h>
|
2013-05-01 16:09:20 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2013-05-01 16:09:20 +02:00
|
|
|
{
|
|
|
|
struct inode* inode;
|
|
|
|
int ret;
|
|
|
|
int x;
|
|
|
|
int logsector;
|
|
|
|
uint16_t seq;
|
|
|
|
struct smart_format_s fmt;
|
|
|
|
uint16_t *sectors;
|
|
|
|
uint16_t *seqs;
|
|
|
|
char *buffer;
|
|
|
|
struct smart_read_write_s readwrite;
|
|
|
|
|
|
|
|
/* Argument given? */
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: flash_test flash_block_device\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Find the inode of the block driver identified by 'source' */
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
ret = open_blockdriver(argv[1], 0, &inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to open %s\n", argv[1]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the low-level format from the device. */
|
|
|
|
|
|
|
|
ret = inode->u.i_bops->ioctl(inode, BIOC_GETFORMAT, (unsigned long) &fmt);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Device is not a SMART block device\n");
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
/* Test if the device is formatted. If not, then we must do a
|
2013-05-01 16:09:20 +02:00
|
|
|
* low-level format first */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-05-01 16:09:20 +02:00
|
|
|
if (!(fmt.flags & SMART_FMT_ISFORMATTED))
|
|
|
|
{
|
|
|
|
/* Perform a low-level format */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_LLFORMAT, 0);
|
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_GETFORMAT, (unsigned long) &fmt);
|
2013-05-01 16:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fmt.flags & SMART_FMT_ISFORMATTED))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to get device format\n");
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Report the device structure */
|
|
|
|
|
|
|
|
printf("FLASH Test on device with:\n");
|
|
|
|
printf(" Sector size: %10d\n", fmt.sectorsize);
|
|
|
|
printf(" Sector count: %10d\n", fmt.nsectors);
|
|
|
|
printf(" Avail bytes: %10d\n", fmt.availbytes);
|
|
|
|
printf(" Total size: %10d\n", fmt.sectorsize * fmt.nsectors);
|
|
|
|
|
|
|
|
/* Allocate buffers to use */
|
|
|
|
|
|
|
|
buffer = (char *) malloc(fmt.availbytes);
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error allocating buffer\n");
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
seqs = (uint16_t *) malloc(fmt.nsectors << 1);
|
|
|
|
if (seqs == NULL)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
free(buffer);
|
2013-05-01 16:09:20 +02:00
|
|
|
fprintf(stderr, "Error allocating seqs buffer\n");
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
sectors = (uint16_t *) malloc(fmt.nsectors << 1);
|
|
|
|
if (sectors == NULL)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
free(seqs);
|
|
|
|
free(buffer);
|
2013-05-01 16:09:20 +02:00
|
|
|
fprintf(stderr, "Error allocating sectors buffer\n");
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a bunch of data to the flash */
|
|
|
|
|
|
|
|
printf("\nAllocating and writing to logical sectors\n");
|
|
|
|
|
|
|
|
seq = 0;
|
|
|
|
for (x = 0; x < fmt.nsectors >> 1; x++)
|
|
|
|
{
|
|
|
|
/* Allocate a new sector */
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
logsector = inode->u.i_bops->ioctl(inode, BIOC_ALLOCSECT,
|
2013-05-01 16:09:20 +02:00
|
|
|
(unsigned long) -1);
|
|
|
|
if (logsector < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error allocating sector: %d\n", logsector);
|
2014-04-14 00:24:28 +02:00
|
|
|
goto errout_with_driver;
|
2013-05-01 16:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the sector in our array */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-05-01 16:09:20 +02:00
|
|
|
sectors[x] = (uint16_t) logsector;
|
|
|
|
seqs[x] = seq++;
|
|
|
|
|
|
|
|
/* Now write some data to the sector */
|
|
|
|
|
|
|
|
sprintf(buffer, "Logical sector %d sequence %d\n",
|
|
|
|
sectors[x], seqs[x]);
|
|
|
|
|
|
|
|
readwrite.logsector = sectors[x];
|
|
|
|
readwrite.offset = 0;
|
|
|
|
readwrite.count = strlen(buffer) + 1;
|
|
|
|
readwrite.buffer = (uint8_t *) buffer;
|
2020-01-02 13:09:50 +01:00
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_WRITESECT, (unsigned long)
|
|
|
|
&readwrite);
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
/* Print the logical sector number */
|
|
|
|
|
|
|
|
printf("\r%d ", sectors[x]);
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
/* Now read the data back to validate everything was written and can
|
2013-05-01 16:09:20 +02:00
|
|
|
* be read. */
|
|
|
|
|
|
|
|
printf("\nDoing read verify test\n");
|
|
|
|
for (x = 0; x < fmt.nsectors >> 1; x++)
|
|
|
|
{
|
|
|
|
/* Read from the logical sector */
|
|
|
|
|
|
|
|
readwrite.logsector = sectors[x];
|
|
|
|
readwrite.offset = 0;
|
|
|
|
readwrite.count = fmt.availbytes;
|
|
|
|
readwrite.buffer = (uint8_t *) buffer;
|
|
|
|
ret = inode->u.i_bops->ioctl(inode, BIOC_READSECT, (unsigned long)
|
|
|
|
&readwrite);
|
|
|
|
|
|
|
|
if (ret != fmt.availbytes)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error reading sector %d\n", sectors[x]);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Generate compare string and do the compare */
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
printf("\r%d ", sectors[x]);
|
|
|
|
|
|
|
|
sprintf(&buffer[100], "Logical sector %d sequence %d\n",
|
|
|
|
sectors[x], seqs[x]);
|
|
|
|
|
|
|
|
if (strcmp(buffer, &buffer[100]) != 0)
|
|
|
|
{
|
|
|
|
printf("Sector %d read verify failed\n", sectors[x]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nPeforming sector re-write\n");
|
|
|
|
|
|
|
|
/* Overwrite data on the sectors to cause relocation */
|
|
|
|
|
|
|
|
for (x = 0; x < fmt.nsectors >> 1; x++)
|
|
|
|
{
|
|
|
|
/* Save a new sequence number for the sector */
|
|
|
|
|
|
|
|
seqs[x] = seq++;
|
|
|
|
|
|
|
|
/* Now write over the sector data with new data, causing a relocation. */
|
|
|
|
|
|
|
|
sprintf(buffer, "Logical sector %d sequence %d\n", sectors[x], seqs[x]);
|
|
|
|
readwrite.logsector = sectors[x];
|
|
|
|
readwrite.offset = 0;
|
|
|
|
readwrite.count = strlen(buffer) + 1;
|
|
|
|
readwrite.buffer = (uint8_t *) buffer;
|
2020-01-02 13:09:50 +01:00
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_WRITESECT, (unsigned long)
|
|
|
|
&readwrite);
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
/* Print the logical sector number */
|
|
|
|
|
|
|
|
printf("\r%d ", sectors[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now append data to empty region of sector */
|
|
|
|
|
|
|
|
printf("\nAppending data to empty region of sector\n");
|
|
|
|
|
|
|
|
for (x = 0; x < fmt.nsectors >> 1; x++)
|
|
|
|
{
|
|
|
|
/* Save a new sequence number for the sector */
|
|
|
|
|
|
|
|
seqs[x] = seq++;
|
|
|
|
|
|
|
|
/* Now write over the sector data with new data, causing a relocation. */
|
|
|
|
|
|
|
|
sprintf(buffer, "Appended data in sector %d\n", sectors[x]);
|
|
|
|
readwrite.logsector = sectors[x];
|
|
|
|
readwrite.offset = 64;
|
|
|
|
readwrite.count = strlen(buffer) + 1;
|
|
|
|
readwrite.buffer = (uint8_t *) buffer;
|
2020-01-02 13:09:50 +01:00
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_WRITESECT, (unsigned long)
|
|
|
|
&readwrite);
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
/* Print the logical sector number */
|
|
|
|
|
|
|
|
printf("\r%d ", sectors[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nDone\n");
|
|
|
|
|
|
|
|
/* Free all the allocated sectors */
|
|
|
|
|
|
|
|
for (x = 0; x < fmt.nsectors >> 1; x++)
|
|
|
|
{
|
|
|
|
/* Only free the sector if it is still valid */
|
|
|
|
|
|
|
|
if (sectors[x] != 0xFFFF)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
inode->u.i_bops->ioctl(inode, BIOC_FREESECT, (unsigned long)
|
|
|
|
sectors[x]);
|
2013-05-01 16:09:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_buffers:
|
|
|
|
/* Free the allocated buffers */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
free(seqs);
|
|
|
|
free(sectors);
|
|
|
|
free(buffer);
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
errout_with_driver:
|
|
|
|
/* Now close the block device and exit */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close_blockdriver(inode);
|
2013-05-01 16:09:20 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|