2 * Parser for TRX format partitions
4 * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
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.
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/partitions.h>
17 #define TRX_PARSER_MAX_PARTS 4
20 #define TRX_MAGIC 0x30524448
21 #define UBI_EC_MAGIC 0x23494255 /* UBI# */
32 static const char *parser_trx_data_part_name(struct mtd_info
*master
,
39 err
= mtd_read(master
, offset
, sizeof(buf
), &bytes_read
,
41 if (err
&& !mtd_is_bitflip(err
)) {
42 pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
47 if (buf
== UBI_EC_MAGIC
)
54 static int parser_trx_parse(struct mtd_info
*mtd
,
55 const struct mtd_partition
**pparts
,
56 struct mtd_part_parser_data
*data
)
58 struct mtd_partition
*parts
;
59 struct mtd_partition
*part
;
60 struct trx_header trx
;
62 uint8_t curr_part
= 0, i
= 0;
65 parts
= kcalloc(TRX_PARSER_MAX_PARTS
, sizeof(struct mtd_partition
),
70 err
= mtd_read(mtd
, 0, sizeof(trx
), &bytes_read
, (uint8_t *)&trx
);
72 pr_err("MTD reading error: %d\n", err
);
77 if (trx
.magic
!= TRX_MAGIC
) {
82 /* We have LZMA loader if there is address in offset[2] */
84 part
= &parts
[curr_part
++];
85 part
->name
= "loader";
86 part
->offset
= trx
.offset
[i
];
91 part
= &parts
[curr_part
++];
93 part
->offset
= trx
.offset
[i
];
98 part
= &parts
[curr_part
++];
99 part
->name
= parser_trx_data_part_name(mtd
, trx
.offset
[i
]);
100 part
->offset
= trx
.offset
[i
];
105 * Assume that every partition ends at the beginning of the one it is
108 for (i
= 0; i
< curr_part
; i
++) {
109 u64 next_part_offset
= (i
< curr_part
- 1) ?
110 parts
[i
+ 1].offset
: mtd
->size
;
112 parts
[i
].size
= next_part_offset
- parts
[i
].offset
;
119 static const struct of_device_id mtd_parser_trx_of_match_table
[] = {
120 { .compatible
= "brcm,trx" },
123 MODULE_DEVICE_TABLE(of
, mtd_parser_trx_of_match_table
);
125 static struct mtd_part_parser mtd_parser_trx
= {
126 .parse_fn
= parser_trx_parse
,
128 .of_match_table
= mtd_parser_trx_of_match_table
,
130 module_mtd_part_parser(mtd_parser_trx
);
132 MODULE_LICENSE("GPL v2");
133 MODULE_DESCRIPTION("Parser for TRX format partitions");