顯示具有 Linux - MTD 標籤的文章。 顯示所有文章
顯示具有 Linux - MTD 標籤的文章。 顯示所有文章

2020年10月9日 星期五

How MSM Parse Partition Table in Kernel


call flow
msm_nand_probe()
  |-> msm_nand_parse_smem_ptable() 
    |-> smem_get_entry(SMEM_AARM_PARTITION_TABLE)
  |-> mtd_device_parse_register()


code snippet, https://android.googlesource.com/kernel/msm/+/android-msm-hammerhead-3.4-kk-r1/drivers/mtd/devices/msm_qpic_nand.c
#define FLASH_PART_MAGIC1        0x55EE73AA
#define FLASH_PART_MAGIC2        0xE35EBDDB
#define FLASH_PTABLE_V3         3
#define FLASH_PTABLE_V4         4
#define FLASH_PTABLE_MAX_PARTS_V3  16
#define FLASH_PTABLE_MAX_PARTS_V4  32
#define FLASH_PTABLE_HDR_LEN     (4*sizeof(uint32_t))

// Parititon Table Store in Flash.
struct flash_partition_entry {
  char name[FLASH_PTABLE_ENTRY_NAME_SIZE];
  u32 offset;   /* Offset in blocks from beginning of device */
  u32 length;   /* Length of the partition in blocks */
  u8 attr;     /* Flags for this partition */
};

struct flash_partition_table {
  u32 magic1;
  u32 magic2;
  u32 version;
  u32 numparts;
  struct flash_partition_entry part_entry[FLASH_PTABLE_MAX_PARTS_V4];
};

static struct mtd_partition mtd_part[FLASH_PTABLE_MAX_PARTS_V4];

static int __devinit msm_nand_probe(struct platform_device *pdev)
{
  ...
  err = msm_nand_parse_smem_ptable(&nr_parts);
  ...
  for (i = 0; i < nr_parts; i++) {
    mtd_part[i].offset *= info->mtd.erasesize;
    mtd_part[i].size *= info->mtd.erasesize;
  }
  err = mtd_device_parse_register(&info->mtd, NULL, NULL, &mtd_part[0], nr_parts);
  ...
}

// Get Partition Table from RAM/Flash via smem_get_entry(SMEM_AARM_PARTITION_TABLE).
static int msm_nand_parse_smem_ptable(int *nr_parts)
{
  uint32_t  i, j;
  uint32_t len = FLASH_PTABLE_HDR_LEN;
  struct flash_partition_entry *pentry;
  char *delimiter = ":";
  pr_info("Parsing partition table info from SMEM\n");
  /* Read only the header portion of ptable */
  ptable = *(struct flash_partition_table *)
            (smem_get_entry(SMEM_AARM_PARTITION_TABLE, &len));
  /* Verify ptable magic */
  if (ptable.magic1 != FLASH_PART_MAGIC1 || 
      ptable.magic2 != FLASH_PART_MAGIC2) {
    pr_err("Partition table magic verification failed\n");
    goto out;
  }
  /* Ensure that # of partitions is less than the max we have allocated */
  if (ptable.numparts > FLASH_PTABLE_MAX_PARTS_V4) {
    pr_err("Partition numbers exceed the max limit\n");
    goto out;
  }
  /* Find out length of partition data based on table version. */
  if (ptable.version <= FLASH_PTABLE_V3) {
    len = FLASH_PTABLE_HDR_LEN + FLASH_PTABLE_MAX_PARTS_V3 *
        sizeof(struct flash_partition_entry);
  } else if (ptable.version == FLASH_PTABLE_V4) {
    len = FLASH_PTABLE_HDR_LEN + FLASH_PTABLE_MAX_PARTS_V4 *
        sizeof(struct flash_partition_entry);
  } else {
    pr_err("Unknown ptable version (%d)", ptable.version);
    goto out;
  }
  *nr_parts = ptable.numparts;
  ptable = *(struct flash_partition_table *)
        (smem_get_entry(SMEM_AARM_PARTITION_TABLE, &len));
  for (i = 0; i < ptable.numparts; i++) {
    pentry = &ptable.part_entry[i];
    if (pentry->name == '\0')
      continue;
    /* Convert name to lower case and discard the initial chars */
    mtd_part[i].name = pentry->name;
    for (j = 0; j < strlen(mtd_part[i].name); j++)
      *(mtd_part[i].name + j) = tolower(*(mtd_part[i].name + j));
      strsep(&(mtd_part[i].name), delimiter);
      mtd_part[i].offset      = pentry->offset;
      mtd_part[i].mask_flags  = pentry->attr;
      mtd_part[i].size        = pentry->length;
      pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
          i, pentry->name, pentry->offset, pentry->length, pentry->attr);
  }
  pr_info("SMEM partition table found: ver: %d len: %d\n",
      ptable.version, ptable.numparts);
  return 0;
