blob: a2cfd8b790249ae3c0e61e9ec8fd761118434f4f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 MediaTek Inc.
4 * Author: Guochun Mao <guochun.mao@mediatek.com>
5 * Xiaolei Li <xiaolei.li@mediatek.com>
6 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/string.h>
12#include <linux/mtd/mtd.h>
13#include <linux/mtd/partitions.h>
14#include <asm/div64.h>
15
16#define MAX_PARTITION_COUNT 128
17#define MAX_PARTITION_NAME_LEN 64
18#define PT_SIG 0x50547633 //"PTv3"
19#define MPT_SIG 0x4D505433 //"MPT3"
20#define PT_SIG_SIZE 8
21
22#define is_valid_mpt(buf) ((*(uint32_t *)(buf)) == MPT_SIG)
23#define is_valid_pt(buf) ((*(uint32_t *)(buf)) == PT_SIG)
24#define is_valid_pt_v1(buf) ((*(uint32_t *)(buf)) == PT_SIG_V1)
25
26typedef struct
27{
28 unsigned char name[MAX_PARTITION_NAME_LEN]; /* partition name */
29 unsigned long long size; /* partition size */
30 unsigned long long part_id; /* partition region */ //will be used as download type on L branch. xiaolei
31 unsigned long long offset; /* partition start */
32 unsigned long long mask_flags; /* partition flags */
33} pt_resident;
34
35static void pmt_add_part(struct mtd_partition *part, char *name,
36 u64 offset, uint32_t mask_flags, uint64_t size)
37{
38 part->name = kstrdup_const(name, GFP_KERNEL);
39 part->offset = offset;
40 part->mask_flags = mask_flags;
41 part->size = size;
42}
43
44static int pmt_parse(struct mtd_info *master,
45 const struct mtd_partition **pparts,
46 struct mtd_part_parser_data *data)
47{
48 struct mtd_partition *parts;
49 int err, i;
50 u_char *buf;
51 u32 pmt_size = sizeof(pt_resident) * MAX_PARTITION_COUNT;
52 u8 pmt[PT_SIG_SIZE] = {0};
53 pt_resident *pt;
54 size_t bytes_read = 0;
55 loff_t pmt_start_addr = (loff_t)master->size - 6 * (loff_t)master->erasesize;
56 u8 max_partition_count = 0;
57
58 dev_dbg(&master->dev, "PMT: enter pmt parser...\n");
59
60 buf = kzalloc(pmt_size, GFP_KERNEL);
61 if (buf == NULL)
62 return -ENOMEM;
63
64 pt = (pt_resident *)buf;
65 err = mtd_read(master, pmt_start_addr, sizeof(pmt), &bytes_read, pmt);
66 if (err < 0)
67 goto freebuf;
68
69 /* look for the PMT tag */
70 if (is_valid_mpt(pmt) == 0 && is_valid_pt(pmt) == 0) {
71 dev_err(&master->dev, "PMT: not find sig in 1st PMT block, try 2nd one\n");
72 pmt_start_addr += master->erasesize;
73 err = mtd_read(master, pmt_start_addr, sizeof(pmt), &bytes_read, pmt);
74 if (err < 0)
75 goto freebuf;
76 if (is_valid_mpt(pmt) == 0 && is_valid_pt(pmt) == 0) {
77 dev_err(&master->dev, "PMT: not find sig in 2st PMT block, give up\n");
78 goto freebuf;
79 }
80 }
81
82 err = mtd_read(master, pmt_start_addr + PT_SIG_SIZE, pmt_size, &bytes_read, buf);
83 if (err < 0)
84 goto freebuf;
85
86 for (i = 0 ; i < MAX_PARTITION_COUNT ; i++) {
87 if (strlen(pt[i].name) == 0)
88 break;
89 }
90 max_partition_count = i;
91 dev_dbg(&master->dev, "pmt there are <%d> parititons.\n",
92 max_partition_count);
93
94 parts = kcalloc(max_partition_count,
95 sizeof(struct mtd_partition), GFP_KERNEL);
96 if (parts == NULL)
97 goto freebuf;
98
99 for (i = 0 ; i < max_partition_count ; i++) {
100 pmt_add_part(&parts[i], pt[i].name,
101 pt[i].offset, 0, pt[i].size);
102
103 }
104
105parsedone:
106 *pparts = parts;
107 kfree(buf);
108 return max_partition_count;
109
110freebuf:
111 kfree(buf);
112 return 0;
113};
114
115static struct mtd_part_parser pmt_parser = {
116 .owner = THIS_MODULE,
117 .parse_fn = pmt_parse,
118 .name = "pmtpart",
119};
120
121static int __init pmtpart_init(void)
122{
123 return register_mtd_parser(&pmt_parser);
124}
125
126static void __exit pmtpart_exit(void)
127{
128 deregister_mtd_parser(&pmt_parser);
129}
130
131module_init(pmtpart_init);
132module_exit(pmtpart_exit);
133
134MODULE_LICENSE("GPL v2");
135MODULE_DESCRIPTION("PMT partitioning for flash memories");