Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application disk support |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 11 | #include <efi_loader.h> |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 12 | #include <part.h> |
| 13 | #include <malloc.h> |
| 14 | |
Heinrich Schuchardt | 405afb5 | 2018-01-19 20:24:45 +0100 | [diff] [blame] | 15 | const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 16 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 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 | */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 31 | struct efi_disk_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 32 | struct efi_object header; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 33 | struct efi_block_io ops; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 34 | const char *ifname; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 35 | int dev_index; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 36 | struct efi_block_io_media media; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 37 | struct efi_device_path *dp; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 38 | unsigned int part; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 39 | struct efi_simple_file_system_protocol *volume; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 40 | lbaint_t offset; |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 41 | struct blk_desc *desc; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 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 | |
Heinrich Schuchardt | 9ce0617 | 2017-08-26 22:33:13 +0200 | [diff] [blame] | 56 | static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this, |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 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 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 66 | diskobj = container_of(this, struct efi_disk_obj, ops); |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 67 | desc = (struct blk_desc *) diskobj->desc; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 68 | blksz = desc->blksz; |
| 69 | blocks = buffer_size / blksz; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 70 | lba += diskobj->offset; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 71 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 72 | debug("EFI: %s:%d blocks=%x lba=%llx blksz=%x dir=%d\n", __func__, |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 73 | __LINE__, blocks, lba, blksz, direction); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 74 | |
| 75 | /* We only support full block access */ |
| 76 | if (buffer_size & (blksz - 1)) |
Rob Clark | 99c4387 | 2017-07-25 20:28:29 -0400 | [diff] [blame] | 77 | return EFI_DEVICE_ERROR; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 78 | |
| 79 | if (direction == EFI_DISK_READ) |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 80 | n = blk_dread(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 81 | else |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 82 | n = blk_dwrite(desc, lba, blocks, buffer); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 83 | |
| 84 | /* We don't do interrupts, so check for timers cooperatively */ |
| 85 | efi_timer_check(); |
| 86 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 87 | debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks); |
| 88 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 89 | if (n != blocks) |
Rob Clark | 99c4387 | 2017-07-25 20:28:29 -0400 | [diff] [blame] | 90 | return EFI_DEVICE_ERROR; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 91 | |
Rob Clark | 99c4387 | 2017-07-25 20:28:29 -0400 | [diff] [blame] | 92 | return EFI_SUCCESS; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 95 | static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this, |
Heinrich Schuchardt | 0844f79 | 2018-01-19 20:24:48 +0100 | [diff] [blame] | 96 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 97 | void *buffer) |
| 98 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 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 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 117 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 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); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 130 | static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this, |
Heinrich Schuchardt | 0844f79 | 2018-01-19 20:24:48 +0100 | [diff] [blame] | 131 | u32 media_id, u64 lba, efi_uintn_t buffer_size, |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 132 | void *buffer) |
| 133 | { |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 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 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 152 | EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 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); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 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 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 179 | /* |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 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 |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 188 | */ |
| 189 | struct efi_simple_file_system_protocol * |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 190 | efi_fs_from_path(struct efi_device_path *full_path) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 191 | { |
| 192 | struct efi_object *efiobj; |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 193 | struct efi_handler *handler; |
| 194 | struct efi_device_path *device_path; |
| 195 | struct efi_device_path *file_path; |
| 196 | efi_status_t ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 197 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 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); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 207 | if (!efiobj) |
| 208 | return NULL; |
| 209 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 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; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 215 | |
Heinrich Schuchardt | 3957876 | 2018-01-19 20:24:39 +0100 | [diff] [blame] | 216 | /* Return the simple file system protocol for the partition */ |
| 217 | return handler->protocol_interface; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 220 | /* |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 221 | * Create a handle for a partition or disk |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 222 | * |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 223 | * @parent parent handle |
| 224 | * @dp_parent parent device path |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 229 | * @return disk object |
Heinrich Schuchardt | 34f5183 | 2017-10-26 19:25:46 +0200 | [diff] [blame] | 230 | */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 231 | static efi_status_t efi_disk_add_dev( |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 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, |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 238 | unsigned int part, |
| 239 | struct efi_disk_obj **disk) |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 240 | { |
| 241 | struct efi_disk_obj *diskobj; |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 242 | efi_status_t ret; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 243 | |
Alexander Graf | 601db1f | 2016-08-05 14:51:47 +0200 | [diff] [blame] | 244 | /* Don't add empty devices */ |
| 245 | if (!desc->lba) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 246 | return EFI_NOT_READY; |
Alexander Graf | 601db1f | 2016-08-05 14:51:47 +0200 | [diff] [blame] | 247 | |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 248 | diskobj = calloc(1, sizeof(*diskobj)); |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 249 | if (!diskobj) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 250 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 251 | |
| 252 | /* Hook up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 253 | efi_add_handle(&diskobj->header); |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 254 | |
| 255 | /* Fill in object data */ |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 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 | } |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 264 | diskobj->part = part; |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 265 | ret = efi_add_protocol(&diskobj->header, &efi_block_io_guid, |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 266 | &diskobj->ops); |
| 267 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 268 | return ret; |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 269 | ret = efi_add_protocol(&diskobj->header, &efi_guid_device_path, |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 270 | diskobj->dp); |
| 271 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 272 | return ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 273 | if (part >= 1) { |
| 274 | diskobj->volume = efi_simple_file_system(desc, part, |
| 275 | diskobj->dp); |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 276 | ret = efi_add_protocol(&diskobj->header, |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 277 | &efi_simple_file_system_protocol_guid, |
Heinrich Schuchardt | ce721a8 | 2018-01-19 20:24:38 +0100 | [diff] [blame] | 278 | diskobj->volume); |
Heinrich Schuchardt | 9fb0f2f | 2017-11-26 14:05:12 +0100 | [diff] [blame] | 279 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 280 | return ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 281 | } |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 282 | diskobj->ops = block_io_disk_template; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 283 | diskobj->ifname = if_typename; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 284 | diskobj->dev_index = dev_index; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 285 | diskobj->offset = offset; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 286 | diskobj->desc = desc; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 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; |
Alexander Graf | 601db1f | 2016-08-05 14:51:47 +0200 | [diff] [blame] | 293 | diskobj->media.last_block = desc->lba - offset; |
Emmanuel Vadot | 5bf2964 | 2017-12-11 19:22:33 +0100 | [diff] [blame] | 294 | if (part != 0) |
| 295 | diskobj->media.logical_partition = 1; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 296 | diskobj->ops.media = &diskobj->media; |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 297 | if (disk) |
| 298 | *disk = diskobj; |
| 299 | return EFI_SUCCESS; |
Alexander Graf | 06fe57b | 2016-04-11 16:16:17 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 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) |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 315 | { |
| 316 | int disks = 0; |
Alexander Graf | 3491ee7 | 2016-04-11 16:16:20 +0200 | [diff] [blame] | 317 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 318 | disk_partition_t info; |
Jonathan Gray | d35cbd5 | 2017-10-10 13:55:26 +1100 | [diff] [blame] | 319 | int part; |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 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; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 328 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 329 | /* Add devices for each partition */ |
Jonathan Gray | d35cbd5 | 2017-10-10 13:55:26 +1100 | [diff] [blame] | 330 | for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) { |
| 331 | if (part_get_info(desc, part, &info)) |
| 332 | continue; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 333 | snprintf(devname, sizeof(devname), "%s:%d", pdevname, |
| 334 | part); |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 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 | } |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 341 | disks++; |
| 342 | } |
Rob Clark | a83272f | 2017-09-13 18:05:31 -0400 | [diff] [blame] | 343 | |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 344 | return disks; |
| 345 | } |
| 346 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 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 | * |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 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 | * |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 356 | * This gets called from do_bootefi_exec(). |
| 357 | */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 358 | efi_status_t efi_disk_register(void) |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 359 | { |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 360 | struct efi_disk_obj *disk; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 361 | int disks = 0; |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 362 | efi_status_t ret; |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 363 | #ifdef CONFIG_BLK |
| 364 | struct udevice *dev; |
| 365 | |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 366 | for (uclass_first_device_check(UCLASS_BLK, &dev); dev; |
xypron.glpk@gmx.de | 308cd8e | 2017-06-20 19:10:27 +0000 | [diff] [blame] | 367 | uclass_next_device_check(&dev)) { |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 368 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
Heinrich Schuchardt | 6a18bb3 | 2018-01-19 20:24:44 +0100 | [diff] [blame] | 369 | const char *if_typename = blk_get_if_type_name(desc->if_type); |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 370 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 371 | /* Add block device for the full device */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 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 | } |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 384 | disks++; |
| 385 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 386 | /* Partitions show up as block devices in EFI */ |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 387 | disks += efi_disk_create_partitions( |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 388 | &disk->header, desc, if_typename, |
Heinrich Schuchardt | 6f3de6b | 2018-01-19 20:24:47 +0100 | [diff] [blame] | 389 | desc->devnum, dev->name); |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 390 | } |
| 391 | #else |
| 392 | int i, if_type; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 393 | |
| 394 | /* Search for all available disk devices */ |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 395 | for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) { |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 396 | const struct blk_driver *cur_drvr; |
| 397 | const char *if_typename; |
| 398 | |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 399 | cur_drvr = blk_driver_lookup_type(if_type); |
| 400 | if (!cur_drvr) |
| 401 | continue; |
| 402 | |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 403 | if_typename = cur_drvr->if_typename; |
| 404 | printf("Scanning disks on %s...\n", if_typename); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 405 | for (i = 0; i < 4; i++) { |
| 406 | struct blk_desc *desc; |
Alexander Graf | 3491ee7 | 2016-04-11 16:16:20 +0200 | [diff] [blame] | 407 | char devname[32] = { 0 }; /* dp->str is u16[32] long */ |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 408 | |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 409 | desc = blk_get_devnum_by_type(if_type, i); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 410 | if (!desc) |
| 411 | continue; |
| 412 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 413 | continue; |
| 414 | |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 415 | snprintf(devname, sizeof(devname), "%s%d", |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 416 | if_typename, i); |
Rob Clark | 1b2ccbb | 2017-10-08 11:33:08 -0400 | [diff] [blame] | 417 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 418 | /* Add block device for the full device */ |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 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 | } |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 430 | disks++; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 431 | |
Alexander Graf | 83a83f2 | 2017-12-01 16:10:33 +0100 | [diff] [blame] | 432 | /* Partitions show up as block devices in EFI */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 433 | disks += efi_disk_create_partitions |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 434 | (&disk->header, desc, |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 435 | if_typename, i, devname); |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 436 | } |
| 437 | } |
Simon Glass | 302d4f8 | 2016-05-14 14:03:05 -0600 | [diff] [blame] | 438 | #endif |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 439 | printf("Found %d disks\n", disks); |
| 440 | |
Heinrich Schuchardt | cde7a36 | 2018-02-09 20:55:47 +0100 | [diff] [blame] | 441 | return EFI_SUCCESS; |
Alexander Graf | e83be45 | 2016-03-04 01:10:02 +0100 | [diff] [blame] | 442 | } |