4 * Parse MyLoader-style flash partition tables and produce a Linux partition
7 * Copyright (C) 2007 OpenWrt.org
8 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
10 * This file was based on drivers/mtd/redboot.c
11 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the
25 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
37 #include <linux/byteorder/generic.h>
39 #include <prom/myloader.h>
41 #define NAME_LEN_MAX 20
42 #define NAME_MYLOADER "MyLoader"
43 #define NAME_PARTITION_TABLE "Partition Table"
44 #define BLOCK_LEN_MIN 0x10000
46 int parse_myloader_partitions(struct mtd_info *master,
47 struct mtd_partition **pparts,
50 struct mylo_partition_table *tab;
51 struct mylo_partition *part;
52 struct mtd_partition *mtd_parts;
53 struct mtd_partition *mtd_part;
59 unsigned long blocklen;
61 tab = vmalloc(sizeof(*tab));
67 blocklen = master->erasesize;
68 if (blocklen < BLOCK_LEN_MIN)
69 blocklen = BLOCK_LEN_MIN;
71 /* Partition Table is always located on the second erase block */
73 printk(KERN_NOTICE "%s: searching for MyLoader partition table at "
74 "offset 0x%lx\n", master->name, offset);
76 ret = master->read(master, offset, sizeof(*tab), &retlen, (void *)tab);
80 if (retlen != sizeof(*tab)) {
85 /* Check for Partition Table magic number */
86 if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
87 printk(KERN_NOTICE "%s: no MyLoader partition table found\n",
93 /* The MyLoader and the Partition Table is always present */
96 /* Detect number of used partitions */
97 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
98 part = &tab->partitions[i];
100 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
106 mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
107 num_parts * NAME_LEN_MAX), GFP_KERNEL);
114 mtd_part = mtd_parts;
115 names = (char *)&mtd_parts[num_parts];
117 strncpy(names, NAME_MYLOADER, NAME_LEN_MAX-1);
118 mtd_part->name = names;
119 mtd_part->offset = 0;
120 mtd_part->size = blocklen;
121 mtd_part->mask_flags = MTD_WRITEABLE;
123 names += NAME_LEN_MAX;
125 strncpy(names, NAME_PARTITION_TABLE, NAME_LEN_MAX-1);
126 mtd_part->name = names;
127 mtd_part->offset = blocklen;
128 mtd_part->size = blocklen;
129 mtd_part->mask_flags = MTD_WRITEABLE;
131 names += NAME_LEN_MAX;
133 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
134 part = &tab->partitions[i];
136 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
139 sprintf(names, "partition%d", i);
140 mtd_part->offset = le32_to_cpu(part->addr);
141 mtd_part->size = le32_to_cpu(part->size);
142 mtd_part->name = names;
144 names += NAME_LEN_MAX;
156 static struct mtd_part_parser mylo_mtd_parser = {
157 .owner = THIS_MODULE,
158 .parse_fn = parse_myloader_partitions,
159 .name = NAME_MYLOADER,
162 static int __init mylo_mtd_parser_init(void)
164 return register_mtd_parser(&mylo_mtd_parser);
167 static void __exit mylo_mtd_parser_exit(void)
169 deregister_mtd_parser(&mylo_mtd_parser);
172 module_init(mylo_mtd_parser_init);
173 module_exit(mylo_mtd_parser_exit);
175 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
176 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
177 MODULE_LICENSE("GPL");