early-lookup.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Code for looking up block devices in the early boot code before mounting the
  4. * root file system.
  5. */
  6. #include <linux/blkdev.h>
  7. #include <linux/ctype.h>
  8. struct uuidcmp {
  9. const char *uuid;
  10. int len;
  11. };
  12. /**
  13. * match_dev_by_uuid - callback for finding a partition using its uuid
  14. * @dev: device passed in by the caller
  15. * @data: opaque pointer to the desired struct uuidcmp to match
  16. *
  17. * Returns 1 if the device matches, and 0 otherwise.
  18. */
  19. static int __init match_dev_by_uuid(struct device *dev, const void *data)
  20. {
  21. struct block_device *bdev = dev_to_bdev(dev);
  22. const struct uuidcmp *cmp = data;
  23. if (!bdev->bd_meta_info ||
  24. strncasecmp(cmp->uuid, bdev->bd_meta_info->uuid, cmp->len))
  25. return 0;
  26. return 1;
  27. }
  28. /**
  29. * devt_from_partuuid - looks up the dev_t of a partition by its UUID
  30. * @uuid_str: char array containing ascii UUID
  31. * @devt: dev_t result
  32. *
  33. * The function will return the first partition which contains a matching
  34. * UUID value in its partition_meta_info struct. This does not search
  35. * by filesystem UUIDs.
  36. *
  37. * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
  38. * extracted and used as an offset from the partition identified by the UUID.
  39. *
  40. * Returns 0 on success or a negative error code on failure.
  41. */
  42. static int __init devt_from_partuuid(const char *uuid_str, dev_t *devt)
  43. {
  44. struct uuidcmp cmp;
  45. struct device *dev = NULL;
  46. int offset = 0;
  47. char *slash;
  48. cmp.uuid = uuid_str;
  49. slash = strchr(uuid_str, '/');
  50. /* Check for optional partition number offset attributes. */
  51. if (slash) {
  52. char c = 0;
  53. /* Explicitly fail on poor PARTUUID syntax. */
  54. if (sscanf(slash + 1, "PARTNROFF=%d%c", &offset, &c) != 1)
  55. goto out_invalid;
  56. cmp.len = slash - uuid_str;
  57. } else {
  58. cmp.len = strlen(uuid_str);
  59. }
  60. if (!cmp.len)
  61. goto out_invalid;
  62. dev = class_find_device(&block_class, NULL, &cmp, &match_dev_by_uuid);
  63. if (!dev)
  64. return -ENODEV;
  65. if (offset) {
  66. /*
  67. * Attempt to find the requested partition by adding an offset
  68. * to the partition number found by UUID.
  69. */
  70. *devt = part_devt(dev_to_disk(dev),
  71. bdev_partno(dev_to_bdev(dev)) + offset);
  72. } else {
  73. *devt = dev->devt;
  74. }
  75. put_device(dev);
  76. return 0;
  77. out_invalid:
  78. pr_err("VFS: PARTUUID= is invalid.\n"
  79. "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
  80. return -EINVAL;
  81. }
  82. /**
  83. * match_dev_by_label - callback for finding a partition using its label
  84. * @dev: device passed in by the caller
  85. * @data: opaque pointer to the label to match
  86. *
  87. * Returns 1 if the device matches, and 0 otherwise.
  88. */
  89. static int __init match_dev_by_label(struct device *dev, const void *data)
  90. {
  91. struct block_device *bdev = dev_to_bdev(dev);
  92. const char *label = data;
  93. if (!bdev->bd_meta_info || strcmp(label, bdev->bd_meta_info->volname))
  94. return 0;
  95. return 1;
  96. }
  97. static int __init devt_from_partlabel(const char *label, dev_t *devt)
  98. {
  99. struct device *dev;
  100. dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
  101. if (!dev)
  102. return -ENODEV;
  103. *devt = dev->devt;
  104. put_device(dev);
  105. return 0;
  106. }
  107. static dev_t __init blk_lookup_devt(const char *name, int partno)
  108. {
  109. dev_t devt = MKDEV(0, 0);
  110. struct class_dev_iter iter;
  111. struct device *dev;
  112. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  113. while ((dev = class_dev_iter_next(&iter))) {
  114. struct gendisk *disk = dev_to_disk(dev);
  115. if (strcmp(dev_name(dev), name))
  116. continue;
  117. if (partno < disk->minors) {
  118. /* We need to return the right devno, even
  119. * if the partition doesn't exist yet.
  120. */
  121. devt = MKDEV(MAJOR(dev->devt),
  122. MINOR(dev->devt) + partno);
  123. } else {
  124. devt = part_devt(disk, partno);
  125. if (devt)
  126. break;
  127. }
  128. }
  129. class_dev_iter_exit(&iter);
  130. return devt;
  131. }
  132. static int __init devt_from_devname(const char *name, dev_t *devt)
  133. {
  134. int part;
  135. char s[32];
  136. char *p;
  137. if (strlen(name) > 31)
  138. return -EINVAL;
  139. strcpy(s, name);
  140. for (p = s; *p; p++) {
  141. if (*p == '/')
  142. *p = '!';
  143. }
  144. *devt = blk_lookup_devt(s, 0);
  145. if (*devt)
  146. return 0;
  147. /*
  148. * Try non-existent, but valid partition, which may only exist after
  149. * opening the device, like partitioned md devices.
  150. */
  151. while (p > s && isdigit(p[-1]))
  152. p--;
  153. if (p == s || !*p || *p == '0')
  154. return -ENODEV;
  155. /* try disk name without <part number> */
  156. part = simple_strtoul(p, NULL, 10);
  157. *p = '\0';
  158. *devt = blk_lookup_devt(s, part);
  159. if (*devt)
  160. return 0;
  161. /* try disk name without p<part number> */
  162. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  163. return -ENODEV;
  164. p[-1] = '\0';
  165. *devt = blk_lookup_devt(s, part);
  166. if (*devt)
  167. return 0;
  168. return -ENODEV;
  169. }
  170. static int __init devt_from_devnum(const char *name, dev_t *devt)
  171. {
  172. unsigned maj, min, offset;
  173. char *p, dummy;
  174. if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
  175. sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
  176. *devt = MKDEV(maj, min);
  177. if (maj != MAJOR(*devt) || min != MINOR(*devt))
  178. return -EINVAL;
  179. } else {
  180. *devt = new_decode_dev(simple_strtoul(name, &p, 16));
  181. if (*p)
  182. return -EINVAL;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * Convert a name into device number. We accept the following variants:
  188. *
  189. * 1) <hex_major><hex_minor> device number in hexadecimal represents itself
  190. * no leading 0x, for example b302.
  191. * 3) /dev/<disk_name> represents the device number of disk
  192. * 4) /dev/<disk_name><decimal> represents the device number
  193. * of partition - device number of disk plus the partition number
  194. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  195. * used when disk name of partitioned disk ends on a digit.
  196. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  197. * unique id of a partition if the partition table provides it.
  198. * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
  199. * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
  200. * filled hex representation of the 32-bit "NT disk signature", and PP
  201. * is a zero-filled hex representation of the 1-based partition number.
  202. * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
  203. * a partition with a known unique id.
  204. * 8) <major>:<minor> major and minor number of the device separated by
  205. * a colon.
  206. * 9) PARTLABEL=<name> with name being the GPT partition label.
  207. * MSDOS partitions do not support labels!
  208. *
  209. * If name doesn't have fall into the categories above, we return (0,0).
  210. * block_class is used to check if something is a disk name. If the disk
  211. * name contains slashes, the device name has them replaced with
  212. * bangs.
  213. */
  214. int __init early_lookup_bdev(const char *name, dev_t *devt)
  215. {
  216. if (strncmp(name, "PARTUUID=", 9) == 0)
  217. return devt_from_partuuid(name + 9, devt);
  218. if (strncmp(name, "PARTLABEL=", 10) == 0)
  219. return devt_from_partlabel(name + 10, devt);
  220. if (strncmp(name, "/dev/", 5) == 0)
  221. return devt_from_devname(name + 5, devt);
  222. return devt_from_devnum(name, devt);
  223. }
  224. static char __init *bdevt_str(dev_t devt, char *buf)
  225. {
  226. if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
  227. char tbuf[BDEVT_SIZE];
  228. snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
  229. snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
  230. } else
  231. snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
  232. return buf;
  233. }
  234. /*
  235. * print a full list of all partitions - intended for places where the root
  236. * filesystem can't be mounted and thus to give the victim some idea of what
  237. * went wrong
  238. */
  239. void __init printk_all_partitions(void)
  240. {
  241. struct class_dev_iter iter;
  242. struct device *dev;
  243. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  244. while ((dev = class_dev_iter_next(&iter))) {
  245. struct gendisk *disk = dev_to_disk(dev);
  246. struct block_device *part;
  247. char devt_buf[BDEVT_SIZE];
  248. unsigned long idx;
  249. /*
  250. * Don't show empty devices or things that have been
  251. * suppressed
  252. */
  253. if (get_capacity(disk) == 0 || (disk->flags & GENHD_FL_HIDDEN))
  254. continue;
  255. /*
  256. * Note, unlike /proc/partitions, I am showing the numbers in
  257. * hex - the same format as the root= option takes.
  258. */
  259. rcu_read_lock();
  260. xa_for_each(&disk->part_tbl, idx, part) {
  261. if (!bdev_nr_sectors(part))
  262. continue;
  263. printk("%s%s %10llu %pg %s",
  264. bdev_is_partition(part) ? " " : "",
  265. bdevt_str(part->bd_dev, devt_buf),
  266. bdev_nr_sectors(part) >> 1, part,
  267. part->bd_meta_info ?
  268. part->bd_meta_info->uuid : "");
  269. if (bdev_is_partition(part))
  270. printk("\n");
  271. else if (dev->parent && dev->parent->driver)
  272. printk(" driver: %s\n",
  273. dev->parent->driver->name);
  274. else
  275. printk(" (driver?)\n");
  276. }
  277. rcu_read_unlock();
  278. }
  279. class_dev_iter_exit(&iter);
  280. }