blob: 7a6b06821a477895354807722c6d0dd36c046c04 [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>
Alexander Graf2a22d052016-03-04 01:10:02 +010012#include <part.h>
13#include <malloc.h>
14
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020015const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Alexander Graf2a22d052016-03-04 01:10:02 +010016
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020017/**
18 * struct efi_disk_obj - EFI disk object
19 *
20 * @header: EFI object header
21 * @ops: EFI disk I/O protocol interface
22 * @ifname: interface name for block device
23 * @dev_index: device index of block device
24 * @media: block I/O media information
25 * @dp: device path to the block device
26 * @part: partition
27 * @volume: simple file system protocol of the partition
28 * @offset: offset into disk for simple partition
29 * @desc: internal block device descriptor
30 */
Alexander Graf2a22d052016-03-04 01:10:02 +010031struct efi_disk_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020032 struct efi_object header;
Alexander Graf2a22d052016-03-04 01:10:02 +010033 struct efi_block_io ops;
Alexander Graf2a22d052016-03-04 01:10:02 +010034 const char *ifname;
Alexander Graf2a22d052016-03-04 01:10:02 +010035 int dev_index;
Alexander Graf2a22d052016-03-04 01:10:02 +010036 struct efi_block_io_media media;
Rob Clark884bcf62017-09-13 18:05:31 -040037 struct efi_device_path *dp;
Rob Clark884bcf62017-09-13 18:05:31 -040038 unsigned int part;
Rob Clark2a920802017-09-13 18:05:34 -040039 struct efi_simple_file_system_protocol *volume;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020040 lbaint_t offset;
Rob Clark884bcf62017-09-13 18:05:31 -040041 struct blk_desc *desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010042};
43
Alexander Graf2a22d052016-03-04 01:10:02 +010044static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
45 char extended_verification)
46{
47 EFI_ENTRY("%p, %x", this, extended_verification);
48 return EFI_EXIT(EFI_DEVICE_ERROR);
49}
50
51enum efi_disk_direction {
52 EFI_DISK_READ,
53 EFI_DISK_WRITE,
54};
55
Heinrich Schuchardta80a2322017-08-26 22:33:13 +020056static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Graf2a22d052016-03-04 01:10:02 +010057 u32 media_id, u64 lba, unsigned long buffer_size,
58 void *buffer, enum efi_disk_direction direction)
59{
60 struct efi_disk_obj *diskobj;
61 struct blk_desc *desc;
62 int blksz;
63 int blocks;
64 unsigned long n;
65
Alexander Graf2a22d052016-03-04 01:10:02 +010066 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graff9d334b2016-08-05 14:49:53 +020067 desc = (struct blk_desc *) diskobj->desc;
Alexander Graf2a22d052016-03-04 01:10:02 +010068 blksz = desc->blksz;
69 blocks = buffer_size / blksz;
Alexander Graf8c3df0b2016-04-11 16:16:18 +020070 lba += diskobj->offset;
Alexander Graf2a22d052016-03-04 01:10:02 +010071
Masahiro Yamadadee37fc2018-08-06 20:47:40 +090072 debug("EFI: %s:%d blocks=%x lba=%llx blksz=%x dir=%d\n", __func__,
Alexander Grafedcef3b2016-06-02 11:38:27 +020073 __LINE__, blocks, lba, blksz, direction);
Alexander Graf2a22d052016-03-04 01:10:02 +010074
75 /* We only support full block access */
76 if (buffer_size & (blksz - 1))
Rob Clark33049902017-07-25 20:28:29 -040077 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010078
79 if (direction == EFI_DISK_READ)
Simon Glass487d7562016-05-14 14:03:05 -060080 n = blk_dread(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010081 else
Simon Glass487d7562016-05-14 14:03:05 -060082 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Graf2a22d052016-03-04 01:10:02 +010083
84 /* We don't do interrupts, so check for timers cooperatively */
85 efi_timer_check();
86
Alexander Grafedcef3b2016-06-02 11:38:27 +020087 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
88
Alexander Graf2a22d052016-03-04 01:10:02 +010089 if (n != blocks)
Rob Clark33049902017-07-25 20:28:29 -040090 return EFI_DEVICE_ERROR;
Alexander Graf2a22d052016-03-04 01:10:02 +010091
Rob Clark33049902017-07-25 20:28:29 -040092 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +010093}
94
Simon Glasse2754582016-09-25 15:27:32 -060095static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +010096 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +010097 void *buffer)
98{
Alexander Graf51735ae2016-05-11 18:25:48 +020099 void *real_buffer = buffer;
100 efi_status_t r;
101
102#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
103 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
104 r = efi_disk_read_blocks(this, media_id, lba,
105 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
106 if (r != EFI_SUCCESS)
107 return r;
108 return efi_disk_read_blocks(this, media_id, lba +
109 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
110 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
111 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
112 }
113
114 real_buffer = efi_bounce_buffer;
115#endif
116
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900117 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200118 buffer_size, buffer);
119
120 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
121 EFI_DISK_READ);
122
123 /* Copy from bounce buffer to real buffer if necessary */
124 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
125 memcpy(buffer, real_buffer, buffer_size);
126
127 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100128}
129
Simon Glasse2754582016-09-25 15:27:32 -0600130static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt4f948652018-01-19 20:24:48 +0100131 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Graf2a22d052016-03-04 01:10:02 +0100132 void *buffer)
133{
Alexander Graf51735ae2016-05-11 18:25:48 +0200134 void *real_buffer = buffer;
135 efi_status_t r;
136
137#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
138 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
139 r = efi_disk_write_blocks(this, media_id, lba,
140 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
141 if (r != EFI_SUCCESS)
142 return r;
143 return efi_disk_write_blocks(this, media_id, lba +
144 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
145 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
146 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
147 }
148
149 real_buffer = efi_bounce_buffer;
150#endif
151
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900152 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf51735ae2016-05-11 18:25:48 +0200153 buffer_size, buffer);
154
155 /* Populate bounce buffer if necessary */
156 if (real_buffer != buffer)
157 memcpy(real_buffer, buffer, buffer_size);
158
159 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
160 EFI_DISK_WRITE);
161
162 return EFI_EXIT(r);
Alexander Graf2a22d052016-03-04 01:10:02 +0100163}
164
165static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
166{
167 /* We always write synchronously */
168 EFI_ENTRY("%p", this);
169 return EFI_EXIT(EFI_SUCCESS);
170}
171
172static const struct efi_block_io block_io_disk_template = {
173 .reset = &efi_disk_reset,
174 .read_blocks = &efi_disk_read_blocks,
175 .write_blocks = &efi_disk_write_blocks,
176 .flush_blocks = &efi_disk_flush_blocks,
177};
178
Rob Clark2a920802017-09-13 18:05:34 -0400179/*
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100180 * Get the simple file system protocol for a file device path.
181 *
182 * The full path provided is split into device part and into a file
183 * part. The device part is used to find the handle on which the
184 * simple file system protocol is installed.
185 *
186 * @full_path device path including device and file
187 * @return simple file system protocol
Rob Clark2a920802017-09-13 18:05:34 -0400188 */
189struct efi_simple_file_system_protocol *
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100190efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark2a920802017-09-13 18:05:34 -0400191{
192 struct efi_object *efiobj;
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100193 struct efi_handler *handler;
194 struct efi_device_path *device_path;
195 struct efi_device_path *file_path;
196 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400197
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100198 /* Split the path into a device part and a file part */
199 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
200 if (ret != EFI_SUCCESS)
201 return NULL;
202 efi_free_pool(file_path);
203
204 /* Get the EFI object for the partition */
205 efiobj = efi_dp_find_obj(device_path, NULL);
206 efi_free_pool(device_path);
Rob Clark2a920802017-09-13 18:05:34 -0400207 if (!efiobj)
208 return NULL;
209
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100210 /* Find the simple file system protocol */
211 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
212 &handler);
213 if (ret != EFI_SUCCESS)
214 return NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400215
Heinrich Schuchardt110d80a2018-01-19 20:24:39 +0100216 /* Return the simple file system protocol for the partition */
217 return handler->protocol_interface;
Rob Clark2a920802017-09-13 18:05:34 -0400218}
219
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200220/*
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100221 * Create a handle for a partition or disk
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200222 *
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100223 * @parent parent handle
224 * @dp_parent parent device path
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200225 * @if_typename interface name for block device
226 * @desc internal block device
227 * @dev_index device index for block device
228 * @offset offset into disk for simple partitions
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100229 * @return disk object
Heinrich Schuchardt93945f22017-10-26 19:25:46 +0200230 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100231static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100232 efi_handle_t parent,
233 struct efi_device_path *dp_parent,
234 const char *if_typename,
235 struct blk_desc *desc,
236 int dev_index,
237 lbaint_t offset,
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100238 unsigned int part,
239 struct efi_disk_obj **disk)
Alexander Graf4a12a972016-04-11 16:16:17 +0200240{
241 struct efi_disk_obj *diskobj;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100242 efi_status_t ret;
Alexander Graf4a12a972016-04-11 16:16:17 +0200243
Alexander Graf0812d1a2016-08-05 14:51:47 +0200244 /* Don't add empty devices */
245 if (!desc->lba)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100246 return EFI_NOT_READY;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200247
Rob Clark884bcf62017-09-13 18:05:31 -0400248 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100249 if (!diskobj)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100250 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100251
252 /* Hook up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200253 efi_add_handle(&diskobj->header);
Alexander Graf4a12a972016-04-11 16:16:17 +0200254
255 /* Fill in object data */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100256 if (part) {
257 struct efi_device_path *node = efi_dp_part_node(desc, part);
258
259 diskobj->dp = efi_dp_append_node(dp_parent, node);
260 efi_free_pool(node);
261 } else {
262 diskobj->dp = efi_dp_from_part(desc, part);
263 }
Rob Clark884bcf62017-09-13 18:05:31 -0400264 diskobj->part = part;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200265 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100266 &diskobj->ops);
267 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100268 return ret;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200269 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100270 diskobj->dp);
271 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100272 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400273 if (part >= 1) {
274 diskobj->volume = efi_simple_file_system(desc, part,
275 diskobj->dp);
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200276 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100277 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardt22de1de2018-01-19 20:24:38 +0100278 diskobj->volume);
Heinrich Schuchardt4b9f7aa2017-11-26 14:05:12 +0100279 if (ret != EFI_SUCCESS)
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100280 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400281 }
Alexander Graf4a12a972016-04-11 16:16:17 +0200282 diskobj->ops = block_io_disk_template;
Simon Glass487d7562016-05-14 14:03:05 -0600283 diskobj->ifname = if_typename;
Alexander Graf4a12a972016-04-11 16:16:17 +0200284 diskobj->dev_index = dev_index;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200285 diskobj->offset = offset;
Alexander Graff9d334b2016-08-05 14:49:53 +0200286 diskobj->desc = desc;
Alexander Graf4a12a972016-04-11 16:16:17 +0200287
288 /* Fill in EFI IO Media info (for read/write callbacks) */
289 diskobj->media.removable_media = desc->removable;
290 diskobj->media.media_present = 1;
291 diskobj->media.block_size = desc->blksz;
292 diskobj->media.io_align = desc->blksz;
Alexander Graf0812d1a2016-08-05 14:51:47 +0200293 diskobj->media.last_block = desc->lba - offset;
Emmanuel Vadot5f770832017-12-11 19:22:33 +0100294 if (part != 0)
295 diskobj->media.logical_partition = 1;
Alexander Graf4a12a972016-04-11 16:16:17 +0200296 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100297 if (disk)
298 *disk = diskobj;
299 return EFI_SUCCESS;
Alexander Graf4a12a972016-04-11 16:16:17 +0200300}
301
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100302/*
303 * Create handles and protocols for the partitions of a block device
304 *
305 * @parent handle of the parent disk
306 * @blk_desc block device
307 * @if_typename interface type
308 * @diskid device number
309 * @pdevname device name
310 * @return number of partitions created
311 */
312int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
313 const char *if_typename, int diskid,
314 const char *pdevname)
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200315{
316 int disks = 0;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200317 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200318 disk_partition_t info;
Jonathan Gray16a73b22017-10-10 13:55:26 +1100319 int part;
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100320 struct efi_device_path *dp = NULL;
321 efi_status_t ret;
322 struct efi_handler *handler;
323
324 /* Get the device path of the parent */
325 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
326 if (ret == EFI_SUCCESS)
327 dp = handler->protocol_interface;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200328
Alexander Grafc034b7f2017-12-01 16:10:33 +0100329 /* Add devices for each partition */
Jonathan Gray16a73b22017-10-10 13:55:26 +1100330 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
331 if (part_get_info(desc, part, &info))
332 continue;
Alexander Graff9d334b2016-08-05 14:49:53 +0200333 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
334 part);
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100335 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
336 info.start, part, NULL);
337 if (ret != EFI_SUCCESS) {
338 printf("Adding partition %s failed\n", pdevname);
339 continue;
340 }
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200341 disks++;
342 }
Rob Clark884bcf62017-09-13 18:05:31 -0400343
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200344 return disks;
345}
346
Alexander Graf2a22d052016-03-04 01:10:02 +0100347/*
348 * U-Boot doesn't have a list of all online disk devices. So when running our
349 * EFI payload, we scan through all of the potentially available ones and
350 * store them in our object pool.
351 *
Simon Glass487d7562016-05-14 14:03:05 -0600352 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
353 * Consider converting the code to look up devices as needed. The EFI device
354 * could be a child of the UCLASS_BLK block device, perhaps.
355 *
Alexander Graf2a22d052016-03-04 01:10:02 +0100356 * This gets called from do_bootefi_exec().
357 */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100358efi_status_t efi_disk_register(void)
Alexander Graf2a22d052016-03-04 01:10:02 +0100359{
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100360 struct efi_disk_obj *disk;
Alexander Graf2a22d052016-03-04 01:10:02 +0100361 int disks = 0;
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100362 efi_status_t ret;
Simon Glass487d7562016-05-14 14:03:05 -0600363#ifdef CONFIG_BLK
364 struct udevice *dev;
365
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100366 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de70bfcdc2017-06-20 19:10:27 +0000367 uclass_next_device_check(&dev)) {
Simon Glass487d7562016-05-14 14:03:05 -0600368 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt9bfca9f2018-01-19 20:24:44 +0100369 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass487d7562016-05-14 14:03:05 -0600370
Alexander Grafc034b7f2017-12-01 16:10:33 +0100371 /* Add block device for the full device */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100372 printf("Scanning disk %s...\n", dev->name);
373 ret = efi_disk_add_dev(NULL, NULL, if_typename,
374 desc, desc->devnum, 0, 0, &disk);
375 if (ret == EFI_NOT_READY) {
376 printf("Disk %s not ready\n", dev->name);
377 continue;
378 }
379 if (ret) {
380 printf("ERROR: failure to add disk device %s, r = %lu\n",
381 dev->name, ret & ~EFI_ERROR_MASK);
382 return ret;
383 }
Simon Glass487d7562016-05-14 14:03:05 -0600384 disks++;
385
Alexander Grafc034b7f2017-12-01 16:10:33 +0100386 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100387 disks += efi_disk_create_partitions(
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200388 &disk->header, desc, if_typename,
Heinrich Schuchardt64e4db02018-01-19 20:24:47 +0100389 desc->devnum, dev->name);
Simon Glass487d7562016-05-14 14:03:05 -0600390 }
391#else
392 int i, if_type;
Alexander Graf2a22d052016-03-04 01:10:02 +0100393
394 /* Search for all available disk devices */
Simon Glass6dd9faf2016-05-01 13:52:32 -0600395 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass487d7562016-05-14 14:03:05 -0600396 const struct blk_driver *cur_drvr;
397 const char *if_typename;
398
Simon Glass6dd9faf2016-05-01 13:52:32 -0600399 cur_drvr = blk_driver_lookup_type(if_type);
400 if (!cur_drvr)
401 continue;
402
Simon Glass487d7562016-05-14 14:03:05 -0600403 if_typename = cur_drvr->if_typename;
404 printf("Scanning disks on %s...\n", if_typename);
Alexander Graf2a22d052016-03-04 01:10:02 +0100405 for (i = 0; i < 4; i++) {
406 struct blk_desc *desc;
Alexander Grafecbe1a02016-04-11 16:16:20 +0200407 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Graf2a22d052016-03-04 01:10:02 +0100408
Simon Glass6dd9faf2016-05-01 13:52:32 -0600409 desc = blk_get_devnum_by_type(if_type, i);
Alexander Graf2a22d052016-03-04 01:10:02 +0100410 if (!desc)
411 continue;
412 if (desc->type == DEV_TYPE_UNKNOWN)
413 continue;
414
Alexander Graf2a22d052016-03-04 01:10:02 +0100415 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass487d7562016-05-14 14:03:05 -0600416 if_typename, i);
Rob Clark77511b32017-10-08 11:33:08 -0400417
Alexander Grafc034b7f2017-12-01 16:10:33 +0100418 /* Add block device for the full device */
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100419 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
420 i, 0, 0, &disk);
421 if (ret == EFI_NOT_READY) {
422 printf("Disk %s not ready\n", devname);
423 continue;
424 }
425 if (ret) {
426 printf("ERROR: failure to add disk device %s, r = %lu\n",
427 devname, ret & ~EFI_ERROR_MASK);
428 return ret;
429 }
Alexander Graf2a22d052016-03-04 01:10:02 +0100430 disks++;
Alexander Graf8c3df0b2016-04-11 16:16:18 +0200431
Alexander Grafc034b7f2017-12-01 16:10:33 +0100432 /* Partitions show up as block devices in EFI */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200433 disks += efi_disk_create_partitions
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200434 (&disk->header, desc,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200435 if_typename, i, devname);
Alexander Graf2a22d052016-03-04 01:10:02 +0100436 }
437 }
Simon Glass487d7562016-05-14 14:03:05 -0600438#endif
Alexander Graf2a22d052016-03-04 01:10:02 +0100439 printf("Found %d disks\n", disks);
440
Heinrich Schuchardtdf9cf562018-02-09 20:55:47 +0100441 return EFI_SUCCESS;
Alexander Graf2a22d052016-03-04 01:10:02 +0100442}