blob: fd8fe175670989086f28343b607d2dcee8658683 [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
8#include <common.h>
Simon Glasse3dfa912016-05-01 13:52:32 -06009#include <blk.h>
Simon Glass302d4f82016-05-14 14:03:05 -060010#include <dm.h>
Alexander Grafe83be452016-03-04 01:10:02 +010011#include <efi_loader.h>
AKASHI Takahiro120a01a2019-10-07 14:59:38 +090012#include <fs.h>
Alexander Grafe83be452016-03-04 01:10:02 +010013#include <part.h>
14#include <malloc.h>
15
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020016const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Alexander Grafe83be452016-03-04 01:10:02 +010017
Heinrich Schuchardt72928722018-09-26 05:27:56 +020018/**
19 * struct efi_disk_obj - EFI disk object
20 *
21 * @header: EFI object header
22 * @ops: EFI disk I/O protocol interface
23 * @ifname: interface name for block device
24 * @dev_index: device index of block device
25 * @media: block I/O media information
26 * @dp: device path to the block device
27 * @part: partition
28 * @volume: simple file system protocol of the partition
29 * @offset: offset into disk for simple partition
30 * @desc: internal block device descriptor
31 */
Alexander Grafe83be452016-03-04 01:10:02 +010032struct efi_disk_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020033 struct efi_object header;
Alexander Grafe83be452016-03-04 01:10:02 +010034 struct efi_block_io ops;
Alexander Grafe83be452016-03-04 01:10:02 +010035 const char *ifname;
Alexander Grafe83be452016-03-04 01:10:02 +010036 int dev_index;
Alexander Grafe83be452016-03-04 01:10:02 +010037 struct efi_block_io_media media;
Rob Clarka83272f2017-09-13 18:05:31 -040038 struct efi_device_path *dp;
Rob Clarka83272f2017-09-13 18:05:31 -040039 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040040 struct efi_simple_file_system_protocol *volume;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020041 lbaint_t offset;
Rob Clarka83272f2017-09-13 18:05:31 -040042 struct blk_desc *desc;
Alexander Grafe83be452016-03-04 01:10:02 +010043};
44
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020045/**
46 * efi_disk_reset() - reset block device
47 *
48 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
49 *
50 * As U-Boot's block devices do not have a reset function simply return
51 * EFI_SUCCESS.
52 *
53 * See the Unified Extensible Firmware Interface (UEFI) specification for
54 * details.
55 *
56 * @this: pointer to the BLOCK_IO_PROTOCOL
57 * @extended_verification: extended verification
58 * Return: status code
59 */
Alexander Grafe83be452016-03-04 01:10:02 +010060static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
61 char extended_verification)
62{
63 EFI_ENTRY("%p, %x", this, extended_verification);
Heinrich Schuchardt670198c2019-09-05 20:13:46 +020064 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafe83be452016-03-04 01:10:02 +010065}
66
67enum efi_disk_direction {
68 EFI_DISK_READ,
69 EFI_DISK_WRITE,
70};
71
Heinrich Schuchardt9ce06172017-08-26 22:33:13 +020072static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +010073 u32 media_id, u64 lba, unsigned long buffer_size,
74 void *buffer, enum efi_disk_direction direction)
75{
76 struct efi_disk_obj *diskobj;
77 struct blk_desc *desc;
78 int blksz;
79 int blocks;
80 unsigned long n;
81
Alexander Grafe83be452016-03-04 01:10:02 +010082 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graf34118e72016-08-05 14:49:53 +020083 desc = (struct blk_desc *) diskobj->desc;
Alexander Grafe83be452016-03-04 01:10:02 +010084 blksz = desc->blksz;
85 blocks = buffer_size / blksz;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020086 lba += diskobj->offset;
Alexander Grafe83be452016-03-04 01:10:02 +010087
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +020088 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
89 blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +010090
91 /* We only support full block access */
92 if (buffer_size & (blksz - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +020093 return EFI_BAD_BUFFER_SIZE;
Alexander Grafe83be452016-03-04 01:10:02 +010094
95 if (direction == EFI_DISK_READ)
Simon Glass302d4f82016-05-14 14:03:05 -060096 n = blk_dread(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010097 else
Simon Glass302d4f82016-05-14 14:03:05 -060098 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010099
100 /* We don't do interrupts, so check for timers cooperatively */
101 efi_timer_check();
102
Heinrich Schuchardtee6ef8b2019-09-05 19:43:17 +0200103 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
Alexander Graf62a67482016-06-02 11:38:27 +0200104
Alexander Grafe83be452016-03-04 01:10:02 +0100105 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -0400106 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +0100107
Rob Clark99c43872017-07-25 20:28:29 -0400108 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100109}
110
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200111/**
112 * efi_disk_read_blocks() - reads blocks from device
113 *
114 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
115 *
116 * See the Unified Extensible Firmware Interface (UEFI) specification for
117 * details.
118 *
119 * @this: pointer to the BLOCK_IO_PROTOCOL
120 * @media_id: id of the medium to be read from
121 * @lba: starting logical block for reading
122 * @buffer_size: size of the read buffer
123 * @buffer: pointer to the destination buffer
124 * Return: status code
125 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600126static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100127 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100128 void *buffer)
129{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200130 void *real_buffer = buffer;
131 efi_status_t r;
132
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200133 if (!this)
134 return EFI_INVALID_PARAMETER;
135 /* TODO: check for media changes */
136 if (media_id != this->media->media_id)
137 return EFI_MEDIA_CHANGED;
138 if (!this->media->media_present)
139 return EFI_NO_MEDIA;
140 /* media->io_align is a power of 2 */
141 if ((uintptr_t)buffer & (this->media->io_align - 1))
142 return EFI_INVALID_PARAMETER;
143 if (lba * this->media->block_size + buffer_size >
144 this->media->last_block * this->media->block_size)
145 return EFI_INVALID_PARAMETER;
146
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200147#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
148 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
149 r = efi_disk_read_blocks(this, media_id, lba,
150 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
151 if (r != EFI_SUCCESS)
152 return r;
153 return efi_disk_read_blocks(this, media_id, lba +
154 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
155 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
156 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
157 }
158
159 real_buffer = efi_bounce_buffer;
160#endif
161
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900162 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200163 buffer_size, buffer);
164
165 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
166 EFI_DISK_READ);
167
168 /* Copy from bounce buffer to real buffer if necessary */
169 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
170 memcpy(buffer, real_buffer, buffer_size);
171
172 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100173}
174
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200175/**
176 * efi_disk_write_blocks() - writes blocks to device
177 *
178 * This function implements the WriteBlocks service of the
179 * EFI_BLOCK_IO_PROTOCOL.
180 *
181 * See the Unified Extensible Firmware Interface (UEFI) specification for
182 * details.
183 *
184 * @this: pointer to the BLOCK_IO_PROTOCOL
185 * @media_id: id of the medium to be written to
186 * @lba: starting logical block for writing
187 * @buffer_size: size of the write buffer
188 * @buffer: pointer to the source buffer
189 * Return: status code
190 */
Simon Glassc2194bf2016-09-25 15:27:32 -0600191static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100192 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100193 void *buffer)
194{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200195 void *real_buffer = buffer;
196 efi_status_t r;
197
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200198 if (!this)
199 return EFI_INVALID_PARAMETER;
200 if (this->media->read_only)
201 return EFI_WRITE_PROTECTED;
202 /* TODO: check for media changes */
203 if (media_id != this->media->media_id)
204 return EFI_MEDIA_CHANGED;
205 if (!this->media->media_present)
206 return EFI_NO_MEDIA;
207 /* media->io_align is a power of 2 */
208 if ((uintptr_t)buffer & (this->media->io_align - 1))
209 return EFI_INVALID_PARAMETER;
210 if (lba * this->media->block_size + buffer_size >
211 this->media->last_block * this->media->block_size)
212 return EFI_INVALID_PARAMETER;
213
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200214#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
215 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
216 r = efi_disk_write_blocks(this, media_id, lba,
217 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
218 if (r != EFI_SUCCESS)
219 return r;
220 return efi_disk_write_blocks(this, media_id, lba +
221 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
222 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
223 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
224 }
225
226 real_buffer = efi_bounce_buffer;
227#endif
228
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900229 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200230 buffer_size, buffer);
231
232 /* Populate bounce buffer if necessary */
233 if (real_buffer != buffer)
234 memcpy(real_buffer, buffer, buffer_size);
235
236 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
237 EFI_DISK_WRITE);
238
239 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100240}
241
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200242/**
243 * efi_disk_flush_blocks() - flushes modified data to the device
244 *
245 * This function implements the FlushBlocks service of the
246 * EFI_BLOCK_IO_PROTOCOL.
247 *
248 * As we always write synchronously nothing is done here.
249 *
250 * See the Unified Extensible Firmware Interface (UEFI) specification for
251 * details.
252 *
253 * @this: pointer to the BLOCK_IO_PROTOCOL
254 * Return: status code
255 */
Alexander Grafe83be452016-03-04 01:10:02 +0100256static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
257{
Alexander Grafe83be452016-03-04 01:10:02 +0100258 EFI_ENTRY("%p", this);
259 return EFI_EXIT(EFI_SUCCESS);
260}
261
262static const struct efi_block_io block_io_disk_template = {
263 .reset = &efi_disk_reset,
264 .read_blocks = &efi_disk_read_blocks,
265 .write_blocks = &efi_disk_write_blocks,
266 .flush_blocks = &efi_disk_flush_blocks,
267};
268
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100269/**
270 * efi_fs_from_path() - retrieve simple file system protocol
271 *
272 * Gets the simple file system protocol for a file device path.
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100273 *
274 * The full path provided is split into device part and into a file
275 * part. The device part is used to find the handle on which the
276 * simple file system protocol is installed.
277 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100278 * @full_path: device path including device and file
279 * Return: simple file system protocol
Rob Clark53142032017-09-13 18:05:34 -0400280 */
281struct efi_simple_file_system_protocol *
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100282efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark53142032017-09-13 18:05:34 -0400283{
284 struct efi_object *efiobj;
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100285 struct efi_handler *handler;
286 struct efi_device_path *device_path;
287 struct efi_device_path *file_path;
288 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400289
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100290 /* Split the path into a device part and a file part */
291 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
292 if (ret != EFI_SUCCESS)
293 return NULL;
294 efi_free_pool(file_path);
295
296 /* Get the EFI object for the partition */
297 efiobj = efi_dp_find_obj(device_path, NULL);
298 efi_free_pool(device_path);
Rob Clark53142032017-09-13 18:05:34 -0400299 if (!efiobj)
300 return NULL;
301
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100302 /* Find the simple file system protocol */
303 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
304 &handler);
305 if (ret != EFI_SUCCESS)
306 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400307
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100308 /* Return the simple file system protocol for the partition */
309 return handler->protocol_interface;
Rob Clark53142032017-09-13 18:05:34 -0400310}
311
AKASHI Takahiro120a01a2019-10-07 14:59:38 +0900312/**
313 * efi_fs_exists() - check if a partition bears a file system
314 *
315 * @desc: block device descriptor
316 * @part: partition number
317 * Return: 1 if a file system exists on the partition
318 * 0 otherwise
319 */
320static int efi_fs_exists(struct blk_desc *desc, int part)
321{
322 if (fs_set_blk_dev_with_part(desc, part))
323 return 0;
324
325 if (fs_get_type() == FS_TYPE_ANY)
326 return 0;
327
328 fs_close();
329
330 return 1;
331}
332
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200333/**
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100334 * efi_disk_add_dev() - create a handle for a partition or disk
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200335 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100336 * @parent: parent handle
337 * @dp_parent: parent device path
338 * @if_typename: interface name for block device
339 * @desc: internal block device
340 * @dev_index: device index for block device
341 * @offset: offset into disk for simple partitions
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200342 * @part: partition
343 * @disk: pointer to receive the created handle
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100344 * Return: disk object
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200345 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100346static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100347 efi_handle_t parent,
348 struct efi_device_path *dp_parent,
349 const char *if_typename,
350 struct blk_desc *desc,
351 int dev_index,
352 lbaint_t offset,
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100353 unsigned int part,
354 struct efi_disk_obj **disk)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200355{
356 struct efi_disk_obj *diskobj;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100357 efi_status_t ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200358
Alexander Graf601db1f2016-08-05 14:51:47 +0200359 /* Don't add empty devices */
360 if (!desc->lba)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100361 return EFI_NOT_READY;
Alexander Graf601db1f2016-08-05 14:51:47 +0200362
Rob Clarka83272f2017-09-13 18:05:31 -0400363 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100364 if (!diskobj)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100365 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100366
367 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200368 efi_add_handle(&diskobj->header);
Alexander Graf06fe57b2016-04-11 16:16:17 +0200369
370 /* Fill in object data */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100371 if (part) {
372 struct efi_device_path *node = efi_dp_part_node(desc, part);
373
374 diskobj->dp = efi_dp_append_node(dp_parent, node);
375 efi_free_pool(node);
376 } else {
377 diskobj->dp = efi_dp_from_part(desc, part);
378 }
Rob Clarka83272f2017-09-13 18:05:31 -0400379 diskobj->part = part;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200380 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100381 &diskobj->ops);
382 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100383 return ret;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200384 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100385 diskobj->dp);
386 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100387 return ret;
AKASHI Takahiro356ef902019-10-07 14:59:39 +0900388 /* partitions or whole disk without partitions */
389 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
390 efi_fs_exists(desc, part)) {
Rob Clark53142032017-09-13 18:05:34 -0400391 diskobj->volume = efi_simple_file_system(desc, part,
392 diskobj->dp);
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200393 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100394 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardtce721a82018-01-19 20:24:38 +0100395 diskobj->volume);
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100396 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100397 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400398 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200399 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600400 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200401 diskobj->dev_index = dev_index;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200402 diskobj->offset = offset;
Alexander Graf34118e72016-08-05 14:49:53 +0200403 diskobj->desc = desc;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200404
405 /* Fill in EFI IO Media info (for read/write callbacks) */
406 diskobj->media.removable_media = desc->removable;
407 diskobj->media.media_present = 1;
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200408 /*
409 * MediaID is just an arbitrary counter.
410 * We have to change it if the medium is removed or changed.
411 */
412 diskobj->media.media_id = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200413 diskobj->media.block_size = desc->blksz;
414 diskobj->media.io_align = desc->blksz;
Alexander Graf601db1f2016-08-05 14:51:47 +0200415 diskobj->media.last_block = desc->lba - offset;
Heinrich Schuchardtaf8c5b62020-03-19 15:45:52 +0100416 if (part)
Emmanuel Vadot5bf29642017-12-11 19:22:33 +0100417 diskobj->media.logical_partition = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200418 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100419 if (disk)
420 *disk = diskobj;
421 return EFI_SUCCESS;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200422}
423
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100424/**
425 * efi_disk_create_partitions() - create handles and protocols for partitions
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100426 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100427 * Create handles and protocols for the partitions of a block device.
428 *
429 * @parent: handle of the parent disk
Heinrich Schuchardt76f57892020-04-10 17:10:34 +0200430 * @desc: block device
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100431 * @if_typename: interface type
432 * @diskid: device number
433 * @pdevname: device name
434 * Return: number of partitions created
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100435 */
436int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
437 const char *if_typename, int diskid,
438 const char *pdevname)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200439{
440 int disks = 0;
Alexander Graf3491ee72016-04-11 16:16:20 +0200441 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200442 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100443 int part;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100444 struct efi_device_path *dp = NULL;
445 efi_status_t ret;
446 struct efi_handler *handler;
447
448 /* Get the device path of the parent */
449 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
450 if (ret == EFI_SUCCESS)
451 dp = handler->protocol_interface;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200452
Alexander Graf83a83f22017-12-01 16:10:33 +0100453 /* Add devices for each partition */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100454 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
455 if (part_get_info(desc, part, &info))
456 continue;
Alexander Graf34118e72016-08-05 14:49:53 +0200457 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
458 part);
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100459 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
460 info.start, part, NULL);
461 if (ret != EFI_SUCCESS) {
462 printf("Adding partition %s failed\n", pdevname);
463 continue;
464 }
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200465 disks++;
466 }
Rob Clarka83272f2017-09-13 18:05:31 -0400467
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200468 return disks;
469}
470
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100471/**
472 * efi_disk_register() - register block devices
473 *
Alexander Grafe83be452016-03-04 01:10:02 +0100474 * U-Boot doesn't have a list of all online disk devices. So when running our
475 * EFI payload, we scan through all of the potentially available ones and
476 * store them in our object pool.
477 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100478 * This function is called in efi_init_obj_list().
479 *
Simon Glass302d4f82016-05-14 14:03:05 -0600480 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
481 * Consider converting the code to look up devices as needed. The EFI device
482 * could be a child of the UCLASS_BLK block device, perhaps.
483 *
Heinrich Schuchardt62036062020-03-19 13:49:34 +0100484 * Return: status code
Alexander Grafe83be452016-03-04 01:10:02 +0100485 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100486efi_status_t efi_disk_register(void)
Alexander Grafe83be452016-03-04 01:10:02 +0100487{
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100488 struct efi_disk_obj *disk;
Alexander Grafe83be452016-03-04 01:10:02 +0100489 int disks = 0;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100490 efi_status_t ret;
Simon Glass302d4f82016-05-14 14:03:05 -0600491#ifdef CONFIG_BLK
492 struct udevice *dev;
493
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100494 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de308cd8e2017-06-20 19:10:27 +0000495 uclass_next_device_check(&dev)) {
Simon Glass302d4f82016-05-14 14:03:05 -0600496 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt6a18bb32018-01-19 20:24:44 +0100497 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass302d4f82016-05-14 14:03:05 -0600498
Alexander Graf83a83f22017-12-01 16:10:33 +0100499 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100500 printf("Scanning disk %s...\n", dev->name);
501 ret = efi_disk_add_dev(NULL, NULL, if_typename,
502 desc, desc->devnum, 0, 0, &disk);
503 if (ret == EFI_NOT_READY) {
504 printf("Disk %s not ready\n", dev->name);
505 continue;
506 }
507 if (ret) {
508 printf("ERROR: failure to add disk device %s, r = %lu\n",
509 dev->name, ret & ~EFI_ERROR_MASK);
510 return ret;
511 }
Simon Glass302d4f82016-05-14 14:03:05 -0600512 disks++;
513
Alexander Graf83a83f22017-12-01 16:10:33 +0100514 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100515 disks += efi_disk_create_partitions(
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200516 &disk->header, desc, if_typename,
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100517 desc->devnum, dev->name);
Simon Glass302d4f82016-05-14 14:03:05 -0600518 }
519#else
520 int i, if_type;
Alexander Grafe83be452016-03-04 01:10:02 +0100521
522 /* Search for all available disk devices */
Simon Glasse3dfa912016-05-01 13:52:32 -0600523 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass302d4f82016-05-14 14:03:05 -0600524 const struct blk_driver *cur_drvr;
525 const char *if_typename;
526
Simon Glasse3dfa912016-05-01 13:52:32 -0600527 cur_drvr = blk_driver_lookup_type(if_type);
528 if (!cur_drvr)
529 continue;
530
Simon Glass302d4f82016-05-14 14:03:05 -0600531 if_typename = cur_drvr->if_typename;
532 printf("Scanning disks on %s...\n", if_typename);
Alexander Grafe83be452016-03-04 01:10:02 +0100533 for (i = 0; i < 4; i++) {
534 struct blk_desc *desc;
Alexander Graf3491ee72016-04-11 16:16:20 +0200535 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafe83be452016-03-04 01:10:02 +0100536
Simon Glasse3dfa912016-05-01 13:52:32 -0600537 desc = blk_get_devnum_by_type(if_type, i);
Alexander Grafe83be452016-03-04 01:10:02 +0100538 if (!desc)
539 continue;
540 if (desc->type == DEV_TYPE_UNKNOWN)
541 continue;
542
Alexander Grafe83be452016-03-04 01:10:02 +0100543 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass302d4f82016-05-14 14:03:05 -0600544 if_typename, i);
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400545
Alexander Graf83a83f22017-12-01 16:10:33 +0100546 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100547 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
548 i, 0, 0, &disk);
549 if (ret == EFI_NOT_READY) {
550 printf("Disk %s not ready\n", devname);
551 continue;
552 }
553 if (ret) {
554 printf("ERROR: failure to add disk device %s, r = %lu\n",
555 devname, ret & ~EFI_ERROR_MASK);
556 return ret;
557 }
Alexander Grafe83be452016-03-04 01:10:02 +0100558 disks++;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200559
Alexander Graf83a83f22017-12-01 16:10:33 +0100560 /* Partitions show up as block devices in EFI */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200561 disks += efi_disk_create_partitions
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200562 (&disk->header, desc,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200563 if_typename, i, devname);
Alexander Grafe83be452016-03-04 01:10:02 +0100564 }
565 }
Simon Glass302d4f82016-05-14 14:03:05 -0600566#endif
Alexander Grafe83be452016-03-04 01:10:02 +0100567 printf("Found %d disks\n", disks);
568
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100569 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100570}