Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * BCM47XX MTD partitioning |
| 3 | * |
| 4 | * Copyright © 2012 Rafał Miłecki <zajec5@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <linux/mtd/partitions.h> |
Hauke Mehrtens | 111bd98 | 2012-12-26 19:51:14 +0000 | [diff] [blame] | 17 | #include <bcm47xx_nvram.h> |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 18 | |
| 19 | /* 10 parts were found on sflash on Netgear WNDR4500 */ |
| 20 | #define BCM47XXPART_MAX_PARTS 12 |
| 21 | |
Rafał Miłecki | 5ca1088 | 2013-03-07 09:02:38 +0100 | [diff] [blame] | 22 | /* |
| 23 | * Amount of bytes we read when analyzing each block of flash memory. |
| 24 | * Set it big enough to allow detecting partition and reading important data. |
| 25 | */ |
| 26 | #define BCM47XXPART_BYTES_TO_READ 0x404 |
| 27 | |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 28 | /* Magics */ |
| 29 | #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */ |
| 30 | #define POT_MAGIC1 0x54544f50 /* POTT */ |
| 31 | #define POT_MAGIC2 0x504f /* OP */ |
| 32 | #define ML_MAGIC1 0x39685a42 |
| 33 | #define ML_MAGIC2 0x26594131 |
| 34 | #define TRX_MAGIC 0x30524448 |
Rafał Miłecki | 020c6bc | 2013-10-21 22:34:37 +0200 | [diff] [blame^] | 35 | #define SQSH_MAGIC 0x71736873 /* shsq */ |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 36 | |
| 37 | struct trx_header { |
| 38 | uint32_t magic; |
| 39 | uint32_t length; |
| 40 | uint32_t crc32; |
| 41 | uint16_t flags; |
| 42 | uint16_t version; |
| 43 | uint32_t offset[3]; |
| 44 | } __packed; |
| 45 | |
| 46 | static void bcm47xxpart_add_part(struct mtd_partition *part, char *name, |
| 47 | u64 offset, uint32_t mask_flags) |
| 48 | { |
| 49 | part->name = name; |
| 50 | part->offset = offset; |
| 51 | part->mask_flags = mask_flags; |
| 52 | } |
| 53 | |
| 54 | static int bcm47xxpart_parse(struct mtd_info *master, |
| 55 | struct mtd_partition **pparts, |
| 56 | struct mtd_part_parser_data *data) |
| 57 | { |
| 58 | struct mtd_partition *parts; |
| 59 | uint8_t i, curr_part = 0; |
| 60 | uint32_t *buf; |
| 61 | size_t bytes_read; |
| 62 | uint32_t offset; |
Hauke Mehrtens | 25bad1d | 2013-01-24 17:39:58 +0100 | [diff] [blame] | 63 | uint32_t blocksize = master->erasesize; |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 64 | struct trx_header *trx; |
Rafał Miłecki | 396afe5 | 2013-01-06 16:08:36 +0100 | [diff] [blame] | 65 | int trx_part = -1; |
| 66 | int last_trx_part = -1; |
Rafał Miłecki | 91d542f | 2013-03-07 09:02:39 +0100 | [diff] [blame] | 67 | int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, }; |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 68 | |
Hauke Mehrtens | 25bad1d | 2013-01-24 17:39:58 +0100 | [diff] [blame] | 69 | if (blocksize <= 0x10000) |
| 70 | blocksize = 0x10000; |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 71 | |
| 72 | /* Alloc */ |
| 73 | parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS, |
| 74 | GFP_KERNEL); |
Hauke Mehrtens | 99b1d18 | 2013-10-13 22:53:49 +0200 | [diff] [blame] | 75 | if (!parts) |
| 76 | return -ENOMEM; |
| 77 | |
Rafał Miłecki | 5ca1088 | 2013-03-07 09:02:38 +0100 | [diff] [blame] | 78 | buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL); |
Hauke Mehrtens | 99b1d18 | 2013-10-13 22:53:49 +0200 | [diff] [blame] | 79 | if (!buf) { |
| 80 | kfree(parts); |
| 81 | return -ENOMEM; |
| 82 | } |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 83 | |
| 84 | /* Parse block by block looking for magics */ |
| 85 | for (offset = 0; offset <= master->size - blocksize; |
| 86 | offset += blocksize) { |
| 87 | /* Nothing more in higher memory */ |
| 88 | if (offset >= 0x2000000) |
| 89 | break; |
| 90 | |
| 91 | if (curr_part > BCM47XXPART_MAX_PARTS) { |
| 92 | pr_warn("Reached maximum number of partitions, scanning stopped!\n"); |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Read beginning of the block */ |
Rafał Miłecki | 5ca1088 | 2013-03-07 09:02:38 +0100 | [diff] [blame] | 97 | if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ, |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 98 | &bytes_read, (uint8_t *)buf) < 0) { |
| 99 | pr_err("mtd_read error while parsing (offset: 0x%X)!\n", |
| 100 | offset); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | /* CFE has small NVRAM at 0x400 */ |
| 105 | if (buf[0x400 / 4] == NVRAM_HEADER) { |
| 106 | bcm47xxpart_add_part(&parts[curr_part++], "boot", |
| 107 | offset, MTD_WRITEABLE); |
| 108 | continue; |
| 109 | } |
| 110 | |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 111 | /* |
| 112 | * board_data starts with board_id which differs across boards, |
| 113 | * but we can use 'MPFR' (hopefully) magic at 0x100 |
| 114 | */ |
| 115 | if (buf[0x100 / 4] == BOARD_DATA_MAGIC) { |
| 116 | bcm47xxpart_add_part(&parts[curr_part++], "board_data", |
| 117 | offset, MTD_WRITEABLE); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | /* POT(TOP) */ |
| 122 | if (buf[0x000 / 4] == POT_MAGIC1 && |
| 123 | (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) { |
| 124 | bcm47xxpart_add_part(&parts[curr_part++], "POT", offset, |
| 125 | MTD_WRITEABLE); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | /* ML */ |
| 130 | if (buf[0x010 / 4] == ML_MAGIC1 && |
| 131 | buf[0x014 / 4] == ML_MAGIC2) { |
| 132 | bcm47xxpart_add_part(&parts[curr_part++], "ML", offset, |
| 133 | MTD_WRITEABLE); |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | /* TRX */ |
| 138 | if (buf[0x000 / 4] == TRX_MAGIC) { |
| 139 | trx = (struct trx_header *)buf; |
| 140 | |
Rafał Miłecki | 396afe5 | 2013-01-06 16:08:36 +0100 | [diff] [blame] | 141 | trx_part = curr_part; |
| 142 | bcm47xxpart_add_part(&parts[curr_part++], "firmware", |
| 143 | offset, 0); |
| 144 | |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 145 | i = 0; |
| 146 | /* We have LZMA loader if offset[2] points to sth */ |
| 147 | if (trx->offset[2]) { |
| 148 | bcm47xxpart_add_part(&parts[curr_part++], |
| 149 | "loader", |
| 150 | offset + trx->offset[i], |
| 151 | 0); |
| 152 | i++; |
| 153 | } |
| 154 | |
| 155 | bcm47xxpart_add_part(&parts[curr_part++], "linux", |
| 156 | offset + trx->offset[i], 0); |
| 157 | i++; |
| 158 | |
| 159 | /* |
| 160 | * Pure rootfs size is known and can be calculated as: |
| 161 | * trx->length - trx->offset[i]. We don't fill it as |
| 162 | * we want to have jffs2 (overlay) in the same mtd. |
| 163 | */ |
| 164 | bcm47xxpart_add_part(&parts[curr_part++], "rootfs", |
| 165 | offset + trx->offset[i], 0); |
| 166 | i++; |
| 167 | |
Rafał Miłecki | 396afe5 | 2013-01-06 16:08:36 +0100 | [diff] [blame] | 168 | last_trx_part = curr_part - 1; |
| 169 | |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 170 | /* |
| 171 | * We have whole TRX scanned, skip to the next part. Use |
| 172 | * roundown (not roundup), as the loop will increase |
| 173 | * offset in next step. |
| 174 | */ |
| 175 | offset = rounddown(offset + trx->length, blocksize); |
| 176 | continue; |
| 177 | } |
Rafał Miłecki | 020c6bc | 2013-10-21 22:34:37 +0200 | [diff] [blame^] | 178 | |
| 179 | /* Squashfs on devices not using TRX */ |
| 180 | if (buf[0x000 / 4] == SQSH_MAGIC) { |
| 181 | bcm47xxpart_add_part(&parts[curr_part++], "rootfs", |
| 182 | offset, 0); |
| 183 | continue; |
| 184 | } |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 185 | } |
Rafał Miłecki | 91d542f | 2013-03-07 09:02:39 +0100 | [diff] [blame] | 186 | |
| 187 | /* Look for NVRAM at the end of the last block. */ |
| 188 | for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) { |
| 189 | if (curr_part > BCM47XXPART_MAX_PARTS) { |
| 190 | pr_warn("Reached maximum number of partitions, scanning stopped!\n"); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | offset = master->size - possible_nvram_sizes[i]; |
| 195 | if (mtd_read(master, offset, 0x4, &bytes_read, |
| 196 | (uint8_t *)buf) < 0) { |
| 197 | pr_err("mtd_read error while reading at offset 0x%X!\n", |
| 198 | offset); |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | /* Standard NVRAM */ |
| 203 | if (buf[0] == NVRAM_HEADER) { |
| 204 | bcm47xxpart_add_part(&parts[curr_part++], "nvram", |
| 205 | master->size - blocksize, 0); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 210 | kfree(buf); |
| 211 | |
| 212 | /* |
| 213 | * Assume that partitions end at the beginning of the one they are |
| 214 | * followed by. |
| 215 | */ |
Rafał Miłecki | 648bdbe | 2013-01-06 16:08:35 +0100 | [diff] [blame] | 216 | for (i = 0; i < curr_part; i++) { |
| 217 | u64 next_part_offset = (i < curr_part - 1) ? |
| 218 | parts[i + 1].offset : master->size; |
| 219 | |
| 220 | parts[i].size = next_part_offset - parts[i].offset; |
Rafał Miłecki | 396afe5 | 2013-01-06 16:08:36 +0100 | [diff] [blame] | 221 | if (i == last_trx_part && trx_part >= 0) |
| 222 | parts[trx_part].size = next_part_offset - |
| 223 | parts[trx_part].offset; |
Rafał Miłecki | 648bdbe | 2013-01-06 16:08:35 +0100 | [diff] [blame] | 224 | } |
Rafał Miłecki | 3cf7f13 | 2012-08-30 07:41:16 +0200 | [diff] [blame] | 225 | |
| 226 | *pparts = parts; |
| 227 | return curr_part; |
| 228 | }; |
| 229 | |
| 230 | static struct mtd_part_parser bcm47xxpart_mtd_parser = { |
| 231 | .owner = THIS_MODULE, |
| 232 | .parse_fn = bcm47xxpart_parse, |
| 233 | .name = "bcm47xxpart", |
| 234 | }; |
| 235 | |
| 236 | static int __init bcm47xxpart_init(void) |
| 237 | { |
| 238 | return register_mtd_parser(&bcm47xxpart_mtd_parser); |
| 239 | } |
| 240 | |
| 241 | static void __exit bcm47xxpart_exit(void) |
| 242 | { |
| 243 | deregister_mtd_parser(&bcm47xxpart_mtd_parser); |
| 244 | } |
| 245 | |
| 246 | module_init(bcm47xxpart_init); |
| 247 | module_exit(bcm47xxpart_exit); |
| 248 | |
| 249 | MODULE_LICENSE("GPL"); |
| 250 | MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories"); |