blob: 93a9a5ac025068328aba35fcb278e2b77cd8da92 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf2a22d052016-03-04 01:10:02 +01002/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf2a22d052016-03-04 01:10:02 +01006 */
7
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +02008#define LOG_CATEGORY LOGC_EFI
9
Simon Glass6dd9faf2016-05-01 13:52:32 -060010#include <blk.h>
Simon Glass487d7562016-05-14 14:03:05 -060011#include <dm.h>
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +090012#include <dm/device-internal.h>
13#include <dm/tag.h>
14#include <event.h>
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +020015#include <efi_driver.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010016#include <efi_loader.h>
AKASHI Takahiro86740062019-10-07 14:59:38 +090017#include <fs.h>
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +020018#include <log.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010019#include <part.h>
20#include <malloc.h>
21
Heinrich Schuchardt2b55ad32022-10-21 08:33:44 +020022struct efi_system_partition efi_system_partition = {
23 .uclass_id = UCLASS_INVALID,
24};
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +010025
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020026const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010027const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010028
Heinrich Schuchardtd39646a2018-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 Schuchardtd39646a2018-09-26 05:27:56 +020034 * @media: block I/O media information
35 * @dp: device path to the block device
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020036 * @volume: simple file system protocol of the partition
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020037 */
Alexander Graf2a22d052016-03-04 01:10:02 +010038struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020039 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010040 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010041 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040042 struct efi_device_path *dp;
Rob Clark2a920802017-09-13 18:05:34 -040043 struct efi_simple_file_system_protocol *volume;
Alexander Graf2a22d052016-03-04 01:10:02 +010044};
45
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020046/**
47 * efi_disk_reset() - reset block device
48 *
49 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
50 *
51 * As U-Boot's block devices do not have a reset function simply return
52 * EFI_SUCCESS.
53 *
54 * See the Unified Extensible Firmware Interface (UEFI) specification for
55 * details.
56 *
57 * @this: pointer to the BLOCK_IO_PROTOCOL
58 * @extended_verification: extended verification
59 * Return: status code
60 */
Alexander Graf2a22d052016-03-04 01:10:02 +010061static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
62 char extended_verification)
63{
64 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020065 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010066}
67
AKASHI Takahiro05f391e2022-05-12 11:29:01 +090068/**
69 * efi_disk_is_removable() - check if the device is removable media
70 * @handle: efi object handle;
71 *
72 * Examine the device and determine if the device is a local block device
73 * and removable media.
74 *
75 * Return: true if removable, false otherwise
76 */
77bool efi_disk_is_removable(efi_handle_t handle)
78{
79 struct efi_handler *handler;
80 struct efi_block_io *io;
81 efi_status_t ret;
82
83 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
84 if (ret != EFI_SUCCESS)
85 return false;
86
87 io = handler->protocol_interface;
88
89 if (!io || !io->media)
90 return false;
91
92 return (bool)io->media->removable_media;
93}
94
Alexander Graf2a22d052016-03-04 01:10:02 +010095enum efi_disk_direction {
96 EFI_DISK_READ,
97 EFI_DISK_WRITE,
98};
99
Heinrich Schuchardta80a2322017-08-26 22:33:13 +0200100static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +0100101 u32 media_id, u64 lba, unsigned long buffer_size,
102 void *buffer, enum efi_disk_direction direction)
103{
104 struct efi_disk_obj *diskobj;
Alexander Graf2a22d052016-03-04 01:10:02 +0100105 int blksz;
106 int blocks;
107 unsigned long n;
108
Alexander Graf2a22d052016-03-04 01:10:02 +0100109 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900110 blksz = diskobj->media.block_size;
Alexander Graf2a22d052016-03-04 01:10:02 +0100111 blocks = buffer_size / blksz;
112
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200113 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
114 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +0100115
116 /* We only support full block access */
117 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200118 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +0100119
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900120 if (CONFIG_IS_ENABLED(PARTITIONS) &&
Masahisa Kojimaee576662022-07-22 11:39:10 +0900121 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900122 if (direction == EFI_DISK_READ)
Simon Glass76c839f2022-10-20 18:22:52 -0600123 n = disk_blk_read(diskobj->header.dev, lba, blocks,
124 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900125 else
Simon Glass76c839f2022-10-20 18:22:52 -0600126 n = disk_blk_write(diskobj->header.dev, lba, blocks,
127 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900128 } else {
129 /* dev is a block device (UCLASS_BLK) */
130 struct blk_desc *desc;
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900131
Masahisa Kojimaee576662022-07-22 11:39:10 +0900132 desc = dev_get_uclass_plat(diskobj->header.dev);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900133 if (direction == EFI_DISK_READ)
134 n = blk_dread(desc, lba, blocks, buffer);
135 else
136 n = blk_dwrite(desc, lba, blocks, buffer);
137 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100138
139 /* We don't do interrupts, so check for timers cooperatively */
140 efi_timer_check();
141
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200142 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200143
Alexander Graf2a22d052016-03-04 01:10:02 +0100144 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400145 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100146
Rob Clark33049902017-07-25 20:28:29 -0400147 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100148}
149
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200150/**
151 * efi_disk_read_blocks() - reads blocks from device
152 *
153 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
154 *
155 * See the Unified Extensible Firmware Interface (UEFI) specification for
156 * details.
157 *
158 * @this: pointer to the BLOCK_IO_PROTOCOL
159 * @media_id: id of the medium to be read from
160 * @lba: starting logical block for reading
161 * @buffer_size: size of the read buffer
162 * @buffer: pointer to the destination buffer
163 * Return: status code
164 */
Simon Glasse2754582016-09-25 15:27:32 -0600165static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100166 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100167 void *buffer)
168{
Alexander Graf51735ae2016-05-11 18:25:48 +0200169 void *real_buffer = buffer;
170 efi_status_t r;
171
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200172 if (!this)
173 return EFI_INVALID_PARAMETER;
174 /* TODO: check for media changes */
175 if (media_id != this->media->media_id)
176 return EFI_MEDIA_CHANGED;
177 if (!this->media->media_present)
178 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100179 /* media->io_align is a power of 2 or 0 */
180 if (this->media->io_align &&
181 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200182 return EFI_INVALID_PARAMETER;
183 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100184 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200185 return EFI_INVALID_PARAMETER;
186
Alexander Graf51735ae2016-05-11 18:25:48 +0200187#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
188 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
189 r = efi_disk_read_blocks(this, media_id, lba,
190 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
191 if (r != EFI_SUCCESS)
192 return r;
193 return efi_disk_read_blocks(this, media_id, lba +
194 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
195 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
196 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
197 }
198
199 real_buffer = efi_bounce_buffer;
200#endif
201
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900202 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200203 buffer_size, buffer);
204
205 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
206 EFI_DISK_READ);
207
208 /* Copy from bounce buffer to real buffer if necessary */
209 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
210 memcpy(buffer, real_buffer, buffer_size);
211
212 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100213}
214
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200215/**
216 * efi_disk_write_blocks() - writes blocks to device
217 *
218 * This function implements the WriteBlocks service of the
219 * EFI_BLOCK_IO_PROTOCOL.
220 *
221 * See the Unified Extensible Firmware Interface (UEFI) specification for
222 * details.
223 *
224 * @this: pointer to the BLOCK_IO_PROTOCOL
225 * @media_id: id of the medium to be written to
226 * @lba: starting logical block for writing
227 * @buffer_size: size of the write buffer
228 * @buffer: pointer to the source buffer
229 * Return: status code
230 */
Simon Glasse2754582016-09-25 15:27:32 -0600231static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100232 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100233 void *buffer)
234{
Alexander Graf51735ae2016-05-11 18:25:48 +0200235 void *real_buffer = buffer;
236 efi_status_t r;
237
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200238 if (!this)
239 return EFI_INVALID_PARAMETER;
240 if (this->media->read_only)
241 return EFI_WRITE_PROTECTED;
242 /* TODO: check for media changes */
243 if (media_id != this->media->media_id)
244 return EFI_MEDIA_CHANGED;
245 if (!this->media->media_present)
246 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100247 /* media->io_align is a power of 2 or 0 */
248 if (this->media->io_align &&
249 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200250 return EFI_INVALID_PARAMETER;
251 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100252 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200253 return EFI_INVALID_PARAMETER;
254
Alexander Graf51735ae2016-05-11 18:25:48 +0200255#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
256 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
257 r = efi_disk_write_blocks(this, media_id, lba,
258 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
259 if (r != EFI_SUCCESS)
260 return r;
261 return efi_disk_write_blocks(this, media_id, lba +
262 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
263 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
264 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
265 }
266
267 real_buffer = efi_bounce_buffer;
268#endif
269
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900270 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200271 buffer_size, buffer);
272
273 /* Populate bounce buffer if necessary */
274 if (real_buffer != buffer)
275 memcpy(real_buffer, buffer, buffer_size);
276
277 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
278 EFI_DISK_WRITE);
279
280 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100281}
282
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200283/**
284 * efi_disk_flush_blocks() - flushes modified data to the device
285 *
286 * This function implements the FlushBlocks service of the
287 * EFI_BLOCK_IO_PROTOCOL.
288 *
289 * As we always write synchronously nothing is done here.
290 *
291 * See the Unified Extensible Firmware Interface (UEFI) specification for
292 * details.
293 *
294 * @this: pointer to the BLOCK_IO_PROTOCOL
295 * Return: status code
296 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100297static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
298{
Alexander Graf2a22d052016-03-04 01:10:02 +0100299 EFI_ENTRY("%p", this);
300 return EFI_EXIT(EFI_SUCCESS);
301}
302
303static const struct efi_block_io block_io_disk_template = {
304 .reset = &efi_disk_reset,
305 .read_blocks = &efi_disk_read_blocks,
306 .write_blocks = &efi_disk_write_blocks,
307 .flush_blocks = &efi_disk_flush_blocks,
308};
309
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100310/**
311 * efi_fs_from_path() - retrieve simple file system protocol
312 *
313 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100314 *
315 * The full path provided is split into device part and into a file
316 * part. The device part is used to find the handle on which the
317 * simple file system protocol is installed.
318 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100319 * @full_path: device path including device and file
320 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400321 */
322struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100323efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400324{
325 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100326 struct efi_handler *handler;
327 struct efi_device_path *device_path;
328 struct efi_device_path *file_path;
329 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400330
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100331 /* Split the path into a device part and a file part */
332 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
333 if (ret != EFI_SUCCESS)
334 return NULL;
335 efi_free_pool(file_path);
336
337 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100338 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100339 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400340 if (!efiobj)
341 return NULL;
342
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100343 /* Find the simple file system protocol */
344 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
345 &handler);
346 if (ret != EFI_SUCCESS)
347 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400348
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100349 /* Return the simple file system protocol for the partition */
350 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400351}
352
AKASHI Takahiro86740062019-10-07 14:59:38 +0900353/**
354 * efi_fs_exists() - check if a partition bears a file system
355 *
356 * @desc: block device descriptor
357 * @part: partition number
358 * Return: 1 if a file system exists on the partition
359 * 0 otherwise
360 */
361static int efi_fs_exists(struct blk_desc *desc, int part)
362{
363 if (fs_set_blk_dev_with_part(desc, part))
364 return 0;
365
366 if (fs_get_type() == FS_TYPE_ANY)
367 return 0;
368
369 fs_close();
370
371 return 1;
372}
373
Masahisa Kojima0351b652024-01-19 09:45:45 +0900374static void efi_disk_free_diskobj(struct efi_disk_obj *diskobj)
375{
376 struct efi_device_path *dp = diskobj->dp;
377 struct efi_simple_file_system_protocol *volume = diskobj->volume;
378
379 /*
380 * ignore error of efi_delete_handle() since this function
381 * is expected to be called in error path.
382 */
383 efi_delete_handle(&diskobj->header);
384 efi_free_pool(dp);
385 free(volume);
386}
387
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200388/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100389 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200390 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100391 * @parent: parent handle
392 * @dp_parent: parent device path
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100393 * @desc: internal block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100394 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200395 * @part: partition
396 * @disk: pointer to receive the created handle
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200397 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100398 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200399 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100400static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100401 efi_handle_t parent,
402 struct efi_device_path *dp_parent,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100403 struct blk_desc *desc,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100404 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100405 unsigned int part,
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200406 struct efi_disk_obj **disk,
407 efi_handle_t agent_handle)
Alexander Graf4a12a972016-04-11 16:16:17 +0200408{
409 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200410 struct efi_object *handle;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200411 const efi_guid_t *esp_guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100412 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200413
Alexander Graf0812d1a2016-08-05 14:51:47 +0200414 /* Don't add empty devices */
415 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100416 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200417
Rob Clark884bcf62017-09-13 18:05:31 -0400418 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100419 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100420 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100421
422 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200423 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200424
425 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100426 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100427 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100428 struct efi_handler *handler;
429 void *protocol_interface;
430
Heinrich Schuchardt01caf282022-10-06 13:36:02 +0200431 if (!node) {
432 ret = EFI_OUT_OF_RESOURCES;
Simon Glass3722cc92023-01-17 10:47:32 -0700433 log_debug("no node\n");
Heinrich Schuchardt01caf282022-10-06 13:36:02 +0200434 goto error;
435 }
436
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100437 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
438 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
Simon Glass3722cc92023-01-17 10:47:32 -0700439 if (ret != EFI_SUCCESS) {
440 log_debug("search failed\n");
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100441 goto error;
Simon Glass3722cc92023-01-17 10:47:32 -0700442 }
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100443
444 /*
445 * Link the partition (child controller) to the block device
446 * (controller).
447 */
448 ret = efi_protocol_open(handler, &protocol_interface, NULL,
449 &diskobj->header,
450 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
Simon Glass3722cc92023-01-17 10:47:32 -0700451 if (ret != EFI_SUCCESS) {
452 log_debug("prot open failed\n");
453 goto error;
454 }
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100455
456 diskobj->dp = efi_dp_append_node(dp_parent, node);
457 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100458 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100459 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200460 esp_guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100461 } else {
462 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100463 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100464 }
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200465
466 /*
467 * Install the device path and the block IO protocol.
468 *
469 * InstallMultipleProtocolInterfaces() checks if the device path is
470 * already installed on an other handle and returns EFI_ALREADY_STARTED
471 * in this case.
472 */
473 handle = &diskobj->header;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200474 ret = efi_install_multiple_protocol_interfaces(
475 &handle,
476 &efi_guid_device_path, diskobj->dp,
477 &efi_block_io_guid, &diskobj->ops,
478 /*
479 * esp_guid must be last entry as it
480 * can be NULL. Its interface is NULL.
481 */
482 esp_guid, NULL,
483 NULL);
Simon Glass3722cc92023-01-17 10:47:32 -0700484 if (ret != EFI_SUCCESS) {
485 log_debug("install failed %lx\n", ret);
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100486 goto error;
Simon Glass3722cc92023-01-17 10:47:32 -0700487 }
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200488
489 /*
490 * On partitions or whole disks without partitions install the
491 * simple file system protocol if a file system is available.
492 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900493 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
494 efi_fs_exists(desc, part)) {
Heinrich Schuchardtcff77002023-07-30 14:03:53 +0200495 ret = efi_create_simple_file_system(desc, part, diskobj->dp,
496 &diskobj->volume);
497 if (ret != EFI_SUCCESS)
498 goto error;
499
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200500 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100501 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100502 diskobj->volume);
Heinrich Schuchardtcff77002023-07-30 14:03:53 +0200503 if (ret != EFI_SUCCESS)
504 goto error;
Rob Clark2a920802017-09-13 18:05:34 -0400505 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200506 diskobj->ops = block_io_disk_template;
Alexander Graf4a12a972016-04-11 16:16:17 +0200507
508 /* Fill in EFI IO Media info (for read/write callbacks) */
509 diskobj->media.removable_media = desc->removable;
510 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200511 /*
512 * MediaID is just an arbitrary counter.
513 * We have to change it if the medium is removed or changed.
514 */
515 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200516 diskobj->media.block_size = desc->blksz;
517 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100518 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100519 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200520 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100521 if (disk)
522 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100523
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100524 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
Paul Barbieri7a85f322022-06-30 07:02:04 -0400525 ", last_block %llu\n",
Masahisa Kojima6caf3a32023-12-25 13:43:54 +0900526 part,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100527 diskobj->media.media_present,
528 diskobj->media.logical_partition,
529 diskobj->media.removable_media,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100530 diskobj->media.last_block);
531
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100532 /* Store first EFI system partition */
Heinrich Schuchardt2b55ad32022-10-21 08:33:44 +0200533 if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100534 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Simon Glass8149b152022-09-17 09:00:09 -0600535 efi_system_partition.uclass_id = desc->uclass_id;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100536 efi_system_partition.devnum = desc->devnum;
537 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200538 EFI_PRINT("EFI system partition: %s %x:%x\n",
Simon Glass8149b152022-09-17 09:00:09 -0600539 blk_get_uclass_name(desc->uclass_id),
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100540 desc->devnum, part);
541 }
542 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100543 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100544error:
Masahisa Kojima0351b652024-01-19 09:45:45 +0900545 efi_disk_free_diskobj(diskobj);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100546 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200547}
548
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200549/**
550 * efi_disk_create_raw() - create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100551 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200552 * @dev: udevice (UCLASS_BLK)
553 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100554 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900555 * Create an efi_disk object which is associated with @dev.
556 * The type of @dev must be UCLASS_BLK.
557 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200558 * Return: 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100559 */
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200560static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
Alexander Graf2a22d052016-03-04 01:10:02 +0100561{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100562 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900563 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900564 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100565 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600566
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900567 desc = dev_get_uclass_plat(dev);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900568 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600569
AKASHI Takahiroab31c8a2022-08-19 09:48:30 +0900570 ret = efi_disk_add_dev(NULL, NULL, desc,
Masahisa Kojima6caf3a32023-12-25 13:43:54 +0900571 NULL, 0, &disk, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900572 if (ret != EFI_SUCCESS) {
Simon Glass3722cc92023-01-17 10:47:32 -0700573 if (ret == EFI_NOT_READY) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200574 log_notice("Disk %s not ready\n", dev->name);
Simon Glass3722cc92023-01-17 10:47:32 -0700575 ret = -EBUSY;
576 } else {
Heinrich Schuchardt50457f52024-02-02 15:12:52 +0100577 log_err("Adding block device %s failed, r = %lu\n",
578 dev->name, ret & ~EFI_ERROR_MASK);
Simon Glass3722cc92023-01-17 10:47:32 -0700579 ret = -ENOENT;
580 }
Simon Glass487d7562016-05-14 14:03:05 -0600581
Simon Glass3722cc92023-01-17 10:47:32 -0700582 return ret;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900583 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900584 if (efi_link_dev(&disk->header, dev)) {
Masahisa Kojima0351b652024-01-19 09:45:45 +0900585 efi_disk_free_diskobj(disk);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900586
Simon Glass3722cc92023-01-17 10:47:32 -0700587 return -EINVAL;
Simon Glass487d7562016-05-14 14:03:05 -0600588 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100589
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900590 return 0;
591}
592
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200593/**
594 * efi_disk_create_part() - create a handle for a disk partition
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900595 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200596 * @dev: udevice (UCLASS_PARTITION)
597 * @agent_handle: handle of the EFI block driver
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900598 *
599 * Create an efi_disk object which is associated with @dev.
600 * The type of @dev must be UCLASS_PARTITION.
601 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200602 * Return: 0 on success, -1 otherwise
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900603 */
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200604static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900605{
606 efi_handle_t parent;
607 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900608 struct disk_part *part_data;
609 struct disk_partition *info;
610 unsigned int part;
611 int diskid;
612 struct efi_handler *handler;
613 struct efi_device_path *dp_parent;
614 struct efi_disk_obj *disk;
615 efi_status_t ret;
616
617 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
618 return -1;
619
620 desc = dev_get_uclass_plat(dev_get_parent(dev));
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900621 diskid = desc->devnum;
622
623 part_data = dev_get_uclass_plat(dev);
624 part = part_data->partnum;
625 info = &part_data->gpt_part_info;
626
627 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
628 if (ret != EFI_SUCCESS)
629 return -1;
630 dp_parent = (struct efi_device_path *)handler->protocol_interface;
631
Masahisa Kojima6caf3a32023-12-25 13:43:54 +0900632 ret = efi_disk_add_dev(parent, dp_parent, desc,
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200633 info, part, &disk, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900634 if (ret != EFI_SUCCESS) {
635 log_err("Adding partition for %s failed\n", dev->name);
636 return -1;
637 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900638 if (efi_link_dev(&disk->header, dev)) {
Masahisa Kojima0351b652024-01-19 09:45:45 +0900639 efi_disk_free_diskobj(disk);
640
641 /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900642
643 return -1;
644 }
645
646 return 0;
647}
648
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200649/**
650 * efi_disk_probe() - create efi_disk objects for a block device
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900651 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200652 * @ctx: event context - driver binding protocol
653 * @event: EV_PM_POST_PROBE event
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900654 *
655 * Create efi_disk objects for partitions as well as a raw disk
656 * which is associated with @dev.
657 * The type of @dev must be UCLASS_BLK.
658 * This function is expected to be called at EV_PM_POST_PROBE.
659 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200660 * Return: 0 on success, -1 otherwise
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900661 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200662int efi_disk_probe(void *ctx, struct event *event)
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900663{
664 struct udevice *dev;
665 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900666 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900667 struct udevice *child;
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200668 struct efi_driver_binding_extended_protocol *db_prot = ctx;
669 efi_handle_t agent_handle = db_prot->bp.driver_binding_handle;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900670 int ret;
671
672 dev = event->data.dm.dev;
673 id = device_get_uclass_id(dev);
674
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200675 /* We won't support partitions in a partition */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900676 if (id != UCLASS_BLK)
677 return 0;
678
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900679 /*
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200680 * Avoid creating duplicated objects now that efi_driver
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900681 * has already created an efi_disk at this moment.
682 */
683 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600684 if (desc->uclass_id != UCLASS_EFI_LOADER) {
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200685 ret = efi_disk_create_raw(dev, agent_handle);
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900686 if (ret)
687 return -1;
688 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900689
690 device_foreach_child(child, dev) {
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200691 ret = efi_disk_create_part(child, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900692 if (ret)
693 return -1;
694 }
695
Raymond Mao550862b2023-11-10 13:25:37 +0900696 /* only do the boot option management when UEFI sub-system is initialized */
697 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
698 ret = efi_bootmgr_update_media_device_boot_option();
699 if (ret != EFI_SUCCESS)
700 return -1;
701 }
702
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900703 return 0;
704}
705
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300706/**
707 * efi_disk_remove - delete an efi_disk object for a block device or partition
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900708 *
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300709 * @ctx: event context: driver binding protocol
710 * @event: EV_PM_PRE_REMOVE event
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900711 *
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300712 * Delete an efi_disk object which is associated with the UCLASS_BLK or
713 * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900714 *
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300715 * Return: 0 on success, -1 otherwise
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900716 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200717int efi_disk_remove(void *ctx, struct event *event)
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900718{
719 enum uclass_id id;
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300720 struct udevice *dev = event->data.dm.dev;
721 efi_handle_t handle;
722 struct blk_desc *desc;
Masahisa Kojimaf674a2f2024-01-19 09:45:44 +0900723 struct efi_device_path *dp = NULL;
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300724 struct efi_disk_obj *diskobj = NULL;
Masahisa Kojimaf674a2f2024-01-19 09:45:44 +0900725 struct efi_simple_file_system_protocol *volume = NULL;
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300726 efi_status_t ret;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900727
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300728 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900729 return 0;
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300730
731 id = device_get_uclass_id(dev);
732 switch (id) {
733 case UCLASS_BLK:
734 desc = dev_get_uclass_plat(dev);
Masahisa Kojima2c98f742024-01-19 09:45:46 +0900735 if (desc && desc->uclass_id == UCLASS_EFI_LOADER)
736 /*
737 * EFI application/driver manages the EFI handle,
738 * no need to delete EFI handle.
739 */
740 return 0;
741
742 diskobj = (struct efi_disk_obj *)handle;
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300743 break;
744 case UCLASS_PARTITION:
Masahisa Kojimaf674a2f2024-01-19 09:45:44 +0900745 diskobj = (struct efi_disk_obj *)handle;
746
747 /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
748
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300749 break;
750 default:
751 return 0;
752 }
753
Masahisa Kojima2c98f742024-01-19 09:45:46 +0900754 dp = diskobj->dp;
755 volume = diskobj->volume;
Masahisa Kojimaf674a2f2024-01-19 09:45:44 +0900756
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300757 ret = efi_delete_handle(handle);
758 /* Do not delete DM device if there are still EFI drivers attached. */
759 if (ret != EFI_SUCCESS)
760 return -1;
761
Masahisa Kojimaf674a2f2024-01-19 09:45:44 +0900762 efi_free_pool(dp);
763 free(volume);
Ilias Apalodimas3cc2b9f2023-06-12 18:35:58 +0300764 dev_tag_del(dev, DM_TAG_EFI);
765
766 return 0;
Raymond Mao550862b2023-11-10 13:25:37 +0900767
768 /*
769 * TODO A flag to distinguish below 2 different scenarios of this
770 * function call is needed:
771 * a) Unplugging of a removable media under U-Boot
772 * b) U-Boot exiting and booting an OS
773 * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
774 * needs to be invoked here to update the boot options and remove the
775 * unnecessary ones.
776 */
777
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900778}
779
Masahisa Kojima87d79142022-09-12 17:33:50 +0900780/**
781 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
782 *
783 * @handle: pointer to the EFI handle
784 * @buf: pointer to the buffer to store the string
785 * @size: size of buffer
786 * Return: status code
787 */
788efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
789{
790 int count;
791 int diskid;
792 enum uclass_id id;
793 unsigned int part;
794 struct udevice *dev;
795 struct blk_desc *desc;
796 const char *if_typename;
797 bool is_partition = false;
798 struct disk_part *part_data;
799
800 if (!handle || !buf || !size)
801 return EFI_INVALID_PARAMETER;
802
803 dev = handle->dev;
804 id = device_get_uclass_id(dev);
805 if (id == UCLASS_BLK) {
806 desc = dev_get_uclass_plat(dev);
807 } else if (id == UCLASS_PARTITION) {
808 desc = dev_get_uclass_plat(dev_get_parent(dev));
809 is_partition = true;
810 } else {
811 return EFI_INVALID_PARAMETER;
812 }
Simon Glass8149b152022-09-17 09:00:09 -0600813 if_typename = blk_get_uclass_name(desc->uclass_id);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900814 diskid = desc->devnum;
815
816 if (is_partition) {
817 part_data = dev_get_uclass_plat(dev);
818 part = part_data->partnum;
Heinrich Schuchardt2eeb7fe2022-10-07 12:55:16 +0200819 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
820 part);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900821 } else {
822 count = snprintf(buf, size, "%s %d", if_typename, diskid);
823 }
824
825 if (count < 0 || (count + 1) > size)
826 return EFI_INVALID_PARAMETER;
827
828 return EFI_SUCCESS;
829}
Tom Rinie9a1ff92022-09-19 13:19:39 -0400830
831/**
Heinrich Schuchardtd5391bf2022-08-31 16:37:35 +0200832 * efi_disks_register() - ensure all block devices are available in UEFI
833 *
834 * The function probes all block devices. As we store UEFI variables on the
835 * EFI system partition this function has to be called before enabling
836 * variable services.
837 */
838efi_status_t efi_disks_register(void)
839{
840 struct udevice *dev;
841
842 uclass_foreach_dev_probe(UCLASS_BLK, dev) {
843 }
844
845 return EFI_SUCCESS;
846}