1 // SPDX-License-Identifier: GPL-2.0-only
3 * Parser for TRX format partitions
5 * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/partitions.h>
13 #define TRX_PARSER_MAX_PARTS 4
16 #define TRX_MAGIC 0x30524448
17 #define UBI_EC_MAGIC 0x23494255 /* UBI# */
28 static const char *parser_trx_data_part_name(struct mtd_info *master,
35 err = mtd_read(master, offset, sizeof(buf), &bytes_read,
37 if (err && !mtd_is_bitflip(err)) {
38 pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
43 if (buf == UBI_EC_MAGIC)
50 static int parser_trx_parse(struct mtd_info *mtd,
51 const struct mtd_partition **pparts,
52 struct mtd_part_parser_data *data)
54 struct mtd_partition *parts;
55 struct mtd_partition *part;
56 struct trx_header trx;
58 uint8_t curr_part = 0, i = 0;
61 parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
66 err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
68 pr_err("MTD reading error: %d\n", err);
73 if (trx.magic != TRX_MAGIC) {
78 /* We have LZMA loader if there is address in offset[2] */
80 part = &parts[curr_part++];
81 part->name = "loader";
82 part->offset = trx.offset[i];
87 part = &parts[curr_part++];
89 part->offset = trx.offset[i];
94 part = &parts[curr_part++];
95 part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
96 part->offset = trx.offset[i];
101 * Assume that every partition ends at the beginning of the one it is
104 for (i = 0; i < curr_part; i++) {
105 u64 next_part_offset = (i < curr_part - 1) ?
106 parts[i + 1].offset : mtd->size;
108 parts[i].size = next_part_offset - parts[i].offset;
115 static const struct of_device_id mtd_parser_trx_of_match_table[] = {
116 { .compatible = "brcm,trx" },
119 MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
121 static struct mtd_part_parser mtd_parser_trx = {
122 .parse_fn = parser_trx_parse,
124 .of_match_table = mtd_parser_trx_of_match_table,
126 module_mtd_part_parser(mtd_parser_trx);
128 MODULE_LICENSE("GPL v2");
129 MODULE_DESCRIPTION("Parser for TRX format partitions");