linux-old/drivers/mtd/cmdlinepart.c
<<
>>
Prefs
   1/*
   2 * $Id: cmdlinepart.c,v 1.6 2002/11/16 01:37:39 dneuer Exp $
   3 *
   4 * Read flash partition table from command line
   5 *
   6 * Copyright 2002 SYSGO Real-Time Solutions GmbH
   7 *
   8 * The format for the command line is as follows:
   9 * 
  10 * mtdparts=<mtddef>[;<mtddef]
  11 * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
  12 * <partdef> := <size>[@offset][<name>][ro]
  13 * <mtd-id>  := unique id used in mapping driver/device
  14 * <size>    := standard linux memsize OR "-" to denote all remaining space
  15 * <name>    := '(' NAME ')'
  16 * 
  17 * Examples:
  18 * 
  19 * 1 NOR Flash, with 1 single writable partition:
  20 * edb7312-nor:-
  21 * 
  22 * 1 NOR Flash with 2 partitions, 1 NAND with one
  23 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  24 */
  25
  26#include <linux/kernel.h>
  27#include <linux/slab.h>
  28
  29#include <linux/mtd/mtd.h>
  30#include <linux/mtd/partitions.h>
  31#include <asm/setup.h>
  32#include <linux/bootmem.h>
  33
  34/* error message prefix */
  35#define ERRP "mtd: "
  36
  37/* debug macro */
  38#if 0
  39#define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
  40#else
  41#define dbg(x)
  42#endif
  43
  44
  45/* special size referring to all the remaining space in a partition */
  46#define SIZE_REMAINING 0xffffffff
  47
  48struct cmdline_mtd_partition {
  49        struct cmdline_mtd_partition *next;
  50        char *mtd_id;
  51        int num_parts;
  52        struct mtd_partition *parts;
  53};
  54
  55/* mtdpart_setup() parses into here */
  56static struct cmdline_mtd_partition *partitions;
  57
  58/* the command line passed to mtdpart_setupd() */
  59static char *cmdline;
  60static int cmdline_parsed = 0;
  61
  62/*
  63 * Parse one partition definition for an MTD. Since there can be many
  64 * comma separated partition definitions, this function calls itself 
  65 * recursively until no more partition definitions are found. Nice side
  66 * effect: the memory to keep the mtd_partition structs and the names
  67 * is allocated upon the last definition being found. At that point the
  68 * syntax has been verified ok.
  69 */
  70static struct mtd_partition * newpart(char *s, 
  71                                      char **retptr,
  72                                      int *num_parts,
  73                                      int this_part, 
  74                                      unsigned char **extra_mem_ptr, 
  75                                      int extra_mem_size)
  76{
  77        struct mtd_partition *parts;
  78        unsigned long size;
  79        unsigned long offset = 0;
  80        char *name;
  81        int name_len;
  82        unsigned char *extra_mem;
  83        char delim;
  84        unsigned int mask_flags;
  85
  86        /* fetch the partition size */
  87        if (*s == '-')
  88        {       /* assign all remaining space to this partition */
  89                size = SIZE_REMAINING;
  90                s++;
  91        }
  92        else
  93        {
  94                size = memparse(s, &s);
  95                if (size < PAGE_SIZE)
  96                {
  97                        printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
  98                        return 0;
  99                }
 100        }
 101
 102        /* fetch partition name and flags */
 103        mask_flags = 0; /* this is going to be a regular partition */
 104        delim = 0;
 105        /* check for offset */
 106        if (*s == '@') 
 107        {
 108                s++;
 109                offset = memparse(s, &s);
 110        }
 111        /* now look for name */
 112        if (*s == '(')
 113        {
 114                delim = ')';
 115        }
 116                
 117        if (delim)
 118        {
 119                char *p;
 120
 121                name = ++s;
 122                if ((p = strchr(name, delim)) == 0)
 123                {
 124                        printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
 125                        return 0;
 126                }
 127                name_len = p - name;
 128                s = p + 1;
 129        }
 130        else
 131        {
 132                name = NULL;
 133                name_len = 13; /* Partition_000 */
 134        }
 135   
 136        /* record name length for memory allocation later */
 137        extra_mem_size += name_len + 1;
 138
 139        /* test for options */
 140        if (strncmp(s, "ro", 2) == 0) 
 141        {
 142                mask_flags |= MTD_WRITEABLE;
 143                s += 2;
 144        }
 145
 146        /* test if more partitions are following */
 147        if (*s == ',')
 148        {
 149                if (size == SIZE_REMAINING)
 150                {
 151                        printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
 152                        return 0;
 153                }
 154                /* more partitions follow, parse them */
 155                if ((parts = newpart(s + 1, &s, num_parts, 
 156                                     this_part + 1, &extra_mem, extra_mem_size)) == 0)
 157                  return 0;
 158        }
 159        else
 160        {       /* this is the last partition: allocate space for all */
 161                int alloc_size;
 162
 163                *num_parts = this_part + 1;
 164                alloc_size = *num_parts * sizeof(struct mtd_partition) +
 165                             extra_mem_size;
 166                parts = kmalloc(alloc_size, GFP_KERNEL);
 167                if (!parts)
 168                {
 169                        printk(KERN_ERR ERRP "out of memory\n");
 170                        return 0;
 171                }
 172                memset(parts, 0, alloc_size);
 173                extra_mem = (unsigned char *)(parts + *num_parts);
 174        }
 175        /* enter this partition (offset will be calculated later if it is zero at this point) */
 176        parts[this_part].size = size;
 177        parts[this_part].offset = offset;
 178        parts[this_part].mask_flags = mask_flags;
 179        if (name)
 180        {
 181                strncpy(extra_mem, name, name_len);
 182                extra_mem[name_len] = 0;
 183        }
 184        else
 185        {
 186                sprintf(extra_mem, "Partition_%03d", this_part);
 187        }
 188        parts[this_part].name = extra_mem;
 189        extra_mem += name_len + 1;
 190
 191        dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
 192             this_part, 
 193             parts[this_part].name,
 194             parts[this_part].offset,
 195             parts[this_part].size,
 196             parts[this_part].mask_flags));
 197
 198        /* return (updated) pointer to extra_mem memory */
 199        if (extra_mem_ptr)
 200          *extra_mem_ptr = extra_mem;
 201
 202        /* return (updated) pointer command line string */
 203        *retptr = s;
 204
 205        /* return partition table */
 206        return parts;
 207}
 208
 209/* 
 210 * Parse the command line. 
 211 */
 212static int mtdpart_setup_real(char *s)
 213{
 214        cmdline_parsed = 1;
 215
 216        for( ; s != NULL; )
 217        {
 218                struct cmdline_mtd_partition *this_mtd;
 219                struct mtd_partition *parts;
 220                int mtd_id_len;
 221                int num_parts;
 222                char *p, *mtd_id;
 223
 224                mtd_id = s;
 225                /* fetch <mtd-id> */
 226                if (!(p = strchr(s, ':')))
 227                {
 228                        printk(KERN_ERR ERRP "no mtd-id\n");
 229                        return 0;
 230                }
 231                mtd_id_len = p - mtd_id;
 232
 233                dbg(("parsing <%s>\n", p+1));
 234
 235                /* 
 236                 * parse one mtd. have it reserve memory for the
 237                 * struct cmdline_mtd_partition and the mtd-id string.
 238                 */
 239                parts = newpart(p + 1,          /* cmdline */
 240                                &s,             /* out: updated cmdline ptr */
 241                                &num_parts,     /* out: number of parts */
 242                                0,              /* first partition */
 243                                (unsigned char**)&this_mtd, /* out: extra mem */
 244                                mtd_id_len + 1 + sizeof(*this_mtd));
 245                if(!parts)
 246                {
 247                        /*
 248                         * An error occurred. We're either:
 249                         * a) out of memory, or
 250                         * b) in the middle of the partition spec
 251                         * Either way, this mtd is hosed and we're
 252                         * unlikely to succeed in parsing any more
 253                         */
 254                         return 0;
 255                 }
 256
 257                /* enter results */         
 258                this_mtd->parts = parts;
 259                this_mtd->num_parts = num_parts;
 260                this_mtd->mtd_id = (char*)(this_mtd + 1);
 261                strncpy(this_mtd->mtd_id, mtd_id, mtd_id_len);
 262                this_mtd->mtd_id[mtd_id_len] = 0;
 263
 264                /* link into chain */
 265                this_mtd->next = partitions;            
 266                partitions = this_mtd;
 267
 268                dbg(("mtdid=<%s> num_parts=<%d>\n", 
 269                     this_mtd->mtd_id, this_mtd->num_parts));
 270                
 271
 272                /* EOS - we're done */
 273                if (*s == 0)
 274                        break;
 275
 276                /* does another spec follow? */
 277                if (*s != ';')
 278                {
 279                        printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
 280                        return 0;
 281                }
 282                s++;
 283        }
 284        return 1;
 285}
 286
 287/*
 288 * Main function to be called from the MTD mapping driver/device to
 289 * obtain the partitioning information. At this point the command line
 290 * arguments will actually be parsed and turned to struct mtd_partition
 291 * information. It returns partitions for the requested mtd device, or
 292 * the first one in the chain if a NULL mtd_id is passed in.
 293 */
 294int parse_cmdline_partitions(struct mtd_info *master, 
 295                             struct mtd_partition **pparts,
 296                             const char *mtd_id)
 297{
 298        unsigned long offset;
 299        int i;
 300        struct cmdline_mtd_partition *part;
 301
 302        if(!cmdline)
 303                return -EINVAL;
 304
 305        /* parse command line */
 306        if (!cmdline_parsed)
 307                mtdpart_setup_real(cmdline);
 308
 309        for(part = partitions; part; part = part->next)
 310        {
 311                if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
 312                {
 313                        for(i = 0, offset = 0; i < part->num_parts; i++)
 314                        {
 315                                if (!part->parts[i].offset)
 316                                  part->parts[i].offset = offset;
 317                                else
 318                                  offset = part->parts[i].offset;
 319                                if (part->parts[i].size == SIZE_REMAINING)
 320                                  part->parts[i].size = master->size - offset;
 321                                if (offset + part->parts[i].size > master->size)
 322                                {
 323                                        printk(KERN_WARNING ERRP 
 324                                               "%s: partitioning exceeds flash size, truncating\n",
 325                                               part->mtd_id);
 326                                        part->parts[i].size = master->size - offset;
 327                                        part->num_parts = i;
 328                                }
 329                                offset += part->parts[i].size;
 330                        }
 331                        *pparts = part->parts;
 332                        return part->num_parts;
 333                }
 334        }
 335        return -EINVAL;
 336}
 337
 338
 339/* 
 340 * This is the handler for our kernel parameter, called from 
 341 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
 342 * so we only save the commandline for later processing.
 343 */
 344static int __init mtdpart_setup(char *s)
 345{
 346        cmdline = s;
 347        return 1;
 348}
 349
 350__setup("mtdparts=", mtdpart_setup);
 351
 352EXPORT_SYMBOL(parse_cmdline_partitions);
 353
 354MODULE_LICENSE("GPL");
 355MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
 356MODULE_DESCRIPTION("Command line configuration of MTD partitions");
 357