Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/flash Add support for defining partitions using a Li...



details:   https://anonhg.NetBSD.org/src/rev/ae87945da76b
branches:  trunk
changeset: 827814:ae87945da76b
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Mon Nov 13 17:35:58 2017 +0000

description:
Add support for defining partitions using a Linux-style "mtdparts" string.

diffstat:

 sys/dev/flash/files.flash      |    5 +-
 sys/dev/flash/flash.c          |   22 +---
 sys/dev/flash/flash.h          |    4 +-
 sys/dev/flash/flash_mtdparts.c |  196 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 209 insertions(+), 18 deletions(-)

diffs (286 lines):

diff -r b4ee852fd462 -r ae87945da76b sys/dev/flash/files.flash
--- a/sys/dev/flash/files.flash Mon Nov 13 15:01:16 2017 +0000
+++ b/sys/dev/flash/files.flash Mon Nov 13 17:35:58 2017 +0000
@@ -1,10 +1,11 @@
-# $NetBSD: files.flash,v 1.2 2011/06/28 18:14:11 ahoka Exp $
+# $NetBSD: files.flash,v 1.3 2017/11/13 17:35:58 jmcneill Exp $
 
-define flashbus        { [offset = 0], [size = 0], [readonly = 0] }
+define flashbus        { [offset = 0], [size = 0], [readonly = 0], [dynamic = 0] }
 
 device flash
 attach flash at flashbus
 file   dev/flash/flash.c               flash
 file   dev/flash/flash_io.c            flash
+file   dev/flash/flash_mtdparts.c      flash
 
 defflag opt_flash.h                    FLASH_STATIC_PARTITIONS
