2 * Parse MyLoader-style flash partition tables and produce a Linux partition
5 * Copyright (C) 2007 OpenWrt.org
6 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
8 * This file was based on drivers/mtd/redboot.c
9 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/vmalloc.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/partitions.h>
35 #include <linux/byteorder/generic.h>
36 #include <asm/mach-adm5120/myloader.h>
38 #define NAME_LEN_MAX 20
39 #define NAME_MYLOADER "MyLoader"
40 #define NAME_PARTITION_TABLE "Partition Table"
41 #define BLOCK_LEN_MIN 0x10000
43 static int parse_myloader_partitions(struct mtd_info *master,
44 struct mtd_partition **pparts,
47 struct mylo_partition_table *tab;
48 struct mylo_partition *part;
49 struct mtd_partition *mtd_parts;
50 struct mtd_partition *mtd_part;
57 unsigned long blocklen;
59 tab = vmalloc(sizeof(*tab));
65 blocklen = master->erasesize;
66 if (blocklen < BLOCK_LEN_MIN)
67 blocklen = BLOCK_LEN_MIN;
69 /* Partition Table is always located on the second erase block */
71 printk(KERN_NOTICE "Searching for MyLoader partition table "
72 "in %s at offset 0x%lx\n", master->name, offset);
74 ret = master->read(master, offset, sizeof(*tab), &retlen,
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 "No MyLoader partition table detected "
88 "in %s\n", master->name);
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)
107 mtd_parts = kzalloc((num_parts*sizeof(*mtd_part) + num_parts*NAME_LEN_MAX),
115 mtd_part = mtd_parts;
116 names = (char *)&mtd_parts[num_parts];
118 strcpy(NAME_MYLOADER, names);
119 mtd_part->name = names;
120 mtd_part->offset = 0;
121 mtd_part->size = blocklen;
123 names += NAME_LEN_MAX;
125 strcpy(NAME_PARTITION_TABLE, names);
126 mtd_part->name = names;
127 mtd_part->offset = blocklen;
128 mtd_part->size = blocklen;
130 names += NAME_LEN_MAX;
132 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
133 part = &tab->partitions[i];
135 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
138 sprintf(names, "partition%d", i);
139 mtd_part->name = names;
140 mtd_part->offset = le32_to_cpu(part->addr);
141 mtd_part->size = le32_to_cpu(part->size);
143 names += NAME_LEN_MAX;
155 static struct mtd_part_parser mylo_mtd_parser = {
156 .owner = THIS_MODULE,
157 .parse_fn = parse_myloader_partitions,
158 .name = NAME_MYLOADER,
161 static int __init mylo_mtd_parser_init(void)
163 return register_mtd_parser(&mylo_mtd_parser);
166 static void __exit mylo_mtd_parser_exit(void)
168 deregister_mtd_parser(&mylo_mtd_parser);
171 module_init(mylo_mtd_parser_init);
172 module_exit(mylo_mtd_parser_exit);
174 EXPORT_SYMBOL_GPL(parse_myloader_partitions);
176 MODULE_AUTHOR("Gabor Juhos <juhosg@freemail.hu>");
177 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
178 MODULE_LICENSE("GPL");