OpenWrt – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Copyright (C) Gabor Juhos <juhosg@openwrt.org> |
||
3 | * |
||
4 | * This program is free software; you can redistribute it and/or modify it |
||
5 | * under the terms of the GNU General Public License version 2 as published |
||
6 | * by the Free Software Foundation. |
||
7 | * |
||
8 | */ |
||
9 | |||
10 | #include <linux/module.h> |
||
11 | #include <linux/types.h> |
||
12 | #include <linux/kernel.h> |
||
13 | #include <linux/slab.h> |
||
14 | #include <linux/list.h> |
||
15 | #include <linux/kmod.h> |
||
16 | #include <linux/root_dev.h> |
||
17 | |||
18 | #include <linux/mtd/mtd.h> |
||
19 | #include <linux/mtd/partitions.h> |
||
20 | |||
21 | #include <linux/byteorder/generic.h> |
||
22 | |||
23 | #define PFX "trxsplit: " |
||
24 | |||
25 | #define TRX_MAGIC 0x30524448 /* "HDR0" */ |
||
26 | #define TRX_VERSION 1 |
||
27 | #define TRX_MAX_LEN 0x3A0000 |
||
28 | #define TRX_NO_HEADER 0x1 /* do not write TRX header */ |
||
29 | #define TRX_GZ_FILES 0x2 /* contains individual gzip files */ |
||
30 | #define TRX_MAX_OFFSET 3 |
||
31 | #define TRX_MIN_KERNEL_SIZE (256 * 1024) |
||
32 | |||
33 | struct trx_header { |
||
34 | u32 magic; /* "HDR0" */ |
||
35 | u32 len; /* Length of file including header */ |
||
36 | u32 crc32; /* 32-bit CRC from flag_version to end of file */ |
||
37 | u32 flag_version; /* 0:15 flags, 16:31 version */ |
||
38 | u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions */ |
||
39 | }; |
||
40 | |||
41 | #define TRX_ALIGN 0x1000 |
||
42 | |||
43 | static int trx_nr_parts; |
||
44 | static unsigned long trx_offset; |
||
45 | static struct mtd_info *trx_mtd; |
||
46 | static struct mtd_partition trx_parts[TRX_MAX_OFFSET]; |
||
47 | static struct trx_header trx_hdr; |
||
48 | |||
49 | static int trxsplit_refresh_partitions(struct mtd_info *mtd); |
||
50 | |||
51 | static int trxsplit_checktrx(struct mtd_info *mtd, unsigned long offset) |
||
52 | { |
||
53 | size_t retlen; |
||
54 | int err; |
||
55 | |||
56 | err = mtd_read(mtd, offset, sizeof(trx_hdr), &retlen, (void *)&trx_hdr); |
||
57 | if (err) { |
||
58 | printk(KERN_ALERT PFX "unable to read from '%s'\n", mtd->name); |
||
59 | goto err_out; |
||
60 | } |
||
61 | |||
62 | if (retlen != sizeof(trx_hdr)) { |
||
63 | printk(KERN_ALERT PFX "reading failed on '%s'\n", mtd->name); |
||
64 | goto err_out; |
||
65 | } |
||
66 | |||
67 | trx_hdr.magic = le32_to_cpu(trx_hdr.magic); |
||
68 | trx_hdr.len = le32_to_cpu(trx_hdr.len); |
||
69 | trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32); |
||
70 | trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version); |
||
71 | trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]); |
||
72 | trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]); |
||
73 | trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]); |
||
74 | |||
75 | /* sanity checks */ |
||
76 | if (trx_hdr.magic != TRX_MAGIC) |
||
77 | goto err_out; |
||
78 | |||
79 | if (trx_hdr.len > mtd->size - offset) |
||
80 | goto err_out; |
||
81 | |||
82 | /* TODO: add crc32 checking too? */ |
||
83 | |||
84 | return 0; |
||
85 | |||
86 | err_out: |
||
87 | return -1; |
||
88 | } |
||
89 | |||
90 | static void trxsplit_findtrx(struct mtd_info *mtd) |
||
91 | { |
||
92 | unsigned long offset; |
||
93 | int err; |
||
94 | |||
95 | printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name); |
||
96 | |||
97 | err = 0; |
||
98 | for (offset = 0; offset < mtd->size; offset += TRX_ALIGN) { |
||
99 | err = trxsplit_checktrx(mtd, offset); |
||
100 | if (err == 0) |
||
101 | break; |
||
102 | } |
||
103 | |||
104 | if (err) |
||
105 | return; |
||
106 | |||
107 | printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset); |
||
108 | |||
109 | trx_mtd = mtd; |
||
110 | trx_offset = offset; |
||
111 | } |
||
112 | |||
113 | static void trxsplit_create_partitions(struct mtd_info *mtd) |
||
114 | { |
||
115 | struct mtd_partition *part = trx_parts; |
||
116 | int err; |
||
117 | int i; |
||
118 | |||
119 | for (i = 0; i < TRX_MAX_OFFSET; i++) { |
||
120 | part = &trx_parts[i]; |
||
121 | if (trx_hdr.offsets[i] == 0) |
||
122 | continue; |
||
123 | part->offset = trx_offset + trx_hdr.offsets[i]; |
||
124 | trx_nr_parts++; |
||
125 | } |
||
126 | |||
127 | for (i = 0; i < trx_nr_parts-1; i++) |
||
128 | trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset; |
||
129 | |||
130 | trx_parts[i].size = mtd->size - trx_parts[i].offset; |
||
131 | |||
132 | i = 0; |
||
133 | part = &trx_parts[i]; |
||
134 | if (part->size < TRX_MIN_KERNEL_SIZE) { |
||
135 | part->name = "loader"; |
||
136 | i++; |
||
137 | } |
||
138 | |||
139 | part = &trx_parts[i]; |
||
140 | part->name = "kernel"; |
||
141 | i++; |
||
142 | |||
143 | part = &trx_parts[i]; |
||
144 | part->name = "rootfs"; |
||
145 | |||
146 | err = mtd_device_register(mtd, trx_parts, trx_nr_parts); |
||
147 | if (err) { |
||
148 | printk(KERN_ALERT PFX "adding TRX partitions failed\n"); |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | mtd->refresh_device = trxsplit_refresh_partitions; |
||
153 | } |
||
154 | |||
155 | static int trxsplit_refresh_partitions(struct mtd_info *mtd) |
||
156 | { |
||
157 | printk(KERN_INFO PFX "refreshing TRX partitions in '%s' (%d,%d)\n", |
||
158 | mtd->name, MTD_BLOCK_MAJOR, mtd->index); |
||
159 | |||
160 | /* remove old partitions */ |
||
161 | mtd_device_unregister(mtd); |
||
162 | |||
163 | trxsplit_findtrx(mtd); |
||
164 | if (!trx_mtd) |
||
165 | goto err; |
||
166 | |||
167 | trxsplit_create_partitions(trx_mtd); |
||
168 | return 1; |
||
169 | |||
170 | err: |
||
171 | return 0; |
||
172 | } |
||
173 | |||
174 | static void __init trxsplit_add_mtd(struct mtd_info *mtd) |
||
175 | { |
||
176 | if (mtd->type != MTD_NORFLASH) { |
||
177 | printk(KERN_INFO PFX "'%s' is not a NOR flash, skipped\n", |
||
178 | mtd->name); |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | if (!trx_mtd) |
||
183 | trxsplit_findtrx(mtd); |
||
184 | } |
||
185 | |||
186 | static void __init trxsplit_remove_mtd(struct mtd_info *mtd) |
||
187 | { |
||
188 | /* nothing to do */ |
||
189 | } |
||
190 | |||
191 | static struct mtd_notifier trxsplit_notifier __initdata = { |
||
192 | .add = trxsplit_add_mtd, |
||
193 | .remove = trxsplit_remove_mtd, |
||
194 | }; |
||
195 | |||
196 | static void __init trxsplit_scan(void) |
||
197 | { |
||
198 | register_mtd_user(&trxsplit_notifier); |
||
199 | unregister_mtd_user(&trxsplit_notifier); |
||
200 | } |
||
201 | |||
202 | static int __init trxsplit_init(void) |
||
203 | { |
||
204 | trxsplit_scan(); |
||
205 | |||
206 | if (trx_mtd) { |
||
207 | printk(KERN_INFO PFX "creating TRX partitions in '%s' " |
||
208 | "(%d,%d)\n", trx_mtd->name, MTD_BLOCK_MAJOR, |
||
209 | trx_mtd->index); |
||
210 | trxsplit_create_partitions(trx_mtd); |
||
211 | } |
||
212 | |||
213 | return 0; |
||
214 | } |
||
215 | |||
216 | late_initcall(trxsplit_init); |