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 device_node *np = mtd_get_of_node(mtd);
55 struct mtd_partition *parts;
56 struct mtd_partition *part;
57 struct trx_header trx;
59 uint8_t curr_part = 0, i = 0;
60 uint32_t trx_magic = TRX_MAGIC;
63 /* Get different magic from device tree if specified */
64 err = of_property_read_u32(np, "brcm,trx-magic", &trx_magic);
65 if (err != 0 && err != -EINVAL)
66 pr_err("failed to parse \"brcm,trx-magic\" DT attribute, using default: %d\n", err);
68 parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
73 err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
75 pr_err("MTD reading error: %d\n", err);
80 if (trx.magic != trx_magic) {
85 /* We have LZMA loader if there is address in offset[2] */
87 part = &parts[curr_part++];
88 part->name = "loader";
89 part->offset = trx.offset[i];
94 part = &parts[curr_part++];
96 part->offset = trx.offset[i];
101 part = &parts[curr_part++];
102 part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
103 part->offset = trx.offset[i];
108 * Assume that every partition ends at the beginning of the one it is
111 for (i = 0; i < curr_part; i++) {
112 u64 next_part_offset = (i < curr_part - 1) ?
113 parts[i + 1].offset : mtd->size;
115 parts[i].size = next_part_offset - parts[i].offset;
122 static const struct of_device_id mtd_parser_trx_of_match_table[] = {
123 { .compatible = "brcm,trx" },
126 MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
128 static struct mtd_part_parser mtd_parser_trx = {
129 .parse_fn = parser_trx_parse,
131 .of_match_table = mtd_parser_trx_of_match_table,
133 module_mtd_part_parser(mtd_parser_trx);
135 MODULE_LICENSE("GPL v2");
136 MODULE_DESCRIPTION("Parser for TRX format partitions");