blob: f5b462fb164a7ef3d1e361940008589b54e1200d [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 Takahiro6c640422022-04-28 13:49:16 +0900102 if (CONFIG_IS_ENABLED(PARTITIONS) &&
103 device_get_uclass_id(diskobj->dev) == UCLASS_PARTITION) {
104 if (direction == EFI_DISK_READ)
105 n = dev_read(diskobj->dev, lba, blocks, buffer);
106 else
107 n = dev_write(diskobj->dev, lba, blocks, buffer);
108 } else {
109 /* dev is a block device (UCLASS_BLK) */
110 struct blk_desc *desc;
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900111
AKASHI Takahiro6c640422022-04-28 13:49:16 +0900112 desc = dev_get_uclass_plat(diskobj->dev);
113 if (direction == EFI_DISK_READ)
114 n = blk_dread(desc, lba, blocks, buffer);
115 else
116 n = blk_dwrite(desc, lba, blocks, buffer);
117 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100118
119 /* We don't do interrupts, so check for timers cooperatively */
120 efi_timer_check();
121
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200122 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200123
Alexander Graf2a22d052016-03-04 01:10:02 +0100124 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400125 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100126
Rob Clark33049902017-07-25 20:28:29 -0400127 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100128}
129
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200130/**
131 * efi_disk_read_blocks() - reads blocks from device
132 *
133 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
134 *
135 * See the Unified Extensible Firmware Interface (UEFI) specification for
136 * details.
137 *
138 * @this: pointer to the BLOCK_IO_PROTOCOL
139 * @media_id: id of the medium to be read from
140 * @lba: starting logical block for reading
141 * @buffer_size: size of the read buffer
142 * @buffer: pointer to the destination buffer
143 * Return: status code
144 */
Simon Glasse2754582016-09-25 15:27:32 -0600145static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100146 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100147 void *buffer)
148{
Alexander Graf51735ae2016-05-11 18:25:48 +0200149 void *real_buffer = buffer;
150 efi_status_t r;
151
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200152 if (!this)
153 return EFI_INVALID_PARAMETER;
154 /* TODO: check for media changes */
155 if (media_id != this->media->media_id)
156 return EFI_MEDIA_CHANGED;
157 if (!this->media->media_present)
158 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100159 /* media->io_align is a power of 2 or 0 */
160 if (this->media->io_align &&
161 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200162 return EFI_INVALID_PARAMETER;
163 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100164 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200165 return EFI_INVALID_PARAMETER;
166
Alexander Graf51735ae2016-05-11 18:25:48 +0200167#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
168 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
169 r = efi_disk_read_blocks(this, media_id, lba,
170 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
171 if (r != EFI_SUCCESS)
172 return r;
173 return efi_disk_read_blocks(this, media_id, lba +
174 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
175 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
176 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
177 }
178
179 real_buffer = efi_bounce_buffer;
180#endif
181
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900182 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200183 buffer_size, buffer);
184
185 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
186 EFI_DISK_READ);
187
188 /* Copy from bounce buffer to real buffer if necessary */
189 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
190 memcpy(buffer, real_buffer, buffer_size);
191
192 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100193}
194
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200195/**
196 * efi_disk_write_blocks() - writes blocks to device
197 *
198 * This function implements the WriteBlocks service of the
199 * EFI_BLOCK_IO_PROTOCOL.
200 *
201 * See the Unified Extensible Firmware Interface (UEFI) specification for
202 * details.
203 *
204 * @this: pointer to the BLOCK_IO_PROTOCOL
205 * @media_id: id of the medium to be written to
206 * @lba: starting logical block for writing
207 * @buffer_size: size of the write buffer
208 * @buffer: pointer to the source buffer
209 * Return: status code
210 */
Simon Glasse2754582016-09-25 15:27:32 -0600211static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100212 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100213 void *buffer)
214{
Alexander Graf51735ae2016-05-11 18:25:48 +0200215 void *real_buffer = buffer;
216 efi_status_t r;
217
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200218 if (!this)
219 return EFI_INVALID_PARAMETER;
220 if (this->media->read_only)
221 return EFI_WRITE_PROTECTED;
222 /* TODO: check for media changes */
223 if (media_id != this->media->media_id)
224 return EFI_MEDIA_CHANGED;
225 if (!this->media->media_present)
226 return EFI_NO_MEDIA;
Heinrich Schuchardt688e8822021-01-23 19:33:11 +0100227 /* media->io_align is a power of 2 or 0 */
228 if (this->media->io_align &&
229 (uintptr_t)buffer & (this->media->io_align - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200230 return EFI_INVALID_PARAMETER;
231 if (lba * this->media->block_size + buffer_size >
Jesper Schmitz Mouridsene67beff2021-02-09 17:17:17 +0100232 (this->media->last_block + 1) * this->media->block_size)
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200233 return EFI_INVALID_PARAMETER;
234
Alexander Graf51735ae2016-05-11 18:25:48 +0200235#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
236 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
237 r = efi_disk_write_blocks(this, media_id, lba,
238 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
239 if (r != EFI_SUCCESS)
240 return r;
241 return efi_disk_write_blocks(this, media_id, lba +
242 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
243 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
244 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
245 }
246
247 real_buffer = efi_bounce_buffer;
248#endif
249
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900250 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200251 buffer_size, buffer);
252
253 /* Populate bounce buffer if necessary */
254 if (real_buffer != buffer)
255 memcpy(real_buffer, buffer, buffer_size);
256
257 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
258 EFI_DISK_WRITE);
259
260 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100261}
262
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200263/**
264 * efi_disk_flush_blocks() - flushes modified data to the device
265 *
266 * This function implements the FlushBlocks service of the
267 * EFI_BLOCK_IO_PROTOCOL.
268 *
269 * As we always write synchronously nothing is done here.
270 *
271 * See the Unified Extensible Firmware Interface (UEFI) specification for
272 * details.
273 *
274 * @this: pointer to the BLOCK_IO_PROTOCOL
275 * Return: status code
276 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100277static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
278{
Alexander Graf2a22d052016-03-04 01:10:02 +0100279 EFI_ENTRY("%p", this);
280 return EFI_EXIT(EFI_SUCCESS);
281}
282
283static const struct efi_block_io block_io_disk_template = {
284 .reset = &efi_disk_reset,
285 .read_blocks = &efi_disk_read_blocks,
286 .write_blocks = &efi_disk_write_blocks,
287 .flush_blocks = &efi_disk_flush_blocks,
288};
289
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100290/**
291 * efi_fs_from_path() - retrieve simple file system protocol
292 *
293 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100294 *
295 * The full path provided is split into device part and into a file
296 * part. The device part is used to find the handle on which the
297 * simple file system protocol is installed.
298 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100299 * @full_path: device path including device and file
300 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400301 */
302struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100303efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400304{
305 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100306 struct efi_handler *handler;
307 struct efi_device_path *device_path;
308 struct efi_device_path *file_path;
309 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400310
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100311 /* Split the path into a device part and a file part */
312 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
313 if (ret != EFI_SUCCESS)
314 return NULL;
315 efi_free_pool(file_path);
316
317 /* Get the EFI object for the partition */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100318 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100319 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400320 if (!efiobj)
321 return NULL;
322
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100323 /* Find the simple file system protocol */
324 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
325 &handler);
326 if (ret != EFI_SUCCESS)
327 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400328
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100329 /* Return the simple file system protocol for the partition */
330 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400331}
332
AKASHI Takahiro86740062019-10-07 14:59:38 +0900333/**
334 * efi_fs_exists() - check if a partition bears a file system
335 *
336 * @desc: block device descriptor
337 * @part: partition number
338 * Return: 1 if a file system exists on the partition
339 * 0 otherwise
340 */
341static int efi_fs_exists(struct blk_desc *desc, int part)
342{
343 if (fs_set_blk_dev_with_part(desc, part))
344 return 0;
345
346 if (fs_get_type() == FS_TYPE_ANY)
347 return 0;
348
349 fs_close();
350
351 return 1;
352}
353
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200354/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100355 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200356 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100357 * @parent: parent handle
358 * @dp_parent: parent device path
359 * @if_typename: interface name for block device
360 * @desc: internal block device
361 * @dev_index: device index for block device
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100362 * @part_info: partition info
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200363 * @part: partition
364 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100365 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200366 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100367static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100368 efi_handle_t parent,
369 struct efi_device_path *dp_parent,
370 const char *if_typename,
371 struct blk_desc *desc,
372 int dev_index,
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100373 struct disk_partition *part_info,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100374 unsigned int part,
375 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200376{
377 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200378 struct efi_object *handle;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100379 const efi_guid_t *guid = NULL;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100380 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200381
Alexander Graf0812d1a2016-08-05 14:51:47 +0200382 /* Don't add empty devices */
383 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100384 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200385
Rob Clark884bcf62017-09-13 18:05:31 -0400386 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100387 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100388 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100389
390 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200391 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200392
393 /* Fill in object data */
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100394 if (part_info) {
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100395 struct efi_device_path *node = efi_dp_part_node(desc, part);
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100396 struct efi_handler *handler;
397 void *protocol_interface;
398
399 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
400 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
401 if (ret != EFI_SUCCESS)
402 goto error;
403
404 /*
405 * Link the partition (child controller) to the block device
406 * (controller).
407 */
408 ret = efi_protocol_open(handler, &protocol_interface, NULL,
409 &diskobj->header,
410 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
411 if (ret != EFI_SUCCESS)
412 goto error;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100413
414 diskobj->dp = efi_dp_append_node(dp_parent, node);
415 efi_free_pool(node);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100416 diskobj->offset = part_info->start;
417 diskobj->media.last_block = part_info->size - 1;
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100418 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
419 guid = &efi_system_partition_guid;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100420 } else {
421 diskobj->dp = efi_dp_from_part(desc, part);
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100422 diskobj->offset = 0;
423 diskobj->media.last_block = desc->lba - 1;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100424 }
Rob Clark884bcf62017-09-13 18:05:31 -0400425 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200426
427 /*
428 * Install the device path and the block IO protocol.
429 *
430 * InstallMultipleProtocolInterfaces() checks if the device path is
431 * already installed on an other handle and returns EFI_ALREADY_STARTED
432 * in this case.
433 */
434 handle = &diskobj->header;
435 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
436 &handle, &efi_guid_device_path, diskobj->dp,
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100437 &efi_block_io_guid, &diskobj->ops,
438 guid, NULL, NULL));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100439 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcd9a26b2021-11-20 13:56:02 +0100440 goto error;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200441
442 /*
443 * On partitions or whole disks without partitions install the
444 * simple file system protocol if a file system is available.
445 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900446 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
447 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400448 diskobj->volume = efi_simple_file_system(desc, part,
449 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200450 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100451 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100452 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100453 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100454 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400455 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200456 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600457 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200458 diskobj->dev_index = dev_index;
459
460 /* Fill in EFI IO Media info (for read/write callbacks) */
461 diskobj->media.removable_media = desc->removable;
462 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200463 /*
464 * MediaID is just an arbitrary counter.
465 * We have to change it if the medium is removed or changed.
466 */
467 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200468 diskobj->media.block_size = desc->blksz;
469 diskobj->media.io_align = desc->blksz;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100470 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100471 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200472 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100473 if (disk)
474 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100475
Heinrich Schuchardt8d0949b2021-01-23 06:52:21 +0100476 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
477 ", offset " LBAF ", last_block %llu\n",
478 diskobj->part,
479 diskobj->media.media_present,
480 diskobj->media.logical_partition,
481 diskobj->media.removable_media,
482 diskobj->offset,
483 diskobj->media.last_block);
484
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100485 /* Store first EFI system partition */
486 if (part && !efi_system_partition.if_type) {
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100487 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100488 efi_system_partition.if_type = desc->if_type;
489 efi_system_partition.devnum = desc->devnum;
490 efi_system_partition.part = part;
Heinrich Schuchardt3dca77b2021-05-25 18:00:13 +0200491 EFI_PRINT("EFI system partition: %s %x:%x\n",
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100492 blk_get_if_type_name(desc->if_type),
493 desc->devnum, part);
494 }
495 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100496 return EFI_SUCCESS;
Heinrich Schuchardt26448512020-01-10 12:36:01 +0100497error:
498 efi_delete_handle(&diskobj->header);
499 return ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200500}
501
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900502/*
503 * Create a handle for a whole raw disk
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100504 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900505 * @dev uclass device (UCLASS_BLK)
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100506 *
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900507 * Create an efi_disk object which is associated with @dev.
508 * The type of @dev must be UCLASS_BLK.
509 *
510 * @return 0 on success, -1 otherwise
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100511 */
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900512static int efi_disk_create_raw(struct udevice *dev)
Alexander Graf2a22d052016-03-04 01:10:02 +0100513{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100514 struct efi_disk_obj *disk;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900515 struct blk_desc *desc;
516 const char *if_typename;
517 int diskid;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100518 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600519
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900520 desc = dev_get_uclass_plat(dev);
521 if_typename = blk_get_if_type_name(desc->if_type);
522 diskid = desc->devnum;
Simon Glass487d7562016-05-14 14:03:05 -0600523
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900524 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
525 diskid, NULL, 0, &disk);
526 if (ret != EFI_SUCCESS) {
527 if (ret == EFI_NOT_READY)
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200528 log_notice("Disk %s not ready\n", dev->name);
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900529 else
530 log_err("Adding disk for %s failed\n", dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600531
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900532 return -1;
533 }
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900534 disk->dev = dev;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900535 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
536 efi_free_pool(disk->dp);
537 efi_delete_handle(&disk->header);
538
539 return -1;
Simon Glass487d7562016-05-14 14:03:05 -0600540 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100541
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900542 return 0;
543}
544
545/*
546 * Create a handle for a disk partition
547 *
548 * @dev uclass device (UCLASS_PARTITION)
549 *
550 * Create an efi_disk object which is associated with @dev.
551 * The type of @dev must be UCLASS_PARTITION.
552 *
553 * @return 0 on success, -1 otherwise
554 */
555static int efi_disk_create_part(struct udevice *dev)
556{
557 efi_handle_t parent;
558 struct blk_desc *desc;
559 const char *if_typename;
560 struct disk_part *part_data;
561 struct disk_partition *info;
562 unsigned int part;
563 int diskid;
564 struct efi_handler *handler;
565 struct efi_device_path *dp_parent;
566 struct efi_disk_obj *disk;
567 efi_status_t ret;
568
569 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
570 return -1;
571
572 desc = dev_get_uclass_plat(dev_get_parent(dev));
573 if_typename = blk_get_if_type_name(desc->if_type);
574 diskid = desc->devnum;
575
576 part_data = dev_get_uclass_plat(dev);
577 part = part_data->partnum;
578 info = &part_data->gpt_part_info;
579
580 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
581 if (ret != EFI_SUCCESS)
582 return -1;
583 dp_parent = (struct efi_device_path *)handler->protocol_interface;
584
585 ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid,
586 info, part, &disk);
587 if (ret != EFI_SUCCESS) {
588 log_err("Adding partition for %s failed\n", dev->name);
589 return -1;
590 }
AKASHI Takahirod97e98c2022-04-19 10:05:17 +0900591 disk->dev = dev;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900592 if (dev_tag_set_ptr(dev, DM_TAG_EFI, &disk->header)) {
593 efi_free_pool(disk->dp);
594 efi_delete_handle(&disk->header);
595
596 return -1;
597 }
598
599 return 0;
600}
601
602/*
603 * Create efi_disk objects for a block device
604 *
605 * @dev uclass device (UCLASS_BLK)
606 *
607 * Create efi_disk objects for partitions as well as a raw disk
608 * which is associated with @dev.
609 * The type of @dev must be UCLASS_BLK.
610 * This function is expected to be called at EV_PM_POST_PROBE.
611 *
612 * @return 0 on success, -1 otherwise
613 */
614static int efi_disk_probe(void *ctx, struct event *event)
615{
616 struct udevice *dev;
617 enum uclass_id id;
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900618 struct blk_desc *desc;
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900619 struct udevice *child;
620 int ret;
621
622 dev = event->data.dm.dev;
623 id = device_get_uclass_id(dev);
624
625 /* TODO: We won't support partitions in a partition */
626 if (id != UCLASS_BLK)
627 return 0;
628
AKASHI Takahiro3c809df2022-04-19 10:05:13 +0900629 /*
630 * avoid creating duplicated objects now that efi_driver
631 * has already created an efi_disk at this moment.
632 */
633 desc = dev_get_uclass_plat(dev);
634 if (desc->if_type != IF_TYPE_EFI_LOADER) {
635 ret = efi_disk_create_raw(dev);
636 if (ret)
637 return -1;
638 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900639
640 device_foreach_child(child, dev) {
641 ret = efi_disk_create_part(child);
642 if (ret)
643 return -1;
644 }
645
646 return 0;
647}
648
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900649/*
650 * Delete an efi_disk object for a whole raw disk
651 *
652 * @dev uclass device (UCLASS_BLK)
653 *
654 * Delete an efi_disk object which is associated with @dev.
655 * The type of @dev must be UCLASS_BLK.
656 *
657 * @return 0 on success, -1 otherwise
658 */
659static int efi_disk_delete_raw(struct udevice *dev)
660{
661 efi_handle_t handle;
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900662 struct blk_desc *desc;
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900663 struct efi_disk_obj *diskobj;
664
665 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
666 return -1;
667
AKASHI Takahiroa3cb34e2022-04-19 10:05:15 +0900668 desc = dev_get_uclass_plat(dev);
669 if (desc->if_type != IF_TYPE_EFI_LOADER) {
670 diskobj = container_of(handle, struct efi_disk_obj, header);
671 efi_free_pool(diskobj->dp);
672 }
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900673
674 efi_delete_handle(handle);
675 dev_tag_del(dev, DM_TAG_EFI);
676
677 return 0;
678}
679
680/*
681 * Delete an efi_disk object for a disk partition
682 *
683 * @dev uclass device (UCLASS_PARTITION)
684 *
685 * Delete an efi_disk object which is associated with @dev.
686 * The type of @dev must be UCLASS_PARTITION.
687 *
688 * @return 0 on success, -1 otherwise
689 */
690static int efi_disk_delete_part(struct udevice *dev)
691{
692 efi_handle_t handle;
693 struct efi_disk_obj *diskobj;
694
695 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
696 return -1;
697
698 diskobj = container_of(handle, struct efi_disk_obj, header);
699
700 efi_free_pool(diskobj->dp);
701 efi_delete_handle(handle);
702 dev_tag_del(dev, DM_TAG_EFI);
703
704 return 0;
705}
706
707/*
708 * Delete an efi_disk object for a block device
709 *
710 * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION)
711 *
712 * Delete an efi_disk object which is associated with @dev.
713 * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION.
714 * This function is expected to be called at EV_PM_PRE_REMOVE.
715 *
716 * @return 0 on success, -1 otherwise
717 */
718static int efi_disk_remove(void *ctx, struct event *event)
719{
720 enum uclass_id id;
721 struct udevice *dev;
722
723 dev = event->data.dm.dev;
724 id = device_get_uclass_id(dev);
725
726 if (id == UCLASS_BLK)
727 return efi_disk_delete_raw(dev);
728 else if (id == UCLASS_PARTITION)
729 return efi_disk_delete_part(dev);
730 else
731 return 0;
732}
733
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900734efi_status_t efi_disk_init(void)
735{
736 int ret;
737
738 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
739 efi_disk_probe, NULL);
740 if (ret) {
741 log_err("Event registration for efi_disk add failed\n");
742 return EFI_OUT_OF_RESOURCES;
743 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100744
AKASHI Takahirob406eb02022-04-19 10:05:14 +0900745 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
746 efi_disk_remove, NULL);
747 if (ret) {
748 log_err("Event registration for efi_disk del failed\n");
749 return EFI_OUT_OF_RESOURCES;
750 }
751
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100752 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100753}