2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-06-21 14:26:47 +08:00
|
|
|
/*
|
|
|
|
* Parser for TRX format partitions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
|
|
|
|
|
|
|
#define TRX_PARSER_MAX_PARTS 4
|
|
|
|
|
|
|
|
/* Magics */
|
|
|
|
#define TRX_MAGIC 0x30524448
|
|
|
|
#define UBI_EC_MAGIC 0x23494255 /* UBI# */
|
|
|
|
|
|
|
|
struct trx_header {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t version;
|
|
|
|
uint32_t offset[3];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static const char *parser_trx_data_part_name(struct mtd_info *master,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
uint32_t buf;
|
|
|
|
size_t bytes_read;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mtd_read(master, offset, sizeof(buf), &bytes_read,
|
|
|
|
(uint8_t *)&buf);
|
|
|
|
if (err && !mtd_is_bitflip(err)) {
|
2017-06-23 13:07:14 +08:00
|
|
|
pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
|
2017-06-21 14:26:47 +08:00
|
|
|
offset, err);
|
|
|
|
goto out_default;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf == UBI_EC_MAGIC)
|
|
|
|
return "ubi";
|
|
|
|
|
|
|
|
out_default:
|
|
|
|
return "rootfs";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parser_trx_parse(struct mtd_info *mtd,
|
|
|
|
const struct mtd_partition **pparts,
|
|
|
|
struct mtd_part_parser_data *data)
|
|
|
|
{
|
|
|
|
struct mtd_partition *parts;
|
|
|
|
struct mtd_partition *part;
|
|
|
|
struct trx_header trx;
|
|
|
|
size_t bytes_read;
|
|
|
|
uint8_t curr_part = 0, i = 0;
|
|
|
|
int err;
|
|
|
|
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
|
2017-06-21 14:26:47 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!parts)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
|
|
|
|
if (err) {
|
|
|
|
pr_err("MTD reading error: %d\n", err);
|
|
|
|
kfree(parts);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trx.magic != TRX_MAGIC) {
|
|
|
|
kfree(parts);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have LZMA loader if there is address in offset[2] */
|
|
|
|
if (trx.offset[2]) {
|
|
|
|
part = &parts[curr_part++];
|
|
|
|
part->name = "loader";
|
|
|
|
part->offset = trx.offset[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trx.offset[i]) {
|
|
|
|
part = &parts[curr_part++];
|
|
|
|
part->name = "linux";
|
|
|
|
part->offset = trx.offset[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trx.offset[i]) {
|
|
|
|
part = &parts[curr_part++];
|
|
|
|
part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
|
|
|
|
part->offset = trx.offset[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume that every partition ends at the beginning of the one it is
|
|
|
|
* followed by.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < curr_part; i++) {
|
|
|
|
u64 next_part_offset = (i < curr_part - 1) ?
|
|
|
|
parts[i + 1].offset : mtd->size;
|
|
|
|
|
|
|
|
parts[i].size = next_part_offset - parts[i].offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pparts = parts;
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
2018-06-26 06:05:08 +08:00
|
|
|
static const struct of_device_id mtd_parser_trx_of_match_table[] = {
|
|
|
|
{ .compatible = "brcm,trx" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
|
|
|
|
|
2017-06-21 14:26:47 +08:00
|
|
|
static struct mtd_part_parser mtd_parser_trx = {
|
|
|
|
.parse_fn = parser_trx_parse,
|
|
|
|
.name = "trx",
|
2018-06-26 06:05:08 +08:00
|
|
|
.of_match_table = mtd_parser_trx_of_match_table,
|
2017-06-21 14:26:47 +08:00
|
|
|
};
|
|
|
|
module_mtd_part_parser(mtd_parser_trx);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_DESCRIPTION("Parser for TRX format partitions");
|