out:
  return -EINVAL;
}

static const struct of_device_id msm_nand_match_table[] = {
  { .compatible = "qcom,msm-nand", },
  {},
};


2020年9月27日 星期日

Linux Kernel(10.3.1)- Command line partition table parsing for Kernel 4.19


由於10.3的kernel過時,所以10.3.1是用kernel 4.19來做補充。
MTD Partition除了在code中寫死以外,其實還可以透過一些parsers來作規劃,這一章就要來教大家如何使用"Command line partition table parsing"。
首先必須在kernel中啟用"Command line partition table parsing",請參照10.3。
接著在command line中加入mtdparts的設定,其簡單說明如下:
 * mtdparts=<mtddef>[;<mtddef]
 * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
 * <partdef> := <size>[@<offset>][<name>][ro][lk]
 * <mtd-id>  := unique name used in mapping driver/device (mtd->name)
 * <size>    := standard linux memsize OR "-" to denote all remaining space
 *              size is automatically truncated at end of device
 *              if specified or truncated size is 0 the part is skipped
 * <offset>  := standard linux memsize
 *              if omitted the part will immediately follow the previous part
 *              or 0 if the first part
 * <name>    := '(' NAME ')'
 *              NAME will appear in /proc/mtd
 *
 * <size> and <offset> can be specified such that the parts are out of order
 * in physical memory and may even overlap.
 *
 * The parts are assigned MTD numbers in the order they are specified in the
 * command line regardless of their order in physical memory.

 * Due to the way Linux handles the command line, no spaces are
 * allowed in the partition definition, including mtd id's and partition
 * names.

這裡我用qemu與mtdram來執行,執行參數如下
qemu-system-arm -s -M vexpress-a9 -m 128M -kernel ./linux/arch/arm/boot/zImage \
                -dtb ./linux/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -initrd ./initrd-arm.img \
                -nographic -append "console=ttyAMA0 mtdparts=\"mtdram test device:1024k(p1),1024k(p2),-(p3)\""

這裡特別要說明的是mtd-id就是mtd name,基本上你可以以cat /proc/mtd,就可以知道mtd name了,或者trace一下code也行
// file "drivers/mtd/cmdlinepart.c"
static int parse_cmdline_partitions(struct mtd_info *master,
                    const struct mtd_partition **pparts,
                    struct mtd_part_parser_data *data)
{
    ...
const char *mtd_id = master->name;
    /*
     * Search for the partition definition matching master->name.
     * If master->name is not set, stop at first partition definition.
     */
    for (part = partitions; part; part = part->next) {
        if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
            break;
    }
    ...
}

// drivers/mtd/devices/mtdram.c
static int __init init_mtdram(void)
{
    ...
    err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
    ...
}

int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
        unsigned long size, const char *name)
{
    ...
    /* Setup the MTD structure */
    mtd->name = name;
    ...
}

此外我多印了一些deubg資訊在drivers/mtd/cmdlinepart.c
diff --git a/drivers/mtd/cmdlinepart.c b/drivers/mtd/cmdlinepart.c
index 3ea44cff9b75..a411ce85097e 100644
--- a/drivers/mtd/cmdlinepart.c
+++ b/drivers/mtd/cmdlinepart.c
@@ -48,6 +48,7 @@
  * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  */

+#define DEBGU
 #define pr_fmt(fmt)    "mtd: " fmt

 #include 
@@ -58,7 +59,7 @@
 #include 

 /* debug macro */
