Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 13 | #include <bootmeth.h> |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 14 | #include <bootstd.h> |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 15 | #include <fs.h> |
| 16 | #include <log.h> |
| 17 | #include <malloc.h> |
| 18 | #include <part.h> |
| 19 | #include <sort.h> |
| 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/lists.h> |
| 22 | #include <dm/uclass-internal.h> |
| 23 | |
| 24 | enum { |
| 25 | /* |
| 26 | * Set some sort of limit on the number of partitions a bootdev can |
| 27 | * have. Note that for disks this limits the partitions numbers that |
| 28 | * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV |
| 29 | */ |
| 30 | MAX_PART_PER_BOOTDEV = 30, |
| 31 | |
| 32 | /* Maximum supported length of the "boot_targets" env string */ |
| 33 | BOOT_TARGETS_MAX_LEN = 100, |
| 34 | }; |
| 35 | |
| 36 | int bootdev_add_bootflow(struct bootflow *bflow) |
| 37 | { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 38 | struct bootstd_priv *std; |
| 39 | struct bootflow *new; |
| 40 | int ret; |
| 41 | |
| 42 | assert(bflow->dev); |
| 43 | ret = bootstd_get_priv(&std); |
| 44 | if (ret) |
| 45 | return ret; |
| 46 | |
| 47 | new = malloc(sizeof(*bflow)); |
| 48 | if (!new) |
| 49 | return log_msg_ret("bflow", -ENOMEM); |
| 50 | memcpy(new, bflow, sizeof(*bflow)); |
| 51 | |
| 52 | list_add_tail(&new->glob_node, &std->glob_head); |
Simon Glass | dee4d64 | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 53 | if (bflow->dev) { |
| 54 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 55 | |
| 56 | list_add_tail(&new->bm_node, &ucp->bootflow_head); |
| 57 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) |
| 63 | { |
| 64 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 65 | |
| 66 | if (list_empty(&ucp->bootflow_head)) |
| 67 | return -ENOENT; |
| 68 | |
| 69 | *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 70 | bm_node); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int bootdev_next_bootflow(struct bootflow **bflowp) |
| 76 | { |
| 77 | struct bootflow *bflow = *bflowp; |
| 78 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 79 | |
| 80 | *bflowp = NULL; |
| 81 | |
| 82 | if (list_is_last(&bflow->bm_node, &ucp->bootflow_head)) |
| 83 | return -ENOENT; |
| 84 | |
| 85 | *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, |
| 91 | struct udevice **devp) |
| 92 | { |
| 93 | struct udevice *dev; |
| 94 | char dev_name[30]; |
| 95 | char *str; |
| 96 | int ret; |
| 97 | |
| 98 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 99 | str = strdup(dev_name); |
| 100 | if (!str) |
| 101 | return -ENOMEM; |
| 102 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | device_set_name_alloced(dev); |
| 106 | *devp = dev; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, |
| 112 | struct bootflow_iter *iter, struct bootflow *bflow) |
| 113 | { |
| 114 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 115 | struct disk_partition info; |
| 116 | char partstr[20]; |
| 117 | char name[60]; |
| 118 | int ret; |
| 119 | |
| 120 | /* Sanity check */ |
| 121 | if (iter->part >= MAX_PART_PER_BOOTDEV) |
| 122 | return log_msg_ret("max", -ESHUTDOWN); |
| 123 | |
| 124 | bflow->blk = blk; |
| 125 | if (iter->part) |
| 126 | snprintf(partstr, sizeof(partstr), "part_%x", iter->part); |
| 127 | else |
| 128 | strcpy(partstr, "whole"); |
| 129 | snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); |
| 130 | bflow->name = strdup(name); |
| 131 | if (!bflow->name) |
| 132 | return log_msg_ret("name", -ENOMEM); |
| 133 | |
| 134 | bflow->part = iter->part; |
| 135 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 136 | ret = bootmeth_check(bflow->method, iter); |
| 137 | if (ret) |
| 138 | return log_msg_ret("check", ret); |
| 139 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 140 | /* |
| 141 | * partition numbers start at 0 so this cannot succeed, but it can tell |
| 142 | * us whether there is valid media there |
| 143 | */ |
| 144 | ret = part_get_info(desc, iter->part, &info); |
| 145 | if (!iter->part && ret == -ENOENT) |
| 146 | ret = 0; |
| 147 | |
| 148 | /* |
| 149 | * This error indicates the media is not present. Otherwise we just |
| 150 | * blindly scan the next partition. We could be more intelligent here |
| 151 | * and check which partition numbers actually exist. |
| 152 | */ |
| 153 | if (ret == -EOPNOTSUPP) |
| 154 | ret = -ESHUTDOWN; |
| 155 | else |
| 156 | bflow->state = BOOTFLOWST_MEDIA; |
| 157 | if (ret) |
| 158 | return log_msg_ret("part", ret); |
| 159 | |
| 160 | /* |
| 161 | * Currently we don't get the number of partitions, so just |
| 162 | * assume a large number |
| 163 | */ |
| 164 | iter->max_part = MAX_PART_PER_BOOTDEV; |
| 165 | |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame^] | 166 | /* If this is the whole disk, check if we have bootable partitions */ |
| 167 | if (!iter->part) { |
| 168 | iter->first_bootable = part_get_bootable(desc); |
| 169 | log_debug("checking bootable=%d\n", iter->first_bootable); |
| 170 | |
| 171 | /* if there are bootable partitions, scan only those */ |
| 172 | } else if (iter->first_bootable ? !info.bootable : iter->part != 1) { |
| 173 | return log_msg_ret("boot", -EINVAL); |
| 174 | } else { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 175 | ret = fs_set_blk_dev_with_part(desc, bflow->part); |
| 176 | bflow->state = BOOTFLOWST_PART; |
| 177 | |
| 178 | /* Use an #ifdef due to info.sys_ind */ |
| 179 | #ifdef CONFIG_DOS_PARTITION |
| 180 | log_debug("%s: Found partition %x type %x fstype %d\n", |
| 181 | blk->name, bflow->part, info.sys_ind, |
| 182 | ret ? -1 : fs_get_type()); |
| 183 | #endif |
| 184 | if (ret) |
| 185 | return log_msg_ret("fs", ret); |
| 186 | bflow->state = BOOTFLOWST_FS; |
| 187 | } |
| 188 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 189 | ret = bootmeth_read_bootflow(bflow->method, bflow); |
| 190 | if (ret) |
| 191 | return log_msg_ret("method", ret); |
| 192 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void bootdev_list(bool probe) |
| 197 | { |
| 198 | struct udevice *dev; |
| 199 | int ret; |
| 200 | int i; |
| 201 | |
| 202 | printf("Seq Probed Status Uclass Name\n"); |
| 203 | printf("--- ------ ------ -------- ------------------\n"); |
| 204 | if (probe) |
Michal Suchanek | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 205 | ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 206 | else |
| 207 | ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev); |
| 208 | for (i = 0; dev; i++) { |
| 209 | printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev), |
| 210 | device_active(dev) ? '+' : ' ', |
| 211 | ret ? simple_itoa(ret) : "OK", |
| 212 | dev_get_uclass_name(dev_get_parent(dev)), dev->name); |
| 213 | if (probe) |
Michal Suchanek | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 214 | ret = uclass_next_device_check(&dev); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 215 | else |
| 216 | ret = uclass_find_next_device(&dev); |
| 217 | } |
| 218 | printf("--- ------ ------ -------- ------------------\n"); |
| 219 | printf("(%d bootdev%s)\n", i, i != 1 ? "s" : ""); |
| 220 | } |
| 221 | |
| 222 | int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name) |
| 223 | { |
| 224 | struct udevice *bdev; |
| 225 | int ret; |
| 226 | |
| 227 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, |
| 228 | &bdev); |
| 229 | if (ret) { |
| 230 | if (ret != -ENODEV) { |
| 231 | log_debug("Cannot access bootdev device\n"); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | ret = bootdev_bind(parent, drv_name, "bootdev", &bdev); |
| 236 | if (ret) { |
| 237 | log_debug("Cannot create bootdev device\n"); |
| 238 | return ret; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 245 | static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix) |
| 246 | { |
| 247 | int len, slen; |
| 248 | |
| 249 | len = strlen(dev->name); |
| 250 | slen = strlen(suffix); |
| 251 | if (len > slen && !strcmp(suffix, dev->name + len - slen)) |
| 252 | return len - slen; |
| 253 | |
| 254 | return len; |
| 255 | } |
| 256 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 257 | int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name) |
| 258 | { |
| 259 | struct udevice *parent, *dev; |
| 260 | char dev_name[50]; |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 261 | int ret, len; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 262 | |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 263 | len = bootdev_get_suffix_start(blk, ".blk"); |
| 264 | snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, |
| 265 | "bootdev"); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 266 | |
| 267 | parent = dev_get_parent(blk); |
| 268 | ret = device_find_child_by_name(parent, dev_name, &dev); |
| 269 | if (ret) { |
| 270 | char *str; |
| 271 | |
| 272 | if (ret != -ENODEV) { |
| 273 | log_debug("Cannot access bootdev device\n"); |
| 274 | return ret; |
| 275 | } |
| 276 | str = strdup(dev_name); |
| 277 | if (!str) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 281 | if (ret) { |
| 282 | log_debug("Cannot create bootdev device\n"); |
| 283 | return ret; |
| 284 | } |
| 285 | device_set_name_alloced(dev); |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp) |
| 292 | { |
| 293 | struct udevice *parent = dev_get_parent(dev); |
| 294 | struct udevice *blk; |
| 295 | int ret, len; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 296 | |
| 297 | if (device_get_uclass_id(dev) != UCLASS_BOOTDEV) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 301 | len = bootdev_get_suffix_start(dev, ".bootdev"); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 302 | ret = device_find_child_by_namelen(parent, dev->name, len, &blk); |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 303 | if (ret) { |
| 304 | char dev_name[50]; |
| 305 | |
| 306 | snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len, |
| 307 | dev->name); |
| 308 | ret = device_find_child_by_name(parent, dev_name, &blk); |
| 309 | if (ret) |
| 310 | return log_msg_ret("find", ret); |
| 311 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 312 | *blkp = blk; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp) |
| 318 | { |
| 319 | struct udevice *parent = dev_get_parent(blk); |
| 320 | struct udevice *bootdev; |
| 321 | char dev_name[50]; |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 322 | int ret, len; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 323 | |
| 324 | if (device_get_uclass_id(blk) != UCLASS_BLK) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 328 | len = bootdev_get_suffix_start(blk, ".blk"); |
| 329 | snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, |
| 330 | "bootdev"); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 331 | ret = device_find_child_by_name(parent, dev_name, &bootdev); |
| 332 | if (ret) |
| 333 | return log_msg_ret("find", ret); |
| 334 | *bootdevp = bootdev; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | int bootdev_unbind_dev(struct udevice *parent) |
| 340 | { |
| 341 | struct udevice *dev; |
| 342 | int ret; |
| 343 | |
| 344 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev); |
| 345 | if (!ret) { |
| 346 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 347 | if (ret) |
| 348 | return log_msg_ret("rem", ret); |
| 349 | ret = device_unbind(dev); |
| 350 | if (ret) |
| 351 | return log_msg_ret("unb", ret); |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * bootdev_find_by_label() - Convert a label string to a bootdev device |
| 359 | * |
| 360 | * Looks up a label name to find the associated bootdev. For example, if the |
| 361 | * label name is "mmc2", this will find a bootdev for an mmc device whose |
| 362 | * sequence number is 2. |
| 363 | * |
| 364 | * @label: Label string to convert, e.g. "mmc2" |
| 365 | * @devp: Returns bootdev device corresponding to that boot label |
| 366 | * Return: 0 if OK, -EINVAL if the label name (e.g. "mmc") does not refer to a |
| 367 | * uclass, -ENOENT if no bootdev for that media has the sequence number |
| 368 | * (e.g. 2) |
| 369 | */ |
| 370 | int bootdev_find_by_label(const char *label, struct udevice **devp) |
| 371 | { |
| 372 | struct udevice *media; |
| 373 | struct uclass *uc; |
| 374 | enum uclass_id id; |
| 375 | const char *end; |
| 376 | int seq; |
| 377 | |
| 378 | seq = trailing_strtoln_end(label, NULL, &end); |
| 379 | id = uclass_get_by_namelen(label, end - label); |
| 380 | log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id, |
| 381 | uclass_get_name(id)); |
| 382 | if (id == UCLASS_INVALID) { |
| 383 | log_warning("Unknown uclass '%s' in label\n", label); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | if (id == UCLASS_USB) |
| 387 | id = UCLASS_MASS_STORAGE; |
| 388 | |
| 389 | /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ |
| 390 | uclass_id_foreach_dev(id, media, uc) { |
| 391 | struct udevice *bdev, *blk; |
| 392 | int ret; |
| 393 | |
| 394 | /* if there is no seq, match anything */ |
| 395 | if (seq != -1 && dev_seq(media) != seq) { |
| 396 | log_debug("- skip, media seq=%d\n", dev_seq(media)); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, |
| 401 | &bdev); |
| 402 | if (ret) { |
| 403 | log_debug("- looking via blk, seq=%d, id=%d\n", seq, |
| 404 | id); |
| 405 | ret = blk_find_device(id, seq, &blk); |
| 406 | if (!ret) { |
| 407 | log_debug("- get from blk %s\n", blk->name); |
| 408 | ret = bootdev_get_from_blk(blk, &bdev); |
| 409 | } |
| 410 | } |
| 411 | if (!ret) { |
| 412 | log_debug("- found %s\n", bdev->name); |
| 413 | *devp = bdev; |
| 414 | return 0; |
| 415 | } |
| 416 | log_debug("- no device in %s\n", media->name); |
| 417 | } |
| 418 | log_warning("Unknown seq %d for label '%s'\n", seq, label); |
| 419 | |
| 420 | return -ENOENT; |
| 421 | } |
| 422 | |
| 423 | int bootdev_find_by_any(const char *name, struct udevice **devp) |
| 424 | { |
| 425 | struct udevice *dev; |
| 426 | int ret, seq; |
| 427 | char *endp; |
| 428 | |
| 429 | seq = simple_strtol(name, &endp, 16); |
| 430 | |
| 431 | /* Select by name, label or number */ |
| 432 | if (*endp) { |
| 433 | ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); |
| 434 | if (ret == -ENODEV) { |
| 435 | ret = bootdev_find_by_label(name, &dev); |
| 436 | if (ret) { |
| 437 | printf("Cannot find bootdev '%s' (err=%d)\n", |
| 438 | name, ret); |
| 439 | return ret; |
| 440 | } |
| 441 | ret = device_probe(dev); |
| 442 | } |
| 443 | if (ret) { |
| 444 | printf("Cannot probe bootdev '%s' (err=%d)\n", name, |
| 445 | ret); |
| 446 | return ret; |
| 447 | } |
| 448 | } else { |
| 449 | ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); |
| 450 | } |
| 451 | if (ret) { |
| 452 | printf("Cannot find '%s' (err=%d)\n", name, ret); |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | *devp = dev; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Simon Glass | d6e39d1 | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 461 | static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 462 | struct bootflow *bflow) |
| 463 | { |
| 464 | struct udevice *blk; |
| 465 | int ret; |
| 466 | |
| 467 | ret = bootdev_get_sibling_blk(dev, &blk); |
| 468 | /* |
| 469 | * If there is no media, indicate that no more partitions should be |
| 470 | * checked |
| 471 | */ |
| 472 | if (ret == -EOPNOTSUPP) |
| 473 | ret = -ESHUTDOWN; |
| 474 | if (ret) |
| 475 | return log_msg_ret("blk", ret); |
| 476 | assert(blk); |
| 477 | ret = bootdev_find_in_blk(dev, blk, iter, bflow); |
| 478 | if (ret) |
| 479 | return log_msg_ret("find", ret); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 484 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 485 | struct bootflow *bflow) |
| 486 | { |
| 487 | const struct bootdev_ops *ops = bootdev_get_ops(dev); |
| 488 | |
Simon Glass | 00501fc | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 489 | bootflow_init(bflow, dev, iter->method); |
Simon Glass | d6e39d1 | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 490 | if (!ops->get_bootflow) |
| 491 | return default_get_bootflow(dev, iter, bflow); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 492 | |
| 493 | return ops->get_bootflow(dev, iter, bflow); |
| 494 | } |
| 495 | |
| 496 | void bootdev_clear_bootflows(struct udevice *dev) |
| 497 | { |
| 498 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 499 | |
| 500 | while (!list_empty(&ucp->bootflow_head)) { |
| 501 | struct bootflow *bflow; |
| 502 | |
| 503 | bflow = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 504 | bm_node); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 505 | bootflow_remove(bflow); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * h_cmp_bootdev() - Compare two bootdevs to find out which should go first |
| 511 | * |
| 512 | * @v1: struct udevice * of first bootdev device |
| 513 | * @v2: struct udevice * of second bootdev device |
| 514 | * Return: sort order (<0 if dev1 < dev2, ==0 if equal, >0 if dev1 > dev2) |
| 515 | */ |
| 516 | static int h_cmp_bootdev(const void *v1, const void *v2) |
| 517 | { |
| 518 | const struct udevice *dev1 = *(struct udevice **)v1; |
| 519 | const struct udevice *dev2 = *(struct udevice **)v2; |
| 520 | const struct bootdev_uc_plat *ucp1 = dev_get_uclass_plat(dev1); |
| 521 | const struct bootdev_uc_plat *ucp2 = dev_get_uclass_plat(dev2); |
| 522 | int diff; |
| 523 | |
| 524 | /* Use priority first */ |
| 525 | diff = ucp1->prio - ucp2->prio; |
| 526 | if (diff) |
| 527 | return diff; |
| 528 | |
| 529 | /* Fall back to seq for devices of the same priority */ |
| 530 | diff = dev_seq(dev1) - dev_seq(dev2); |
| 531 | |
| 532 | return diff; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * build_order() - Build the ordered list of bootdevs to use |
| 537 | * |
| 538 | * This builds an ordered list of devices by one of three methods: |
| 539 | * - using the boot_targets environment variable, if non-empty |
| 540 | * - using the bootdev-order devicetree property, if present |
| 541 | * - sorted by priority and sequence number |
| 542 | * |
| 543 | * @bootstd: BOOTSTD device to use |
| 544 | * @order: Bootdevs listed in default order |
| 545 | * @max_count: Number of entries in @order |
| 546 | * Return: number of bootdevs found in the ordering, or -E2BIG if the |
| 547 | * boot_targets string is too long, or -EXDEV if the ordering produced 0 results |
| 548 | */ |
| 549 | static int build_order(struct udevice *bootstd, struct udevice **order, |
| 550 | int max_count) |
| 551 | { |
| 552 | const char *overflow_target = NULL; |
| 553 | const char *const *labels; |
| 554 | struct udevice *dev; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 555 | int i, ret, count; |
Simon Glass | a1bcafb | 2023-01-17 10:47:15 -0700 | [diff] [blame] | 556 | bool ok; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 557 | |
Simon Glass | a1bcafb | 2023-01-17 10:47:15 -0700 | [diff] [blame] | 558 | labels = bootstd_get_bootdev_order(bootstd, &ok); |
| 559 | if (!ok) |
| 560 | return log_msg_ret("ord", -ENOMEM); |
| 561 | if (labels) { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 562 | int upto; |
| 563 | |
| 564 | upto = 0; |
| 565 | for (i = 0; labels[i]; i++) { |
| 566 | ret = bootdev_find_by_label(labels[i], &dev); |
| 567 | if (!ret) { |
| 568 | if (upto == max_count) { |
| 569 | overflow_target = labels[i]; |
| 570 | break; |
| 571 | } |
| 572 | order[upto++] = dev; |
| 573 | } |
| 574 | } |
| 575 | count = upto; |
| 576 | } else { |
| 577 | /* sort them into priority order */ |
| 578 | count = max_count; |
| 579 | qsort(order, count, sizeof(struct udevice *), h_cmp_bootdev); |
| 580 | } |
| 581 | |
| 582 | if (overflow_target) { |
| 583 | log_warning("Expected at most %d bootdevs, but overflowed with boot_target '%s'\n", |
| 584 | max_count, overflow_target); |
| 585 | } |
| 586 | |
| 587 | if (!count) |
| 588 | return log_msg_ret("targ", -EXDEV); |
| 589 | |
| 590 | return count; |
| 591 | } |
| 592 | |
| 593 | int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp) |
| 594 | { |
| 595 | struct udevice *bootstd, *dev = *devp, **order; |
Simon Glass | 99c55a8 | 2023-01-17 10:47:16 -0700 | [diff] [blame] | 596 | struct uclass *uc; |
| 597 | int count, upto; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 598 | int ret; |
| 599 | |
| 600 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 601 | if (ret) { |
| 602 | log_err("Missing bootstd device\n"); |
| 603 | return log_msg_ret("std", ret); |
| 604 | } |
| 605 | |
| 606 | /* Handle scanning a single device */ |
| 607 | if (dev) { |
| 608 | iter->flags |= BOOTFLOWF_SINGLE_DEV; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | count = uclass_id_count(UCLASS_BOOTDEV); |
| 613 | if (!count) |
| 614 | return log_msg_ret("count", -ENOENT); |
| 615 | |
| 616 | order = calloc(count, sizeof(struct udevice *)); |
| 617 | if (!order) |
| 618 | return log_msg_ret("order", -ENOMEM); |
| 619 | |
Simon Glass | 99c55a8 | 2023-01-17 10:47:16 -0700 | [diff] [blame] | 620 | /* Get the list of bootdevs */ |
| 621 | uclass_id_foreach_dev(UCLASS_BOOTDEV, dev, uc) |
| 622 | order[upto++] = dev; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 623 | log_debug("Found %d bootdevs\n", count); |
| 624 | if (upto != count) |
| 625 | log_debug("Expected %d bootdevs, found %d using aliases\n", |
| 626 | count, upto); |
| 627 | |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 628 | ret = build_order(bootstd, order, upto); |
| 629 | if (ret < 0) { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 630 | free(order); |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 631 | return log_msg_ret("build", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 632 | } |
| 633 | |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 634 | iter->num_devs = ret; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 635 | iter->dev_order = order; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 636 | iter->cur_dev = 0; |
| 637 | |
| 638 | dev = *order; |
| 639 | ret = device_probe(dev); |
| 640 | if (ret) |
| 641 | return log_msg_ret("probe", ret); |
| 642 | *devp = dev; |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Simon Glass | 1248d2b | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 647 | static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show) |
| 648 | { |
| 649 | const char *name = uclass_get_name(info->uclass); |
| 650 | struct bootstd_priv *std; |
| 651 | int ret; |
| 652 | |
| 653 | ret = bootstd_get_priv(&std); |
| 654 | if (ret) |
| 655 | return log_msg_ret("std", ret); |
| 656 | |
| 657 | if (!(std->hunters_used & BIT(seq))) { |
| 658 | if (show) |
| 659 | printf("Hunting with: %s\n", |
| 660 | uclass_get_name(info->uclass)); |
| 661 | log_debug("Hunting with: %s\n", name); |
| 662 | if (info->hunt) { |
| 663 | ret = info->hunt(info, show); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | } |
| 667 | std->hunters_used |= BIT(seq); |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | int bootdev_hunt(const char *spec, bool show) |
| 674 | { |
| 675 | struct bootdev_hunter *start; |
| 676 | const char *end; |
| 677 | int n_ent, i; |
| 678 | int result; |
| 679 | size_t len; |
| 680 | |
| 681 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 682 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 683 | result = 0; |
| 684 | |
| 685 | len = SIZE_MAX; |
| 686 | if (spec) { |
| 687 | trailing_strtoln_end(spec, NULL, &end); |
| 688 | len = end - spec; |
| 689 | } |
| 690 | |
| 691 | for (i = 0; i < n_ent; i++) { |
| 692 | struct bootdev_hunter *info = start + i; |
| 693 | const char *name = uclass_get_name(info->uclass); |
| 694 | int ret; |
| 695 | |
| 696 | log_debug("looking at %.*s for %s\n", |
| 697 | (int)max(strlen(name), len), spec, name); |
| 698 | if (spec && strncmp(spec, name, max(strlen(name), len))) |
| 699 | continue; |
| 700 | ret = bootdev_hunt_drv(info, i, show); |
| 701 | if (ret) |
| 702 | result = ret; |
| 703 | } |
| 704 | |
| 705 | return result; |
| 706 | } |
| 707 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 708 | void bootdev_list_hunters(struct bootstd_priv *std) |
| 709 | { |
| 710 | struct bootdev_hunter *orig, *start; |
| 711 | int n_ent, i; |
| 712 | |
| 713 | orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 714 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 715 | |
| 716 | /* |
| 717 | * workaround for strange bug in clang-12 which sees all the below data |
| 718 | * as zeroes. Any access of start seems to fix it, such as |
| 719 | * |
| 720 | * printf("%p", start); |
| 721 | * |
| 722 | * Use memcpy() to force the correct behaviour. |
| 723 | */ |
| 724 | memcpy(&start, &orig, sizeof(orig)); |
| 725 | printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter"); |
| 726 | printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------"); |
| 727 | for (i = 0; i < n_ent; i++) { |
| 728 | struct bootdev_hunter *info = start + i; |
| 729 | |
| 730 | printf("%4d %4s %-15s %s\n", info->prio, |
| 731 | std->hunters_used & BIT(i) ? "*" : "", |
| 732 | uclass_get_name(info->uclass), |
| 733 | info->drv ? info->drv->name : "(none)"); |
| 734 | } |
| 735 | |
| 736 | printf("(total hunters: %d)\n", n_ent); |
| 737 | } |
| 738 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 739 | static int bootdev_post_bind(struct udevice *dev) |
| 740 | { |
| 741 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 742 | |
| 743 | INIT_LIST_HEAD(&ucp->bootflow_head); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static int bootdev_pre_unbind(struct udevice *dev) |
| 749 | { |
| 750 | bootdev_clear_bootflows(dev); |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | UCLASS_DRIVER(bootdev) = { |
| 756 | .id = UCLASS_BOOTDEV, |
| 757 | .name = "bootdev", |
| 758 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 759 | .per_device_plat_auto = sizeof(struct bootdev_uc_plat), |
| 760 | .post_bind = bootdev_post_bind, |
| 761 | .pre_unbind = bootdev_pre_unbind, |
| 762 | }; |