blob: 34bb662e9cc096f96e500691829974830d3acc4b [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
39 * @desc: internal block device descriptor
40 */
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;
Rob Clark884bcf62017-09-13 18:05:31 -040051 struct blk_desc *desc;
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;
86 struct blk_desc *desc;
87 int blksz;
88 int blocks;
89 unsigned long n;
90
Alexander Graf2a22d052016-03-04 01:10:02 +010091 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020092 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010093 blksz = desc->blksz;
94 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020095 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010096
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +020097 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
98 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010099
100 /* We only support full block access */
101 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200102 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +0100103
104 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -0600105 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100106 else
Simon Glass487d7562016-05-14 14:03:05 -0600107 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100108
109 /* We don't do interrupts, so check for timers cooperatively */
110 efi_timer_check();
111
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200112 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200113
Alexander Graf2a22d052016-03-04 01:10:02 +0100114 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400115 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100116
Rob Clark33049902017-07-25 20:28:29 -0400117 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100118}
119
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200120/**
121 * efi_disk_read_blocks() - reads blocks from device
122 *
123 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
124 *
125 * See the Unified Extensible Firmware Interface (UEFI) specification for
126 * details.
127 *
128 * @this: pointer to the BLOCK_IO_PROTOCOL
129 * @media_id: id of the medium to be read from
130 * @lba: starting logical block for reading
131 * @buffer_size: size of the read buffer
132 * @buffer: pointer to the destination buffer
133 * Return: status code
134 */
Simon Glasse2754582016-09-25 15:27:32 -0600135static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100136 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100137 void *buffer)
138{
Alexander Graf51735ae2016-05-11 18:25:48 +0200139 void *real_buffer = buffer;
140 efi_status_t r;
141
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200142 if (!this)
143 return EFI_INVALID_PARAMETER;
144 /* TODO: check for media changes */
145 if (media_id != this->media->media_id)
146 return EFI_MEDIA_CHANGED;
147 if (!this->media->media_present)
148 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100149 /* media->io_align is a power of 2 or 0 */
150 if (this->media->io_align &&
151 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200152 return EFI_INVALID_PARAMETER;
153 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100154 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200155 return EFI_INVALID_PARAMETER;
156
Alexander Graf51735ae2016-05-11 18:25:48 +0200157#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
158 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
159 r = efi_disk_read_blocks(this, media_id, lba,
160 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
161 if (r != EFI_SUCCESS)
162 return r;
163 return efi_disk_read_blocks(this, media_id, lba +
164 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
165 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
166 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
167 }
168
169 real_buffer = efi_bounce_buffer;
170#endif
171
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900172 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200173 buffer_size, buffer);
174
175 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
176 EFI_DISK_READ);
177
178 /* Copy from bounce buffer to real buffer if necessary */
179 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
180 memcpy(buffer, real_buffer, buffer_size);
181
182 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100183}
184
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200185/**
186 * efi_disk_write_blocks() - writes blocks to device
187 *
188 * This function implements the WriteBlocks service of the
189 * EFI_BLOCK_IO_PROTOCOL.
190 *
191 * See the Unified Extensible Firmware Interface (UEFI) specification for
192 * details.
193 *
194 * @this: pointer to the BLOCK_IO_PROTOCOL
195 * @media_id: id of the medium to be written to
196 * @lba: starting logical block for writing
197 * @buffer_size: size of the write buffer
198 * @buffer: pointer to the source buffer
199 * Return: status code
200 */
Simon Glasse2754582016-09-25 15:27:32 -0600201static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100202 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100203 void *buffer)
204{
Alexander Graf51735ae2016-05-11 18:25:48 +0200205 void *real_buffer = buffer;
206 efi_status_t r;
207
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200208 if (!this)
209 return EFI_INVALID_PARAMETER;
210 if (this->media->read_only)
211 return EFI_WRITE_PROTECTED;
212 /* TODO: check for media changes */
213 if (media_id != this->media->media_id)
214 return EFI_MEDIA_CHANGED;
215 if (!this->media->media_present)
216 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100217 /* media->io_align is a power of 2 or 0 */
218 if (this->media->io_align &&
219 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200220 return EFI_INVALID_PARAMETER;
221 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100222 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200223 return EFI_INVALID_PARAMETER;
224
Alexander Graf51735ae2016-05-11 18:25:48 +0200225#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
226 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
227 r = efi_disk_write_blocks(this, media_id, lba,
228 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
229 if (r != EFI_SUCCESS)
230 return r;
231 return efi_disk_write_blocks(this, media_id, lba +
232 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
233 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
234 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
235 }
236
237 real_buffer = efi_bounce_buffer;
238#endif
239
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900240 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200241 buffer_size, buffer);
242
243 /* Populate bounce buffer if necessary */
244 if (real_buffer != buffer)
245 memcpy(real_buffer, buffer, buffer_size);
246
247 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
248 EFI_DISK_WRITE);
249
250 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100251}
252
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200253/**
254 * efi_disk_flush_blocks() - flushes modified data to the device
255 *
256 * This function implements the FlushBlocks service of the
257 * EFI_BLOCK_IO_PROTOCOL.
258 *
259 * As we always write synchronously nothing is done here.
260 *
261 * See the Unified Extensible Firmware Interface (UEFI) specification for
262 * details.
263 *
264 * @this: pointer to the BLOCK_IO_PROTOCOL
265 * Return: status code
266 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100267static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
268{
Alexander Graf2a22d052016-03-04 01:10:02 +0100269 EFI_ENTRY("%p", this);
270 return EFI_EXIT(EFI_SUCCESS);
271}
272
273static const struct efi_block_io block_io_disk_template = {
274 .reset = &efi_disk_reset,
275 .read_blocks = &efi_disk_read_blocks,
276 .write_blocks = &efi_disk_write_blocks,
277 .flush_blocks = &efi_disk_flush_blocks,
278};
279
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100280/**
281 * efi_fs_from_path() - retrieve simple file system protocol
282 *
283 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100284 *
285 * The full path provided is split into device part and into a file
286 * part. The device part is used to find the handle on which the
287 * simple file system protocol is installed.
288 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100289 * @full_path: device path including device and file
290 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400291 */
292struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100293efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400294{
295 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100296 struct efi_handler *handler;
297 struct efi_device_path *device_path;
298 struct efi_device_path *file_path;
299 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400300
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100301 /* Split the path into a device part and a file part */
302 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
303 if (ret != EFI_SUCCESS)
304 return NULL;
305 efi_free_pool(file_path);
306
307 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100308 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100309 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400310 if (!efiobj)
311 return NULL;
312
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100313 /* Find the simple file system protocol */
314 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
315 &handler);
316 if (ret != EFI_SUCCESS)
317 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400318
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100319 /* Return the simple file system protocol for the partition */
320 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400321}
322
AKASHI Takahiro86740062019-10-07 14:59:38 +0900323/**
324 * efi_fs_exists() - check if a partition bears a file system
325 *
326 * @desc: block device descriptor
327 * @part: partition number
328 * Return: 1 if a file system exists on the partition
329 * 0 otherwise
330 */
331static int efi_fs_exists(struct blk_desc *desc, int part)
332{
333 if (fs_set_blk_dev_with_part(desc, part))
334 return 0;
335
336 if (fs_get_type() == FS_TYPE_ANY)
337 return 0;
338
339 fs_close();
340
341 return 1;
342}
343
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200344/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100345 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200346 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100347 * @parent: parent handle
348 * @dp_parent: parent device path
349 * @if_typename: interface name for block device
350 * @desc: internal block device
351 * @dev_index: device index for block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100352 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200353 * @part: partition
354 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100355 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200356 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100357static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100358 efi_handle_t parent,
359 struct efi_device_path *dp_parent,
360 const char *if_typename,
361 struct blk_desc *desc,
362 int dev_index,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100363 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100364 unsigned int part,
365 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200366{
367 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200368 struct efi_object *handle;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100369 const efi_guid_t *guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100370 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200371
Alexander Graf0812d1a2016-08-05 14:51:47 +0200372 /* Don't add empty devices */
373 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100374 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200375
Rob Clark884bcf62017-09-13 18:05:31 -0400376 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100377 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100378 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100379
380 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200381 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200382
383 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100384 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100385 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100386 struct efi_handler *handler;
387 void *protocol_interface;
388
389 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
390 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
391 if (ret != EFI_SUCCESS)
392 goto error;
393
394 /*
395 * Link the partition (child controller) to the block device
396 * (controller).
397 */
398 ret = efi_protocol_open(handler, &protocol_interface, NULL,
399 &diskobj->header,
400 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
401 if (ret != EFI_SUCCESS)
402 goto error;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100403
404 diskobj->dp = efi_dp_append_node(dp_parent, node);
405 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100406 diskobj->offset = part_info->start;
407 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100408 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
409 guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100410 } else {
411 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100412 diskobj->offset = 0;
413 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100414 }
Rob Clark884bcf62017-09-13 18:05:31 -0400415 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200416
417 /*
418 * Install the device path and the block IO protocol.
419 *
420 * InstallMultipleProtocolInterfaces() checks if the device path is
421 * already installed on an other handle and returns EFI_ALREADY_STARTED
422 * in this case.
423 */
424 handle = &diskobj->header;
425 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
426 &handle, &efi_guid_device_path, diskobj->dp,
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100427 &efi_block_io_guid, &diskobj->ops,
428 guid, NULL, NULL));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100429 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100430 goto error;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200431
432 /*
433 * On partitions or whole disks without partitions install the
434 * simple file system protocol if a file system is available.
435 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900436 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
437 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400438 diskobj->volume = efi_simple_file_system(desc, part,
439 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200440 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100441 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100442 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100443 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100444 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400445 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200446 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600447 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200448 diskobj->dev_index = dev_index;
Alexander Graff9d334b2016-08-05 14:49:53 +0200449 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200450
451 /* Fill in EFI IO Media info (for read/write callbacks) */
452 diskobj->media.removable_media = desc->removable;
453 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200454 /*
455 * MediaID is just an arbitrary counter.
456 * We have to change it if the medium is removed or changed.
457 */
458 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200459 diskobj->media.block_size = desc->blksz;
460 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100461 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100462 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200463 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100464 if (disk)
465 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100466
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100467 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
468 ", offset " LBAF ", last_block %llu\n",
469 diskobj->part,
470 diskobj->media.media_present,
471 diskobj->media.logical_partition,
472 diskobj->media.removable_media,
473 diskobj->offset,
474 diskobj->media.last_block);
475
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100476 /* Store first EFI system partition */
477 if (part && !efi_system_partition.if_type) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100478 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100479 efi_system_partition.if_type = desc->if_type;
480 efi_system_partition.devnum = desc->devnum;
481 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200482 EFI_PRINT("EFI system partition: %s %x:%x\n",
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100483 blk_get_if_type_name(desc->if_type),
484 desc->devnum, part);
485 }
486 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100487 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100488error:
489 efi_delete_handle(&diskobj->header);
490 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200491}
492
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900493/*
494 * Create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100495 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900496 * @dev uclass device (UCLASS_BLK)
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100497 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900498 * Create an efi_disk object which is associated with @dev.
499 * The type of @dev must be UCLASS_BLK.
500 *
501 * @return 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100502 */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900503static int efi_disk_create_raw(struct udevice *dev)
Alexander Graf2a22d052016-03-04 01:10:02 +0100504{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100505 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900506 struct blk_desc *desc;
507 const char *if_typename;
508 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100509 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600510
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900511 desc = dev_get_uclass_plat(dev);
512 if_typename = blk_get_if_type_name(desc->if_type);
513 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600514
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900515 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
516 diskid, NULL, 0, &disk);
517 if (ret != EFI_SUCCESS) {
518 if (ret == EFI_NOT_READY)
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200519 log_notice("Disk %s not ready\n", dev->name);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900520 else
521 log_err("Adding disk for %s failed\n", dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600522
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900523 return -1;
524 }
525 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
526 efi_free_pool(disk->dp);
527 efi_delete_handle(&disk->header);
528
529 return -1;
Simon Glass487d7562016-05-14 14:03:05 -0600530 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100531
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900532 return 0;
533}
534
535/*
536 * Create a handle for a disk partition
537 *
538 * @dev uclass device (UCLASS_PARTITION)
539 *
540 * Create an efi_disk object which is associated with @dev.
541 * The type of @dev must be UCLASS_PARTITION.
542 *
543 * @return 0 on success, -1 otherwise
544 */
545static int efi_disk_create_part(struct udevice *dev)
546{
547 efi_handle_t parent;
548 struct blk_desc *desc;
549 const char *if_typename;
550 struct disk_part *part_data;
551 struct disk_partition *info;
552 unsigned int part;
553 int diskid;
554 struct efi_handler *handler;
555 struct efi_device_path *dp_parent;
556 struct efi_disk_obj *disk;
557 efi_status_t ret;
558
559 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
560 return -1;
561
562 desc = dev_get_uclass_plat(dev_get_parent(dev));
563 if_typename = blk_get_if_type_name(desc->if_type);
564 diskid = desc->devnum;
565
566 part_data = dev_get_uclass_plat(dev);
567 part = part_data->partnum;
568 info = &part_data->gpt_part_info;
569
570 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
571 if (ret != EFI_SUCCESS)
572 return -1;
573 dp_parent = (struct efi_device_path *)handler->protocol_interface;
574
575 ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid,
576 info, part, &disk);
577 if (ret != EFI_SUCCESS) {
578 log_err("Adding partition for %s failed\n", dev->name);
579 return -1;
580 }
581 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
582 efi_free_pool(disk->dp);
583 efi_delete_handle(&disk->header);
584
585 return -1;
586 }
587
588 return 0;
589}
590
591/*
592 * Create efi_disk objects for a block device
593 *
594 * @dev uclass device (UCLASS_BLK)
595 *
596 * Create efi_disk objects for partitions as well as a raw disk
597 * which is associated with @dev.
598 * The type of @dev must be UCLASS_BLK.
599 * This function is expected to be called at EV_PM_POST_PROBE.
600 *
601 * @return 0 on success, -1 otherwise
602 */
603static int efi_disk_probe(void *ctx, struct event *event)
604{
605 struct udevice *dev;
606 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900607 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900608 struct udevice *child;
609 int ret;
610
611 dev = event->data.dm.dev;
612 id = device_get_uclass_id(dev);
613
614 /* TODO: We won't support partitions in a partition */
615 if (id != UCLASS_BLK)
616 return 0;
617
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900618 /*
619 * avoid creating duplicated objects now that efi_driver
620 * has already created an efi_disk at this moment.
621 */
622 desc = dev_get_uclass_plat(dev);
623 if (desc->if_type != IF_TYPE_EFI_LOADER) {
624 ret = efi_disk_create_raw(dev);
625 if (ret)
626 return -1;
627 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900628
629 device_foreach_child(child, dev) {
630 ret = efi_disk_create_part(child);
631 if (ret)
632 return -1;
633 }
634
635 return 0;
636}
637
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900638/*
639 * Delete an efi_disk object for a whole raw disk
640 *
641 * @dev uclass device (UCLASS_BLK)
642 *
643 * Delete an efi_disk object which is associated with @dev.
644 * The type of @dev must be UCLASS_BLK.
645 *
646 * @return 0 on success, -1 otherwise
647 */
648static int efi_disk_delete_raw(struct udevice *dev)
649{
650 efi_handle_t handle;
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900651 struct blk_desc *desc;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900652 struct efi_disk_obj *diskobj;
653
654 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
655 return -1;
656
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900657 desc = dev_get_uclass_plat(dev);
658 if (desc->if_type != IF_TYPE_EFI_LOADER) {
659 diskobj = container_of(handle, struct efi_disk_obj, header);
660 efi_free_pool(diskobj->dp);
661 }
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900662
663 efi_delete_handle(handle);
664 dev_tag_del(dev, DM_TAG_EFI);
665
666 return 0;
667}
668
669/*
670 * Delete an efi_disk object for a disk partition
671 *
672 * @dev uclass device (UCLASS_PARTITION)
673 *
674 * Delete an efi_disk object which is associated with @dev.
675 * The type of @dev must be UCLASS_PARTITION.
676 *
677 * @return 0 on success, -1 otherwise
678 */
679static int efi_disk_delete_part(struct udevice *dev)
680{
681 efi_handle_t handle;
682 struct efi_disk_obj *diskobj;
683
684 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
685 return -1;
686
687 diskobj = container_of(handle, struct efi_disk_obj, header);
688
689 efi_free_pool(diskobj->dp);
690 efi_delete_handle(handle);
691 dev_tag_del(dev, DM_TAG_EFI);
692
693 return 0;
694}
695
696/*
697 * Delete an efi_disk object for a block device
698 *
699 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
700 *
701 * Delete an efi_disk object which is associated with @dev.
702 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
703 * This function is expected to be called at EV_PM_PRE_REMOVE.
704 *
705 * @return 0 on success, -1 otherwise
706 */
707static int efi_disk_remove(void *ctx, struct event *event)
708{
709 enum uclass_id id;
710 struct udevice *dev;
711
712 dev = event->data.dm.dev;
713 id = device_get_uclass_id(dev);
714
715 if (id == UCLASS_BLK)
716 return efi_disk_delete_raw(dev);
717 else if (id == UCLASS_PARTITION)
718 return efi_disk_delete_part(dev);
719 else
720 return 0;
721}
722
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900723efi_status_t efi_disk_init(void)
724{
725 int ret;
726
727 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
728 efi_disk_probe, NULL);
729 if (ret) {
730 log_err("Event registration for efi_disk add failed\n");
731 return EFI_OUT_OF_RESOURCES;
732 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100733
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900734 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
735 efi_disk_remove, NULL);
736 if (ret) {
737 log_err("Event registration for efi_disk del failed\n");
738 return EFI_OUT_OF_RESOURCES;
739 }
740
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100741 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100742}