fix a socket leak in udhcpc (patch by Eric L. Chen)
[openwrt/svn-archive/archive.git] / target / linux / adm5120-2.6 / files / arch / mips / adm5120 / trxsplit.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) Gabor Juhos <juhosg@freemail.hu>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
28 #include <linux/kmod.h>
29 #include <linux/root_dev.h>
30
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33
34 #include <linux/byteorder/generic.h>
35
36 #define PFX "trxsplit: "
37
38 #define TRX_MAGIC 0x30524448 /* "HDR0" */
39 #define TRX_VERSION 1
40 #define TRX_MAX_LEN 0x3A0000
41 #define TRX_NO_HEADER 1 /* Do not write TRX header */
42 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
43 #define TRX_MAX_OFFSET 3
44 #define TRX_MIN_KERNEL_SIZE 256*1024
45
46 struct trx_header {
47 u32 magic; /* "HDR0" */
48 u32 len; /* Length of file including header */
49 u32 crc32; /* 32-bit CRC from flag_version to end of file */
50 u32 flag_version; /* 0:15 flags, 16:31 version */
51 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
52 };
53
54 #define BLOCK_LEN_MIN 0x10000
55
56 static struct mtd_info *trx_mtd_master = NULL;
57 static struct mtd_info *trx_mtds[TRX_MAX_OFFSET];
58 static struct mtd_partition trx_parts[TRX_MAX_OFFSET];
59 static struct trx_header trx_hdr;
60 static int trx_nr_parts = 0;
61 static int trx_rootfs_part = -1;
62
63 static int __init trxsplit_checktrx(struct mtd_info *mtd, unsigned long offset)
64 {
65 size_t retlen;
66 int err;
67
68 err = mtd->read(mtd, offset, sizeof(trx_hdr), &retlen, (void *)&trx_hdr);
69 if (err)
70 goto err_out;
71
72 if (retlen != sizeof(trx_hdr))
73 goto err_out;
74
75 trx_hdr.magic = le32_to_cpu(trx_hdr.magic);
76 trx_hdr.len = le32_to_cpu(trx_hdr.len);
77 trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32);
78 trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version);
79 trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]);
80 trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]);
81 trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]);
82
83 /* sanity checks */
84 if (trx_hdr.magic != TRX_MAGIC)
85 goto err_out;
86
87 if (trx_hdr.len > mtd->size - offset)
88 goto err_out;
89
90 /* TODO: add crc32 checking too? */
91
92 return 1;
93
94 err_out:
95 return 0;
96 }
97
98 static void __init trxsplit_create_partitions(struct mtd_info *mtd,
99 unsigned long offset)
100 {
101 struct mtd_partition *part = trx_parts;
102 int i;
103
104 for (i=0;i<TRX_MAX_OFFSET;i++) {
105 part = &trx_parts[i];
106 if (trx_hdr.offsets[i] == 0)
107 continue;
108 part->offset = offset + trx_hdr.offsets[i];
109 part->mtdp = &trx_mtds[trx_nr_parts];
110 trx_nr_parts++;
111 }
112
113 for (i=0; i<trx_nr_parts-1; i++) {
114 trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset;
115 }
116 trx_parts[i].size = mtd->size - trx_parts[i].offset;
117
118 i=0;
119 part = &trx_parts[i];
120 if (part->size < TRX_MIN_KERNEL_SIZE) {
121 part->name = "trx_loader";
122 i++;
123 }
124
125 part = &trx_parts[i];
126 part->name = "trx_kernel";
127 i++;
128
129 part = &trx_parts[i];
130 part->name = "trx_rootfs";
131 trx_rootfs_part = i;
132 }
133
134 static void __init trxsplit_add_mtd(struct mtd_info *mtd)
135 {
136 unsigned long offset;
137 unsigned long blocklen;
138 int found;
139
140 if (trx_mtd_master)
141 return;
142
143 blocklen = mtd->erasesize;
144 if (blocklen < BLOCK_LEN_MIN)
145 blocklen = BLOCK_LEN_MIN;
146
147 printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name);
148
149 found = 0;
150 for (offset=0; offset < mtd->size; offset+=blocklen) {
151 found = trxsplit_checktrx(mtd, offset);
152 if (found)
153 break;
154 }
155
156 if (found == 0) {
157 printk(KERN_ALERT PFX "no TRX header found\n");
158 return;
159 }
160
161 printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset);
162
163 trxsplit_create_partitions(mtd, offset);
164
165 trx_mtd_master = mtd;
166 }
167
168 static void trxsplit_remove_mtd(struct mtd_info *mtd)
169 {
170 }
171
172 static struct mtd_notifier trxsplit_notifier __initdata = {
173 .add = trxsplit_add_mtd,
174 .remove = trxsplit_remove_mtd,
175 };
176
177 static void __init trxsplit_find_trx(void)
178 {
179 register_mtd_user(&trxsplit_notifier);
180 unregister_mtd_user(&trxsplit_notifier);
181 }
182
183 static int __init trxsplit_init(void)
184 {
185 int err;
186 int i;
187
188 trxsplit_find_trx();
189
190 if (trx_mtd_master == NULL)
191 goto err;
192
193 printk(KERN_INFO PFX "creating TRX partitions in '%s'\n",
194 trx_mtd_master->name);
195
196 err = add_mtd_partitions(trx_mtd_master, trx_parts, trx_nr_parts);
197 if (err) {
198 printk(KERN_ALERT PFX "creating TRX partitions failed\n");
199 goto err;
200 }
201
202 for (i=0; i<trx_nr_parts; i++) {
203 /* TODO: add error checking */
204 add_mtd_device(trx_mtds[i]);
205 }
206
207 if (ROOT_DEV == 0 && trx_rootfs_part >= 0) {
208 printk(KERN_INFO PFX "set '%s' to be root filesystem\n",
209 trx_mtds[trx_rootfs_part]->name);
210 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, trx_mtds[trx_rootfs_part]->index);
211 }
212
213 return 0;
214
215 err:
216 return -1;
217 }
218
219 late_initcall(trxsplit_init);