blob: 8fb5b2363c451202275ada9ca9208241e15376f7 [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
32 * @ifname: interface name for block device
33 * @dev_index: device index of block device
34 * @media: block I/O media information
35 * @dp: device path to the block device
36 * @part: partition
37 * @volume: simple file system protocol of the partition
38 * @offset: offset into disk for simple partition
AKASHI Takahirod97e98c2022-04-19 10:05:17 +090039 * @dev: associated DM device
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020040 */
Alexander Graf2a22d052016-03-04 01:10:02 +010041struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020042 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010043 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010044 const char *ifname;
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 Graf8c3df0b2016-04-11 16:16:18 +020050 lbaint_t offset;
AKASHI Takahirod97e98c2022-04-19 10:05:17 +090051 struct udevice *dev; /* TODO: move it to efi_object */
Alexander Graf2a22d052016-03-04 01:10:02 +010052};
53
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020054/**
55 * efi_disk_reset() - reset block device
56 *
57 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
58 *
59 * As U-Boot's block devices do not have a reset function simply return
60 * EFI_SUCCESS.
61 *
62 * See the Unified Extensible Firmware Interface (UEFI) specification for
63 * details.
64 *
65 * @this: pointer to the BLOCK_IO_PROTOCOL
66 * @extended_verification: extended verification
67 * Return: status code
68 */
Alexander Graf2a22d052016-03-04 01:10:02 +010069static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
70 char extended_verification)
71{
72 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020073 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010074}
75
76enum efi_disk_direction {
77 EFI_DISK_READ,
78 EFI_DISK_WRITE,
79};
80
Heinrich Schuchardta80a2322017-08-26 22:33:13 +020081static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010082 u32 media_id, u64 lba, unsigned long buffer_size,
83 void *buffer, enum efi_disk_direction direction)
84{
85 struct efi_disk_obj *diskobj;
Alexander Graf2a22d052016-03-04 01:10:02 +010086 int blksz;
87 int blocks;
88 unsigned long n;
89
Alexander Graf2a22d052016-03-04 01:10:02 +010090 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahirod97e98c2022-04-19 10:05:17 +090091 blksz = diskobj->media.block_size;
Alexander Graf2a22d052016-03-04 01:10:02 +010092 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020093 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010094
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +020095 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
96 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010097
98 /* We only support full block access */
99 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200100 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +0100101
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900102#if CONFIG_IS_ENABLED(PARTITIONS)
103 if (direction == EFI_DISK_READ)
104 n = dev_read(diskobj->dev, lba, blocks, buffer);
105 else
106 n = dev_write(diskobj->dev, lba, blocks, buffer);
107#else
108 /* dev is always a block device (UCLASS_BLK) */
109 struct blk_desc *desc;
110
111 desc = dev_get_uclass_plat(diskobj->dev);
Alexander Graf2a22d052016-03-04 01:10:02 +0100112 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -0600113 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100114 else
Simon Glass487d7562016-05-14 14:03:05 -0600115 n = blk_dwrite(desc, lba, blocks, buffer);
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900116#endif
Alexander Graf2a22d052016-03-04 01:10:02 +0100117
118 /* We don't do interrupts, so check for timers cooperatively */
119 efi_timer_check();
120
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200121 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200122
Alexander Graf2a22d052016-03-04 01:10:02 +0100123 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400124 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100125
Rob Clark33049902017-07-25 20:28:29 -0400126 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100127}
128
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200129/**
130 * efi_disk_read_blocks() - reads blocks from device
131 *
132 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
133 *
134 * See the Unified Extensible Firmware Interface (UEFI) specification for
135 * details.
136 *
137 * @this: pointer to the BLOCK_IO_PROTOCOL
138 * @media_id: id of the medium to be read from
139 * @lba: starting logical block for reading
140 * @buffer_size: size of the read buffer
141 * @buffer: pointer to the destination buffer
142 * Return: status code
143 */
Simon Glasse2754582016-09-25 15:27:32 -0600144static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100145 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100146 void *buffer)
147{
Alexander Graf51735ae2016-05-11 18:25:48 +0200148 void *real_buffer = buffer;
149 efi_status_t r;
150
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200151 if (!this)
152 return EFI_INVALID_PARAMETER;
153 /* TODO: check for media changes */
154 if (media_id != this->media->media_id)
155 return EFI_MEDIA_CHANGED;
156 if (!this->media->media_present)
157 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100158 /* media->io_align is a power of 2 or 0 */
159 if (this->media->io_align &&
160 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200161 return EFI_INVALID_PARAMETER;
162 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100163 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200164 return EFI_INVALID_PARAMETER;
165
Alexander Graf51735ae2016-05-11 18:25:48 +0200166#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
167 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
168 r = efi_disk_read_blocks(this, media_id, lba,
169 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
170 if (r != EFI_SUCCESS)
171 return r;
172 return efi_disk_read_blocks(this, media_id, lba +
173 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
174 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
175 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
176 }
177
178 real_buffer = efi_bounce_buffer;
179#endif
180
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900181 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200182 buffer_size, buffer);
183
184 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
185 EFI_DISK_READ);
186
187 /* Copy from bounce buffer to real buffer if necessary */
188 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
189 memcpy(buffer, real_buffer, buffer_size);
190
191 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100192}
193
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200194/**
195 * efi_disk_write_blocks() - writes blocks to device
196 *
197 * This function implements the WriteBlocks service of the
198 * EFI_BLOCK_IO_PROTOCOL.
199 *
200 * See the Unified Extensible Firmware Interface (UEFI) specification for
201 * details.
202 *
203 * @this: pointer to the BLOCK_IO_PROTOCOL
204 * @media_id: id of the medium to be written to
205 * @lba: starting logical block for writing
206 * @buffer_size: size of the write buffer
207 * @buffer: pointer to the source buffer
208 * Return: status code
209 */
Simon Glasse2754582016-09-25 15:27:32 -0600210static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100211 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100212 void *buffer)
213{
Alexander Graf51735ae2016-05-11 18:25:48 +0200214 void *real_buffer = buffer;
215 efi_status_t r;
216
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200217 if (!this)
218 return EFI_INVALID_PARAMETER;
219 if (this->media->read_only)
220 return EFI_WRITE_PROTECTED;
221 /* TODO: check for media changes */
222 if (media_id != this->media->media_id)
223 return EFI_MEDIA_CHANGED;
224 if (!this->media->media_present)
225 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100226 /* media->io_align is a power of 2 or 0 */
227 if (this->media->io_align &&
228 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200229 return EFI_INVALID_PARAMETER;
230 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100231 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200232 return EFI_INVALID_PARAMETER;
233
Alexander Graf51735ae2016-05-11 18:25:48 +0200234#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
235 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
236 r = efi_disk_write_blocks(this, media_id, lba,
237 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
238 if (r != EFI_SUCCESS)
239 return r;
240 return efi_disk_write_blocks(this, media_id, lba +
241 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
242 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
243 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
244 }
245
246 real_buffer = efi_bounce_buffer;
247#endif
248
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900249 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200250 buffer_size, buffer);
251
252 /* Populate bounce buffer if necessary */
253 if (real_buffer != buffer)
254 memcpy(real_buffer, buffer, buffer_size);
255
256 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
257 EFI_DISK_WRITE);
258
259 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100260}
261
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200262/**
263 * efi_disk_flush_blocks() - flushes modified data to the device
264 *
265 * This function implements the FlushBlocks service of the
266 * EFI_BLOCK_IO_PROTOCOL.
267 *
268 * As we always write synchronously nothing is done here.
269 *
270 * See the Unified Extensible Firmware Interface (UEFI) specification for
271 * details.
272 *
273 * @this: pointer to the BLOCK_IO_PROTOCOL
274 * Return: status code
275 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100276static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
277{
Alexander Graf2a22d052016-03-04 01:10:02 +0100278 EFI_ENTRY("%p", this);
279 return EFI_EXIT(EFI_SUCCESS);
280}
281
282static const struct efi_block_io block_io_disk_template = {
283 .reset = &efi_disk_reset,
284 .read_blocks = &efi_disk_read_blocks,
285 .write_blocks = &efi_disk_write_blocks,
286 .flush_blocks = &efi_disk_flush_blocks,
287};
288
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100289/**
290 * efi_fs_from_path() - retrieve simple file system protocol
291 *
292 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100293 *
294 * The full path provided is split into device part and into a file
295 * part. The device part is used to find the handle on which the
296 * simple file system protocol is installed.
297 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100298 * @full_path: device path including device and file
299 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400300 */
301struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100302efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400303{
304 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100305 struct efi_handler *handler;
306 struct efi_device_path *device_path;
307 struct efi_device_path *file_path;
308 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400309
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100310 /* Split the path into a device part and a file part */
311 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
312 if (ret != EFI_SUCCESS)
313 return NULL;
314 efi_free_pool(file_path);
315
316 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100317 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100318 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400319 if (!efiobj)
320 return NULL;
321
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100322 /* Find the simple file system protocol */
323 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
324 &handler);
325 if (ret != EFI_SUCCESS)
326 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400327
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100328 /* Return the simple file system protocol for the partition */
329 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400330}
331
AKASHI Takahiro86740062019-10-07 14:59:38 +0900332/**
333 * efi_fs_exists() - check if a partition bears a file system
334 *
335 * @desc: block device descriptor
336 * @part: partition number
337 * Return: 1 if a file system exists on the partition
338 * 0 otherwise
339 */
340static int efi_fs_exists(struct blk_desc *desc, int part)
341{
342 if (fs_set_blk_dev_with_part(desc, part))
343 return 0;
344
345 if (fs_get_type() == FS_TYPE_ANY)
346 return 0;
347
348 fs_close();
349
350 return 1;
351}
352
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200353/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100354 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200355 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100356 * @parent: parent handle
357 * @dp_parent: parent device path
358 * @if_typename: interface name for block device
359 * @desc: internal block device
360 * @dev_index: device index for block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100361 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200362 * @part: partition
363 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100364 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200365 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100366static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100367 efi_handle_t parent,
368 struct efi_device_path *dp_parent,
369 const char *if_typename,
370 struct blk_desc *desc,
371 int dev_index,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100372 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100373 unsigned int part,
374 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200375{
376 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200377 struct efi_object *handle;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100378 const efi_guid_t *guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100379 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200380
Alexander Graf0812d1a2016-08-05 14:51:47 +0200381 /* Don't add empty devices */
382 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100383 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200384
Rob Clark884bcf62017-09-13 18:05:31 -0400385 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100386 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100387 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100388
389 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200390 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200391
392 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100393 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100394 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100395 struct efi_handler *handler;
396 void *protocol_interface;
397
398 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
399 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
400 if (ret != EFI_SUCCESS)
401 goto error;
402
403 /*
404 * Link the partition (child controller) to the block device
405 * (controller).
406 */
407 ret = efi_protocol_open(handler, &protocol_interface, NULL,
408 &diskobj->header,
409 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
410 if (ret != EFI_SUCCESS)
411 goto error;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100412
413 diskobj->dp = efi_dp_append_node(dp_parent, node);
414 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100415 diskobj->offset = part_info->start;
416 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100417 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
418 guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100419 } else {
420 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100421 diskobj->offset = 0;
422 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100423 }
Rob Clark884bcf62017-09-13 18:05:31 -0400424 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200425
426 /*
427 * Install the device path and the block IO protocol.
428 *
429 * InstallMultipleProtocolInterfaces() checks if the device path is
430 * already installed on an other handle and returns EFI_ALREADY_STARTED
431 * in this case.
432 */
433 handle = &diskobj->header;
434 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
435 &handle, &efi_guid_device_path, diskobj->dp,
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100436 &efi_block_io_guid, &diskobj->ops,
437 guid, NULL, NULL));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100438 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100439 goto error;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200440
441 /*
442 * On partitions or whole disks without partitions install the
443 * simple file system protocol if a file system is available.
444 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900445 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
446 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400447 diskobj->volume = efi_simple_file_system(desc, part,
448 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200449 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100450 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100451 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100452 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100453 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400454 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200455 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600456 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200457 diskobj->dev_index = dev_index;
458
459 /* Fill in EFI IO Media info (for read/write callbacks) */
460 diskobj->media.removable_media = desc->removable;
461 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200462 /*
463 * MediaID is just an arbitrary counter.
464 * We have to change it if the medium is removed or changed.
465 */
466 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200467 diskobj->media.block_size = desc->blksz;
468 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100469 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100470 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200471 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100472 if (disk)
473 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100474
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100475 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
476 ", offset " LBAF ", last_block %llu\n",
477 diskobj->part,
478 diskobj->media.media_present,
479 diskobj->media.logical_partition,
480 diskobj->media.removable_media,
481 diskobj->offset,
482 diskobj->media.last_block);
483
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100484 /* Store first EFI system partition */
485 if (part && !efi_system_partition.if_type) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100486 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100487 efi_system_partition.if_type = desc->if_type;
488 efi_system_partition.devnum = desc->devnum;
489 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200490 EFI_PRINT("EFI system partition: %s %x:%x\n",
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100491 blk_get_if_type_name(desc->if_type),
492 desc->devnum, part);
493 }
494 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100495 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100496error:
497 efi_delete_handle(&diskobj->header);
498 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200499}
500
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900501/*
502 * Create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100503 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900504 * @dev uclass device (UCLASS_BLK)
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100505 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900506 * Create an efi_disk object which is associated with @dev.
507 * The type of @dev must be UCLASS_BLK.
508 *
509 * @return 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100510 */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900511static int efi_disk_create_raw(struct udevice *dev)
Alexander Graf2a22d052016-03-04 01:10:02 +0100512{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100513 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900514 struct blk_desc *desc;
515 const char *if_typename;
516 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100517 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600518
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900519 desc = dev_get_uclass_plat(dev);
520 if_typename = blk_get_if_type_name(desc->if_type);
521 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600522
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900523 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
524 diskid, NULL, 0, &disk);
525 if (ret != EFI_SUCCESS) {
526 if (ret == EFI_NOT_READY)
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200527 log_notice("Disk %s not ready\n", dev->name);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900528 else
529 log_err("Adding disk for %s failed\n", dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600530
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900531 return -1;
532 }
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900533 disk->dev = dev;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900534 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
535 efi_free_pool(disk->dp);
536 efi_delete_handle(&disk->header);
537
538 return -1;
Simon Glass487d7562016-05-14 14:03:05 -0600539 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100540
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900541 return 0;
542}
543
544/*
545 * Create a handle for a disk partition
546 *
547 * @dev uclass device (UCLASS_PARTITION)
548 *
549 * Create an efi_disk object which is associated with @dev.
550 * The type of @dev must be UCLASS_PARTITION.
551 *
552 * @return 0 on success, -1 otherwise
553 */
554static int efi_disk_create_part(struct udevice *dev)
555{
556 efi_handle_t parent;
557 struct blk_desc *desc;
558 const char *if_typename;
559 struct disk_part *part_data;
560 struct disk_partition *info;
561 unsigned int part;
562 int diskid;
563 struct efi_handler *handler;
564 struct efi_device_path *dp_parent;
565 struct efi_disk_obj *disk;
566 efi_status_t ret;
567
568 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
569 return -1;
570
571 desc = dev_get_uclass_plat(dev_get_parent(dev));
572 if_typename = blk_get_if_type_name(desc->if_type);
573 diskid = desc->devnum;
574
575 part_data = dev_get_uclass_plat(dev);
576 part = part_data->partnum;
577 info = &part_data->gpt_part_info;
578
579 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
580 if (ret != EFI_SUCCESS)
581 return -1;
582 dp_parent = (struct efi_device_path *)handler->protocol_interface;
583
584 ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid,
585 info, part, &disk);
586 if (ret != EFI_SUCCESS) {
587 log_err("Adding partition for %s failed\n", dev->name);
588 return -1;
589 }
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900590 disk->dev = dev;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900591 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
592 efi_free_pool(disk->dp);
593 efi_delete_handle(&disk->header);
594
595 return -1;
596 }
597
598 return 0;
599}
600
601/*
602 * Create efi_disk objects for a block device
603 *
604 * @dev uclass device (UCLASS_BLK)
605 *
606 * Create efi_disk objects for partitions as well as a raw disk
607 * which is associated with @dev.
608 * The type of @dev must be UCLASS_BLK.
609 * This function is expected to be called at EV_PM_POST_PROBE.
610 *
611 * @return 0 on success, -1 otherwise
612 */
613static int efi_disk_probe(void *ctx, struct event *event)
614{
615 struct udevice *dev;
616 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900617 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900618 struct udevice *child;
619 int ret;
620
621 dev = event->data.dm.dev;
622 id = device_get_uclass_id(dev);
623
624 /* TODO: We won't support partitions in a partition */
625 if (id != UCLASS_BLK)
626 return 0;
627
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900628 /*
629 * avoid creating duplicated objects now that efi_driver
630 * has already created an efi_disk at this moment.
631 */
632 desc = dev_get_uclass_plat(dev);
633 if (desc->if_type != IF_TYPE_EFI_LOADER) {
634 ret = efi_disk_create_raw(dev);
635 if (ret)
636 return -1;
637 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900638
639 device_foreach_child(child, dev) {
640 ret = efi_disk_create_part(child);
641 if (ret)
642 return -1;
643 }
644
645 return 0;
646}
647
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900648/*
649 * Delete an efi_disk object for a whole raw disk
650 *
651 * @dev uclass device (UCLASS_BLK)
652 *
653 * Delete an efi_disk object which is associated with @dev.
654 * The type of @dev must be UCLASS_BLK.
655 *
656 * @return 0 on success, -1 otherwise
657 */
658static int efi_disk_delete_raw(struct udevice *dev)
659{
660 efi_handle_t handle;
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900661 struct blk_desc *desc;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900662 struct efi_disk_obj *diskobj;
663
664 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
665 return -1;
666
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900667 desc = dev_get_uclass_plat(dev);
668 if (desc->if_type != IF_TYPE_EFI_LOADER) {
669 diskobj = container_of(handle, struct efi_disk_obj, header);
670 efi_free_pool(diskobj->dp);
671 }
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900672
673 efi_delete_handle(handle);
674 dev_tag_del(dev, DM_TAG_EFI);
675
676 return 0;
677}
678
679/*
680 * Delete an efi_disk object for a disk partition
681 *
682 * @dev uclass device (UCLASS_PARTITION)
683 *
684 * Delete an efi_disk object which is associated with @dev.
685 * The type of @dev must be UCLASS_PARTITION.
686 *
687 * @return 0 on success, -1 otherwise
688 */
689static int efi_disk_delete_part(struct udevice *dev)
690{
691 efi_handle_t handle;
692 struct efi_disk_obj *diskobj;
693
694 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
695 return -1;
696
697 diskobj = container_of(handle, struct efi_disk_obj, header);
698
699 efi_free_pool(diskobj->dp);
700 efi_delete_handle(handle);
701 dev_tag_del(dev, DM_TAG_EFI);
702
703 return 0;
704}
705
706/*
707 * Delete an efi_disk object for a block device
708 *
709 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
710 *
711 * Delete an efi_disk object which is associated with @dev.
712 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
713 * This function is expected to be called at EV_PM_PRE_REMOVE.
714 *
715 * @return 0 on success, -1 otherwise
716 */
717static int efi_disk_remove(void *ctx, struct event *event)
718{
719 enum uclass_id id;
720 struct udevice *dev;
721
722 dev = event->data.dm.dev;
723 id = device_get_uclass_id(dev);
724
725 if (id == UCLASS_BLK)
726 return efi_disk_delete_raw(dev);
727 else if (id == UCLASS_PARTITION)
728 return efi_disk_delete_part(dev);
729 else
730 return 0;
731}
732
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900733efi_status_t efi_disk_init(void)
734{
735 int ret;
736
737 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
738 efi_disk_probe, NULL);
739 if (ret) {
740 log_err("Event registration for efi_disk add failed\n");
741 return EFI_OUT_OF_RESOURCES;
742 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100743
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900744 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
745 efi_disk_remove, NULL);
746 if (ret) {
747 log_err("Event registration for efi_disk del failed\n");
748 return EFI_OUT_OF_RESOURCES;
749 }
750
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100751 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100752}