2 * Infineon/ADMTek ADM8668 (WildPass) partition parser support
4 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
5 * Copyright (C) 2012 Florian Fainelli <florian@openwrt.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.
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43 #include <linux/wait.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/partitions.h>
46 #include <linux/vmalloc.h>
47 #include <linux/slab.h>
48 #include <linux/crc32.h>
49 #include <linux/magic.h>
51 #define PFX "adm8668-part: "
53 /* first a little bit about the headers i need.. */
55 /* just interested in part of the full struct */
56 struct squashfs_super_block {
58 __le32 pad0[9]; /* it's not really padding */
62 #define IH_MAGIC 0x56190527 /* Image Magic Number */
64 uint32_t ih_magic; /* Image Header Magic Number */
65 uint32_t ih_hcrc; /* Image Header CRC Checksum */
66 uint32_t ih_time; /* Image Creation Timestamp */
67 uint32_t ih_size; /* Image Data Size */
68 uint32_t ih_load; /* Data Load Address */
69 uint32_t ih_ep; /* Entry Point Address */
70 uint32_t ih_dcrc; /* Image Data CRC Checksum */
71 uint8_t ih_os; /* Operating System */
72 uint8_t ih_arch; /* CPU architecture */
73 uint8_t ih_type; /* Image Type */
74 uint8_t ih_comp; /* Compression Type */
75 char ih_name[32]; /* image name */
78 /* in case i wanna change stuff later, and to clarify the math section... */
81 #define PART_UBOOT_ENV 2
84 static int adm8668_parse_partitions(struct mtd_info *master,
85 struct mtd_partition **pparts,
86 struct mtd_part_parser_data *data)
89 struct uboot_header uhdr;
91 size_t len, linux_len;
92 struct squashfs_super_block shdr;
93 struct erase_info erase_info;
94 struct mtd_partition *adm8668_parts;
96 memset(&erase_info, 0, sizeof(erase_info));
98 blocksize = master->erasesize;
100 if (blocksize < 0x10000)
103 adm8668_parts = kzalloc(sizeof(*adm8668_parts) * NR_PARTS, GFP_KERNEL);
107 adm8668_parts[PART_LINUX].name = kstrdup("linux", GFP_KERNEL);
108 adm8668_parts[PART_LINUX].offset = 0x40000;
109 adm8668_parts[PART_LINUX].size = master->size - 0x40000;
110 adm8668_parts[PART_ROOTFS].name = kstrdup("rootfs", GFP_KERNEL);
111 adm8668_parts[PART_ROOTFS].offset = 0xe0000;
112 adm8668_parts[PART_ROOTFS].size = 0x140000;
113 adm8668_parts[PART_UBOOT_ENV].name = kstrdup("uboot_env", GFP_KERNEL);
114 adm8668_parts[PART_UBOOT_ENV].offset = 0x20000;
115 adm8668_parts[PART_UBOOT_ENV].size = 0x20000;
117 /* now find squashfs */
118 memset(&shdr, 0xe5, sizeof(shdr));
120 for (off = 0x40000; off < master->size; off += blocksize) {
124 if (mtd_read(master, off, sizeof(shdr), &len, (char *)&shdr) ||
128 if (shdr.s_magic == SQUASHFS_MAGIC) {
129 uint32_t fs_size = (uint32_t)shdr.bytes_used;
131 pr_info(PFX "filesystem type: squashfs, size=%dkB\n",
135 * Update rootfs based on the superblock info, and
136 * stretch to end of MTD. rootfs_split will split it
138 adm8668_parts[PART_ROOTFS].offset = off;
139 adm8668_parts[PART_ROOTFS].size = master->size -
140 adm8668_parts[PART_ROOTFS].offset;
143 * kernel ends where rootfs starts
144 * but we'll keep it full-length for upgrades
146 linux_len = adm8668_parts[PART_LINUX + 1].offset -
147 adm8668_parts[PART_LINUX].offset;
149 adm8668_parts[PART_LINUX].size = master->size -
150 adm8668_parts[PART_LINUX].offset;
155 pr_err(PFX "could't find root filesystem\n");
159 if (mtd_read(master, adm8668_parts[PART_LINUX].offset, sizeof(uhdr), &len, (char *)&uhdr) ||
160 len != sizeof(uhdr)) {
161 pr_err(PFX "failed to read u-boot header\n");
165 if (uhdr.ih_magic != IH_MAGIC) {
166 pr_info(PFX "invalid u-boot magic detected?!?!\n");
170 if (be32_to_cpu(uhdr.ih_size) != (linux_len - sizeof(uhdr))) {
173 unsigned char *block;
176 offset = adm8668_parts[PART_LINUX].offset +
177 sizeof(struct uboot_header);
179 pr_info(PFX "Updating U-boot image:\n");
180 pr_info(PFX " old: [size: %8d crc32: 0x%08x]\n",
181 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
183 if (mtd_read(master, offset, sizeof(data), &data_len, (char *)&data)) {
184 pr_err(PFX "failed to read data\n");
188 /* Update the data length & crc32 */
189 uhdr.ih_size = cpu_to_be32(linux_len - sizeof(uhdr));
190 uhdr.ih_dcrc = crc32_le(~0, (char *)&data, linux_len - sizeof(uhdr)) ^ (~0);
191 uhdr.ih_dcrc = cpu_to_be32(uhdr.ih_dcrc);
193 pr_info(PFX " new: [size: %8d crc32: 0x%08x]\n",
194 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
196 /* update header's crc... */
198 uhdr.ih_hcrc = crc32_le(~0, (unsigned char *)&uhdr,
199 sizeof(uhdr)) ^ (~0);
200 uhdr.ih_hcrc = cpu_to_be32(uhdr.ih_hcrc);
202 /* read first eraseblock from the image */
203 block = vmalloc(master->erasesize);
207 if (mtd_read(master, adm8668_parts[PART_LINUX].offset, master->erasesize, &len, block)
208 || len != master->erasesize) {
209 pr_err(PFX "error copying first eraseblock\n");
213 /* Write updated header to the flash */
214 memcpy(block, &uhdr, sizeof(uhdr));
216 master->unlock(master, off, master->erasesize);
218 erase_info.mtd = master;
219 erase_info.addr = (uint64_t)adm8668_parts[PART_LINUX].offset;
220 erase_info.len = master->erasesize;
221 ret = mtd_erase(master, &erase_info);
223 if (mtd_write(master, adm8668_parts[PART_LINUX].offset, master->erasesize,
225 pr_err(PFX "write failed");
227 pr_err(PFX "erase failed");
233 pr_info(PFX "done\n");
236 *pparts = adm8668_parts;
241 static struct mtd_part_parser adm8668_parser = {
242 .owner = THIS_MODULE,
243 .parse_fn = adm8668_parse_partitions,
244 .name = "adm8668part",
247 static int __init adm8668_parser_init(void)
249 register_mtd_parser(&adm8668_parser);
254 static void __exit adm8668_parser_exit(void)
256 deregister_mtd_parser(&adm8668_parser);
259 module_init(adm8668_parser_init);
260 module_exit(adm8668_parser_exit);
262 MODULE_LICENSE("GPL");
263 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
264 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
265 MODULE_DESCRIPTION("MTD partition parser for ADM8668");