blob: fbb05ef928eba5880378b8a89749816ab58ee030 [file] [log] [blame]
Simon Glassad8ec372022-04-24 23:31:13 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassb71d7f72023-05-10 16:34:46 -06003 * Bootmethod for extlinux boot from a block device
Simon Glassad8ec372022-04-24 23:31:13 -06004 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
Simon Glassad8ec372022-04-24 23:31:13 -060011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
14#include <bootstd.h>
15#include <command.h>
Simon Glassad8ec372022-04-24 23:31:13 -060016#include <dm.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060017#include <extlinux.h>
Simon Glassad8ec372022-04-24 23:31:13 -060018#include <fs.h>
19#include <malloc.h>
20#include <mapmem.h>
21#include <mmc.h>
22#include <pxe_utils.h>
23
Simon Glassb71d7f72023-05-10 16:34:46 -060024static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
Simon Glassf6d71a82022-07-30 15:52:19 -060025{
26 if (IS_ENABLED(CONFIG_SANDBOX)) {
27 int len;
28
29 len = snprintf(buf, maxsize, "OK");
30
31 return len + 1 < maxsize ? 0 : -ENOSPC;
32 }
33
34 return 0;
35}
36
Simon Glassb71d7f72023-05-10 16:34:46 -060037static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
38 char *file_addr, ulong *sizep)
Simon Glassad8ec372022-04-24 23:31:13 -060039{
Simon Glassb71d7f72023-05-10 16:34:46 -060040 struct extlinux_info *info = ctx->userdata;
Simon Glassad8ec372022-04-24 23:31:13 -060041 ulong addr;
42 int ret;
43
44 addr = simple_strtoul(file_addr, NULL, 16);
45
46 /* Allow up to 1GB */
47 *sizep = 1 << 30;
48 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
49 sizep);
50 if (ret)
51 return log_msg_ret("read", ret);
52
53 return 0;
54}
55
Simon Glassb71d7f72023-05-10 16:34:46 -060056static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glassad8ec372022-04-24 23:31:13 -060057{
58 int ret;
59
60 /* This only works on block devices */
Simon Glass18c50402023-01-17 10:47:54 -070061 ret = bootflow_iter_check_blk(iter);
Simon Glassad8ec372022-04-24 23:31:13 -060062 if (ret)
63 return log_msg_ret("blk", ret);
64
65 return 0;
66}
67
Simon Glass72b7b192023-01-06 08:52:33 -060068/**
Simon Glassb71d7f72023-05-10 16:34:46 -060069 * extlinux_fill_info() - Decode the extlinux file to find out its info
Simon Glass72b7b192023-01-06 08:52:33 -060070 *
71 * @bflow: Bootflow to process
72 * @return 0 if OK, -ve on error
73 */
Simon Glassb71d7f72023-05-10 16:34:46 -060074static int extlinux_fill_info(struct bootflow *bflow)
Simon Glass72b7b192023-01-06 08:52:33 -060075{
76 struct membuff mb;
77 char line[200];
78 char *data;
79 int len;
80
81 log_debug("parsing bflow file size %x\n", bflow->size);
82 membuff_init(&mb, bflow->buf, bflow->size);
83 membuff_putraw(&mb, bflow->size, true, &data);
Ion Agorria7e06c1f2024-01-05 09:22:10 +020084 while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
Simon Glass72b7b192023-01-06 08:52:33 -060085 char *tok, *p = line;
86
87 tok = strsep(&p, " ");
88 if (p) {
89 if (!strcmp("label", tok)) {
90 bflow->os_name = strdup(p);
91 if (!bflow->os_name)
92 return log_msg_ret("os", -ENOMEM);
93 }
94 }
95 }
96
97 return 0;
98}
99
Simon Glassb71d7f72023-05-10 16:34:46 -0600100static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600101{
102 struct blk_desc *desc;
103 const char *const *prefixes;
104 struct udevice *bootstd;
105 const char *prefix;
106 loff_t size;
107 int ret, i;
108
109 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
110 if (ret)
111 return log_msg_ret("std", ret);
112
113 /* If a block device, we require a partition table */
114 if (bflow->blk && !bflow->part)
115 return -ENOENT;
116
117 prefixes = bootstd_get_prefixes(bootstd);
118 i = 0;
119 desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
120 do {
121 prefix = prefixes ? prefixes[i] : NULL;
122
Simon Glassb71d7f72023-05-10 16:34:46 -0600123 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
Simon Glassad8ec372022-04-24 23:31:13 -0600124 } while (ret && prefixes && prefixes[++i]);
125 if (ret)
126 return log_msg_ret("try", ret);
127 size = bflow->size;
128
129 ret = bootmeth_alloc_file(bflow, 0x10000, 1);
130 if (ret)
131 return log_msg_ret("read", ret);
132
Simon Glassb71d7f72023-05-10 16:34:46 -0600133 ret = extlinux_fill_info(bflow);
Simon Glass72b7b192023-01-06 08:52:33 -0600134 if (ret)
135 return log_msg_ret("inf", ret);
136
Simon Glassad8ec372022-04-24 23:31:13 -0600137 return 0;
138}
139
Simon Glassb71d7f72023-05-10 16:34:46 -0600140static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600141{
142 struct cmd_tbl cmdtp = {}; /* dummy */
143 struct pxe_context ctx;
Simon Glassb71d7f72023-05-10 16:34:46 -0600144 struct extlinux_info info;
Simon Glassad8ec372022-04-24 23:31:13 -0600145 ulong addr;
146 int ret;
147
148 addr = map_to_sysmem(bflow->buf);
149 info.dev = dev;
150 info.bflow = bflow;
Simon Glassb71d7f72023-05-10 16:34:46 -0600151 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
Jonas Karlmanf99af492023-06-09 14:59:01 +0000152 bflow->fname, false);
Simon Glassad8ec372022-04-24 23:31:13 -0600153 if (ret)
154 return log_msg_ret("ctx", -EINVAL);
155
156 ret = pxe_process(&ctx, addr, false);
157 if (ret)
158 return log_msg_ret("bread", -EINVAL);
159
160 return 0;
161}
162
Simon Glassb71d7f72023-05-10 16:34:46 -0600163static int extlinux_bootmeth_bind(struct udevice *dev)
Simon Glassad8ec372022-04-24 23:31:13 -0600164{
165 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
166
167 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
Simon Glassb71d7f72023-05-10 16:34:46 -0600168 "Extlinux boot from a block device" : "extlinux";
Simon Glassad8ec372022-04-24 23:31:13 -0600169
170 return 0;
171}
172
Simon Glassb71d7f72023-05-10 16:34:46 -0600173static struct bootmeth_ops extlinux_bootmeth_ops = {
174 .get_state_desc = extlinux_get_state_desc,
175 .check = extlinux_check,
176 .read_bootflow = extlinux_read_bootflow,
Simon Glassad8ec372022-04-24 23:31:13 -0600177 .read_file = bootmeth_common_read_file,
Simon Glassb71d7f72023-05-10 16:34:46 -0600178 .boot = extlinux_boot,
Simon Glassad8ec372022-04-24 23:31:13 -0600179};
180
Simon Glassb71d7f72023-05-10 16:34:46 -0600181static const struct udevice_id extlinux_bootmeth_ids[] = {
182 { .compatible = "u-boot,extlinux" },
Simon Glassad8ec372022-04-24 23:31:13 -0600183 { }
184};
185
Simon Glasse5cb0182024-07-17 09:30:59 +0100186/* Put a number before 'extlinux' to provide a default ordering */
Simon Glass2f27e472023-08-19 16:49:35 -0600187U_BOOT_DRIVER(bootmeth_1extlinux) = {
Simon Glassb71d7f72023-05-10 16:34:46 -0600188 .name = "bootmeth_extlinux",
Simon Glassad8ec372022-04-24 23:31:13 -0600189 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600190 .of_match = extlinux_bootmeth_ids,
191 .ops = &extlinux_bootmeth_ops,
192 .bind = extlinux_bootmeth_bind,
Simon Glassad8ec372022-04-24 23:31:13 -0600193};