blob: ed997008c4ed7d49e605c9b6fab5dfee439307df [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafe83be452016-03-04 01:10:02 +01002/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafe83be452016-03-04 01:10:02 +01006 */
7
Heinrich Schuchardt37240572020-07-17 20:33:05 +02008#define LOG_CATEGORY LOGC_EFI
9
Simon Glasse3dfa912016-05-01 13:52:32 -060010#include <blk.h>
Simon Glass302d4f82016-05-14 14:03:05 -060011#include <dm.h>
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +090012#include <dm/device-internal.h>
13#include <dm/tag.h>
14#include <event.h>
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +020015#include <efi_driver.h>
Alexander Grafe83be452016-03-04 01:10:02 +010016#include <efi_loader.h>
AKASHI Takahiro120a01a2019-10-07 14:59:38 +090017#include <fs.h>
Heinrich Schuchardt37240572020-07-17 20:33:05 +020018#include <log.h>
Alexander Grafe83be452016-03-04 01:10:02 +010019#include <part.h>
20#include <malloc.h>
21
Heinrich Schuchardt0397d812022-10-21 08:33:44 +020022struct efi_system_partition efi_system_partition = {
23 .uclass_id = UCLASS_INVALID,
24};
Heinrich Schuchardta9931732020-03-19 15:16:31 +010025
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020026const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Heinrich Schuchardt26397622021-02-02 17:53:14 +010027const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Alexander Grafe83be452016-03-04 01:10:02 +010028
Heinrich Schuchardt72928722018-09-26 05:27:56 +020029/**
30 * struct efi_disk_obj - EFI disk object
31 *
32 * @header: EFI object header
33 * @ops: EFI disk I/O protocol interface
Heinrich Schuchardt72928722018-09-26 05:27:56 +020034 * @dev_index: device index of block device
35 * @media: block I/O media information
36 * @dp: device path to the block device
37 * @part: partition
38 * @volume: simple file system protocol of the partition
AKASHI Takahiro808ece12022-04-19 10:05:17 +090039 * @dev: associated DM device
Heinrich Schuchardt72928722018-09-26 05:27:56 +020040 */
Alexander Grafe83be452016-03-04 01:10:02 +010041struct efi_disk_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020042 struct efi_object header;
Alexander Grafe83be452016-03-04 01:10:02 +010043 struct efi_block_io ops;
Alexander Grafe83be452016-03-04 01:10:02 +010044 int dev_index;
Alexander Grafe83be452016-03-04 01:10:02 +010045 struct efi_block_io_media media;
Rob Clarka83272f2017-09-13 18:05:31 -040046 struct efi_device_path *dp;
Rob Clarka83272f2017-09-13 18:05:31 -040047 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040048 struct efi_simple_file_system_protocol *volume;
Alexander Grafe83be452016-03-04 01:10:02 +010049};
50
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020051/**
52 * efi_disk_reset() - reset block device
53 *
54 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
55 *
56 * As U-Boot's block devices do not have a reset function simply return
57 * EFI_SUCCESS.
58 *
59 * See the Unified Extensible Firmware Interface (UEFI) specification for
60 * details.
61 *
62 * @this: pointer to the BLOCK_IO_PROTOCOL
63 * @extended_verification: extended verification
64 * Return: status code
65 */
Alexander Grafe83be452016-03-04 01:10:02 +010066static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
67 char extended_verification)
68{
69 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020070 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafe83be452016-03-04 01:10:02 +010071}
72
AKASHI Takahiroba647af2022-05-12 11:29:01 +090073/**
74 * efi_disk_is_removable() - check if the device is removable media
75 * @handle: efi object handle;
76 *
77 * Examine the device and determine if the device is a local block device
78 * and removable media.
79 *
80 * Return: true if removable, false otherwise
81 */
82bool efi_disk_is_removable(efi_handle_t handle)
83{
84 struct efi_handler *handler;
85 struct efi_block_io *io;
86 efi_status_t ret;
87
88 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
89 if (ret != EFI_SUCCESS)
90 return false;
91
92 io = handler->protocol_interface;
93
94 if (!io || !io->media)
95 return false;
96
97 return (bool)io->media->removable_media;
98}
99
Alexander Grafe83be452016-03-04 01:10:02 +0100100enum efi_disk_direction {
101 EFI_DISK_READ,
102 EFI_DISK_WRITE,
103};
104
Heinrich Schuchardt9ce06172017-08-26 22:33:13 +0200105static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +0100106 u32 media_id, u64 lba, unsigned long buffer_size,
107 void *buffer, enum efi_disk_direction direction)
108{
109 struct efi_disk_obj *diskobj;
Alexander Grafe83be452016-03-04 01:10:02 +0100110 int blksz;
111 int blocks;
112 unsigned long n;
113
Alexander Grafe83be452016-03-04 01:10:02 +0100114 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahiro808ece12022-04-19 10:05:17 +0900115 blksz = diskobj->media.block_size;
Alexander Grafe83be452016-03-04 01:10:02 +0100116 blocks = buffer_size / blksz;
117
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +0200118 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
119 blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +0100120
121 /* We only support full block access */
122 if (buffer_size & (blksz - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200123 return EFI_BAD_BUFFER_SIZE;
Alexander Grafe83be452016-03-04 01:10:02 +0100124
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900125 if (CONFIG_IS_ENABLED(PARTITIONS) &&
Masahisa Kojimac9611082022-07-22 11:39:10 +0900126 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900127 if (direction == EFI_DISK_READ)
Simon Glass4e93eea2022-10-20 18:22:52 -0600128 n = disk_blk_read(diskobj->header.dev, lba, blocks,
129 buffer);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900130 else
Simon Glass4e93eea2022-10-20 18:22:52 -0600131 n = disk_blk_write(diskobj->header.dev, lba, blocks,
132 buffer);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900133 } else {
134 /* dev is a block device (UCLASS_BLK) */
135 struct blk_desc *desc;
AKASHI Takahiro808ece12022-04-19 10:05:17 +0900136
Masahisa Kojimac9611082022-07-22 11:39:10 +0900137 desc = dev_get_uclass_plat(diskobj->header.dev);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900138 if (direction == EFI_DISK_READ)
139 n = blk_dread(desc, lba, blocks, buffer);
140 else
141 n = blk_dwrite(desc, lba, blocks, buffer);
142 }
Alexander Grafe83be452016-03-04 01:10:02 +0100143
144 /* We don't do interrupts, so check for timers cooperatively */
145 efi_timer_check();
146
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +0200147 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Graf62a67482016-06-02 11:38:27 +0200148
Alexander Grafe83be452016-03-04 01:10:02 +0100149 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -0400150 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +0100151
Rob Clark99c43872017-07-25 20:28:29 -0400152 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100153}
154
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200155/**
156 * efi_disk_read_blocks() - reads blocks from device
157 *
158 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
159 *
160 * See the Unified Extensible Firmware Interface (UEFI) specification for
161 * details.
162 *
163 * @this: pointer to the BLOCK_IO_PROTOCOL
164 * @media_id: id of the medium to be read from
165 * @lba: starting logical block for reading
166 * @buffer_size: size of the read buffer
167 * @buffer: pointer to the destination buffer
168 * Return: status code
169 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600170static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100171 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100172 void *buffer)
173{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200174 void *real_buffer = buffer;
175 efi_status_t r;
176
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200177 if (!this)
178 return EFI_INVALID_PARAMETER;
179 /* TODO: check for media changes */
180 if (media_id != this->media->media_id)
181 return EFI_MEDIA_CHANGED;
182 if (!this->media->media_present)
183 return EFI_NO_MEDIA;
Heinrich Schuchardt0bb73892021-01-23 19:33:11 +0100184 /* media->io_align is a power of 2 or 0 */
185 if (this->media->io_align &&
186 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200187 return EFI_INVALID_PARAMETER;
188 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsenf1424932021-02-09 17:17:17 +0100189 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200190 return EFI_INVALID_PARAMETER;
191
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200192#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
193 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
194 r = efi_disk_read_blocks(this, media_id, lba,
195 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
196 if (r != EFI_SUCCESS)
197 return r;
198 return efi_disk_read_blocks(this, media_id, lba +
199 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
200 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
201 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
202 }
203
204 real_buffer = efi_bounce_buffer;
205#endif
206
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900207 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200208 buffer_size, buffer);
209
210 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
211 EFI_DISK_READ);
212
213 /* Copy from bounce buffer to real buffer if necessary */
214 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
215 memcpy(buffer, real_buffer, buffer_size);
216
217 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100218}
219
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200220/**
221 * efi_disk_write_blocks() - writes blocks to device
222 *
223 * This function implements the WriteBlocks service of the
224 * EFI_BLOCK_IO_PROTOCOL.
225 *
226 * See the Unified Extensible Firmware Interface (UEFI) specification for
227 * details.
228 *
229 * @this: pointer to the BLOCK_IO_PROTOCOL
230 * @media_id: id of the medium to be written to
231 * @lba: starting logical block for writing
232 * @buffer_size: size of the write buffer
233 * @buffer: pointer to the source buffer
234 * Return: status code
235 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600236static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100237 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100238 void *buffer)
239{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200240 void *real_buffer = buffer;
241 efi_status_t r;
242
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200243 if (!this)
244 return EFI_INVALID_PARAMETER;
245 if (this->media->read_only)
246 return EFI_WRITE_PROTECTED;
247 /* TODO: check for media changes */
248 if (media_id != this->media->media_id)
249 return EFI_MEDIA_CHANGED;
250 if (!this->media->media_present)
251 return EFI_NO_MEDIA;
Heinrich Schuchardt0bb73892021-01-23 19:33:11 +0100252 /* media->io_align is a power of 2 or 0 */
253 if (this->media->io_align &&
254 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200255 return EFI_INVALID_PARAMETER;
256 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsenf1424932021-02-09 17:17:17 +0100257 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200258 return EFI_INVALID_PARAMETER;
259
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200260#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
261 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
262 r = efi_disk_write_blocks(this, media_id, lba,
263 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
264 if (r != EFI_SUCCESS)
265 return r;
266 return efi_disk_write_blocks(this, media_id, lba +
267 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
268 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
269 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
270 }
271
272 real_buffer = efi_bounce_buffer;
273#endif
274
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900275 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200276 buffer_size, buffer);
277
278 /* Populate bounce buffer if necessary */
279 if (real_buffer != buffer)
280 memcpy(real_buffer, buffer, buffer_size);
281
282 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
283 EFI_DISK_WRITE);
284
285 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100286}
287
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200288/**
289 * efi_disk_flush_blocks() - flushes modified data to the device
290 *
291 * This function implements the FlushBlocks service of the
292 * EFI_BLOCK_IO_PROTOCOL.
293 *
294 * As we always write synchronously nothing is done here.
295 *
296 * See the Unified Extensible Firmware Interface (UEFI) specification for
297 * details.
298 *
299 * @this: pointer to the BLOCK_IO_PROTOCOL
300 * Return: status code
301 */
Alexander Grafe83be452016-03-04 01:10:02 +0100302static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
303{
Alexander Grafe83be452016-03-04 01:10:02 +0100304 EFI_ENTRY("%p", this);
305 return EFI_EXIT(EFI_SUCCESS);
306}
307
308static const struct efi_block_io block_io_disk_template = {
309 .reset = &efi_disk_reset,
310 .read_blocks = &efi_disk_read_blocks,
311 .write_blocks = &efi_disk_write_blocks,
312 .flush_blocks = &efi_disk_flush_blocks,
313};
314
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100315/**
316 * efi_fs_from_path() - retrieve simple file system protocol
317 *
318 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100319 *
320 * The full path provided is split into device part and into a file
321 * part. The device part is used to find the handle on which the
322 * simple file system protocol is installed.
323 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100324 * @full_path: device path including device and file
325 * Return: simple file system protocol
Rob Clark53142032017-09-13 18:05:34 -0400326 */
327struct efi_simple_file_system_protocol *
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100328efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark53142032017-09-13 18:05:34 -0400329{
330 struct efi_object *efiobj;
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100331 struct efi_handler *handler;
332 struct efi_device_path *device_path;
333 struct efi_device_path *file_path;
334 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400335
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100336 /* Split the path into a device part and a file part */
337 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
338 if (ret != EFI_SUCCESS)
339 return NULL;
340 efi_free_pool(file_path);
341
342 /* Get the EFI object for the partition */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100343 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100344 efi_free_pool(device_path);
Rob Clark53142032017-09-13 18:05:34 -0400345 if (!efiobj)
346 return NULL;
347
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100348 /* Find the simple file system protocol */
349 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
350 &handler);
351 if (ret != EFI_SUCCESS)
352 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400353
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100354 /* Return the simple file system protocol for the partition */
355 return handler->protocol_interface;
Rob Clark53142032017-09-13 18:05:34 -0400356}
357
AKASHI Takahiro120a01a2019-10-07 14:59:38 +0900358/**
359 * efi_fs_exists() - check if a partition bears a file system
360 *
361 * @desc: block device descriptor
362 * @part: partition number
363 * Return: 1 if a file system exists on the partition
364 * 0 otherwise
365 */
366static int efi_fs_exists(struct blk_desc *desc, int part)
367{
368 if (fs_set_blk_dev_with_part(desc, part))
369 return 0;
370
371 if (fs_get_type() == FS_TYPE_ANY)
372 return 0;
373
374 fs_close();
375
376 return 1;
377}
378
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200379/**
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100380 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200381 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100382 * @parent: parent handle
383 * @dp_parent: parent device path
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100384 * @desc: internal block device
385 * @dev_index: device index for block device
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100386 * @part_info: partition info
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200387 * @part: partition
388 * @disk: pointer to receive the created handle
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200389 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100390 * Return: disk object
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200391 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100392static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100393 efi_handle_t parent,
394 struct efi_device_path *dp_parent,
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100395 struct blk_desc *desc,
396 int dev_index,
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100397 struct disk_partition *part_info,
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100398 unsigned int part,
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200399 struct efi_disk_obj **disk,
400 efi_handle_t agent_handle)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200401{
402 struct efi_disk_obj *diskobj;
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200403 struct efi_object *handle;
Heinrich Schuchardt31d63b82022-10-07 11:03:01 +0200404 const efi_guid_t *esp_guid = NULL;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100405 efi_status_t ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200406
Alexander Graf601db1f2016-08-05 14:51:47 +0200407 /* Don't add empty devices */
408 if (!desc->lba)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100409 return EFI_NOT_READY;
Alexander Graf601db1f2016-08-05 14:51:47 +0200410
Rob Clarka83272f2017-09-13 18:05:31 -0400411 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100412 if (!diskobj)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100413 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100414
415 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200416 efi_add_handle(&diskobj->header);
Alexander Graf06fe57b2016-04-11 16:16:17 +0200417
418 /* Fill in object data */
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100419 if (part_info) {
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100420 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100421 struct efi_handler *handler;
422 void *protocol_interface;
423
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200424 if (!node) {
425 ret = EFI_OUT_OF_RESOURCES;
Simon Glass438e0492023-01-17 10:47:32 -0700426 log_debug("no node\n");
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200427 goto error;
428 }
429
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100430 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
431 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
Simon Glass438e0492023-01-17 10:47:32 -0700432 if (ret != EFI_SUCCESS) {
433 log_debug("search failed\n");
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100434 goto error;
Simon Glass438e0492023-01-17 10:47:32 -0700435 }
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100436
437 /*
438 * Link the partition (child controller) to the block device
439 * (controller).
440 */
441 ret = efi_protocol_open(handler, &protocol_interface, NULL,
442 &diskobj->header,
443 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
Simon Glass438e0492023-01-17 10:47:32 -0700444 if (ret != EFI_SUCCESS) {
445 log_debug("prot open failed\n");
446 goto error;
447 }
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100448
449 diskobj->dp = efi_dp_append_node(dp_parent, node);
450 efi_free_pool(node);
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100451 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100452 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
Heinrich Schuchardt31d63b82022-10-07 11:03:01 +0200453 esp_guid = &efi_system_partition_guid;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100454 } else {
455 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100456 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100457 }
Rob Clarka83272f2017-09-13 18:05:31 -0400458 diskobj->part = part;
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200459
460 /*
461 * Install the device path and the block IO protocol.
462 *
463 * InstallMultipleProtocolInterfaces() checks if the device path is
464 * already installed on an other handle and returns EFI_ALREADY_STARTED
465 * in this case.
466 */
467 handle = &diskobj->header;
Heinrich Schuchardt31d63b82022-10-07 11:03:01 +0200468 ret = efi_install_multiple_protocol_interfaces(
469 &handle,
470 &efi_guid_device_path, diskobj->dp,
471 &efi_block_io_guid, &diskobj->ops,
472 /*
473 * esp_guid must be last entry as it
474 * can be NULL. Its interface is NULL.
475 */
476 esp_guid, NULL,
477 NULL);
Simon Glass438e0492023-01-17 10:47:32 -0700478 if (ret != EFI_SUCCESS) {
479 log_debug("install failed %lx\n", ret);
Heinrich Schuchardte47b68b2021-11-20 13:56:02 +0100480 goto error;
Simon Glass438e0492023-01-17 10:47:32 -0700481 }
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200482
483 /*
484 * On partitions or whole disks without partitions install the
485 * simple file system protocol if a file system is available.
486 */
AKASHI Takahiro356ef902019-10-07 14:59:39 +0900487 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
488 efi_fs_exists(desc, part)) {
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +0200489 ret = efi_create_simple_file_system(desc, part, diskobj->dp,
490 &diskobj->volume);
491 if (ret != EFI_SUCCESS)
492 goto error;
493
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200494 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100495 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardtce721a82018-01-19 20:24:38 +0100496 diskobj->volume);
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +0200497 if (ret != EFI_SUCCESS)
498 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400499 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200500 diskobj->ops = block_io_disk_template;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200501 diskobj->dev_index = dev_index;
502
503 /* Fill in EFI IO Media info (for read/write callbacks) */
504 diskobj->media.removable_media = desc->removable;
505 diskobj->media.media_present = 1;
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200506 /*
507 * MediaID is just an arbitrary counter.
508 * We have to change it if the medium is removed or changed.
509 */
510 diskobj->media.media_id = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200511 diskobj->media.block_size = desc->blksz;
512 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardtaf8c5b62020-03-19 15:45:52 +0100513 if (part)
Emmanuel Vadot5bf29642017-12-11 19:22:33 +0100514 diskobj->media.logical_partition = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200515 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100516 if (disk)
517 *disk = diskobj;
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100518
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100519 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
Paul Barbieriab814f12022-06-30 07:02:04 -0400520 ", last_block %llu\n",
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100521 diskobj->part,
522 diskobj->media.media_present,
523 diskobj->media.logical_partition,
524 diskobj->media.removable_media,
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100525 diskobj->media.last_block);
526
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100527 /* Store first EFI system partition */
Heinrich Schuchardt0397d812022-10-21 08:33:44 +0200528 if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100529 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Simon Glassfada3f92022-09-17 09:00:09 -0600530 efi_system_partition.uclass_id = desc->uclass_id;
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100531 efi_system_partition.devnum = desc->devnum;
532 efi_system_partition.part = part;
Heinrich Schuchardt54f180d2021-05-25 18:00:13 +0200533 EFI_PRINT("EFI system partition: %s %x:%x\n",
Simon Glassfada3f92022-09-17 09:00:09 -0600534 blk_get_uclass_name(desc->uclass_id),
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100535 desc->devnum, part);
536 }
537 }
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100538 return EFI_SUCCESS;
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100539error:
540 efi_delete_handle(&diskobj->header);
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +0200541 free(diskobj->volume);
542 free(diskobj);
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100543 return ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200544}
545
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200546/**
547 * efi_disk_create_raw() - create a handle for a whole raw disk
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100548 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200549 * @dev: udevice (UCLASS_BLK)
550 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100551 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900552 * Create an efi_disk object which is associated with @dev.
553 * The type of @dev must be UCLASS_BLK.
554 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200555 * Return: 0 on success, -1 otherwise
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100556 */
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200557static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200558{
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900559 struct efi_disk_obj *disk;
560 struct blk_desc *desc;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900561 int diskid;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100562 efi_status_t ret;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100563
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900564 desc = dev_get_uclass_plat(dev);
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900565 diskid = desc->devnum;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200566
AKASHI Takahiro7ec925d2022-08-19 09:48:30 +0900567 ret = efi_disk_add_dev(NULL, NULL, desc,
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200568 diskid, NULL, 0, &disk, agent_handle);
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900569 if (ret != EFI_SUCCESS) {
Simon Glass438e0492023-01-17 10:47:32 -0700570 if (ret == EFI_NOT_READY) {
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900571 log_notice("Disk %s not ready\n", dev->name);
Simon Glass438e0492023-01-17 10:47:32 -0700572 ret = -EBUSY;
573 } else {
Simon Glasse57f8d42022-10-29 19:47:17 -0600574 log_err("Adding disk for %s failed (err=%ld/%#lx)\n", dev->name, ret, ret);
Simon Glass438e0492023-01-17 10:47:32 -0700575 ret = -ENOENT;
576 }
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100577
Simon Glass438e0492023-01-17 10:47:32 -0700578 return ret;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200579 }
Masahisa Kojimac9611082022-07-22 11:39:10 +0900580 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900581 efi_free_pool(disk->dp);
582 efi_delete_handle(&disk->header);
Rob Clarka83272f2017-09-13 18:05:31 -0400583
Simon Glass438e0492023-01-17 10:47:32 -0700584 return -EINVAL;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900585 }
586
587 return 0;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200588}
589
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200590/**
591 * efi_disk_create_part() - create a handle for a disk partition
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100592 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200593 * @dev: udevice (UCLASS_PARTITION)
594 * @agent_handle: handle of the EFI block driver
Alexander Grafe83be452016-03-04 01:10:02 +0100595 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900596 * Create an efi_disk object which is associated with @dev.
597 * The type of @dev must be UCLASS_PARTITION.
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100598 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200599 * Return: 0 on success, -1 otherwise
Alexander Grafe83be452016-03-04 01:10:02 +0100600 */
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200601static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
Alexander Grafe83be452016-03-04 01:10:02 +0100602{
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900603 efi_handle_t parent;
604 struct blk_desc *desc;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900605 struct disk_part *part_data;
606 struct disk_partition *info;
607 unsigned int part;
608 int diskid;
609 struct efi_handler *handler;
610 struct efi_device_path *dp_parent;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100611 struct efi_disk_obj *disk;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100612 efi_status_t ret;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900613
614 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
615 return -1;
616
617 desc = dev_get_uclass_plat(dev_get_parent(dev));
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900618 diskid = desc->devnum;
619
620 part_data = dev_get_uclass_plat(dev);
621 part = part_data->partnum;
622 info = &part_data->gpt_part_info;
623
624 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
625 if (ret != EFI_SUCCESS)
626 return -1;
627 dp_parent = (struct efi_device_path *)handler->protocol_interface;
628
AKASHI Takahiro7ec925d2022-08-19 09:48:30 +0900629 ret = efi_disk_add_dev(parent, dp_parent, desc, diskid,
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200630 info, part, &disk, agent_handle);
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900631 if (ret != EFI_SUCCESS) {
632 log_err("Adding partition for %s failed\n", dev->name);
633 return -1;
634 }
Masahisa Kojimac9611082022-07-22 11:39:10 +0900635 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900636 efi_free_pool(disk->dp);
637 efi_delete_handle(&disk->header);
638
639 return -1;
640 }
641
642 return 0;
643}
644
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200645/**
646 * efi_disk_probe() - create efi_disk objects for a block device
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900647 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200648 * @ctx: event context - driver binding protocol
649 * @event: EV_PM_POST_PROBE event
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900650 *
651 * Create efi_disk objects for partitions as well as a raw disk
652 * which is associated with @dev.
653 * The type of @dev must be UCLASS_BLK.
654 * This function is expected to be called at EV_PM_POST_PROBE.
655 *
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200656 * Return: 0 on success, -1 otherwise
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900657 */
Heinrich Schuchardtda620cf2022-10-06 07:29:41 +0200658int efi_disk_probe(void *ctx, struct event *event)
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900659{
Simon Glass302d4f82016-05-14 14:03:05 -0600660 struct udevice *dev;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900661 enum uclass_id id;
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900662 struct blk_desc *desc;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900663 struct udevice *child;
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200664 struct efi_driver_binding_extended_protocol *db_prot = ctx;
665 efi_handle_t agent_handle = db_prot->bp.driver_binding_handle;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900666 int ret;
Simon Glass302d4f82016-05-14 14:03:05 -0600667
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900668 dev = event->data.dm.dev;
669 id = device_get_uclass_id(dev);
Simon Glass302d4f82016-05-14 14:03:05 -0600670
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200671 /* We won't support partitions in a partition */
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900672 if (id != UCLASS_BLK)
673 return 0;
674
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900675 /*
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200676 * Avoid creating duplicated objects now that efi_driver
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900677 * has already created an efi_disk at this moment.
678 */
679 desc = dev_get_uclass_plat(dev);
Simon Glassfada3f92022-09-17 09:00:09 -0600680 if (desc->uclass_id != UCLASS_EFI_LOADER) {
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200681 ret = efi_disk_create_raw(dev, agent_handle);
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900682 if (ret)
683 return -1;
684 }
Simon Glass302d4f82016-05-14 14:03:05 -0600685
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900686 device_foreach_child(child, dev) {
Heinrich Schuchardt175b0a92022-10-08 09:11:54 +0200687 ret = efi_disk_create_part(child, agent_handle);
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900688 if (ret)
689 return -1;
Simon Glass302d4f82016-05-14 14:03:05 -0600690 }
Alexander Grafe83be452016-03-04 01:10:02 +0100691
Raymond Mao1fcaf952023-11-10 13:25:37 +0900692 /* only do the boot option management when UEFI sub-system is initialized */
693 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
694 ret = efi_bootmgr_update_media_device_boot_option();
695 if (ret != EFI_SUCCESS)
696 return -1;
697 }
698
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900699 return 0;
700}
701
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300702/**
703 * efi_disk_remove - delete an efi_disk object for a block device or partition
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900704 *
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300705 * @ctx: event context: driver binding protocol
706 * @event: EV_PM_PRE_REMOVE event
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900707 *
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300708 * Delete an efi_disk object which is associated with the UCLASS_BLK or
709 * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900710 *
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300711 * Return: 0 on success, -1 otherwise
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900712 */
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300713int efi_disk_remove(void *ctx, struct event *event)
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900714{
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300715 enum uclass_id id;
716 struct udevice *dev = event->data.dm.dev;
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900717 efi_handle_t handle;
AKASHI Takahiroaff330a2022-04-19 10:05:15 +0900718 struct blk_desc *desc;
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300719 struct efi_disk_obj *diskobj = NULL;
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300720 efi_status_t ret;
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900721
722 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300723 return 0;
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900724
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300725 id = device_get_uclass_id(dev);
726 switch (id) {
727 case UCLASS_BLK:
728 desc = dev_get_uclass_plat(dev);
729 if (desc && desc->uclass_id != UCLASS_EFI_LOADER)
730 diskobj = container_of(handle, struct efi_disk_obj,
731 header);
732 break;
733 case UCLASS_PARTITION:
AKASHI Takahiroaff330a2022-04-19 10:05:15 +0900734 diskobj = container_of(handle, struct efi_disk_obj, header);
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300735 break;
736 default:
737 return 0;
AKASHI Takahiroaff330a2022-04-19 10:05:15 +0900738 }
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900739
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300740 ret = efi_delete_handle(handle);
741 /* Do not delete DM device if there are still EFI drivers attached. */
742 if (ret != EFI_SUCCESS)
743 return -1;
744
Ilias Apalodimasf1db7df2023-06-12 18:35:58 +0300745 if (diskobj)
746 efi_free_pool(diskobj->dp);
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900747
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900748 dev_tag_del(dev, DM_TAG_EFI);
749
750 return 0;
Raymond Mao1fcaf952023-11-10 13:25:37 +0900751
752 /*
753 * TODO A flag to distinguish below 2 different scenarios of this
754 * function call is needed:
755 * a) Unplugging of a removable media under U-Boot
756 * b) U-Boot exiting and booting an OS
757 * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
758 * needs to be invoked here to update the boot options and remove the
759 * unnecessary ones.
760 */
761
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900762}
763
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900764/**
765 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
766 *
767 * @handle: pointer to the EFI handle
768 * @buf: pointer to the buffer to store the string
769 * @size: size of buffer
770 * Return: status code
771 */
772efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
773{
774 int count;
775 int diskid;
776 enum uclass_id id;
777 unsigned int part;
778 struct udevice *dev;
779 struct blk_desc *desc;
780 const char *if_typename;
781 bool is_partition = false;
782 struct disk_part *part_data;
783
784 if (!handle || !buf || !size)
785 return EFI_INVALID_PARAMETER;
786
787 dev = handle->dev;
788 id = device_get_uclass_id(dev);
789 if (id == UCLASS_BLK) {
790 desc = dev_get_uclass_plat(dev);
791 } else if (id == UCLASS_PARTITION) {
792 desc = dev_get_uclass_plat(dev_get_parent(dev));
793 is_partition = true;
794 } else {
795 return EFI_INVALID_PARAMETER;
796 }
Simon Glassfada3f92022-09-17 09:00:09 -0600797 if_typename = blk_get_uclass_name(desc->uclass_id);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900798 diskid = desc->devnum;
799
800 if (is_partition) {
801 part_data = dev_get_uclass_plat(dev);
802 part = part_data->partnum;
Heinrich Schuchardt217cb202022-10-07 12:55:16 +0200803 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
804 part);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900805 } else {
806 count = snprintf(buf, size, "%s %d", if_typename, diskid);
807 }
808
809 if (count < 0 || (count + 1) > size)
810 return EFI_INVALID_PARAMETER;
811
812 return EFI_SUCCESS;
813}
Tom Rini586c22b2022-09-19 13:19:39 -0400814
815/**
Heinrich Schuchardta02228f2022-08-31 16:37:35 +0200816 * efi_disks_register() - ensure all block devices are available in UEFI
817 *
818 * The function probes all block devices. As we store UEFI variables on the
819 * EFI system partition this function has to be called before enabling
820 * variable services.
821 */
822efi_status_t efi_disks_register(void)
823{
824 struct udevice *dev;
825
826 uclass_foreach_dev_probe(UCLASS_BLK, dev) {
827 }
828
829 return EFI_SUCCESS;
830}