Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Parser for TRX format partitions
0004  *
0005  * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/mtd/mtd.h>
0011 #include <linux/mtd/partitions.h>
0012 
0013 #define TRX_PARSER_MAX_PARTS        4
0014 
0015 /* Magics */
0016 #define TRX_MAGIC           0x30524448
0017 #define UBI_EC_MAGIC            0x23494255  /* UBI# */
0018 
0019 struct trx_header {
0020     uint32_t magic;
0021     uint32_t length;
0022     uint32_t crc32;
0023     uint16_t flags;
0024     uint16_t version;
0025     uint32_t offset[3];
0026 } __packed;
0027 
0028 static const char *parser_trx_data_part_name(struct mtd_info *master,
0029                          size_t offset)
0030 {
0031     uint32_t buf;
0032     size_t bytes_read;
0033     int err;
0034 
0035     err  = mtd_read(master, offset, sizeof(buf), &bytes_read,
0036             (uint8_t *)&buf);
0037     if (err && !mtd_is_bitflip(err)) {
0038         pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
0039             offset, err);
0040         goto out_default;
0041     }
0042 
0043     if (buf == UBI_EC_MAGIC)
0044         return "ubi";
0045 
0046 out_default:
0047     return "rootfs";
0048 }
0049 
0050 static int parser_trx_parse(struct mtd_info *mtd,
0051                 const struct mtd_partition **pparts,
0052                 struct mtd_part_parser_data *data)
0053 {
0054     struct device_node *np = mtd_get_of_node(mtd);
0055     struct mtd_partition *parts;
0056     struct mtd_partition *part;
0057     struct trx_header trx;
0058     size_t bytes_read;
0059     uint8_t curr_part = 0, i = 0;
0060     uint32_t trx_magic = TRX_MAGIC;
0061     int err;
0062 
0063     /* Get different magic from device tree if specified */
0064     err = of_property_read_u32(np, "brcm,trx-magic", &trx_magic);
0065     if (err != 0 && err != -EINVAL)
0066         pr_err("failed to parse \"brcm,trx-magic\" DT attribute, using default: %d\n", err);
0067 
0068     parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
0069             GFP_KERNEL);
0070     if (!parts)
0071         return -ENOMEM;
0072 
0073     err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
0074     if (err) {
0075         pr_err("MTD reading error: %d\n", err);
0076         kfree(parts);
0077         return err;
0078     }
0079 
0080     if (trx.magic != trx_magic) {
0081         kfree(parts);
0082         return -ENOENT;
0083     }
0084 
0085     /* We have LZMA loader if there is address in offset[2] */
0086     if (trx.offset[2]) {
0087         part = &parts[curr_part++];
0088         part->name = "loader";
0089         part->offset = trx.offset[i];
0090         i++;
0091     }
0092 
0093     if (trx.offset[i]) {
0094         part = &parts[curr_part++];
0095         part->name = "linux";
0096         part->offset = trx.offset[i];
0097         i++;
0098     }
0099 
0100     if (trx.offset[i]) {
0101         part = &parts[curr_part++];
0102         part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
0103         part->offset = trx.offset[i];
0104         i++;
0105     }
0106 
0107     /*
0108      * Assume that every partition ends at the beginning of the one it is
0109      * followed by.
0110      */
0111     for (i = 0; i < curr_part; i++) {
0112         u64 next_part_offset = (i < curr_part - 1) ?
0113                        parts[i + 1].offset : mtd->size;
0114 
0115         parts[i].size = next_part_offset - parts[i].offset;
0116     }
0117 
0118     *pparts = parts;
0119     return i;
0120 };
0121 
0122 static const struct of_device_id mtd_parser_trx_of_match_table[] = {
0123     { .compatible = "brcm,trx" },
0124     {},
0125 };
0126 MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
0127 
0128 static struct mtd_part_parser mtd_parser_trx = {
0129     .parse_fn = parser_trx_parse,
0130     .name = "trx",
0131     .of_match_table = mtd_parser_trx_of_match_table,
0132 };
0133 module_mtd_part_parser(mtd_parser_trx);
0134 
0135 MODULE_LICENSE("GPL v2");
0136 MODULE_DESCRIPTION("Parser for TRX format partitions");