0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/ctype.h>
0009 #include <linux/module.h>
0010 #include <linux/mtd/mtd.h>
0011 #include <linux/mtd/partitions.h>
0012 #include <linux/slab.h>
0013 #include <linux/soc/qcom/smem.h>
0014
0015 #define SMEM_AARM_PARTITION_TABLE 9
0016 #define SMEM_APPS 0
0017
0018 #define SMEM_FLASH_PART_MAGIC1 0x55ee73aa
0019 #define SMEM_FLASH_PART_MAGIC2 0xe35ebddb
0020 #define SMEM_FLASH_PTABLE_V3 3
0021 #define SMEM_FLASH_PTABLE_V4 4
0022 #define SMEM_FLASH_PTABLE_MAX_PARTS_V3 16
0023 #define SMEM_FLASH_PTABLE_MAX_PARTS_V4 48
0024 #define SMEM_FLASH_PTABLE_HDR_LEN (4 * sizeof(u32))
0025 #define SMEM_FLASH_PTABLE_NAME_SIZE 16
0026
0027
0028
0029
0030
0031
0032
0033
0034 struct smem_flash_pentry {
0035 char name[SMEM_FLASH_PTABLE_NAME_SIZE];
0036 __le32 offset;
0037 __le32 length;
0038 u8 attr;
0039 } __packed __aligned(4);
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 struct smem_flash_ptable {
0050 __le32 magic1;
0051 __le32 magic2;
0052 __le32 version;
0053 __le32 numparts;
0054 struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
0055 } __packed __aligned(4);
0056
0057 static int parse_qcomsmem_part(struct mtd_info *mtd,
0058 const struct mtd_partition **pparts,
0059 struct mtd_part_parser_data *data)
0060 {
0061 size_t len = SMEM_FLASH_PTABLE_HDR_LEN;
0062 int ret, i, j, tmpparts, numparts = 0;
0063 struct smem_flash_pentry *pentry;
0064 struct smem_flash_ptable *ptable;
0065 struct mtd_partition *parts;
0066 char *name, *c;
0067
0068 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_USE_4K_SECTORS)
0069 && mtd->type == MTD_NORFLASH) {
0070 pr_err("%s: SMEM partition parser is incompatible with 4K sectors\n",
0071 mtd->name);
0072 return -EINVAL;
0073 }
0074
0075 pr_debug("Parsing partition table info from SMEM\n");
0076 ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
0077 if (IS_ERR(ptable)) {
0078 if (PTR_ERR(ptable) != -EPROBE_DEFER)
0079 pr_err("Error reading partition table header\n");
0080 return PTR_ERR(ptable);
0081 }
0082
0083
0084 if (le32_to_cpu(ptable->magic1) != SMEM_FLASH_PART_MAGIC1 ||
0085 le32_to_cpu(ptable->magic2) != SMEM_FLASH_PART_MAGIC2) {
0086 pr_err("Partition table magic verification failed\n");
0087 return -EINVAL;
0088 }
0089
0090
0091 tmpparts = le32_to_cpu(ptable->numparts);
0092 if (tmpparts > SMEM_FLASH_PTABLE_MAX_PARTS_V4) {
0093 pr_err("Partition numbers exceed the max limit\n");
0094 return -EINVAL;
0095 }
0096
0097
0098 if (le32_to_cpu(ptable->version) <= SMEM_FLASH_PTABLE_V3) {
0099 len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V3 *
0100 sizeof(struct smem_flash_pentry);
0101 } else if (le32_to_cpu(ptable->version) == SMEM_FLASH_PTABLE_V4) {
0102 len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V4 *
0103 sizeof(struct smem_flash_pentry);
0104 } else {
0105 pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
0106 return -EINVAL;
0107 }
0108
0109
0110
0111
0112
0113
0114 ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
0115 if (IS_ERR(ptable)) {
0116 pr_err("Error reading partition table\n");
0117 return PTR_ERR(ptable);
0118 }
0119
0120 for (i = 0; i < tmpparts; i++) {
0121 pentry = &ptable->pentry[i];
0122 if (pentry->name[0] != '\0')
0123 numparts++;
0124 }
0125
0126 parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
0127 if (!parts)
0128 return -ENOMEM;
0129
0130 for (i = 0, j = 0; i < tmpparts; i++) {
0131 pentry = &ptable->pentry[i];
0132 if (pentry->name[0] == '\0')
0133 continue;
0134
0135 name = kstrdup(pentry->name, GFP_KERNEL);
0136 if (!name) {
0137 ret = -ENOMEM;
0138 goto out_free_parts;
0139 }
0140
0141
0142 for (c = name; *c != '\0'; c++)
0143 *c = tolower(*c);
0144
0145 parts[j].name = name;
0146 parts[j].offset = le32_to_cpu(pentry->offset) * mtd->erasesize;
0147 parts[j].mask_flags = pentry->attr;
0148 parts[j].size = le32_to_cpu(pentry->length) * mtd->erasesize;
0149 pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
0150 i, pentry->name, le32_to_cpu(pentry->offset),
0151 le32_to_cpu(pentry->length), pentry->attr);
0152 j++;
0153 }
0154
0155 pr_debug("SMEM partition table found: ver: %d len: %d\n",
0156 le32_to_cpu(ptable->version), tmpparts);
0157 *pparts = parts;
0158
0159 return numparts;
0160
0161 out_free_parts:
0162 while (--j >= 0)
0163 kfree(parts[j].name);
0164 kfree(parts);
0165 *pparts = NULL;
0166
0167 return ret;
0168 }
0169
0170 static void parse_qcomsmem_cleanup(const struct mtd_partition *pparts,
0171 int nr_parts)
0172 {
0173 int i;
0174
0175 for (i = 0; i < nr_parts; i++)
0176 kfree(pparts[i].name);
0177
0178 kfree(pparts);
0179 }
0180
0181 static const struct of_device_id qcomsmem_of_match_table[] = {
0182 { .compatible = "qcom,smem-part" },
0183 {},
0184 };
0185 MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
0186
0187 static struct mtd_part_parser mtd_parser_qcomsmem = {
0188 .parse_fn = parse_qcomsmem_part,
0189 .cleanup = parse_qcomsmem_cleanup,
0190 .name = "qcomsmem",
0191 .of_match_table = qcomsmem_of_match_table,
0192 };
0193 module_mtd_part_parser(mtd_parser_qcomsmem);
0194
0195 MODULE_LICENSE("GPL v2");
0196 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
0197 MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");