1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Qualcomm SMEM NAND flash partition parser
4  *
5  * Copyright (C) 2020, Linaro Ltd.
6  */
7 
8 #include <linux/ctype.h>
9 #include <linux/module.h>
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/partitions.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/smem.h>
14 
15 #define SMEM_AARM_PARTITION_TABLE	9
16 #define SMEM_APPS			0
17 
18 #define SMEM_FLASH_PART_MAGIC1		0x55ee73aa
19 #define SMEM_FLASH_PART_MAGIC2		0xe35ebddb
20 #define SMEM_FLASH_PTABLE_V3		3
21 #define SMEM_FLASH_PTABLE_V4		4
22 #define SMEM_FLASH_PTABLE_MAX_PARTS_V3	16
23 #define SMEM_FLASH_PTABLE_MAX_PARTS_V4	48
24 #define SMEM_FLASH_PTABLE_HDR_LEN	(4 * sizeof(u32))
25 #define SMEM_FLASH_PTABLE_NAME_SIZE	16
26 
27 /**
28  * struct smem_flash_pentry - SMEM Flash partition entry
29  * @name: Name of the partition
30  * @offset: Offset in blocks
31  * @length: Length of the partition in blocks
32  * @attr: Flags for this partition
33  */
34 struct smem_flash_pentry {
35 	char name[SMEM_FLASH_PTABLE_NAME_SIZE];
36 	__le32 offset;
37 	__le32 length;
38 	u8 attr;
39 } __packed __aligned(4);
40 
41 /**
42  * struct smem_flash_ptable - SMEM Flash partition table
43  * @magic1: Partition table Magic 1
44  * @magic2: Partition table Magic 2
45  * @version: Partition table version
46  * @numparts: Number of partitions in this ptable
47  * @pentry: Flash partition entries belonging to this ptable
48  */
49 struct smem_flash_ptable {
50 	__le32 magic1;
51 	__le32 magic2;
52 	__le32 version;
53 	__le32 numparts;
54 	struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
55 } __packed __aligned(4);
56 
57 static int parse_qcomsmem_part(struct mtd_info *mtd,
58 			       const struct mtd_partition **pparts,
59 			       struct mtd_part_parser_data *data)
60 {
61 	struct smem_flash_pentry *pentry;
62 	struct smem_flash_ptable *ptable;
63 	size_t len = SMEM_FLASH_PTABLE_HDR_LEN;
64 	struct mtd_partition *parts;
65 	int ret, i, numparts;
66 	char *name, *c;
67 
68 	pr_debug("Parsing partition table info from SMEM\n");
69 	ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
70 	if (IS_ERR(ptable)) {
71 		pr_err("Error reading partition table header\n");
72 		return PTR_ERR(ptable);
73 	}
74 
75 	/* Verify ptable magic */
76 	if (le32_to_cpu(ptable->magic1) != SMEM_FLASH_PART_MAGIC1 ||
77 	    le32_to_cpu(ptable->magic2) != SMEM_FLASH_PART_MAGIC2) {
78 		pr_err("Partition table magic verification failed\n");
79 		return -EINVAL;
80 	}
81 
82 	/* Ensure that # of partitions is less than the max we have allocated */
83 	numparts = le32_to_cpu(ptable->numparts);
84 	if (numparts > SMEM_FLASH_PTABLE_MAX_PARTS_V4) {
85 		pr_err("Partition numbers exceed the max limit\n");
86 		return -EINVAL;
87 	}
88 
89 	/* Find out length of partition data based on table version */
90 	if (le32_to_cpu(ptable->version) <= SMEM_FLASH_PTABLE_V3) {
91 		len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V3 *
92 			sizeof(struct smem_flash_pentry);
93 	} else if (le32_to_cpu(ptable->version) == SMEM_FLASH_PTABLE_V4) {
94 		len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V4 *
95 			sizeof(struct smem_flash_pentry);
96 	} else {
97 		pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
98 		return -EINVAL;
99 	}
100 
101 	/*
102 	 * Now that the partition table header has been parsed, verified
103 	 * and the length of the partition table calculated, read the
104 	 * complete partition table
105 	 */
106 	ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
107 	if (IS_ERR_OR_NULL(ptable)) {
108 		pr_err("Error reading partition table\n");
109 		return PTR_ERR(ptable);
110 	}
111 
112 	parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
113 	if (!parts)
114 		return -ENOMEM;
115 
116 	for (i = 0; i < numparts; i++) {
117 		pentry = &ptable->pentry[i];
118 		if (pentry->name[0] == '\0')
119 			continue;
120 
121 		name = kstrdup(pentry->name, GFP_KERNEL);
122 		if (!name) {
123 			ret = -ENOMEM;
124 			goto out_free_parts;
125 		}
126 
127 		/* Convert name to lower case */
128 		for (c = name; *c != '\0'; c++)
129 			*c = tolower(*c);
130 
131 		parts[i].name = name;
132 		parts[i].offset = le32_to_cpu(pentry->offset) * mtd->erasesize;
133 		parts[i].mask_flags = pentry->attr;
134 		parts[i].size = le32_to_cpu(pentry->length) * mtd->erasesize;
135 		pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
136 			 i, pentry->name, le32_to_cpu(pentry->offset),
137 			 le32_to_cpu(pentry->length), pentry->attr);
138 	}
139 
140 	pr_debug("SMEM partition table found: ver: %d len: %d\n",
141 		 le32_to_cpu(ptable->version), numparts);
142 	*pparts = parts;
143 
144 	return numparts;
145 
146 out_free_parts:
147 	while (--i >= 0)
148 		kfree(parts[i].name);
149 	kfree(parts);
150 	*pparts = NULL;
151 
152 	return ret;
153 }
154 
155 static const struct of_device_id qcomsmem_of_match_table[] = {
156 	{ .compatible = "qcom,smem-part" },
157 	{},
158 };
159 MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
160 
161 static struct mtd_part_parser mtd_parser_qcomsmem = {
162 	.parse_fn = parse_qcomsmem_part,
163 	.name = "qcomsmem",
164 	.of_match_table = qcomsmem_of_match_table,
165 };
166 module_mtd_part_parser(mtd_parser_qcomsmem);
167 
168 MODULE_LICENSE("GPL v2");
169 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
170 MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");
171