Simon Glass | a950d31 | 2022-04-24 23:31:08 -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 | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 9 | #include <blk.h> |
| 10 | #include <bootflow.h> |
| 11 | #include <bootmeth.h> |
| 12 | #include <bootstd.h> |
| 13 | #include <dm.h> |
| 14 | #include <env_internal.h> |
| 15 | #include <fs.h> |
| 16 | #include <malloc.h> |
| 17 | #include <mapmem.h> |
| 18 | #include <dm/uclass-internal.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | 988caca | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 22 | int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize) |
| 23 | { |
| 24 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 25 | |
| 26 | if (!ops->get_state_desc) |
| 27 | return -ENOSYS; |
| 28 | |
| 29 | return ops->get_state_desc(dev, buf, maxsize); |
| 30 | } |
| 31 | |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 32 | int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter) |
| 33 | { |
| 34 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 35 | |
| 36 | if (!ops->check) |
| 37 | return 0; |
| 38 | |
| 39 | return ops->check(dev, iter); |
| 40 | } |
| 41 | |
| 42 | int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 43 | { |
| 44 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 45 | |
| 46 | if (!ops->read_bootflow) |
| 47 | return -ENOSYS; |
| 48 | |
| 49 | return ops->read_bootflow(dev, bflow); |
| 50 | } |
| 51 | |
Simon Glass | 22061d3 | 2023-01-17 10:48:01 -0700 | [diff] [blame] | 52 | int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow, |
| 53 | char *buf, int size) |
| 54 | { |
| 55 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 56 | |
| 57 | if (!ops->set_bootflow) |
| 58 | return -ENOSYS; |
| 59 | |
| 60 | return ops->set_bootflow(dev, bflow, buf, size); |
| 61 | } |
| 62 | |
Simon Glass | c279224 | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 63 | #if CONFIG_IS_ENABLED(BOOTSTD_FULL) |
| 64 | int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow) |
| 65 | { |
| 66 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 67 | |
| 68 | if (!ops->read_all) |
| 69 | return -ENOSYS; |
| 70 | |
| 71 | return ops->read_all(dev, bflow); |
| 72 | } |
| 73 | #endif /* BOOTSTD_FULL */ |
| 74 | |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 75 | int bootmeth_boot(struct udevice *dev, struct bootflow *bflow) |
| 76 | { |
| 77 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 78 | |
| 79 | if (!ops->boot) |
| 80 | return -ENOSYS; |
| 81 | |
| 82 | return ops->boot(dev, bflow); |
| 83 | } |
| 84 | |
| 85 | int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow, |
| 86 | const char *file_path, ulong addr, ulong *sizep) |
| 87 | { |
| 88 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 89 | |
| 90 | if (!ops->read_file) |
| 91 | return -ENOSYS; |
| 92 | |
| 93 | return ops->read_file(dev, bflow, file_path, addr, sizep); |
| 94 | } |
| 95 | |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 96 | int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 97 | { |
| 98 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 99 | |
| 100 | if (!ops->read_bootflow) |
| 101 | return -ENOSYS; |
Simon Glass | b190deb | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 102 | bootflow_init(bflow, NULL, dev); |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 103 | |
| 104 | return ops->read_bootflow(dev, bflow); |
| 105 | } |
| 106 | |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 107 | int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 108 | { |
| 109 | struct bootstd_priv *std; |
| 110 | struct udevice **order; |
| 111 | int count; |
| 112 | int ret; |
| 113 | |
| 114 | ret = bootstd_get_priv(&std); |
| 115 | if (ret) |
| 116 | return ret; |
| 117 | |
| 118 | /* Create an array large enough */ |
| 119 | count = std->bootmeth_count ? std->bootmeth_count : |
| 120 | uclass_id_count(UCLASS_BOOTMETH); |
| 121 | if (!count) |
| 122 | return log_msg_ret("count", -ENOENT); |
| 123 | |
| 124 | order = calloc(count, sizeof(struct udevice *)); |
| 125 | if (!order) |
| 126 | return log_msg_ret("order", -ENOMEM); |
| 127 | |
| 128 | /* If we have an ordering, copy it */ |
| 129 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) { |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 130 | int i; |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 131 | |
| 132 | /* |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 133 | * We don't support skipping global bootmeths. Instead, the user |
| 134 | * should omit them from the ordering |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 135 | */ |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 136 | if (!include_global) |
| 137 | return log_msg_ret("glob", -EPERM); |
| 138 | memcpy(order, std->bootmeth_order, |
| 139 | count * sizeof(struct bootmeth *)); |
| 140 | |
| 141 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) { |
| 142 | for (i = 0; i < count; i++) { |
| 143 | struct udevice *dev = order[i]; |
| 144 | struct bootmeth_uc_plat *ucp; |
| 145 | bool is_global; |
| 146 | |
| 147 | ucp = dev_get_uclass_plat(dev); |
| 148 | is_global = ucp->flags & |
| 149 | BOOTMETHF_GLOBAL; |
| 150 | if (is_global) { |
| 151 | iter->first_glob_method = i; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | struct udevice *dev; |
| 158 | int i, upto, pass; |
| 159 | |
| 160 | /* |
| 161 | * Do two passes, one to find the normal bootmeths and another |
| 162 | * to find the global ones, if required, The global ones go at |
| 163 | * the end. |
| 164 | */ |
| 165 | for (pass = 0, upto = 0; pass < 1 + include_global; pass++) { |
| 166 | if (pass) |
| 167 | iter->first_glob_method = upto; |
| 168 | /* |
| 169 | * Get a list of bootmethods, in seq order (i.e. using |
| 170 | * aliases). There may be gaps so try to count up high |
| 171 | * enough to find them all. |
| 172 | */ |
| 173 | for (i = 0; upto < count && i < 20 + count * 2; i++) { |
| 174 | struct bootmeth_uc_plat *ucp; |
| 175 | bool is_global; |
| 176 | |
| 177 | ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, |
| 178 | i, &dev); |
| 179 | if (ret) |
| 180 | continue; |
| 181 | ucp = dev_get_uclass_plat(dev); |
| 182 | is_global = |
| 183 | IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && |
| 184 | (ucp->flags & BOOTMETHF_GLOBAL); |
| 185 | if (pass ? is_global : !is_global) |
| 186 | order[upto++] = dev; |
| 187 | } |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 188 | } |
| 189 | count = upto; |
| 190 | } |
Simon Glass | 10d16fa | 2022-07-30 15:52:18 -0600 | [diff] [blame] | 191 | if (!count) |
| 192 | return log_msg_ret("count2", -ENOENT); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 193 | |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 194 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global && |
| 195 | iter->first_glob_method != -1 && iter->first_glob_method != count) { |
| 196 | iter->cur_method = iter->first_glob_method; |
| 197 | iter->doing_global = true; |
| 198 | } |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 199 | iter->method_order = order; |
| 200 | iter->num_methods = count; |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int bootmeth_set_order(const char *order_str) |
| 206 | { |
| 207 | struct bootstd_priv *std; |
| 208 | struct udevice **order; |
| 209 | int count, ret, i, len; |
| 210 | const char *s, *p; |
| 211 | |
| 212 | ret = bootstd_get_priv(&std); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
| 216 | if (!order_str) { |
| 217 | free(std->bootmeth_order); |
| 218 | std->bootmeth_order = NULL; |
| 219 | std->bootmeth_count = 0; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* Create an array large enough */ |
| 224 | count = uclass_id_count(UCLASS_BOOTMETH); |
| 225 | if (!count) |
| 226 | return log_msg_ret("count", -ENOENT); |
| 227 | |
| 228 | order = calloc(count + 1, sizeof(struct udevice *)); |
| 229 | if (!order) |
| 230 | return log_msg_ret("order", -ENOMEM); |
| 231 | |
| 232 | for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) { |
| 233 | struct udevice *dev; |
| 234 | |
| 235 | p = strchrnul(s, ' '); |
| 236 | len = p - s; |
| 237 | ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len, |
| 238 | &dev); |
| 239 | if (ret) { |
| 240 | printf("Unknown bootmeth '%.*s'\n", len, s); |
| 241 | free(order); |
| 242 | return ret; |
| 243 | } |
| 244 | order[i] = dev; |
| 245 | } |
| 246 | order[i] = NULL; |
| 247 | free(std->bootmeth_order); |
| 248 | std->bootmeth_order = order; |
| 249 | std->bootmeth_count = i; |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Martyn Welch | 3809fd3 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 254 | int bootmeth_set_property(const char *name, const char *property, const char *value) |
| 255 | { |
| 256 | int ret; |
| 257 | int len; |
| 258 | struct udevice *dev; |
| 259 | const struct bootmeth_ops *ops; |
| 260 | |
| 261 | len = strlen(name); |
| 262 | |
| 263 | ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, name, len, |
| 264 | &dev); |
| 265 | if (ret) { |
| 266 | printf("Unknown bootmeth '%s'\n", name); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | ops = bootmeth_get_ops(dev); |
| 271 | if (!ops->set_property) { |
| 272 | printf("set_property not found\n"); |
| 273 | return -ENODEV; |
| 274 | } |
| 275 | |
| 276 | return ops->set_property(dev, property, value); |
| 277 | } |
| 278 | |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 279 | int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc) |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 280 | { |
| 281 | int ret; |
| 282 | |
| 283 | if (desc) { |
| 284 | ret = fs_set_blk_dev_with_part(desc, bflow->part); |
| 285 | if (ret) |
| 286 | return log_msg_ret("set", ret); |
| 287 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) { |
| 288 | fs_set_type(bflow->fs_type); |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc, |
| 295 | const char *prefix, const char *fname) |
| 296 | { |
| 297 | char path[200]; |
| 298 | loff_t size; |
| 299 | int ret, ret2; |
| 300 | |
| 301 | snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname); |
| 302 | log_debug("trying: %s\n", path); |
| 303 | |
| 304 | free(bflow->fname); |
| 305 | bflow->fname = strdup(path); |
| 306 | if (!bflow->fname) |
| 307 | return log_msg_ret("name", -ENOMEM); |
| 308 | |
| 309 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) |
| 310 | fs_set_type(bflow->fs_type); |
| 311 | |
| 312 | ret = fs_size(path, &size); |
| 313 | log_debug(" %s - err=%d\n", path, ret); |
| 314 | |
| 315 | /* Sadly FS closes the file after fs_size() so we must redo this */ |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 316 | ret2 = bootmeth_setup_fs(bflow, desc); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 317 | if (ret2) |
| 318 | return log_msg_ret("fs", ret2); |
| 319 | |
| 320 | if (ret) |
| 321 | return log_msg_ret("size", ret); |
| 322 | |
| 323 | bflow->size = size; |
| 324 | bflow->state = BOOTFLOWST_FILE; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 329 | int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align) |
| 330 | { |
| 331 | void *buf; |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 332 | uint size; |
| 333 | int ret; |
| 334 | |
| 335 | size = bflow->size; |
| 336 | log_debug(" - script file size %x\n", size); |
| 337 | if (size > size_limit) |
| 338 | return log_msg_ret("chk", -E2BIG); |
| 339 | |
Simon Glass | de7b5a8 | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 340 | ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf); |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 341 | if (ret) |
| 342 | return log_msg_ret("all", ret); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 343 | |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 344 | bflow->state = BOOTFLOWST_READY; |
| 345 | bflow->buf = buf; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 350 | int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, |
| 351 | void **bufp, uint *sizep) |
| 352 | { |
| 353 | struct blk_desc *desc = NULL; |
| 354 | char path[200]; |
| 355 | loff_t size; |
| 356 | void *buf; |
| 357 | int ret; |
| 358 | |
| 359 | snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname); |
| 360 | log_debug("trying: %s\n", path); |
| 361 | |
| 362 | if (bflow->blk) |
| 363 | desc = dev_get_uclass_plat(bflow->blk); |
| 364 | |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 365 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 366 | if (ret) |
| 367 | return log_msg_ret("fs", ret); |
| 368 | |
| 369 | ret = fs_size(path, &size); |
| 370 | log_debug(" %s - err=%d\n", path, ret); |
| 371 | |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 372 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 373 | if (ret) |
| 374 | return log_msg_ret("fs", ret); |
| 375 | |
Simon Glass | de7b5a8 | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 376 | ret = fs_read_alloc(path, size, 0, &buf); |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 377 | if (ret) |
| 378 | return log_msg_ret("all", ret); |
| 379 | |
| 380 | *bufp = buf; |
| 381 | *sizep = size; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 386 | int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, |
| 387 | const char *file_path, ulong addr, ulong *sizep) |
| 388 | { |
| 389 | struct blk_desc *desc = NULL; |
| 390 | loff_t len_read; |
| 391 | loff_t size; |
| 392 | int ret; |
| 393 | |
| 394 | if (bflow->blk) |
| 395 | desc = dev_get_uclass_plat(bflow->blk); |
| 396 | |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 397 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 398 | if (ret) |
| 399 | return log_msg_ret("fs", ret); |
| 400 | |
| 401 | ret = fs_size(file_path, &size); |
| 402 | if (ret) |
| 403 | return log_msg_ret("size", ret); |
| 404 | if (size > *sizep) |
| 405 | return log_msg_ret("spc", -ENOSPC); |
| 406 | |
Simon Glass | 0c0c82b | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 407 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 408 | if (ret) |
| 409 | return log_msg_ret("fs", ret); |
| 410 | |
| 411 | ret = fs_read(file_path, addr, 0, 0, &len_read); |
| 412 | if (ret) |
| 413 | return ret; |
| 414 | *sizep = len_read; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | #ifdef CONFIG_BOOTSTD_FULL |
| 420 | /** |
| 421 | * on_bootmeths() - Update the bootmeth order |
| 422 | * |
Simon Glass | 2270c36 | 2023-07-12 09:04:33 -0600 | [diff] [blame] | 423 | * This will check for a valid list of bootmeths and only apply it if valid. |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 424 | */ |
| 425 | static int on_bootmeths(const char *name, const char *value, enum env_op op, |
| 426 | int flags) |
| 427 | { |
| 428 | int ret; |
| 429 | |
| 430 | switch (op) { |
| 431 | case env_op_create: |
| 432 | case env_op_overwrite: |
| 433 | ret = bootmeth_set_order(value); |
| 434 | if (ret) |
| 435 | return 1; |
| 436 | return 0; |
| 437 | case env_op_delete: |
| 438 | bootmeth_set_order(NULL); |
| 439 | fallthrough; |
| 440 | default: |
| 441 | return 0; |
| 442 | } |
| 443 | } |
| 444 | U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths); |
| 445 | #endif /* CONFIG_BOOTSTD_FULL */ |
| 446 | |
| 447 | UCLASS_DRIVER(bootmeth) = { |
| 448 | .id = UCLASS_BOOTMETH, |
| 449 | .name = "bootmeth", |
| 450 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 451 | .per_device_plat_auto = sizeof(struct bootmeth_uc_plat), |
| 452 | }; |