| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright © 2007 Eugene Konev <ejka@openwrt.org> | 
|  | 3 | * | 
|  | 4 | * This program is free software; you can redistribute it and/or modify | 
|  | 5 | * it under the terms of the GNU General Public License as published by | 
|  | 6 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 7 | * (at your option) any later version. | 
|  | 8 | * | 
|  | 9 | * This program is distributed in the hope that it will be useful, | 
|  | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 12 | * GNU General Public License for more details. | 
|  | 13 | * | 
|  | 14 | * You should have received a copy of the GNU General Public License | 
|  | 15 | * along with this program; if not, write to the Free Software | 
|  | 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
|  | 17 | * | 
|  | 18 | * TI AR7 flash partition table. | 
|  | 19 | * Based on ar7 map by Felix Fietkau <nbd@openwrt.org> | 
|  | 20 | * | 
|  | 21 | */ | 
|  | 22 |  | 
|  | 23 | #include <linux/kernel.h> | 
|  | 24 | #include <linux/slab.h> | 
|  | 25 |  | 
|  | 26 | #include <linux/mtd/mtd.h> | 
|  | 27 | #include <linux/mtd/partitions.h> | 
|  | 28 | #include <linux/bootmem.h> | 
|  | 29 | #include <linux/magic.h> | 
| Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 30 | #include <linux/module.h> | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 31 |  | 
|  | 32 | #define AR7_PARTS	4 | 
|  | 33 | #define ROOT_OFFSET	0xe0000 | 
|  | 34 |  | 
|  | 35 | #define LOADER_MAGIC1	le32_to_cpu(0xfeedfa42) | 
|  | 36 | #define LOADER_MAGIC2	le32_to_cpu(0xfeed1281) | 
|  | 37 |  | 
| David Woodhouse | 986ee01 | 2008-04-23 09:39:49 +0100 | [diff] [blame] | 38 | #ifndef SQUASHFS_MAGIC | 
|  | 39 | #define SQUASHFS_MAGIC	0x73717368 | 
|  | 40 | #endif | 
|  | 41 |  | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 42 | struct ar7_bin_rec { | 
|  | 43 | unsigned int checksum; | 
|  | 44 | unsigned int length; | 
|  | 45 | unsigned int address; | 
|  | 46 | }; | 
|  | 47 |  | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 48 | static int create_mtd_partitions(struct mtd_info *master, | 
|  | 49 | struct mtd_partition **pparts, | 
| Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 50 | struct mtd_part_parser_data *data) | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 51 | { | 
|  | 52 | struct ar7_bin_rec header; | 
| David Woodhouse | 986ee01 | 2008-04-23 09:39:49 +0100 | [diff] [blame] | 53 | unsigned int offset; | 
|  | 54 | size_t len; | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 55 | unsigned int pre_size = master->erasesize, post_size = 0; | 
|  | 56 | unsigned int root_offset = ROOT_OFFSET; | 
|  | 57 |  | 
|  | 58 | int retries = 10; | 
| Atsushi Nemoto | 17b536c | 2009-03-06 20:01:08 +0900 | [diff] [blame] | 59 | struct mtd_partition *ar7_parts; | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 60 |  | 
| Atsushi Nemoto | 17b536c | 2009-03-06 20:01:08 +0900 | [diff] [blame] | 61 | ar7_parts = kzalloc(sizeof(*ar7_parts) * AR7_PARTS, GFP_KERNEL); | 
|  | 62 | if (!ar7_parts) | 
|  | 63 | return -ENOMEM; | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 64 | ar7_parts[0].name = "loader"; | 
|  | 65 | ar7_parts[0].offset = 0; | 
|  | 66 | ar7_parts[0].size = master->erasesize; | 
|  | 67 | ar7_parts[0].mask_flags = MTD_WRITEABLE; | 
|  | 68 |  | 
|  | 69 | ar7_parts[1].name = "config"; | 
|  | 70 | ar7_parts[1].offset = 0; | 
|  | 71 | ar7_parts[1].size = master->erasesize; | 
|  | 72 | ar7_parts[1].mask_flags = 0; | 
|  | 73 |  | 
|  | 74 | do { /* Try 10 blocks starting from master->erasesize */ | 
|  | 75 | offset = pre_size; | 
| Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 76 | mtd_read(master, offset, sizeof(header), &len, | 
|  | 77 | (uint8_t *)&header); | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 78 | if (!strncmp((char *)&header, "TIENV0.8", 8)) | 
|  | 79 | ar7_parts[1].offset = pre_size; | 
|  | 80 | if (header.checksum == LOADER_MAGIC1) | 
|  | 81 | break; | 
|  | 82 | if (header.checksum == LOADER_MAGIC2) | 
|  | 83 | break; | 
|  | 84 | pre_size += master->erasesize; | 
|  | 85 | } while (retries--); | 
|  | 86 |  | 
|  | 87 | pre_size = offset; | 
|  | 88 |  | 
|  | 89 | if (!ar7_parts[1].offset) { | 
|  | 90 | ar7_parts[1].offset = master->size - master->erasesize; | 
|  | 91 | post_size = master->erasesize; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | switch (header.checksum) { | 
|  | 95 | case LOADER_MAGIC1: | 
|  | 96 | while (header.length) { | 
|  | 97 | offset += sizeof(header) + header.length; | 
| Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 98 | mtd_read(master, offset, sizeof(header), &len, | 
|  | 99 | (uint8_t *)&header); | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 100 | } | 
|  | 101 | root_offset = offset + sizeof(header) + 4; | 
|  | 102 | break; | 
|  | 103 | case LOADER_MAGIC2: | 
|  | 104 | while (header.length) { | 
|  | 105 | offset += sizeof(header) + header.length; | 
| Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 106 | mtd_read(master, offset, sizeof(header), &len, | 
|  | 107 | (uint8_t *)&header); | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 108 | } | 
|  | 109 | root_offset = offset + sizeof(header) + 4 + 0xff; | 
| David Woodhouse | 986ee01 | 2008-04-23 09:39:49 +0100 | [diff] [blame] | 110 | root_offset &= ~(uint32_t)0xff; | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 111 | break; | 
|  | 112 | default: | 
|  | 113 | printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum); | 
|  | 114 | break; | 
|  | 115 | } | 
|  | 116 |  | 
| Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 117 | mtd_read(master, root_offset, sizeof(header), &len, (u8 *)&header); | 
| Matteo Croce | f079788 | 2008-03-12 02:25:06 +0100 | [diff] [blame] | 118 | if (header.checksum != SQUASHFS_MAGIC) { | 
|  | 119 | root_offset += master->erasesize - 1; | 
|  | 120 | root_offset &= ~(master->erasesize - 1); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | ar7_parts[2].name = "linux"; | 
|  | 124 | ar7_parts[2].offset = pre_size; | 
|  | 125 | ar7_parts[2].size = master->size - pre_size - post_size; | 
|  | 126 | ar7_parts[2].mask_flags = 0; | 
|  | 127 |  | 
|  | 128 | ar7_parts[3].name = "rootfs"; | 
|  | 129 | ar7_parts[3].offset = root_offset; | 
|  | 130 | ar7_parts[3].size = master->size - root_offset - post_size; | 
|  | 131 | ar7_parts[3].mask_flags = 0; | 
|  | 132 |  | 
|  | 133 | *pparts = ar7_parts; | 
|  | 134 | return AR7_PARTS; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | static struct mtd_part_parser ar7_parser = { | 
|  | 138 | .owner = THIS_MODULE, | 
|  | 139 | .parse_fn = create_mtd_partitions, | 
|  | 140 | .name = "ar7part", | 
|  | 141 | }; | 
|  | 142 |  | 
|  | 143 | static int __init ar7_parser_init(void) | 
|  | 144 | { | 
|  | 145 | return register_mtd_parser(&ar7_parser); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | module_init(ar7_parser_init); | 
|  | 149 |  | 
|  | 150 | MODULE_LICENSE("GPL"); | 
|  | 151 | MODULE_AUTHOR(	"Felix Fietkau <nbd@openwrt.org>, " | 
|  | 152 | "Eugene Konev <ejka@openwrt.org>"); | 
|  | 153 | MODULE_DESCRIPTION("MTD partitioning for TI AR7"); |