blob: 5c6ec5258cdad97e7b855d1f99ec9ad630f046bc [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>
12#include <inttypes.h>
13#include <part.h>
14#include <malloc.h>
15
Heinrich Schuchardt405afb52018-01-19 20:24:45 +010016const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
Alexander Grafe83be452016-03-04 01:10:02 +010017
18struct efi_disk_obj {
19 /* Generic EFI object parent class data */
20 struct efi_object parent;
21 /* EFI Interface callback struct for block I/O */
22 struct efi_block_io ops;
23 /* U-Boot ifname for block device */
24 const char *ifname;
25 /* U-Boot dev_index for block device */
26 int dev_index;
27 /* EFI Interface Media descriptor struct, referenced by ops */
28 struct efi_block_io_media media;
29 /* EFI device path to this block device */
Rob Clarka83272f2017-09-13 18:05:31 -040030 struct efi_device_path *dp;
31 /* partition # */
32 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040033 /* handle to filesys proto (for partition objects) */
34 struct efi_simple_file_system_protocol *volume;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020035 /* Offset into disk for simple partitions */
36 lbaint_t offset;
Alexander Graf34118e72016-08-05 14:49:53 +020037 /* Internal block device */
Rob Clarka83272f2017-09-13 18:05:31 -040038 struct blk_desc *desc;
Alexander Grafe83be452016-03-04 01:10:02 +010039};
40
Alexander Grafe83be452016-03-04 01:10:02 +010041static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
42 char extended_verification)
43{
44 EFI_ENTRY("%p, %x", this, extended_verification);
45 return EFI_EXIT(EFI_DEVICE_ERROR);
46}
47
48enum efi_disk_direction {
49 EFI_DISK_READ,
50 EFI_DISK_WRITE,
51};
52
Heinrich Schuchardt9ce06172017-08-26 22:33:13 +020053static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-03-04 01:10:02 +010054 u32 media_id, u64 lba, unsigned long buffer_size,
55 void *buffer, enum efi_disk_direction direction)
56{
57 struct efi_disk_obj *diskobj;
58 struct blk_desc *desc;
59 int blksz;
60 int blocks;
61 unsigned long n;
62
Alexander Grafe83be452016-03-04 01:10:02 +010063 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graf34118e72016-08-05 14:49:53 +020064 desc = (struct blk_desc *) diskobj->desc;
Alexander Grafe83be452016-03-04 01:10:02 +010065 blksz = desc->blksz;
66 blocks = buffer_size / blksz;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020067 lba += diskobj->offset;
Alexander Grafe83be452016-03-04 01:10:02 +010068
Alexander Graf62a67482016-06-02 11:38:27 +020069 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
70 __LINE__, blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +010071
72 /* We only support full block access */
73 if (buffer_size & (blksz - 1))
Rob Clark99c43872017-07-25 20:28:29 -040074 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +010075
76 if (direction == EFI_DISK_READ)
Simon Glass302d4f82016-05-14 14:03:05 -060077 n = blk_dread(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010078 else
Simon Glass302d4f82016-05-14 14:03:05 -060079 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010080
81 /* We don't do interrupts, so check for timers cooperatively */
82 efi_timer_check();
83
Alexander Graf62a67482016-06-02 11:38:27 +020084 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
85
Alexander Grafe83be452016-03-04 01:10:02 +010086 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -040087 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +010088
Rob Clark99c43872017-07-25 20:28:29 -040089 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +010090}
91
Simon Glassc2194bf2016-09-25 15:27:32 -060092static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +010093 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +010094 void *buffer)
95{
Alexander Graf7c00a3c2016-05-11 18:25:48 +020096 void *real_buffer = buffer;
97 efi_status_t r;
98
99#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
100 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
101 r = efi_disk_read_blocks(this, media_id, lba,
102 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
103 if (r != EFI_SUCCESS)
104 return r;
105 return efi_disk_read_blocks(this, media_id, lba +
106 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
107 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
108 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
109 }
110
111 real_buffer = efi_bounce_buffer;
112#endif
113
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100114 EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200115 buffer_size, buffer);
116
117 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
118 EFI_DISK_READ);
119
120 /* Copy from bounce buffer to real buffer if necessary */
121 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
122 memcpy(buffer, real_buffer, buffer_size);
123
124 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100125}
126
Simon Glassc2194bf2016-09-25 15:27:32 -0600127static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100128 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100129 void *buffer)
130{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200131 void *real_buffer = buffer;
132 efi_status_t r;
133
134#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
135 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
136 r = efi_disk_write_blocks(this, media_id, lba,
137 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
138 if (r != EFI_SUCCESS)
139 return r;
140 return efi_disk_write_blocks(this, media_id, lba +
141 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
142 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
143 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
144 }
145
146 real_buffer = efi_bounce_buffer;
147#endif
148
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100149 EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200150 buffer_size, buffer);
151
152 /* Populate bounce buffer if necessary */
153 if (real_buffer != buffer)
154 memcpy(real_buffer, buffer, buffer_size);
155
156 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
157 EFI_DISK_WRITE);
158
159 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100160}
161
162static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
163{
164 /* We always write synchronously */
165 EFI_ENTRY("%p", this);
166 return EFI_EXIT(EFI_SUCCESS);
167}
168
169static const struct efi_block_io block_io_disk_template = {
170 .reset = &efi_disk_reset,
171 .read_blocks = &efi_disk_read_blocks,
172 .write_blocks = &efi_disk_write_blocks,
173 .flush_blocks = &efi_disk_flush_blocks,
174};
175
Rob Clark53142032017-09-13 18:05:34 -0400176/*
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100177 * Get the simple file system protocol for a file device path.
178 *
179 * The full path provided is split into device part and into a file
180 * part. The device part is used to find the handle on which the
181 * simple file system protocol is installed.
182 *
183 * @full_path device path including device and file
184 * @return simple file system protocol
Rob Clark53142032017-09-13 18:05:34 -0400185 */
186struct efi_simple_file_system_protocol *
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100187efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark53142032017-09-13 18:05:34 -0400188{
189 struct efi_object *efiobj;
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100190 struct efi_handler *handler;
191 struct efi_device_path *device_path;
192 struct efi_device_path *file_path;
193 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400194
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100195 /* Split the path into a device part and a file part */
196 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
197 if (ret != EFI_SUCCESS)
198 return NULL;
199 efi_free_pool(file_path);
200
201 /* Get the EFI object for the partition */
202 efiobj = efi_dp_find_obj(device_path, NULL);
203 efi_free_pool(device_path);
Rob Clark53142032017-09-13 18:05:34 -0400204 if (!efiobj)
205 return NULL;
206
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100207 /* Find the simple file system protocol */
208 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
209 &handler);
210 if (ret != EFI_SUCCESS)
211 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400212
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100213 /* Return the simple file system protocol for the partition */
214 return handler->protocol_interface;
Rob Clark53142032017-09-13 18:05:34 -0400215}
216
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200217/*
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100218 * Create a handle for a partition or disk
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200219 *
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100220 * @parent parent handle
221 * @dp_parent parent device path
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200222 * @if_typename interface name for block device
223 * @desc internal block device
224 * @dev_index device index for block device
225 * @offset offset into disk for simple partitions
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100226 * @return disk object
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200227 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100228static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100229 efi_handle_t parent,
230 struct efi_device_path *dp_parent,
231 const char *if_typename,
232 struct blk_desc *desc,
233 int dev_index,
234 lbaint_t offset,
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100235 unsigned int part,
236 struct efi_disk_obj **disk)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200237{
238 struct efi_disk_obj *diskobj;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100239 efi_status_t ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200240
Alexander Graf601db1f2016-08-05 14:51:47 +0200241 /* Don't add empty devices */
242 if (!desc->lba)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100243 return EFI_NOT_READY;
Alexander Graf601db1f2016-08-05 14:51:47 +0200244
Rob Clarka83272f2017-09-13 18:05:31 -0400245 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100246 if (!diskobj)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100247 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100248
249 /* Hook up to the device list */
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100250 efi_add_handle(&diskobj->parent);
Alexander Graf06fe57b2016-04-11 16:16:17 +0200251
252 /* Fill in object data */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100253 if (part) {
254 struct efi_device_path *node = efi_dp_part_node(desc, part);
255
256 diskobj->dp = efi_dp_append_node(dp_parent, node);
257 efi_free_pool(node);
258 } else {
259 diskobj->dp = efi_dp_from_part(desc, part);
260 }
Rob Clarka83272f2017-09-13 18:05:31 -0400261 diskobj->part = part;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100262 ret = efi_add_protocol(diskobj->parent.handle, &efi_block_io_guid,
263 &diskobj->ops);
264 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100265 return ret;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100266 ret = efi_add_protocol(diskobj->parent.handle, &efi_guid_device_path,
267 diskobj->dp);
268 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100269 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400270 if (part >= 1) {
271 diskobj->volume = efi_simple_file_system(desc, part,
272 diskobj->dp);
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100273 ret = efi_add_protocol(diskobj->parent.handle,
274 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardtce721a82018-01-19 20:24:38 +0100275 diskobj->volume);
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100276 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100277 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400278 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200279 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600280 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200281 diskobj->dev_index = dev_index;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200282 diskobj->offset = offset;
Alexander Graf34118e72016-08-05 14:49:53 +0200283 diskobj->desc = desc;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200284
285 /* Fill in EFI IO Media info (for read/write callbacks) */
286 diskobj->media.removable_media = desc->removable;
287 diskobj->media.media_present = 1;
288 diskobj->media.block_size = desc->blksz;
289 diskobj->media.io_align = desc->blksz;
Alexander Graf601db1f2016-08-05 14:51:47 +0200290 diskobj->media.last_block = desc->lba - offset;
Emmanuel Vadot5bf29642017-12-11 19:22:33 +0100291 if (part != 0)
292 diskobj->media.logical_partition = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200293 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100294 if (disk)
295 *disk = diskobj;
296 return EFI_SUCCESS;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200297}
298
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100299/*
300 * Create handles and protocols for the partitions of a block device
301 *
302 * @parent handle of the parent disk
303 * @blk_desc block device
304 * @if_typename interface type
305 * @diskid device number
306 * @pdevname device name
307 * @return number of partitions created
308 */
309int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
310 const char *if_typename, int diskid,
311 const char *pdevname)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200312{
313 int disks = 0;
Alexander Graf3491ee72016-04-11 16:16:20 +0200314 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200315 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100316 int part;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100317 struct efi_device_path *dp = NULL;
318 efi_status_t ret;
319 struct efi_handler *handler;
320
321 /* Get the device path of the parent */
322 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
323 if (ret == EFI_SUCCESS)
324 dp = handler->protocol_interface;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200325
Alexander Graf83a83f22017-12-01 16:10:33 +0100326 /* Add devices for each partition */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100327 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
328 if (part_get_info(desc, part, &info))
329 continue;
Alexander Graf34118e72016-08-05 14:49:53 +0200330 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
331 part);
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100332 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
333 info.start, part, NULL);
334 if (ret != EFI_SUCCESS) {
335 printf("Adding partition %s failed\n", pdevname);
336 continue;
337 }
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200338 disks++;
339 }
Rob Clarka83272f2017-09-13 18:05:31 -0400340
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200341 return disks;
342}
343
Alexander Grafe83be452016-03-04 01:10:02 +0100344/*
345 * U-Boot doesn't have a list of all online disk devices. So when running our
346 * EFI payload, we scan through all of the potentially available ones and
347 * store them in our object pool.
348 *
Simon Glass302d4f82016-05-14 14:03:05 -0600349 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
350 * Consider converting the code to look up devices as needed. The EFI device
351 * could be a child of the UCLASS_BLK block device, perhaps.
352 *
Alexander Grafe83be452016-03-04 01:10:02 +0100353 * This gets called from do_bootefi_exec().
354 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100355efi_status_t efi_disk_register(void)
Alexander Grafe83be452016-03-04 01:10:02 +0100356{
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100357 struct efi_disk_obj *disk;
Alexander Grafe83be452016-03-04 01:10:02 +0100358 int disks = 0;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100359 efi_status_t ret;
Simon Glass302d4f82016-05-14 14:03:05 -0600360#ifdef CONFIG_BLK
361 struct udevice *dev;
362
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100363 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de308cd8e2017-06-20 19:10:27 +0000364 uclass_next_device_check(&dev)) {
Simon Glass302d4f82016-05-14 14:03:05 -0600365 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt6a18bb32018-01-19 20:24:44 +0100366 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass302d4f82016-05-14 14:03:05 -0600367
Alexander Graf83a83f22017-12-01 16:10:33 +0100368 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100369 printf("Scanning disk %s...\n", dev->name);
370 ret = efi_disk_add_dev(NULL, NULL, if_typename,
371 desc, desc->devnum, 0, 0, &disk);
372 if (ret == EFI_NOT_READY) {
373 printf("Disk %s not ready\n", dev->name);
374 continue;
375 }
376 if (ret) {
377 printf("ERROR: failure to add disk device %s, r = %lu\n",
378 dev->name, ret & ~EFI_ERROR_MASK);
379 return ret;
380 }
Simon Glass302d4f82016-05-14 14:03:05 -0600381 disks++;
382
Alexander Graf83a83f22017-12-01 16:10:33 +0100383 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100384 disks += efi_disk_create_partitions(
385 disk->parent.handle, desc, if_typename,
386 desc->devnum, dev->name);
Simon Glass302d4f82016-05-14 14:03:05 -0600387 }
388#else
389 int i, if_type;
Alexander Grafe83be452016-03-04 01:10:02 +0100390
391 /* Search for all available disk devices */
Simon Glasse3dfa912016-05-01 13:52:32 -0600392 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass302d4f82016-05-14 14:03:05 -0600393 const struct blk_driver *cur_drvr;
394 const char *if_typename;
395
Simon Glasse3dfa912016-05-01 13:52:32 -0600396 cur_drvr = blk_driver_lookup_type(if_type);
397 if (!cur_drvr)
398 continue;
399
Simon Glass302d4f82016-05-14 14:03:05 -0600400 if_typename = cur_drvr->if_typename;
401 printf("Scanning disks on %s...\n", if_typename);
Alexander Grafe83be452016-03-04 01:10:02 +0100402 for (i = 0; i < 4; i++) {
403 struct blk_desc *desc;
Alexander Graf3491ee72016-04-11 16:16:20 +0200404 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafe83be452016-03-04 01:10:02 +0100405
Simon Glasse3dfa912016-05-01 13:52:32 -0600406 desc = blk_get_devnum_by_type(if_type, i);
Alexander Grafe83be452016-03-04 01:10:02 +0100407 if (!desc)
408 continue;
409 if (desc->type == DEV_TYPE_UNKNOWN)
410 continue;
411
Alexander Grafe83be452016-03-04 01:10:02 +0100412 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass302d4f82016-05-14 14:03:05 -0600413 if_typename, i);
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400414
Alexander Graf83a83f22017-12-01 16:10:33 +0100415 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100416 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
417 i, 0, 0, &disk);
418 if (ret == EFI_NOT_READY) {
419 printf("Disk %s not ready\n", devname);
420 continue;
421 }
422 if (ret) {
423 printf("ERROR: failure to add disk device %s, r = %lu\n",
424 devname, ret & ~EFI_ERROR_MASK);
425 return ret;
426 }
Alexander Grafe83be452016-03-04 01:10:02 +0100427 disks++;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200428
Alexander Graf83a83f22017-12-01 16:10:33 +0100429 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100430 disks += efi_disk_create_partitions(
431 disk->parent.handle, desc,
432 if_typename, i, devname);
Alexander Grafe83be452016-03-04 01:10:02 +0100433 }
434 }
Simon Glass302d4f82016-05-14 14:03:05 -0600435#endif
Alexander Grafe83be452016-03-04 01:10:02 +0100436 printf("Found %d disks\n", disks);
437
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100438 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100439}