blob: 0bf436ac666e2c5ccc928eaddafe6751c15d5659 [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>
Alexander Grafe83be452016-03-04 01:10:02 +010012#include <part.h>
13#include <malloc.h>
14
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020015const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
Alexander Grafe83be452016-03-04 01:10:02 +010016
Heinrich Schuchardt72928722018-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 Grafe83be452016-03-04 01:10:02 +010031struct efi_disk_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020032 struct efi_object header;
Alexander Grafe83be452016-03-04 01:10:02 +010033 struct efi_block_io ops;
Alexander Grafe83be452016-03-04 01:10:02 +010034 const char *ifname;
Alexander Grafe83be452016-03-04 01:10:02 +010035 int dev_index;
Alexander Grafe83be452016-03-04 01:10:02 +010036 struct efi_block_io_media media;
Rob Clarka83272f2017-09-13 18:05:31 -040037 struct efi_device_path *dp;
Rob Clarka83272f2017-09-13 18:05:31 -040038 unsigned int part;
Rob Clark53142032017-09-13 18:05:34 -040039 struct efi_simple_file_system_protocol *volume;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020040 lbaint_t offset;
Rob Clarka83272f2017-09-13 18:05:31 -040041 struct blk_desc *desc;
Alexander Grafe83be452016-03-04 01:10:02 +010042};
43
Alexander Grafe83be452016-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 Schuchardt9ce06172017-08-26 22:33:13 +020056static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
Alexander Grafe83be452016-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 Grafe83be452016-03-04 01:10:02 +010066 diskobj = container_of(this, struct efi_disk_obj, ops);
Alexander Graf34118e72016-08-05 14:49:53 +020067 desc = (struct blk_desc *) diskobj->desc;
Alexander Grafe83be452016-03-04 01:10:02 +010068 blksz = desc->blksz;
69 blocks = buffer_size / blksz;
Alexander Grafeb8d82c2016-04-11 16:16:18 +020070 lba += diskobj->offset;
Alexander Grafe83be452016-03-04 01:10:02 +010071
Masahiro Yamadac7570a32018-08-06 20:47:40 +090072 debug("EFI: %s:%d blocks=%x lba=%llx blksz=%x dir=%d\n", __func__,
Alexander Graf62a67482016-06-02 11:38:27 +020073 __LINE__, blocks, lba, blksz, direction);
Alexander Grafe83be452016-03-04 01:10:02 +010074
75 /* We only support full block access */
76 if (buffer_size & (blksz - 1))
Heinrich Schuchardt35922522019-09-05 19:31:23 +020077 return EFI_BAD_BUFFER_SIZE;
Alexander Grafe83be452016-03-04 01:10:02 +010078
79 if (direction == EFI_DISK_READ)
Simon Glass302d4f82016-05-14 14:03:05 -060080 n = blk_dread(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010081 else
Simon Glass302d4f82016-05-14 14:03:05 -060082 n = blk_dwrite(desc, lba, blocks, buffer);
Alexander Grafe83be452016-03-04 01:10:02 +010083
84 /* We don't do interrupts, so check for timers cooperatively */
85 efi_timer_check();
86
Alexander Graf62a67482016-06-02 11:38:27 +020087 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
88
Alexander Grafe83be452016-03-04 01:10:02 +010089 if (n != blocks)
Rob Clark99c43872017-07-25 20:28:29 -040090 return EFI_DEVICE_ERROR;
Alexander Grafe83be452016-03-04 01:10:02 +010091
Rob Clark99c43872017-07-25 20:28:29 -040092 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +010093}
94
Simon Glassc2194bf2016-09-25 15:27:32 -060095static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +010096 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +010097 void *buffer)
98{
Alexander Graf7c00a3c2016-05-11 18:25:48 +020099 void *real_buffer = buffer;
100 efi_status_t r;
101
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200102 if (!this)
103 return EFI_INVALID_PARAMETER;
104 /* TODO: check for media changes */
105 if (media_id != this->media->media_id)
106 return EFI_MEDIA_CHANGED;
107 if (!this->media->media_present)
108 return EFI_NO_MEDIA;
109 /* media->io_align is a power of 2 */
110 if ((uintptr_t)buffer & (this->media->io_align - 1))
111 return EFI_INVALID_PARAMETER;
112 if (lba * this->media->block_size + buffer_size >
113 this->media->last_block * this->media->block_size)
114 return EFI_INVALID_PARAMETER;
115
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200116#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
117 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
118 r = efi_disk_read_blocks(this, media_id, lba,
119 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
120 if (r != EFI_SUCCESS)
121 return r;
122 return efi_disk_read_blocks(this, media_id, lba +
123 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
124 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
125 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
126 }
127
128 real_buffer = efi_bounce_buffer;
129#endif
130
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900131 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200132 buffer_size, buffer);
133
134 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
135 EFI_DISK_READ);
136
137 /* Copy from bounce buffer to real buffer if necessary */
138 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
139 memcpy(buffer, real_buffer, buffer_size);
140
141 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100142}
143
Simon Glassc2194bf2016-09-25 15:27:32 -0600144static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
Heinrich Schuchardt0844f792018-01-19 20:24:48 +0100145 u32 media_id, u64 lba, efi_uintn_t buffer_size,
Alexander Grafe83be452016-03-04 01:10:02 +0100146 void *buffer)
147{
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200148 void *real_buffer = buffer;
149 efi_status_t r;
150
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200151 if (!this)
152 return EFI_INVALID_PARAMETER;
153 if (this->media->read_only)
154 return EFI_WRITE_PROTECTED;
155 /* TODO: check for media changes */
156 if (media_id != this->media->media_id)
157 return EFI_MEDIA_CHANGED;
158 if (!this->media->media_present)
159 return EFI_NO_MEDIA;
160 /* media->io_align is a power of 2 */
161 if ((uintptr_t)buffer & (this->media->io_align - 1))
162 return EFI_INVALID_PARAMETER;
163 if (lba * this->media->block_size + buffer_size >
164 this->media->last_block * this->media->block_size)
165 return EFI_INVALID_PARAMETER;
166
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200167#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
168 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
169 r = efi_disk_write_blocks(this, media_id, lba,
170 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
171 if (r != EFI_SUCCESS)
172 return r;
173 return efi_disk_write_blocks(this, media_id, lba +
174 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
175 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
176 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
177 }
178
179 real_buffer = efi_bounce_buffer;
180#endif
181
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900182 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200183 buffer_size, buffer);
184
185 /* Populate bounce buffer if necessary */
186 if (real_buffer != buffer)
187 memcpy(real_buffer, buffer, buffer_size);
188
189 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
190 EFI_DISK_WRITE);
191
192 return EFI_EXIT(r);
Alexander Grafe83be452016-03-04 01:10:02 +0100193}
194
195static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
196{
197 /* We always write synchronously */
198 EFI_ENTRY("%p", this);
199 return EFI_EXIT(EFI_SUCCESS);
200}
201
202static const struct efi_block_io block_io_disk_template = {
203 .reset = &efi_disk_reset,
204 .read_blocks = &efi_disk_read_blocks,
205 .write_blocks = &efi_disk_write_blocks,
206 .flush_blocks = &efi_disk_flush_blocks,
207};
208
Rob Clark53142032017-09-13 18:05:34 -0400209/*
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100210 * Get the simple file system protocol for a file device path.
211 *
212 * The full path provided is split into device part and into a file
213 * part. The device part is used to find the handle on which the
214 * simple file system protocol is installed.
215 *
216 * @full_path device path including device and file
217 * @return simple file system protocol
Rob Clark53142032017-09-13 18:05:34 -0400218 */
219struct efi_simple_file_system_protocol *
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100220efi_fs_from_path(struct efi_device_path *full_path)
Rob Clark53142032017-09-13 18:05:34 -0400221{
222 struct efi_object *efiobj;
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100223 struct efi_handler *handler;
224 struct efi_device_path *device_path;
225 struct efi_device_path *file_path;
226 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400227
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100228 /* Split the path into a device part and a file part */
229 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
230 if (ret != EFI_SUCCESS)
231 return NULL;
232 efi_free_pool(file_path);
233
234 /* Get the EFI object for the partition */
235 efiobj = efi_dp_find_obj(device_path, NULL);
236 efi_free_pool(device_path);
Rob Clark53142032017-09-13 18:05:34 -0400237 if (!efiobj)
238 return NULL;
239
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100240 /* Find the simple file system protocol */
241 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
242 &handler);
243 if (ret != EFI_SUCCESS)
244 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400245
Heinrich Schuchardt39578762018-01-19 20:24:39 +0100246 /* Return the simple file system protocol for the partition */
247 return handler->protocol_interface;
Rob Clark53142032017-09-13 18:05:34 -0400248}
249
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200250/*
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100251 * Create a handle for a partition or disk
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200252 *
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100253 * @parent parent handle
254 * @dp_parent parent device path
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200255 * @if_typename interface name for block device
256 * @desc internal block device
257 * @dev_index device index for block device
258 * @offset offset into disk for simple partitions
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100259 * @return disk object
Heinrich Schuchardt34f51832017-10-26 19:25:46 +0200260 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100261static efi_status_t efi_disk_add_dev(
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100262 efi_handle_t parent,
263 struct efi_device_path *dp_parent,
264 const char *if_typename,
265 struct blk_desc *desc,
266 int dev_index,
267 lbaint_t offset,
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100268 unsigned int part,
269 struct efi_disk_obj **disk)
Alexander Graf06fe57b2016-04-11 16:16:17 +0200270{
271 struct efi_disk_obj *diskobj;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100272 efi_status_t ret;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200273
Alexander Graf601db1f2016-08-05 14:51:47 +0200274 /* Don't add empty devices */
275 if (!desc->lba)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100276 return EFI_NOT_READY;
Alexander Graf601db1f2016-08-05 14:51:47 +0200277
Rob Clarka83272f2017-09-13 18:05:31 -0400278 diskobj = calloc(1, sizeof(*diskobj));
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100279 if (!diskobj)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100280 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100281
282 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200283 efi_add_handle(&diskobj->header);
Alexander Graf06fe57b2016-04-11 16:16:17 +0200284
285 /* Fill in object data */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100286 if (part) {
287 struct efi_device_path *node = efi_dp_part_node(desc, part);
288
289 diskobj->dp = efi_dp_append_node(dp_parent, node);
290 efi_free_pool(node);
291 } else {
292 diskobj->dp = efi_dp_from_part(desc, part);
293 }
Rob Clarka83272f2017-09-13 18:05:31 -0400294 diskobj->part = part;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200295 ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100296 &diskobj->ops);
297 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100298 return ret;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200299 ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100300 diskobj->dp);
301 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100302 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400303 if (part >= 1) {
304 diskobj->volume = efi_simple_file_system(desc, part,
305 diskobj->dp);
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200306 ret = efi_add_protocol(&diskobj->header,
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100307 &efi_simple_file_system_protocol_guid,
Heinrich Schuchardtce721a82018-01-19 20:24:38 +0100308 diskobj->volume);
Heinrich Schuchardt9fb0f2f2017-11-26 14:05:12 +0100309 if (ret != EFI_SUCCESS)
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100310 return ret;
Rob Clark53142032017-09-13 18:05:34 -0400311 }
Alexander Graf06fe57b2016-04-11 16:16:17 +0200312 diskobj->ops = block_io_disk_template;
Simon Glass302d4f82016-05-14 14:03:05 -0600313 diskobj->ifname = if_typename;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200314 diskobj->dev_index = dev_index;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200315 diskobj->offset = offset;
Alexander Graf34118e72016-08-05 14:49:53 +0200316 diskobj->desc = desc;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200317
318 /* Fill in EFI IO Media info (for read/write callbacks) */
319 diskobj->media.removable_media = desc->removable;
320 diskobj->media.media_present = 1;
Heinrich Schuchardt35922522019-09-05 19:31:23 +0200321 /*
322 * MediaID is just an arbitrary counter.
323 * We have to change it if the medium is removed or changed.
324 */
325 diskobj->media.media_id = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200326 diskobj->media.block_size = desc->blksz;
327 diskobj->media.io_align = desc->blksz;
Alexander Graf601db1f2016-08-05 14:51:47 +0200328 diskobj->media.last_block = desc->lba - offset;
Emmanuel Vadot5bf29642017-12-11 19:22:33 +0100329 if (part != 0)
330 diskobj->media.logical_partition = 1;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200331 diskobj->ops.media = &diskobj->media;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100332 if (disk)
333 *disk = diskobj;
334 return EFI_SUCCESS;
Alexander Graf06fe57b2016-04-11 16:16:17 +0200335}
336
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100337/*
338 * Create handles and protocols for the partitions of a block device
339 *
340 * @parent handle of the parent disk
341 * @blk_desc block device
342 * @if_typename interface type
343 * @diskid device number
344 * @pdevname device name
345 * @return number of partitions created
346 */
347int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
348 const char *if_typename, int diskid,
349 const char *pdevname)
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200350{
351 int disks = 0;
Alexander Graf3491ee72016-04-11 16:16:20 +0200352 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200353 disk_partition_t info;
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100354 int part;
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100355 struct efi_device_path *dp = NULL;
356 efi_status_t ret;
357 struct efi_handler *handler;
358
359 /* Get the device path of the parent */
360 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
361 if (ret == EFI_SUCCESS)
362 dp = handler->protocol_interface;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200363
Alexander Graf83a83f22017-12-01 16:10:33 +0100364 /* Add devices for each partition */
Jonathan Grayd35cbd52017-10-10 13:55:26 +1100365 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
366 if (part_get_info(desc, part, &info))
367 continue;
Alexander Graf34118e72016-08-05 14:49:53 +0200368 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
369 part);
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100370 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
371 info.start, part, NULL);
372 if (ret != EFI_SUCCESS) {
373 printf("Adding partition %s failed\n", pdevname);
374 continue;
375 }
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200376 disks++;
377 }
Rob Clarka83272f2017-09-13 18:05:31 -0400378
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200379 return disks;
380}
381
Alexander Grafe83be452016-03-04 01:10:02 +0100382/*
383 * U-Boot doesn't have a list of all online disk devices. So when running our
384 * EFI payload, we scan through all of the potentially available ones and
385 * store them in our object pool.
386 *
Simon Glass302d4f82016-05-14 14:03:05 -0600387 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
388 * Consider converting the code to look up devices as needed. The EFI device
389 * could be a child of the UCLASS_BLK block device, perhaps.
390 *
Alexander Grafe83be452016-03-04 01:10:02 +0100391 * This gets called from do_bootefi_exec().
392 */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100393efi_status_t efi_disk_register(void)
Alexander Grafe83be452016-03-04 01:10:02 +0100394{
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100395 struct efi_disk_obj *disk;
Alexander Grafe83be452016-03-04 01:10:02 +0100396 int disks = 0;
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100397 efi_status_t ret;
Simon Glass302d4f82016-05-14 14:03:05 -0600398#ifdef CONFIG_BLK
399 struct udevice *dev;
400
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100401 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
xypron.glpk@gmx.de308cd8e2017-06-20 19:10:27 +0000402 uclass_next_device_check(&dev)) {
Simon Glass302d4f82016-05-14 14:03:05 -0600403 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Heinrich Schuchardt6a18bb32018-01-19 20:24:44 +0100404 const char *if_typename = blk_get_if_type_name(desc->if_type);
Simon Glass302d4f82016-05-14 14:03:05 -0600405
Alexander Graf83a83f22017-12-01 16:10:33 +0100406 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100407 printf("Scanning disk %s...\n", dev->name);
408 ret = efi_disk_add_dev(NULL, NULL, if_typename,
409 desc, desc->devnum, 0, 0, &disk);
410 if (ret == EFI_NOT_READY) {
411 printf("Disk %s not ready\n", dev->name);
412 continue;
413 }
414 if (ret) {
415 printf("ERROR: failure to add disk device %s, r = %lu\n",
416 dev->name, ret & ~EFI_ERROR_MASK);
417 return ret;
418 }
Simon Glass302d4f82016-05-14 14:03:05 -0600419 disks++;
420
Alexander Graf83a83f22017-12-01 16:10:33 +0100421 /* Partitions show up as block devices in EFI */
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100422 disks += efi_disk_create_partitions(
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200423 &disk->header, desc, if_typename,
Heinrich Schuchardt6f3de6b2018-01-19 20:24:47 +0100424 desc->devnum, dev->name);
Simon Glass302d4f82016-05-14 14:03:05 -0600425 }
426#else
427 int i, if_type;
Alexander Grafe83be452016-03-04 01:10:02 +0100428
429 /* Search for all available disk devices */
Simon Glasse3dfa912016-05-01 13:52:32 -0600430 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
Simon Glass302d4f82016-05-14 14:03:05 -0600431 const struct blk_driver *cur_drvr;
432 const char *if_typename;
433
Simon Glasse3dfa912016-05-01 13:52:32 -0600434 cur_drvr = blk_driver_lookup_type(if_type);
435 if (!cur_drvr)
436 continue;
437
Simon Glass302d4f82016-05-14 14:03:05 -0600438 if_typename = cur_drvr->if_typename;
439 printf("Scanning disks on %s...\n", if_typename);
Alexander Grafe83be452016-03-04 01:10:02 +0100440 for (i = 0; i < 4; i++) {
441 struct blk_desc *desc;
Alexander Graf3491ee72016-04-11 16:16:20 +0200442 char devname[32] = { 0 }; /* dp->str is u16[32] long */
Alexander Grafe83be452016-03-04 01:10:02 +0100443
Simon Glasse3dfa912016-05-01 13:52:32 -0600444 desc = blk_get_devnum_by_type(if_type, i);
Alexander Grafe83be452016-03-04 01:10:02 +0100445 if (!desc)
446 continue;
447 if (desc->type == DEV_TYPE_UNKNOWN)
448 continue;
449
Alexander Grafe83be452016-03-04 01:10:02 +0100450 snprintf(devname, sizeof(devname), "%s%d",
Simon Glass302d4f82016-05-14 14:03:05 -0600451 if_typename, i);
Rob Clark1b2ccbb2017-10-08 11:33:08 -0400452
Alexander Graf83a83f22017-12-01 16:10:33 +0100453 /* Add block device for the full device */
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100454 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
455 i, 0, 0, &disk);
456 if (ret == EFI_NOT_READY) {
457 printf("Disk %s not ready\n", devname);
458 continue;
459 }
460 if (ret) {
461 printf("ERROR: failure to add disk device %s, r = %lu\n",
462 devname, ret & ~EFI_ERROR_MASK);
463 return ret;
464 }
Alexander Grafe83be452016-03-04 01:10:02 +0100465 disks++;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200466
Alexander Graf83a83f22017-12-01 16:10:33 +0100467 /* Partitions show up as block devices in EFI */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200468 disks += efi_disk_create_partitions
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200469 (&disk->header, desc,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200470 if_typename, i, devname);
Alexander Grafe83be452016-03-04 01:10:02 +0100471 }
472 }
Simon Glass302d4f82016-05-14 14:03:05 -0600473#endif
Alexander Grafe83be452016-03-04 01:10:02 +0100474 printf("Found %d disks\n", disks);
475
Heinrich Schuchardtcde7a362018-02-09 20:55:47 +0100476 return EFI_SUCCESS;
Alexander Grafe83be452016-03-04 01:10:02 +0100477}