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