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