summaryrefslogtreecommitdiffstats
path: root/common/partitions.c
blob: 17c2f1eb281a066b43f220aa311387a0b125a5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2009...2011 Juergen Beisert, Pengutronix
 */

/**
 * @file
 * @brief Generic support for partition tables on disk like media
 */
#include <common.h>
#include <malloc.h>
#include <errno.h>
#include <block.h>
#include <asm/unaligned.h>
#include <disks.h>
#include <filetype.h>
#include <linux/err.h>
#include <partitions.h>

static LIST_HEAD(partition_parser_list);

/**
 * Register one partition on the given block device
 * @param blk Block device to register to
 * @param part Partition description
 * @param no Partition number
 * @return 0 on success
 */
static int register_one_partition(struct block_device *blk, struct partition *part)
{
	char *partition_name;
	int ret;
	struct cdev *cdev;
	struct devfs_partition partinfo = {
		.offset = part->first_sec * SECTOR_SIZE,
		.size = part->size * SECTOR_SIZE,
	};

	partition_name = basprintf("%s.%d", blk->cdev.name, part->num);
	if (!partition_name)
		return -ENOMEM;

	partinfo.name = partition_name;

	dev_dbg(blk->dev, "Registering partition %s on drive %s (partuuid=%s)\n",
				partition_name, blk->cdev.name, part->partuuid);
	cdev = cdevfs_add_partition(&blk->cdev, &partinfo);
	if (IS_ERR(cdev)) {
		ret = PTR_ERR(cdev);
		goto out;
	}

	cdev->flags |= DEVFS_PARTITION_FROM_TABLE | part->flags;
	cdev->typeflags |= part->typeflags;
	cdev->typeuuid = part->typeuuid;
	strcpy(cdev->partuuid, part->partuuid);

	free(partition_name);

	if (!part->name[0])
		return 0;

	partition_name = xasprintf("%s.%s", blk->cdev.name, part->name);
	ret = devfs_create_link(cdev, partition_name);
	if (ret)
		dev_warn(blk->dev, "Failed to create link from %s to %s\n",
			 partition_name, blk->cdev.name);
	free(partition_name);

	return 0;
out:
	free(partition_name);
	return ret;
}

static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
{
	enum filetype type;
	struct partition_parser *parser;

	/* first new partition table as EFI GPT */
	type = file_detect_partition_table(buf, SECTOR_SIZE * 2);

	list_for_each_entry(parser, &partition_parser_list, list) {
		if (parser->type == type)
			return parser;
	}

	/* if not parser found search for old one
	 * so if EFI GPT not enable take it as MBR
	 * useful for compatibility
	 */
	type = file_detect_partition_table(buf, SECTOR_SIZE);
	if (type == filetype_fat && !is_fat_boot_sector(buf))
		type = filetype_mbr;

	list_for_each_entry(parser, &partition_parser_list, list) {
		if (parser->type == type)
			return parser;
	}

	return NULL;
}

struct partition_desc *partition_table_new(struct block_device *blk, const char *type)
{
	struct partition_desc *pdesc;
	struct partition_parser *parser;

	list_for_each_entry(parser, &partition_parser_list, list) {
		if (!strcmp(parser->name, type))
			goto found;
	}

	pr_err("Cannot find partition parser \"%s\"\n", type);

	return ERR_PTR(-ENOSYS);

found:
	pdesc = parser->create(blk);
	if (IS_ERR(pdesc))
		return ERR_CAST(pdesc);

	pdesc->parser = parser;

	return pdesc;
}

struct partition_desc *partition_table_read(struct block_device *blk)
{
	struct partition_parser *parser;
	struct partition_desc *pdesc = NULL;
	uint8_t *buf;
	int ret;

	buf = malloc(2 * SECTOR_SIZE);

	ret = block_read(blk, buf, 0, 2);
	if (ret != 0) {
		dev_err(blk->dev, "Cannot read MBR/partition table: %pe\n", ERR_PTR(ret));
		goto err;
	}

	parser = partition_parser_get_by_filetype(buf);
	if (!parser)
		goto err;

	pdesc = parser->parse(buf, blk);
	if (!pdesc)
		goto err;

	pdesc->parser = parser;
err:
	free(buf);

	return pdesc;
}

