summaryrefslogtreecommitdiff
path: root/boot/bootmeth_qfw.c
blob: ecd4b082fd20d3032592845f82125654a51af363 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Bootmethod for QEMU qfw
 *
 * Copyright 2023 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY UCLASS_BOOTSTD

#include <common.h>
#include <command.h>
#include <bootdev.h>
#include <bootflow.h>
#include <bootmeth.h>
#include <env.h>
#include <qfw.h>
#include <dm.h>

static int qfw_check(struct udevice *dev, struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("media=%s\n", media->name);
	if (id == UCLASS_QFW)
		return 0;

	return -ENOTSUPP;
}

static int qfw_read_bootflow(struct udevice *dev, struct bootflow *bflow)
{
	struct udevice *qfw_dev = dev_get_parent(bflow->dev);
	ulong load, initrd;
	int ret;

	load = env_get_hex("kernel_addr_r", 0);
	initrd = env_get_hex("ramdisk_addr_r", 0);
	log_debug("setup kernel %s %lx %lx\n", qfw_dev->name, load, initrd);
	bflow->name = strdup("qfw");
	if (!bflow->name)
		return log_msg_ret("name", -ENOMEM);

	ret = qemu_fwcfg_setup_kernel(qfw_dev, load, initrd);
	log_debug("setup kernel result %d\n", ret);
	if (ret)
		return log_msg_ret("cmd", -EIO);

	bflow->state = BOOTFLOWST_READY;

	return 0;
}

static int qfw_read_file(struct udevice *dev, struct bootflow *bflow,
			 const char *file_path, ulong addr, ulong *sizep)
{
	return -ENOSYS;
}

static int qfw_boot(struct udevice *dev, struct bootflow *bflow)
{
	int ret;

	ret = run_command("booti ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdtcontroladdr}",
			  0);
	if (ret) {
		ret = run_command("bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} "
				  "${fdtcontroladdr}", 0);
	}

	return ret ? -EIO : 0;
}

static int qfw_bootmeth_bind(struct udevice *dev)
{
	struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);

	plat->desc = "Sandbox boot for testing";

	return 0;
}

static struct bootmeth_ops qfw_bootmeth_ops = {
	.check		= qfw_check,
	.read_bootflow	= qfw_read_bootflow,
	.read_file	= qfw_read_file,
	.boot		= qfw_boot,
};

static const struct udevice_id qfw_bootmeth_ids[] = {
	{ .compatible = "u-boot,qfw-extlinux" },
	{ }
};

U_BOOT_DRIVER(bootmeth_qfw) = {
	.name		= "bootmeth_qfw",
	.id		= UCLASS_BOOTMETH,
	.of_match	= qfw_bootmeth_ids,
	.ops		= &qfw_bootmeth_ops,
	.bind		= qfw_bootmeth_bind,
};