blob: 50ded055f3fe514639be2ee9a2ff6c05b7ec91ef [file] [log] [blame]
Simon Glassa950d312022-04-24 23:31:08 -06001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#ifndef __bootmeth_h
8#define __bootmeth_h
9
10struct blk_desc;
11struct bootflow;
12struct bootflow_iter;
13struct udevice;
14
15/**
Simon Glassbc06aa02022-07-30 15:52:21 -060016 * enum bootmeth_flags - Flags for bootmeths
17 *
18 * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically
19 */
20enum bootmeth_flags {
21 BOOTMETHF_GLOBAL = BIT(0),
22};
23
24/**
Simon Glassa950d312022-04-24 23:31:08 -060025 * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth
26 *
27 * @desc: A long description of the bootmeth
Simon Glassbc06aa02022-07-30 15:52:21 -060028 * @flags: Flags for this bootmeth (enum bootmeth_flags)
Simon Glassa950d312022-04-24 23:31:08 -060029 */
30struct bootmeth_uc_plat {
31 const char *desc;
Simon Glassbc06aa02022-07-30 15:52:21 -060032 int flags;
Simon Glassa950d312022-04-24 23:31:08 -060033};
34
35/** struct bootmeth_ops - Operations for boot methods */
36struct bootmeth_ops {
37 /**
Simon Glass988caca2022-07-30 15:52:19 -060038 * get_state_desc() - get detailed state information
39 *
40 * Prodecues a textual description of the state of the bootmeth. This
41 * can include newline characters if it extends to multiple lines. It
42 * must be a nul-terminated string.
43 *
44 * This may involve reading state from the system, e.g. some data in
45 * the firmware area.
46 *
47 * @dev: Bootmethod device to check
48 * @buf: Buffer to place the info in (terminator must fit)
49 * @maxsize: Size of buffer
50 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
51 * something else went wrong
52 */
53 int (*get_state_desc)(struct udevice *dev, char *buf, int maxsize);
54
55 /**
56 * check_supported() - check if a bootmeth supports this bootdev
Simon Glassa950d312022-04-24 23:31:08 -060057 *
58 * This is optional. If not provided, the bootdev is assumed to be
59 * supported
60 *
61 * The bootmeth can check the bootdev (e.g. to make sure it is a
62 * network device) or the partition information. The following fields
63 * in @iter are available:
64 *
65 * name, dev, state, part
66 * max_part may be set if part != 0 (i.e. there is a valid partition
67 * table). Otherwise max_part is 0
68 * method is available but is the same as @dev
69 * the partition has not yet been read, nor has the filesystem been
70 * checked
71 *
72 * It may update only the flags in @iter
73 *
74 * @dev: Bootmethod device to check against
75 * @iter: On entry, provides bootdev, hwpart, part
76 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
77 */
78 int (*check)(struct udevice *dev, struct bootflow_iter *iter);
79
80 /**
81 * read_bootflow() - read a bootflow for a device
82 *
83 * @dev: Bootmethod device to use
84 * @bflow: On entry, provides dev, hwpart, part and method.
85 * Returns updated bootflow if found
86 * Return: 0 if OK, -ve on error
87 */
88 int (*read_bootflow)(struct udevice *dev, struct bootflow *bflow);
89
90 /**
91 * read_file() - read a file needed for a bootflow
92 *
93 * Read a file from the same place as the bootflow came from
94 *
95 * @dev: Bootmethod device to use
96 * @bflow: Bootflow providing info on where to read from
97 * @file_path: Path to file (may be absolute or relative)
98 * @addr: Address to load file
99 * @sizep: On entry provides the maximum permitted size; on exit
100 * returns the size of the file
101 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
102 * -ve value if something else goes wrong
103 */
104 int (*read_file)(struct udevice *dev, struct bootflow *bflow,
105 const char *file_path, ulong addr, ulong *sizep);
106
107 /**
108 * boot() - boot a bootflow
109 *
110 * @dev: Bootmethod device to boot
111 * @bflow: Bootflow to boot
112 * Return: does not return on success, since it should boot the
113 * Operating Systemn. Returns -EFAULT if that fails, -ENOTSUPP if
114 * trying method resulted in finding out that is not actually
115 * supported for this boot and should not be tried again unless
116 * something changes, other -ve on other error
117 */
118 int (*boot)(struct udevice *dev, struct bootflow *bflow);
119};
120
121#define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
122
123/**
Simon Glass988caca2022-07-30 15:52:19 -0600124 * bootmeth_get_state_desc() - get detailed state information
125 *
126 * Prodecues a textual description of the state of the bootmeth. This
127 * can include newline characters if it extends to multiple lines. It
128 * must be a nul-terminated string.
129 *
130 * This may involve reading state from the system, e.g. some data in
131 * the firmware area.
132 *
133 * @dev: Bootmethod device to check
134 * @buf: Buffer to place the info in (terminator must fit)
135 * @maxsize: Size of buffer
136 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
137 * something else went wrong
138 */
139int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize);
140
141/**
Simon Glassa950d312022-04-24 23:31:08 -0600142 * bootmeth_check() - check if a bootmeth supports this bootflow
143 *
144 * This is optional. If not provided, the bootdev is assumed to be
145 * supported
146 *
147 * The bootmeth can check the bootdev (e.g. to make sure it is a
148 * network device) or the partition information. The following fields
149 * in @iter are available:
150 *
151 * name, dev, state, part
152 * max_part may be set if part != 0 (i.e. there is a valid partition
153 * table). Otherwise max_part is 0
154 * method is available but is the same as @dev
155 * the partition has not yet been read, nor has the filesystem been
156 * checked
157 *
158 * It may update only the flags in @iter
159 *
160 * @dev: Bootmethod device to check against
161 * @iter: On entry, provides bootdev, hwpart, part
162 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
163 */
164int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter);
165
166/**
167 * bootmeth_read_bootflow() - set up a bootflow for a device
168 *
169 * @dev: Bootmethod device to check
170 * @bflow: On entry, provides dev, hwpart, part and method.
171 * Returns updated bootflow if found
172 * Return: 0 if OK, -ve on error
173 */
174int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow);
175
176/**
177 * bootmeth_read_file() - read a file needed for a bootflow
178 *
179 * Read a file from the same place as the bootflow came from
180 *
181 * @dev: Bootmethod device to use
182 * @bflow: Bootflow providing info on where to read from
183 * @file_path: Path to file (may be absolute or relative)
184 * @addr: Address to load file
185 * @sizep: On entry provides the maximum permitted size; on exit
186 * returns the size of the file
187 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
188 * -ve value if something else goes wrong
189 */
190int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
191 const char *file_path, ulong addr, ulong *sizep);
192
193/**
194 * bootmeth_boot() - boot a bootflow
195 *
196 * @dev: Bootmethod device to boot
197 * @bflow: Bootflow to boot
198 * Return: does not return on success, since it should boot the
199 * Operating Systemn. Returns -EFAULT if that fails, other -ve on
200 * other error
201 */
202int bootmeth_boot(struct udevice *dev, struct bootflow *bflow);
203
204/**
205 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
206 *
207 * This sets up the ordering information in @iter, based on the selected
208 * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
209 * ordering there, then all bootmethods are added
210 *
211 * @iter: Iterator to update with the order
Simon Glassc627cfc2022-07-30 15:52:27 -0600212 * @include_global: true to add the global bootmeths, in which case they appear
213 * first
Simon Glassa950d312022-04-24 23:31:08 -0600214 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
215 * on other error
216 */
Simon Glassc627cfc2022-07-30 15:52:27 -0600217int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global);
Simon Glassa950d312022-04-24 23:31:08 -0600218
219/**
220 * bootmeth_set_order() - Set the bootmeth order
221 *
222 * This selects the ordering to use for bootmeths
223 *
224 * @order_str: String containing the ordering. This is a comma-separate list of
225 * bootmeth-device names, e.g. "syslinux,efi". If empty then a default ordering
226 * is used, based on the sequence number of devices (i.e. using aliases)
227 * Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if
228 * out of memory, -ENOENT if there are no bootmeth devices
229 */
230int bootmeth_set_order(const char *order_str);
231
232/**
233 * bootmeth_try_file() - See we can access a given file
234 *
235 * Check for a file with a given name. If found, the filename is allocated in
236 * @bflow
237 *
238 * Sets the state to BOOTFLOWST_FILE on success. It also calls
239 * fs_set_blk_dev_with_part() so that this does not need to be done by the
240 * caller before reading the file.
241 *
242 * @bflow: Information about file to try
243 * @desc: Block descriptor to read from
244 * @prefix: Filename prefix to prepend to @fname (NULL for none)
245 * @fname: Filename to read
246 * Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname,
247 * other -ve value on other error
248 */
249int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
250 const char *prefix, const char *fname);
251
252/**
253 * bootmeth_alloc_file() - Allocate and read a bootflow file
254 *
255 * Allocates memory for a bootflow file and reads it in. Sets the state to
256 * BOOTFLOWST_READY on success
257 *
258 * Note that fs_set_blk_dev_with_part() must have been called previously.
259 *
260 * @bflow: Information about file to read
261 * @size_limit: Maximum file size to permit
262 * @align: Allocation alignment (1 for unaligned)
263 * Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory,
264 * other -ve on other error
265 */
266int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align);
267
268/**
269 * bootmeth_common_read_file() - Common handler for reading a file
270 *
271 * Reads a named file from the same location as the bootflow file.
272 *
273 * @dev: bootmeth device to read from
274 * @bflow: Bootflow information
275 * @file_path: Path to file
276 * @addr: Address to load file to
277 * @sizep: On entry, the maximum file size to accept, on exit the actual file
278 * size read
279 */
280int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
281 const char *file_path, ulong addr, ulong *sizep);
282
Simon Glassbc06aa02022-07-30 15:52:21 -0600283/**
284 * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth
285 *
286 * Check the bootmeth for a bootflow which can be used. In this case the
287 * bootmeth handles all bootdev selection, etc.
288 *
289 * @dev: bootmeth device to read from
290 * @bflow: Bootflow information
291 * @return 0 on success, -ve if a bootflow could not be found or had an error
292 */
293int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow);
294
Simon Glassa950d312022-04-24 23:31:08 -0600295#endif