]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2a22d05d AG |
2 | /* |
3 | * EFI application disk support | |
4 | * | |
5 | * Copyright (c) 2016 Alexander Graf | |
2a22d05d AG |
6 | */ |
7 | ||
af457cfc HS |
8 | #define LOG_CATEGORY LOGC_EFI |
9 | ||
2a22d05d | 10 | #include <common.h> |
6dd9faf8 | 11 | #include <blk.h> |
487d756f | 12 | #include <dm.h> |
a9bf024b AT |
13 | #include <dm/device-internal.h> |
14 | #include <dm/tag.h> | |
15 | #include <event.h> | |
2a22d05d | 16 | #include <efi_loader.h> |
86740067 | 17 | #include <fs.h> |
af457cfc | 18 | #include <log.h> |
2a22d05d AG |
19 | #include <part.h> |
20 | #include <malloc.h> | |
21 | ||
11078bb2 HS |
22 | struct efi_system_partition efi_system_partition; |
23 | ||
dec88e41 | 24 | const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID; |
b9b0ea30 | 25 | const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; |
2a22d05d | 26 | |
d39646a3 HS |
27 | /** |
28 | * struct efi_disk_obj - EFI disk object | |
29 | * | |
30 | * @header: EFI object header | |
31 | * @ops: EFI disk I/O protocol interface | |
32 | * @ifname: interface name for block device | |
33 | * @dev_index: device index of block device | |
34 | * @media: block I/O media information | |
35 | * @dp: device path to the block device | |
36 | * @part: partition | |
37 | * @volume: simple file system protocol of the partition | |
d97e98c8 | 38 | * @dev: associated DM device |
d39646a3 | 39 | */ |
2a22d05d | 40 | struct efi_disk_obj { |
d39646a3 | 41 | struct efi_object header; |
2a22d05d | 42 | struct efi_block_io ops; |
2a22d05d | 43 | const char *ifname; |
2a22d05d | 44 | int dev_index; |
2a22d05d | 45 | struct efi_block_io_media media; |
884bcf6f | 46 | struct efi_device_path *dp; |
884bcf6f | 47 | unsigned int part; |
2a92080d | 48 | struct efi_simple_file_system_protocol *volume; |
2a22d05d AG |
49 | }; |
50 | ||
cda9b352 HS |
51 | /** |
52 | * efi_disk_reset() - reset block device | |
53 | * | |
54 | * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL. | |
55 | * | |
56 | * As U-Boot's block devices do not have a reset function simply return | |
57 | * EFI_SUCCESS. | |
58 | * | |
59 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
60 | * details. | |
61 | * | |
62 | * @this: pointer to the BLOCK_IO_PROTOCOL | |
63 | * @extended_verification: extended verification | |
64 | * Return: status code | |
65 | */ | |
2a22d05d AG |
66 | static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this, |
67 | char extended_verification) | |
68 | { | |
69 | EFI_ENTRY("%p, %x", this, extended_verification); | |
cda9b352 | 70 | return EFI_EXIT(EFI_SUCCESS); |
2a22d05d AG |
71 | } |
72 | ||
05f391e2 AT |
73 | /** |
74 | * efi_disk_is_removable() - check if the device is removable media | |
75 | * @handle: efi object handle; | |
76 | * | |
77 | * Examine the device and determine if the device is a local block device | |
78 | * and removable media. | |
79 | * | |
80 | * Return: true if removable, false otherwise | |
81 | */ | |
82 | bool efi_disk_is_removable(efi_handle_t handle) | |
83 | { | |
84 | struct efi_handler *handler; | |
85 | struct efi_block_io *io; | |
86 | efi_status_t ret; | |
87 | ||
88 | ret = efi_search_protocol(handle, &efi_block_io_guid, &handler); | |
89 | if (ret != EFI_SUCCESS) | |
90 | return false; | |
91 | ||
92 | io = handler->protocol_interface; | |
93 | ||
94 | if (!io || !io->media) | |
95 | return false; | |
96 | ||
97 | return (bool)io->media->removable_media; | |
98 | } | |
99 | ||
2a22d05d AG |
100 | enum efi_disk_direction { |
101 | EFI_DISK_READ, | |
102 | EFI_DISK_WRITE, | |
103 | }; | |
104 | ||
a80a232e | 105 | static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this, |
2a22d05d AG |
106 | u32 media_id, u64 lba, unsigned long buffer_size, |
107 | void *buffer, enum efi_disk_direction direction) | |
108 | { | |
109 | struct efi_disk_obj *diskobj; | |
2a22d05d AG |
110 | int blksz; |
111 | int blocks; | |
112 | unsigned long n; | |
113 | ||
2a22d05d | 114 | diskobj = container_of(this, struct efi_disk_obj, ops); |
d97e98c8 | 115 | blksz = diskobj->media.block_size; |
2a22d05d AG |
116 | blocks = buffer_size / blksz; |
117 | ||
9d3f3398 HS |
118 | EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n", |
119 | blocks, lba, blksz, direction); | |
2a22d05d AG |
120 | |
121 | /* We only support full block access */ | |
122 | if (buffer_size & (blksz - 1)) | |
f59f0825 | 123 | return EFI_BAD_BUFFER_SIZE; |
2a22d05d | 124 | |
6c640422 | 125 | if (CONFIG_IS_ENABLED(PARTITIONS) && |
ee576662 | 126 | device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) { |
6c640422 | 127 | if (direction == EFI_DISK_READ) |
ee576662 | 128 | n = dev_read(diskobj->header.dev, lba, blocks, buffer); |
6c640422 | 129 | else |
ee576662 | 130 | n = dev_write(diskobj->header.dev, lba, blocks, buffer); |
6c640422 AT |
131 | } else { |
132 | /* dev is a block device (UCLASS_BLK) */ | |
133 | struct blk_desc *desc; | |
134 | ||
ee576662 | 135 | desc = dev_get_uclass_plat(diskobj->header.dev); |
6c640422 AT |
136 | if (direction == EFI_DISK_READ) |
137 | n = blk_dread(desc, lba, blocks, buffer); | |
138 | else | |
139 | n = blk_dwrite(desc, lba, blocks, buffer); | |
140 | } | |
2a22d05d AG |
141 | |
142 | /* We don't do interrupts, so check for timers cooperatively */ | |
143 | efi_timer_check(); | |
144 | ||
9d3f3398 | 145 | EFI_PRINT("n=%lx blocks=%x\n", n, blocks); |
edcef3ba | 146 | |
2a22d05d | 147 | if (n != blocks) |
3304990b | 148 | return EFI_DEVICE_ERROR; |
2a22d05d | 149 | |
3304990b | 150 | return EFI_SUCCESS; |
2a22d05d AG |
151 | } |
152 | ||
55976b7f HS |
153 | /** |
154 | * efi_disk_read_blocks() - reads blocks from device | |
155 | * | |
156 | * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL. | |
157 | * | |
158 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
159 | * details. | |
160 | * | |
161 | * @this: pointer to the BLOCK_IO_PROTOCOL | |
162 | * @media_id: id of the medium to be read from | |
163 | * @lba: starting logical block for reading | |
164 | * @buffer_size: size of the read buffer | |
165 | * @buffer: pointer to the destination buffer | |
166 | * Return: status code | |
167 | */ | |
e275458c | 168 | static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this, |
4f94865b | 169 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
2a22d05d AG |
170 | void *buffer) |
171 | { | |
51735ae0 AG |
172 | void *real_buffer = buffer; |
173 | efi_status_t r; | |
174 | ||
f59f0825 HS |
175 | if (!this) |
176 | return EFI_INVALID_PARAMETER; | |
177 | /* TODO: check for media changes */ | |
178 | if (media_id != this->media->media_id) | |
179 | return EFI_MEDIA_CHANGED; | |
180 | if (!this->media->media_present) | |
181 | return EFI_NO_MEDIA; | |
688e8825 HS |
182 | /* media->io_align is a power of 2 or 0 */ |
183 | if (this->media->io_align && | |
184 | (uintptr_t)buffer & (this->media->io_align - 1)) | |
f59f0825 HS |
185 | return EFI_INVALID_PARAMETER; |
186 | if (lba * this->media->block_size + buffer_size > | |
e67beffe | 187 | (this->media->last_block + 1) * this->media->block_size) |
f59f0825 HS |
188 | return EFI_INVALID_PARAMETER; |
189 | ||
51735ae0 AG |
190 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
191 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { | |
192 | r = efi_disk_read_blocks(this, media_id, lba, | |
193 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); | |
194 | if (r != EFI_SUCCESS) | |
195 | return r; | |
196 | return efi_disk_read_blocks(this, media_id, lba + | |
197 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, | |
198 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, | |
199 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); | |
200 | } | |
201 | ||
202 | real_buffer = efi_bounce_buffer; | |
203 | #endif | |
204 | ||
dee37fc9 | 205 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
51735ae0 AG |
206 | buffer_size, buffer); |
207 | ||
208 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, | |
209 | EFI_DISK_READ); | |
210 | ||
211 | /* Copy from bounce buffer to real buffer if necessary */ | |
212 | if ((r == EFI_SUCCESS) && (real_buffer != buffer)) | |
213 | memcpy(buffer, real_buffer, buffer_size); | |
214 | ||
215 | return EFI_EXIT(r); | |
2a22d05d AG |
216 | } |
217 | ||
55976b7f HS |
218 | /** |
219 | * efi_disk_write_blocks() - writes blocks to device | |
220 | * | |
221 | * This function implements the WriteBlocks service of the | |
222 | * EFI_BLOCK_IO_PROTOCOL. | |
223 | * | |
224 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
225 | * details. | |
226 | * | |
227 | * @this: pointer to the BLOCK_IO_PROTOCOL | |
228 | * @media_id: id of the medium to be written to | |
229 | * @lba: starting logical block for writing | |
230 | * @buffer_size: size of the write buffer | |
231 | * @buffer: pointer to the source buffer | |
232 | * Return: status code | |
233 | */ | |
e275458c | 234 | static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this, |
4f94865b | 235 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
2a22d05d AG |
236 | void *buffer) |
237 | { | |
51735ae0 AG |
238 | void *real_buffer = buffer; |
239 | efi_status_t r; | |
240 | ||
f59f0825 HS |
241 | if (!this) |
242 | return EFI_INVALID_PARAMETER; | |
243 | if (this->media->read_only) | |
244 | return EFI_WRITE_PROTECTED; | |
245 | /* TODO: check for media changes */ | |
246 | if (media_id != this->media->media_id) | |
247 | return EFI_MEDIA_CHANGED; | |
248 | if (!this->media->media_present) | |
249 | return EFI_NO_MEDIA; | |
688e8825 HS |
250 | /* media->io_align is a power of 2 or 0 */ |
251 | if (this->media->io_align && | |
252 | (uintptr_t)buffer & (this->media->io_align - 1)) | |
f59f0825 HS |
253 | return EFI_INVALID_PARAMETER; |
254 | if (lba * this->media->block_size + buffer_size > | |
e67beffe | 255 | (this->media->last_block + 1) * this->media->block_size) |
f59f0825 HS |
256 | return EFI_INVALID_PARAMETER; |
257 | ||
51735ae0 AG |
258 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
259 | if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) { | |
260 | r = efi_disk_write_blocks(this, media_id, lba, | |
261 | EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer); | |
262 | if (r != EFI_SUCCESS) | |
263 | return r; | |
264 | return efi_disk_write_blocks(this, media_id, lba + | |
265 | EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size, | |
266 | buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE, | |
267 | buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE); | |
268 | } | |
269 | ||
270 | real_buffer = efi_bounce_buffer; | |
271 | #endif | |
272 | ||
dee37fc9 | 273 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
51735ae0 AG |
274 | buffer_size, buffer); |
275 | ||
276 | /* Populate bounce buffer if necessary */ | |
277 | if (real_buffer != buffer) | |
278 | memcpy(real_buffer, buffer, buffer_size); | |
279 | ||
280 | r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer, | |
281 | EFI_DISK_WRITE); | |
282 | ||
283 | return EFI_EXIT(r); | |
2a22d05d AG |
284 | } |
285 | ||
55976b7f HS |
286 | /** |
287 | * efi_disk_flush_blocks() - flushes modified data to the device | |
288 | * | |
289 | * This function implements the FlushBlocks service of the | |
290 | * EFI_BLOCK_IO_PROTOCOL. | |
291 | * | |
292 | * As we always write synchronously nothing is done here. | |
293 | * | |
294 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
295 | * details. | |
296 | * | |
297 | * @this: pointer to the BLOCK_IO_PROTOCOL | |
298 | * Return: status code | |
299 | */ | |
2a22d05d AG |
300 | static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this) |
301 | { | |
2a22d05d AG |
302 | EFI_ENTRY("%p", this); |
303 | return EFI_EXIT(EFI_SUCCESS); | |
304 | } | |
305 | ||
306 | static const struct efi_block_io block_io_disk_template = { | |
307 | .reset = &efi_disk_reset, | |
308 | .read_blocks = &efi_disk_read_blocks, | |
309 | .write_blocks = &efi_disk_write_blocks, | |
310 | .flush_blocks = &efi_disk_flush_blocks, | |
311 | }; | |
312 | ||
47a95963 HS |
313 | /** |
314 | * efi_fs_from_path() - retrieve simple file system protocol | |
315 | * | |
316 | * Gets the simple file system protocol for a file device path. | |
110d80a1 HS |
317 | * |
318 | * The full path provided is split into device part and into a file | |
319 | * part. The device part is used to find the handle on which the | |
320 | * simple file system protocol is installed. | |
321 | * | |
47a95963 HS |
322 | * @full_path: device path including device and file |
323 | * Return: simple file system protocol | |
2a92080d RC |
324 | */ |
325 | struct efi_simple_file_system_protocol * | |
110d80a1 | 326 | efi_fs_from_path(struct efi_device_path *full_path) |
2a92080d RC |
327 | { |
328 | struct efi_object *efiobj; | |
110d80a1 HS |
329 | struct efi_handler *handler; |
330 | struct efi_device_path *device_path; | |
331 | struct efi_device_path *file_path; | |
332 | efi_status_t ret; | |
2a92080d | 333 | |
110d80a1 HS |
334 | /* Split the path into a device part and a file part */ |
335 | ret = efi_dp_split_file_path(full_path, &device_path, &file_path); | |
336 | if (ret != EFI_SUCCESS) | |
337 | return NULL; | |
338 | efi_free_pool(file_path); | |
339 | ||
340 | /* Get the EFI object for the partition */ | |
e46ef1db | 341 | efiobj = efi_dp_find_obj(device_path, NULL, NULL); |
110d80a1 | 342 | efi_free_pool(device_path); |
2a92080d RC |
343 | if (!efiobj) |
344 | return NULL; | |
345 | ||
110d80a1 HS |
346 | /* Find the simple file system protocol */ |
347 | ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid, | |
348 | &handler); | |
349 | if (ret != EFI_SUCCESS) | |
350 | return NULL; | |
2a92080d | 351 | |
110d80a1 HS |
352 | /* Return the simple file system protocol for the partition */ |
353 | return handler->protocol_interface; | |
2a92080d RC |
354 | } |
355 | ||
86740067 AT |
356 | /** |
357 | * efi_fs_exists() - check if a partition bears a file system | |
358 | * | |
359 | * @desc: block device descriptor | |
360 | * @part: partition number | |
361 | * Return: 1 if a file system exists on the partition | |
362 | * 0 otherwise | |
363 | */ | |
364 | static int efi_fs_exists(struct blk_desc *desc, int part) | |
365 | { | |
366 | if (fs_set_blk_dev_with_part(desc, part)) | |
367 | return 0; | |
368 | ||
369 | if (fs_get_type() == FS_TYPE_ANY) | |
370 | return 0; | |
371 | ||
372 | fs_close(); | |
373 | ||
374 | return 1; | |
375 | } | |
376 | ||
55976b7f | 377 | /** |
47a95963 | 378 | * efi_disk_add_dev() - create a handle for a partition or disk |
93945f2c | 379 | * |
47a95963 HS |
380 | * @parent: parent handle |
381 | * @dp_parent: parent device path | |
382 | * @if_typename: interface name for block device | |
383 | * @desc: internal block device | |
384 | * @dev_index: device index for block device | |
8d0949b3 | 385 | * @part_info: partition info |
55976b7f HS |
386 | * @part: partition |
387 | * @disk: pointer to receive the created handle | |
47a95963 | 388 | * Return: disk object |
93945f2c | 389 | */ |
df9cf561 | 390 | static efi_status_t efi_disk_add_dev( |
64e4db0f HS |
391 | efi_handle_t parent, |
392 | struct efi_device_path *dp_parent, | |
393 | const char *if_typename, | |
394 | struct blk_desc *desc, | |
395 | int dev_index, | |
8d0949b3 | 396 | struct disk_partition *part_info, |
df9cf561 HS |
397 | unsigned int part, |
398 | struct efi_disk_obj **disk) | |
4a12a97c AG |
399 | { |
400 | struct efi_disk_obj *diskobj; | |
0a87e05d | 401 | struct efi_object *handle; |
b9b0ea30 | 402 | const efi_guid_t *guid = NULL; |
4b9f7aaf | 403 | efi_status_t ret; |
4a12a97c | 404 | |
0812d1a0 AG |
405 | /* Don't add empty devices */ |
406 | if (!desc->lba) | |
df9cf561 | 407 | return EFI_NOT_READY; |
0812d1a0 | 408 | |
884bcf6f | 409 | diskobj = calloc(1, sizeof(*diskobj)); |
4b9f7aaf | 410 | if (!diskobj) |
df9cf561 | 411 | return EFI_OUT_OF_RESOURCES; |
4b9f7aaf HS |
412 | |
413 | /* Hook up to the device list */ | |
d39646a3 | 414 | efi_add_handle(&diskobj->header); |
4a12a97c AG |
415 | |
416 | /* Fill in object data */ | |
8d0949b3 | 417 | if (part_info) { |
64e4db0f | 418 | struct efi_device_path *node = efi_dp_part_node(desc, part); |
26448513 HS |
419 | struct efi_handler *handler; |
420 | void *protocol_interface; | |
421 | ||
422 | /* Parent must expose EFI_BLOCK_IO_PROTOCOL */ | |
423 | ret = efi_search_protocol(parent, &efi_block_io_guid, &handler); | |
424 | if (ret != EFI_SUCCESS) | |
425 | goto error; | |
426 | ||
427 | /* | |
428 | * Link the partition (child controller) to the block device | |
429 | * (controller). | |
430 | */ | |
431 | ret = efi_protocol_open(handler, &protocol_interface, NULL, | |
432 | &diskobj->header, | |
433 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER); | |
434 | if (ret != EFI_SUCCESS) | |
435 | goto error; | |
64e4db0f HS |
436 | |
437 | diskobj->dp = efi_dp_append_node(dp_parent, node); | |
438 | efi_free_pool(node); | |
8d0949b3 | 439 | diskobj->media.last_block = part_info->size - 1; |
b9b0ea30 HS |
440 | if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) |
441 | guid = &efi_system_partition_guid; | |
64e4db0f HS |
442 | } else { |
443 | diskobj->dp = efi_dp_from_part(desc, part); | |
8d0949b3 | 444 | diskobj->media.last_block = desc->lba - 1; |
64e4db0f | 445 | } |
884bcf6f | 446 | diskobj->part = part; |
0a87e05d HS |
447 | |
448 | /* | |
449 | * Install the device path and the block IO protocol. | |
450 | * | |
451 | * InstallMultipleProtocolInterfaces() checks if the device path is | |
452 | * already installed on an other handle and returns EFI_ALREADY_STARTED | |
453 | * in this case. | |
454 | */ | |
455 | handle = &diskobj->header; | |
456 | ret = EFI_CALL(efi_install_multiple_protocol_interfaces( | |
457 | &handle, &efi_guid_device_path, diskobj->dp, | |
b9b0ea30 HS |
458 | &efi_block_io_guid, &diskobj->ops, |
459 | guid, NULL, NULL)); | |
4b9f7aaf | 460 | if (ret != EFI_SUCCESS) |
cd9a26bf | 461 | goto error; |
0a87e05d HS |
462 | |
463 | /* | |
464 | * On partitions or whole disks without partitions install the | |
465 | * simple file system protocol if a file system is available. | |
466 | */ | |
89cb6a5d AT |
467 | if ((part || desc->part_type == PART_TYPE_UNKNOWN) && |
468 | efi_fs_exists(desc, part)) { | |
2a92080d RC |
469 | diskobj->volume = efi_simple_file_system(desc, part, |
470 | diskobj->dp); | |
d39646a3 | 471 | ret = efi_add_protocol(&diskobj->header, |
4b9f7aaf | 472 | &efi_simple_file_system_protocol_guid, |
22de1de9 | 473 | diskobj->volume); |
4b9f7aaf | 474 | if (ret != EFI_SUCCESS) |
df9cf561 | 475 | return ret; |
2a92080d | 476 | } |
4a12a97c | 477 | diskobj->ops = block_io_disk_template; |
487d756f | 478 | diskobj->ifname = if_typename; |
4a12a97c AG |
479 | diskobj->dev_index = dev_index; |
480 | ||
481 | /* Fill in EFI IO Media info (for read/write callbacks) */ | |
482 | diskobj->media.removable_media = desc->removable; | |
483 | diskobj->media.media_present = 1; | |
f59f0825 HS |
484 | /* |
485 | * MediaID is just an arbitrary counter. | |
486 | * We have to change it if the medium is removed or changed. | |
487 | */ | |
488 | diskobj->media.media_id = 1; | |
4a12a97c AG |
489 | diskobj->media.block_size = desc->blksz; |
490 | diskobj->media.io_align = desc->blksz; | |
9f888969 | 491 | if (part) |
5f770836 | 492 | diskobj->media.logical_partition = 1; |
4a12a97c | 493 | diskobj->ops.media = &diskobj->media; |
df9cf561 HS |
494 | if (disk) |
495 | *disk = diskobj; | |
11078bb2 | 496 | |
8d0949b3 | 497 | EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d" |
7a85f324 | 498 | ", last_block %llu\n", |
8d0949b3 HS |
499 | diskobj->part, |
500 | diskobj->media.media_present, | |
501 | diskobj->media.logical_partition, | |
502 | diskobj->media.removable_media, | |
8d0949b3 HS |
503 | diskobj->media.last_block); |
504 | ||
11078bb2 HS |
505 | /* Store first EFI system partition */ |
506 | if (part && !efi_system_partition.if_type) { | |
b9b0ea30 | 507 | if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) { |
11078bb2 HS |
508 | efi_system_partition.if_type = desc->if_type; |
509 | efi_system_partition.devnum = desc->devnum; | |
510 | efi_system_partition.part = part; | |
3dca77b1 | 511 | EFI_PRINT("EFI system partition: %s %x:%x\n", |
11078bb2 HS |
512 | blk_get_if_type_name(desc->if_type), |
513 | desc->devnum, part); | |
514 | } | |
515 | } | |
df9cf561 | 516 | return EFI_SUCCESS; |
26448513 HS |
517 | error: |
518 | efi_delete_handle(&diskobj->header); | |
519 | return ret; | |
4a12a97c AG |
520 | } |
521 | ||
a9bf024b AT |
522 | /* |
523 | * Create a handle for a whole raw disk | |
524 | * | |
525 | * @dev uclass device (UCLASS_BLK) | |
64e4db0f | 526 | * |
a9bf024b AT |
527 | * Create an efi_disk object which is associated with @dev. |
528 | * The type of @dev must be UCLASS_BLK. | |
47a95963 | 529 | * |
a9bf024b | 530 | * @return 0 on success, -1 otherwise |
64e4db0f | 531 | */ |
a9bf024b | 532 | static int efi_disk_create_raw(struct udevice *dev) |
8c3df0bf | 533 | { |
a9bf024b AT |
534 | struct efi_disk_obj *disk; |
535 | struct blk_desc *desc; | |
536 | const char *if_typename; | |
537 | int diskid; | |
64e4db0f | 538 | efi_status_t ret; |
64e4db0f | 539 | |
a9bf024b AT |
540 | desc = dev_get_uclass_plat(dev); |
541 | if_typename = blk_get_if_type_name(desc->if_type); | |
542 | diskid = desc->devnum; | |
543 | ||
544 | ret = efi_disk_add_dev(NULL, NULL, if_typename, desc, | |
545 | diskid, NULL, 0, &disk); | |
546 | if (ret != EFI_SUCCESS) { | |
547 | if (ret == EFI_NOT_READY) | |
548 | log_notice("Disk %s not ready\n", dev->name); | |
549 | else | |
550 | log_err("Adding disk for %s failed\n", dev->name); | |
551 | ||
552 | return -1; | |
553 | } | |
ee576662 | 554 | if (efi_link_dev(&disk->header, dev)) { |
a9bf024b AT |
555 | efi_free_pool(disk->dp); |
556 | efi_delete_handle(&disk->header); | |
557 | ||
558 | return -1; | |
8c3df0bf | 559 | } |
884bcf6f | 560 | |
a9bf024b | 561 | return 0; |
8c3df0bf AG |
562 | } |
563 | ||
a9bf024b AT |
564 | /* |
565 | * Create a handle for a disk partition | |
2a22d05d | 566 | * |
a9bf024b | 567 | * @dev uclass device (UCLASS_PARTITION) |
47a95963 | 568 | * |
a9bf024b AT |
569 | * Create an efi_disk object which is associated with @dev. |
570 | * The type of @dev must be UCLASS_PARTITION. | |
487d756f | 571 | * |
a9bf024b | 572 | * @return 0 on success, -1 otherwise |
2a22d05d | 573 | */ |
a9bf024b | 574 | static int efi_disk_create_part(struct udevice *dev) |
2a22d05d | 575 | { |
a9bf024b AT |
576 | efi_handle_t parent; |
577 | struct blk_desc *desc; | |
578 | const char *if_typename; | |
579 | struct disk_part *part_data; | |
580 | struct disk_partition *info; | |
581 | unsigned int part; | |
582 | int diskid; | |
583 | struct efi_handler *handler; | |
584 | struct efi_device_path *dp_parent; | |
64e4db0f | 585 | struct efi_disk_obj *disk; |
df9cf561 | 586 | efi_status_t ret; |
a9bf024b AT |
587 | |
588 | if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent)) | |
589 | return -1; | |
590 | ||
591 | desc = dev_get_uclass_plat(dev_get_parent(dev)); | |
592 | if_typename = blk_get_if_type_name(desc->if_type); | |
593 | diskid = desc->devnum; | |
594 | ||
595 | part_data = dev_get_uclass_plat(dev); | |
596 | part = part_data->partnum; | |
597 | info = &part_data->gpt_part_info; | |
598 | ||
599 | ret = efi_search_protocol(parent, &efi_guid_device_path, &handler); | |
600 | if (ret != EFI_SUCCESS) | |
601 | return -1; | |
602 | dp_parent = (struct efi_device_path *)handler->protocol_interface; | |
603 | ||
604 | ret = efi_disk_add_dev(parent, dp_parent, if_typename, desc, diskid, | |
605 | info, part, &disk); | |
606 | if (ret != EFI_SUCCESS) { | |
607 | log_err("Adding partition for %s failed\n", dev->name); | |
608 | return -1; | |
609 | } | |
ee576662 | 610 | if (efi_link_dev(&disk->header, dev)) { |
a9bf024b AT |
611 | efi_free_pool(disk->dp); |
612 | efi_delete_handle(&disk->header); | |
613 | ||
614 | return -1; | |
615 | } | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
620 | /* | |
621 | * Create efi_disk objects for a block device | |
622 | * | |
623 | * @dev uclass device (UCLASS_BLK) | |
624 | * | |
625 | * Create efi_disk objects for partitions as well as a raw disk | |
626 | * which is associated with @dev. | |
627 | * The type of @dev must be UCLASS_BLK. | |
628 | * This function is expected to be called at EV_PM_POST_PROBE. | |
629 | * | |
630 | * @return 0 on success, -1 otherwise | |
631 | */ | |
632 | static int efi_disk_probe(void *ctx, struct event *event) | |
633 | { | |
487d756f | 634 | struct udevice *dev; |
a9bf024b | 635 | enum uclass_id id; |
3c809dfe | 636 | struct blk_desc *desc; |
a9bf024b AT |
637 | struct udevice *child; |
638 | int ret; | |
487d756f | 639 | |
a9bf024b AT |
640 | dev = event->data.dm.dev; |
641 | id = device_get_uclass_id(dev); | |
487d756f | 642 | |
a9bf024b AT |
643 | /* TODO: We won't support partitions in a partition */ |
644 | if (id != UCLASS_BLK) | |
645 | return 0; | |
646 | ||
3c809dfe AT |
647 | /* |
648 | * avoid creating duplicated objects now that efi_driver | |
649 | * has already created an efi_disk at this moment. | |
650 | */ | |
651 | desc = dev_get_uclass_plat(dev); | |
652 | if (desc->if_type != IF_TYPE_EFI_LOADER) { | |
653 | ret = efi_disk_create_raw(dev); | |
654 | if (ret) | |
655 | return -1; | |
656 | } | |
487d756f | 657 | |
a9bf024b AT |
658 | device_foreach_child(child, dev) { |
659 | ret = efi_disk_create_part(child); | |
660 | if (ret) | |
661 | return -1; | |
487d756f | 662 | } |
2a22d05d | 663 | |
a9bf024b AT |
664 | return 0; |
665 | } | |
666 | ||
b406eb04 AT |
667 | /* |
668 | * Delete an efi_disk object for a whole raw disk | |
669 | * | |
670 | * @dev uclass device (UCLASS_BLK) | |
671 | * | |
672 | * Delete an efi_disk object which is associated with @dev. | |
673 | * The type of @dev must be UCLASS_BLK. | |
674 | * | |
675 | * @return 0 on success, -1 otherwise | |
676 | */ | |
677 | static int efi_disk_delete_raw(struct udevice *dev) | |
678 | { | |
679 | efi_handle_t handle; | |
a3cb34e9 | 680 | struct blk_desc *desc; |
b406eb04 AT |
681 | struct efi_disk_obj *diskobj; |
682 | ||
683 | if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) | |
684 | return -1; | |
685 | ||
a3cb34e9 AT |
686 | desc = dev_get_uclass_plat(dev); |
687 | if (desc->if_type != IF_TYPE_EFI_LOADER) { | |
688 | diskobj = container_of(handle, struct efi_disk_obj, header); | |
689 | efi_free_pool(diskobj->dp); | |
690 | } | |
b406eb04 AT |
691 | |
692 | efi_delete_handle(handle); | |
693 | dev_tag_del(dev, DM_TAG_EFI); | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Delete an efi_disk object for a disk partition | |
700 | * | |
701 | * @dev uclass device (UCLASS_PARTITION) | |
702 | * | |
703 | * Delete an efi_disk object which is associated with @dev. | |
704 | * The type of @dev must be UCLASS_PARTITION. | |
705 | * | |
706 | * @return 0 on success, -1 otherwise | |
707 | */ | |
708 | static int efi_disk_delete_part(struct udevice *dev) | |
709 | { | |
710 | efi_handle_t handle; | |
711 | struct efi_disk_obj *diskobj; | |
712 | ||
713 | if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) | |
714 | return -1; | |
715 | ||
716 | diskobj = container_of(handle, struct efi_disk_obj, header); | |
717 | ||
718 | efi_free_pool(diskobj->dp); | |
719 | efi_delete_handle(handle); | |
720 | dev_tag_del(dev, DM_TAG_EFI); | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | /* | |
726 | * Delete an efi_disk object for a block device | |
727 | * | |
728 | * @dev uclass device (UCLASS_BLK or UCLASS_PARTITION) | |
729 | * | |
730 | * Delete an efi_disk object which is associated with @dev. | |
731 | * The type of @dev must be either UCLASS_BLK or UCLASS_PARTITION. | |
732 | * This function is expected to be called at EV_PM_PRE_REMOVE. | |
733 | * | |
734 | * @return 0 on success, -1 otherwise | |
735 | */ | |
736 | static int efi_disk_remove(void *ctx, struct event *event) | |
737 | { | |
738 | enum uclass_id id; | |
739 | struct udevice *dev; | |
740 | ||
741 | dev = event->data.dm.dev; | |
742 | id = device_get_uclass_id(dev); | |
743 | ||
744 | if (id == UCLASS_BLK) | |
745 | return efi_disk_delete_raw(dev); | |
746 | else if (id == UCLASS_PARTITION) | |
747 | return efi_disk_delete_part(dev); | |
748 | else | |
749 | return 0; | |
750 | } | |
751 | ||
a9bf024b AT |
752 | efi_status_t efi_disk_init(void) |
753 | { | |
754 | int ret; | |
755 | ||
756 | ret = event_register("efi_disk add", EVT_DM_POST_PROBE, | |
757 | efi_disk_probe, NULL); | |
758 | if (ret) { | |
759 | log_err("Event registration for efi_disk add failed\n"); | |
760 | return EFI_OUT_OF_RESOURCES; | |
761 | } | |
2a22d05d | 762 | |
b406eb04 AT |
763 | ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE, |
764 | efi_disk_remove, NULL); | |
765 | if (ret) { | |
766 | log_err("Event registration for efi_disk del failed\n"); | |
767 | return EFI_OUT_OF_RESOURCES; | |
768 | } | |
769 | ||
df9cf561 | 770 | return EFI_SUCCESS; |
2a22d05d | 771 | } |