Simon Glass | 201417d | 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 | |
Tom Rini | d678a59 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 13 | #include <bootmeth.h> |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 14 | #include <bootstd.h> |
Simon Glass | 201417d | 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 | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 38 | struct bootstd_priv *std; |
| 39 | struct bootflow *new; |
| 40 | int ret; |
| 41 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 42 | ret = bootstd_get_priv(&std); |
| 43 | if (ret) |
| 44 | return ret; |
| 45 | |
| 46 | new = malloc(sizeof(*bflow)); |
| 47 | if (!new) |
| 48 | return log_msg_ret("bflow", -ENOMEM); |
| 49 | memcpy(new, bflow, sizeof(*bflow)); |
| 50 | |
| 51 | list_add_tail(&new->glob_node, &std->glob_head); |
Simon Glass | eccb25c | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 52 | if (bflow->dev) { |
| 53 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 54 | |
| 55 | list_add_tail(&new->bm_node, &ucp->bootflow_head); |
| 56 | } |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) |
| 62 | { |
| 63 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 64 | |
| 65 | if (list_empty(&ucp->bootflow_head)) |
| 66 | return -ENOENT; |
| 67 | |
| 68 | *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 69 | bm_node); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int bootdev_next_bootflow(struct bootflow **bflowp) |
| 75 | { |
| 76 | struct bootflow *bflow = *bflowp; |
| 77 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 78 | |
| 79 | *bflowp = NULL; |
| 80 | |
| 81 | if (list_is_last(&bflow->bm_node, &ucp->bootflow_head)) |
| 82 | return -ENOENT; |
| 83 | |
| 84 | *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, |
| 90 | struct udevice **devp) |
| 91 | { |
| 92 | struct udevice *dev; |
| 93 | char dev_name[30]; |
| 94 | char *str; |
| 95 | int ret; |
| 96 | |
| 97 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 98 | str = strdup(dev_name); |
| 99 | if (!str) |
| 100 | return -ENOMEM; |
| 101 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | device_set_name_alloced(dev); |
| 105 | *devp = dev; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, |
| 111 | struct bootflow_iter *iter, struct bootflow *bflow) |
| 112 | { |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 113 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method); |
| 114 | bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 115 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 116 | struct disk_partition info; |
| 117 | char partstr[20]; |
| 118 | char name[60]; |
| 119 | int ret; |
| 120 | |
| 121 | /* Sanity check */ |
| 122 | if (iter->part >= MAX_PART_PER_BOOTDEV) |
| 123 | return log_msg_ret("max", -ESHUTDOWN); |
| 124 | |
| 125 | bflow->blk = blk; |
| 126 | if (iter->part) |
| 127 | snprintf(partstr, sizeof(partstr), "part_%x", iter->part); |
| 128 | else |
| 129 | strcpy(partstr, "whole"); |
| 130 | snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); |
| 131 | bflow->name = strdup(name); |
| 132 | if (!bflow->name) |
| 133 | return log_msg_ret("name", -ENOMEM); |
| 134 | |
| 135 | bflow->part = iter->part; |
| 136 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 137 | ret = bootmeth_check(bflow->method, iter); |
| 138 | if (ret) |
| 139 | return log_msg_ret("check", ret); |
| 140 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 141 | /* |
| 142 | * partition numbers start at 0 so this cannot succeed, but it can tell |
| 143 | * us whether there is valid media there |
| 144 | */ |
| 145 | ret = part_get_info(desc, iter->part, &info); |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 146 | log_debug("part_get_info() returned %d\n", ret); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 147 | if (!iter->part && ret == -ENOENT) |
| 148 | ret = 0; |
| 149 | |
| 150 | /* |
| 151 | * This error indicates the media is not present. Otherwise we just |
| 152 | * blindly scan the next partition. We could be more intelligent here |
| 153 | * and check which partition numbers actually exist. |
| 154 | */ |
| 155 | if (ret == -EOPNOTSUPP) |
| 156 | ret = -ESHUTDOWN; |
| 157 | else |
| 158 | bflow->state = BOOTFLOWST_MEDIA; |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 159 | if (ret && !allow_any_part) { |
Simon Glass | 76afc84 | 2023-04-28 13:18:09 -0600 | [diff] [blame] | 160 | /* allow partition 1 to be missing */ |
| 161 | if (iter->part == 1) { |
| 162 | iter->max_part = 3; |
| 163 | ret = -ENOENT; |
| 164 | } |
| 165 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 166 | return log_msg_ret("part", ret); |
Simon Glass | 76afc84 | 2023-04-28 13:18:09 -0600 | [diff] [blame] | 167 | } |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Currently we don't get the number of partitions, so just |
| 171 | * assume a large number |
| 172 | */ |
| 173 | iter->max_part = MAX_PART_PER_BOOTDEV; |
| 174 | |
Nam Cao | 1132471 | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 175 | if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) { |
| 176 | /* a particular partition was specified, scan it without checking */ |
| 177 | } else if (!iter->part) { |
| 178 | /* This is the whole disk, check if we have bootable partitions */ |
Simon Glass | f0e358f | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 179 | iter->first_bootable = part_get_bootable(desc); |
| 180 | log_debug("checking bootable=%d\n", iter->first_bootable); |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 181 | } else if (allow_any_part) { |
| 182 | /* |
| 183 | * allow any partition to be scanned, by skipping any checks |
| 184 | * for filesystems or partition contents on this disk |
| 185 | */ |
Simon Glass | f0e358f | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 186 | |
| 187 | /* if there are bootable partitions, scan only those */ |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 188 | } else if (iter->first_bootable >= 0 && |
| 189 | (iter->first_bootable ? !info.bootable : iter->part != 1)) { |
Simon Glass | f0e358f | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 190 | return log_msg_ret("boot", -EINVAL); |
| 191 | } else { |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 192 | ret = fs_set_blk_dev_with_part(desc, bflow->part); |
| 193 | bflow->state = BOOTFLOWST_PART; |
Simon Glass | 1aabe4e | 2023-04-24 13:49:49 +1200 | [diff] [blame] | 194 | if (ret) |
| 195 | return log_msg_ret("fs", ret); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 196 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 197 | log_debug("%s: Found partition %x type %x fstype %d\n", |
Simon Glass | b2b7e6c | 2023-08-24 13:55:33 -0600 | [diff] [blame] | 198 | blk->name, bflow->part, |
| 199 | IS_ENABLED(CONFIG_DOS_PARTITION) ? |
| 200 | disk_partition_sys_ind(&info) : 0, |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 201 | ret ? -1 : fs_get_type()); |
Simon Glass | 1aabe4e | 2023-04-24 13:49:49 +1200 | [diff] [blame] | 202 | bflow->blk = blk; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 203 | bflow->state = BOOTFLOWST_FS; |
| 204 | } |
| 205 | |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 206 | log_debug("method %s\n", bflow->method->name); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 207 | ret = bootmeth_read_bootflow(bflow->method, bflow); |
| 208 | if (ret) |
| 209 | return log_msg_ret("method", ret); |
| 210 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void bootdev_list(bool probe) |
| 215 | { |
| 216 | struct udevice *dev; |
| 217 | int ret; |
| 218 | int i; |
| 219 | |
| 220 | printf("Seq Probed Status Uclass Name\n"); |
| 221 | printf("--- ------ ------ -------- ------------------\n"); |
| 222 | if (probe) |
Michal Suchanek | 28a22cd | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 223 | ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 224 | else |
| 225 | ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev); |
| 226 | for (i = 0; dev; i++) { |
| 227 | printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev), |
| 228 | device_active(dev) ? '+' : ' ', |
Heinrich Schuchardt | ca9d926 | 2023-07-30 16:29:25 +0200 | [diff] [blame] | 229 | ret ? simple_itoa(-ret) : "OK", |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 230 | dev_get_uclass_name(dev_get_parent(dev)), dev->name); |
| 231 | if (probe) |
Michal Suchanek | 28a22cd | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 232 | ret = uclass_next_device_check(&dev); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 233 | else |
| 234 | ret = uclass_find_next_device(&dev); |
| 235 | } |
| 236 | printf("--- ------ ------ -------- ------------------\n"); |
| 237 | printf("(%d bootdev%s)\n", i, i != 1 ? "s" : ""); |
| 238 | } |
| 239 | |
| 240 | int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name) |
| 241 | { |
| 242 | struct udevice *bdev; |
| 243 | int ret; |
| 244 | |
| 245 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, |
| 246 | &bdev); |
| 247 | if (ret) { |
| 248 | if (ret != -ENODEV) { |
| 249 | log_debug("Cannot access bootdev device\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | ret = bootdev_bind(parent, drv_name, "bootdev", &bdev); |
| 254 | if (ret) { |
| 255 | log_debug("Cannot create bootdev device\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 263 | static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix) |
| 264 | { |
| 265 | int len, slen; |
| 266 | |
| 267 | len = strlen(dev->name); |
| 268 | slen = strlen(suffix); |
| 269 | if (len > slen && !strcmp(suffix, dev->name + len - slen)) |
| 270 | return len - slen; |
| 271 | |
| 272 | return len; |
| 273 | } |
| 274 | |
Simon Glass | d7d7857 | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 275 | int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name) |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 276 | { |
| 277 | struct udevice *parent, *dev; |
| 278 | char dev_name[50]; |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 279 | int ret, len; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 280 | |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 281 | len = bootdev_get_suffix_start(blk, ".blk"); |
| 282 | snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, |
| 283 | "bootdev"); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 284 | |
| 285 | parent = dev_get_parent(blk); |
| 286 | ret = device_find_child_by_name(parent, dev_name, &dev); |
| 287 | if (ret) { |
| 288 | char *str; |
| 289 | |
| 290 | if (ret != -ENODEV) { |
| 291 | log_debug("Cannot access bootdev device\n"); |
| 292 | return ret; |
| 293 | } |
| 294 | str = strdup(dev_name); |
| 295 | if (!str) |
| 296 | return -ENOMEM; |
| 297 | |
| 298 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 299 | if (ret) { |
| 300 | log_debug("Cannot create bootdev device\n"); |
| 301 | return ret; |
| 302 | } |
| 303 | device_set_name_alloced(dev); |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp) |
| 310 | { |
| 311 | struct udevice *parent = dev_get_parent(dev); |
| 312 | struct udevice *blk; |
| 313 | int ret, len; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 314 | |
| 315 | if (device_get_uclass_id(dev) != UCLASS_BOOTDEV) |
| 316 | return -EINVAL; |
| 317 | |
Simon Glass | d7d7857 | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 318 | /* |
| 319 | * This should always work if bootdev_setup_for_sibling_blk() was used |
| 320 | */ |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 321 | len = bootdev_get_suffix_start(dev, ".bootdev"); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 322 | ret = device_find_child_by_namelen(parent, dev->name, len, &blk); |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 323 | if (ret) { |
| 324 | char dev_name[50]; |
| 325 | |
| 326 | snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len, |
| 327 | dev->name); |
| 328 | ret = device_find_child_by_name(parent, dev_name, &blk); |
| 329 | if (ret) |
| 330 | return log_msg_ret("find", ret); |
| 331 | } |
Simon Glass | 965020c | 2023-01-28 15:00:19 -0700 | [diff] [blame] | 332 | ret = device_probe(blk); |
| 333 | if (ret) |
| 334 | return log_msg_ret("act", ret); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 335 | *blkp = blk; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp) |
| 341 | { |
| 342 | struct udevice *parent = dev_get_parent(blk); |
| 343 | struct udevice *bootdev; |
| 344 | char dev_name[50]; |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 345 | int ret, len; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 346 | |
| 347 | if (device_get_uclass_id(blk) != UCLASS_BLK) |
| 348 | return -EINVAL; |
| 349 | |
Simon Glass | d7d7857 | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 350 | /* This should always work if bootdev_setup_for_sibling_blk() was used */ |
Simon Glass | 3a2cb96e5 | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 351 | len = bootdev_get_suffix_start(blk, ".blk"); |
| 352 | snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, |
| 353 | "bootdev"); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 354 | ret = device_find_child_by_name(parent, dev_name, &bootdev); |
| 355 | if (ret) |
| 356 | return log_msg_ret("find", ret); |
| 357 | *bootdevp = bootdev; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int bootdev_unbind_dev(struct udevice *parent) |
| 363 | { |
| 364 | struct udevice *dev; |
| 365 | int ret; |
| 366 | |
| 367 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev); |
| 368 | if (!ret) { |
| 369 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 370 | if (ret) |
| 371 | return log_msg_ret("rem", ret); |
| 372 | ret = device_unbind(dev); |
| 373 | if (ret) |
| 374 | return log_msg_ret("unb", ret); |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /** |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 381 | * label_to_uclass() - Convert a label to a uclass and sequence number |
| 382 | * |
| 383 | * @label: Label to look up (e.g. "mmc1" or "mmc0") |
| 384 | * @seqp: Returns the sequence number, or -1 if none |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 385 | * @method_flagsp: If non-NULL, returns any flags implied by the label |
| 386 | * (enum bootflow_meth_flags_t), 0 if none |
Simon Glass | 1736b4a | 2023-04-24 13:49:47 +1200 | [diff] [blame] | 387 | * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not |
| 388 | * known, other -ve error code on other error |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 389 | */ |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 390 | static int label_to_uclass(const char *label, int *seqp, int *method_flagsp) |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 391 | { |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 392 | int seq, len, method_flags; |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 393 | enum uclass_id id; |
| 394 | const char *end; |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 395 | |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 396 | method_flags = 0; |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 397 | seq = trailing_strtoln_end(label, NULL, &end); |
| 398 | len = end - label; |
| 399 | if (!len) |
| 400 | return -EINVAL; |
| 401 | id = uclass_get_by_namelen(label, len); |
| 402 | log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id, |
| 403 | uclass_get_name(id)); |
| 404 | if (id == UCLASS_INVALID) { |
Simon Glass | 0c1f4a9 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 405 | /* try some special cases */ |
| 406 | if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) && |
| 407 | !strncmp("spi", label, len)) { |
| 408 | id = UCLASS_SPI_FLASH; |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 409 | } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) && |
| 410 | !strncmp("pxe", label, len)) { |
| 411 | id = UCLASS_ETH; |
| 412 | method_flags |= BOOTFLOW_METHF_PXE_ONLY; |
| 413 | } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) && |
| 414 | !strncmp("dhcp", label, len)) { |
| 415 | id = UCLASS_ETH; |
| 416 | method_flags |= BOOTFLOW_METHF_DHCP_ONLY; |
Simon Glass | 0c1f4a9 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 417 | } else { |
Simon Glass | 1736b4a | 2023-04-24 13:49:47 +1200 | [diff] [blame] | 418 | return -EPFNOSUPPORT; |
Simon Glass | 0c1f4a9 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 419 | } |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 420 | } |
| 421 | if (id == UCLASS_USB) |
| 422 | id = UCLASS_MASS_STORAGE; |
| 423 | *seqp = seq; |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 424 | if (method_flagsp) |
| 425 | *method_flagsp = method_flags; |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 426 | |
| 427 | return id; |
| 428 | } |
| 429 | |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 430 | int bootdev_find_by_label(const char *label, struct udevice **devp, |
| 431 | int *method_flagsp) |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 432 | { |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 433 | int seq, ret, method_flags = 0; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 434 | struct udevice *media; |
| 435 | struct uclass *uc; |
| 436 | enum uclass_id id; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 437 | |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 438 | ret = label_to_uclass(label, &seq, &method_flags); |
Simon Glass | 74ebfb6 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 439 | if (ret < 0) |
| 440 | return log_msg_ret("uc", ret); |
| 441 | id = ret; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 442 | |
| 443 | /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ |
| 444 | uclass_id_foreach_dev(id, media, uc) { |
| 445 | struct udevice *bdev, *blk; |
| 446 | int ret; |
| 447 | |
| 448 | /* if there is no seq, match anything */ |
| 449 | if (seq != -1 && dev_seq(media) != seq) { |
| 450 | log_debug("- skip, media seq=%d\n", dev_seq(media)); |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, |
| 455 | &bdev); |
| 456 | if (ret) { |
| 457 | log_debug("- looking via blk, seq=%d, id=%d\n", seq, |
| 458 | id); |
| 459 | ret = blk_find_device(id, seq, &blk); |
| 460 | if (!ret) { |
| 461 | log_debug("- get from blk %s\n", blk->name); |
| 462 | ret = bootdev_get_from_blk(blk, &bdev); |
| 463 | } |
| 464 | } |
| 465 | if (!ret) { |
| 466 | log_debug("- found %s\n", bdev->name); |
| 467 | *devp = bdev; |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * if no sequence number was provided, we must scan all |
| 471 | * bootdevs for this media uclass |
| 472 | */ |
Simon Glass | e824d0d | 2023-09-23 14:50:15 -0600 | [diff] [blame] | 473 | if (seq == -1) |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 474 | method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS; |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 475 | if (method_flagsp) |
| 476 | *method_flagsp = method_flags; |
Simon Glass | e824d0d | 2023-09-23 14:50:15 -0600 | [diff] [blame] | 477 | log_debug("method flags %x\n", method_flags); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | log_debug("- no device in %s\n", media->name); |
| 481 | } |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 482 | |
| 483 | return -ENOENT; |
| 484 | } |
| 485 | |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 486 | int bootdev_find_by_any(const char *name, struct udevice **devp, |
| 487 | int *method_flagsp) |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 488 | { |
| 489 | struct udevice *dev; |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 490 | int method_flags = 0; |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 491 | int ret = -ENODEV, seq; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 492 | char *endp; |
| 493 | |
| 494 | seq = simple_strtol(name, &endp, 16); |
| 495 | |
| 496 | /* Select by name, label or number */ |
| 497 | if (*endp) { |
| 498 | ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); |
| 499 | if (ret == -ENODEV) { |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 500 | ret = bootdev_find_by_label(name, &dev, &method_flags); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 501 | if (ret) { |
| 502 | printf("Cannot find bootdev '%s' (err=%d)\n", |
| 503 | name, ret); |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 504 | return log_msg_ret("lab", ret); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 505 | } |
| 506 | ret = device_probe(dev); |
| 507 | } |
| 508 | if (ret) { |
| 509 | printf("Cannot probe bootdev '%s' (err=%d)\n", name, |
| 510 | ret); |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 511 | return log_msg_ret("pro", ret); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 512 | } |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 513 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 514 | ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 515 | method_flags |= BOOTFLOW_METHF_SINGLE_DEV; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 516 | } |
| 517 | if (ret) { |
| 518 | printf("Cannot find '%s' (err=%d)\n", name, ret); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | *devp = dev; |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 523 | if (method_flagsp) |
| 524 | *method_flagsp = method_flags; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Simon Glass | 66e3dce | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 529 | int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp, |
| 530 | int *method_flagsp) |
| 531 | { |
| 532 | int ret; |
| 533 | |
| 534 | ret = bootdev_hunt(label, false); |
| 535 | if (ret) |
| 536 | return log_msg_ret("scn", ret); |
| 537 | ret = bootdev_find_by_label(label, devp, method_flagsp); |
| 538 | if (ret) |
| 539 | return log_msg_ret("fnd", ret); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Simon Glass | b85fc8d | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 544 | static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 545 | struct bootflow *bflow) |
| 546 | { |
| 547 | struct udevice *blk; |
| 548 | int ret; |
| 549 | |
| 550 | ret = bootdev_get_sibling_blk(dev, &blk); |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 551 | log_debug("sibling_blk ret=%d, blk=%s\n", ret, |
| 552 | ret ? "(none)" : blk->name); |
Simon Glass | b85fc8d | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 553 | /* |
| 554 | * If there is no media, indicate that no more partitions should be |
| 555 | * checked |
| 556 | */ |
| 557 | if (ret == -EOPNOTSUPP) |
| 558 | ret = -ESHUTDOWN; |
| 559 | if (ret) |
| 560 | return log_msg_ret("blk", ret); |
| 561 | assert(blk); |
| 562 | ret = bootdev_find_in_blk(dev, blk, iter, bflow); |
| 563 | if (ret) |
| 564 | return log_msg_ret("find", ret); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 569 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 570 | struct bootflow *bflow) |
| 571 | { |
| 572 | const struct bootdev_ops *ops = bootdev_get_ops(dev); |
| 573 | |
Simon Glass | 831405f | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 574 | log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part, |
| 575 | ops->get_bootflow); |
Simon Glass | b190deb | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 576 | bootflow_init(bflow, dev, iter->method); |
Simon Glass | b85fc8d | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 577 | if (!ops->get_bootflow) |
| 578 | return default_get_bootflow(dev, iter, bflow); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 579 | |
| 580 | return ops->get_bootflow(dev, iter, bflow); |
| 581 | } |
| 582 | |
| 583 | void bootdev_clear_bootflows(struct udevice *dev) |
| 584 | { |
| 585 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 586 | |
| 587 | while (!list_empty(&ucp->bootflow_head)) { |
| 588 | struct bootflow *bflow; |
| 589 | |
| 590 | bflow = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 591 | bm_node); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 592 | bootflow_remove(bflow); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Simon Glass | e4b6948 | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 596 | int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp, |
| 597 | int *method_flagsp) |
| 598 | { |
| 599 | struct udevice *dev; |
| 600 | |
| 601 | log_debug("next\n"); |
| 602 | for (dev = NULL; !dev && iter->labels[++iter->cur_label];) { |
Simon Glass | 1736b4a | 2023-04-24 13:49:47 +1200 | [diff] [blame] | 603 | const char *label = iter->labels[iter->cur_label]; |
| 604 | int ret; |
| 605 | |
| 606 | log_debug("Scanning: %s\n", label); |
| 607 | ret = bootdev_hunt_and_find_by_label(label, &dev, |
| 608 | method_flagsp); |
| 609 | if (iter->flags & BOOTFLOWIF_SHOW) { |
| 610 | if (ret == -EPFNOSUPPORT) { |
| 611 | log_warning("Unknown uclass '%s' in label\n", |
| 612 | label); |
| 613 | } else if (ret == -ENOENT) { |
| 614 | /* |
| 615 | * looking for, e.g. 'scsi0' should find |
| 616 | * something if SCSI is present |
| 617 | */ |
| 618 | if (!trailing_strtol(label)) { |
| 619 | log_warning("No bootdevs for '%s'\n", |
| 620 | label); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Simon Glass | e4b6948 | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | if (!dev) |
| 628 | return log_msg_ret("fin", -ENODEV); |
| 629 | *devp = dev; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 634 | int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp) |
| 635 | { |
Caleb Connolly | 9d92c41 | 2024-01-04 16:03:35 +0000 | [diff] [blame] | 636 | struct udevice *dev = *devp, *last_dev = NULL; |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 637 | bool found; |
| 638 | int ret; |
| 639 | |
| 640 | /* find the next device with this priority */ |
| 641 | *devp = NULL; |
| 642 | log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev, |
| 643 | dev ? dev->name : "none"); |
| 644 | do { |
| 645 | /* |
| 646 | * Don't probe devices here since they may not be of the |
| 647 | * required priority |
| 648 | */ |
| 649 | if (!dev) |
| 650 | uclass_find_first_device(UCLASS_BOOTDEV, &dev); |
| 651 | else |
| 652 | uclass_find_next_device(&dev); |
| 653 | found = false; |
| 654 | |
| 655 | /* scan for the next device with the correct priority */ |
| 656 | while (dev) { |
| 657 | struct bootdev_uc_plat *plat; |
| 658 | |
| 659 | plat = dev_get_uclass_plat(dev); |
| 660 | log_debug("- %s: %d, want %d\n", dev->name, plat->prio, |
| 661 | iter->cur_prio); |
| 662 | if (plat->prio == iter->cur_prio) |
| 663 | break; |
| 664 | uclass_find_next_device(&dev); |
| 665 | } |
| 666 | |
| 667 | /* none found for this priority, so move to the next */ |
| 668 | if (!dev) { |
| 669 | log_debug("None found at prio %d, moving to %d\n", |
| 670 | iter->cur_prio, iter->cur_prio + 1); |
| 671 | if (++iter->cur_prio == BOOTDEVP_COUNT) |
| 672 | return log_msg_ret("fin", -ENODEV); |
| 673 | |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 674 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 675 | /* hunt to find new bootdevs */ |
| 676 | ret = bootdev_hunt_prio(iter->cur_prio, |
| 677 | iter->flags & |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 678 | BOOTFLOWIF_SHOW); |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 679 | log_debug("- bootdev_hunt_prio() ret %d\n", |
| 680 | ret); |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 681 | if (ret) |
| 682 | return log_msg_ret("hun", ret); |
| 683 | } |
| 684 | } else { |
| 685 | ret = device_probe(dev); |
Caleb Connolly | 9d92c41 | 2024-01-04 16:03:35 +0000 | [diff] [blame] | 686 | if (!ret) |
| 687 | last_dev = dev; |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 688 | if (ret) { |
Caleb Connolly | 9d92c41 | 2024-01-04 16:03:35 +0000 | [diff] [blame] | 689 | log_warning("Device '%s' failed to probe\n", |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 690 | dev->name); |
Caleb Connolly | 9d92c41 | 2024-01-04 16:03:35 +0000 | [diff] [blame] | 691 | if (last_dev == dev) { |
| 692 | /* |
| 693 | * We have already tried this device |
| 694 | * and it failed to probe. Give up. |
| 695 | */ |
| 696 | return log_msg_ret("probe", ret); |
| 697 | } |
| 698 | last_dev = dev; |
Simon Glass | 43e89a3 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 699 | dev = NULL; |
| 700 | } |
| 701 | } |
| 702 | } while (!dev); |
| 703 | |
| 704 | *devp = dev; |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 709 | int bootdev_setup_iter(struct bootflow_iter *iter, const char *label, |
| 710 | struct udevice **devp, int *method_flagsp) |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 711 | { |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 712 | struct udevice *bootstd, *dev = NULL; |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 713 | bool show = iter->flags & BOOTFLOWIF_SHOW; |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 714 | int method_flags; |
Nam Cao | 1132471 | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 715 | char buf[32]; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 716 | int ret; |
| 717 | |
Nam Cao | 1132471 | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 718 | if (label) { |
| 719 | const char *end = strchr(label, ':'); |
| 720 | |
| 721 | if (end) { |
| 722 | size_t len = (size_t)(end - label); |
| 723 | const char *part = end + 1; |
| 724 | |
| 725 | if (len + 1 > sizeof(buf)) { |
| 726 | log_err("label \"%s\" is way too long\n", label); |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
| 730 | memcpy(buf, label, len); |
| 731 | buf[len] = '\0'; |
| 732 | label = buf; |
| 733 | |
| 734 | unsigned long tmp; |
| 735 | |
| 736 | if (strict_strtoul(part, 0, &tmp)) { |
| 737 | log_err("Invalid partition number: %s\n", part); |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
| 741 | iter->flags |= BOOTFLOWIF_SINGLE_PARTITION; |
| 742 | iter->part = tmp; |
| 743 | } |
| 744 | } |
| 745 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 746 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 747 | if (ret) { |
| 748 | log_err("Missing bootstd device\n"); |
| 749 | return log_msg_ret("std", ret); |
| 750 | } |
| 751 | |
Simon Glass | eacc261 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 752 | /* hunt for any pre-scan devices */ |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 753 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | eacc261 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 754 | ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show); |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 755 | log_debug("- bootdev_hunt_prio() ret %d\n", ret); |
Simon Glass | eacc261 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 756 | if (ret) |
| 757 | return log_msg_ret("pre", ret); |
| 758 | } |
| 759 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 760 | /* Handle scanning a single device */ |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 761 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) { |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 762 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 763 | ret = bootdev_hunt(label, show); |
| 764 | if (ret) |
| 765 | return log_msg_ret("hun", ret); |
| 766 | } |
| 767 | ret = bootdev_find_by_any(label, &dev, &method_flags); |
| 768 | if (ret) |
| 769 | return log_msg_ret("lab", ret); |
| 770 | |
| 771 | log_debug("method_flags: %x\n", method_flags); |
| 772 | if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 773 | iter->flags |= BOOTFLOWIF_SINGLE_UCLASS; |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 774 | else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 775 | iter->flags |= BOOTFLOWIF_SINGLE_DEV; |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 776 | else |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 777 | iter->flags |= BOOTFLOWIF_SINGLE_MEDIA; |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 778 | log_debug("Selected label: %s, flags %x\n", label, iter->flags); |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 779 | } else { |
| 780 | bool ok; |
| 781 | |
| 782 | /* This either returns a non-empty list or NULL */ |
| 783 | iter->labels = bootstd_get_bootdev_order(bootstd, &ok); |
| 784 | if (!ok) |
| 785 | return log_msg_ret("ord", -ENOMEM); |
| 786 | log_debug("setup labels %p\n", iter->labels); |
| 787 | if (iter->labels) { |
| 788 | iter->cur_label = -1; |
| 789 | ret = bootdev_next_label(iter, &dev, &method_flags); |
| 790 | } else { |
| 791 | ret = bootdev_next_prio(iter, &dev); |
| 792 | method_flags = 0; |
| 793 | } |
| 794 | if (!dev) |
| 795 | return log_msg_ret("fin", -ENOENT); |
| 796 | log_debug("Selected bootdev: %s\n", dev->name); |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 797 | } |
| 798 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 799 | ret = device_probe(dev); |
| 800 | if (ret) |
| 801 | return log_msg_ret("probe", ret); |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 802 | if (method_flagsp) |
| 803 | *method_flagsp = method_flags; |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 804 | *devp = dev; |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
Simon Glass | c7b63d5 | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 809 | static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show) |
| 810 | { |
| 811 | const char *name = uclass_get_name(info->uclass); |
| 812 | struct bootstd_priv *std; |
| 813 | int ret; |
| 814 | |
| 815 | ret = bootstd_get_priv(&std); |
| 816 | if (ret) |
| 817 | return log_msg_ret("std", ret); |
| 818 | |
| 819 | if (!(std->hunters_used & BIT(seq))) { |
| 820 | if (show) |
| 821 | printf("Hunting with: %s\n", |
| 822 | uclass_get_name(info->uclass)); |
| 823 | log_debug("Hunting with: %s\n", name); |
| 824 | if (info->hunt) { |
| 825 | ret = info->hunt(info, show); |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 826 | log_debug(" - hunt result %d\n", ret); |
Tony Dinh | ee2ce29 | 2023-11-02 11:51:15 -0700 | [diff] [blame] | 827 | if (ret && ret != -ENOENT) |
Simon Glass | c7b63d5 | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | std->hunters_used |= BIT(seq); |
| 831 | } |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | int bootdev_hunt(const char *spec, bool show) |
| 837 | { |
| 838 | struct bootdev_hunter *start; |
| 839 | const char *end; |
| 840 | int n_ent, i; |
| 841 | int result; |
| 842 | size_t len; |
| 843 | |
| 844 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 845 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 846 | result = 0; |
| 847 | |
| 848 | len = SIZE_MAX; |
| 849 | if (spec) { |
| 850 | trailing_strtoln_end(spec, NULL, &end); |
| 851 | len = end - spec; |
| 852 | } |
| 853 | |
| 854 | for (i = 0; i < n_ent; i++) { |
| 855 | struct bootdev_hunter *info = start + i; |
| 856 | const char *name = uclass_get_name(info->uclass); |
| 857 | int ret; |
| 858 | |
| 859 | log_debug("looking at %.*s for %s\n", |
| 860 | (int)max(strlen(name), len), spec, name); |
Simon Glass | e4b6948 | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 861 | if (spec && strncmp(spec, name, max(strlen(name), len))) { |
| 862 | if (info->uclass != UCLASS_ETH || |
| 863 | (strcmp("dhcp", spec) && strcmp("pxe", spec))) |
| 864 | continue; |
| 865 | } |
Simon Glass | c7b63d5 | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 866 | ret = bootdev_hunt_drv(info, i, show); |
| 867 | if (ret) |
| 868 | result = ret; |
| 869 | } |
| 870 | |
| 871 | return result; |
| 872 | } |
| 873 | |
Simon Glass | 1b1d36e | 2023-09-20 07:29:49 -0600 | [diff] [blame] | 874 | int bootdev_unhunt(enum uclass_id id) |
| 875 | { |
| 876 | struct bootdev_hunter *start; |
| 877 | int n_ent, i; |
| 878 | |
| 879 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 880 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 881 | for (i = 0; i < n_ent; i++) { |
| 882 | struct bootdev_hunter *info = start + i; |
| 883 | |
| 884 | if (info->uclass == id) { |
| 885 | struct bootstd_priv *std; |
| 886 | int ret; |
| 887 | |
| 888 | ret = bootstd_get_priv(&std); |
| 889 | if (ret) |
| 890 | return log_msg_ret("std", ret); |
| 891 | if (!(std->hunters_used & BIT(i))) |
| 892 | return -EALREADY; |
| 893 | std->hunters_used &= ~BIT(i); |
| 894 | return 0; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | return -ENOENT; |
| 899 | } |
| 900 | |
Simon Glass | 79a7d4a | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 901 | int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show) |
| 902 | { |
| 903 | struct bootdev_hunter *start; |
| 904 | int n_ent, i; |
| 905 | int result; |
| 906 | |
| 907 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 908 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 909 | result = 0; |
| 910 | |
| 911 | log_debug("Hunting for priority %d\n", prio); |
| 912 | for (i = 0; i < n_ent; i++) { |
| 913 | struct bootdev_hunter *info = start + i; |
| 914 | int ret; |
| 915 | |
| 916 | if (prio != info->prio) |
| 917 | continue; |
| 918 | ret = bootdev_hunt_drv(info, i, show); |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 919 | log_debug("bootdev_hunt_drv() return %d\n", ret); |
Simon Glass | 79a7d4a | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 920 | if (ret && ret != -ENOENT) |
| 921 | result = ret; |
| 922 | } |
Simon Glass | 3500ae1 | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 923 | log_debug("exit %d\n", result); |
Simon Glass | 79a7d4a | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 924 | |
| 925 | return result; |
| 926 | } |
| 927 | |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 928 | void bootdev_list_hunters(struct bootstd_priv *std) |
| 929 | { |
| 930 | struct bootdev_hunter *orig, *start; |
| 931 | int n_ent, i; |
| 932 | |
| 933 | orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 934 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 935 | |
| 936 | /* |
| 937 | * workaround for strange bug in clang-12 which sees all the below data |
| 938 | * as zeroes. Any access of start seems to fix it, such as |
| 939 | * |
| 940 | * printf("%p", start); |
| 941 | * |
| 942 | * Use memcpy() to force the correct behaviour. |
| 943 | */ |
| 944 | memcpy(&start, &orig, sizeof(orig)); |
| 945 | printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter"); |
| 946 | printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------"); |
| 947 | for (i = 0; i < n_ent; i++) { |
| 948 | struct bootdev_hunter *info = start + i; |
| 949 | |
| 950 | printf("%4d %4s %-15s %s\n", info->prio, |
| 951 | std->hunters_used & BIT(i) ? "*" : "", |
| 952 | uclass_get_name(info->uclass), |
| 953 | info->drv ? info->drv->name : "(none)"); |
| 954 | } |
| 955 | |
| 956 | printf("(total hunters: %d)\n", n_ent); |
| 957 | } |
| 958 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 959 | static int bootdev_post_bind(struct udevice *dev) |
| 960 | { |
| 961 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 962 | |
| 963 | INIT_LIST_HEAD(&ucp->bootflow_head); |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | static int bootdev_pre_unbind(struct udevice *dev) |
| 969 | { |
| 970 | bootdev_clear_bootflows(dev); |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | UCLASS_DRIVER(bootdev) = { |
| 976 | .id = UCLASS_BOOTDEV, |
| 977 | .name = "bootdev", |
| 978 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 979 | .per_device_plat_auto = sizeof(struct bootdev_uc_plat), |
| 980 | .post_bind = bootdev_post_bind, |
| 981 | .pre_unbind = bootdev_pre_unbind, |
| 982 | }; |