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 mtd_partition *parts; 55 struct mtd_partition *part; 56 struct trx_header trx; 57 size_t bytes_read; 58 uint8_t curr_part = 0, i = 0; 59 int err; 60 61 parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition), 62 GFP_KERNEL); 63 if (!parts) 64 return -ENOMEM; 65 66 err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx); 67 if (err) { 68 pr_err("MTD reading error: %d\n", err); 69 kfree(parts); 70 return err; 71 } 72 73 if (trx.magic != TRX_MAGIC) { 74 kfree(parts); 75 return -ENOENT; 76 } 77 78 /* We have LZMA loader if there is address in offset[2] */ 79 if (trx.offset[2]) { 80 part = &parts[curr_part++]; 81 part->name = "loader"; 82 part->offset = trx.offset[i]; 83 i++; 84 } 85 86 if (trx.offset[i]) { 87 part = &parts[curr_part++]; 88 part->name = "linux"; 89 part->offset = trx.offset[i]; 90 i++; 91 } 92 93 if (trx.offset[i]) { 94 part = &parts[curr_part++]; 95 part->name = parser_trx_data_part_name(mtd, trx.offset[i]); 96 part->offset = trx.offset[i]; 97 i++; 98 } 99 100 /* 101 * Assume that every partition ends at the beginning of the one it is 102 * followed by. 103 */ 104 for (i = 0; i < curr_part; i++) { 105 u64 next_part_offset = (i < curr_part - 1) ? 106 parts[i + 1].offset : mtd->size; 107 108 parts[i].size = next_part_offset - parts[i].offset; 109 } 110 111 *pparts = parts; 112 return i; 113 }; 114 115 static const struct of_device_id mtd_parser_trx_of_match_table[] = { 116 { .compatible = "brcm,trx" }, 117 {}, 118 }; 119 MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table); 120 121 static struct mtd_part_parser mtd_parser_trx = { 122 .parse_fn = parser_trx_parse, 123 .name = "trx", 124 .of_match_table = mtd_parser_trx_of_match_table, 125 }; 126 module_mtd_part_parser(mtd_parser_trx); 127 128 MODULE_LICENSE("GPL v2"); 129 MODULE_DESCRIPTION("Parser for TRX format partitions"); 130