blob: 7bd1ccec4501a8e27472b493ea1c8c420320254a [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>
Alexander Graf2a22d052016-03-04 01:10:02 +010013#include <efi_loader.h>
AKASHI Takahiro86740062019-10-07 14:59:38 +090014#include <fs.h>
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +020015#include <log.h>
Alexander Graf2a22d052016-03-04 01:10:02 +010016#include <part.h>
17#include <malloc.h>
18
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +010019struct efi_system_partition efi_system_partition;
20
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020021const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010022
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020023/**
24 * struct efi_disk_obj - EFI disk object
25 *
26 * @header: EFI object header
27 * @ops: EFI disk I/O protocol interface
28 * @ifname: interface name for block device
29 * @dev_index: device index of block device
30 * @media: block I/O media information
31 * @dp: device path to the block device
32 * @part: partition
33 * @volume: simple file system protocol of the partition
34 * @offset: offset into disk for simple partition
35 * @desc: internal block device descriptor
36 */
Alexander Graf2a22d052016-03-04 01:10:02 +010037struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020038 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010039 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010040 const char *ifname;
Alexander Graf2a22d052016-03-04 01:10:02 +010041 int dev_index;
Alexander Graf2a22d052016-03-04 01:10:02 +010042 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040043 struct efi_device_path *dp;
Rob Clark884bcf62017-09-13 18:05:31 -040044 unsigned int part;
Rob Clark2a920802017-09-13 18:05:34 -040045 struct efi_simple_file_system_protocol *volume;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020046 lbaint_t offset;
Rob Clark884bcf62017-09-13 18:05:31 -040047 struct blk_desc *desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010048};
49
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020050/**
51 * efi_disk_reset() - reset block device
52 *
53 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
54 *
55 * As U-Boot's block devices do not have a reset function simply return
56 * EFI_SUCCESS.
57 *
58 * See the Unified Extensible Firmware Interface (UEFI) specification for
59 * details.
60 *
61 * @this: pointer to the BLOCK_IO_PROTOCOL
62 * @extended_verification: extended verification
63 * Return: status code
64 */
Alexander Graf2a22d052016-03-04 01:10:02 +010065static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
66 char extended_verification)
67{
68 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardtcda9b352019-09-05 20:13:46 +020069 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf2a22d052016-03-04 01:10:02 +010070}
71
72enum efi_disk_direction {
73 EFI_DISK_READ,
74 EFI_DISK_WRITE,
75};
76
Heinrich Schuchardta80a2322017-08-26 22:33:13 +020077static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010078 u32 media_id, u64 lba, unsigned long buffer_size,
79 void *buffer, enum efi_disk_direction direction)
80{
81 struct efi_disk_obj *diskobj;
82 struct blk_desc *desc;
83 int blksz;
84 int blocks;
85 unsigned long n;
86
Alexander Graf2a22d052016-03-04 01:10:02 +010087 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020088 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010089 blksz = desc->blksz;
90 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020091 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010092
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +020093 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
94 blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010095
96 /* We only support full block access */
97 if (buffer_size & (blksz - 1))
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +020098 return EFI_BAD_BUFFER_SIZE;
Alexander Graf2a22d052016-03-04 01:10:02 +010099
100 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -0600101 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100102 else
Simon Glass487d7562016-05-14 14:03:05 -0600103 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +0100104
105 /* We don't do interrupts, so check for timers cooperatively */
106 efi_timer_check();
107
Heinrich Schuchardt9d3f3392019-09-05 19:43:17 +0200108 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Grafedcef3b2016-06-02 11:38:27 +0200109
Alexander Graf2a22d052016-03-04 01:10:02 +0100110 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -0400111 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +0100112
Rob Clark33049902017-07-25 20:28:29 -0400113 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100114}
115
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200116/**
117 * efi_disk_read_blocks() - reads blocks from device
118 *
119 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
120 *
121 * See the Unified Extensible Firmware Interface (UEFI) specification for
122 * details.
123 *
124 * @this: pointer to the BLOCK_IO_PROTOCOL
125 * @media_id: id of the medium to be read from
126 * @lba: starting logical block for reading
127 * @buffer_size: size of the read buffer
128 * @buffer: pointer to the destination buffer
129 * Return: status code
130 */
Simon Glasse2754582016-09-25 15:27:32 -0600131static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100132 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100133 void *buffer)
134{
Alexander Graf51735ae2016-05-11 18:25:48 +0200135 void *real_buffer = buffer;
136 efi_status_t r;
137
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200138 if (!this)
139 return EFI_INVALID_PARAMETER;
140 /* TODO: check for media changes */
141 if (media_id != this->media->media_id)
142 return EFI_MEDIA_CHANGED;
143 if (!this->media->media_present)
144 return EFI_NO_MEDIA;
145 /* media->io_align is a power of 2 */
146 if ((uintptr_t)buffer & (this->media->io_align - 1))
147 return EFI_INVALID_PARAMETER;
148 if (lba * this->media->block_size + buffer_size >
149 this->media->last_block * this->media->block_size)
150 return EFI_INVALID_PARAMETER;
151
Alexander Graf51735ae2016-05-11 18:25:48 +0200152#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
153 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
154 r = efi_disk_read_blocks(this, media_id, lba,
155 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
156 if (r != EFI_SUCCESS)
157 return r;
158 return efi_disk_read_blocks(this, media_id, lba +
159 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
160 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
161 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
162 }
163
164 real_buffer = efi_bounce_buffer;
165#endif
166
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900167 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200168 buffer_size, buffer);
169
170 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
171 EFI_DISK_READ);
172
173 /* Copy from bounce buffer to real buffer if necessary */
174 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
175 memcpy(buffer, real_buffer, buffer_size);
176
177 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100178}
179
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200180/**
181 * efi_disk_write_blocks() - writes blocks to device
182 *
183 * This function implements the WriteBlocks service of the
184 * EFI_BLOCK_IO_PROTOCOL.
185 *
186 * See the Unified Extensible Firmware Interface (UEFI) specification for
187 * details.
188 *
189 * @this: pointer to the BLOCK_IO_PROTOCOL
190 * @media_id: id of the medium to be written to
191 * @lba: starting logical block for writing
192 * @buffer_size: size of the write buffer
193 * @buffer: pointer to the source buffer
194 * Return: status code
195 */
Simon Glasse2754582016-09-25 15:27:32 -0600196static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100197 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100198 void *buffer)
199{
Alexander Graf51735ae2016-05-11 18:25:48 +0200200 void *real_buffer = buffer;
201 efi_status_t r;
202
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200203 if (!this)
204 return EFI_INVALID_PARAMETER;
205 if (this->media->read_only)
206 return EFI_WRITE_PROTECTED;
207 /* TODO: check for media changes */
208 if (media_id != this->media->media_id)
209 return EFI_MEDIA_CHANGED;
210 if (!this->media->media_present)
211 return EFI_NO_MEDIA;
212 /* media->io_align is a power of 2 */
213 if ((uintptr_t)buffer & (this->media->io_align - 1))
214 return EFI_INVALID_PARAMETER;
215 if (lba * this->media->block_size + buffer_size >
216 this->media->last_block * this->media->block_size)
217 return EFI_INVALID_PARAMETER;
218
Alexander Graf51735ae2016-05-11 18:25:48 +0200219#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
220 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
221 r = efi_disk_write_blocks(this, media_id, lba,
222 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
223 if (r != EFI_SUCCESS)
224 return r;
225 return efi_disk_write_blocks(this, media_id, lba +
226 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
227 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
228 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
229 }
230
231 real_buffer = efi_bounce_buffer;
232#endif
233
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900234 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200235 buffer_size, buffer);
236
237 /* Populate bounce buffer if necessary */
238 if (real_buffer != buffer)
239 memcpy(real_buffer, buffer, buffer_size);
240
241 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
242 EFI_DISK_WRITE);
243
244 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100245}
246
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200247/**
248 * efi_disk_flush_blocks() - flushes modified data to the device
249 *
250 * This function implements the FlushBlocks service of the
251 * EFI_BLOCK_IO_PROTOCOL.
252 *
253 * As we always write synchronously nothing is done here.
254 *
255 * See the Unified Extensible Firmware Interface (UEFI) specification for
256 * details.
257 *
258 * @this: pointer to the BLOCK_IO_PROTOCOL
259 * Return: status code
260 */
Alexander Graf2a22d052016-03-04 01:10:02 +0100261static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
262{
Alexander Graf2a22d052016-03-04 01:10:02 +0100263 EFI_ENTRY("%p", this);
264 return EFI_EXIT(EFI_SUCCESS);
265}
266
267static const struct efi_block_io block_io_disk_template = {
268 .reset = &efi_disk_reset,
269 .read_blocks = &efi_disk_read_blocks,
270 .write_blocks = &efi_disk_write_blocks,
271 .flush_blocks = &efi_disk_flush_blocks,
272};
273
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100274/**
275 * efi_fs_from_path() - retrieve simple file system protocol
276 *
277 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100278 *
279 * The full path provided is split into device part and into a file
280 * part. The device part is used to find the handle on which the
281 * simple file system protocol is installed.
282 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100283 * @full_path: device path including device and file
284 * Return: simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400285 */
286struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100287efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400288{
289 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100290 struct efi_handler *handler;
291 struct efi_device_path *device_path;
292 struct efi_device_path *file_path;
293 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400294
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100295 /* Split the path into a device part and a file part */
296 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
297 if (ret != EFI_SUCCESS)
298 return NULL;
299 efi_free_pool(file_path);
300
301 /* Get the EFI object for the partition */
302 efiobj = efi_dp_find_obj(device_path, NULL);
303 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400304 if (!efiobj)
305 return NULL;
306
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100307 /* Find the simple file system protocol */
308 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
309 &handler);
310 if (ret != EFI_SUCCESS)
311 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400312
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100313 /* Return the simple file system protocol for the partition */
314 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400315}
316
AKASHI Takahiro86740062019-10-07 14:59:38 +0900317/**
318 * efi_fs_exists() - check if a partition bears a file system
319 *
320 * @desc: block device descriptor
321 * @part: partition number
322 * Return: 1 if a file system exists on the partition
323 * 0 otherwise
324 */
325static int efi_fs_exists(struct blk_desc *desc, int part)
326{
327 if (fs_set_blk_dev_with_part(desc, part))
328 return 0;
329
330 if (fs_get_type() == FS_TYPE_ANY)
331 return 0;
332
333 fs_close();
334
335 return 1;
336}
337
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200338/**
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100339 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200340 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100341 * @parent: parent handle
342 * @dp_parent: parent device path
343 * @if_typename: interface name for block device
344 * @desc: internal block device
345 * @dev_index: device index for block device
346 * @offset: offset into disk for simple partitions
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200347 * @part: partition
348 * @disk: pointer to receive the created handle
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100349 * Return: disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200350 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100351static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100352 efi_handle_t parent,
353 struct efi_device_path *dp_parent,
354 const char *if_typename,
355 struct blk_desc *desc,
356 int dev_index,
357 lbaint_t offset,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100358 unsigned int part,
359 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200360{
361 struct efi_disk_obj *diskobj;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200362 struct efi_object *handle;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100363 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200364
Alexander Graf0812d1a2016-08-05 14:51:47 +0200365 /* Don't add empty devices */
366 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100367 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200368
Rob Clark884bcf62017-09-13 18:05:31 -0400369 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100370 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100371 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100372
373 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200374 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200375
376 /* Fill in object data */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100377 if (part) {
378 struct efi_device_path *node = efi_dp_part_node(desc, part);
379
380 diskobj->dp = efi_dp_append_node(dp_parent, node);
381 efi_free_pool(node);
382 } else {
383 diskobj->dp = efi_dp_from_part(desc, part);
384 }
Rob Clark884bcf62017-09-13 18:05:31 -0400385 diskobj->part = part;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200386
387 /*
388 * Install the device path and the block IO protocol.
389 *
390 * InstallMultipleProtocolInterfaces() checks if the device path is
391 * already installed on an other handle and returns EFI_ALREADY_STARTED
392 * in this case.
393 */
394 handle = &diskobj->header;
395 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
396 &handle, &efi_guid_device_path, diskobj->dp,
397 &efi_block_io_guid, &diskobj->ops, NULL));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100398 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100399 return ret;
Heinrich Schuchardt0a87e052020-05-21 09:22:06 +0200400
401 /*
402 * On partitions or whole disks without partitions install the
403 * simple file system protocol if a file system is available.
404 */
AKASHI Takahiro89cb6a52019-10-07 14:59:39 +0900405 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
406 efi_fs_exists(desc, part)) {
Rob Clark2a920802017-09-13 18:05:34 -0400407 diskobj->volume = efi_simple_file_system(desc, part,
408 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200409 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100410 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100411 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100412 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100413 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400414 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200415 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600416 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200417 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200418 diskobj->offset = offset;
Alexander Graff9d334b2016-08-05 14:49:53 +0200419 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200420
421 /* Fill in EFI IO Media info (for read/write callbacks) */
422 diskobj->media.removable_media = desc->removable;
423 diskobj->media.media_present = 1;
Heinrich Schuchardtf59f0822019-09-05 19:31:23 +0200424 /*
425 * MediaID is just an arbitrary counter.
426 * We have to change it if the medium is removed or changed.
427 */
428 diskobj->media.media_id = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200429 diskobj->media.block_size = desc->blksz;
430 diskobj->media.io_align = desc->blksz;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200431 diskobj->media.last_block = desc->lba - offset;
Heinrich Schuchardt9f888962020-03-19 15:45:52 +0100432 if (part)
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100433 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200434 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100435 if (disk)
436 *disk = diskobj;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100437
438 /* Store first EFI system partition */
439 if (part && !efi_system_partition.if_type) {
440 int r;
Simon Glass05289792020-05-10 11:39:57 -0600441 struct disk_partition info;
Heinrich Schuchardt11078bb2020-03-19 15:16:31 +0100442
443 r = part_get_info(desc, part, &info);
444 if (r)
445 return EFI_DEVICE_ERROR;
446 if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
447 efi_system_partition.if_type = desc->if_type;
448 efi_system_partition.devnum = desc->devnum;
449 efi_system_partition.part = part;
450 EFI_PRINT("EFI system partition: %s %d:%d\n",
451 blk_get_if_type_name(desc->if_type),
452 desc->devnum, part);
453 }
454 }
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100455 return EFI_SUCCESS;
Alexander Graf4a12a972016-04-11 16:16:17 +0200456}
457
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100458/**
459 * efi_disk_create_partitions() - create handles and protocols for partitions
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100460 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100461 * Create handles and protocols for the partitions of a block device.
462 *
463 * @parent: handle of the parent disk
Heinrich Schuchardt55976b72020-04-10 17:10:34 +0200464 * @desc: block device
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100465 * @if_typename: interface type
466 * @diskid: device number
467 * @pdevname: device name
468 * Return: number of partitions created
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100469 */
470int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
471 const char *if_typename, int diskid,
472 const char *pdevname)
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200473{
474 int disks = 0;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200475 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Simon Glass05289792020-05-10 11:39:57 -0600476 struct disk_partition info;
Jonathan Gray16a73b22017-10-10 13:55:26 +1100477 int part;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100478 struct efi_device_path *dp = NULL;
479 efi_status_t ret;
480 struct efi_handler *handler;
481
482 /* Get the device path of the parent */
483 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
484 if (ret == EFI_SUCCESS)
485 dp = handler->protocol_interface;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200486
Alexander Grafc034b7f2017-12-01 16:10:33 +0100487 /* Add devices for each partition */
Jonathan Gray16a73b22017-10-10 13:55:26 +1100488 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
489 if (part_get_info(desc, part, &info))
490 continue;
Alexander Graff9d334b2016-08-05 14:49:53 +0200491 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
492 part);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100493 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
494 info.start, part, NULL);
495 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200496 log_err("Adding partition %s failed\n", pdevname);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100497 continue;
498 }
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200499 disks++;
500 }
Rob Clark884bcf62017-09-13 18:05:31 -0400501
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200502 return disks;
503}
504
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100505/**
506 * efi_disk_register() - register block devices
507 *
Alexander Graf2a22d052016-03-04 01:10:02 +0100508 * U-Boot doesn't have a list of all online disk devices. So when running our
509 * EFI payload, we scan through all of the potentially available ones and
510 * store them in our object pool.
511 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100512 * This function is called in efi_init_obj_list().
513 *
Simon Glass487d7562016-05-14 14:03:05 -0600514 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
515 * Consider converting the code to look up devices as needed. The EFI device
516 * could be a child of the UCLASS_BLK block device, perhaps.
517 *
Heinrich Schuchardt47a95962020-03-19 13:49:34 +0100518 * Return: status code
Alexander Graf2a22d052016-03-04 01:10:02 +0100519 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100520efi_status_t efi_disk_register(void)
Alexander Graf2a22d052016-03-04 01:10:02 +0100521{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100522 struct efi_disk_obj *disk;
Alexander Graf2a22d052016-03-04 01:10:02 +0100523 int disks = 0;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100524 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600525#ifdef CONFIG_BLK
526 struct udevice *dev;
527
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100528 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000529 uclass_next_device_check(&dev)) {
Simon Glass487d7562016-05-14 14:03:05 -0600530 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt9bfca9f2018-01-19 20:24:44 +0100531 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass487d7562016-05-14 14:03:05 -0600532
Alexander Grafc034b7f2017-12-01 16:10:33 +0100533 /* Add block device for the full device */
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200534 log_info("Scanning disk %s...\n", dev->name);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100535 ret = efi_disk_add_dev(NULL, NULL, if_typename,
536 desc, desc->devnum, 0, 0, &disk);
537 if (ret == EFI_NOT_READY) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200538 log_notice("Disk %s not ready\n", dev->name);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100539 continue;
540 }
541 if (ret) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200542 log_err("ERROR: failure to add disk device %s, r = %lu\n",
543 dev->name, ret & ~EFI_ERROR_MASK);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100544 return ret;
545 }
Simon Glass487d7562016-05-14 14:03:05 -0600546 disks++;
547
Alexander Grafc034b7f2017-12-01 16:10:33 +0100548 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100549 disks += efi_disk_create_partitions(
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200550 &disk->header, desc, if_typename,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100551 desc->devnum, dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600552 }
553#else
554 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100555
556 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600557 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass487d7562016-05-14 14:03:05 -0600558 const struct blk_driver *cur_drvr;
559 const char *if_typename;
560
Simon Glass6dd9faf2016-05-01 13:52:32 -0600561 cur_drvr = blk_driver_lookup_type(if_type);
562 if (!cur_drvr)
563 continue;
564
Simon Glass487d7562016-05-14 14:03:05 -0600565 if_typename = cur_drvr->if_typename;
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200566 log_info("Scanning disks on %s...\n", if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100567 for (i = 0; i < 4; i++) {
568 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200569 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100570
Simon Glass6dd9faf2016-05-01 13:52:32 -0600571 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100572 if (!desc)
573 continue;
574 if (desc->type == DEV_TYPE_UNKNOWN)
575 continue;
576
Alexander Graf2a22d052016-03-04 01:10:02 +0100577 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass487d7562016-05-14 14:03:05 -0600578 if_typename, i);
Rob Clark77511b32017-10-08 11:33:08 -0400579
Alexander Grafc034b7f2017-12-01 16:10:33 +0100580 /* Add block device for the full device */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100581 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
582 i, 0, 0, &disk);
583 if (ret == EFI_NOT_READY) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200584 log_notice("Disk %s not ready\n", devname);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100585 continue;
586 }
587 if (ret) {
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200588 log_err("ERROR: failure to add disk device %s, r = %lu\n",
589 devname, ret & ~EFI_ERROR_MASK);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100590 return ret;
591 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100592 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200593
Alexander Grafc034b7f2017-12-01 16:10:33 +0100594 /* Partitions show up as block devices in EFI */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200595 disks += efi_disk_create_partitions
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200596 (&disk->header, desc,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200597 if_typename, i, devname);
Alexander Graf2a22d052016-03-04 01:10:02 +0100598 }
599 }
Simon Glass487d7562016-05-14 14:03:05 -0600600#endif
Heinrich Schuchardtaf457cf2020-07-17 20:33:05 +0200601 log_info("Found %d disks\n", disks);
Alexander Graf2a22d052016-03-04 01:10:02 +0100602
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100603 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100604}
AKASHI Takahiro41fd5062020-04-27 18:48:20 +0900605
606/**
607 * efi_disk_is_system_part() - check if handle refers to an EFI system partition
608 *
609 * @handle: handle of partition
610 *
611 * Return: true if handle refers to an EFI system partition
612 */
613bool efi_disk_is_system_part(efi_handle_t handle)
614{
615 struct efi_handler *handler;
616 struct efi_disk_obj *diskobj;
Simon Glass05289792020-05-10 11:39:57 -0600617 struct disk_partition info;
AKASHI Takahiro41fd5062020-04-27 18:48:20 +0900618 efi_status_t ret;
619 int r;
620
621 /* check if this is a block device */
622 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
623 if (ret != EFI_SUCCESS)
624 return false;
625
626 diskobj = container_of(handle, struct efi_disk_obj, header);
627
628 r = part_get_info(diskobj->desc, diskobj->part, &info);
629 if (r)
630 return false;
631
632 return !!(info.bootable & PART_EFI_SYSTEM_PARTITION);
633}