xref: /openbmc/u-boot/lib/efi_loader/efi_disk.c (revision c7ba7bdc)
1 /*
2  *  EFI application disk support
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <blk.h>
11 #include <efi_loader.h>
12 #include <inttypes.h>
13 #include <part.h>
14 #include <malloc.h>
15 
16 static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
17 
18 struct 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 */
30 	struct efi_device_path_file_path *dp;
31 	/* Offset into disk for simple partitions */
32 	lbaint_t offset;
33 };
34 
35 static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
36 			void **protocol_interface, void *agent_handle,
37 			void *controller_handle, uint32_t attributes)
38 {
39 	struct efi_disk_obj *diskobj = handle;
40 
41 	*protocol_interface = &diskobj->ops;
42 
43 	return EFI_SUCCESS;
44 }
45 
46 static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
47 			void **protocol_interface, void *agent_handle,
48 			void *controller_handle, uint32_t attributes)
49 {
50 	struct efi_disk_obj *diskobj = handle;
51 
52 	*protocol_interface = diskobj->dp;
53 
54 	return EFI_SUCCESS;
55 }
56 
57 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
58 			char extended_verification)
59 {
60 	EFI_ENTRY("%p, %x", this, extended_verification);
61 	return EFI_EXIT(EFI_DEVICE_ERROR);
62 }
63 
64 enum efi_disk_direction {
65 	EFI_DISK_READ,
66 	EFI_DISK_WRITE,
67 };
68 
69 static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
70 			u32 media_id, u64 lba, unsigned long buffer_size,
71 			void *buffer, enum efi_disk_direction direction)
72 {
73 	struct efi_disk_obj *diskobj;
74 	struct blk_desc *desc;
75 	int blksz;
76 	int blocks;
77 	unsigned long n;
78 
79 	EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
80 		  buffer_size, buffer);
81 
82 	diskobj = container_of(this, struct efi_disk_obj, ops);
83 	if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index)))
84 		return EFI_EXIT(EFI_DEVICE_ERROR);
85 	blksz = desc->blksz;
86 	blocks = buffer_size / blksz;
87 	lba += diskobj->offset;
88 
89 #ifdef DEBUG_EFI
90 	printf("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
91 	       __LINE__, blocks, lba, blksz, direction);
92 #endif
93 
94 	/* We only support full block access */
95 	if (buffer_size & (blksz - 1))
96 		return EFI_EXIT(EFI_DEVICE_ERROR);
97 
98 	if (direction == EFI_DISK_READ)
99 		n = desc->block_read(desc, lba, blocks, buffer);
100 	else
101 		n = desc->block_write(desc, lba, blocks, buffer);
102 
103 	/* We don't do interrupts, so check for timers cooperatively */
104 	efi_timer_check();
105 
106 #ifdef DEBUG_EFI
107 	printf("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
108 #endif
109 	if (n != blocks)
110 		return EFI_EXIT(EFI_DEVICE_ERROR);
111 
112 	return EFI_EXIT(EFI_SUCCESS);
113 }
114 
115 static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
116 			u32 media_id, u64 lba, unsigned long buffer_size,
117 			void *buffer)
118 {
119 	return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
120 				  EFI_DISK_READ);
121 }
122 
123 static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
124 			u32 media_id, u64 lba, unsigned long buffer_size,
125 			void *buffer)
126 {
127 	return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
128 				  EFI_DISK_WRITE);
129 }
130 
131 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
132 {
133 	/* We always write synchronously */
134 	EFI_ENTRY("%p", this);
135 	return EFI_EXIT(EFI_SUCCESS);
136 }
137 
138 static const struct efi_block_io block_io_disk_template = {
139 	.reset = &efi_disk_reset,
140 	.read_blocks = &efi_disk_read_blocks,
141 	.write_blocks = &efi_disk_write_blocks,
142 	.flush_blocks = &efi_disk_flush_blocks,
143 };
144 
145 static void efi_disk_add_dev(char *name,
146 			     const struct blk_driver *cur_drvr,
147 			     const struct blk_desc *desc,
148 			     int dev_index,
149 			     lbaint_t offset)
150 {
151 	struct efi_disk_obj *diskobj;
152 	struct efi_device_path_file_path *dp;
153 	int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
154 
155 	diskobj = calloc(1, objlen);
156 
157 	/* Fill in object data */
158 	diskobj->parent.protocols[0].guid = &efi_block_io_guid;
159 	diskobj->parent.protocols[0].open = efi_disk_open_block;
160 	diskobj->parent.protocols[1].guid = &efi_guid_device_path;
161 	diskobj->parent.protocols[1].open = efi_disk_open_dp;
162 	diskobj->parent.handle = diskobj;
163 	diskobj->ops = block_io_disk_template;
164 	diskobj->ifname = cur_drvr->if_typename;
165 	diskobj->dev_index = dev_index;
166 	diskobj->offset = offset;
167 
168 	/* Fill in EFI IO Media info (for read/write callbacks) */
169 	diskobj->media.removable_media = desc->removable;
170 	diskobj->media.media_present = 1;
171 	diskobj->media.block_size = desc->blksz;
172 	diskobj->media.io_align = desc->blksz;
173 	diskobj->media.last_block = desc->lba;
174 	diskobj->ops.media = &diskobj->media;
175 
176 	/* Fill in device path */
177 	dp = (void*)&diskobj[1];
178 	diskobj->dp = dp;
179 	dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
180 	dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
181 	dp[0].dp.length = sizeof(*dp);
182 	ascii2unicode(dp[0].str, name);
183 
184 	dp[1].dp.type = DEVICE_PATH_TYPE_END;
185 	dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
186 	dp[1].dp.length = sizeof(*dp);
187 
188 	/* Hook up to the device list */
189 	list_add_tail(&diskobj->parent.link, &efi_obj_list);
190 }
191 
192 static int efi_disk_create_eltorito(struct blk_desc *desc,
193 				    const struct blk_driver *cur_drvr,
194 				    int diskid)
195 {
196 	int disks = 0;
197 #ifdef CONFIG_ISO_PARTITION
198 	char devname[32] = { 0 }; /* dp->str is u16[32] long */
199 	disk_partition_t info;
200 	int part = 1;
201 
202 	if (desc->part_type != PART_TYPE_ISO)
203 		return 0;
204 
205 	while (!part_get_info(desc, part, &info)) {
206 		snprintf(devname, sizeof(devname), "%s%d:%d",
207 			 cur_drvr->if_typename, diskid, part);
208 		efi_disk_add_dev(devname, cur_drvr, desc, diskid, info.start);
209 		part++;
210 		disks++;
211 	}
212 #endif
213 
214 	return disks;
215 }
216 
217 /*
218  * U-Boot doesn't have a list of all online disk devices. So when running our
219  * EFI payload, we scan through all of the potentially available ones and
220  * store them in our object pool.
221  *
222  * This gets called from do_bootefi_exec().
223  */
224 int efi_disk_register(void)
225 {
226 	const struct blk_driver *cur_drvr;
227 	int i, if_type;
228 	int disks = 0;
229 
230 	/* Search for all available disk devices */
231 	for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
232 		cur_drvr = blk_driver_lookup_type(if_type);
233 		if (!cur_drvr)
234 			continue;
235 
236 		printf("Scanning disks on %s...\n", cur_drvr->if_typename);
237 		for (i = 0; i < 4; i++) {
238 			struct blk_desc *desc;
239 			char devname[32] = { 0 }; /* dp->str is u16[32] long */
240 
241 			desc = blk_get_devnum_by_type(if_type, i);
242 			if (!desc)
243 				continue;
244 			if (desc->type == DEV_TYPE_UNKNOWN)
245 				continue;
246 
247 			snprintf(devname, sizeof(devname), "%s%d",
248 				 cur_drvr->if_typename, i);
249 			efi_disk_add_dev(devname, cur_drvr, desc, i, 0);
250 			disks++;
251 
252 			/*
253 			 * El Torito images show up as block devices
254 			 * in an EFI world, so let's create them here
255 			 */
256 			disks += efi_disk_create_eltorito(desc, cur_drvr, i);
257 		}
258 	}
259 	printf("Found %d disks\n", disks);
260 
261 	return 0;
262 }
263