-#if 0
+#if 1
 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
 #else
 #define dbg(x)
@@ -315,6 +316,7 @@ static int parse_cmdline_partitions(struct mtd_info *master,
        struct cmdline_mtd_partition *part;
        const char *mtd_id = master->name;

+       printk("BROOK, %s(#%d), cmdline_parsed:%d\n", __func__, __LINE__, cmdline_parsed);
        /* parse command line */
        if (!cmdline_parsed) {
                err = mtdpart_setup_real(cmdline);
@@ -327,12 +329,15 @@ static int parse_cmdline_partitions(struct mtd_info *master,
         * If master->name is not set, stop at first partition definition.
         */
        for (part = partitions; part; part = part->next) {
+           printk("BROOK, %s(#%d), mtd_id:%s, part->mtd_id:%s\n", __func__, __LINE__, mtd_id, part->mtd_id);
                if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
                        break;
        }

-       if (!part)
+       printk("BROOK, %s(#%d), part:%p\n", __func__, __LINE__, part);
+       if (!part) {
                return 0;
+       }

        for (i = 0, offset = 0; i < part->num_parts; i++) {
                if (part->parts[i].offset == OFFSET_CONTINUOUS)

執行結果如下:
/ # dmesg
...
[    4.688086] BROOK, parse_cmdline_partitions(#319), cmdline_parsed:0
[    4.688876] DEBUG-CMDLINE-PART:
[    4.688994] parsing <1024k(p1),1024k(p2),-(p3)>
[    4.690787] DEBUG-CMDLINE-PART:
[    4.690944] partition 2: name <p3>, offset ffffffffffffffff, size ffffffffffffffff, mask flags 0
[    4.692017] DEBUG-CMDLINE-PART:
[    4.692102] partition 1: name <p2>, offset ffffffffffffffff, size 100000, mask flags 0
[    4.692950] DEBUG-CMDLINE-PART:
[    4.693034] partition 0: name <p1>, offset ffffffffffffffff, size 100000, mask flags 0
[    4.693993] DEBUG-CMDLINE-PART:
[    4.694101] mtdid=<mtdram test device> num_parts=<3>
[    4.695000] BROOK, parse_cmdline_partitions(#332), mtd_id:40000000.flash, part->mtd_id:mtdram test device
[    4.696546] BROOK, parse_cmdline_partitions(#337), part:  (null)
[    4.763129] BROOK, parse_cmdline_partitions(#319), cmdline_parsed:1
[    4.763742] BROOK, parse_cmdline_partitions(#332), mtd_id:48000000.psram, part->mtd_id:mtdram test device
[    4.764461] BROOK, parse_cmdline_partitions(#337), part:  (null)
[    4.806551] BROOK, parse_cmdline_partitions(#319), cmdline_parsed:1
[    4.808150] BROOK, parse_cmdline_partitions(#332), mtd_id:mtdram test device, part->mtd_id:mtdram test device
[    4.809227] BROOK, parse_cmdline_partitions(#337), part:(ptrval)
[    4.810325] 3 cmdlinepart partitions found on MTD device mtdram test device
[    4.811006] Creating 3 MTD partitions on "mtdram test device":
[    4.813103] 0x000000000000-0x000000100000 : "p1"
[    4.830841] 0x000000100000-0x000000200000 : "p2"
[    4.846502] 0x000000200000-0x000000400000 : "p3"
...

/ # cat /proc/mtd
dev:    size   erasesize  name
mtd0: 08000000 00040000 "40000000.flash"
mtd1: 02000000 00001000 "48000000.psram"
mtd2: 00100000 00020000 "p1"
mtd3: 00100000 00020000 "p2"
mtd4: 00200000 00020000 "p3"

/ # zcat /proc/config.gz | grep -i MTDRAM
CONFIG_MTD_MTDRAM=y
CONFIG_MTDRAM_TOTAL_SIZE=4096
CONFIG_MTDRAM_ERASE_SIZE=128




2018年9月9日 星期日

Mount UBI with nandsim


UBI/UBIFS無法使用loop-back方式mount, 不過我們可以使用NAND simulator將UBI燒入到nandsim中.
root@vista:~# mkfs.ubifs -m 2KiB -e 129024 -c 2048 -r ubifs-root -x zlib ubifs.img
root@vista:~# cat ubi.ini
[ubi_rfs]
mode=ubi
image=ubifs.img
vol_id=0
vol_type=dynamic
vol_name=ubi_rfs
vol_alignment=1
vol_flags=autoresize

root@vista:~# ubinize -o my.ubi -p 128KiB -m 2KiB -O 512 ubi.ini
ubinize: volume size was not specified in section "ubi_rfs", assume minimum to fit image "ubifs.img"1806336 bytes (1.7 MiB)
root@vista:~# modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15
root@vista:~# ubiformat /dev/mtd0 -f my.ubi
ubiformat: mtd0 (nand), size 268435456 bytes (256.0 MiB), 2048 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 2047 -- 100 % complete
ubiformat: 2048 eraseblocks have valid erase counter, mean value is 3
ubiformat: flashing eraseblock 15 -- 100 % complete
ubiformat: formatting eraseblock 2047 -- 100 % complete
ubiformat: formatting eraseblock 2047 -- 100 % complete
root@vista:~# ubiattach -m 0
UBI device number 0, total 2048 LEBs (264241152 bytes, 252.0 MiB), available 0 LEBs (0 bytes), LEB size 129024 bytes (126.0 KiB)
root@vista:~# ubinfo -a
UBI version:                    1
Count of UBI devices:           1
UBI control device major/minor: 10:55
Present UBI devices:            ubi0

ubi0
Volumes count:                           1
Logical eraseblock size:                 129024 bytes, 126.0 KiB
Total amount of logical eraseblocks:     2048 (264241152 bytes, 252.0 MiB)
Amount of available logical eraseblocks: 0 (0 bytes)
Maximum count of volumes                 128
Count of bad physical eraseblocks:       0
Count of reserved physical eraseblocks:  40
Current maximum erase counter value:     9
Minimum input/output unit size:          2048 bytes
Character device major/minor:            245:0
Present volumes:                         0

Volume ID:   0 (on ubi0)
Type:        dynamic
Alignment:   1
Size:        2002 LEBs (258306048 bytes, 246.3 MiB)
State:       OK
Name:        ubi_rfs
Character device major/minor: 245:1

root@vista:~# mount -t ubifs /dev/ubi0_0 www

Usage: mkfs.ubifs [OPTIONS] target
Make a UBIFS file system image from an existing directory tree
Options:
-r, -d, --root=DIR       build file system from directory DIR
-m, --min-io-size=SIZE   minimum I/O unit size
-e, --leb-size=SIZE      logical erase block size
-c, --max-leb-cnt=COUNT  maximum logical erase block count
-x, --compr=TYPE         compression type - "lzo", "favor_lzo", "zlib" or
                         "none" (default: "lzo")

# mkfs.ubifs -F -r </path/to/your/rootfs/tree> -m <min io size> -e <LEB size> -c 
        <Erase Blocks count> -o </path/to/output/Image.ubifs>
<min io size> Equals the page-size of the used NAND-Flash
<LEB size> Logical Erase Block size
<Erase Blocks count> maximum logical erase block count
mkfs.ubifs -m 2KiB -e 129024 -c 2048 -r ubifs-root -x zlib ubifs.img這些相關參數是先用ubiformat確認的, 不過-e是在mount failed的時候看dmesg的


Usage: ubinize [-o filename] [-p ] [-m ] [-s ] [-O ] [-e ]
                [-x ] [-Q ] [-v] [-h] [-V] [--output=] [--peb-size=]
                [--min-io-size=] [--sub-page-size=] [--vid-hdr-offset=]
                [--erase-counter=] [--ubi-ver=] [--image-seq=] [--verbose] [--help]
                [--version] ini-file
-p, --peb-size=       size of the physical eraseblock of the flash
                             this UBI image is created for in bytes,
                             kilobytes (KiB), or megabytes (MiB)
                             (mandatory parameter)
-m, --min-io-size=    minimum input/output unit size of the flash
                             in bytes
-O, --vid-hdr-offset=   offset if the VID header from start of the
                             physical eraseblock (default is the next
                             minimum I/O unit or sub-page after the EC
                             header)

# ubinize -vv -o <output image> -m <min io size> -p <PEB size>KiB 
        -s <subpage-size> -O <VID-hdr-offset> <configuration file>
<min io size> Equals the page-size of the used NAND-Flash
<PEB size> Physical Erase Block size (in KiB) - Equals the block size of
        the NAND-Flash
<Erase Blocks count> Count of the available Erase Blocks
<subpage-size> Subpage size in bytes. Default value is the minimum 
        input/output size (page-size)
<VID-hdr-offset> offset if the VID header from start of the PEB 
        (default is the next min I/O unit or sub-page after the EC header)
<configuration file> Path to a configuration file.

ubinize -o my.ubi -p 128KiB -m 2KiB -O 512 ubi.ini這些參數也是在ubiformat修正的, 如-O 512


    參考資料:
  • http://www.linux-mtd.infradead.org/faq/ubifs.html#L_loop_mount, Can UBIFS mount loop-back devices?
  • https://elinux.org/UBIFS, UBIFS
  • http://www.linux-mtd.infradead.org/faq/nand.html#L_nand_nandsim, How do I use NAND simulator?




2010年1月27日 星期三

Linux Kernel(10.3)- Command line partition table parsing


MTD Partition除了在code中寫死以外,其實還可以透過一些parsers來作規劃,這一章就要來教大家如何使用"Command line partition table parsing"。首先必須在kernel中啟用"Command line partition table parsing",請參照下圖。

這樣kernel就可以支援"Command line partition table parsing",然後我們還是拿mtdram.c的code來改(紅色的部份)。

/*
 * mtdram - a test mtd device
 * Author: Alexander Larsson <alex@cendio.se>
 *
 * Copyright (c) 1999 Alexander Larsson alex@cendio.se>
 * Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>
 *
 * This code is GPL
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/mtdram.h>
#include <linux/mtd/partitions.h>

static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
#define MTDRAM_TOTAL_SIZE (total_size * 1024)
#define MTDRAM_ERASE_SIZE (erase_size * 1024)

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
#endif

// We could store these in the mtd structure, but we only support 1 device..
static struct mtd_info *mtd_info;
static char partitioned = 0;

static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
    if (instr->addr + instr->len > mtd->size)
        return -EINVAL;

    memset((char *)mtd->priv + instr->addr, 0xff, instr->len);

    instr->state = MTD_ERASE_DONE;
    mtd_erase_callback(instr);

    return 0;
}

static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
        size_t *retlen, void **virt, resource_size_t *phys)
{
    if (from + len > mtd->size)
        return -EINVAL;

    /* can we return a physical address with this driver? */
    if (phys)
        return -EINVAL;

    *virt = mtd->priv + from;
    *retlen = len;
    return 0;
}

static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
{
}

/*
 * Allow NOMMU mmap() to directly map the device (if not NULL)
 * - return the address to which the offset maps
 * - return -ENOSYS to indicate refusal to do the mapping
 */
static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
                       unsigned long len,
                       unsigned long offset,
                       unsigned long flags)
{
    return (unsigned long) mtd->priv + offset;
}

static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
        size_t *retlen, u_char *buf)
{
    if (from + len > mtd->size)
        return -EINVAL;

    memcpy(buf, mtd->priv + from, len);

    *retlen = len;
    return 0;
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
        size_t *retlen, const u_char *buf)
{
    if (to + len > mtd->size)
        return -EINVAL;

    memcpy((char *)mtd->priv + to, buf, len);

    *retlen = len;
    return 0;
}

static void __exit cleanup_mtdram(void)
{
    if (mtd_info) {

        if (mtd_has_partitions() && partitioned) {
            del_mtd_partitions(mtd_info);
        } else {
            del_mtd_device(mtd_info);
        }

        vfree(mtd_info->priv);
        kfree(mtd_info);
    }
}

int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
        unsigned long size, char *name)
{
    memset(mtd, 0, sizeof(*mtd));

    /* Setup the MTD structure */
    mtd->name = name;
    mtd->type = MTD_RAM;
    mtd->flags = MTD_CAP_RAM;
    mtd->size = size;
    mtd->writesize = 1;
    mtd->erasesize = MTDRAM_ERASE_SIZE;
    mtd->priv = mapped_address;

    mtd->owner = THIS_MODULE;
    mtd->erase = ram_erase;
    mtd->point = ram_point;
    mtd->unpoint = ram_unpoint;
    mtd->get_unmapped_area = ram_get_unmapped_area;
    mtd->read = ram_read;
    mtd->write = ram_write;

    if (mtd_has_partitions()) {
        struct mtd_partition *mtd_parts = NULL;
        static const char *probes[] =
                    { "cmdlinepart", NULL };
        int nb_parts = 0;
        printk("has partitions\n");
        if (mtd_has_cmdlinepart()) {
            printk("has probs\n");
            nb_parts = parse_mtd_partitions(mtd, probes, &mtd_parts, 0);
        }
        if (nb_parts > 0) {
            printk("partitioned\n");
            partitioned = 1;
            return add_mtd_partitions(mtd, mtd_parts, nb_parts);
        }
    }

    if (add_mtd_device(mtd)) {
        return -EIO;
    }

    return 0;
}

static int __init init_mtdram(void)
{
    void *addr;
    int err;

    if (!total_size)
        return -EINVAL;

    /* Allocate some memory */
    mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
    if (!mtd_info)
        return -ENOMEM;

    addr = vmalloc(MTDRAM_TOTAL_SIZE);
    if (!addr) {
        kfree(mtd_info);
        mtd_info = NULL;
        return -ENOMEM;
    }
    err = mtdram_init_device(mtd_info, 
                  addr, MTDRAM_TOTAL_SIZE, "brook_flash");
    if (err) {
        vfree(addr);
        kfree(mtd_info);
        mtd_info = NULL;
        return err;
    }
    memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
    return err;
}

module_init(init_mtdram);
module_exit(cleanup_mtdram);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
MODULE_DESCRIPTION("Simulated MTD driver for testing");




Linux Kernel(10.2)- mtd partitions


在drivers/mtd/devices/mtdram.c中可以知道,透過add_mtd_device()/del_mtd_device()新增和刪除mtd device,但是並沒有規劃partition,在這邊將介紹add_mtd_partitions()/del_mtd_partitions()讓您可以透過這兩個函數為mtd切割partition。
此範例為drivers/mtd/devices/mtdram.c內容來修改而成(紅色粗體為修改部份)。
/*
 * mtdram - a test mtd device
 * Author: Alexander Larsson 
 *
 * Copyright (c) 1999 Alexander Larsson 
 * Copyright (c) 2005 Joern Engel 
 *
 * This code is GPL
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/mtdram.h>
#include <linux/mtd/partitions.h>

static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
#define MTDRAM_TOTAL_SIZE (total_size * 1024)
#define MTDRAM_ERASE_SIZE (erase_size * 1024)

static struct mtd_partition brook_partitions[] = {
    {
        .name = "part-1",
        .size = 0x00100000,
        .offset = 0x0000000
    }, {
        .name = "part-2",
        .size = 0x00100000,
        .offset = MTDPART_OFS_APPEND,
        .mask_flags = MTD_WRITEABLE
    }, {
        .name = "part-3",
        .offset = MTDPART_OFS_APPEND,
    },
};

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
#endif

// We could store these in the mtd structure, but we only support 1 device..
static struct mtd_info *mtd_info;

static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
    if (instr->addr + instr->len > mtd->size)
        return -EINVAL;

    memset((char *)mtd->priv + instr->addr, 0xff, instr->len);

    instr->state = MTD_ERASE_DONE;
    mtd_erase_callback(instr);

    return 0;
}

static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
        size_t *retlen, void **virt, resource_size_t *phys)
{
    if (from + len > mtd->size)
        return -EINVAL;

    /* can we return a physical address with this driver? */
    if (phys)
        return -EINVAL;

    *virt = mtd->priv + from;
    *retlen = len;。
    return 0;
}

static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
{
}

/*
 * Allow NOMMU mmap() to directly map the device (if not NULL)
 * - return the address to which the offset maps
 * - return -ENOSYS to indicate refusal to do the mapping
 */
static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
                       unsigned long len,
                       unsigned long offset,
                       unsigned long flags)
{
    return (unsigned long) mtd->priv + offset;
}

static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
        size_t *retlen, u_char *buf)
{
    if (from + len > mtd->size)
        return -EINVAL;

    memcpy(buf, mtd->priv + from, len);

    *retlen = len;
    return 0;
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
        size_t *retlen, const u_char *buf)
{
    if (to + len > mtd->size)
        return -EINVAL;

    memcpy((char *)mtd->priv + to, buf, len);

    *retlen = len;
    return 0;
}

static void __exit cleanup_mtdram(void)
{
    if (mtd_info) {
        del_mtd_partitions(mtd_info);
        vfree(mtd_info->priv);
        kfree(mtd_info);
    }
}

int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
        unsigned long size, char *name)
{
    memset(mtd, 0, sizeof(*mtd));

    /* Setup the MTD structure */
    mtd->name = name;
    mtd->type = MTD_RAM;
    mtd->flags = MTD_CAP_RAM;
    mtd->size = size;
    mtd->writesize = 1;
    mtd->erasesize = MTDRAM_ERASE_SIZE;
    mtd->priv = mapped_address;

    mtd->owner = THIS_MODULE;
    mtd->erase = ram_erase;
    mtd->point = ram_point;
    mtd->unpoint = ram_unpoint;
    mtd->get_unmapped_area = ram_get_unmapped_area;
    mtd->read = ram_read;
    mtd->write = ram_write;

    if (add_mtd_partitions(mtd, brook_partitions,
                ARRAY_SIZE(brook_partitions))) {
        return -EIO;
    }

    return 0;
}

static int __init init_mtdram(void)
{
    void *addr;
    int err;

    if (!total_size)
        return -EINVAL;

    /* Allocate some memory */
    mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
    if (!mtd_info)
        return -ENOMEM;

    addr = vmalloc(MTDRAM_TOTAL_SIZE);
    if (!addr) {
        kfree(mtd_info);
        mtd_info = NULL;
        return -ENOMEM;
    }
    err = mtdram_init_device(mtd_info, addr, 
                  MTDRAM_TOTAL_SIZE, "brook_flash");
    if (err) {
        vfree(addr);
        kfree(mtd_info);
        mtd_info = NULL;
        return err;
    }
    memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
    return err;
}

module_init(init_mtdram);
module_exit(cleanup_mtdram);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alexander Larsson >alexl@redhat.com>");
MODULE_DESCRIPTION("Simulated MTD driver for testing");
如果要切割partitions就必須要提供partitions的資訊,於是我們就宣告了一個名為brook_partitions的static struct mtd_partition,然後將原本呼叫add_mtd_device()/del_mtd_device()分別改成add_mtd_partitions()/del_mtd_partitions()就大功告成了。




2010年1月18日 星期一

Linux Kernel(10.1)- drivers/mtd/devices/mtdram.c


MTD的基本介紹可以參考MTD - Memory Technology Devices,這篇文章要透過drivers/mtd/devices/mtdram.c來了解mtd的driver如何運作。
static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
#define MTDRAM_TOTAL_SIZE (total_size * 1024)

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
#endif

static int __init init_mtdram(void)
{
 void *addr;
 int err;

 if (!total_size)
  return -EINVAL;

 /* Allocate some memory */
 mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
 if (!mtd_info)
  return -ENOMEM;

 addr = vmalloc(MTDRAM_TOTAL_SIZE);
 if (!addr) {
  kfree(mtd_info);
  mtd_info = NULL;
  return -ENOMEM;
 }
 err = mtdram_init_device(mtd_info, addr,
                   MTDRAM_TOTAL_SIZE, "mtdram test device");
 if (err) {
  vfree(addr);
  kfree(mtd_info);
  mtd_info = NULL;
  return err;
 }
 memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
 return err;
}
首先,看到init_mtdram(),在該function中,我們分配一塊記憶體給mtd_info,以及一塊v-memory給稍候模擬的flash用,接著就呼叫mtdram_init_device()進行mtd的註冊動作,(一個mtd partition需要一個mtd_info來存放所需的資訊),init_mtdram()後面就將mtd_info->priv(即v-memory)的內容全部設成0xff,這是因為一個空的flash裡面預設就是0xff。

int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  unsigned long size, char *name)
{
 memset(mtd, 0, sizeof(*mtd));

 /* Setup the MTD structure */
 mtd->name = name;
 mtd->type = MTD_RAM;
 mtd->flags = MTD_CAP_RAM;
 mtd->size = size;
 mtd->writesize = 1;
 mtd->erasesize = MTDRAM_ERASE_SIZE;
 mtd->priv = mapped_address;

 mtd->owner = THIS_MODULE;
 mtd->erase = ram_erase;
 mtd->point = ram_point;
 mtd->unpoint = ram_unpoint;
 mtd->get_unmapped_area = ram_get_unmapped_area;
 mtd->read = ram_read;
 mtd->write = ram_write;

 if (add_mtd_device(mtd)) {
  return -EIO;
 }

 return 0;
}
在mtdram_init_device()主要是填mtd_info相關資訊,然後呼叫add_mtd_device()進行註冊mtd的動作。呼叫del_mtd_device()進行移除mtd的工作。
point()/unpoint()可以參考http://www.linux-mtd.infradead.org/faq/general.html#L_point。

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
#endif

// We could store these in the mtd structure, but we only support 1 device.
static struct mtd_info *mtd_info;

static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
 if (instr->addr + instr->len > mtd->size)
  return -EINVAL;

 memset((char *)mtd->priv + instr->addr, 0xff, instr->len);

 instr->state = MTD_ERASE_DONE;
 mtd_erase_callback(instr);

 return 0;
}

static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  size_t *retlen, void **virt, resource_size_t *phys)
{
 if (from + len > mtd->size)
  return -EINVAL;

 /* can we return a physical address with this driver? */
 if (phys)
  return -EINVAL;

 *virt = mtd->priv + from;
 *retlen = len;
 return 0;
}

static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
{
}

/*
 * Allow NOMMU mmap() to directly map the device (if not NULL)
 * - return the address to which the offset maps
 * - return -ENOSYS to indicate refusal to do the mapping
 */
static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
        unsigned long len,
        unsigned long offset,
        unsigned long flags)
{
 return (unsigned long) mtd->priv + offset;
}

static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  size_t *retlen, u_char *buf)
{
 if (from + len > mtd->size)
  return -EINVAL;

 memcpy(buf, mtd->priv + from, len);

 *retlen = len;
 return 0;
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  size_t *retlen, const u_char *buf)
{
 if (to + len > mtd->size)
  return -EINVAL;

 memcpy((char *)mtd->priv + to, buf, len);

 *retlen = len;
 return 0;
}
剩下的read()/write()/erase()都是copy from/to memory和清成0xff,所以您可以發現讀寫NOR flash和讀寫memory差不多。



