2 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
3 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
5 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
7 * original functions for finding root filesystem from Mike Baker
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
20 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 * Copyright 2004, Broadcom Corporation
31 * All Rights Reserved.
33 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
34 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
35 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
36 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
38 * Flash mapping for adm8668 boards
42 #include <linux/module.h>
43 #include <linux/types.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/wait.h>
47 #include <linux/mtd/mtd.h>
48 #include <linux/mtd/map.h>
49 #include <linux/slab.h>
50 #ifdef CONFIG_MTD_PARTITIONS
51 #include <linux/mtd/partitions.h>
53 #include <linux/crc32.h>
54 #include <linux/magic.h>
57 #define WINDOW_ADDR 0x10000000
58 #define WINDOW_SIZE 0x800000
61 /* first a little bit about the headers i need.. */
63 /* just interested in part of the full struct */
64 struct squashfs_super_block {
66 __le32 pad0[9]; /* it's not really padding */
70 #define IH_MAGIC 0x56190527 /* Image Magic Number */
72 uint32_t ih_magic; /* Image Header Magic Number */
73 uint32_t ih_hcrc; /* Image Header CRC Checksum */
74 uint32_t ih_time; /* Image Creation Timestamp */
75 uint32_t ih_size; /* Image Data Size */
76 uint32_t ih_load; /* Data Load Address */
77 uint32_t ih_ep; /* Entry Point Address */
78 uint32_t ih_dcrc; /* Image Data CRC Checksum */
79 uint8_t ih_os; /* Operating System */
80 uint8_t ih_arch; /* CPU architecture */
81 uint8_t ih_type; /* Image Type */
82 uint8_t ih_comp; /* Compression Type */
83 char ih_name[32]; /* image name */
86 /************************************************/
88 static struct mtd_info *adm8668_mtd;
90 struct map_info adm8668_map = {
97 #ifdef CONFIG_MTD_PARTITIONS
100 * Copied from mtdblock.c
104 * Since typical flash erasable sectors are much larger than what Linux's
105 * buffer cache can handle, we must implement read-modify-write on flash
106 * sectors for each block write requests. To avoid over-erasing flash sectors
107 * and to speed things up, we locally cache a whole flash sector while it is
108 * being written to until a different sector is required.
111 static void erase_callback(struct erase_info *done)
113 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
117 static int erase_write (struct mtd_info *mtd, unsigned long pos,
118 int len, const char *buf)
120 struct erase_info erase;
121 DECLARE_WAITQUEUE(wait, current);
122 wait_queue_head_t wait_q;
127 * First, let's erase the flash block.
130 init_waitqueue_head(&wait_q);
132 erase.callback = erase_callback;
135 erase.priv = (u_long)&wait_q;
137 set_current_state(TASK_INTERRUPTIBLE);
138 add_wait_queue(&wait_q, &wait);
140 ret = mtd->erase(mtd, &erase);
142 set_current_state(TASK_RUNNING);
143 remove_wait_queue(&wait_q, &wait);
144 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
145 "on \"%s\" failed\n",
146 pos, len, mtd->name);
150 schedule(); /* Wait for erase to finish. */
151 remove_wait_queue(&wait_q, &wait);
154 * Next, write data to flash.
157 ret = mtd->write (mtd, pos, len, &retlen, buf);
165 /* decent defaults in case... shrug */
166 static struct mtd_partition adm8668_parts[] = {
167 { name: "linux", offset: 0x40000, size: WINDOW_SIZE-0x40000, },
168 { name: "rootfs", offset: 0xe0000, size: 0x140000, },
169 { name: "uboot_env", offset: 0x20000, size: 0x20000, },
173 /* in case i wanna change stuff later, and to clarify the math section... */
175 #define PART_ROOTFS 1
179 init_mtd_partitions(struct mtd_info *mtd, size_t size)
181 struct uboot_header uhdr;
183 size_t len, linux_len;
184 struct squashfs_super_block shdr;
186 blocksize = mtd->erasesize;
187 if (blocksize < 0x10000)
190 /* now find squashfs */
191 memset(&shdr, 0xe5, sizeof(shdr));
192 for (off = adm8668_parts[PART_LINUX].offset; off < size; off += blocksize) {
196 if (mtd->read(mtd, off, sizeof(shdr), &len, (char *)&shdr) ||
200 if (shdr.s_magic == SQUASHFS_MAGIC) {
201 uint32_t fs_size = (uint32_t)shdr.bytes_used;
203 printk(KERN_INFO "%s: Filesystem type: squashfs, size=%dkB\n",
204 mtd->name, fs_size>>10);
206 /* Update rootfs based on the superblock info, and
207 * stretch to end of MTD. rootfs_split will split it */
208 adm8668_parts[PART_ROOTFS].offset = off;
209 adm8668_parts[PART_ROOTFS].size = mtd->size -
210 adm8668_parts[PART_ROOTFS].offset;
212 /* kernel ends where rootfs starts
213 * but we'll keep it full-length for upgrades */
214 linux_len = adm8668_parts[PART_LINUX+1].offset -
215 adm8668_parts[PART_LINUX].offset;
217 adm8668_parts[PART_LINUX].size = mtd->size -
218 adm8668_parts[PART_LINUX].offset;
220 adm8668_parts[PART_LINUX].size = linux_len;
227 "%s: Couldn't find root filesystem\n",
232 if (mtd->read(mtd, adm8668_parts[PART_LINUX].offset, sizeof(uhdr), &len, (char *)&uhdr) ||
236 /* that's odd. how'd ya boot it then */
237 if (uhdr.ih_magic != IH_MAGIC)
240 if (be32_to_cpu(uhdr.ih_size) != (linux_len - sizeof(uhdr))) {
241 unsigned char *block, *data = (unsigned char *)(WINDOW_ADDR | (adm8668_parts[PART_LINUX].offset + sizeof(struct uboot_header)) | 0xA0000000);
243 printk(KERN_NOTICE "Updating U-boot image:\n");
244 printk(KERN_NOTICE " old: [size: %8d crc32: 0x%08x]\n",
245 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
247 /* Update the data length & crc32 */
248 uhdr.ih_size = cpu_to_be32(linux_len - sizeof(uhdr));
249 uhdr.ih_dcrc = crc32_le(~0, data, linux_len - sizeof(uhdr)) ^ (~0);
250 uhdr.ih_dcrc = cpu_to_be32(uhdr.ih_dcrc);
252 printk(KERN_NOTICE " new: [size: %8d crc32: 0x%08x]\n",
253 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
255 /* update header's crc... */
257 uhdr.ih_hcrc = crc32_le(~0, (unsigned char *)&uhdr,
258 sizeof(uhdr)) ^ (~0);
259 uhdr.ih_hcrc = cpu_to_be32(uhdr.ih_hcrc);
261 /* read first eraseblock from the image */
262 block = kmalloc(mtd->erasesize, GFP_KERNEL);
263 if (mtd->read(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, &len, block) || len != mtd->erasesize) {
264 printk("Error copying first eraseblock\n");
268 /* Write updated header to the flash */
269 memcpy(block, &uhdr, sizeof(uhdr));
271 mtd->unlock(mtd, off, mtd->erasesize);
272 erase_write(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, block);
276 printk(KERN_NOTICE "Done\n");
285 int __init init_adm8668_map(void)
287 #ifdef CONFIG_MTD_PARTITIONS
291 adm8668_map.virt = (unsigned long)ioremap(WINDOW_ADDR, WINDOW_SIZE);
293 if (!adm8668_map.virt) {
294 printk(KERN_ERR "Failed to ioremap\n");
298 simple_map_init(&adm8668_map);
299 if (!(adm8668_mtd = do_map_probe("cfi_probe", &adm8668_map))) {
300 printk(KERN_ERR "cfi_probe failed\n");
301 iounmap((void *)adm8668_map.virt);
305 adm8668_mtd->owner = THIS_MODULE;
307 #ifdef CONFIG_MTD_PARTITIONS
308 nr_parts = init_mtd_partitions(adm8668_mtd, adm8668_mtd->size);
309 ret = add_mtd_partitions(adm8668_mtd, adm8668_parts, nr_parts);
311 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
320 map_destroy(adm8668_mtd);
321 if (adm8668_map.virt)
322 iounmap((void *) adm8668_map.virt);
323 adm8668_map.virt = 0;
327 void __exit cleanup_adm8668_map(void)
329 #ifdef CONFIG_MTD_PARTITIONS
330 del_mtd_partitions(adm8668_mtd);
332 map_destroy(adm8668_mtd);
333 iounmap((void *) adm8668_map.virt);
334 adm8668_map.virt = 0;
337 module_init(init_adm8668_map);
338 module_exit(cleanup_adm8668_map);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
342 MODULE_DESCRIPTION("MTD map driver for ADM8668 NOR Flash");