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 | |
Simon Glass | a63bda6 | 2023-01-17 10:48:01 -0700 | [diff] [blame] | 53 | int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow, |
| 54 | char *buf, int size) |
| 55 | { |
| 56 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 57 | |
| 58 | if (!ops->set_bootflow) |
| 59 | return -ENOSYS; |
| 60 | |
| 61 | return ops->set_bootflow(dev, bflow, buf, size); |
| 62 | } |
| 63 | |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 64 | int bootmeth_boot(struct udevice *dev, struct bootflow *bflow) |
| 65 | { |
| 66 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 67 | |
| 68 | if (!ops->boot) |
| 69 | return -ENOSYS; |
| 70 | |
| 71 | return ops->boot(dev, bflow); |
| 72 | } |
| 73 | |
| 74 | int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow, |
| 75 | const char *file_path, ulong addr, ulong *sizep) |
| 76 | { |
| 77 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 78 | |
| 79 | if (!ops->read_file) |
| 80 | return -ENOSYS; |
| 81 | |
| 82 | return ops->read_file(dev, bflow, file_path, addr, sizep); |
| 83 | } |
| 84 | |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 85 | int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 86 | { |
| 87 | const struct bootmeth_ops *ops = bootmeth_get_ops(dev); |
| 88 | |
| 89 | if (!ops->read_bootflow) |
| 90 | return -ENOSYS; |
Simon Glass | 00501fc | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 91 | bootflow_init(bflow, NULL, dev); |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 92 | |
| 93 | return ops->read_bootflow(dev, bflow); |
| 94 | } |
| 95 | |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 96 | int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 97 | { |
| 98 | struct bootstd_priv *std; |
| 99 | struct udevice **order; |
| 100 | int count; |
| 101 | int ret; |
| 102 | |
| 103 | ret = bootstd_get_priv(&std); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | /* Create an array large enough */ |
| 108 | count = std->bootmeth_count ? std->bootmeth_count : |
| 109 | uclass_id_count(UCLASS_BOOTMETH); |
| 110 | if (!count) |
| 111 | return log_msg_ret("count", -ENOENT); |
| 112 | |
| 113 | order = calloc(count, sizeof(struct udevice *)); |
| 114 | if (!order) |
| 115 | return log_msg_ret("order", -ENOMEM); |
| 116 | |
| 117 | /* If we have an ordering, copy it */ |
| 118 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) { |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 119 | int i; |
| 120 | |
| 121 | /* |
| 122 | * We don't support skipping global bootmeths. Instead, the user |
| 123 | * should omit them from the ordering |
| 124 | */ |
| 125 | if (!include_global) |
| 126 | return log_msg_ret("glob", -EPERM); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 127 | memcpy(order, std->bootmeth_order, |
| 128 | count * sizeof(struct bootmeth *)); |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 129 | |
| 130 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) { |
| 131 | for (i = 0; i < count; i++) { |
| 132 | struct udevice *dev = order[i]; |
| 133 | struct bootmeth_uc_plat *ucp; |
| 134 | bool is_global; |
| 135 | |
| 136 | ucp = dev_get_uclass_plat(dev); |
| 137 | is_global = ucp->flags & |
| 138 | BOOTMETHF_GLOBAL; |
| 139 | if (is_global) { |
| 140 | iter->first_glob_method = i; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 145 | } else { |
| 146 | struct udevice *dev; |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 147 | int i, upto, pass; |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 148 | |
| 149 | /* |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 150 | * Do two passes, one to find the normal bootmeths and another |
| 151 | * to find the global ones, if required, The global ones go at |
| 152 | * the end. |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 153 | */ |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 154 | for (pass = 0, upto = 0; pass < 1 + include_global; pass++) { |
| 155 | if (pass) |
| 156 | iter->first_glob_method = upto; |
| 157 | /* |
| 158 | * Get a list of bootmethods, in seq order (i.e. using |
| 159 | * aliases). There may be gaps so try to count up high |
| 160 | * enough to find them all. |
| 161 | */ |
| 162 | for (i = 0; upto < count && i < 20 + count * 2; i++) { |
| 163 | struct bootmeth_uc_plat *ucp; |
| 164 | bool is_global; |
| 165 | |
| 166 | ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, |
| 167 | i, &dev); |
| 168 | if (ret) |
| 169 | continue; |
| 170 | ucp = dev_get_uclass_plat(dev); |
| 171 | is_global = |
| 172 | IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && |
| 173 | (ucp->flags & BOOTMETHF_GLOBAL); |
| 174 | if (pass ? is_global : !is_global) |
| 175 | order[upto++] = dev; |
| 176 | } |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 177 | } |
| 178 | count = upto; |
| 179 | } |
Simon Glass | 23e59ec | 2022-07-30 15:52:18 -0600 | [diff] [blame] | 180 | if (!count) |
| 181 | return log_msg_ret("count2", -ENOENT); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 182 | |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 183 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global && |
| 184 | iter->first_glob_method != -1 && iter->first_glob_method != count) { |
| 185 | iter->cur_method = iter->first_glob_method; |
| 186 | iter->doing_global = true; |
| 187 | } |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 188 | iter->method_order = order; |
| 189 | iter->num_methods = count; |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | int bootmeth_set_order(const char *order_str) |
| 195 | { |
| 196 | struct bootstd_priv *std; |
| 197 | struct udevice **order; |
| 198 | int count, ret, i, len; |
| 199 | const char *s, *p; |
| 200 | |
| 201 | ret = bootstd_get_priv(&std); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | if (!order_str) { |
| 206 | free(std->bootmeth_order); |
| 207 | std->bootmeth_order = NULL; |
| 208 | std->bootmeth_count = 0; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* Create an array large enough */ |
| 213 | count = uclass_id_count(UCLASS_BOOTMETH); |
| 214 | if (!count) |
| 215 | return log_msg_ret("count", -ENOENT); |
| 216 | |
| 217 | order = calloc(count + 1, sizeof(struct udevice *)); |
| 218 | if (!order) |
| 219 | return log_msg_ret("order", -ENOMEM); |
| 220 | |
| 221 | for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) { |
| 222 | struct udevice *dev; |
| 223 | |
| 224 | p = strchrnul(s, ' '); |
| 225 | len = p - s; |
| 226 | ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len, |
| 227 | &dev); |
| 228 | if (ret) { |
| 229 | printf("Unknown bootmeth '%.*s'\n", len, s); |
| 230 | free(order); |
| 231 | return ret; |
| 232 | } |
| 233 | order[i] = dev; |
| 234 | } |
| 235 | order[i] = NULL; |
| 236 | free(std->bootmeth_order); |
| 237 | std->bootmeth_order = order; |
| 238 | std->bootmeth_count = i; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 243 | int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc) |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 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 */ |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 280 | ret2 = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 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 | int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align) |
| 294 | { |
| 295 | void *buf; |
| 296 | uint size; |
| 297 | int ret; |
| 298 | |
| 299 | size = bflow->size; |
| 300 | log_debug(" - script file size %x\n", size); |
| 301 | if (size > size_limit) |
| 302 | return log_msg_ret("chk", -E2BIG); |
| 303 | |
Simon Glass | e6c49fc | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 304 | ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 305 | if (ret) |
| 306 | return log_msg_ret("all", ret); |
| 307 | |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 308 | bflow->state = BOOTFLOWST_READY; |
| 309 | bflow->buf = buf; |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 314 | int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, |
| 315 | void **bufp, uint *sizep) |
| 316 | { |
| 317 | struct blk_desc *desc = NULL; |
| 318 | char path[200]; |
| 319 | loff_t size; |
| 320 | void *buf; |
| 321 | int ret; |
| 322 | |
| 323 | snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname); |
| 324 | log_debug("trying: %s\n", path); |
| 325 | |
| 326 | if (bflow->blk) |
| 327 | desc = dev_get_uclass_plat(bflow->blk); |
| 328 | |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 329 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 330 | if (ret) |
| 331 | return log_msg_ret("fs", ret); |
| 332 | |
| 333 | ret = fs_size(path, &size); |
| 334 | log_debug(" %s - err=%d\n", path, ret); |
| 335 | |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 336 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 337 | if (ret) |
| 338 | return log_msg_ret("fs", ret); |
| 339 | |
Simon Glass | e6c49fc | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 340 | ret = fs_read_alloc(path, size, 0, &buf); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 341 | if (ret) |
| 342 | return log_msg_ret("all", ret); |
| 343 | |
| 344 | *bufp = buf; |
| 345 | *sizep = size; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 350 | int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, |
| 351 | const char *file_path, ulong addr, ulong *sizep) |
| 352 | { |
| 353 | struct blk_desc *desc = NULL; |
| 354 | loff_t len_read; |
| 355 | loff_t size; |
| 356 | int ret; |
| 357 | |
| 358 | if (bflow->blk) |
| 359 | desc = dev_get_uclass_plat(bflow->blk); |
| 360 | |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 361 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 362 | if (ret) |
| 363 | return log_msg_ret("fs", ret); |
| 364 | |
| 365 | ret = fs_size(file_path, &size); |
| 366 | if (ret) |
| 367 | return log_msg_ret("size", ret); |
| 368 | if (size > *sizep) |
| 369 | return log_msg_ret("spc", -ENOSPC); |
| 370 | |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 371 | ret = bootmeth_setup_fs(bflow, desc); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 372 | if (ret) |
| 373 | return log_msg_ret("fs", ret); |
| 374 | |
| 375 | ret = fs_read(file_path, addr, 0, 0, &len_read); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | *sizep = len_read; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | #ifdef CONFIG_BOOTSTD_FULL |
| 384 | /** |
| 385 | * on_bootmeths() - Update the bootmeth order |
| 386 | * |
Simon Glass | 7fbbbc8 | 2023-07-12 09:04:33 -0600 | [diff] [blame] | 387 | * This will check for a valid list of bootmeths and only apply it if valid. |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 388 | */ |
| 389 | static int on_bootmeths(const char *name, const char *value, enum env_op op, |
| 390 | int flags) |
| 391 | { |
| 392 | int ret; |
| 393 | |
| 394 | switch (op) { |
| 395 | case env_op_create: |
| 396 | case env_op_overwrite: |
| 397 | ret = bootmeth_set_order(value); |
| 398 | if (ret) |
| 399 | return 1; |
| 400 | return 0; |
| 401 | case env_op_delete: |
| 402 | bootmeth_set_order(NULL); |
| 403 | fallthrough; |
| 404 | default: |
| 405 | return 0; |
| 406 | } |
| 407 | } |
| 408 | U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths); |
| 409 | #endif /* CONFIG_BOOTSTD_FULL */ |
| 410 | |
| 411 | UCLASS_DRIVER(bootmeth) = { |
| 412 | .id = UCLASS_BOOTMETH, |
| 413 | .name = "bootmeth", |
| 414 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 415 | .per_device_plat_auto = sizeof(struct bootmeth_uc_plat), |
| 416 | }; |