diff -r b4ee852fd462 -r ae87945da76b sys/dev/flash/flash.c
--- a/sys/dev/flash/flash.c     Mon Nov 13 15:01:16 2017 +0000
+++ b/sys/dev/flash/flash.c     Mon Nov 13 17:35:58 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: flash.c,v 1.13 2017/10/28 04:53:55 riastradh Exp $     */
+/*     $NetBSD: flash.c,v 1.14 2017/11/13 17:35:58 jmcneill Exp $      */
 
 /*-
  * Copyright (c) 2011 Department of Software Engineering,
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.13 2017/10/28 04:53:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.14 2017/11/13 17:35:58 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -147,20 +147,12 @@
 
        aprint_naive("\n");
 
-       switch (sc->flash_if->type) {
-       case FLASH_TYPE_NOR:
-               aprint_normal(": NOR flash partition size %s, offset %#jx",
-                       pbuf[0], (uintmax_t )sc->sc_partinfo.part_offset);
-               break;
+       aprint_normal(": partition");
+       if (sc->sc_partinfo.part_name != NULL)
+               aprint_normal(" \"%s\"", sc->sc_partinfo.part_name);
 
-       case FLASH_TYPE_NAND:
-               aprint_normal(": NAND flash partition size %s, offset %#jx",
-                       pbuf[0], (uintmax_t )sc->sc_partinfo.part_offset);
-               break;
-
-       default:
-               aprint_normal(": %s unknown flash", pbuf[0]);
-       }
+       aprint_normal(", size %s, offset %#jx",
+               pbuf[0], (uintmax_t)sc->sc_partinfo.part_offset);
 
        if (sc->sc_partinfo.part_flags & FLASH_PART_READONLY) {
                sc->sc_readonly = true;
diff -r b4ee852fd462 -r ae87945da76b sys/dev/flash/flash.h
--- a/sys/dev/flash/flash.h     Mon Nov 13 15:01:16 2017 +0000
+++ b/sys/dev/flash/flash.h     Mon Nov 13 17:35:58 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: flash.h,v 1.7 2011/07/29 20:48:33 ahoka Exp $  */
+/*     $NetBSD: flash.h,v 1.8 2017/11/13 17:35:58 jmcneill Exp $       */
 
 /*-
  * Copyright (c) 2011 Department of Software Engineering,
@@ -53,6 +53,7 @@
        flash_off_t part_offset;
        flash_size_t part_size;
        int part_flags;
+       const char *part_name;
 };
 
 /**
@@ -120,6 +121,7 @@
 };
 
 device_t flash_attach_mi(struct flash_interface *, device_t);
+void flash_attach_mtdparts(struct flash_interface *, device_t, flash_size_t, const char *, const char *);
 const struct flash_interface *flash_get_interface(dev_t);
 const struct flash_softc *flash_get_softc(dev_t);
 device_t flash_get_device(dev_t);
diff -r b4ee852fd462 -r ae87945da76b sys/dev/flash/flash_mtdparts.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/flash/flash_mtdparts.c    Mon Nov 13 17:35:58 2017 +0000
@@ -0,0 +1,196 @@
+/* $NetBSD: flash_mtdparts.c,v 1.1 2017/11/13 17:35:58 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2017 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: flash_mtdparts.c,v 1.1 2017/11/13 17:35:58 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/systm.h>
+#include <sys/kmem.h>
+
+#include <dev/flash/flash.h>
+
+extern int flash_print(void *, const char *);
+
+static void
+flash_attach_partition(struct flash_interface *flash_if, device_t parent,
+    struct flash_partition *part)
+{
+       struct flash_attach_args faa;   
+
+       faa.flash_if = flash_if;
+       faa.partinfo = *part;
+
+       config_found_ia(parent, "flashbus", &faa, flash_print);
+}
+
+static flash_size_t
+flash_parse_size(char *partdef, char **ep)
+{
+       flash_size_t size;
+
+       /* Parse the size parameter */
+       size = strtoul(partdef, ep, 10);
+       if (partdef == *ep)
+               return 0;
+
+       switch (**ep) {
+       case 'G':
+       case 'g':
+               size <<= 10;
+               /* FALLTHROUGH */
+       case 'M':
+       case 'm':
+               size <<= 10;
+               /* FALLTHROUGH */
+       case 'K':
+       case 'k':
+               size <<= 10;
+               (*ep)++;
+               break;
+       }
+
+       return size;
+}
+
+static int
+flash_parse_partdef(flash_size_t flash_size, char *partdef, flash_off_t *offset,
+    struct flash_partition *ppart)
+{
+       struct flash_partition part;
+
+       /* Get the partition size */
+       if (*partdef == '-') {
+               /* Use the remaining space */
+               part.part_size = 0;
+               partdef++;
+       } else {
+               part.part_size = flash_parse_size(partdef, &partdef);
+               if (part.part_size == 0)
+                       return EINVAL;
+       }
+
+       if (*partdef == '@') {
+               /* Explicit offset */
+               partdef++;
+               part.part_offset = flash_parse_size(partdef, &partdef);
+       } else {
+               /* Offset is the end of the previous partition */
+               part.part_offset = *offset;
+       }
+
+       /* Calculate partition size for "all remaining space" parts */
+       if (part.part_size == 0)
+               part.part_size = flash_size - part.part_offset;
+
+       if (*partdef == '(') {
+               /* Partition name */
+               partdef++;
+               part.part_name = partdef;
+               partdef = strchr(partdef, ')');
+               if (partdef == NULL)
+                       return EINVAL;
+               *partdef = '\0';
+               partdef++;
+       }
+
+       part.part_flags = 0;
+       if (strncmp(partdef, "ro", 2) == 0) {
+               part.part_flags |= FLASH_PART_READONLY;
+               partdef += 2;
+       }
+
+       *ppart = part;
+
+       *offset = part.part_offset + part.part_size;
+
+       return 0;
+}
+
+static void
+flash_parse_mtddef(struct flash_interface *flash_if, device_t parent,
+    flash_size_t flash_size, char *mtddef)
+{
+       struct flash_partition part;
+       char *partdef = mtddef, *nextdef;
+       flash_off_t offset = 0;
+       int error;
+
+       while (partdef && offset < flash_size) {
+               /* Find the end */
+               nextdef = strchr(partdef, ',');
+               if (nextdef == NULL)
+                   nextdef = strchr(partdef, ' ');
+               if (nextdef)
+                       *nextdef++ = '\0';
+
+               error = flash_parse_partdef(flash_size, partdef, &offset,
+                   &part);
+               if (error) {
+                       aprint_error_dev(parent, "bad partition def '%s'\n",
+                           partdef);
+                       return;
+               }
+
+               flash_attach_partition(flash_if, parent, &part);
+
+               partdef = nextdef;
+       }
+}
+
+/*
+ * Attach partitions to a given parent device node that match the supplied
+ * device id. The cmdline follows the following format:
+ *
+ * mtdparts=<mtddef>[;<mtddef]
+ * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
+ * <partdef> := <size>[@offset][<name>][ro]
+ * <mtd-id>  := unique id used in mapping driver/device (number of flash bank)
+ * <size>    := memsize OR "-" to denote all remaining space
+ * <name>    := '(' NAME ')'
+ */
+void
+flash_attach_mtdparts(struct flash_interface *flash_if, device_t parent,
+    flash_size_t flash_size, const char *mtd_id, const char *cmdline)
+{
+       char *mtddef;
+       size_t mtddeflen;
+
+       /* Find the definition for our mtd id */
+       const char *s = strstr(cmdline, mtd_id);
+       if (s == NULL || s[strlen(mtd_id)] != ':')
+               return;
+
+       mtddef = kmem_strdupsize(s + strlen(mtd_id) + 1, &mtddeflen, KM_SLEEP);
+
+       flash_parse_mtddef(flash_if, parent, flash_size, mtddef);
+
+       kmem_free(mtddef, mtddeflen);
+}



Home | Main Index | Thread Index | Old Index