[adm5120] fix a switch driver bug
[openwrt.git] / target / linux / adm5120 / files / drivers / mtd / myloader.c
1 /*
2 * $Id$
3 *
4 * Parse MyLoader-style flash partition tables and produce a Linux partition
5 * array to match.
6 *
7 * Copyright (C) 2007 OpenWrt.org
8 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
9 *
10 * This file was based on drivers/mtd/redboot.c
11 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
12 *
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.
16 *
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/vmalloc.h>
23
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26
27 #include <linux/byteorder/generic.h>
28
29 #include <prom/myloader.h>
30
31 #define NAME_LEN_MAX 20
32 #define NAME_MYLOADER "MyLoader"
33 #define NAME_PARTITION_TABLE "Partition Table"
34 #define BLOCK_LEN_MIN 0x10000
35
36 int parse_myloader_partitions(struct mtd_info *master,
37 struct mtd_partition **pparts,
38 unsigned long origin)
39 {
40 struct mylo_partition_table *tab;
41 struct mylo_partition *part;
42 struct mtd_partition *mtd_parts;
43 struct mtd_partition *mtd_part;
44 int num_parts;
45 int ret, i;
46 size_t retlen;
47 char *names;
48 unsigned long offset;
49 unsigned long blocklen;
50
51 tab = vmalloc(sizeof(*tab));
52 if (!tab) {
53 return -ENOMEM;
54 goto out;
55 }
56
57 blocklen = master->erasesize;
58 if (blocklen < BLOCK_LEN_MIN)
59 blocklen = BLOCK_LEN_MIN;
60
61 /* Partition Table is always located on the second erase block */
62 offset = blocklen;
63 printk(KERN_NOTICE "%s: searching for MyLoader partition table at "
64 "offset 0x%lx\n", master->name, offset);
65
66 ret = master->read(master, offset, sizeof(*tab), &retlen, (void *)tab);
67 if (ret)
68 goto out;
69
70 if (retlen != sizeof(*tab)) {
71 ret = -EIO;
72 goto out_free_buf;
73 }
74
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",
78 master->name);
79 ret = 0;
80 goto out_free_buf;
81 }
82
83 /* The MyLoader and the Partition Table is always present */
84 num_parts = 2;
85
86 /* Detect number of used partitions */
87 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
88 part = &tab->partitions[i];
89
90 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
91 continue;
92
93 num_parts++;
94 }
95
96 mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
97 num_parts * NAME_LEN_MAX), GFP_KERNEL);
98
99 if (!mtd_parts) {
100 ret = -ENOMEM;
101 goto out_free_buf;
102 }
103
104 mtd_part = mtd_parts;
105 names = (char *)&mtd_parts[num_parts];
106
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;
112 mtd_part++;
113 names += NAME_LEN_MAX;
114
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;
120 mtd_part++;
121 names += NAME_LEN_MAX;
122
123 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
124 part = &tab->partitions[i];
125
126 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
127 continue;
128
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;
133 mtd_part++;
134 names += NAME_LEN_MAX;
135 }
136
137 *pparts = mtd_parts;
138 ret = num_parts;
139
140 out_free_buf:
141 vfree(tab);
142 out:
143 return ret;
144 }
145
146 static struct mtd_part_parser mylo_mtd_parser = {
147 .owner = THIS_MODULE,
148 .parse_fn = parse_myloader_partitions,
149 .name = NAME_MYLOADER,
150 };
151
152 static int __init mylo_mtd_parser_init(void)
153 {
154 return register_mtd_parser(&mylo_mtd_parser);
155 }
156
157 static void __exit mylo_mtd_parser_exit(void)
158 {
159 deregister_mtd_parser(&mylo_mtd_parser);
160 }
161
162 module_init(mylo_mtd_parser_init);
163 module_exit(mylo_mtd_parser_exit);
164
165 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
166 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
167 MODULE_LICENSE("GPL v2");
This page took 0.050948 seconds and 5 git commands to generate.