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 modify it
14 * under the terms of the GNU General Public License version 2 as published
15 * by the Free Software Foundation.
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/vmalloc.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
27 #include <linux/byteorder/generic.h>
29 #include <prom/myloader.h>
31 #define NAME_LEN_MAX 20
32 #define NAME_MYLOADER "MyLoader"
33 #define NAME_PARTITION_TABLE "Partition Table"
34 #define BLOCK_LEN_MIN 0x10000
36 int parse_myloader_partitions(struct mtd_info
*master
,
37 struct mtd_partition
**pparts
,
40 struct mylo_partition_table
*tab
;
41 struct mylo_partition
*part
;
42 struct mtd_partition
*mtd_parts
;
43 struct mtd_partition
*mtd_part
;
49 unsigned long blocklen
;
51 tab
= vmalloc(sizeof(*tab
));
57 blocklen
= master
->erasesize
;
58 if (blocklen
< BLOCK_LEN_MIN
)
59 blocklen
= BLOCK_LEN_MIN
;
61 /* Partition Table is always located on the second erase block */
63 printk(KERN_NOTICE
"%s: searching for MyLoader partition table at "
64 "offset 0x%lx\n", master
->name
, offset
);
66 ret
= master
->read(master
, offset
, sizeof(*tab
), &retlen
, (void *)tab
);
70 if (retlen
!= sizeof(*tab
)) {
75 /* Check for Partition Table magic number */
76 if (tab
->magic
!= le32_to_cpu(MYLO_MAGIC_PARTITIONS
)) {
77 printk(KERN_NOTICE
"%s: no MyLoader partition table found\n",
83 /* The MyLoader and the Partition Table is always present */
86 /* Detect number of used partitions */
87 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
88 part
= &tab
->partitions
[i
];
90 if (le16_to_cpu(part
->type
) == PARTITION_TYPE_FREE
)
96 mtd_parts
= kzalloc((num_parts
* sizeof(*mtd_part
) +
97 num_parts
* NAME_LEN_MAX
), GFP_KERNEL
);
104 mtd_part
= mtd_parts
;
105 names
= (char *)&mtd_parts
[num_parts
];
107 strncpy(names
, NAME_MYLOADER
, NAME_LEN_MAX
-1);
108 mtd_part
->name
= names
;
109 mtd_part
->offset
= 0;
110 mtd_part
->size
= blocklen
;
111 mtd_part
->mask_flags
= MTD_WRITEABLE
;
113 names
+= NAME_LEN_MAX
;
115 strncpy(names
, NAME_PARTITION_TABLE
, NAME_LEN_MAX
-1);
116 mtd_part
->name
= names
;
117 mtd_part
->offset
= blocklen
;
118 mtd_part
->size
= blocklen
;
119 mtd_part
->mask_flags
= MTD_WRITEABLE
;
121 names
+= NAME_LEN_MAX
;
123 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
124 part
= &tab
->partitions
[i
];
126 if (le16_to_cpu(part
->type
) == PARTITION_TYPE_FREE
)
129 sprintf(names
, "partition%d", i
);
130 mtd_part
->offset
= le32_to_cpu(part
->addr
);
131 mtd_part
->size
= le32_to_cpu(part
->size
);
132 mtd_part
->name
= names
;
134 names
+= NAME_LEN_MAX
;
146 static struct mtd_part_parser mylo_mtd_parser
= {
147 .owner
= THIS_MODULE
,
148 .parse_fn
= parse_myloader_partitions
,
149 .name
= NAME_MYLOADER
,
152 static int __init
mylo_mtd_parser_init(void)
154 return register_mtd_parser(&mylo_mtd_parser
);
157 static void __exit
mylo_mtd_parser_exit(void)
159 deregister_mtd_parser(&mylo_mtd_parser
);
162 module_init(mylo_mtd_parser_init
);
163 module_exit(mylo_mtd_parser_exit
);
165 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
166 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
167 MODULE_LICENSE("GPL v2");