xref: /openbmc/u-boot/lib/efi_loader/efi_disk.c (revision 286a5c44)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application disk support
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7 
8 #include <common.h>
9 #include <blk.h>
10 #include <dm.h>
11 #include <efi_loader.h>
12 #include <part.h>
13 #include <malloc.h>
14 
15 const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
16 
17 /**
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  */
31 struct efi_disk_obj {
32 	struct efi_object header;
33 	struct efi_block_io ops;
34 	const char *ifname;
35 	int dev_index;
36 	struct efi_block_io_media media;
37 	struct efi_device_path *dp;
38 	unsigned int part;
39 	struct efi_simple_file_system_protocol *volume;
40 	lbaint_t offset;
41 	struct blk_desc *desc;
42 };
43 
44 static 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 
51 enum efi_disk_direction {
52 	EFI_DISK_READ,
53 	EFI_DISK_WRITE,
54 };
55 
56 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
57 			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 
66 	diskobj = container_of(this, struct efi_disk_obj, ops);
67 	desc = (struct blk_desc *) diskobj->desc;
68 	blksz = desc->blksz;
69 	blocks = buffer_size / blksz;
70 	lba += diskobj->offset;
71 
72 	debug("EFI: %s:%d blocks=%x lba=%llx blksz=%x dir=%d\n", __func__,
73 	      __LINE__, blocks, lba, blksz, direction);
74 
75 	/* We only support full block access */
76 	if (buffer_size & (blksz - 1))
77 		return EFI_DEVICE_ERROR;
78 
79 	if (direction == EFI_DISK_READ)
80 		n = blk_dread(desc, lba, blocks, buffer);
81 	else
82 		n = blk_dwrite(desc, lba, blocks, buffer);
83 
84 	/* We don't do interrupts, so check for timers cooperatively */
85 	efi_timer_check();
86 
87 	debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
88 
89 	if (n != blocks)
90 		return EFI_DEVICE_ERROR;
91 
92 	return EFI_SUCCESS;
93 }
94 
95 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
96 			u32 media_id, u64 lba, efi_uintn_t buffer_size,
97 			void *buffer)
98 {
99 	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 
117 	EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
118 		  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);
128 }
129 
130 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
131 			u32 media_id, u64 lba, efi_uintn_t buffer_size,
132 			void *buffer)
133 {
134 	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 
152 	EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
153 		  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);
163 }
164 
165 static 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 
172 static 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 
179 /*
180  * 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
188  */
189 struct efi_simple_file_system_protocol *
190 efi_fs_from_path(struct efi_device_path *full_path)
191 {
192 	struct efi_object *efiobj;
193 	struct efi_handler *handler;
194 	struct efi_device_path *device_path;
195 	struct efi_device_path *file_path;
196 	efi_status_t ret;
197 
198 	/* 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);
207 	if (!efiobj)
208 		return NULL;
209 
210 	/* 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;
215 
216 	/* Return the simple file system protocol for the partition */
217 	return handler->protocol_interface;
218 }
219 
220 /*
221  * Create a handle for a partition or disk
222  *
223  * @parent	parent handle
224  * @dp_parent	parent device path
225  * @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
229  * @return	disk object
230  */
231 static efi_status_t efi_disk_add_dev(
232 				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,
238 				unsigned int part,
239 				struct efi_disk_obj **disk)
240 {
241 	struct efi_disk_obj *diskobj;
242 	efi_status_t ret;
243 
244 	/* Don't add empty devices */
245 	if (!desc->lba)
246 		return EFI_NOT_READY;
247 
248 	diskobj = calloc(1, sizeof(*diskobj));
249 	if (!diskobj)
250 		return EFI_OUT_OF_RESOURCES;
251 
252 	/* Hook up to the device list */
253 	efi_add_handle(&diskobj->header);
254 
255 	/* Fill in object data */
256 	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 	}
264 	diskobj->part = part;
265 	ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid,
266 			       &diskobj->ops);
267 	if (ret != EFI_SUCCESS)
268 		return ret;
269 	ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path,
270 			       diskobj->dp);
271 	if (ret != EFI_SUCCESS)
272 		return ret;
273 	if (part >= 1) {
274 		diskobj->volume = efi_simple_file_system(desc, part,
275 							 diskobj->dp);
276 		ret = efi_add_protocol(&diskobj->header,
277 				       &efi_simple_file_system_protocol_guid,
278 				       diskobj->volume);
279 		if (ret != EFI_SUCCESS)
280 			return ret;
281 	}
282 	diskobj->ops = block_io_disk_template;
283 	diskobj->ifname = if_typename;
284 	diskobj->dev_index = dev_index;
285 	diskobj->offset = offset;
286 	diskobj->desc = desc;
287 
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;
293 	diskobj->media.last_block = desc->lba - offset;
294 	if (part != 0)
295 		diskobj->media.logical_partition = 1;
296 	diskobj->ops.media = &diskobj->media;
297 	if (disk)
298 		*disk = diskobj;
299 	return EFI_SUCCESS;
300 }
301 
302 /*
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  */
312 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
313 			       const char *if_typename, int diskid,
314 			       const char *pdevname)
315 {
316 	int disks = 0;
317 	char devname[32] = { 0 }; /* dp->str is u16[32] long */
318 	disk_partition_t info;
319 	int part;
320 	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;
328 
329 	/* Add devices for each partition */
330 	for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
331 		if (part_get_info(desc, part, &info))
332 			continue;
333 		snprintf(devname, sizeof(devname), "%s:%d", pdevname,
334 			 part);
335 		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 		}
341 		disks++;
342 	}
343 
344 	return disks;
345 }
346 
347 /*
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  *
352  * 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  *
356  * This gets called from do_bootefi_exec().
357  */
358 efi_status_t efi_disk_register(void)
359 {
360 	struct efi_disk_obj *disk;
361 	int disks = 0;
362 	efi_status_t ret;
363 #ifdef CONFIG_BLK
364 	struct udevice *dev;
365 
366 	for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
367 	     uclass_next_device_check(&dev)) {
368 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
369 		const char *if_typename = blk_get_if_type_name(desc->if_type);
370 
371 		/* Add block device for the full device */
372 		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 		}
384 		disks++;
385 
386 		/* Partitions show up as block devices in EFI */
387 		disks += efi_disk_create_partitions(
388 					&disk->header, desc, if_typename,
389 					desc->devnum, dev->name);
390 	}
391 #else
392 	int i, if_type;
393 
394 	/* Search for all available disk devices */
395 	for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
396 		const struct blk_driver *cur_drvr;
397 		const char *if_typename;
398 
399 		cur_drvr = blk_driver_lookup_type(if_type);
400 		if (!cur_drvr)
401 			continue;
402 
403 		if_typename = cur_drvr->if_typename;
404 		printf("Scanning disks on %s...\n", if_typename);
405 		for (i = 0; i < 4; i++) {
406 			struct blk_desc *desc;
407 			char devname[32] = { 0 }; /* dp->str is u16[32] long */
408 
409 			desc = blk_get_devnum_by_type(if_type, i);
410 			if (!desc)
411 				continue;
412 			if (desc->type == DEV_TYPE_UNKNOWN)
413 				continue;
414 
415 			snprintf(devname, sizeof(devname), "%s%d",
416 				 if_typename, i);
417 
418 			/* Add block device for the full device */
419 			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 			}
430 			disks++;
431 
432 			/* Partitions show up as block devices in EFI */
433 			disks += efi_disk_create_partitions
434 						(&disk->header, desc,
435 						 if_typename, i, devname);
436 		}
437 	}
438 #endif
439 	printf("Found %d disks\n", disks);
440 
441 	return EFI_SUCCESS;
442 }
443