summaryrefslogtreecommitdiff
path: root/boot/vbe_simple_os.c
blob: 8c641ec07e2d8ed2b324279540e324860c876e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: GPL-2.0
/*
 * Verified Boot for Embedded (VBE) loading firmware phases
 *
 * Copyright 2022 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY LOGC_BOOT

#include <common.h>
#include <dm.h>
#include <bootflow.h>
#include <vbe.h>
#include <version_string.h>
#include <dm/device-internal.h>
#include "vbe_simple.h"

int vbe_simple_fixup_node(ofnode node, struct simple_state *state)
{
	const char *version, *str;
	int ret;

	version = strdup(state->fw_version);
	if (!version)
		return log_msg_ret("dup", -ENOMEM);

	ret = ofnode_write_string(node, "cur-version", version);
	if (ret)
		return log_msg_ret("ver", ret);
	ret = ofnode_write_u32(node, "cur-vernum", state->fw_vernum);
	if (ret)
		return log_msg_ret("num", ret);

	/* Drop the 'U-Boot ' at the start */
	str = version_string;
	if (!strncmp("U-Boot ", str, 7))
		str += 7;
	ret = ofnode_write_string(node, "bootloader-version", str);
	if (ret)
		return log_msg_ret("bl", ret);

	return 0;
}

/**
 * bootmeth_vbe_simple_ft_fixup() - Write out all VBE simple data to the DT
 *
 * @ctx: Context for event
 * @event: Event to process
 * @return 0 if OK, -ve on error
 */
static int bootmeth_vbe_simple_ft_fixup(void *ctx, struct event *event)
{
	oftree tree = event->data.ft_fixup.tree;
	struct udevice *dev;

	/*
	 * Ideally we would have driver model support for fixups, but that does
	 * not exist yet. It is a step too far to try to do this before VBE is
	 * in place.
	 */
	for (vbe_find_first_device(&dev); dev; vbe_find_next_device(&dev)) {
		struct simple_state state;
		ofnode node, subnode, chosen;
		int ret;

		if (strcmp("vbe_simple", dev->driver->name))
			continue;

		/* Check if there is a node to fix up, adding if not */
		chosen = oftree_path(tree, "/chosen");
		if (!ofnode_valid(chosen))
			continue;

		ret = device_probe(dev);
		if (ret) {
			/*
			 * This should become an error when VBE is updated to
			 * only bind this device when a node exists
			 */
			log_debug("VBE device '%s' failed to probe (err=%d)",
				  dev->name, ret);
			return 0;
		}

		ret = ofnode_add_subnode(chosen, "fwupd", &node);
		if (ret && ret != -EEXIST)
			return log_msg_ret("fwu", ret);

		ret = ofnode_add_subnode(node, dev->name, &subnode);
		if (ret && ret != -EEXIST)
			return log_msg_ret("dev", ret);

		/* Copy over the vbe properties for fwupd */
		log_debug("Fixing up: %s\n", dev->name);
		ret = ofnode_copy_props(dev_ofnode(dev), subnode);
		if (ret)
			return log_msg_ret("cp", ret);

		ret = vbe_simple_read_state(dev, &state);
		if (ret)
			return log_msg_ret("read", ret);

		ret = vbe_simple_fixup_node(subnode, &state);
		if (ret)
			return log_msg_ret("fix", ret);
	}

	return 0;
}
EVENT_SPY(EVT_FT_FIXUP, bootmeth_vbe_simple_ft_fixup);