blob: f11722db2ce45f65731b87ac60c440b9f3ac05a0 [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
Alexander Graf2a22d052016-03-04 01:10:02 +010010#include <common.h>
Simon Glass6dd9faf2016-05-01 13:52:32 -060011#include <blk.h>
Simon Glass487d7562016-05-14 14:03:05 -060012#include <dm.h>
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +090013#include <dm/device-internal.h>
14#include <dm/tag.h>
15#include <event.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 Schuchardt11078bb2020-03-19 15:16:31 +010022struct efi_system_partition efi_system_partition;
23
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020024const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010025const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010026
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020027/**
28 * struct efi_disk_obj - EFI disk object
29 *
30 * @header: EFI object header
31 * @ops: EFI disk I/O protocol interface
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020032 * @dev_index: device index of block device
33 * @media: block I/O media information
34 * @dp: device path to the block device
35 * @part: partition
36 * @volume: simple file system protocol of the partition
AKASHI Takahirod97e98c2022-04-19 10:05:17 +090037 * @dev: associated DM device
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020038 */
Alexander Graf2a22d052016-03-04 01:10:02 +010039struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020040 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010041 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010042 int dev_index;
Alexander Graf2a22d052016-03-04 01:10:02 +010043 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040044 struct efi_device_path *dp;
Rob Clark884bcf62017-09-13 18:05:31 -040045 unsigned int part;
Rob Clark2a920802017-09-13 18:05:34 -040046 struct efi_simple_file_system_protocol *volume;
Alexander Graf2a22d052016-03-04 01:10:02 +010047};
48
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020049/**
50 * efi_disk_reset() - reset block device
51 *
52 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
53 *
54 * As U-Boot's block devices do not have a reset function simply return
55 * EFI_SUCCESS.
56 *
57 * See the Unified Extensible Firmware Interface (UEFI) specification for
58 * details.
59 *
60 * @this: pointer to the BLOCK_IO_PROTOCOL
61 * @extended_verification: extended verification
62 * Return: status code
63 */
Alexander Graf2a22d052016-03-04 01:10:02 +010064static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
65 char extended_verification)
66{
67 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020068 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010069}
70
AKASHI Takahiro05f391e2022-05-12 11:29:01 +090071/**
72 * efi_disk_is_removable() - check if the device is removable media
73 * @handle: efi object handle;
74 *
75 * Examine the device and determine if the device is a local block device
76 * and removable media.
77 *
78 * Return: true if removable, false otherwise
79 */
80bool efi_disk_is_removable(efi_handle_t handle)
81{
82 struct efi_handler *handler;
83 struct efi_block_io *io;
84 efi_status_t ret;
85
86 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
87 if (ret != EFI_SUCCESS)
88 return false;
89
90 io = handler->protocol_interface;
91
92 if (!io || !io->media)
93 return false;
94
95 return (bool)io->media->removable_media;
96}
97
Alexander Graf2a22d052016-03-04 01:10:02 +010098enum efi_disk_direction {
99 EFI_DISK_READ,
100 EFI_DISK_WRITE,
101};
102
Heinrich Schuchardta80a2322017-08-26 22:33:13 +0200103static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +0100104 u32 media_id, u64 lba, unsigned long buffer_size,
105 void *buffer, enum efi_disk_direction direction)
106{
107 struct efi_disk_obj *diskobj;
Alexander Graf2a22d052016-03-04 01:10:02 +0100108 int blksz;
109 int blocks;
110 unsigned long n;
111
Alexander Graf2a22d052016-03-04 01:10:02 +0100112 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900113 blksz = diskobj->media.block_size;
Alexander Graf2a22d052016-03-04 01:10:02 +0100114 blocks = buffer_size / blksz;
115
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200116 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
117 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +0100118
119 /* We only support full block access */
120 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200121 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +0100122
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900123 if (CONFIG_IS_ENABLED(PARTITIONS) &&
Masahisa Kojimaee576662022-07-22 11:39:10 +0900124 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900125 if (direction == EFI_DISK_READ)
Simon Glass76c839f2022-10-20 18:22:52 -0600126 n = disk_blk_read(diskobj->header.dev, lba, blocks,
127 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900128 else
Simon Glass76c839f2022-10-20 18:22:52 -0600129 n = disk_blk_write(diskobj->header.dev, lba, blocks,
130 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900131 } else {
132 /* dev is a block device (UCLASS_BLK) */
133 struct blk_desc *desc;
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900134
Masahisa Kojimaee576662022-07-22 11:39:10 +0900135 desc = dev_get_uclass_plat(diskobj->header.dev);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900136 if (direction == EFI_DISK_READ)
137 n = blk_dread(desc, lba, blocks, buffer);
138 else
139 n = blk_dwrite(desc, lba, blocks, buffer);
140 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100141
142 /* We don't do interrupts, so check for timers cooperatively */
143 efi_timer_check();
144
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200145 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200146
Alexander Graf2a22d052016-03-04 01:10:02 +0100147 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400148 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100149
Rob Clark33049902017-07-25 20:28:29 -0400150 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100151}
152
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200153/**
154 * efi_disk_read_blocks() - reads blocks from device
155 *
156 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
157 *
158 * See the Unified Extensible Firmware Interface (UEFI) specification for
159 * details.
160 *
161 * @this: pointer to the BLOCK_IO_PROTOCOL
162 * @media_id: id of the medium to be read from
163 * @lba: starting logical block for reading
164 * @buffer_size: size of the read buffer
165 * @buffer: pointer to the destination buffer
166 * Return: status code
167 */
Simon Glasse2754582016-09-25 15:27:32 -0600168static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100169 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100170 void *buffer)
171{
Alexander Graf51735ae2016-05-11 18:25:48 +0200172 void *real_buffer = buffer;
173 efi_status_t r;
174
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200175 if (!this)
176 return EFI_INVALID_PARAMETER;
177 /* TODO: check for media changes */
178 if (media_id != this->media->media_id)
179 return EFI_MEDIA_CHANGED;
180 if (!this->media->media_present)
181 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100182 /* media->io_align is a power of 2 or 0 */
183 if (this->media->io_align &&
184 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200185 return EFI_INVALID_PARAMETER;
186 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100187 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200188 return EFI_INVALID_PARAMETER;
189
Alexander Graf51735ae2016-05-11 18:25:48 +0200190#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
191 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
192 r = efi_disk_read_blocks(this, media_id, lba,
193 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
194 if (r != EFI_SUCCESS)
195 return r;
196 return efi_disk_read_blocks(this, media_id, lba +
197 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
198 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
199 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
200 }
201
202 real_buffer = efi_bounce_buffer;
203#endif
204
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900205 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200206 buffer_size, buffer);
207
208 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
209 EFI_DISK_READ);
210
211 /* Copy from bounce buffer to real buffer if necessary */
212 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
213 memcpy(buffer, real_buffer, buffer_size);
214
215 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100216}
217
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200218/**
219 * efi_disk_write_blocks() - writes blocks to device
220 *
221 * This function implements the WriteBlocks service of the
222 * EFI_BLOCK_IO_PROTOCOL.
223 *
224 * See the Unified Extensible Firmware Interface (UEFI) specification for
225 * details.
226 *
227 * @this: pointer to the BLOCK_IO_PROTOCOL
228 * @media_id: id of the medium to be written to
229 * @lba: starting logical block for writing
230 * @buffer_size: size of the write buffer
231 * @buffer: pointer to the source buffer
232 * Return: status code
233 */
Simon Glasse2754582016-09-25 15:27:32 -0600234static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100235 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100236 void *buffer)
237{
Alexander Graf51735ae2016-05-11 18:25:48 +0200238 void *real_buffer = buffer;
239 efi_status_t r;
240
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200241 if (!this)
242 return EFI_INVALID_PARAMETER;
243 if (this->media->read_only)
244 return EFI_WRITE_PROTECTED;
245 /* TODO: check for media changes */
246 if (media_id != this->media->media_id)
247 return EFI_MEDIA_CHANGED;
248 if (!this->media->media_present)
249 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100250 /* media->io_align is a power of 2 or 0 */
251 if (this->media->io_align &&
252 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200253 return EFI_INVALID_PARAMETER;
254 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100255 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200256 return EFI_INVALID_PARAMETER;
257
Alexander Graf51735ae2016-05-11 18:25:48 +0200258#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
259 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
260 r = efi_disk_write_blocks(this, media_id, lba,
261 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
262 if (r != EFI_SUCCESS)
263 return r;
264 return efi_disk_write_blocks(this, media_id, lba +
265 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
266 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
267 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
268 }
269
270 real_buffer = efi_bounce_buffer;
271#endif
272
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900273 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200274 buffer_size, buffer);
275
276 /* Populate bounce buffer if necessary */
277 if (real_buffer != buffer)
278 memcpy(real_buffer, buffer, buffer_size);
279
280 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
281 EFI_DISK_WRITE);
282
283 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100284}
285
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200286/**
287 * efi_disk_flush_blocks() - flushes modified data to the device
288 *
289 * This function implements the FlushBlocks service of the
290 * EFI_BLOCK_IO_PROTOCOL.
291 *
292 * As we always write synchronously nothing is done here.
293 *
294 * See the Unified Extensible Firmware Interface (UEFI) specification for
295 * details.
296 *
297 * @this: pointer to the BLOCK_IO_PROTOCOL
298 * Return: status code
299 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100300static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
301{
Alexander Graf2a22d052016-03-04 01:10:02 +0100302 EFI_ENTRY("%p", this);
303 return EFI_EXIT(EFI_SUCCESS);
304}
305
306static const struct efi_block_io block_io_disk_template = {
307 .reset = &efi_disk_reset,
308 .read_blocks = &efi_disk_read_blocks,
309 .write_blocks = &efi_disk_write_blocks,
310 .flush_blocks = &efi_disk_flush_blocks,
311};
312
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100313/**
314 * efi_fs_from_path() - retrieve simple file system protocol
315 *
316 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100317 *
318 * The full path provided is split into device part and into a file
319 * part. The device part is used to find the handle on which the
320 * simple file system protocol is installed.
321 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100322 * @full_path: device path including device and file
323 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400324 */
325struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100326efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400327{
328 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100329 struct efi_handler *handler;
330 struct efi_device_path *device_path;
331 struct efi_device_path *file_path;
332 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400333
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100334 /* Split the path into a device part and a file part */
335 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
336 if (ret != EFI_SUCCESS)
337 return NULL;
338 efi_free_pool(file_path);
339
340 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100341 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100342 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400343 if (!efiobj)
344 return NULL;
345
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100346 /* Find the simple file system protocol */
347 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
348 &handler);
349 if (ret != EFI_SUCCESS)
350 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400351
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100352 /* Return the simple file system protocol for the partition */
353 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400354}
355
AKASHI Takahiro86740062019-10-07 14:59:38 +0900356/**
357 * efi_fs_exists() - check if a partition bears a file system
358 *
359 * @desc: block device descriptor
360 * @part: partition number
361 * Return: 1 if a file system exists on the partition
362 * 0 otherwise
363 */
364static int efi_fs_exists(struct blk_desc *desc, int part)
365{
366 if (fs_set_blk_dev_with_part(desc, part))
367 return 0;
368
369 if (fs_get_type() == FS_TYPE_ANY)
370 return 0;
371
372 fs_close();
373
374 return 1;
375}
376
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200377/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100378 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200379 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100380 * @parent: parent handle
381 * @dp_parent: parent device path
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100382 * @desc: internal block device
383 * @dev_index: device index for block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100384 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200385 * @part: partition
386 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100387 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200388 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100389static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100390 efi_handle_t parent,
391 struct efi_device_path *dp_parent,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100392 struct blk_desc *desc,
393 int dev_index,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100394 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100395 unsigned int part,
396 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200397{
398 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200399 struct efi_object *handle;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200400 const efi_guid_t *esp_guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100401 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200402
Alexander Graf0812d1a2016-08-05 14:51:47 +0200403 /* Don't add empty devices */
404 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100405 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200406
Rob Clark884bcf62017-09-13 18:05:31 -0400407 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100408 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100409 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100410
411 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200412 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200413
414 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100415 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100416 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100417 struct efi_handler *handler;
418 void *protocol_interface;
419
Heinrich Schuchardt01caf282022-10-06 13:36:02 +0200420 if (!node) {
421 ret = EFI_OUT_OF_RESOURCES;
422 goto error;
423 }
424
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100425 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
426 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
427 if (ret != EFI_SUCCESS)
428 goto error;
429
430 /*
431 * Link the partition (child controller) to the block device
432 * (controller).
433 */
434 ret = efi_protocol_open(handler, &protocol_interface, NULL,
435 &diskobj->header,
436 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
437 if (ret != EFI_SUCCESS)
438 goto error;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100439
440 diskobj->dp = efi_dp_append_node(dp_parent, node);
441 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100442 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100443 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200444 esp_guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100445 } else {
446 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100447 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100448 }
Rob Clark884bcf62017-09-13 18:05:31 -0400449 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200450
451 /*
452 * Install the device path and the block IO protocol.
453 *
454 * InstallMultipleProtocolInterfaces() checks if the device path is
455 * already installed on an other handle and returns EFI_ALREADY_STARTED
456 * in this case.
457 */
458 handle = &diskobj->header;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200459 ret = efi_install_multiple_protocol_interfaces(
460 &handle,
461 &efi_guid_device_path, diskobj->dp,
462 &efi_block_io_guid, &diskobj->ops,
463 /*
464 * esp_guid must be last entry as it
465 * can be NULL. Its interface is NULL.
466 */
467 esp_guid, NULL,
468 NULL);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100469 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100470 goto error;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200471
472 /*
473 * On partitions or whole disks without partitions install the
474 * simple file system protocol if a file system is available.
475 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900476 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
477 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400478 diskobj->volume = efi_simple_file_system(desc, part,
479 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200480 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100481 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100482 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100483 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100484 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400485 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200486 diskobj->ops = block_io_disk_template;
Alexander Graf4a12a972016-04-11 16:16:17 +0200487 diskobj->dev_index = dev_index;
488
489 /* Fill in EFI IO Media info (for read/write callbacks) */
490 diskobj->media.removable_media = desc->removable;
491 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200492 /*
493 * MediaID is just an arbitrary counter.
494 * We have to change it if the medium is removed or changed.
495 */
496 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200497 diskobj->media.block_size = desc->blksz;
498 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100499 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100500 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200501 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100502 if (disk)
503 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100504
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100505 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
Paul Barbieri7a85f322022-06-30 07:02:04 -0400506 ", last_block %llu\n",
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100507 diskobj->part,
508 diskobj->media.media_present,
509 diskobj->media.logical_partition,
510 diskobj->media.removable_media,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100511 diskobj->media.last_block);
512
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100513 /* Store first EFI system partition */
Simon Glass8149b152022-09-17 09:00:09 -0600514 if (part && !efi_system_partition.uclass_id) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100515 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Simon Glass8149b152022-09-17 09:00:09 -0600516 efi_system_partition.uclass_id = desc->uclass_id;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100517 efi_system_partition.devnum = desc->devnum;
518 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200519 EFI_PRINT("EFI system partition: %s %x:%x\n",
Simon Glass8149b152022-09-17 09:00:09 -0600520 blk_get_uclass_name(desc->uclass_id),
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100521 desc->devnum, part);
522 }
523 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100524 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100525error:
526 efi_delete_handle(&diskobj->header);
527 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200528}
529
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900530/*
531 * Create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100532 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900533 * @dev uclass device (UCLASS_BLK)
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100534 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900535 * Create an efi_disk object which is associated with @dev.
536 * The type of @dev must be UCLASS_BLK.
537 *
538 * @return 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100539 */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900540static int efi_disk_create_raw(struct udevice *dev)
Alexander Graf2a22d052016-03-04 01:10:02 +0100541{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100542 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900543 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900544 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100545 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600546
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900547 desc = dev_get_uclass_plat(dev);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900548 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600549
AKASHI Takahiroab31c8a2022-08-19 09:48:30 +0900550 ret = efi_disk_add_dev(NULL, NULL, desc,
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900551 diskid, NULL, 0, &disk);
552 if (ret != EFI_SUCCESS) {
553 if (ret == EFI_NOT_READY)
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200554 log_notice("Disk %s not ready\n", dev->name);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900555 else
556 log_err("Adding disk for %s failed\n", dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600557
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900558 return -1;
559 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900560 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900561 efi_free_pool(disk->dp);
562 efi_delete_handle(&disk->header);
563
564 return -1;
Simon Glass487d7562016-05-14 14:03:05 -0600565 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100566
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900567 return 0;
568}
569
570/*
571 * Create a handle for a disk partition
572 *
573 * @dev uclass device (UCLASS_PARTITION)
574 *
575 * Create an efi_disk object which is associated with @dev.
576 * The type of @dev must be UCLASS_PARTITION.
577 *
578 * @return 0 on success, -1 otherwise
579 */
580static int efi_disk_create_part(struct udevice *dev)
581{
582 efi_handle_t parent;
583 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900584 struct disk_part *part_data;
585 struct disk_partition *info;
586 unsigned int part;
587 int diskid;
588 struct efi_handler *handler;
589 struct efi_device_path *dp_parent;
590 struct efi_disk_obj *disk;
591 efi_status_t ret;
592
593 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
594 return -1;
595
596 desc = dev_get_uclass_plat(dev_get_parent(dev));
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900597 diskid = desc->devnum;
598
599 part_data = dev_get_uclass_plat(dev);
600 part = part_data->partnum;
601 info = &part_data->gpt_part_info;
602
603 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
604 if (ret != EFI_SUCCESS)
605 return -1;
606 dp_parent = (struct efi_device_path *)handler->protocol_interface;
607
AKASHI Takahiroab31c8a2022-08-19 09:48:30 +0900608 ret = efi_disk_add_dev(parent, dp_parent, desc, diskid,
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900609 info, part, &disk);
610 if (ret != EFI_SUCCESS) {
611 log_err("Adding partition for %s failed\n", dev->name);
612 return -1;
613 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900614 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900615 efi_free_pool(disk->dp);
616 efi_delete_handle(&disk->header);
617
618 return -1;
619 }
620
621 return 0;
622}
623
624/*
625 * Create efi_disk objects for a block device
626 *
627 * @dev uclass device (UCLASS_BLK)
628 *
629 * Create efi_disk objects for partitions as well as a raw disk
630 * which is associated with @dev.
631 * The type of @dev must be UCLASS_BLK.
632 * This function is expected to be called at EV_PM_POST_PROBE.
633 *
634 * @return 0 on success, -1 otherwise
635 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200636int efi_disk_probe(void *ctx, struct event *event)
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900637{
638 struct udevice *dev;
639 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900640 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900641 struct udevice *child;
642 int ret;
643
644 dev = event->data.dm.dev;
645 id = device_get_uclass_id(dev);
646
647 /* TODO: We won't support partitions in a partition */
648 if (id != UCLASS_BLK)
649 return 0;
650
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900651 /*
652 * avoid creating duplicated objects now that efi_driver
653 * has already created an efi_disk at this moment.
654 */
655 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600656 if (desc->uclass_id != UCLASS_EFI_LOADER) {
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900657 ret = efi_disk_create_raw(dev);
658 if (ret)
659 return -1;
660 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900661
662 device_foreach_child(child, dev) {
663 ret = efi_disk_create_part(child);
664 if (ret)
665 return -1;
666 }
667
668 return 0;
669}
670
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900671/*
672 * Delete an efi_disk object for a whole raw disk
673 *
674 * @dev uclass device (UCLASS_BLK)
675 *
676 * Delete an efi_disk object which is associated with @dev.
677 * The type of @dev must be UCLASS_BLK.
678 *
679 * @return 0 on success, -1 otherwise
680 */
681static int efi_disk_delete_raw(struct udevice *dev)
682{
683 efi_handle_t handle;
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900684 struct blk_desc *desc;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900685 struct efi_disk_obj *diskobj;
686
687 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
688 return -1;
689
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900690 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600691 if (desc->uclass_id != UCLASS_EFI_LOADER) {
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900692 diskobj = container_of(handle, struct efi_disk_obj, header);
693 efi_free_pool(diskobj->dp);
694 }
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900695
696 efi_delete_handle(handle);
697 dev_tag_del(dev, DM_TAG_EFI);
698
699 return 0;
700}
701
702/*
703 * Delete an efi_disk object for a disk partition
704 *
705 * @dev uclass device (UCLASS_PARTITION)
706 *
707 * Delete an efi_disk object which is associated with @dev.
708 * The type of @dev must be UCLASS_PARTITION.
709 *
710 * @return 0 on success, -1 otherwise
711 */
712static int efi_disk_delete_part(struct udevice *dev)
713{
714 efi_handle_t handle;
715 struct efi_disk_obj *diskobj;
716
717 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
718 return -1;
719
720 diskobj = container_of(handle, struct efi_disk_obj, header);
721
722 efi_free_pool(diskobj->dp);
723 efi_delete_handle(handle);
724 dev_tag_del(dev, DM_TAG_EFI);
725
726 return 0;
727}
728
729/*
730 * Delete an efi_disk object for a block device
731 *
732 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
733 *
734 * Delete an efi_disk object which is associated with @dev.
735 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
736 * This function is expected to be called at EV_PM_PRE_REMOVE.
737 *
738 * @return 0 on success, -1 otherwise
739 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200740int efi_disk_remove(void *ctx, struct event *event)
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900741{
742 enum uclass_id id;
743 struct udevice *dev;
744
745 dev = event->data.dm.dev;
746 id = device_get_uclass_id(dev);
747
748 if (id == UCLASS_BLK)
749 return efi_disk_delete_raw(dev);
750 else if (id == UCLASS_PARTITION)
751 return efi_disk_delete_part(dev);
752 else
753 return 0;
754}
755
Masahisa Kojima87d79142022-09-12 17:33:50 +0900756/**
757 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
758 *
759 * @handle: pointer to the EFI handle
760 * @buf: pointer to the buffer to store the string
761 * @size: size of buffer
762 * Return: status code
763 */
764efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
765{
766 int count;
767 int diskid;
768 enum uclass_id id;
769 unsigned int part;
770 struct udevice *dev;
771 struct blk_desc *desc;
772 const char *if_typename;
773 bool is_partition = false;
774 struct disk_part *part_data;
775
776 if (!handle || !buf || !size)
777 return EFI_INVALID_PARAMETER;
778
779 dev = handle->dev;
780 id = device_get_uclass_id(dev);
781 if (id == UCLASS_BLK) {
782 desc = dev_get_uclass_plat(dev);
783 } else if (id == UCLASS_PARTITION) {
784 desc = dev_get_uclass_plat(dev_get_parent(dev));
785 is_partition = true;
786 } else {
787 return EFI_INVALID_PARAMETER;
788 }
Simon Glass8149b152022-09-17 09:00:09 -0600789 if_typename = blk_get_uclass_name(desc->uclass_id);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900790 diskid = desc->devnum;
791
792 if (is_partition) {
793 part_data = dev_get_uclass_plat(dev);
794 part = part_data->partnum;
Heinrich Schuchardt2eeb7fe2022-10-07 12:55:16 +0200795 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
796 part);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900797 } else {
798 count = snprintf(buf, size, "%s %d", if_typename, diskid);
799 }
800
801 if (count < 0 || (count + 1) > size)
802 return EFI_INVALID_PARAMETER;
803
804 return EFI_SUCCESS;
805}
Tom Rinie9a1ff92022-09-19 13:19:39 -0400806
807/**
Heinrich Schuchardtd5391bf2022-08-31 16:37:35 +0200808 * efi_disks_register() - ensure all block devices are available in UEFI
809 *
810 * The function probes all block devices. As we store UEFI variables on the
811 * EFI system partition this function has to be called before enabling
812 * variable services.
813 */
814efi_status_t efi_disks_register(void)
815{
816 struct udevice *dev;
817
818 uclass_foreach_dev_probe(UCLASS_BLK, dev) {
819 }
820
821 return EFI_SUCCESS;
822}