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