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