blob: 670bf2b8ef0185961662d14db1a0a1fc3d51c0c7 [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
8#include <common.h>
Simon Glass6dd9faf2016-05-01 13:52:32 -06009#include <blk.h>
Simon Glass487d7562016-05-14 14:03:05 -060010#include <dm.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010011#include <efi_loader.h>
AKASHI Takahiro86740062019-10-07 14:59:38 +090012#include <fs.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010013#include <part.h>
14#include <malloc.h>
15
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +010016struct efi_system_partition efi_system_partition;
17
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020018const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010019
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020020/**
21 * struct efi_disk_obj - EFI disk object
22 *
23 * @header: EFI object header
24 * @ops: EFI disk I/O protocol interface
25 * @ifname: interface name for block device
26 * @dev_index: device index of block device
27 * @media: block I/O media information
28 * @dp: device path to the block device
29 * @part: partition
30 * @volume: simple file system protocol of the partition
31 * @offset: offset into disk for simple partition
32 * @desc: internal block device descriptor
33 */
Alexander Graf2a22d052016-03-04 01:10:02 +010034struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020035 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010036 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010037 const char *ifname;
Alexander Graf2a22d052016-03-04 01:10:02 +010038 int dev_index;
Alexander Graf2a22d052016-03-04 01:10:02 +010039 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040040 struct efi_device_path *dp;
Rob Clark884bcf62017-09-13 18:05:31 -040041 unsigned int part;
Rob Clark2a920802017-09-13 18:05:34 -040042 struct efi_simple_file_system_protocol *volume;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020043 lbaint_t offset;
Rob Clark884bcf62017-09-13 18:05:31 -040044 struct blk_desc *desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010045};
46
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020047/**
48 * efi_disk_reset() - reset block device
49 *
50 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
51 *
52 * As U-Boot's block devices do not have a reset function simply return
53 * EFI_SUCCESS.
54 *
55 * See the Unified Extensible Firmware Interface (UEFI) specification for
56 * details.
57 *
58 * @this: pointer to the BLOCK_IO_PROTOCOL
59 * @extended_verification: extended verification
60 * Return: status code
61 */
Alexander Graf2a22d052016-03-04 01:10:02 +010062static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
63 char extended_verification)
64{
65 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020066 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010067}
68
69enum efi_disk_direction {
70 EFI_DISK_READ,
71 EFI_DISK_WRITE,
72};
73
Heinrich Schuchardta80a2322017-08-26 22:33:13 +020074static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010075 u32 media_id, u64 lba, unsigned long buffer_size,
76 void *buffer, enum efi_disk_direction direction)
77{
78 struct efi_disk_obj *diskobj;
79 struct blk_desc *desc;
80 int blksz;
81 int blocks;
82 unsigned long n;
83
Alexander Graf2a22d052016-03-04 01:10:02 +010084 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020085 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010086 blksz = desc->blksz;
87 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020088 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010089
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +020090 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
91 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010092
93 /* We only support full block access */
94 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +020095 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +010096
97 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -060098 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010099 else
Simon Glass487d7562016-05-14 14:03:05 -0600100 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100101
102 /* We don't do interrupts, so check for timers cooperatively */
103 efi_timer_check();
104
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200105 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200106
Alexander Graf2a22d052016-03-04 01:10:02 +0100107 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400108 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100109
Rob Clark33049902017-07-25 20:28:29 -0400110 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100111}
112
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200113/**
114 * efi_disk_read_blocks() - reads blocks from device
115 *
116 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
117 *
118 * See the Unified Extensible Firmware Interface (UEFI) specification for
119 * details.
120 *
121 * @this: pointer to the BLOCK_IO_PROTOCOL
122 * @media_id: id of the medium to be read from
123 * @lba: starting logical block for reading
124 * @buffer_size: size of the read buffer
125 * @buffer: pointer to the destination buffer
126 * Return: status code
127 */
Simon Glasse2754582016-09-25 15:27:32 -0600128static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100129 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100130 void *buffer)
131{
Alexander Graf51735ae2016-05-11 18:25:48 +0200132 void *real_buffer = buffer;
133 efi_status_t r;
134
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200135 if (!this)
136 return EFI_INVALID_PARAMETER;
137 /* TODO: check for media changes */
138 if (media_id != this->media->media_id)
139 return EFI_MEDIA_CHANGED;
140 if (!this->media->media_present)
141 return EFI_NO_MEDIA;
142 /* media->io_align is a power of 2 */
143 if ((uintptr_t)buffer & (this->media->io_align - 1))
144 return EFI_INVALID_PARAMETER;
145 if (lba * this->media->block_size + buffer_size >
146 this->media->last_block * this->media->block_size)
147 return EFI_INVALID_PARAMETER;
148
Alexander Graf51735ae2016-05-11 18:25:48 +0200149#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
150 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
151 r = efi_disk_read_blocks(this, media_id, lba,
152 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
153 if (r != EFI_SUCCESS)
154 return r;
155 return efi_disk_read_blocks(this, media_id, lba +
156 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
157 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
158 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
159 }
160
161 real_buffer = efi_bounce_buffer;
162#endif
163
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900164 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200165 buffer_size, buffer);
166
167 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
168 EFI_DISK_READ);
169
170 /* Copy from bounce buffer to real buffer if necessary */
171 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
172 memcpy(buffer, real_buffer, buffer_size);
173
174 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100175}
176
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200177/**
178 * efi_disk_write_blocks() - writes blocks to device
179 *
180 * This function implements the WriteBlocks service of the
181 * EFI_BLOCK_IO_PROTOCOL.
182 *
183 * See the Unified Extensible Firmware Interface (UEFI) specification for
184 * details.
185 *
186 * @this: pointer to the BLOCK_IO_PROTOCOL
187 * @media_id: id of the medium to be written to
188 * @lba: starting logical block for writing
189 * @buffer_size: size of the write buffer
190 * @buffer: pointer to the source buffer
191 * Return: status code
192 */
Simon Glasse2754582016-09-25 15:27:32 -0600193static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100194 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100195 void *buffer)
196{
Alexander Graf51735ae2016-05-11 18:25:48 +0200197 void *real_buffer = buffer;
198 efi_status_t r;
199
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200200 if (!this)
201 return EFI_INVALID_PARAMETER;
202 if (this->media->read_only)
203 return EFI_WRITE_PROTECTED;
204 /* TODO: check for media changes */
205 if (media_id != this->media->media_id)
206 return EFI_MEDIA_CHANGED;
207 if (!this->media->media_present)
208 return EFI_NO_MEDIA;
209 /* media->io_align is a power of 2 */
210 if ((uintptr_t)buffer & (this->media->io_align - 1))
211 return EFI_INVALID_PARAMETER;
212 if (lba * this->media->block_size + buffer_size >
213 this->media->last_block * this->media->block_size)
214 return EFI_INVALID_PARAMETER;
215
Alexander Graf51735ae2016-05-11 18:25:48 +0200216#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
217 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
218 r = efi_disk_write_blocks(this, media_id, lba,
219 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
220 if (r != EFI_SUCCESS)
221 return r;
222 return efi_disk_write_blocks(this, media_id, lba +
223 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
224 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
225 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
226 }
227
228 real_buffer = efi_bounce_buffer;
229#endif
230
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900231 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200232 buffer_size, buffer);
233
234 /* Populate bounce buffer if necessary */
235 if (real_buffer != buffer)
236 memcpy(real_buffer, buffer, buffer_size);
237
238 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
239 EFI_DISK_WRITE);
240
241 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100242}
243
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200244/**
245 * efi_disk_flush_blocks() - flushes modified data to the device
246 *
247 * This function implements the FlushBlocks service of the
248 * EFI_BLOCK_IO_PROTOCOL.
249 *
250 * As we always write synchronously nothing is done here.
251 *
252 * See the Unified Extensible Firmware Interface (UEFI) specification for
253 * details.
254 *
255 * @this: pointer to the BLOCK_IO_PROTOCOL
256 * Return: status code
257 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100258static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
259{
Alexander Graf2a22d052016-03-04 01:10:02 +0100260 EFI_ENTRY("%p", this);
261 return EFI_EXIT(EFI_SUCCESS);
262}
263
264static const struct efi_block_io block_io_disk_template = {
265 .reset = &efi_disk_reset,
266 .read_blocks = &efi_disk_read_blocks,
267 .write_blocks = &efi_disk_write_blocks,
268 .flush_blocks = &efi_disk_flush_blocks,
269};
270
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100271/**
272 * efi_fs_from_path() - retrieve simple file system protocol
273 *
274 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100275 *
276 * The full path provided is split into device part and into a file
277 * part. The device part is used to find the handle on which the
278 * simple file system protocol is installed.
279 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100280 * @full_path: device path including device and file
281 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400282 */
283struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100284efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400285{
286 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100287 struct efi_handler *handler;
288 struct efi_device_path *device_path;
289 struct efi_device_path *file_path;
290 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400291
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100292 /* Split the path into a device part and a file part */
293 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
294 if (ret != EFI_SUCCESS)
295 return NULL;
296 efi_free_pool(file_path);
297
298 /* Get the EFI object for the partition */
299 efiobj = efi_dp_find_obj(device_path, NULL);
300 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400301 if (!efiobj)
302 return NULL;
303
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100304 /* Find the simple file system protocol */
305 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
306 &handler);
307 if (ret != EFI_SUCCESS)
308 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400309
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100310 /* Return the simple file system protocol for the partition */
311 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400312}
313
AKASHI Takahiro86740062019-10-07 14:59:38 +0900314/**
315 * efi_fs_exists() - check if a partition bears a file system
316 *
317 * @desc: block device descriptor
318 * @part: partition number
319 * Return: 1 if a file system exists on the partition
320 * 0 otherwise
321 */
322static int efi_fs_exists(struct blk_desc *desc, int part)
323{
324 if (fs_set_blk_dev_with_part(desc, part))
325 return 0;
326
327 if (fs_get_type() == FS_TYPE_ANY)
328 return 0;
329
330 fs_close();
331
332 return 1;
333}
334
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200335/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100336 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200337 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100338 * @parent: parent handle
339 * @dp_parent: parent device path
340 * @if_typename: interface name for block device
341 * @desc: internal block device
342 * @dev_index: device index for block device
343 * @offset: offset into disk for simple partitions
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200344 * @part: partition
345 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100346 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200347 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100348static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100349 efi_handle_t parent,
350 struct efi_device_path *dp_parent,
351 const char *if_typename,
352 struct blk_desc *desc,
353 int dev_index,
354 lbaint_t offset,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100355 unsigned int part,
356 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200357{
358 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200359 struct efi_object *handle;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100360 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200361
Alexander Graf0812d1a2016-08-05 14:51:47 +0200362 /* Don't add empty devices */
363 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100364 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200365
Rob Clark884bcf62017-09-13 18:05:31 -0400366 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100367 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100368 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100369
370 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200371 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200372
373 /* Fill in object data */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100374 if (part) {
375 struct efi_device_path *node = efi_dp_part_node(desc, part);
376
377 diskobj->dp = efi_dp_append_node(dp_parent, node);
378 efi_free_pool(node);
379 } else {
380 diskobj->dp = efi_dp_from_part(desc, part);
381 }
Rob Clark884bcf62017-09-13 18:05:31 -0400382 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200383
384 /*
385 * Install the device path and the block IO protocol.
386 *
387 * InstallMultipleProtocolInterfaces() checks if the device path is
388 * already installed on an other handle and returns EFI_ALREADY_STARTED
389 * in this case.
390 */
391 handle = &diskobj->header;
392 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
393 &handle, &efi_guid_device_path, diskobj->dp,
394 &efi_block_io_guid, &diskobj->ops, NULL));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100395 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100396 return ret;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200397
398 /*
399 * On partitions or whole disks without partitions install the
400 * simple file system protocol if a file system is available.
401 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900402 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
403 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400404 diskobj->volume = efi_simple_file_system(desc, part,
405 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200406 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100407 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100408 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100409 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100410 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400411 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200412 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600413 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200414 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200415 diskobj->offset = offset;
Alexander Graff9d334b2016-08-05 14:49:53 +0200416 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200417
418 /* Fill in EFI IO Media info (for read/write callbacks) */
419 diskobj->media.removable_media = desc->removable;
420 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200421 /*
422 * MediaID is just an arbitrary counter.
423 * We have to change it if the medium is removed or changed.
424 */
425 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200426 diskobj->media.block_size = desc->blksz;
427 diskobj->media.io_align = desc->blksz;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200428 diskobj->media.last_block = desc->lba - offset;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100429 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100430 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200431 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100432 if (disk)
433 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100434
435 /* Store first EFI system partition */
436 if (part && !efi_system_partition.if_type) {
437 int r;
Simon Glass05289792020-05-10 11:39:57 -0600438 struct disk_partition info;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100439
440 r = part_get_info(desc, part, &info);
441 if (r)
442 return EFI_DEVICE_ERROR;
443 if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
444 efi_system_partition.if_type = desc->if_type;
445 efi_system_partition.devnum = desc->devnum;
446 efi_system_partition.part = part;
447 EFI_PRINT("EFI system partition: %s %d:%d\n",
448 blk_get_if_type_name(desc->if_type),
449 desc->devnum, part);
450 }
451 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100452 return EFI_SUCCESS;
Alexander Graf4a12a972016-04-11 16:16:17 +0200453}
454
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100455/**
456 * efi_disk_create_partitions() - create handles and protocols for partitions
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100457 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100458 * Create handles and protocols for the partitions of a block device.
459 *
460 * @parent: handle of the parent disk
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200461 * @desc: block device
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100462 * @if_typename: interface type
463 * @diskid: device number
464 * @pdevname: device name
465 * Return: number of partitions created
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100466 */
467int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
468 const char *if_typename, int diskid,
469 const char *pdevname)
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200470{
471 int disks = 0;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200472 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Simon Glass05289792020-05-10 11:39:57 -0600473 struct disk_partition info;
Jonathan Gray16a73b22017-10-10 13:55:26 +1100474 int part;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100475 struct efi_device_path *dp = NULL;
476 efi_status_t ret;
477 struct efi_handler *handler;
478
479 /* Get the device path of the parent */
480 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
481 if (ret == EFI_SUCCESS)
482 dp = handler->protocol_interface;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200483
Alexander Grafc034b7f2017-12-01 16:10:33 +0100484 /* Add devices for each partition */
Jonathan Gray16a73b22017-10-10 13:55:26 +1100485 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
486 if (part_get_info(desc, part, &info))
487 continue;
Alexander Graff9d334b2016-08-05 14:49:53 +0200488 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
489 part);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100490 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
491 info.start, part, NULL);
492 if (ret != EFI_SUCCESS) {
493 printf("Adding partition %s failed\n", pdevname);
494 continue;
495 }
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200496 disks++;
497 }
Rob Clark884bcf62017-09-13 18:05:31 -0400498
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200499 return disks;
500}
501
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100502/**
503 * efi_disk_register() - register block devices
504 *
Alexander Graf2a22d052016-03-04 01:10:02 +0100505 * U-Boot doesn't have a list of all online disk devices. So when running our
506 * EFI payload, we scan through all of the potentially available ones and
507 * store them in our object pool.
508 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100509 * This function is called in efi_init_obj_list().
510 *
Simon Glass487d7562016-05-14 14:03:05 -0600511 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
512 * Consider converting the code to look up devices as needed. The EFI device
513 * could be a child of the UCLASS_BLK block device, perhaps.
514 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100515 * Return: status code
Alexander Graf2a22d052016-03-04 01:10:02 +0100516 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100517efi_status_t efi_disk_register(void)
Alexander Graf2a22d052016-03-04 01:10:02 +0100518{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100519 struct efi_disk_obj *disk;
Alexander Graf2a22d052016-03-04 01:10:02 +0100520 int disks = 0;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100521 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600522#ifdef CONFIG_BLK
523 struct udevice *dev;
524
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100525 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000526 uclass_next_device_check(&dev)) {
Simon Glass487d7562016-05-14 14:03:05 -0600527 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt9bfca9f2018-01-19 20:24:44 +0100528 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass487d7562016-05-14 14:03:05 -0600529
Alexander Grafc034b7f2017-12-01 16:10:33 +0100530 /* Add block device for the full device */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100531 printf("Scanning disk %s...\n", dev->name);
532 ret = efi_disk_add_dev(NULL, NULL, if_typename,
533 desc, desc->devnum, 0, 0, &disk);
534 if (ret == EFI_NOT_READY) {
535 printf("Disk %s not ready\n", dev->name);
536 continue;
537 }
538 if (ret) {
539 printf("ERROR: failure to add disk device %s, r = %lu\n",
540 dev->name, ret & ~EFI_ERROR_MASK);
541 return ret;
542 }
Simon Glass487d7562016-05-14 14:03:05 -0600543 disks++;
544
Alexander Grafc034b7f2017-12-01 16:10:33 +0100545 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100546 disks += efi_disk_create_partitions(
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200547 &disk->header, desc, if_typename,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100548 desc->devnum, dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600549 }
550#else
551 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100552
553 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600554 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass487d7562016-05-14 14:03:05 -0600555 const struct blk_driver *cur_drvr;
556 const char *if_typename;
557
Simon Glass6dd9faf2016-05-01 13:52:32 -0600558 cur_drvr = blk_driver_lookup_type(if_type);
559 if (!cur_drvr)
560 continue;
561
Simon Glass487d7562016-05-14 14:03:05 -0600562 if_typename = cur_drvr->if_typename;
563 printf("Scanning disks on %s...\n", if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100564 for (i = 0; i < 4; i++) {
565 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200566 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100567
Simon Glass6dd9faf2016-05-01 13:52:32 -0600568 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100569 if (!desc)
570 continue;
571 if (desc->type == DEV_TYPE_UNKNOWN)
572 continue;
573
Alexander Graf2a22d052016-03-04 01:10:02 +0100574 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass487d7562016-05-14 14:03:05 -0600575 if_typename, i);
Rob Clark77511b32017-10-08 11:33:08 -0400576
Alexander Grafc034b7f2017-12-01 16:10:33 +0100577 /* Add block device for the full device */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100578 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
579 i, 0, 0, &disk);
580 if (ret == EFI_NOT_READY) {
581 printf("Disk %s not ready\n", devname);
582 continue;
583 }
584 if (ret) {
585 printf("ERROR: failure to add disk device %s, r = %lu\n",
586 devname, ret & ~EFI_ERROR_MASK);
587 return ret;
588 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100589 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200590
Alexander Grafc034b7f2017-12-01 16:10:33 +0100591 /* Partitions show up as block devices in EFI */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200592 disks += efi_disk_create_partitions
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200593 (&disk->header, desc,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200594 if_typename, i, devname);
Alexander Graf2a22d052016-03-04 01:10:02 +0100595 }
596 }
Simon Glass487d7562016-05-14 14:03:05 -0600597#endif
Alexander Graf2a22d052016-03-04 01:10:02 +0100598 printf("Found %d disks\n", disks);
599
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100600 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100601}
AKASHI Takahiro41fd5062020-04-27 18:48:20 +0900602
603/**
604 * efi_disk_is_system_part() - check if handle refers to an EFI system partition
605 *
606 * @handle: handle of partition
607 *
608 * Return: true if handle refers to an EFI system partition
609 */
610bool efi_disk_is_system_part(efi_handle_t handle)
611{
612 struct efi_handler *handler;
613 struct efi_disk_obj *diskobj;
Simon Glass05289792020-05-10 11:39:57 -0600614 struct disk_partition info;
AKASHI Takahiro41fd5062020-04-27 18:48:20 +0900615 efi_status_t ret;
616 int r;
617
618 /* check if this is a block device */
619 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
620 if (ret != EFI_SUCCESS)
621 return false;
622
623 diskobj = container_of(handle, struct efi_disk_obj, header);
624
625 r = part_get_info(diskobj->desc, diskobj->part, &info);
626 if (r)
627 return false;
628
629 return !!(info.bootable & PART_EFI_SYSTEM_PARTITION);
630}