2010年1月16日 星期六

Linux Kernel(10)- MTD - Memory Technology Devices


說到MTD您就不得不親自拜訪一下MTD的官網(http://www.linux-mtd.infradead.org/),傳統上UNIX將device分成兩大類,char device和block device,char device就像鍵盤,可以讀資料,但卻不能做seek,也沒有固定大小,而block就像硬碟一樣,可以隨機存取某個位置(seek)。而MTD並不是char device也不是block device,因此建立了新的device類別,稱為MTD。
MTD subsystem提供一個抽象層(FTL)來存取flash device(如NAN、OneNAND、NOR等等),而我們一般用的USB flash因為有IC控制,以Linux的角度看起來就像block device,而不是一個原生的(raw) flash。

一般PC都不會接這些raw flash,不過我們可以透過一些simulate來練習這些device。
在insmod mtd.ko之後我可以透過/proc/mtd得知目前有哪些MTD,因為我們系統當然沒有MTD的device,所以可以insmod mtdram.ko安裝一個虛擬的MTD。

如果要能mount raw flash,還必須透過block device的介面存取,所以在安裝一下mtdblock.ko吧。

利用,mkfs.jffs2建立一個jffs2的image,再利用flashcp將image燒錄到flash中,最後就可以mount來用啦。

這一張圖是利用dd將flash的資料備份下來,再利用flashcp還原資料。



熱門文章