Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 8 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <bootdev.h> |
| 11 | #include <bootflow.h> |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 12 | #include <bootmeth.h> |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 13 | #include <bootstd.h> |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 14 | #include <fs.h> |
| 15 | #include <log.h> |
| 16 | #include <malloc.h> |
| 17 | #include <part.h> |
| 18 | #include <sort.h> |
| 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/lists.h> |
| 21 | #include <dm/uclass-internal.h> |
| 22 | |
| 23 | enum { |
| 24 | /* |
| 25 | * Set some sort of limit on the number of partitions a bootdev can |
| 26 | * have. Note that for disks this limits the partitions numbers that |
| 27 | * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV |
| 28 | */ |
| 29 | MAX_PART_PER_BOOTDEV = 30, |
| 30 | |
| 31 | /* Maximum supported length of the "boot_targets" env string */ |
| 32 | BOOT_TARGETS_MAX_LEN = 100, |
| 33 | }; |
| 34 | |
| 35 | int bootdev_add_bootflow(struct bootflow *bflow) |
| 36 | { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 37 | struct bootstd_priv *std; |
| 38 | struct bootflow *new; |
| 39 | int ret; |
| 40 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 41 | ret = bootstd_get_priv(&std); |
| 42 | if (ret) |
| 43 | return ret; |
| 44 | |
| 45 | new = malloc(sizeof(*bflow)); |
| 46 | if (!new) |
| 47 | return log_msg_ret("bflow", -ENOMEM); |
| 48 | memcpy(new, bflow, sizeof(*bflow)); |
| 49 | |
| 50 | list_add_tail(&new->glob_node, &std->glob_head); |
Simon Glass | dee4d64 | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 51 | if (bflow->dev) { |
| 52 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 53 | |
| 54 | list_add_tail(&new->bm_node, &ucp->bootflow_head); |
| 55 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) |
| 61 | { |
| 62 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 63 | |
| 64 | if (list_empty(&ucp->bootflow_head)) |
| 65 | return -ENOENT; |
| 66 | |
| 67 | *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 68 | bm_node); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int bootdev_next_bootflow(struct bootflow **bflowp) |
| 74 | { |
| 75 | struct bootflow *bflow = *bflowp; |
| 76 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 77 | |
| 78 | *bflowp = NULL; |
| 79 | |
| 80 | if (list_is_last(&bflow->bm_node, &ucp->bootflow_head)) |
| 81 | return -ENOENT; |
| 82 | |
| 83 | *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, |
| 89 | struct udevice **devp) |
| 90 | { |
| 91 | struct udevice *dev; |
| 92 | char dev_name[30]; |
| 93 | char *str; |
| 94 | int ret; |
| 95 | |
| 96 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 97 | str = strdup(dev_name); |
| 98 | if (!str) |
| 99 | return -ENOMEM; |
| 100 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | device_set_name_alloced(dev); |
| 104 | *devp = dev; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, |
| 110 | struct bootflow_iter *iter, struct bootflow *bflow) |
| 111 | { |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 112 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method); |
| 113 | bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 114 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 115 | struct disk_partition info; |
| 116 | char partstr[20]; |
| 117 | char name[60]; |
| 118 | int ret; |
| 119 | |
| 120 | /* Sanity check */ |
| 121 | if (iter->part >= MAX_PART_PER_BOOTDEV) |
| 122 | return log_msg_ret("max", -ESHUTDOWN); |
| 123 | |
| 124 | bflow->blk = blk; |
| 125 | if (iter->part) |
| 126 | snprintf(partstr, sizeof(partstr), "part_%x", iter->part); |
| 127 | else |
| 128 | strcpy(partstr, "whole"); |
| 129 | snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); |
| 130 | bflow->name = strdup(name); |
| 131 | if (!bflow->name) |
| 132 | return log_msg_ret("name", -ENOMEM); |
| 133 | |
| 134 | bflow->part = iter->part; |
| 135 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 136 | ret = bootmeth_check(bflow->method, iter); |
| 137 | if (ret) |
| 138 | return log_msg_ret("check", ret); |
| 139 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 140 | /* |
| 141 | * partition numbers start at 0 so this cannot succeed, but it can tell |
| 142 | * us whether there is valid media there |
| 143 | */ |
| 144 | ret = part_get_info(desc, iter->part, &info); |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 145 | log_debug("part_get_info() returned %d\n", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 146 | if (!iter->part && ret == -ENOENT) |
| 147 | ret = 0; |
| 148 | |
| 149 | /* |
| 150 | * This error indicates the media is not present. Otherwise we just |
| 151 | * blindly scan the next partition. We could be more intelligent here |
| 152 | * and check which partition numbers actually exist. |
| 153 | */ |
| 154 | if (ret == -EOPNOTSUPP) |
| 155 | ret = -ESHUTDOWN; |
| 156 | else |
| 157 | bflow->state = BOOTFLOWST_MEDIA; |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 158 | if (ret && !allow_any_part) { |
Simon Glass | a8e7d51 | 2023-04-28 13:18:09 -0600 | [diff] [blame] | 159 | /* allow partition 1 to be missing */ |
| 160 | if (iter->part == 1) { |
| 161 | iter->max_part = 3; |
| 162 | ret = -ENOENT; |
| 163 | } |
| 164 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 165 | return log_msg_ret("part", ret); |
Simon Glass | a8e7d51 | 2023-04-28 13:18:09 -0600 | [diff] [blame] | 166 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Currently we don't get the number of partitions, so just |
| 170 | * assume a large number |
| 171 | */ |
| 172 | iter->max_part = MAX_PART_PER_BOOTDEV; |
| 173 | |
Nam Cao | cb0d3fb | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 174 | if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) { |
| 175 | /* a particular partition was specified, scan it without checking */ |
| 176 | } else if (!iter->part) { |
| 177 | /* This is the whole disk, check if we have bootable partitions */ |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 178 | iter->first_bootable = part_get_bootable(desc); |
| 179 | log_debug("checking bootable=%d\n", iter->first_bootable); |
Simon Glass | 0fca7e8 | 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 | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 185 | |
| 186 | /* if there are bootable partitions, scan only those */ |
Simon Glass | 0fca7e8 | 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 | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 189 | return log_msg_ret("boot", -EINVAL); |
| 190 | } else { |
Simon Glass | 017656e | 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 | a40f782 | 2023-04-24 13:49:49 +1200 | [diff] [blame] | 193 | if (ret) |
| 194 | return log_msg_ret("fs", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 195 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 196 | log_debug("%s: Found partition %x type %x fstype %d\n", |
Simon Glass | ed0ec66 | 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 | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 200 | ret ? -1 : fs_get_type()); |
Simon Glass | a40f782 | 2023-04-24 13:49:49 +1200 | [diff] [blame] | 201 | bflow->blk = blk; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 202 | bflow->state = BOOTFLOWST_FS; |
| 203 | } |
| 204 | |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 205 | log_debug("method %s\n", bflow->method->name); |
Simon Glass | 03fcbf9 | 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 | 017656e | 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 | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 222 | ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev); |
Simon Glass | 017656e | 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 | 91592d6 | 2023-07-30 16:29:25 +0200 | [diff] [blame] | 228 | ret ? simple_itoa(-ret) : "OK", |
Simon Glass | 017656e | 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 | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 231 | ret = uclass_next_device_check(&dev); |
Simon Glass | 017656e | 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 | b1dc36a | 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 | b1d581d | 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 | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 275 | { |
| 276 | struct udevice *parent, *dev; |
| 277 | char dev_name[50]; |
Simon Glass | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 278 | int ret, len; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 279 | |
Simon Glass | b1dc36a | 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 | 017656e | 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 | 017656e | 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 | b1d581d | 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 | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 320 | len = bootdev_get_suffix_start(dev, ".bootdev"); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 321 | ret = device_find_child_by_namelen(parent, dev->name, len, &blk); |
Simon Glass | b1dc36a | 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 | b73a829 | 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 | 017656e | 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 | b1dc36a | 2023-01-17 10:47:25 -0700 | [diff] [blame] | 344 | int ret, len; |
Simon Glass | 017656e | 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 | b1d581d | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 349 | /* This should always work if bootdev_setup_for_sibling_blk() was used */ |
Simon Glass | b1dc36a | 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 | 017656e | 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 | 7370825 | 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 | e22fe92 | 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 | 4108c6b | 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 | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 388 | */ |
Simon Glass | e22fe92 | 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 | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 390 | { |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 391 | int seq, len, method_flags; |
Simon Glass | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 392 | enum uclass_id id; |
| 393 | const char *end; |
Simon Glass | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 394 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 395 | method_flags = 0; |
Simon Glass | 7370825 | 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 | 215be68 | 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 | e22fe92 | 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 | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 416 | } else { |
Simon Glass | 4108c6b | 2023-04-24 13:49:47 +1200 | [diff] [blame] | 417 | return -EPFNOSUPPORT; |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 418 | } |
Simon Glass | 7370825 | 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 | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 423 | if (method_flagsp) |
| 424 | *method_flagsp = method_flags; |
Simon Glass | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 425 | |
| 426 | return id; |
| 427 | } |
| 428 | |
Simon Glass | e22fe92 | 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 | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 431 | { |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 432 | int seq, ret, method_flags = 0; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 433 | struct udevice *media; |
| 434 | struct uclass *uc; |
| 435 | enum uclass_id id; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 436 | |
Simon Glass | 1d80964 | 2024-09-01 16:27:28 -0600 | [diff] [blame] | 437 | if (!CONFIG_IS_ENABLED(BLK)) |
| 438 | return -ENOSYS; |
| 439 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 440 | ret = label_to_uclass(label, &seq, &method_flags); |
Simon Glass | 7370825 | 2023-01-17 10:48:00 -0700 | [diff] [blame] | 441 | if (ret < 0) |
| 442 | return log_msg_ret("uc", ret); |
| 443 | id = ret; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 444 | |
| 445 | /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ |
| 446 | uclass_id_foreach_dev(id, media, uc) { |
| 447 | struct udevice *bdev, *blk; |
| 448 | int ret; |
| 449 | |
| 450 | /* if there is no seq, match anything */ |
| 451 | if (seq != -1 && dev_seq(media) != seq) { |
| 452 | log_debug("- skip, media seq=%d\n", dev_seq(media)); |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, |
| 457 | &bdev); |
| 458 | if (ret) { |
| 459 | log_debug("- looking via blk, seq=%d, id=%d\n", seq, |
| 460 | id); |
| 461 | ret = blk_find_device(id, seq, &blk); |
| 462 | if (!ret) { |
| 463 | log_debug("- get from blk %s\n", blk->name); |
| 464 | ret = bootdev_get_from_blk(blk, &bdev); |
| 465 | } |
| 466 | } |
| 467 | if (!ret) { |
| 468 | log_debug("- found %s\n", bdev->name); |
| 469 | *devp = bdev; |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * if no sequence number was provided, we must scan all |
| 473 | * bootdevs for this media uclass |
| 474 | */ |
Simon Glass | 60bcbf0 | 2023-09-23 14:50:15 -0600 | [diff] [blame] | 475 | if (seq == -1) |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 476 | method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS; |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 477 | if (method_flagsp) |
| 478 | *method_flagsp = method_flags; |
Simon Glass | 60bcbf0 | 2023-09-23 14:50:15 -0600 | [diff] [blame] | 479 | log_debug("method flags %x\n", method_flags); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | log_debug("- no device in %s\n", media->name); |
| 483 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 484 | |
| 485 | return -ENOENT; |
| 486 | } |
| 487 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 488 | int bootdev_find_by_any(const char *name, struct udevice **devp, |
| 489 | int *method_flagsp) |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 490 | { |
| 491 | struct udevice *dev; |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 492 | int method_flags = 0; |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 493 | int ret = -ENODEV, seq; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 494 | char *endp; |
| 495 | |
| 496 | seq = simple_strtol(name, &endp, 16); |
| 497 | |
| 498 | /* Select by name, label or number */ |
| 499 | if (*endp) { |
| 500 | ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); |
| 501 | if (ret == -ENODEV) { |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 502 | ret = bootdev_find_by_label(name, &dev, &method_flags); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 503 | if (ret) { |
| 504 | printf("Cannot find bootdev '%s' (err=%d)\n", |
| 505 | name, ret); |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 506 | return log_msg_ret("lab", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 507 | } |
| 508 | ret = device_probe(dev); |
| 509 | } |
| 510 | if (ret) { |
| 511 | printf("Cannot probe bootdev '%s' (err=%d)\n", name, |
| 512 | ret); |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 513 | return log_msg_ret("pro", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 514 | } |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 515 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 516 | ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 517 | method_flags |= BOOTFLOW_METHF_SINGLE_DEV; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 518 | } |
| 519 | if (ret) { |
| 520 | printf("Cannot find '%s' (err=%d)\n", name, ret); |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | *devp = dev; |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 525 | if (method_flagsp) |
| 526 | *method_flagsp = method_flags; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 531 | int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp, |
| 532 | int *method_flagsp) |
| 533 | { |
| 534 | int ret; |
| 535 | |
| 536 | ret = bootdev_hunt(label, false); |
| 537 | if (ret) |
| 538 | return log_msg_ret("scn", ret); |
| 539 | ret = bootdev_find_by_label(label, devp, method_flagsp); |
| 540 | if (ret) |
| 541 | return log_msg_ret("fnd", ret); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Simon Glass | d6e39d1 | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 546 | static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 547 | struct bootflow *bflow) |
| 548 | { |
| 549 | struct udevice *blk; |
| 550 | int ret; |
| 551 | |
| 552 | ret = bootdev_get_sibling_blk(dev, &blk); |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 553 | log_debug("sibling_blk ret=%d, blk=%s\n", ret, |
| 554 | ret ? "(none)" : blk->name); |
Simon Glass | d6e39d1 | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 555 | /* |
| 556 | * If there is no media, indicate that no more partitions should be |
| 557 | * checked |
| 558 | */ |
| 559 | if (ret == -EOPNOTSUPP) |
| 560 | ret = -ESHUTDOWN; |
| 561 | if (ret) |
| 562 | return log_msg_ret("blk", ret); |
| 563 | assert(blk); |
| 564 | ret = bootdev_find_in_blk(dev, blk, iter, bflow); |
| 565 | if (ret) |
| 566 | return log_msg_ret("find", ret); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 571 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 572 | struct bootflow *bflow) |
| 573 | { |
| 574 | const struct bootdev_ops *ops = bootdev_get_ops(dev); |
| 575 | |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 576 | log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part, |
| 577 | ops->get_bootflow); |
Simon Glass | 00501fc | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 578 | bootflow_init(bflow, dev, iter->method); |
Simon Glass | d6e39d1 | 2023-01-17 10:47:26 -0700 | [diff] [blame] | 579 | if (!ops->get_bootflow) |
| 580 | return default_get_bootflow(dev, iter, bflow); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 581 | |
| 582 | return ops->get_bootflow(dev, iter, bflow); |
| 583 | } |
| 584 | |
| 585 | void bootdev_clear_bootflows(struct udevice *dev) |
| 586 | { |
| 587 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 588 | |
| 589 | while (!list_empty(&ucp->bootflow_head)) { |
| 590 | struct bootflow *bflow; |
| 591 | |
| 592 | bflow = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 593 | bm_node); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 594 | bootflow_remove(bflow); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 598 | int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp, |
| 599 | int *method_flagsp) |
| 600 | { |
| 601 | struct udevice *dev; |
| 602 | |
| 603 | log_debug("next\n"); |
| 604 | for (dev = NULL; !dev && iter->labels[++iter->cur_label];) { |
Simon Glass | 4108c6b | 2023-04-24 13:49:47 +1200 | [diff] [blame] | 605 | const char *label = iter->labels[iter->cur_label]; |
| 606 | int ret; |
| 607 | |
| 608 | log_debug("Scanning: %s\n", label); |
| 609 | ret = bootdev_hunt_and_find_by_label(label, &dev, |
| 610 | method_flagsp); |
| 611 | if (iter->flags & BOOTFLOWIF_SHOW) { |
| 612 | if (ret == -EPFNOSUPPORT) { |
| 613 | log_warning("Unknown uclass '%s' in label\n", |
| 614 | label); |
| 615 | } else if (ret == -ENOENT) { |
| 616 | /* |
| 617 | * looking for, e.g. 'scsi0' should find |
| 618 | * something if SCSI is present |
| 619 | */ |
| 620 | if (!trailing_strtol(label)) { |
| 621 | log_warning("No bootdevs for '%s'\n", |
| 622 | label); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | if (!dev) |
| 630 | return log_msg_ret("fin", -ENODEV); |
| 631 | *devp = dev; |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 636 | int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp) |
| 637 | { |
Simon Glass | 15f128a | 2024-08-15 14:30:21 -0600 | [diff] [blame] | 638 | struct udevice *dev = *devp; |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 639 | bool found; |
| 640 | int ret; |
| 641 | |
| 642 | /* find the next device with this priority */ |
| 643 | *devp = NULL; |
| 644 | log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev, |
| 645 | dev ? dev->name : "none"); |
Simon Glass | 5aaa562 | 2024-08-15 14:30:22 -0600 | [diff] [blame] | 646 | found = false; |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 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 | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 677 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | 660a995 | 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 | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 681 | BOOTFLOWIF_SHOW); |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 682 | log_debug("- bootdev_hunt_prio() ret %d\n", |
| 683 | ret); |
Simon Glass | 660a995 | 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); |
Simon Glass | 5aaa562 | 2024-08-15 14:30:22 -0600 | [diff] [blame] | 689 | if (ret) |
Simon Glass | 15f128a | 2024-08-15 14:30:21 -0600 | [diff] [blame] | 690 | log_debug("Device '%s' failed to probe\n", |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 691 | dev->name); |
Simon Glass | 5aaa562 | 2024-08-15 14:30:22 -0600 | [diff] [blame] | 692 | else |
| 693 | found = true; |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 694 | } |
Simon Glass | 5aaa562 | 2024-08-15 14:30:22 -0600 | [diff] [blame] | 695 | } while (!found); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 696 | |
| 697 | *devp = dev; |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Simon Glass | ba3d537 | 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 | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 704 | { |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 705 | struct udevice *bootstd, *dev = NULL; |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 706 | bool show = iter->flags & BOOTFLOWIF_SHOW; |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 707 | int method_flags; |
Nam Cao | cb0d3fb | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 708 | char buf[32]; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 709 | int ret; |
| 710 | |
Nam Cao | cb0d3fb | 2024-02-21 13:41:44 +0100 | [diff] [blame] | 711 | if (label) { |
| 712 | const char *end = strchr(label, ':'); |
| 713 | |
| 714 | if (end) { |
| 715 | size_t len = (size_t)(end - label); |
| 716 | const char *part = end + 1; |
| 717 | |
| 718 | if (len + 1 > sizeof(buf)) { |
| 719 | log_err("label \"%s\" is way too long\n", label); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | memcpy(buf, label, len); |
| 724 | buf[len] = '\0'; |
| 725 | label = buf; |
| 726 | |
| 727 | unsigned long tmp; |
| 728 | |
| 729 | if (strict_strtoul(part, 0, &tmp)) { |
| 730 | log_err("Invalid partition number: %s\n", part); |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | |
| 734 | iter->flags |= BOOTFLOWIF_SINGLE_PARTITION; |
| 735 | iter->part = tmp; |
| 736 | } |
| 737 | } |
| 738 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 739 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 740 | if (ret) { |
| 741 | log_err("Missing bootstd device\n"); |
| 742 | return log_msg_ret("std", ret); |
| 743 | } |
| 744 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 745 | /* hunt for any pre-scan devices */ |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 746 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 747 | ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show); |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 748 | log_debug("- bootdev_hunt_prio() ret %d\n", ret); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 749 | if (ret) |
| 750 | return log_msg_ret("pre", ret); |
| 751 | } |
| 752 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 753 | /* Handle scanning a single device */ |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 754 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) { |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 755 | if (iter->flags & BOOTFLOWIF_HUNT) { |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 756 | ret = bootdev_hunt(label, show); |
| 757 | if (ret) |
| 758 | return log_msg_ret("hun", ret); |
| 759 | } |
| 760 | ret = bootdev_find_by_any(label, &dev, &method_flags); |
| 761 | if (ret) |
| 762 | return log_msg_ret("lab", ret); |
| 763 | |
| 764 | log_debug("method_flags: %x\n", method_flags); |
| 765 | if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS) |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 766 | iter->flags |= BOOTFLOWIF_SINGLE_UCLASS; |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 767 | else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV) |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 768 | iter->flags |= BOOTFLOWIF_SINGLE_DEV; |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 769 | else |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 770 | iter->flags |= BOOTFLOWIF_SINGLE_MEDIA; |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 771 | log_debug("Selected label: %s, flags %x\n", label, iter->flags); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 772 | } else { |
| 773 | bool ok; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 774 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 775 | /* This either returns a non-empty list or NULL */ |
| 776 | iter->labels = bootstd_get_bootdev_order(bootstd, &ok); |
| 777 | if (!ok) |
| 778 | return log_msg_ret("ord", -ENOMEM); |
| 779 | log_debug("setup labels %p\n", iter->labels); |
| 780 | if (iter->labels) { |
| 781 | iter->cur_label = -1; |
| 782 | ret = bootdev_next_label(iter, &dev, &method_flags); |
| 783 | } else { |
| 784 | ret = bootdev_next_prio(iter, &dev); |
| 785 | method_flags = 0; |
| 786 | } |
| 787 | if (!dev) |
| 788 | return log_msg_ret("fin", -ENOENT); |
| 789 | log_debug("Selected bootdev: %s\n", dev->name); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 790 | } |
| 791 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 792 | ret = device_probe(dev); |
| 793 | if (ret) |
| 794 | return log_msg_ret("probe", ret); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 795 | if (method_flagsp) |
| 796 | *method_flagsp = method_flags; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 797 | *devp = dev; |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Simon Glass | 1248d2b | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 802 | static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show) |
| 803 | { |
| 804 | const char *name = uclass_get_name(info->uclass); |
| 805 | struct bootstd_priv *std; |
| 806 | int ret; |
| 807 | |
| 808 | ret = bootstd_get_priv(&std); |
| 809 | if (ret) |
| 810 | return log_msg_ret("std", ret); |
| 811 | |
| 812 | if (!(std->hunters_used & BIT(seq))) { |
| 813 | if (show) |
| 814 | printf("Hunting with: %s\n", |
| 815 | uclass_get_name(info->uclass)); |
| 816 | log_debug("Hunting with: %s\n", name); |
| 817 | if (info->hunt) { |
| 818 | ret = info->hunt(info, show); |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 819 | log_debug(" - hunt result %d\n", ret); |
Tony Dinh | 81356b4 | 2023-11-02 11:51:15 -0700 | [diff] [blame] | 820 | if (ret && ret != -ENOENT) |
Simon Glass | 1248d2b | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 821 | return ret; |
| 822 | } |
| 823 | std->hunters_used |= BIT(seq); |
| 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | int bootdev_hunt(const char *spec, bool show) |
| 830 | { |
| 831 | struct bootdev_hunter *start; |
| 832 | const char *end; |
| 833 | int n_ent, i; |
| 834 | int result; |
| 835 | size_t len; |
| 836 | |
| 837 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 838 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 839 | result = 0; |
| 840 | |
| 841 | len = SIZE_MAX; |
| 842 | if (spec) { |
| 843 | trailing_strtoln_end(spec, NULL, &end); |
| 844 | len = end - spec; |
| 845 | } |
| 846 | |
| 847 | for (i = 0; i < n_ent; i++) { |
| 848 | struct bootdev_hunter *info = start + i; |
| 849 | const char *name = uclass_get_name(info->uclass); |
| 850 | int ret; |
| 851 | |
| 852 | log_debug("looking at %.*s for %s\n", |
| 853 | (int)max(strlen(name), len), spec, name); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 854 | if (spec && strncmp(spec, name, max(strlen(name), len))) { |
| 855 | if (info->uclass != UCLASS_ETH || |
| 856 | (strcmp("dhcp", spec) && strcmp("pxe", spec))) |
| 857 | continue; |
| 858 | } |
Simon Glass | 1248d2b | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 859 | ret = bootdev_hunt_drv(info, i, show); |
| 860 | if (ret) |
| 861 | result = ret; |
| 862 | } |
| 863 | |
| 864 | return result; |
| 865 | } |
| 866 | |
Simon Glass | 62b03cc | 2023-09-20 07:29:49 -0600 | [diff] [blame] | 867 | int bootdev_unhunt(enum uclass_id id) |
| 868 | { |
| 869 | struct bootdev_hunter *start; |
| 870 | int n_ent, i; |
| 871 | |
| 872 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 873 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 874 | for (i = 0; i < n_ent; i++) { |
| 875 | struct bootdev_hunter *info = start + i; |
| 876 | |
| 877 | if (info->uclass == id) { |
| 878 | struct bootstd_priv *std; |
| 879 | int ret; |
| 880 | |
| 881 | ret = bootstd_get_priv(&std); |
| 882 | if (ret) |
| 883 | return log_msg_ret("std", ret); |
| 884 | if (!(std->hunters_used & BIT(i))) |
| 885 | return -EALREADY; |
| 886 | std->hunters_used &= ~BIT(i); |
| 887 | return 0; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return -ENOENT; |
| 892 | } |
| 893 | |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 894 | int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show) |
| 895 | { |
| 896 | struct bootdev_hunter *start; |
| 897 | int n_ent, i; |
| 898 | int result; |
| 899 | |
| 900 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 901 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 902 | result = 0; |
| 903 | |
| 904 | log_debug("Hunting for priority %d\n", prio); |
| 905 | for (i = 0; i < n_ent; i++) { |
| 906 | struct bootdev_hunter *info = start + i; |
| 907 | int ret; |
| 908 | |
| 909 | if (prio != info->prio) |
| 910 | continue; |
| 911 | ret = bootdev_hunt_drv(info, i, show); |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 912 | log_debug("bootdev_hunt_drv() return %d\n", ret); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 913 | if (ret && ret != -ENOENT) |
| 914 | result = ret; |
| 915 | } |
Simon Glass | bf2d02a | 2023-07-30 11:15:16 -0600 | [diff] [blame] | 916 | log_debug("exit %d\n", result); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 917 | |
| 918 | return result; |
| 919 | } |
| 920 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 921 | void bootdev_list_hunters(struct bootstd_priv *std) |
| 922 | { |
| 923 | struct bootdev_hunter *orig, *start; |
| 924 | int n_ent, i; |
| 925 | |
| 926 | orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 927 | n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); |
| 928 | |
| 929 | /* |
| 930 | * workaround for strange bug in clang-12 which sees all the below data |
| 931 | * as zeroes. Any access of start seems to fix it, such as |
| 932 | * |
| 933 | * printf("%p", start); |
| 934 | * |
| 935 | * Use memcpy() to force the correct behaviour. |
| 936 | */ |
| 937 | memcpy(&start, &orig, sizeof(orig)); |
| 938 | printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter"); |
| 939 | printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------"); |
| 940 | for (i = 0; i < n_ent; i++) { |
| 941 | struct bootdev_hunter *info = start + i; |
| 942 | |
| 943 | printf("%4d %4s %-15s %s\n", info->prio, |
| 944 | std->hunters_used & BIT(i) ? "*" : "", |
| 945 | uclass_get_name(info->uclass), |
| 946 | info->drv ? info->drv->name : "(none)"); |
| 947 | } |
| 948 | |
| 949 | printf("(total hunters: %d)\n", n_ent); |
| 950 | } |
| 951 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 952 | static int bootdev_post_bind(struct udevice *dev) |
| 953 | { |
| 954 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 955 | |
| 956 | INIT_LIST_HEAD(&ucp->bootflow_head); |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static int bootdev_pre_unbind(struct udevice *dev) |
| 962 | { |
| 963 | bootdev_clear_bootflows(dev); |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | UCLASS_DRIVER(bootdev) = { |
| 969 | .id = UCLASS_BOOTDEV, |
| 970 | .name = "bootdev", |
| 971 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 972 | .per_device_plat_auto = sizeof(struct bootdev_uc_plat), |
| 973 | .post_bind = bootdev_post_bind, |
| 974 | .pre_unbind = bootdev_pre_unbind, |
| 975 | }; |