1 // SPDX-License-Identifier: GPL-2.0-only
3 * Code for looking up block devices in the early boot code before mounting the
6 #include <linux/blkdev.h>
7 #include <linux/ctype.h>
15 * match_dev_by_uuid - callback for finding a partition using its uuid
16 * @dev: device passed in by the caller
17 * @data: opaque pointer to the desired struct uuidcmp to match
19 * Returns 1 if the device matches, and 0 otherwise.
21 static int __init match_dev_by_uuid(struct device *dev, const void *data)
23 struct block_device *bdev = dev_to_bdev(dev);
24 const struct uuidcmp *cmp = data;
26 if (!bdev->bd_meta_info ||
27 strncasecmp(cmp->uuid, bdev->bd_meta_info->uuid, cmp->len))
33 * devt_from_partuuid - looks up the dev_t of a partition by its UUID
34 * @uuid_str: char array containing ascii UUID
37 * The function will return the first partition which contains a matching
38 * UUID value in its partition_meta_info struct. This does not search
39 * by filesystem UUIDs.
41 * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
42 * extracted and used as an offset from the partition identified by the UUID.
44 * Returns 0 on success or a negative error code on failure.
46 static int __init devt_from_partuuid(const char *uuid_str, dev_t *devt)
49 struct device *dev = NULL;
55 slash = strchr(uuid_str, '/');
56 /* Check for optional partition number offset attributes. */
60 /* Explicitly fail on poor PARTUUID syntax. */
61 if (sscanf(slash + 1, "PARTNROFF=%d%c", &offset, &c) != 1)
63 cmp.len = slash - uuid_str;
65 cmp.len = strlen(uuid_str);
71 dev = class_find_device(&block_class, NULL, &cmp, &match_dev_by_uuid);
77 * Attempt to find the requested partition by adding an offset
78 * to the partition number found by UUID.
80 *devt = part_devt(dev_to_disk(dev),
81 dev_to_bdev(dev)->bd_partno + offset);
90 pr_err("VFS: PARTUUID= is invalid.\n"
91 "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
96 * match_dev_by_label - callback for finding a partition using its label
97 * @dev: device passed in by the caller
98 * @data: opaque pointer to the label to match
100 * Returns 1 if the device matches, and 0 otherwise.
102 static int __init match_dev_by_label(struct device *dev, const void *data)
104 struct block_device *bdev = dev_to_bdev(dev);
105 const char *label = data;
107 if (!bdev->bd_meta_info || strcmp(label, bdev->bd_meta_info->volname))
112 static int __init devt_from_partlabel(const char *label, dev_t *devt)
116 dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
124 static dev_t __init blk_lookup_devt(const char *name, int partno)
126 dev_t devt = MKDEV(0, 0);
127 struct class_dev_iter iter;
130 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
131 while ((dev = class_dev_iter_next(&iter))) {
132 struct gendisk *disk = dev_to_disk(dev);
134 if (strcmp(dev_name(dev), name))
137 if (partno < disk->minors) {
138 /* We need to return the right devno, even
139 * if the partition doesn't exist yet.
141 devt = MKDEV(MAJOR(dev->devt),
142 MINOR(dev->devt) + partno);
144 devt = part_devt(disk, partno);
149 class_dev_iter_exit(&iter);
153 static int __init devt_from_devname(const char *name, dev_t *devt)
159 if (strlen(name) > 31)
162 for (p = s; *p; p++) {
167 *devt = blk_lookup_devt(s, 0);
172 * Try non-existent, but valid partition, which may only exist after
173 * opening the device, like partitioned md devices.
175 while (p > s && isdigit(p[-1]))
177 if (p == s || !*p || *p == '0')
180 /* try disk name without <part number> */
181 part = simple_strtoul(p, NULL, 10);
183 *devt = blk_lookup_devt(s, part);
187 /* try disk name without p<part number> */
188 if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
191 *devt = blk_lookup_devt(s, part);
197 static int __init devt_from_devnum(const char *name, dev_t *devt)
199 unsigned maj, min, offset;
202 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
203 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
204 *devt = MKDEV(maj, min);
205 if (maj != MAJOR(*devt) || min != MINOR(*devt))
208 *devt = new_decode_dev(simple_strtoul(name, &p, 16));
217 * Convert a name into device number. We accept the following variants:
219 * 1) <hex_major><hex_minor> device number in hexadecimal represents itself
220 * no leading 0x, for example b302.
221 * 3) /dev/<disk_name> represents the device number of disk
222 * 4) /dev/<disk_name><decimal> represents the device number
223 * of partition - device number of disk plus the partition number
224 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
225 * used when disk name of partitioned disk ends on a digit.
226 * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
227 * unique id of a partition if the partition table provides it.
228 * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
229 * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
230 * filled hex representation of the 32-bit "NT disk signature", and PP
231 * is a zero-filled hex representation of the 1-based partition number.
232 * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
233 * a partition with a known unique id.
234 * 8) <major>:<minor> major and minor number of the device separated by
236 * 9) PARTLABEL=<name> with name being the GPT partition label.
237 * MSDOS partitions do not support labels!
239 * If name doesn't have fall into the categories above, we return (0,0).
240 * block_class is used to check if something is a disk name. If the disk
241 * name contains slashes, the device name has them replaced with
244 int __init early_lookup_bdev(const char *name, dev_t *devt)
246 if (strncmp(name, "PARTUUID=", 9) == 0)
247 return devt_from_partuuid(name + 9, devt);
248 if (strncmp(name, "PARTLABEL=", 10) == 0)
249 return devt_from_partlabel(name + 10, devt);
250 if (strncmp(name, "/dev/", 5) == 0)
251 return devt_from_devname(name + 5, devt);
252 return devt_from_devnum(name, devt);
255 static char __init *bdevt_str(dev_t devt, char *buf)
257 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
258 char tbuf[BDEVT_SIZE];
259 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
260 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
262 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
268 * print a full list of all partitions - intended for places where the root
269 * filesystem can't be mounted and thus to give the victim some idea of what
272 void __init printk_all_partitions(void)
274 struct class_dev_iter iter;
277 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
278 while ((dev = class_dev_iter_next(&iter))) {
279 struct gendisk *disk = dev_to_disk(dev);
280 struct block_device *part;
281 char devt_buf[BDEVT_SIZE];
285 * Don't show empty devices or things that have been
288 if (get_capacity(disk) == 0 || (disk->flags & GENHD_FL_HIDDEN))
292 * Note, unlike /proc/partitions, I am showing the numbers in
293 * hex - the same format as the root= option takes.
296 xa_for_each(&disk->part_tbl, idx, part) {
297 if (!bdev_nr_sectors(part))
299 printk("%s%s %10llu %pg %s",
300 bdev_is_partition(part) ? " " : "",
301 bdevt_str(part->bd_dev, devt_buf),
302 bdev_nr_sectors(part) >> 1, part,
304 part->bd_meta_info->uuid : "");
305 if (bdev_is_partition(part))
307 else if (dev->parent && dev->parent->driver)
308 printk(" driver: %s\n",
309 dev->parent->driver->name);
311 printk(" (driver?)\n");
315 class_dev_iter_exit(&iter);