2 * Parse MyLoader-style flash partition tables and produce a Linux partition
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
7 * This file was based on drivers/mtd/redboot.c
8 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/vmalloc.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
24 #include <linux/byteorder/generic.h>
26 #include <prom/myloader.h>
28 #define NAME_LEN_MAX 20
29 #define NAME_MYLOADER "MyLoader"
30 #define NAME_PARTITION_TABLE "Partition Table"
31 #define BLOCK_LEN_MIN 0x10000
33 int parse_myloader_partitions(struct mtd_info
*master
,
34 struct mtd_partition
**pparts
,
37 struct mylo_partition_table
*tab
;
38 struct mylo_partition
*part
;
39 struct mtd_partition
*mtd_parts
;
40 struct mtd_partition
*mtd_part
;
46 unsigned long blocklen
;
48 tab
= vmalloc(sizeof(*tab
));
54 blocklen
= master
->erasesize
;
55 if (blocklen
< BLOCK_LEN_MIN
)
56 blocklen
= BLOCK_LEN_MIN
;
58 /* Partition Table is always located on the second erase block */
60 printk(KERN_NOTICE
"%s: searching for MyLoader partition table at "
61 "offset 0x%lx\n", master
->name
, offset
);
63 ret
= master
->read(master
, offset
, sizeof(*tab
), &retlen
, (void *)tab
);
67 if (retlen
!= sizeof(*tab
)) {
72 /* Check for Partition Table magic number */
73 if (tab
->magic
!= le32_to_cpu(MYLO_MAGIC_PARTITIONS
)) {
74 printk(KERN_NOTICE
"%s: no MyLoader partition table found\n",
80 /* The MyLoader and the Partition Table is always present */
83 /* Detect number of used partitions */
84 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
85 part
= &tab
->partitions
[i
];
87 if (le16_to_cpu(part
->type
) == PARTITION_TYPE_FREE
)
93 mtd_parts
= kzalloc((num_parts
* sizeof(*mtd_part
) +
94 num_parts
* NAME_LEN_MAX
), GFP_KERNEL
);
101 mtd_part
= mtd_parts
;
102 names
= (char *)&mtd_parts
[num_parts
];
104 strncpy(names
, NAME_MYLOADER
, NAME_LEN_MAX
-1);
105 mtd_part
->name
= names
;
106 mtd_part
->offset
= 0;
107 mtd_part
->size
= blocklen
;
108 mtd_part
->mask_flags
= MTD_WRITEABLE
;
110 names
+= NAME_LEN_MAX
;
112 strncpy(names
, NAME_PARTITION_TABLE
, NAME_LEN_MAX
-1);
113 mtd_part
->name
= names
;
114 mtd_part
->offset
= blocklen
;
115 mtd_part
->size
= blocklen
;
116 mtd_part
->mask_flags
= MTD_WRITEABLE
;
118 names
+= NAME_LEN_MAX
;
120 for (i
= 0; i
< MYLO_MAX_PARTITIONS
; i
++) {
121 part
= &tab
->partitions
[i
];
123 if (le16_to_cpu(part
->type
) == PARTITION_TYPE_FREE
)
126 sprintf(names
, "partition%d", i
);
127 mtd_part
->offset
= le32_to_cpu(part
->addr
);
128 mtd_part
->size
= le32_to_cpu(part
->size
);
129 mtd_part
->name
= names
;
131 names
+= NAME_LEN_MAX
;
143 static struct mtd_part_parser mylo_mtd_parser
= {
144 .owner
= THIS_MODULE
,
145 .parse_fn
= parse_myloader_partitions
,
146 .name
= NAME_MYLOADER
,
149 static int __init
mylo_mtd_parser_init(void)
151 return register_mtd_parser(&mylo_mtd_parser
);
154 static void __exit
mylo_mtd_parser_exit(void)
156 deregister_mtd_parser(&mylo_mtd_parser
);
159 module_init(mylo_mtd_parser_init
);
160 module_exit(mylo_mtd_parser_exit
);
162 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
163 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
164 MODULE_LICENSE("GPL v2");