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