blob: 487bfe53cd31ef7d22c575f63b76ea8cf2d74d8e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2015 MediaTek Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/partitions.h>
20
21#include <asm/div64.h>
22
23/* GPT Signature should be 0x5452415020494645 */
24#define GPT_SIGNATURE_1 0x54524150U
25#define GPT_SIGNATURE_2 0x20494645U
26
27/* GPT Offsets */
28#define HEADER_SIZE_OFFSET 12
29#define HEADER_CRC_OFFSET 16
30#define PRIMARY_HEADER_OFFSET 24
31#define BACKUP_HEADER_OFFSET 32
32#define FIRST_USABLE_LBA_OFFSET 40
33#define LAST_USABLE_LBA_OFFSET 48
34#define PARTITION_ENTRIES_OFFSET 72
35#define PARTITION_COUNT_OFFSET 80
36#define PENTRY_SIZE_OFFSET 84
37#define PARTITION_CRC_OFFSET 88
38
39#define ENTRY_SIZE 0x80
40
41#define UNIQUE_GUID_OFFSET 16
42#define FIRST_LBA_OFFSET 32
43#define LAST_LBA_OFFSET 40
44#define ATTRIBUTE_FLAG_OFFSET 48
45#define PARTITION_NAME_OFFSET 56
46
47#define MAX_GPT_NAME_SIZE 72
48#define PARTITION_TYPE_GUID_SIZE 16
49#define UNIQUE_PARTITION_GUID_SIZE 16
50#define NUM_PARTITIONS 128
51
52#define GET_LWORD_FROM_BYTE(ptr) (*(uint32_t *)((unsigned long)(ptr)))
53#define GET_LLWORD_FROM_BYTE(ptr) (*(uint64_t *)((unsigned long)(ptr)))
54
55struct chs {
56 uint8_t c;
57 uint8_t h;
58 uint8_t s;
59};
60
61struct mbr_part {
62 uint8_t status;
63 struct chs start;
64 uint8_t type;
65 struct chs end;
66 uint32_t lba_start;
67 uint32_t lba_length;
68};
69
70struct gpt_info {
71 uint64_t first_usable_lba;
72 uint64_t backup_header_lba;
73 uint32_t partition_entry_size;
74 uint32_t header_size;
75 uint32_t max_partition_count;
76};
77
78static int validate_mbr_partition(struct mtd_info *master,
79 const struct mbr_part *part)
80{
81 u32 lba_size, lba_number;
82 uint64_t temp;
83
84 if (mtd_type_is_nand(master) != 0)
85 lba_size = master->writesize;
86 else
87 lba_size = 512;
88
89 /* check for invalid types */
90 if ((int8_t)part->type == 0)
91 return -1;
92 /* check for invalid status */
93 if (part->status != (uint8_t)0x80 &&
94 part->status != (uint8_t)0x00)
95 return -1;
96
97 /* make sure the range fits within the device */
98 temp = (uint64_t)master->size;
99 do_div(temp, lba_size);
100 lba_number = temp;
101 if (part->lba_start >= lba_number)
102 return -1;
103 if ((part->lba_start + part->lba_length) > lba_number)
104 return -1;
105
106 return 0;
107}
108
109/*
110 * Parse the gpt header and get the required header fields
111 * Return 0 on valid signature
112 */
113static int partition_parse_gpt_header(unsigned char *buffer,
114 struct gpt_info *info)
115{
116 /* Check GPT Signature */
117 if (GET_LWORD_FROM_BYTE(&buffer[0]) != GPT_SIGNATURE_2 ||
118 GET_LWORD_FROM_BYTE(&buffer[4]) != GPT_SIGNATURE_1)
119 return 1;
120
121 info->header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
122 info->backup_header_lba =
123 GET_LLWORD_FROM_BYTE(&buffer[BACKUP_HEADER_OFFSET]);
124 info->first_usable_lba =
125 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
126 info->max_partition_count =
127 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
128 info->partition_entry_size =
129 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
130
131 return 0;
132}
133
134static void gpt_add_part(struct mtd_partition *part, char *name,
135 u64 offset, uint32_t mask_flags, uint64_t size)
136{
137 part->name = name;
138 part->offset = offset;
139 part->mask_flags = mask_flags;
140 part->size = size;
141}
142
143#define BBT_RESERVED_BLOCK 4
144#define SGPT_RESERVED_BLOCK 4
145static int gpt_parse(struct mtd_info *master,
146 const struct mtd_partition **pparts,
147 struct mtd_part_parser_data *data)
148{
149 struct mtd_partition *parts;
150 int curr_part = 0;
151 int err;
152 u_char *buf, *temp_buf;
153 size_t bytes_read = 0;
154 int i, j, n, part_entry_cnt, gpt_entries;
155 int gpt_partitions_exist = 0;
156 struct mbr_part part[4];
157 struct gpt_info gptinfo = {0, 0, 0, 0, 0};
158 loff_t partition_0;
159 u32 lba_size;
160 u32 reserved_size;
161 int tmp;
162
163 dev_dbg(&master->dev, "GPT: enter gpt parser...\n");
164
165 if (mtd_type_is_nand(master) != 0) {
166 lba_size = master->writesize;
167 /*
168 * As the unit of the total size given to MT2731 BROM is MB
169 * and BROM read SGPT at the last page(relative to the given
170 * total size), so reserved_size must be xMB.
171 * Generally, SLC NAND erasesize is 128K or 256K.
172 * To make reserved_size to be xMB,
173 * (BBT_RESERVED_BLOCK + SGPT_RESERVED_BLOCK) minimum value
174 * is 8.
175 */
176 reserved_size = (BBT_RESERVED_BLOCK + SGPT_RESERVED_BLOCK) *
177 master->erasesize;
178 } else {
179 lba_size = 512;
180 reserved_size = 0;
181 }
182
183 buf = kzalloc(lba_size, GFP_KERNEL);
184 if (buf == NULL)
185 return -ENOMEM;
186
187 err = mtd_read(master, 0, lba_size, &bytes_read, buf);
188 if (err < 0)
189 goto freebuf;
190
191 /* look for the aa55 tag */
192 if (buf[510] != (u_char)0x55 || buf[511] != (u_char)0xaa) {
193 dev_err(&master->dev, "GPT: not find aa55 @ 510,511\n");
194 goto freebuf;
195 }
196
197 /* see if a partition table makes sense here */
198 temp_buf = (u_char *)&part[0];
199 memcpy(temp_buf, &buf[446], sizeof(part));
200
201 /* validate each of the partition entries */
202 for (i = 0; i < 4; i++) {
203 if (validate_mbr_partition(master, &part[i]) >= 0) {
204 /*
205 * Type 0xEE indicates end of MBR
206 * and GPT partitions exist
207 */
208 if (part[i].type == (uint8_t)0xee) {
209 gpt_partitions_exist = 1;
210 break;
211 }
212 }
213 }
214
215 if (gpt_partitions_exist == 0) {
216 dev_err(&master->dev, "GPT: not find GPT\n");
217 goto freebuf;
218 }
219
220 err = mtd_read(master, (loff_t)lba_size, lba_size, &bytes_read, buf);
221 if (err < 0)
222 goto freebuf;
223
224 err = partition_parse_gpt_header(buf, &gptinfo);
225 if (err != 0) {
226 dev_warn(&master->dev, "GPT: Read GPT header fail, try to check the backup gpt.\n");
227 err = mtd_read(master, (loff_t)master->size - reserved_size -
228 (loff_t)lba_size,
229 lba_size, &bytes_read, buf);
230 if (err < 0) {
231 dev_err(&master->dev, "GPT: Could not read backup gpt.\n");
232 goto freebuf;
233 }
234
235 err = partition_parse_gpt_header(buf, &gptinfo);
236 if (err != 0) {
237 dev_err(&master->dev, "GPT: Primary and backup signatures invalid.\n");
238 goto freebuf;
239 }
240 }
241
242 parts = kcalloc(gptinfo.max_partition_count,
243 sizeof(struct mtd_partition), GFP_KERNEL);
244
245 if (parts == NULL)
246 return -ENOMEM;
247
248 part_entry_cnt = (int)lba_size / ENTRY_SIZE;
249 partition_0 =
250 (loff_t)GET_LLWORD_FROM_BYTE(&buf[PARTITION_ENTRIES_OFFSET]);
251
252 /* Read GPT Entries */
253 gpt_entries = roundup((int)gptinfo.max_partition_count, part_entry_cnt);
254 gpt_entries /= part_entry_cnt;
255 for (i = 0; i < gpt_entries; i++) {
256 err = mtd_read(master,
257 ((partition_0 + (loff_t)i) * (loff_t)lba_size),
258 lba_size, &bytes_read, buf);
259 if (err < 0) {
260 dev_err(&master->dev, "GPT: read failed reading partition entries.\n");
261 goto freeparts;
262 }
263
264 for (j = 0; j < part_entry_cnt; j++) {
265 int8_t type_guid[PARTITION_TYPE_GUID_SIZE];
266 char *name = kzalloc(MAX_GPT_NAME_SIZE,
267 GFP_KERNEL);
268 char UTF16_name[MAX_GPT_NAME_SIZE];
269 uint64_t first_lba, last_lba, size;
270
271 temp_buf = (u_char *)&type_guid[0];
272 memcpy(temp_buf,
273 &buf[(u32)j * gptinfo.partition_entry_size],
274 PARTITION_TYPE_GUID_SIZE);
275 if (type_guid[0] == 0 && type_guid[1] == 0) {
276 kfree(name);
277 goto parsedone;
278 }
279
280 tmp = j * (int)gptinfo.partition_entry_size;
281 tmp += FIRST_LBA_OFFSET;
282 first_lba = GET_LLWORD_FROM_BYTE(&buf[tmp]);
283 tmp = j * (int)gptinfo.partition_entry_size;
284 tmp += LAST_LBA_OFFSET;
285 last_lba = GET_LLWORD_FROM_BYTE(&buf[tmp]);
286 size = last_lba - first_lba + 1ULL;
287
288 memset(&UTF16_name[0], 0x00, MAX_GPT_NAME_SIZE);
289 temp_buf = (u_char *)&UTF16_name[0];
290 memcpy(temp_buf,
291 &buf[(u32)j * gptinfo.partition_entry_size +
292 (uint32_t)PARTITION_NAME_OFFSET],
293 MAX_GPT_NAME_SIZE);
294
295 /*
296 * Currently partition names in *.xml are UTF-8 and
297 * lowercase only supporting english for now so removing
298 * 2nd byte of UTF-16
299 */
300 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++)
301 name[n] = UTF16_name[n * 2];
302
303 dev_dbg(&master->dev, "partition(%s) first_lba(%llu), last_lba(%llu), size(%llu)\n",
304 name, first_lba, last_lba, size);
305
306 gpt_add_part(&parts[curr_part++], name,
307 first_lba * lba_size, 0,
308 (last_lba - first_lba + 1ULL) * lba_size);
309
310 dev_dbg(&master->dev, "gpt there are <%d> parititons.\n",
311 curr_part);
312 }
313 }
314
315parsedone:
316 *pparts = parts;
317 kfree(buf);
318 return curr_part;
319
320freeparts:
321 kfree(parts);
322freebuf:
323 kfree(buf);
324 return 0;
325};
326
327static void gpt_parser_cleanup(const struct mtd_partition *pparts,
328 int nr_parts)
329{
330 struct mtd_partition *tmp_pparts = pparts;
331 int part;
332
333 for (part = 0; part < nr_parts; part++, tmp_pparts++)
334 kfree(tmp_pparts->name);
335
336 kfree(pparts);
337}
338
339static struct mtd_part_parser gpt_parser = {
340 .owner = THIS_MODULE,
341 .parse_fn = gpt_parse,
342 .cleanup = gpt_parser_cleanup,
343 .name = "gptpart",
344};
345
346static int __init gptpart_init(void)
347{
348 return register_mtd_parser(&gpt_parser);
349}
350
351static void __exit gptpart_exit(void)
352{
353 deregister_mtd_parser(&gpt_parser);
354}
355
356module_init(gptpart_init);
357module_exit(gptpart_exit);
358
359MODULE_LICENSE("GPL v2");
360MODULE_DESCRIPTION("GPT partitioning for flash memories");