]>
Commit | Line | Data |
---|---|---|
2a22d05d AG |
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> | |
6dd9faf8 | 10 | #include <blk.h> |
487d756f | 11 | #include <dm.h> |
2a22d05d AG |
12 | #include <efi_loader.h> |
13 | #include <inttypes.h> | |
14 | #include <part.h> | |
15 | #include <malloc.h> | |
16 | ||
17 | static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID; | |
18 | ||
19 | struct efi_disk_obj { | |
20 | /* Generic EFI object parent class data */ | |
21 | struct efi_object parent; | |
22 | /* EFI Interface callback struct for block I/O */ | |
23 | struct efi_block_io ops; | |
24 | /* U-Boot ifname for block device */ | |
25 | const char *ifname; | |
26 | /* U-Boot dev_index for block device */ | |
27 | int dev_index; | |
28 | /* EFI Interface Media descriptor struct, referenced by ops */ | |
29 | struct efi_block_io_media media; | |
30 | /* EFI device path to this block device */ | |
31 | struct efi_device_path_file_path *dp; | |
8c3df0bf AG |
32 | /* Offset into disk for simple partitions */ |
33 | lbaint_t offset; | |
f9d334bd AG |
34 | /* Internal block device */ |
35 | const struct blk_desc *desc; | |
2a22d05d AG |
36 | }; |
37 | ||
2a22d05d AG |
38 | static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this, |
39 | char extended_verification) | |
40 | { | |
41 | EFI_ENTRY("%p, %x", this, extended_verification); | |
42 | return EFI_EXIT(EFI_DEVICE_ERROR); | |
43 | } | |
44 | ||
45 | enum efi_disk_direction { | |
46 | EFI_DISK_READ, | |
47 | EFI_DISK_WRITE, | |
48 | }; | |
49 | ||
50 | static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this, | |
51 | u32 media_id, u64 lba, unsigned long buffer_size, | |
52 | void *buffer, enum efi_disk_direction direction) | |
53 | { | |
54 | struct efi_disk_obj *diskobj; | |
55 | struct blk_desc *desc; | |
56 | int blksz; | |
57 | int blocks; | |
58 | unsigned long n; | |
59 | ||
2a22d05d | 60 | diskobj = container_of(this, struct efi_disk_obj, ops); |
f9d334bd | 61 | desc = (struct blk_desc *) diskobj->desc; |
2a22d05d AG |
62 | blksz = desc->blksz; |
63 | blocks = buffer_size / blksz; | |
8c3df0bf | 64 | lba += diskobj->offset; |
2a22d05d | 65 | |
edcef3ba AG |
66 | debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__, |
67 | __LINE__, blocks, lba, blksz, direction); | |
2a22d05d AG |
68 | |
69 | /* We only support full block access */ | |
70 | if (buffer_size & (blksz - 1)) | |
3304990b | 71 | return EFI_DEVICE_ERROR; |
2a22d05d AG |
72 | |
73 | if (direction == EFI_DISK_READ) | |
487d756f | 74 | n = blk_dread(desc, lba, blocks, buffer); |
2a22d05d | 75 | else |
487d756f | 76 | n = blk_dwrite(desc, lba, blocks, buffer); |
2a22d05d AG |
77 | |
78 | /* We don't do interrupts, so check for timers cooperatively */ | |
79 | efi_timer_check(); | |
80 | ||
edcef3ba AG |
81 | debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks); |
82 | ||
2a22d05d | 83 | if (n != blocks) |
3304990b | 84 | return EFI_DEVICE_ERROR; |
2a22d05d | 85 | |
3304990b | 86 | return EFI_SUCCESS; |
2a22d05d AG |
87 | } |
88 | ||
e275458c | 89 | static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this, |
2a22d05d AG |
90 | u32 media_id, u64 lba, unsigned long buffer_size, |
91 | void *buffer) | |
92 | { | |
51735ae0 AG |
93 | void *real_buffer = buffer; |
94 | efi_status_t r; | |
95 | ||
96 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER | |
97 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { | |
98 | r = efi_disk_read_blocks(this, media_id, lba, | |
99 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); | |
100 | if (r != EFI_SUCCESS) | |
101 | return r; | |
102 | return efi_disk_read_blocks(this, media_id, lba + | |
103 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, | |
104 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, | |
105 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); | |
106 | } | |
107 | ||
108 | real_buffer = efi_bounce_buffer; | |
109 | #endif | |
110 | ||
111 | EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba, | |
112 | buffer_size, buffer); | |
113 | ||
114 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, | |
115 | EFI_DISK_READ); | |
116 | ||
117 | /* Copy from bounce buffer to real buffer if necessary */ | |
118 | if ((r == EFI_SUCCESS) && (real_buffer != buffer)) | |
119 | memcpy(buffer, real_buffer, buffer_size); | |
120 | ||
121 | return EFI_EXIT(r); | |
2a22d05d AG |
122 | } |
123 | ||
e275458c | 124 | static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this, |
2a22d05d AG |
125 | u32 media_id, u64 lba, unsigned long buffer_size, |
126 | void *buffer) | |
127 | { | |
51735ae0 AG |
128 | void *real_buffer = buffer; |
129 | efi_status_t r; | |
130 | ||
131 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER | |
132 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { | |
133 | r = efi_disk_write_blocks(this, media_id, lba, | |
134 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); | |
135 | if (r != EFI_SUCCESS) | |
136 | return r; | |
137 | return efi_disk_write_blocks(this, media_id, lba + | |
138 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, | |
139 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, | |
140 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); | |
141 | } | |
142 | ||
143 | real_buffer = efi_bounce_buffer; | |
144 | #endif | |
145 | ||
146 | EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba, | |
147 | buffer_size, buffer); | |
148 | ||
149 | /* Populate bounce buffer if necessary */ | |
150 | if (real_buffer != buffer) | |
151 | memcpy(real_buffer, buffer, buffer_size); | |
152 | ||
153 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, | |
154 | EFI_DISK_WRITE); | |
155 | ||
156 | return EFI_EXIT(r); | |
2a22d05d AG |
157 | } |
158 | ||
159 | static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this) | |
160 | { | |
161 | /* We always write synchronously */ | |
162 | EFI_ENTRY("%p", this); | |
163 | return EFI_EXIT(EFI_SUCCESS); | |
164 | } | |
165 | ||
166 | static const struct efi_block_io block_io_disk_template = { | |
167 | .reset = &efi_disk_reset, | |
168 | .read_blocks = &efi_disk_read_blocks, | |
169 | .write_blocks = &efi_disk_write_blocks, | |
170 | .flush_blocks = &efi_disk_flush_blocks, | |
171 | }; | |
172 | ||
487d756f SG |
173 | static void efi_disk_add_dev(const char *name, |
174 | const char *if_typename, | |
4a12a97c AG |
175 | const struct blk_desc *desc, |
176 | int dev_index, | |
177 | lbaint_t offset) | |
178 | { | |
179 | struct efi_disk_obj *diskobj; | |
180 | struct efi_device_path_file_path *dp; | |
181 | int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2); | |
182 | ||
0812d1a0 AG |
183 | /* Don't add empty devices */ |
184 | if (!desc->lba) | |
185 | return; | |
186 | ||
4a12a97c AG |
187 | diskobj = calloc(1, objlen); |
188 | ||
189 | /* Fill in object data */ | |
b5349f74 | 190 | dp = (void *)&diskobj[1]; |
4a12a97c | 191 | diskobj->parent.protocols[0].guid = &efi_block_io_guid; |
b5349f74 | 192 | diskobj->parent.protocols[0].protocol_interface = &diskobj->ops; |
4a12a97c | 193 | diskobj->parent.protocols[1].guid = &efi_guid_device_path; |
b5349f74 | 194 | diskobj->parent.protocols[1].protocol_interface = dp; |
4a12a97c AG |
195 | diskobj->parent.handle = diskobj; |
196 | diskobj->ops = block_io_disk_template; | |
487d756f | 197 | diskobj->ifname = if_typename; |
4a12a97c | 198 | diskobj->dev_index = dev_index; |
8c3df0bf | 199 | diskobj->offset = offset; |
f9d334bd | 200 | diskobj->desc = desc; |
4a12a97c AG |
201 | |
202 | /* Fill in EFI IO Media info (for read/write callbacks) */ | |
203 | diskobj->media.removable_media = desc->removable; | |
204 | diskobj->media.media_present = 1; | |
205 | diskobj->media.block_size = desc->blksz; | |
206 | diskobj->media.io_align = desc->blksz; | |
0812d1a0 | 207 | diskobj->media.last_block = desc->lba - offset; |
4a12a97c AG |
208 | diskobj->ops.media = &diskobj->media; |
209 | ||
210 | /* Fill in device path */ | |
4a12a97c AG |
211 | diskobj->dp = dp; |
212 | dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; | |
213 | dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; | |
214 | dp[0].dp.length = sizeof(*dp); | |
215 | ascii2unicode(dp[0].str, name); | |
216 | ||
217 | dp[1].dp.type = DEVICE_PATH_TYPE_END; | |
218 | dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END; | |
219 | dp[1].dp.length = sizeof(*dp); | |
220 | ||
221 | /* Hook up to the device list */ | |
222 | list_add_tail(&diskobj->parent.link, &efi_obj_list); | |
223 | } | |
224 | ||
8c3df0bf | 225 | static int efi_disk_create_eltorito(struct blk_desc *desc, |
487d756f | 226 | const char *if_typename, |
f9d334bd AG |
227 | int diskid, |
228 | const char *pdevname) | |
8c3df0bf AG |
229 | { |
230 | int disks = 0; | |
1acc0087 | 231 | #if CONFIG_IS_ENABLED(ISO_PARTITION) |
ecbe1a07 | 232 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
8c3df0bf AG |
233 | disk_partition_t info; |
234 | int part = 1; | |
235 | ||
236 | if (desc->part_type != PART_TYPE_ISO) | |
237 | return 0; | |
238 | ||
239 | while (!part_get_info(desc, part, &info)) { | |
f9d334bd AG |
240 | snprintf(devname, sizeof(devname), "%s:%d", pdevname, |
241 | part); | |
487d756f SG |
242 | efi_disk_add_dev(devname, if_typename, desc, diskid, |
243 | info.start); | |
8c3df0bf AG |
244 | part++; |
245 | disks++; | |
246 | } | |
247 | #endif | |
248 | ||
249 | return disks; | |
250 | } | |
251 | ||
2a22d05d AG |
252 | /* |
253 | * U-Boot doesn't have a list of all online disk devices. So when running our | |
254 | * EFI payload, we scan through all of the potentially available ones and | |
255 | * store them in our object pool. | |
256 | * | |
487d756f SG |
257 | * TODO([email protected]): Actually with CONFIG_BLK, U-Boot does have this. |
258 | * Consider converting the code to look up devices as needed. The EFI device | |
259 | * could be a child of the UCLASS_BLK block device, perhaps. | |
260 | * | |
2a22d05d AG |
261 | * This gets called from do_bootefi_exec(). |
262 | */ | |
263 | int efi_disk_register(void) | |
264 | { | |
2a22d05d | 265 | int disks = 0; |
487d756f SG |
266 | #ifdef CONFIG_BLK |
267 | struct udevice *dev; | |
268 | ||
70bfcdc6 | 269 | for (uclass_first_device_check(UCLASS_BLK, &dev); |
487d756f | 270 | dev; |
70bfcdc6 | 271 | uclass_next_device_check(&dev)) { |
487d756f SG |
272 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
273 | const char *if_typename = dev->driver->name; | |
274 | ||
275 | printf("Scanning disk %s...\n", dev->name); | |
276 | efi_disk_add_dev(dev->name, if_typename, desc, desc->devnum, 0); | |
277 | disks++; | |
278 | ||
279 | /* | |
280 | * El Torito images show up as block devices in an EFI world, | |
281 | * so let's create them here | |
282 | */ | |
283 | disks += efi_disk_create_eltorito(desc, if_typename, | |
f9d334bd | 284 | desc->devnum, dev->name); |
487d756f SG |
285 | } |
286 | #else | |
287 | int i, if_type; | |
2a22d05d AG |
288 | |
289 | /* Search for all available disk devices */ | |
6dd9faf8 | 290 | for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) { |
487d756f SG |
291 | const struct blk_driver *cur_drvr; |
292 | const char *if_typename; | |
293 | ||
6dd9faf8 SG |
294 | cur_drvr = blk_driver_lookup_type(if_type); |
295 | if (!cur_drvr) | |
296 | continue; | |
297 | ||
487d756f SG |
298 | if_typename = cur_drvr->if_typename; |
299 | printf("Scanning disks on %s...\n", if_typename); | |
2a22d05d AG |
300 | for (i = 0; i < 4; i++) { |
301 | struct blk_desc *desc; | |
ecbe1a07 | 302 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
2a22d05d | 303 | |
6dd9faf8 | 304 | desc = blk_get_devnum_by_type(if_type, i); |
2a22d05d AG |
305 | if (!desc) |
306 | continue; | |
307 | if (desc->type == DEV_TYPE_UNKNOWN) | |
308 | continue; | |
309 | ||
2a22d05d | 310 | snprintf(devname, sizeof(devname), "%s%d", |
487d756f SG |
311 | if_typename, i); |
312 | efi_disk_add_dev(devname, if_typename, desc, i, 0); | |
2a22d05d | 313 | disks++; |
8c3df0bf AG |
314 | |
315 | /* | |
316 | * El Torito images show up as block devices | |
317 | * in an EFI world, so let's create them here | |
318 | */ | |
f9d334bd AG |
319 | disks += efi_disk_create_eltorito(desc, if_typename, |
320 | i, devname); | |
2a22d05d AG |
321 | } |
322 | } | |
487d756f | 323 | #endif |
2a22d05d AG |
324 | printf("Found %d disks\n", disks); |
325 | ||
326 | return 0; | |
327 | } |