blob: d2256713a8e7ab0698710f782b2bf27933101920 [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>
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +020016#include <efi_driver.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010017#include <efi_loader.h>
AKASHI Takahiro86740062019-10-07 14:59:38 +090018#include <fs.h>
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +020019#include <log.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010020#include <part.h>
21#include <malloc.h>
22
Heinrich Schuchardt2b55ad32022-10-21 08:33:44 +020023struct efi_system_partition efi_system_partition = {
24 .uclass_id = UCLASS_INVALID,
25};
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +010026
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020027const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010028const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010029
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020030/**
31 * struct efi_disk_obj - EFI disk object
32 *
33 * @header: EFI object header
34 * @ops: EFI disk I/O protocol interface
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020035 * @dev_index: device index of block device
36 * @media: block I/O media information
37 * @dp: device path to the block device
38 * @part: partition
39 * @volume: simple file system protocol of the partition
AKASHI Takahirod97e98c2022-04-19 10:05:17 +090040 * @dev: associated DM device
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020041 */
Alexander Graf2a22d052016-03-04 01:10:02 +010042struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020043 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010044 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010045 int dev_index;
Alexander Graf2a22d052016-03-04 01:10:02 +010046 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040047 struct efi_device_path *dp;
Rob Clark884bcf62017-09-13 18:05:31 -040048 unsigned int part;
Rob Clark2a920802017-09-13 18:05:34 -040049 struct efi_simple_file_system_protocol *volume;
Alexander Graf2a22d052016-03-04 01:10:02 +010050};
51
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020052/**
53 * efi_disk_reset() - reset block device
54 *
55 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
56 *
57 * As U-Boot's block devices do not have a reset function simply return
58 * EFI_SUCCESS.
59 *
60 * See the Unified Extensible Firmware Interface (UEFI) specification for
61 * details.
62 *
63 * @this: pointer to the BLOCK_IO_PROTOCOL
64 * @extended_verification: extended verification
65 * Return: status code
66 */
Alexander Graf2a22d052016-03-04 01:10:02 +010067static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
68 char extended_verification)
69{
70 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020071 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010072}
73
AKASHI Takahiro05f391e2022-05-12 11:29:01 +090074/**
75 * efi_disk_is_removable() - check if the device is removable media
76 * @handle: efi object handle;
77 *
78 * Examine the device and determine if the device is a local block device
79 * and removable media.
80 *
81 * Return: true if removable, false otherwise
82 */
83bool efi_disk_is_removable(efi_handle_t handle)
84{
85 struct efi_handler *handler;
86 struct efi_block_io *io;
87 efi_status_t ret;
88
89 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
90 if (ret != EFI_SUCCESS)
91 return false;
92
93 io = handler->protocol_interface;
94
95 if (!io || !io->media)
96 return false;
97
98 return (bool)io->media->removable_media;
99}
100
Alexander Graf2a22d052016-03-04 01:10:02 +0100101enum efi_disk_direction {
102 EFI_DISK_READ,
103 EFI_DISK_WRITE,
104};
105
Heinrich Schuchardta80a2322017-08-26 22:33:13 +0200106static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +0100107 u32 media_id, u64 lba, unsigned long buffer_size,
108 void *buffer, enum efi_disk_direction direction)
109{
110 struct efi_disk_obj *diskobj;
Alexander Graf2a22d052016-03-04 01:10:02 +0100111 int blksz;
112 int blocks;
113 unsigned long n;
114
Alexander Graf2a22d052016-03-04 01:10:02 +0100115 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900116 blksz = diskobj->media.block_size;
Alexander Graf2a22d052016-03-04 01:10:02 +0100117 blocks = buffer_size / blksz;
118
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200119 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
120 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +0100121
122 /* We only support full block access */
123 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200124 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +0100125
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900126 if (CONFIG_IS_ENABLED(PARTITIONS) &&
Masahisa Kojimaee576662022-07-22 11:39:10 +0900127 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900128 if (direction == EFI_DISK_READ)
Simon Glass76c839f2022-10-20 18:22:52 -0600129 n = disk_blk_read(diskobj->header.dev, lba, blocks,
130 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900131 else
Simon Glass76c839f2022-10-20 18:22:52 -0600132 n = disk_blk_write(diskobj->header.dev, lba, blocks,
133 buffer);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900134 } else {
135 /* dev is a block device (UCLASS_BLK) */
136 struct blk_desc *desc;
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900137
Masahisa Kojimaee576662022-07-22 11:39:10 +0900138 desc = dev_get_uclass_plat(diskobj->header.dev);
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900139 if (direction == EFI_DISK_READ)
140 n = blk_dread(desc, lba, blocks, buffer);
141 else
142 n = blk_dwrite(desc, lba, blocks, buffer);
143 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100144
145 /* We don't do interrupts, so check for timers cooperatively */
146 efi_timer_check();
147
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200148 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200149
Alexander Graf2a22d052016-03-04 01:10:02 +0100150 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400151 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100152
Rob Clark33049902017-07-25 20:28:29 -0400153 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100154}
155
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200156/**
157 * efi_disk_read_blocks() - reads blocks from device
158 *
159 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
160 *
161 * See the Unified Extensible Firmware Interface (UEFI) specification for
162 * details.
163 *
164 * @this: pointer to the BLOCK_IO_PROTOCOL
165 * @media_id: id of the medium to be read from
166 * @lba: starting logical block for reading
167 * @buffer_size: size of the read buffer
168 * @buffer: pointer to the destination buffer
169 * Return: status code
170 */
Simon Glasse2754582016-09-25 15:27:32 -0600171static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100172 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100173 void *buffer)
174{
Alexander Graf51735ae2016-05-11 18:25:48 +0200175 void *real_buffer = buffer;
176 efi_status_t r;
177
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200178 if (!this)
179 return EFI_INVALID_PARAMETER;
180 /* TODO: check for media changes */
181 if (media_id != this->media->media_id)
182 return EFI_MEDIA_CHANGED;
183 if (!this->media->media_present)
184 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100185 /* media->io_align is a power of 2 or 0 */
186 if (this->media->io_align &&
187 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200188 return EFI_INVALID_PARAMETER;
189 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100190 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200191 return EFI_INVALID_PARAMETER;
192
Alexander Graf51735ae2016-05-11 18:25:48 +0200193#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
194 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
195 r = efi_disk_read_blocks(this, media_id, lba,
196 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
197 if (r != EFI_SUCCESS)
198 return r;
199 return efi_disk_read_blocks(this, media_id, lba +
200 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
201 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
202 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
203 }
204
205 real_buffer = efi_bounce_buffer;
206#endif
207
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900208 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200209 buffer_size, buffer);
210
211 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
212 EFI_DISK_READ);
213
214 /* Copy from bounce buffer to real buffer if necessary */
215 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
216 memcpy(buffer, real_buffer, buffer_size);
217
218 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100219}
220
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200221/**
222 * efi_disk_write_blocks() - writes blocks to device
223 *
224 * This function implements the WriteBlocks service of the
225 * EFI_BLOCK_IO_PROTOCOL.
226 *
227 * See the Unified Extensible Firmware Interface (UEFI) specification for
228 * details.
229 *
230 * @this: pointer to the BLOCK_IO_PROTOCOL
231 * @media_id: id of the medium to be written to
232 * @lba: starting logical block for writing
233 * @buffer_size: size of the write buffer
234 * @buffer: pointer to the source buffer
235 * Return: status code
236 */
Simon Glasse2754582016-09-25 15:27:32 -0600237static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100238 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100239 void *buffer)
240{
Alexander Graf51735ae2016-05-11 18:25:48 +0200241 void *real_buffer = buffer;
242 efi_status_t r;
243
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200244 if (!this)
245 return EFI_INVALID_PARAMETER;
246 if (this->media->read_only)
247 return EFI_WRITE_PROTECTED;
248 /* TODO: check for media changes */
249 if (media_id != this->media->media_id)
250 return EFI_MEDIA_CHANGED;
251 if (!this->media->media_present)
252 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100253 /* media->io_align is a power of 2 or 0 */
254 if (this->media->io_align &&
255 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200256 return EFI_INVALID_PARAMETER;
257 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100258 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200259 return EFI_INVALID_PARAMETER;
260
Alexander Graf51735ae2016-05-11 18:25:48 +0200261#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
262 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
263 r = efi_disk_write_blocks(this, media_id, lba,
264 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
265 if (r != EFI_SUCCESS)
266 return r;
267 return efi_disk_write_blocks(this, media_id, lba +
268 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
269 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
270 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
271 }
272
273 real_buffer = efi_bounce_buffer;
274#endif
275
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900276 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200277 buffer_size, buffer);
278
279 /* Populate bounce buffer if necessary */
280 if (real_buffer != buffer)
281 memcpy(real_buffer, buffer, buffer_size);
282
283 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
284 EFI_DISK_WRITE);
285
286 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100287}
288
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200289/**
290 * efi_disk_flush_blocks() - flushes modified data to the device
291 *
292 * This function implements the FlushBlocks service of the
293 * EFI_BLOCK_IO_PROTOCOL.
294 *
295 * As we always write synchronously nothing is done here.
296 *
297 * See the Unified Extensible Firmware Interface (UEFI) specification for
298 * details.
299 *
300 * @this: pointer to the BLOCK_IO_PROTOCOL
301 * Return: status code
302 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100303static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
304{
Alexander Graf2a22d052016-03-04 01:10:02 +0100305 EFI_ENTRY("%p", this);
306 return EFI_EXIT(EFI_SUCCESS);
307}
308
309static const struct efi_block_io block_io_disk_template = {
310 .reset = &efi_disk_reset,
311 .read_blocks = &efi_disk_read_blocks,
312 .write_blocks = &efi_disk_write_blocks,
313 .flush_blocks = &efi_disk_flush_blocks,
314};
315
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100316/**
317 * efi_fs_from_path() - retrieve simple file system protocol
318 *
319 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100320 *
321 * The full path provided is split into device part and into a file
322 * part. The device part is used to find the handle on which the
323 * simple file system protocol is installed.
324 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100325 * @full_path: device path including device and file
326 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400327 */
328struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100329efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400330{
331 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100332 struct efi_handler *handler;
333 struct efi_device_path *device_path;
334 struct efi_device_path *file_path;
335 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400336
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100337 /* Split the path into a device part and a file part */
338 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
339 if (ret != EFI_SUCCESS)
340 return NULL;
341 efi_free_pool(file_path);
342
343 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100344 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100345 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400346 if (!efiobj)
347 return NULL;
348
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100349 /* Find the simple file system protocol */
350 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
351 &handler);
352 if (ret != EFI_SUCCESS)
353 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400354
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100355 /* Return the simple file system protocol for the partition */
356 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400357}
358
AKASHI Takahiro86740062019-10-07 14:59:38 +0900359/**
360 * efi_fs_exists() - check if a partition bears a file system
361 *
362 * @desc: block device descriptor
363 * @part: partition number
364 * Return: 1 if a file system exists on the partition
365 * 0 otherwise
366 */
367static int efi_fs_exists(struct blk_desc *desc, int part)
368{
369 if (fs_set_blk_dev_with_part(desc, part))
370 return 0;
371
372 if (fs_get_type() == FS_TYPE_ANY)
373 return 0;
374
375 fs_close();
376
377 return 1;
378}
379
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200380/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100381 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200382 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100383 * @parent: parent handle
384 * @dp_parent: parent device path
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100385 * @desc: internal block device
386 * @dev_index: device index for block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100387 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200388 * @part: partition
389 * @disk: pointer to receive the created handle
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200390 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100391 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200392 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100393static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100394 efi_handle_t parent,
395 struct efi_device_path *dp_parent,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100396 struct blk_desc *desc,
397 int dev_index,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100398 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100399 unsigned int part,
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200400 struct efi_disk_obj **disk,
401 efi_handle_t agent_handle)
Alexander Graf4a12a972016-04-11 16:16:17 +0200402{
403 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200404 struct efi_object *handle;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200405 const efi_guid_t *esp_guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100406 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200407
Alexander Graf0812d1a2016-08-05 14:51:47 +0200408 /* Don't add empty devices */
409 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100410 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200411
Rob Clark884bcf62017-09-13 18:05:31 -0400412 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100413 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100414 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100415
416 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200417 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200418
419 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100420 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100421 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100422 struct efi_handler *handler;
423 void *protocol_interface;
424
Heinrich Schuchardt01caf282022-10-06 13:36:02 +0200425 if (!node) {
426 ret = EFI_OUT_OF_RESOURCES;
Simon Glass3722cc92023-01-17 10:47:32 -0700427 log_debug("no node\n");
Heinrich Schuchardt01caf282022-10-06 13:36:02 +0200428 goto error;
429 }
430
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100431 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
432 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
Simon Glass3722cc92023-01-17 10:47:32 -0700433 if (ret != EFI_SUCCESS) {
434 log_debug("search failed\n");
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100435 goto error;
Simon Glass3722cc92023-01-17 10:47:32 -0700436 }
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100437
438 /*
439 * Link the partition (child controller) to the block device
440 * (controller).
441 */
442 ret = efi_protocol_open(handler, &protocol_interface, NULL,
443 &diskobj->header,
444 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
Simon Glass3722cc92023-01-17 10:47:32 -0700445 if (ret != EFI_SUCCESS) {
446 log_debug("prot open failed\n");
447 goto error;
448 }
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100449
450 diskobj->dp = efi_dp_append_node(dp_parent, node);
451 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100452 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100453 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200454 esp_guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100455 } else {
456 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100457 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100458 }
Rob Clark884bcf62017-09-13 18:05:31 -0400459 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200460
461 /*
462 * Install the device path and the block IO protocol.
463 *
464 * InstallMultipleProtocolInterfaces() checks if the device path is
465 * already installed on an other handle and returns EFI_ALREADY_STARTED
466 * in this case.
467 */
468 handle = &diskobj->header;
Heinrich Schuchardt21c4d7c2022-10-07 11:03:01 +0200469 ret = efi_install_multiple_protocol_interfaces(
470 &handle,
471 &efi_guid_device_path, diskobj->dp,
472 &efi_block_io_guid, &diskobj->ops,
473 /*
474 * esp_guid must be last entry as it
475 * can be NULL. Its interface is NULL.
476 */
477 esp_guid, NULL,
478 NULL);
Simon Glass3722cc92023-01-17 10:47:32 -0700479 if (ret != EFI_SUCCESS) {
480 log_debug("install failed %lx\n", ret);
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100481 goto error;
Simon Glass3722cc92023-01-17 10:47:32 -0700482 }
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200483
484 /*
485 * On partitions or whole disks without partitions install the
486 * simple file system protocol if a file system is available.
487 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900488 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
489 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400490 diskobj->volume = efi_simple_file_system(desc, part,
491 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200492 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100493 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100494 diskobj->volume);
Simon Glass3722cc92023-01-17 10:47:32 -0700495 if (ret != EFI_SUCCESS) {
496 log_debug("simple FS failed\n");
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100497 return ret;
Simon Glass3722cc92023-01-17 10:47:32 -0700498 }
Rob Clark2a920802017-09-13 18:05:34 -0400499 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200500 diskobj->ops = block_io_disk_template;
Alexander Graf4a12a972016-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 Schuchardtf59f0822019-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 Graf4a12a972016-04-11 16:16:17 +0200511 diskobj->media.block_size = desc->blksz;
512 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100513 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100514 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200515 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100516 if (disk)
517 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100518
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100519 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
Paul Barbieri7a85f322022-06-30 07:02:04 -0400520 ", last_block %llu\n",
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100521 diskobj->part,
522 diskobj->media.media_present,
523 diskobj->media.logical_partition,
524 diskobj->media.removable_media,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100525 diskobj->media.last_block);
526
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100527 /* Store first EFI system partition */
Heinrich Schuchardt2b55ad32022-10-21 08:33:44 +0200528 if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100529 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Simon Glass8149b152022-09-17 09:00:09 -0600530 efi_system_partition.uclass_id = desc->uclass_id;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100531 efi_system_partition.devnum = desc->devnum;
532 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200533 EFI_PRINT("EFI system partition: %s %x:%x\n",
Simon Glass8149b152022-09-17 09:00:09 -0600534 blk_get_uclass_name(desc->uclass_id),
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100535 desc->devnum, part);
536 }
537 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100538 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100539error:
540 efi_delete_handle(&diskobj->header);
541 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200542}
543
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200544/**
545 * efi_disk_create_raw() - create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100546 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200547 * @dev: udevice (UCLASS_BLK)
548 * @agent_handle: handle of the EFI block driver
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100549 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900550 * Create an efi_disk object which is associated with @dev.
551 * The type of @dev must be UCLASS_BLK.
552 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200553 * Return: 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100554 */
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200555static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
Alexander Graf2a22d052016-03-04 01:10:02 +0100556{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100557 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900558 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900559 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100560 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600561
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900562 desc = dev_get_uclass_plat(dev);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900563 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600564
AKASHI Takahiroab31c8a2022-08-19 09:48:30 +0900565 ret = efi_disk_add_dev(NULL, NULL, desc,
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200566 diskid, NULL, 0, &disk, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900567 if (ret != EFI_SUCCESS) {
Simon Glass3722cc92023-01-17 10:47:32 -0700568 if (ret == EFI_NOT_READY) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200569 log_notice("Disk %s not ready\n", dev->name);
Simon Glass3722cc92023-01-17 10:47:32 -0700570 ret = -EBUSY;
571 } else {
Simon Glass95201812022-10-29 19:47:17 -0600572 log_err("Adding disk for %s failed (err=%ld/%#lx)\n", dev->name, ret, ret);
Simon Glass3722cc92023-01-17 10:47:32 -0700573 ret = -ENOENT;
574 }
Simon Glass487d7562016-05-14 14:03:05 -0600575
Simon Glass3722cc92023-01-17 10:47:32 -0700576 return ret;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900577 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900578 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900579 efi_free_pool(disk->dp);
580 efi_delete_handle(&disk->header);
581
Simon Glass3722cc92023-01-17 10:47:32 -0700582 return -EINVAL;
Simon Glass487d7562016-05-14 14:03:05 -0600583 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100584
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900585 return 0;
586}
587
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200588/**
589 * efi_disk_create_part() - create a handle for a disk partition
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900590 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200591 * @dev: udevice (UCLASS_PARTITION)
592 * @agent_handle: handle of the EFI block driver
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900593 *
594 * Create an efi_disk object which is associated with @dev.
595 * The type of @dev must be UCLASS_PARTITION.
596 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200597 * Return: 0 on success, -1 otherwise
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900598 */
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200599static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900600{
601 efi_handle_t parent;
602 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900603 struct disk_part *part_data;
604 struct disk_partition *info;
605 unsigned int part;
606 int diskid;
607 struct efi_handler *handler;
608 struct efi_device_path *dp_parent;
609 struct efi_disk_obj *disk;
610 efi_status_t ret;
611
612 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
613 return -1;
614
615 desc = dev_get_uclass_plat(dev_get_parent(dev));
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900616 diskid = desc->devnum;
617
618 part_data = dev_get_uclass_plat(dev);
619 part = part_data->partnum;
620 info = &part_data->gpt_part_info;
621
622 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
623 if (ret != EFI_SUCCESS)
624 return -1;
625 dp_parent = (struct efi_device_path *)handler->protocol_interface;
626
AKASHI Takahiroab31c8a2022-08-19 09:48:30 +0900627 ret = efi_disk_add_dev(parent, dp_parent, desc, diskid,
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200628 info, part, &disk, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900629 if (ret != EFI_SUCCESS) {
630 log_err("Adding partition for %s failed\n", dev->name);
631 return -1;
632 }
Masahisa Kojimaee576662022-07-22 11:39:10 +0900633 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900634 efi_free_pool(disk->dp);
635 efi_delete_handle(&disk->header);
636
637 return -1;
638 }
639
640 return 0;
641}
642
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200643/**
644 * efi_disk_probe() - create efi_disk objects for a block device
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900645 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200646 * @ctx: event context - driver binding protocol
647 * @event: EV_PM_POST_PROBE event
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900648 *
649 * Create efi_disk objects for partitions as well as a raw disk
650 * which is associated with @dev.
651 * The type of @dev must be UCLASS_BLK.
652 * This function is expected to be called at EV_PM_POST_PROBE.
653 *
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200654 * Return: 0 on success, -1 otherwise
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900655 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200656int efi_disk_probe(void *ctx, struct event *event)
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900657{
658 struct udevice *dev;
659 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900660 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900661 struct udevice *child;
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200662 struct efi_driver_binding_extended_protocol *db_prot = ctx;
663 efi_handle_t agent_handle = db_prot->bp.driver_binding_handle;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900664 int ret;
665
666 dev = event->data.dm.dev;
667 id = device_get_uclass_id(dev);
668
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200669 /* We won't support partitions in a partition */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900670 if (id != UCLASS_BLK)
671 return 0;
672
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900673 /*
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200674 * Avoid creating duplicated objects now that efi_driver
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900675 * has already created an efi_disk at this moment.
676 */
677 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600678 if (desc->uclass_id != UCLASS_EFI_LOADER) {
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200679 ret = efi_disk_create_raw(dev, agent_handle);
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900680 if (ret)
681 return -1;
682 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900683
684 device_foreach_child(child, dev) {
Heinrich Schuchardt8e4ec3e2022-10-08 09:11:54 +0200685 ret = efi_disk_create_part(child, agent_handle);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900686 if (ret)
687 return -1;
688 }
689
690 return 0;
691}
692
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900693/*
694 * Delete an efi_disk object for a whole raw disk
695 *
696 * @dev uclass device (UCLASS_BLK)
697 *
698 * Delete an efi_disk object which is associated with @dev.
699 * The type of @dev must be UCLASS_BLK.
700 *
701 * @return 0 on success, -1 otherwise
702 */
703static int efi_disk_delete_raw(struct udevice *dev)
704{
705 efi_handle_t handle;
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900706 struct blk_desc *desc;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900707 struct efi_disk_obj *diskobj;
708
709 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
710 return -1;
711
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900712 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600713 if (desc->uclass_id != UCLASS_EFI_LOADER) {
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900714 diskobj = container_of(handle, struct efi_disk_obj, header);
715 efi_free_pool(diskobj->dp);
716 }
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900717
718 efi_delete_handle(handle);
719 dev_tag_del(dev, DM_TAG_EFI);
720
721 return 0;
722}
723
724/*
725 * Delete an efi_disk object for a disk partition
726 *
727 * @dev uclass device (UCLASS_PARTITION)
728 *
729 * Delete an efi_disk object which is associated with @dev.
730 * The type of @dev must be UCLASS_PARTITION.
731 *
732 * @return 0 on success, -1 otherwise
733 */
734static int efi_disk_delete_part(struct udevice *dev)
735{
736 efi_handle_t handle;
737 struct efi_disk_obj *diskobj;
738
739 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
740 return -1;
741
742 diskobj = container_of(handle, struct efi_disk_obj, header);
743
744 efi_free_pool(diskobj->dp);
745 efi_delete_handle(handle);
746 dev_tag_del(dev, DM_TAG_EFI);
747
748 return 0;
749}
750
751/*
752 * Delete an efi_disk object for a block device
753 *
754 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
755 *
756 * Delete an efi_disk object which is associated with @dev.
757 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
758 * This function is expected to be called at EV_PM_PRE_REMOVE.
759 *
760 * @return 0 on success, -1 otherwise
761 */
Heinrich Schuchardtf05911a2022-10-06 07:29:41 +0200762int efi_disk_remove(void *ctx, struct event *event)
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900763{
764 enum uclass_id id;
765 struct udevice *dev;
766
767 dev = event->data.dm.dev;
768 id = device_get_uclass_id(dev);
769
770 if (id == UCLASS_BLK)
771 return efi_disk_delete_raw(dev);
772 else if (id == UCLASS_PARTITION)
773 return efi_disk_delete_part(dev);
774 else
775 return 0;
776}
777
Masahisa Kojima87d79142022-09-12 17:33:50 +0900778/**
779 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
780 *
781 * @handle: pointer to the EFI handle
782 * @buf: pointer to the buffer to store the string
783 * @size: size of buffer
784 * Return: status code
785 */
786efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
787{
788 int count;
789 int diskid;
790 enum uclass_id id;
791 unsigned int part;
792 struct udevice *dev;
793 struct blk_desc *desc;
794 const char *if_typename;
795 bool is_partition = false;
796 struct disk_part *part_data;
797
798 if (!handle || !buf || !size)
799 return EFI_INVALID_PARAMETER;
800
801 dev = handle->dev;
802 id = device_get_uclass_id(dev);
803 if (id == UCLASS_BLK) {
804 desc = dev_get_uclass_plat(dev);
805 } else if (id == UCLASS_PARTITION) {
806 desc = dev_get_uclass_plat(dev_get_parent(dev));
807 is_partition = true;
808 } else {
809 return EFI_INVALID_PARAMETER;
810 }
Simon Glass8149b152022-09-17 09:00:09 -0600811 if_typename = blk_get_uclass_name(desc->uclass_id);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900812 diskid = desc->devnum;
813
814 if (is_partition) {
815 part_data = dev_get_uclass_plat(dev);
816 part = part_data->partnum;
Heinrich Schuchardt2eeb7fe2022-10-07 12:55:16 +0200817 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
818 part);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900819 } else {
820 count = snprintf(buf, size, "%s %d", if_typename, diskid);
821 }
822
823 if (count < 0 || (count + 1) > size)
824 return EFI_INVALID_PARAMETER;
825
826 return EFI_SUCCESS;
827}
Tom Rinie9a1ff92022-09-19 13:19:39 -0400828
829/**
Heinrich Schuchardtd5391bf2022-08-31 16:37:35 +0200830 * efi_disks_register() - ensure all block devices are available in UEFI
831 *
832 * The function probes all block devices. As we store UEFI variables on the
833 * EFI system partition this function has to be called before enabling
834 * variable services.
835 */
836efi_status_t efi_disks_register(void)
837{
838 struct udevice *dev;
839
840 uclass_foreach_dev_probe(UCLASS_BLK, dev) {
841 }
842
843 return EFI_SUCCESS;
844}