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