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
;
60 unsigned long blocklen
;
62 tab
= vmalloc(sizeof(*tab
));
68 blocklen
= master
->erasesize
;
69 if (blocklen
< BLOCK_LEN_MIN
)
70 blocklen
= BLOCK_LEN_MIN
;
72 /* Partition Table is always located on the second erase block */
74 printk(KERN_NOTICE
"%s: searching for MyLoader partition table at "
75 "offset 0x%lx\n", master
->name
, offset
);
77 ret
= master
->read(master
, offset
, sizeof(*tab
), &retlen
, (void *)tab
);
81 if (retlen
!= sizeof(*tab
)) {
86 /* Check for Partition Table magic number */
87 if (tab
->magic
!= le32_to_cpu(MYLO_MAGIC_PARTITIONS
)) {
88 printk(KERN_NOTICE
"%s: no MyLoader partition table found\n",
94 /* The MyLoader and the Partition Table is always present */
97 /* Detect number of used partitions */
98 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
99 part
= &tab
->partitions
[i
];
101 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 strncpy(names
, NAME_MYLOADER
, NAME_LEN_MAX
-1);
119 mtd_part
->name
= names
;
120 mtd_part
->offset
= 0;
121 mtd_part
->size
= blocklen
;
122 mtd_part
->mask_flags
= MTD_WRITEABLE
;
124 names
+= NAME_LEN_MAX
;
126 strncpy(names
, NAME_PARTITION_TABLE
, NAME_LEN_MAX
-1);
127 mtd_part
->name
= names
;
128 mtd_part
->offset
= blocklen
;
129 mtd_part
->size
= blocklen
;
130 mtd_part
->mask_flags
= MTD_WRITEABLE
;
132 names
+= NAME_LEN_MAX
;
134 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
135 part
= &tab
->partitions
[i
];
137 if (le16_to_cpu(part
->type
) == PARTITION_TYPE_FREE
)
140 sprintf(names
, "partition%d", i
);
141 mtd_part
->offset
= le32_to_cpu(part
->addr
);
142 mtd_part
->size
= le32_to_cpu(part
->size
);
143 mtd_part
->name
= names
;
145 names
+= NAME_LEN_MAX
;
157 static struct mtd_part_parser mylo_mtd_parser
= {
158 .owner
= THIS_MODULE
,
159 .parse_fn
= parse_myloader_partitions
,
160 .name
= NAME_MYLOADER
,
163 static int __init
mylo_mtd_parser_init(void)
165 return register_mtd_parser(&mylo_mtd_parser
);
168 static void __exit
mylo_mtd_parser_exit(void)
170 deregister_mtd_parser(&mylo_mtd_parser
);
173 module_init(mylo_mtd_parser_init
);
174 module_exit(mylo_mtd_parser_exit
);
176 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
177 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
178 MODULE_LICENSE("GPL");