int partition_table_write(struct partition_desc *pdesc)
{
	if (!pdesc->parser->write)
		return -ENOSYS;

	return pdesc->parser->write(pdesc);
}

static bool overlap(uint64_t s1, uint64_t e1, uint64_t s2, uint64_t e2)
{
	if (e1 < s2)
		return false;
	if (s1 > e2)
		return false;
	return true;
}

int partition_create(struct partition_desc *pdesc, const char *name,
		     const char *fs_type, uint64_t lba_start, uint64_t lba_end)
{
	struct partition *part;

	if (!pdesc->parser->mkpart)
		return -ENOSYS;

	if (lba_end < lba_start) {
		pr_err("lba_end < lba_start: %llu < %llu\n", lba_end, lba_start);
		return -EINVAL;
	}

	if (lba_end >= pdesc->blk->num_blocks) {
		pr_err("lba_end exceeds device: %llu >= %llu\n", lba_end, pdesc->blk->num_blocks);
		return -EINVAL;
	}

	list_for_each_entry(part, &pdesc->partitions, list) {
		if (overlap(part->first_sec,
				part->first_sec + part->size - 1,
				lba_start, lba_end)) {
			pr_err("new partition %llu-%llu overlaps with partition %s (%llu-%llu)\n",
			       lba_start, lba_end, part->name, part->first_sec,
				part->first_sec + part->size - 1);
			return -EINVAL;
		}
	}

	return pdesc->parser->mkpart(pdesc, name, fs_type, lba_start, lba_end);
}

int partition_remove(struct partition_desc *pdesc, int num)
{
	struct partition *part;

	if (!pdesc->parser->rmpart)
		return -ENOSYS;

	list_for_each_entry(part, &pdesc->partitions, list) {
		if (part->num == num)
			return pdesc->parser->rmpart(pdesc, part);
	}

	pr_err("Partition %d doesn't exist\n", num);
	return -ENOENT;
}

void partition_table_free(struct partition_desc *pdesc)
{
	pdesc->parser->partition_free(pdesc);
}

void partition_desc_init(struct partition_desc *pd, struct block_device *blk)
{
	pd->blk = blk;
	INIT_LIST_HEAD(&pd->partitions);
}

/**
 * Try to collect partition information on the given block device
 * @param blk Block device to examine
 * @return 0 most of the time, negative value else
 *
 * It is not a failure if no partition information is found
 */
int parse_partition_table(struct block_device *blk)
{
	int i;
	int rc = 0;
	struct partition *part;
	struct partition_desc *pdesc;

	pdesc = partition_table_read(blk);
	if (!pdesc)
		return 0;

	/* at least one partition description found */
	list_for_each_entry(part, &pdesc->partitions, list) {
		rc = register_one_partition(blk, part);
		if (rc != 0)
			dev_err(blk->dev,
				"Failed to register partition %d on %s (%d)\n",
				i, blk->cdev.name, rc);
		if (rc != -ENODEV)
			rc = 0;
	}

	partition_table_free(pdesc);

	return rc;
}

int reparse_partition_table(struct block_device *blk)
{
	struct cdev *cdev = &blk->cdev;
	struct cdev *c, *tmp;

	list_for_each_entry(c, &cdev->partitions, partition_entry) {
		if (c->open) {
			pr_warn("%s is busy, will continue to use old partition table\n", c->name);
			return -EBUSY;
		}
	}

	list_for_each_entry_safe(c, tmp, &cdev->partitions, partition_entry) {
		if (c->flags & DEVFS_PARTITION_FROM_TABLE)
			cdevfs_del_partition(c);
	}

	return parse_partition_table(blk);
}

int partition_parser_register(struct partition_parser *p)
{
	list_add_tail(&p->list, &partition_parser_list);

	return 0;
}

/**
 * cdev_unallocated_space - return unallocated space
 * cdev: The cdev
 *
 * This function returns the space that is not allocated by any partition
 * at the start of a device.
 *
 * Return: The unallocated space at the start of the device in bytes
 */
loff_t cdev_unallocated_space(struct cdev *cdev)
{
	struct cdev *partcdev;
	loff_t start;

	if (!cdev)
		return 0;

	start = cdev->size;

	list_for_each_entry(partcdev, &cdev->partitions, partition_entry) {
		if (partcdev->offset < start)
			start = partcdev->offset;
	}

	return start;
}