blob: b4126d8d2d0b32247ac1a6b9490581b277b26ab3 [file] [log] [blame]
Simon Glass39db1992022-10-20 18:23:11 -06001// 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 Glass39db1992022-10-20 18:23:11 -060011#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
18int vbe_simple_fixup_node(ofnode node, struct simple_state *state)
19{
Simon Glassa6a06cc2022-10-20 18:23:12 -060020 const char *version, *str;
Simon Glass39db1992022-10-20 18:23:11 -060021 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 Glassa6a06cc2022-10-20 18:23:12 -060033
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 Glass39db1992022-10-20 18:23:11 -060039 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 */
52static 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 Glassa9289612022-10-20 18:23:14 -060064 ofnode node, subnode, chosen;
Simon Glass39db1992022-10-20 18:23:11 -060065 int ret;
66
67 if (strcmp("vbe_simple", dev->driver->name))
68 continue;
69
Simon Glassa9289612022-10-20 18:23:14 -060070 /* Check if there is a node to fix up, adding if not */
71 chosen = oftree_path(tree, "/chosen");
72 if (!ofnode_valid(chosen))
Simon Glass39db1992022-10-20 18:23:11 -060073 continue;
Simon Glasse3aa2cf2023-01-12 16:48:54 -070074
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 Glassa9289612022-10-20 18:23:14 -060086 ret = ofnode_add_subnode(chosen, "fwupd", &node);
87 if (ret && ret != -EEXIST)
88 return log_msg_ret("fwu", ret);
Simon Glass39db1992022-10-20 18:23:11 -060089
Simon Glassa9289612022-10-20 18:23:14 -060090 ret = ofnode_add_subnode(node, dev->name, &subnode);
91 if (ret && ret != -EEXIST)
92 return log_msg_ret("dev", ret);
93
Simon Glassa9289612022-10-20 18:23:14 -060094 /* Copy over the vbe properties for fwupd */
95 log_debug("Fixing up: %s\n", dev->name);
Simon Glass68164892023-09-26 08:14:37 -060096 ret = ofnode_copy_props(subnode, dev_ofnode(dev));
Simon Glassa9289612022-10-20 18:23:14 -060097 if (ret)
98 return log_msg_ret("cp", ret);
99
Simon Glassbc993e52022-10-20 18:23:13 -0600100 ret = vbe_simple_read_state(dev, &state);
Simon Glass39db1992022-10-20 18:23:11 -0600101 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 Glassa41134f2023-08-21 21:16:57 -0600111EVENT_SPY_FULL(EVT_FT_FIXUP, bootmeth_vbe_simple_ft_fixup);