Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Verified Boot for Embedded (VBE) loading firmware phases |
| 4 | * |
| 5 | * Copyright 2022 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY LOGC_BOOT |
| 10 | |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <bootflow.h> |
| 13 | #include <vbe.h> |
| 14 | #include <version_string.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include "vbe_simple.h" |
| 17 | |
| 18 | int vbe_simple_fixup_node(ofnode node, struct simple_state *state) |
| 19 | { |
Simon Glass | a6a06cc | 2022-10-20 18:23:12 -0600 | [diff] [blame] | 20 | const char *version, *str; |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 21 | int ret; |
| 22 | |
| 23 | version = strdup(state->fw_version); |
| 24 | if (!version) |
| 25 | return log_msg_ret("dup", -ENOMEM); |
| 26 | |
| 27 | ret = ofnode_write_string(node, "cur-version", version); |
| 28 | if (ret) |
| 29 | return log_msg_ret("ver", ret); |
| 30 | ret = ofnode_write_u32(node, "cur-vernum", state->fw_vernum); |
| 31 | if (ret) |
| 32 | return log_msg_ret("num", ret); |
Simon Glass | a6a06cc | 2022-10-20 18:23:12 -0600 | [diff] [blame] | 33 | |
| 34 | /* Drop the 'U-Boot ' at the start */ |
| 35 | str = version_string; |
| 36 | if (!strncmp("U-Boot ", str, 7)) |
| 37 | str += 7; |
| 38 | ret = ofnode_write_string(node, "bootloader-version", str); |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 39 | if (ret) |
| 40 | return log_msg_ret("bl", ret); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * bootmeth_vbe_simple_ft_fixup() - Write out all VBE simple data to the DT |
| 47 | * |
| 48 | * @ctx: Context for event |
| 49 | * @event: Event to process |
| 50 | * @return 0 if OK, -ve on error |
| 51 | */ |
| 52 | static int bootmeth_vbe_simple_ft_fixup(void *ctx, struct event *event) |
| 53 | { |
| 54 | oftree tree = event->data.ft_fixup.tree; |
| 55 | struct udevice *dev; |
| 56 | |
| 57 | /* |
| 58 | * Ideally we would have driver model support for fixups, but that does |
| 59 | * not exist yet. It is a step too far to try to do this before VBE is |
| 60 | * in place. |
| 61 | */ |
| 62 | for (vbe_find_first_device(&dev); dev; vbe_find_next_device(&dev)) { |
| 63 | struct simple_state state; |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 64 | ofnode node, subnode, chosen; |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 65 | int ret; |
| 66 | |
| 67 | if (strcmp("vbe_simple", dev->driver->name)) |
| 68 | continue; |
| 69 | |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 70 | /* Check if there is a node to fix up, adding if not */ |
| 71 | chosen = oftree_path(tree, "/chosen"); |
| 72 | if (!ofnode_valid(chosen)) |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 73 | continue; |
Simon Glass | e3aa2cf | 2023-01-12 16:48:54 -0700 | [diff] [blame] | 74 | |
| 75 | ret = device_probe(dev); |
| 76 | if (ret) { |
| 77 | /* |
| 78 | * This should become an error when VBE is updated to |
| 79 | * only bind this device when a node exists |
| 80 | */ |
| 81 | log_debug("VBE device '%s' failed to probe (err=%d)", |
| 82 | dev->name, ret); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 86 | ret = ofnode_add_subnode(chosen, "fwupd", &node); |
| 87 | if (ret && ret != -EEXIST) |
| 88 | return log_msg_ret("fwu", ret); |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 89 | |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 90 | ret = ofnode_add_subnode(node, dev->name, &subnode); |
| 91 | if (ret && ret != -EEXIST) |
| 92 | return log_msg_ret("dev", ret); |
| 93 | |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 94 | /* Copy over the vbe properties for fwupd */ |
| 95 | log_debug("Fixing up: %s\n", dev->name); |
Simon Glass | 6816489 | 2023-09-26 08:14:37 -0600 | [diff] [blame] | 96 | ret = ofnode_copy_props(subnode, dev_ofnode(dev)); |
Simon Glass | a928961 | 2022-10-20 18:23:14 -0600 | [diff] [blame] | 97 | if (ret) |
| 98 | return log_msg_ret("cp", ret); |
| 99 | |
Simon Glass | bc993e5 | 2022-10-20 18:23:13 -0600 | [diff] [blame] | 100 | ret = vbe_simple_read_state(dev, &state); |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 101 | if (ret) |
| 102 | return log_msg_ret("read", ret); |
| 103 | |
| 104 | ret = vbe_simple_fixup_node(subnode, &state); |
| 105 | if (ret) |
| 106 | return log_msg_ret("fix", ret); |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
Simon Glass | a41134f | 2023-08-21 21:16:57 -0600 | [diff] [blame] | 111 | EVENT_SPY_FULL(EVT_FT_FIXUP, bootmeth_vbe_simple_ft_fixup); |