^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Parser for TRX format partitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define TRX_PARSER_MAX_PARTS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Magics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define TRX_MAGIC 0x30524448
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define UBI_EC_MAGIC 0x23494255 /* UBI# */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct trx_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) uint32_t magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) uint32_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) uint32_t crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) uint16_t flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) uint16_t version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) uint32_t offset[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static const char *parser_trx_data_part_name(struct mtd_info *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) uint32_t buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) size_t bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) err = mtd_read(master, offset, sizeof(buf), &bytes_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (uint8_t *)&buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (err && !mtd_is_bitflip(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) offset, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) goto out_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (buf == UBI_EC_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return "ubi";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) out_default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return "rootfs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int parser_trx_parse(struct mtd_info *mtd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const struct mtd_partition **pparts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct mtd_part_parser_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct mtd_partition *parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct mtd_partition *part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct trx_header trx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) size_t bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) uint8_t curr_part = 0, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!parts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_err("MTD reading error: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kfree(parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (trx.magic != TRX_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) kfree(parts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* We have LZMA loader if there is address in offset[2] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (trx.offset[2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) part = &parts[curr_part++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) part->name = "loader";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) part->offset = trx.offset[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (trx.offset[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) part = &parts[curr_part++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) part->name = "linux";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) part->offset = trx.offset[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (trx.offset[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) part = &parts[curr_part++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) part->offset = trx.offset[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Assume that every partition ends at the beginning of the one it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * followed by.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) for (i = 0; i < curr_part; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u64 next_part_offset = (i < curr_part - 1) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) parts[i + 1].offset : mtd->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) parts[i].size = next_part_offset - parts[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *pparts = parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static const struct of_device_id mtd_parser_trx_of_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) { .compatible = "brcm,trx" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct mtd_part_parser mtd_parser_trx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .parse_fn = parser_trx_parse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .name = "trx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .of_match_table = mtd_parser_trx_of_match_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) module_mtd_part_parser(mtd_parser_trx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MODULE_DESCRIPTION("Parser for TRX format partitions");