Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mtd: bcm47xxpart: move TRX parsing code to separated function

This change simplifies main parsing loop logic a bit. In future it may
be useful for moving TRX support to separated module / parser (if we
implement support for them at some point).
Finally parsing TRX at the end puts us in a better position as we have
better flash layout knowledge. It may be useful e.g. if it appears there
is more than 1 TRX partition.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: Marek Vasut <marek.vasut@gmail.com>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>

authored by

Rafał Miłecki and committed by
Brian Norris
b522d7b0 ccc38234

+74 -47
+74 -47
drivers/mtd/bcm47xxpart.c
··· 83 83 return "rootfs"; 84 84 } 85 85 86 + static int bcm47xxpart_parse_trx(struct mtd_info *master, 87 + struct mtd_partition *trx, 88 + struct mtd_partition *parts, 89 + size_t parts_len) 90 + { 91 + struct trx_header header; 92 + size_t bytes_read; 93 + int curr_part = 0; 94 + int i, err; 95 + 96 + if (parts_len < 3) { 97 + pr_warn("No enough space to add TRX partitions!\n"); 98 + return -ENOMEM; 99 + } 100 + 101 + err = mtd_read(master, trx->offset, sizeof(header), &bytes_read, 102 + (uint8_t *)&header); 103 + if (err && !mtd_is_bitflip(err)) { 104 + pr_err("mtd_read error while reading TRX header: %d\n", err); 105 + return err; 106 + } 107 + 108 + i = 0; 109 + 110 + /* We have LZMA loader if offset[2] points to sth */ 111 + if (header.offset[2]) { 112 + bcm47xxpart_add_part(&parts[curr_part++], "loader", 113 + trx->offset + header.offset[i], 0); 114 + i++; 115 + } 116 + 117 + if (header.offset[i]) { 118 + bcm47xxpart_add_part(&parts[curr_part++], "linux", 119 + trx->offset + header.offset[i], 0); 120 + i++; 121 + } 122 + 123 + if (header.offset[i]) { 124 + size_t offset = trx->offset + header.offset[i]; 125 + const char *name = bcm47xxpart_trx_data_part_name(master, 126 + offset); 127 + 128 + bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0); 129 + i++; 130 + } 131 + 132 + /* 133 + * Assume that every partition ends at the beginning of the one it is 134 + * followed by. 135 + */ 136 + for (i = 0; i < curr_part; i++) { 137 + u64 next_part_offset = (i < curr_part - 1) ? 138 + parts[i + 1].offset : 139 + trx->offset + trx->size; 140 + 141 + parts[i].size = next_part_offset - parts[i].offset; 142 + } 143 + 144 + return curr_part; 145 + } 146 + 86 147 static int bcm47xxpart_parse(struct mtd_info *master, 87 148 const struct mtd_partition **pparts, 88 149 struct mtd_part_parser_data *data) ··· 154 93 size_t bytes_read; 155 94 uint32_t offset; 156 95 uint32_t blocksize = master->erasesize; 157 - struct trx_header *trx; 158 96 int trx_part = -1; 159 - int last_trx_part = -1; 160 97 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, }; 161 98 int err; 162 99 ··· 241 182 242 183 /* TRX */ 243 184 if (buf[0x000 / 4] == TRX_MAGIC) { 244 - if (BCM47XXPART_MAX_PARTS - curr_part < 4) { 245 - pr_warn("Not enough partitions left to register trx, scanning stopped!\n"); 246 - break; 247 - } 248 - 249 - trx = (struct trx_header *)buf; 185 + struct trx_header *trx; 250 186 251 187 trx_part = curr_part; 252 188 bcm47xxpart_add_part(&parts[curr_part++], "firmware", 253 189 offset, 0); 254 190 255 - i = 0; 256 - /* We have LZMA loader if offset[2] points to sth */ 257 - if (trx->offset[2]) { 258 - bcm47xxpart_add_part(&parts[curr_part++], 259 - "loader", 260 - offset + trx->offset[i], 261 - 0); 262 - i++; 263 - } 264 - 265 - if (trx->offset[i]) { 266 - bcm47xxpart_add_part(&parts[curr_part++], 267 - "linux", 268 - offset + trx->offset[i], 269 - 0); 270 - i++; 271 - } 272 - 273 - /* 274 - * Pure rootfs size is known and can be calculated as: 275 - * trx->length - trx->offset[i]. We don't fill it as 276 - * we want to have jffs2 (overlay) in the same mtd. 277 - */ 278 - if (trx->offset[i]) { 279 - const char *name; 280 - 281 - name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]); 282 - bcm47xxpart_add_part(&parts[curr_part++], 283 - name, 284 - offset + trx->offset[i], 285 - 0); 286 - i++; 287 - } 288 - 289 - last_trx_part = curr_part - 1; 290 - 291 191 /* Jump to the end of TRX */ 192 + trx = (struct trx_header *)buf; 292 193 offset = roundup(offset + trx->length, blocksize); 293 194 /* Next loop iteration will increase the offset */ 294 195 offset -= blocksize; ··· 326 307 parts[i + 1].offset : master->size; 327 308 328 309 parts[i].size = next_part_offset - parts[i].offset; 329 - if (i == last_trx_part && trx_part >= 0) 330 - parts[trx_part].size = next_part_offset - 331 - parts[trx_part].offset; 310 + } 311 + 312 + /* If there was TRX parse it now */ 313 + if (trx_part >= 0) { 314 + int num_parts; 315 + 316 + num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part], 317 + parts + curr_part, 318 + BCM47XXPART_MAX_PARTS - curr_part); 319 + if (num_parts > 0) 320 + curr_part += num_parts; 332 321 } 333 322 334 323 *pparts = parts;