2011-04-08 08:08:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <ioctl.h>
|
|
|
|
#include <nand.h>
|
2014-04-09 13:49:32 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
2011-04-08 08:08:32 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2011-04-08 09:32:44 +00:00
|
|
|
#include <linux/list.h>
|
2014-04-09 13:59:07 +00:00
|
|
|
#include <linux/err.h>
|
2011-04-08 08:08:32 +00:00
|
|
|
|
|
|
|
struct nand_bb {
|
|
|
|
char *name;
|
|
|
|
int open;
|
|
|
|
int needs_write;
|
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
struct mtd_info *mtd;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2011-10-14 09:57:55 +00:00
|
|
|
loff_t offset;
|
2011-12-11 12:10:44 +00:00
|
|
|
unsigned long flags;
|
2011-04-08 08:08:32 +00:00
|
|
|
void *writebuf;
|
|
|
|
|
|
|
|
struct cdev cdev;
|
2011-04-08 09:32:44 +00:00
|
|
|
|
|
|
|
struct list_head list;
|
2011-04-08 08:08:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
|
2011-10-14 11:46:09 +00:00
|
|
|
loff_t offset, ulong flags)
|
2011-04-08 08:08:32 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
2014-04-09 13:49:32 +00:00
|
|
|
size_t retlen;
|
2011-04-08 08:08:32 +00:00
|
|
|
int ret, bytes = 0, now;
|
|
|
|
|
2012-07-12 09:46:02 +00:00
|
|
|
debug("%s 0x%08llx %d\n", __func__, offset, count);
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
while (count) {
|
|
|
|
if (mtd_block_isbad(bb->mtd, offset)) {
|
2011-10-14 09:57:55 +00:00
|
|
|
printf("skipping bad block at 0x%08llx\n", bb->offset);
|
2014-04-09 13:49:32 +00:00
|
|
|
bb->offset += bb->mtd->erasesize;
|
2011-04-08 08:08:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
now = min(count, (size_t)(bb->mtd->erasesize -
|
|
|
|
((size_t)bb->offset % bb->mtd->erasesize)));
|
|
|
|
|
|
|
|
ret = mtd_read(bb->mtd, bb->offset, now, &retlen, buf);
|
2011-04-08 08:08:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-09 13:49:32 +00:00
|
|
|
buf += retlen;
|
|
|
|
count -= retlen;
|
|
|
|
bb->offset += retlen;
|
|
|
|
bytes += retlen;
|
2011-04-08 08:08:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be a multiple of the largest NAND page size */
|
|
|
|
#define BB_WRITEBUF_SIZE 4096
|
|
|
|
|
2011-12-21 21:30:40 +00:00
|
|
|
#ifdef CONFIG_MTD_WRITE
|
2011-04-08 08:08:32 +00:00
|
|
|
static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
|
|
|
|
{
|
|
|
|
int ret, now;
|
2014-04-09 13:49:32 +00:00
|
|
|
size_t retlen;
|
2011-04-08 08:08:32 +00:00
|
|
|
void *buf = bb->writebuf;
|
2012-07-12 09:46:02 +00:00
|
|
|
loff_t cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
|
2011-04-08 08:08:32 +00:00
|
|
|
|
|
|
|
while (count) {
|
2014-04-09 13:49:32 +00:00
|
|
|
if (mtd_block_isbad(bb->mtd, cur_ofs)) {
|
2012-07-12 09:46:02 +00:00
|
|
|
debug("skipping bad block at 0x%08llx\n", cur_ofs);
|
2014-04-09 13:49:32 +00:00
|
|
|
bb->offset += bb->mtd->erasesize;
|
|
|
|
cur_ofs += bb->mtd->erasesize;
|
2011-04-08 08:08:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
now = min(count, (size_t)(bb->mtd->erasesize));
|
|
|
|
ret = mtd_write(bb->mtd, cur_ofs, now, &retlen, buf);
|
2011-04-08 08:08:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-09 13:49:32 +00:00
|
|
|
buf += retlen;
|
|
|
|
count -= retlen;
|
|
|
|
cur_ofs += retlen;
|
2011-04-08 08:08:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
|
2011-10-14 11:46:09 +00:00
|
|
|
loff_t offset, ulong flags)
|
2011-04-08 08:08:32 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
|
|
|
int bytes = count, now, wroffs, ret;
|
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
debug("%s offset: 0x%08llx count: 0x%08x\n", __func__, offset, count);
|
2011-04-08 08:08:32 +00:00
|
|
|
|
|
|
|
while (count) {
|
|
|
|
wroffs = bb->offset % BB_WRITEBUF_SIZE;
|
|
|
|
now = min((int)count, BB_WRITEBUF_SIZE - wroffs);
|
|
|
|
memcpy(bb->writebuf + wroffs, buf, now);
|
|
|
|
|
|
|
|
if (wroffs + now == BB_WRITEBUF_SIZE) {
|
|
|
|
bb->needs_write = 0;
|
|
|
|
ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
bb->needs_write = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bb->offset += now;
|
|
|
|
count -= now;
|
|
|
|
buf += now;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
static int nand_bb_erase(struct cdev *cdev, size_t count, loff_t offset)
|
2011-04-08 08:08:32 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
2014-04-09 13:49:32 +00:00
|
|
|
struct erase_info erase = {};
|
2011-04-08 08:08:32 +00:00
|
|
|
|
|
|
|
if (offset != 0) {
|
|
|
|
printf("can only erase from beginning of device\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
erase.addr = 0;
|
|
|
|
erase.len = bb->mtd->size;
|
|
|
|
|
|
|
|
return mtd_erase(bb->mtd, &erase);
|
2011-04-08 08:08:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-11 11:38:04 +00:00
|
|
|
static int nand_bb_open(struct cdev *cdev, unsigned long flags)
|
2011-04-08 08:08:32 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
|
|
|
|
|
|
|
if (bb->open)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2011-12-11 12:10:44 +00:00
|
|
|
bb->flags = flags;
|
2011-04-08 08:08:32 +00:00
|
|
|
bb->open = 1;
|
|
|
|
bb->offset = 0;
|
|
|
|
bb->needs_write = 0;
|
|
|
|
bb->writebuf = xmalloc(BB_WRITEBUF_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nand_bb_close(struct cdev *cdev)
|
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
|
|
|
|
2011-12-21 21:30:40 +00:00
|
|
|
#ifdef CONFIG_MTD_WRITE
|
2011-04-08 08:08:32 +00:00
|
|
|
if (bb->needs_write)
|
|
|
|
nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
|
|
|
|
#endif
|
|
|
|
bb->open = 0;
|
|
|
|
free(bb->writebuf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nand_bb_calc_size(struct nand_bb *bb)
|
|
|
|
{
|
2011-10-14 09:57:55 +00:00
|
|
|
loff_t pos = 0;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
while (pos < bb->mtd->size) {
|
|
|
|
if (!mtd_block_isbad(bb->mtd, pos))
|
|
|
|
bb->cdev.size += bb->mtd->erasesize;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
pos += bb->mtd->erasesize;
|
2011-04-08 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
static loff_t nand_bb_lseek(struct cdev *cdev, loff_t __offset)
|
2011-12-11 12:10:44 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb = cdev->priv;
|
2012-07-08 20:29:39 +00:00
|
|
|
loff_t raw_pos = 0;
|
2011-12-11 12:10:44 +00:00
|
|
|
uint32_t offset = __offset;
|
|
|
|
|
|
|
|
/* lseek only in readonly mode */
|
|
|
|
if (bb->flags & O_ACCMODE)
|
|
|
|
return -ENOSYS;
|
2014-04-09 13:49:32 +00:00
|
|
|
while (raw_pos < bb->mtd->size) {
|
|
|
|
off_t now = min(offset, bb->mtd->erasesize);
|
2011-12-11 12:10:44 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
if (mtd_block_isbad(bb->mtd, raw_pos)) {
|
|
|
|
raw_pos += bb->mtd->erasesize;
|
|
|
|
} else {
|
2011-12-11 12:10:44 +00:00
|
|
|
offset -= now;
|
|
|
|
raw_pos += now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!offset) {
|
|
|
|
bb->offset = raw_pos;
|
|
|
|
return __offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-04-08 08:08:32 +00:00
|
|
|
static struct file_operations nand_bb_ops = {
|
|
|
|
.open = nand_bb_open,
|
|
|
|
.close = nand_bb_close,
|
|
|
|
.read = nand_bb_read,
|
2011-12-11 12:10:44 +00:00
|
|
|
.lseek = nand_bb_lseek,
|
2011-12-21 21:30:40 +00:00
|
|
|
#ifdef CONFIG_MTD_WRITE
|
2011-04-08 08:08:32 +00:00
|
|
|
.write = nand_bb_write,
|
|
|
|
.erase = nand_bb_erase,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-04-08 09:32:44 +00:00
|
|
|
static LIST_HEAD(bb_list);
|
|
|
|
|
2014-04-09 13:59:07 +00:00
|
|
|
struct cdev *mtd_add_bb(struct mtd_info *mtd, const char *name)
|
2011-04-08 08:08:32 +00:00
|
|
|
{
|
|
|
|
struct nand_bb *bb;
|
2014-04-09 13:59:07 +00:00
|
|
|
int ret;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
bb = xzalloc(sizeof(*bb));
|
2014-04-09 13:59:07 +00:00
|
|
|
bb->mtd = mtd;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:49:32 +00:00
|
|
|
if (name)
|
|
|
|
bb->cdev.name = xstrdup(name);
|
|
|
|
else
|
2014-04-09 13:59:07 +00:00
|
|
|
bb->cdev.name = asprintf("%s.bb", mtd->cdev.name);
|
2011-04-08 08:08:32 +00:00
|
|
|
|
|
|
|
nand_bb_calc_size(bb);
|
|
|
|
bb->cdev.ops = &nand_bb_ops;
|
|
|
|
bb->cdev.priv = bb;
|
|
|
|
|
2011-04-08 09:31:02 +00:00
|
|
|
ret = devfs_create(&bb->cdev);
|
|
|
|
if (ret)
|
2014-04-09 13:59:07 +00:00
|
|
|
goto err;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2011-04-08 09:32:44 +00:00
|
|
|
list_add_tail(&bb->list, &bb_list);
|
|
|
|
|
2014-04-09 13:59:07 +00:00
|
|
|
return &bb->cdev;
|
2011-04-08 08:08:32 +00:00
|
|
|
|
2014-04-09 13:59:07 +00:00
|
|
|
err:
|
2011-04-08 08:08:32 +00:00
|
|
|
free(bb);
|
2014-04-09 13:59:07 +00:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a bad block aware device ontop of another (NAND) device
|
|
|
|
* @param[in] dev The device to add a partition on
|
|
|
|
* @param[in] name Partition name (can be obtained with devinfo command)
|
|
|
|
* @return The device representing the new partition.
|
|
|
|
*/
|
|
|
|
int dev_add_bb_dev(const char *path, const char *name)
|
|
|
|
{
|
|
|
|
struct cdev *parent, *cdev;
|
|
|
|
|
|
|
|
parent = cdev_by_name(path);
|
|
|
|
if (!parent)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!parent->mtd)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
cdev = mtd_add_bb(parent->mtd, name);
|
|
|
|
|
|
|
|
return PTR_ERR(cdev);
|
2011-04-08 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
2011-04-08 09:32:44 +00:00
|
|
|
int dev_remove_bb_dev(const char *name)
|
|
|
|
{
|
|
|
|
struct nand_bb *bb;
|
|
|
|
|
|
|
|
list_for_each_entry(bb, &bb_list, list) {
|
|
|
|
if (!strcmp(bb->cdev.name, name)) {
|
|
|
|
devfs_remove(&bb->cdev);
|
2012-11-19 13:22:05 +00:00
|
|
|
list_del_init(&bb->list);
|
2014-04-09 13:49:32 +00:00
|
|
|
free(bb->name);
|
2011-04-08 09:32:44 +00:00
|
|
|
free(bb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|