^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Read flash partition table from command line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2002 SYSGO Real-Time Solutions GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * The format for the command line is as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * mtdparts=<mtddef>[;<mtddef]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * <mtddef> := <mtd-id>:<partdef>[,<partdef>]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * <partdef> := <size>[@<offset>][<name>][ro][lk][slc]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * <mtd-id> := unique name used in mapping driver/device (mtd->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * <size> := standard linux memsize OR "-" to denote all remaining space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * size is automatically truncated at end of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * if specified or truncated size is 0 the part is skipped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * <offset> := standard linux memsize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * if omitted the part will immediately follow the previous part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * or 0 if the first part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * <name> := '(' NAME ')'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * NAME will appear in /proc/mtd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * <size> and <offset> can be specified such that the parts are out of order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * in physical memory and may even overlap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * The parts are assigned MTD numbers in the order they are specified in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * command line regardless of their order in physical memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * 1 NOR Flash, with 1 single writable partition:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * edb7312-nor:-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * 1 NOR Flash with 2 partitions, 1 NAND with one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define pr_fmt(fmt) "mtd: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* debug macro */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define dbg(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* special size referring to all the remaining space in a partition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define SIZE_REMAINING ULLONG_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define OFFSET_CONTINUOUS ULLONG_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct cmdline_mtd_partition {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct cmdline_mtd_partition *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) char *mtd_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int num_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct mtd_partition *parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* mtdpart_setup() parses into here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct cmdline_mtd_partition *partitions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* the command line passed to mtdpart_setup() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static char *mtdparts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static char *cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int cmdline_parsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Parse one partition definition for an MTD. Since there can be many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * comma separated partition definitions, this function calls itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * recursively until no more partition definitions are found. Nice side
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * effect: the memory to keep the mtd_partition structs and the names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * is allocated upon the last definition being found. At that point the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * syntax has been verified ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct mtd_partition * newpart(char *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) char **retptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int *num_parts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int this_part,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned char **extra_mem_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int extra_mem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct mtd_partition *parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned long long size, offset = OFFSET_CONTINUOUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned char *extra_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) char delim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned int mask_flags, add_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* fetch the partition size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (*s == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* assign all remaining space to this partition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) size = SIZE_REMAINING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) size = memparse(s, &s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_err("partition has size 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* fetch partition name and flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) mask_flags = 0; /* this is going to be a regular partition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) add_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) delim = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* check for offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (*s == '@') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) offset = memparse(s, &s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* now look for name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (*s == '(')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) delim = ')';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (delim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) name = ++s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) p = strchr(name, delim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_err("no closing %c found in partition name\n", delim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) name_len = p - name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) s = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) name_len = 13; /* Partition_000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* record name length for memory allocation later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) extra_mem_size += name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* test for options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (strncmp(s, "ro", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mask_flags |= MTD_WRITEABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) s += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* if lk is found do NOT unlock the MTD partition*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (strncmp(s, "lk", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mask_flags |= MTD_POWERUP_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) s += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* if slc is found use emulated SLC mode on this partition*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!strncmp(s, "slc", 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) add_flags |= MTD_SLC_ON_MLC_EMULATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) s += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* test if more partitions are following */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (*s == ',') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (size == SIZE_REMAINING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pr_err("no partitions allowed after a fill-up partition\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* more partitions follow, parse them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) parts = newpart(s + 1, &s, num_parts, this_part + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) &extra_mem, extra_mem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (IS_ERR(parts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* this is the last partition: allocate space for all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *num_parts = this_part + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) alloc_size = *num_parts * sizeof(struct mtd_partition) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) extra_mem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) parts = kzalloc(alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!parts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) extra_mem = (unsigned char *)(parts + *num_parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * enter this partition (offset will be calculated later if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * OFFSET_CONTINUOUS at this point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) parts[this_part].size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) parts[this_part].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) parts[this_part].mask_flags = mask_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) parts[this_part].add_flags = add_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) strlcpy(extra_mem, name, name_len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sprintf(extra_mem, "Partition_%03d", this_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) parts[this_part].name = extra_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) extra_mem += name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) this_part, parts[this_part].name, parts[this_part].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) parts[this_part].size, parts[this_part].mask_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* return (updated) pointer to extra_mem memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (extra_mem_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) *extra_mem_ptr = extra_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* return (updated) pointer command line string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *retptr = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* return partition table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Parse the command line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int mtdpart_setup_real(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) cmdline_parsed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) for( ; s != NULL; )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct cmdline_mtd_partition *this_mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct mtd_partition *parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int mtd_id_len, num_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) char *p, *mtd_id, *semicol, *open_parenth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Replace the first ';' by a NULL char so strrchr can work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) semicol = strchr(s, ';');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (semicol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *semicol = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * make sure that part-names with ":" will not be handled as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * part of the mtd-id with an ":"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) open_parenth = strchr(s, '(');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (open_parenth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *open_parenth = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) mtd_id = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * fetch <mtd-id>. We use strrchr to ignore all ':' that could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * be present in the MTD name, only the last one is interpreted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * as an <mtd-id>/<part-definition> separator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) p = strrchr(s, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* Restore the '(' now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (open_parenth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *open_parenth = '(';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Restore the ';' now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (semicol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *semicol = ';';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pr_err("no mtd-id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) mtd_id_len = p - mtd_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dbg(("parsing <%s>\n", p+1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * parse one mtd. have it reserve memory for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * struct cmdline_mtd_partition and the mtd-id string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) parts = newpart(p + 1, /* cmdline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) &s, /* out: updated cmdline ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) &num_parts, /* out: number of parts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 0, /* first partition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) (unsigned char**)&this_mtd, /* out: extra mem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) mtd_id_len + 1 + sizeof(*this_mtd) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) sizeof(void*)-1 /*alignment*/);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (IS_ERR(parts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * An error occurred. We're either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * a) out of memory, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * b) in the middle of the partition spec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Either way, this mtd is hosed and we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * unlikely to succeed in parsing any more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return PTR_ERR(parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* align this_mtd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) this_mtd = (struct cmdline_mtd_partition *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ALIGN((unsigned long)this_mtd, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* enter results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) this_mtd->parts = parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) this_mtd->num_parts = num_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) this_mtd->mtd_id = (char*)(this_mtd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* link into chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) this_mtd->next = partitions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) partitions = this_mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) dbg(("mtdid=<%s> num_parts=<%d>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) this_mtd->mtd_id, this_mtd->num_parts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* EOS - we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (*s == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* does another spec follow? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (*s != ';') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) pr_err("bad character after partition (%c)\n", *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * Main function to be called from the MTD mapping driver/device to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * obtain the partitioning information. At this point the command line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * arguments will actually be parsed and turned to struct mtd_partition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * information. It returns partitions for the requested mtd device, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * the first one in the chain if a NULL mtd_id is passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static int parse_cmdline_partitions(struct mtd_info *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) const struct mtd_partition **pparts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct mtd_part_parser_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) unsigned long long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct cmdline_mtd_partition *part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) const char *mtd_id = master->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* parse command line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!cmdline_parsed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err = mtdpart_setup_real(cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * Search for the partition definition matching master->name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * If master->name is not set, stop at first partition definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) for (part = partitions; part; part = part->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) for (i = 0, offset = 0; i < part->num_parts; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (part->parts[i].offset == OFFSET_CONTINUOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) part->parts[i].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) offset = part->parts[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (part->parts[i].size == SIZE_REMAINING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) part->parts[i].size = master->size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (offset + part->parts[i].size > master->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) pr_warn("%s: partitioning exceeds flash size, truncating\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) part->mtd_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) part->parts[i].size = master->size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) offset += part->parts[i].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (part->parts[i].size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) pr_warn("%s: skipping zero sized partition\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) part->mtd_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) part->num_parts--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) memmove(&part->parts[i], &part->parts[i + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) sizeof(*part->parts) * (part->num_parts - i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (!*pparts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return part->num_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * This is the handler for our kernel parameter, called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * main.c::checksetup(). Note that we can not yet kmalloc() anything,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * so we only save the commandline for later processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * This function needs to be visible for bootloaders.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int __init mtdpart_setup(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) cmdline = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) __setup("mtdparts=", mtdpart_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static struct mtd_part_parser cmdline_parser = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .parse_fn = parse_cmdline_partitions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) .name = "cmdlinepart",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static int __init cmdline_parser_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (mtdparts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) mtdpart_setup(mtdparts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) register_mtd_parser(&cmdline_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static void __exit cmdline_parser_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) deregister_mtd_parser(&cmdline_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) module_init(cmdline_parser_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) module_exit(cmdline_parser_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) MODULE_PARM_DESC(mtdparts, "Partitioning specification");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) module_param(mtdparts, charp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) MODULE_DESCRIPTION("Command line configuration of MTD partitions");