blob: 16d14b042975be90e817d4b26b0c3cfa2dab34d5 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafe83be452016-03-04 01:10:02 +01002/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafe83be452016-03-04 01:10:02 +01006 */
7
Heinrich Schuchardt37240572020-07-17 20:33:05 +02008#define LOG_CATEGORY LOGC_EFI
9
Alexander Grafe83be452016-03-04 01:10:02 +010010#include <common.h>
Simon Glasse3dfa912016-05-01 13:52:32 -060011#include <blk.h>
Simon Glass302d4f82016-05-14 14:03:05 -060012#include <dm.h>
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +090013#include <dm/device-internal.h>
14#include <dm/tag.h>
15#include <event.h>
Alexander Grafe83be452016-03-04 01:10:02 +010016#include <efi_loader.h>
AKASHI Takahiro120a01a2019-10-07 14:59:38 +090017#include <fs.h>
Heinrich Schuchardt37240572020-07-17 20:33:05 +020018#include <log.h>
Alexander Grafe83be452016-03-04 01:10:02 +010019#include <part.h>
20#include <malloc.h>
21
Heinrich Schuchardta9931732020-03-19 15:16:31 +010022struct efi_system_partition efi_system_partition;
23
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020024const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Heinrich Schuchardt26397622021-02-02 17:53:14 +010025const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Alexander Grafe83be452016-03-04 01:10:02 +010026
Heinrich Schuchardt72928722018-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
AKASHI Takahiro808ece12022-04-19 10:05:17 +090038 * @dev: associated DM device
Heinrich Schuchardt72928722018-09-26 05:27:56 +020039 */
Alexander Grafe83be452016-03-04 01:10:02 +010040struct efi_disk_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020041 struct efi_object header;
Alexander Grafe83be452016-03-04 01:10:02 +010042 struct efi_block_io ops;
Alexander Grafe83be452016-03-04 01:10:02 +010043 const char *ifname;
Alexander Grafe83be452016-03-04 01:10:02 +010044 int dev_index;
Alexander Grafe83be452016-03-04 01:10:02 +010045 struct efi_block_io_media media;
Rob Clarka83272f2017-09-13 18:05:31 -040046 struct efi_device_path *dp;
Rob Clarka83272f2017-09-13 18:05:31 -040047 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040048 struct efi_simple_file_system_protocol *volume;
Alexander Grafe83be452016-03-04 01:10:02 +010049};
50
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020051/**
52 * efi_disk_reset() - reset block device
53 *
54 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
55 *
56 * As U-Boot's block devices do not have a reset function simply return
57 * EFI_SUCCESS.
58 *
59 * See the Unified Extensible Firmware Interface (UEFI) specification for
60 * details.
61 *
62 * @this: pointer to the BLOCK_IO_PROTOCOL
63 * @extended_verification: extended verification
64 * Return: status code
65 */
Alexander Grafe83be452016-03-04 01:10:02 +010066static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
67 char extended_verification)
68{
69 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020070 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafe83be452016-03-04 01:10:02 +010071}
72
AKASHI Takahiroba647af2022-05-12 11:29:01 +090073/**
74 * efi_disk_is_removable() - check if the device is removable media
75 * @handle: efi object handle;
76 *
77 * Examine the device and determine if the device is a local block device
78 * and removable media.
79 *
80 * Return: true if removable, false otherwise
81 */
82bool efi_disk_is_removable(efi_handle_t handle)
83{
84 struct efi_handler *handler;
85 struct efi_block_io *io;
86 efi_status_t ret;
87
88 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
89 if (ret != EFI_SUCCESS)
90 return false;
91
92 io = handler->protocol_interface;
93
94 if (!io || !io->media)
95 return false;
96
97 return (bool)io->media->removable_media;
98}
99
Alexander Grafe83be452016-03-04 01:10:02 +0100100enum efi_disk_direction {
101 EFI_DISK_READ,
102 EFI_DISK_WRITE,
103};
104
Heinrich Schuchardt9ce06172017-08-26 22:33:13 +0200105static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +0100106 u32 media_id, u64 lba, unsigned long buffer_size,
107 void *buffer, enum efi_disk_direction direction)
108{
109 struct efi_disk_obj *diskobj;
Alexander Grafe83be452016-03-04 01:10:02 +0100110 int blksz;
111 int blocks;
112 unsigned long n;
113
Alexander Grafe83be452016-03-04 01:10:02 +0100114 diskobj = container_of(this, struct efi_disk_obj, ops);
AKASHI Takahiro808ece12022-04-19 10:05:17 +0900115 blksz = diskobj->media.block_size;
Alexander Grafe83be452016-03-04 01:10:02 +0100116 blocks = buffer_size / blksz;
117
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +0200118 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
119 blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +0100120
121 /* We only support full block access */
122 if (buffer_size & (blksz - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200123 return EFI_BAD_BUFFER_SIZE;
Alexander Grafe83be452016-03-04 01:10:02 +0100124
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900125 if (CONFIG_IS_ENABLED(PARTITIONS) &&
Masahisa Kojimac9611082022-07-22 11:39:10 +0900126 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900127 if (direction == EFI_DISK_READ)
Masahisa Kojimac9611082022-07-22 11:39:10 +0900128 n = dev_read(diskobj->header.dev, lba, blocks, buffer);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900129 else
Masahisa Kojimac9611082022-07-22 11:39:10 +0900130 n = dev_write(diskobj->header.dev, lba, blocks, buffer);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900131 } else {
132 /* dev is a block device (UCLASS_BLK) */
133 struct blk_desc *desc;
AKASHI Takahiro808ece12022-04-19 10:05:17 +0900134
Masahisa Kojimac9611082022-07-22 11:39:10 +0900135 desc = dev_get_uclass_plat(diskobj->header.dev);
AKASHI Takahiro8c591132022-04-28 13:49:16 +0900136 if (direction == EFI_DISK_READ)
137 n = blk_dread(desc, lba, blocks, buffer);
138 else
139 n = blk_dwrite(desc, lba, blocks, buffer);
140 }
Alexander Grafe83be452016-03-04 01:10:02 +0100141
142 /* We don't do interrupts, so check for timers cooperatively */
143 efi_timer_check();
144
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +0200145 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Graf62a67482016-06-02 11:38:27 +0200146
Alexander Grafe83be452016-03-04 01:10:02 +0100147 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -0400148 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +0100149
Rob Clark99c43872017-07-25 20:28:29 -0400150 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100151}
152
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200153/**
154 * efi_disk_read_blocks() - reads blocks from device
155 *
156 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
157 *
158 * See the Unified Extensible Firmware Interface (UEFI) specification for
159 * details.
160 *
161 * @this: pointer to the BLOCK_IO_PROTOCOL
162 * @media_id: id of the medium to be read from
163 * @lba: starting logical block for reading
164 * @buffer_size: size of the read buffer
165 * @buffer: pointer to the destination buffer
166 * Return: status code
167 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600168static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100169 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100170 void *buffer)
171{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200172 void *real_buffer = buffer;
173 efi_status_t r;
174
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200175 if (!this)
176 return EFI_INVALID_PARAMETER;
177 /* TODO: check for media changes */
178 if (media_id != this->media->media_id)
179 return EFI_MEDIA_CHANGED;
180 if (!this->media->media_present)
181 return EFI_NO_MEDIA;
Heinrich Schuchardt0bb73892021-01-23 19:33:11 +0100182 /* media->io_align is a power of 2 or 0 */
183 if (this->media->io_align &&
184 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200185 return EFI_INVALID_PARAMETER;
186 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsenf1424932021-02-09 17:17:17 +0100187 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200188 return EFI_INVALID_PARAMETER;
189
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200190#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
191 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
192 r = efi_disk_read_blocks(this, media_id, lba,
193 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
194 if (r != EFI_SUCCESS)
195 return r;
196 return efi_disk_read_blocks(this, media_id, lba +
197 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
198 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
199 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
200 }
201
202 real_buffer = efi_bounce_buffer;
203#endif
204
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900205 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200206 buffer_size, buffer);
207
208 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
209 EFI_DISK_READ);
210
211 /* Copy from bounce buffer to real buffer if necessary */
212 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
213 memcpy(buffer, real_buffer, buffer_size);
214
215 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100216}
217
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200218/**
219 * efi_disk_write_blocks() - writes blocks to device
220 *
221 * This function implements the WriteBlocks service of the
222 * EFI_BLOCK_IO_PROTOCOL.
223 *
224 * See the Unified Extensible Firmware Interface (UEFI) specification for
225 * details.
226 *
227 * @this: pointer to the BLOCK_IO_PROTOCOL
228 * @media_id: id of the medium to be written to
229 * @lba: starting logical block for writing
230 * @buffer_size: size of the write buffer
231 * @buffer: pointer to the source buffer
232 * Return: status code
233 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600234static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100235 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100236 void *buffer)
237{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200238 void *real_buffer = buffer;
239 efi_status_t r;
240
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200241 if (!this)
242 return EFI_INVALID_PARAMETER;
243 if (this->media->read_only)
244 return EFI_WRITE_PROTECTED;
245 /* TODO: check for media changes */
246 if (media_id != this->media->media_id)
247 return EFI_MEDIA_CHANGED;
248 if (!this->media->media_present)
249 return EFI_NO_MEDIA;
Heinrich Schuchardt0bb73892021-01-23 19:33:11 +0100250 /* media->io_align is a power of 2 or 0 */
251 if (this->media->io_align &&
252 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200253 return EFI_INVALID_PARAMETER;
254 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsenf1424932021-02-09 17:17:17 +0100255 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200256 return EFI_INVALID_PARAMETER;
257
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200258#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
259 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
260 r = efi_disk_write_blocks(this, media_id, lba,
261 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
262 if (r != EFI_SUCCESS)
263 return r;
264 return efi_disk_write_blocks(this, media_id, lba +
265 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
266 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
267 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
268 }
269
270 real_buffer = efi_bounce_buffer;
271#endif
272
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900273 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200274 buffer_size, buffer);
275
276 /* Populate bounce buffer if necessary */
277 if (real_buffer != buffer)
278 memcpy(real_buffer, buffer, buffer_size);
279
280 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
281 EFI_DISK_WRITE);
282
283 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100284}
285
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200286/**
287 * efi_disk_flush_blocks() - flushes modified data to the device
288 *
289 * This function implements the FlushBlocks service of the
290 * EFI_BLOCK_IO_PROTOCOL.
291 *
292 * As we always write synchronously nothing is done here.
293 *
294 * See the Unified Extensible Firmware Interface (UEFI) specification for
295 * details.
296 *
297 * @this: pointer to the BLOCK_IO_PROTOCOL
298 * Return: status code
299 */
Alexander Grafe83be452016-03-04 01:10:02 +0100300static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
301{
Alexander Grafe83be452016-03-04 01:10:02 +0100302 EFI_ENTRY("%p", this);
303 return EFI_EXIT(EFI_SUCCESS);
304}
305
306static const struct efi_block_io block_io_disk_template = {
307 .reset = &efi_disk_reset,
308 .read_blocks = &efi_disk_read_blocks,
309 .write_blocks = &efi_disk_write_blocks,
310 .flush_blocks = &efi_disk_flush_blocks,
311};
312
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100313/**
314 * efi_fs_from_path() - retrieve simple file system protocol
315 *
316 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100317 *
318 * The full path provided is split into device part and into a file
319 * part. The device part is used to find the handle on which the
320 * simple file system protocol is installed.
321 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100322 * @full_path: device path including device and file
323 * Return: simple file system protocol
Rob Clark53142032017-09-13 18:05:34 -0400324 */
325struct efi_simple_file_system_protocol *
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100326efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark53142032017-09-13 18:05:34 -0400327{
328 struct efi_object *efiobj;
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100329 struct efi_handler *handler;
330 struct efi_device_path *device_path;
331 struct efi_device_path *file_path;
332 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400333
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100334 /* Split the path into a device part and a file part */
335 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
336 if (ret != EFI_SUCCESS)
337 return NULL;
338 efi_free_pool(file_path);
339
340 /* Get the EFI object for the partition */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100341 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100342 efi_free_pool(device_path);
Rob Clark53142032017-09-13 18:05:34 -0400343 if (!efiobj)
344 return NULL;
345
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100346 /* Find the simple file system protocol */
347 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
348 &handler);
349 if (ret != EFI_SUCCESS)
350 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400351
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100352 /* Return the simple file system protocol for the partition */
353 return handler->protocol_interface;
Rob Clark53142032017-09-13 18:05:34 -0400354}
355
AKASHI Takahiro120a01a2019-10-07 14:59:38 +0900356/**
357 * efi_fs_exists() - check if a partition bears a file system
358 *
359 * @desc: block device descriptor
360 * @part: partition number
361 * Return: 1 if a file system exists on the partition
362 * 0 otherwise
363 */
364static int efi_fs_exists(struct blk_desc *desc, int part)
365{
366 if (fs_set_blk_dev_with_part(desc, part))
367 return 0;
368
369 if (fs_get_type() == FS_TYPE_ANY)
370 return 0;
371
372 fs_close();
373
374 return 1;
375}
376
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200377/**
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100378 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200379 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100380 * @parent: parent handle
381 * @dp_parent: parent device path
382 * @if_typename: interface name for block device
383 * @desc: internal block device
384 * @dev_index: device index for block device
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100385 * @part_info: partition info
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200386 * @part: partition
387 * @disk: pointer to receive the created handle
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100388 * Return: disk object
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200389 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100390static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100391 efi_handle_t parent,
392 struct efi_device_path *dp_parent,
393 const char *if_typename,
394 struct blk_desc *desc,
395 int dev_index,
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100396 struct disk_partition *part_info,
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100397 unsigned int part,
398 struct efi_disk_obj **disk)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200399{
400 struct efi_disk_obj *diskobj;
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200401 struct efi_object *handle;
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100402 const efi_guid_t *guid = NULL;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100403 efi_status_t ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200404
Alexander Graf601db1f2016-08-05 14:51:47 +0200405 /* Don't add empty devices */
406 if (!desc->lba)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100407 return EFI_NOT_READY;
Alexander Graf601db1f2016-08-05 14:51:47 +0200408
Rob Clarka83272f2017-09-13 18:05:31 -0400409 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100410 if (!diskobj)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100411 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100412
413 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200414 efi_add_handle(&diskobj->header);
Alexander Graf06fe57b2016-04-11 16:16:17 +0200415
416 /* Fill in object data */
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100417 if (part_info) {
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100418 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100419 struct efi_handler *handler;
420 void *protocol_interface;
421
422 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
423 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
424 if (ret != EFI_SUCCESS)
425 goto error;
426
427 /*
428 * Link the partition (child controller) to the block device
429 * (controller).
430 */
431 ret = efi_protocol_open(handler, &protocol_interface, NULL,
432 &diskobj->header,
433 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
434 if (ret != EFI_SUCCESS)
435 goto error;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100436
437 diskobj->dp = efi_dp_append_node(dp_parent, node);
438 efi_free_pool(node);
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100439 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100440 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
441 guid = &efi_system_partition_guid;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100442 } else {
443 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100444 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100445 }
Rob Clarka83272f2017-09-13 18:05:31 -0400446 diskobj->part = part;
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200447
448 /*
449 * Install the device path and the block IO protocol.
450 *
451 * InstallMultipleProtocolInterfaces() checks if the device path is
452 * already installed on an other handle and returns EFI_ALREADY_STARTED
453 * in this case.
454 */
455 handle = &diskobj->header;
456 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
457 &handle, &efi_guid_device_path, diskobj->dp,
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100458 &efi_block_io_guid, &diskobj->ops,
459 guid, NULL, NULL));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100460 if (ret != EFI_SUCCESS)
Heinrich Schuchardte47b68b2021-11-20 13:56:02 +0100461 goto error;
Heinrich Schuchardt2321b552020-05-21 09:22:06 +0200462
463 /*
464 * On partitions or whole disks without partitions install the
465 * simple file system protocol if a file system is available.
466 */
AKASHI Takahiro356ef902019-10-07 14:59:39 +0900467 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
468 efi_fs_exists(desc, part)) {
Rob Clark53142032017-09-13 18:05:34 -0400469 diskobj->volume = efi_simple_file_system(desc, part,
470 diskobj->dp);
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200471 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100472 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardtce721a82018-01-19 20:24:38 +0100473 diskobj->volume);
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100474 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100475 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400476 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200477 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600478 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200479 diskobj->dev_index = dev_index;
480
481 /* Fill in EFI IO Media info (for read/write callbacks) */
482 diskobj->media.removable_media = desc->removable;
483 diskobj->media.media_present = 1;
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200484 /*
485 * MediaID is just an arbitrary counter.
486 * We have to change it if the medium is removed or changed.
487 */
488 diskobj->media.media_id = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200489 diskobj->media.block_size = desc->blksz;
490 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardtaf8c5b62020-03-19 15:45:52 +0100491 if (part)
Emmanuel Vadot5bf29642017-12-11 19:22:33 +0100492 diskobj->media.logical_partition = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200493 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100494 if (disk)
495 *disk = diskobj;
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100496
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100497 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
Paul Barbieriab814f12022-06-30 07:02:04 -0400498 ", last_block %llu\n",
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100499 diskobj->part,
500 diskobj->media.media_present,
501 diskobj->media.logical_partition,
502 diskobj->media.removable_media,
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100503 diskobj->media.last_block);
504
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100505 /* Store first EFI system partition */
506 if (part && !efi_system_partition.if_type) {
Heinrich Schuchardt26397622021-02-02 17:53:14 +0100507 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100508 efi_system_partition.if_type = desc->if_type;
509 efi_system_partition.devnum = desc->devnum;
510 efi_system_partition.part = part;
Heinrich Schuchardt54f180d2021-05-25 18:00:13 +0200511 EFI_PRINT("EFI system partition: %s %x:%x\n",
Heinrich Schuchardta9931732020-03-19 15:16:31 +0100512 blk_get_if_type_name(desc->if_type),
513 desc->devnum, part);
514 }
515 }
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100516 return EFI_SUCCESS;
Heinrich Schuchardtcb9ff4e2020-01-10 12:36:01 +0100517error:
518 efi_delete_handle(&diskobj->header);
519 return ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200520}
521
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900522/*
523 * Create a handle for a whole raw disk
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100524 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900525 * @dev uclass device (UCLASS_BLK)
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100526 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900527 * Create an efi_disk object which is associated with @dev.
528 * The type of @dev must be UCLASS_BLK.
529 *
530 * @return 0 on success, -1 otherwise
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100531 */
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900532static int efi_disk_create_raw(struct udevice *dev)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200533{
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900534 struct efi_disk_obj *disk;
535 struct blk_desc *desc;
536 const char *if_typename;
537 int diskid;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100538 efi_status_t ret;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100539
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900540 desc = dev_get_uclass_plat(dev);
541 if_typename = blk_get_if_type_name(desc->if_type);
542 diskid = desc->devnum;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200543
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900544 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
545 diskid, NULL, 0, &disk);
546 if (ret != EFI_SUCCESS) {
547 if (ret == EFI_NOT_READY)
548 log_notice("Disk %s not ready\n", dev->name);
549 else
550 log_err("Adding disk for %s failed\n", dev->name);
Heinrich Schuchardtb56e7192021-01-23 06:52:21 +0100551
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900552 return -1;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200553 }
Masahisa Kojimac9611082022-07-22 11:39:10 +0900554 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900555 efi_free_pool(disk->dp);
556 efi_delete_handle(&disk->header);
Rob Clarka83272f2017-09-13 18:05:31 -0400557
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900558 return -1;
559 }
560
561 return 0;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200562}
563
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900564/*
565 * Create a handle for a disk partition
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100566 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900567 * @dev uclass device (UCLASS_PARTITION)
Alexander Grafe83be452016-03-04 01:10:02 +0100568 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900569 * Create an efi_disk object which is associated with @dev.
570 * The type of @dev must be UCLASS_PARTITION.
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100571 *
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900572 * @return 0 on success, -1 otherwise
Alexander Grafe83be452016-03-04 01:10:02 +0100573 */
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900574static int efi_disk_create_part(struct udevice *dev)
Alexander Grafe83be452016-03-04 01:10:02 +0100575{
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900576 efi_handle_t parent;
577 struct blk_desc *desc;
578 const char *if_typename;
579 struct disk_part *part_data;
580 struct disk_partition *info;
581 unsigned int part;
582 int diskid;
583 struct efi_handler *handler;
584 struct efi_device_path *dp_parent;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100585 struct efi_disk_obj *disk;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100586 efi_status_t ret;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900587
588 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
589 return -1;
590
591 desc = dev_get_uclass_plat(dev_get_parent(dev));
592 if_typename = blk_get_if_type_name(desc->if_type);
593 diskid = desc->devnum;
594
595 part_data = dev_get_uclass_plat(dev);
596 part = part_data->partnum;
597 info = &part_data->gpt_part_info;
598
599 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
600 if (ret != EFI_SUCCESS)
601 return -1;
602 dp_parent = (struct efi_device_path *)handler->protocol_interface;
603
604 ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid,
605 info, part, &disk);
606 if (ret != EFI_SUCCESS) {
607 log_err("Adding partition for %s failed\n", dev->name);
608 return -1;
609 }
Masahisa Kojimac9611082022-07-22 11:39:10 +0900610 if (efi_link_dev(&disk->header, dev)) {
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900611 efi_free_pool(disk->dp);
612 efi_delete_handle(&disk->header);
613
614 return -1;
615 }
616
617 return 0;
618}
619
620/*
621 * Create efi_disk objects for a block device
622 *
623 * @dev uclass device (UCLASS_BLK)
624 *
625 * Create efi_disk objects for partitions as well as a raw disk
626 * which is associated with @dev.
627 * The type of @dev must be UCLASS_BLK.
628 * This function is expected to be called at EV_PM_POST_PROBE.
629 *
630 * @return 0 on success, -1 otherwise
631 */
632static int efi_disk_probe(void *ctx, struct event *event)
633{
Simon Glass302d4f82016-05-14 14:03:05 -0600634 struct udevice *dev;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900635 enum uclass_id id;
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900636 struct blk_desc *desc;
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900637 struct udevice *child;
638 int ret;
Simon Glass302d4f82016-05-14 14:03:05 -0600639
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900640 dev = event->data.dm.dev;
641 id = device_get_uclass_id(dev);
Simon Glass302d4f82016-05-14 14:03:05 -0600642
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900643 /* TODO: We won't support partitions in a partition */
644 if (id != UCLASS_BLK)
645 return 0;
646
AKASHI Takahirocdd245a2022-04-19 10:05:13 +0900647 /*
648 * avoid creating duplicated objects now that efi_driver
649 * has already created an efi_disk at this moment.
650 */
651 desc = dev_get_uclass_plat(dev);
652 if (desc->if_type != IF_TYPE_EFI_LOADER) {
653 ret = efi_disk_create_raw(dev);
654 if (ret)
655 return -1;
656 }
Simon Glass302d4f82016-05-14 14:03:05 -0600657
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900658 device_foreach_child(child, dev) {
659 ret = efi_disk_create_part(child);
660 if (ret)
661 return -1;
Simon Glass302d4f82016-05-14 14:03:05 -0600662 }
Alexander Grafe83be452016-03-04 01:10:02 +0100663
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900664 return 0;
665}
666
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900667/*
668 * Delete an efi_disk object for a whole raw disk
669 *
670 * @dev uclass device (UCLASS_BLK)
671 *
672 * Delete an efi_disk object which is associated with @dev.
673 * The type of @dev must be UCLASS_BLK.
674 *
675 * @return 0 on success, -1 otherwise
676 */
677static int efi_disk_delete_raw(struct udevice *dev)
678{
679 efi_handle_t handle;
AKASHI Takahiroaff330a2022-04-19 10:05:15 +0900680 struct blk_desc *desc;
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900681 struct efi_disk_obj *diskobj;
682
683 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
684 return -1;
685
AKASHI Takahiroaff330a2022-04-19 10:05:15 +0900686 desc = dev_get_uclass_plat(dev);
687 if (desc->if_type != IF_TYPE_EFI_LOADER) {
688 diskobj = container_of(handle, struct efi_disk_obj, header);
689 efi_free_pool(diskobj->dp);
690 }
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900691
692 efi_delete_handle(handle);
693 dev_tag_del(dev, DM_TAG_EFI);
694
695 return 0;
696}
697
698/*
699 * Delete an efi_disk object for a disk partition
700 *
701 * @dev uclass device (UCLASS_PARTITION)
702 *
703 * Delete an efi_disk object which is associated with @dev.
704 * The type of @dev must be UCLASS_PARTITION.
705 *
706 * @return 0 on success, -1 otherwise
707 */
708static int efi_disk_delete_part(struct udevice *dev)
709{
710 efi_handle_t handle;
711 struct efi_disk_obj *diskobj;
712
713 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
714 return -1;
715
716 diskobj = container_of(handle, struct efi_disk_obj, header);
717
718 efi_free_pool(diskobj->dp);
719 efi_delete_handle(handle);
720 dev_tag_del(dev, DM_TAG_EFI);
721
722 return 0;
723}
724
725/*
726 * Delete an efi_disk object for a block device
727 *
728 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
729 *
730 * Delete an efi_disk object which is associated with @dev.
731 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
732 * This function is expected to be called at EV_PM_PRE_REMOVE.
733 *
734 * @return 0 on success, -1 otherwise
735 */
736static int efi_disk_remove(void *ctx, struct event *event)
737{
738 enum uclass_id id;
739 struct udevice *dev;
740
741 dev = event->data.dm.dev;
742 id = device_get_uclass_id(dev);
743
744 if (id == UCLASS_BLK)
745 return efi_disk_delete_raw(dev);
746 else if (id == UCLASS_PARTITION)
747 return efi_disk_delete_part(dev);
748 else
749 return 0;
750}
751
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900752efi_status_t efi_disk_init(void)
753{
754 int ret;
755
756 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
757 efi_disk_probe, NULL);
758 if (ret) {
759 log_err("Event registration for efi_disk add failed\n");
760 return EFI_OUT_OF_RESOURCES;
761 }
Alexander Grafe83be452016-03-04 01:10:02 +0100762
AKASHI Takahiro7cebb752022-04-19 10:05:14 +0900763 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
764 efi_disk_remove, NULL);
765 if (ret) {
766 log_err("Event registration for efi_disk del failed\n");
767 return EFI_OUT_OF_RESOURCES;
768 }
769
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100770 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100771}