Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 2 | /* |
| 3 | * efi_selftest_block |
| 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 7 | * This test checks the driver for block IO devices. |
| 8 | * A disk image is created in memory. |
| 9 | * A handle is created for the new block IO device. |
| 10 | * The block I/O protocol is installed on the handle. |
| 11 | * ConnectController is used to setup partitions and to install the simple |
| 12 | * file protocol. |
| 13 | * A known file is read from the file system and verified. |
| 14 | */ |
| 15 | |
| 16 | #include <efi_selftest.h> |
| 17 | #include "efi_selftest_disk_image.h" |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 18 | #include <asm/cache.h> |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 19 | |
| 20 | /* Block size of compressed disk image */ |
| 21 | #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8 |
| 22 | |
| 23 | /* Binary logarithm of the block size */ |
| 24 | #define LB_BLOCK_SIZE 9 |
| 25 | |
| 26 | static struct efi_boot_services *boottime; |
| 27 | |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 28 | static const efi_guid_t block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID; |
| 29 | static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 30 | static const efi_guid_t guid_simple_file_system_protocol = |
| 31 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
Heinrich Schuchardt | b6f15e9 | 2018-04-04 15:42:13 +0200 | [diff] [blame] | 32 | static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 33 | static efi_guid_t guid_vendor = |
| 34 | EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d, |
| 35 | 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xb7, 0xb8); |
| 36 | |
| 37 | static struct efi_device_path *dp; |
| 38 | |
| 39 | /* One 8 byte block of the compressed disk image */ |
| 40 | struct line { |
| 41 | size_t addr; |
| 42 | char *line; |
| 43 | }; |
| 44 | |
| 45 | /* Compressed disk image */ |
| 46 | struct compressed_disk_image { |
| 47 | size_t length; |
| 48 | struct line lines[]; |
| 49 | }; |
| 50 | |
| 51 | static const struct compressed_disk_image img = EFI_ST_DISK_IMG; |
| 52 | |
| 53 | /* Decompressed disk image */ |
| 54 | static u8 *image; |
| 55 | |
| 56 | /* |
| 57 | * Reset service of the block IO protocol. |
| 58 | * |
| 59 | * @this block IO protocol |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 60 | * Return: status code |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 61 | */ |
| 62 | static efi_status_t EFIAPI reset( |
| 63 | struct efi_block_io *this, |
| 64 | char extended_verification) |
| 65 | { |
| 66 | return EFI_SUCCESS; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Read service of the block IO protocol. |
| 71 | * |
| 72 | * @this block IO protocol |
| 73 | * @media_id media id |
| 74 | * @lba start of the read in logical blocks |
| 75 | * @buffer_size number of bytes to read |
| 76 | * @buffer target buffer |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 77 | * Return: status code |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 78 | */ |
| 79 | static efi_status_t EFIAPI read_blocks( |
| 80 | struct efi_block_io *this, u32 media_id, u64 lba, |
| 81 | efi_uintn_t buffer_size, void *buffer) |
| 82 | { |
| 83 | u8 *start; |
| 84 | |
| 85 | if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length) |
| 86 | return EFI_INVALID_PARAMETER; |
| 87 | start = image + (lba << LB_BLOCK_SIZE); |
| 88 | |
| 89 | boottime->copy_mem(buffer, start, buffer_size); |
| 90 | |
| 91 | return EFI_SUCCESS; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Write service of the block IO protocol. |
| 96 | * |
| 97 | * @this block IO protocol |
| 98 | * @media_id media id |
| 99 | * @lba start of the write in logical blocks |
| 100 | * @buffer_size number of bytes to read |
| 101 | * @buffer source buffer |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 102 | * Return: status code |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 103 | */ |
| 104 | static efi_status_t EFIAPI write_blocks( |
| 105 | struct efi_block_io *this, u32 media_id, u64 lba, |
| 106 | efi_uintn_t buffer_size, void *buffer) |
| 107 | { |
| 108 | u8 *start; |
| 109 | |
| 110 | if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length) |
| 111 | return EFI_INVALID_PARAMETER; |
| 112 | start = image + (lba << LB_BLOCK_SIZE); |
| 113 | |
| 114 | boottime->copy_mem(start, buffer, buffer_size); |
| 115 | |
| 116 | return EFI_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Flush service of the block IO protocol. |
| 121 | * |
| 122 | * @this block IO protocol |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 123 | * Return: status code |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 124 | */ |
| 125 | static efi_status_t EFIAPI flush_blocks(struct efi_block_io *this) |
| 126 | { |
| 127 | return EFI_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Decompress the disk image. |
| 132 | * |
| 133 | * @image decompressed disk image |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 134 | * Return: status code |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 135 | */ |
| 136 | static efi_status_t decompress(u8 **image) |
| 137 | { |
| 138 | u8 *buf; |
| 139 | size_t i; |
| 140 | size_t addr; |
| 141 | size_t len; |
| 142 | efi_status_t ret; |
| 143 | |
| 144 | ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length, |
| 145 | (void **)&buf); |
| 146 | if (ret != EFI_SUCCESS) { |
| 147 | efi_st_error("Out of memory\n"); |
| 148 | return ret; |
| 149 | } |
| 150 | boottime->set_mem(buf, img.length, 0); |
| 151 | |
| 152 | for (i = 0; ; ++i) { |
| 153 | if (!img.lines[i].line) |
| 154 | break; |
| 155 | addr = img.lines[i].addr; |
| 156 | len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE; |
| 157 | if (addr + len > img.length) |
| 158 | len = img.length - addr; |
| 159 | boottime->copy_mem(buf + addr, img.lines[i].line, len); |
| 160 | } |
| 161 | *image = buf; |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static struct efi_block_io_media media; |
| 166 | |
| 167 | static struct efi_block_io block_io = { |
| 168 | .media = &media, |
| 169 | .reset = reset, |
| 170 | .read_blocks = read_blocks, |
| 171 | .write_blocks = write_blocks, |
| 172 | .flush_blocks = flush_blocks, |
| 173 | }; |
| 174 | |
| 175 | /* Handle for the block IO device */ |
| 176 | static efi_handle_t disk_handle; |
| 177 | |
| 178 | /* |
| 179 | * Setup unit test. |
| 180 | * |
| 181 | * @handle: handle of the loaded image |
| 182 | * @systable: system table |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 183 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 184 | */ |
| 185 | static int setup(const efi_handle_t handle, |
| 186 | const struct efi_system_table *systable) |
| 187 | { |
| 188 | efi_status_t ret; |
| 189 | struct efi_device_path_vendor vendor_node; |
| 190 | struct efi_device_path end_node; |
| 191 | |
| 192 | boottime = systable->boottime; |
| 193 | |
| 194 | decompress(&image); |
| 195 | |
| 196 | block_io.media->block_size = 1 << LB_BLOCK_SIZE; |
Heinrich Schuchardt | a59b569 | 2021-01-23 19:08:04 +0100 | [diff] [blame] | 197 | block_io.media->last_block = (img.length >> LB_BLOCK_SIZE) - 1; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 198 | |
| 199 | ret = boottime->install_protocol_interface( |
| 200 | &disk_handle, &block_io_protocol_guid, |
| 201 | EFI_NATIVE_INTERFACE, &block_io); |
| 202 | if (ret != EFI_SUCCESS) { |
| 203 | efi_st_error("Failed to install block I/O protocol\n"); |
| 204 | return EFI_ST_FAILURE; |
| 205 | } |
| 206 | |
| 207 | ret = boottime->allocate_pool(EFI_LOADER_DATA, |
| 208 | sizeof(struct efi_device_path_vendor) + |
| 209 | sizeof(struct efi_device_path), |
| 210 | (void **)&dp); |
| 211 | if (ret != EFI_SUCCESS) { |
| 212 | efi_st_error("Out of memory\n"); |
| 213 | return EFI_ST_FAILURE; |
| 214 | } |
| 215 | vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 216 | vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 217 | vendor_node.dp.length = sizeof(struct efi_device_path_vendor); |
| 218 | |
| 219 | boottime->copy_mem(&vendor_node.guid, &guid_vendor, |
| 220 | sizeof(efi_guid_t)); |
| 221 | boottime->copy_mem(dp, &vendor_node, |
| 222 | sizeof(struct efi_device_path_vendor)); |
| 223 | end_node.type = DEVICE_PATH_TYPE_END; |
| 224 | end_node.sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 225 | end_node.length = sizeof(struct efi_device_path); |
| 226 | |
| 227 | boottime->copy_mem((char *)dp + sizeof(struct efi_device_path_vendor), |
| 228 | &end_node, sizeof(struct efi_device_path)); |
| 229 | ret = boottime->install_protocol_interface(&disk_handle, |
| 230 | &guid_device_path, |
| 231 | EFI_NATIVE_INTERFACE, |
| 232 | dp); |
| 233 | if (ret != EFI_SUCCESS) { |
| 234 | efi_st_error("InstallProtocolInterface failed\n"); |
| 235 | return EFI_ST_FAILURE; |
| 236 | } |
| 237 | return EFI_ST_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Tear down unit test. |
| 242 | * |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 243 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 244 | */ |
| 245 | static int teardown(void) |
| 246 | { |
| 247 | efi_status_t r = EFI_ST_SUCCESS; |
| 248 | |
| 249 | if (disk_handle) { |
| 250 | r = boottime->uninstall_protocol_interface(disk_handle, |
| 251 | &guid_device_path, |
| 252 | dp); |
| 253 | if (r != EFI_SUCCESS) { |
| 254 | efi_st_error("Uninstall device path failed\n"); |
| 255 | return EFI_ST_FAILURE; |
| 256 | } |
| 257 | r = boottime->uninstall_protocol_interface( |
| 258 | disk_handle, &block_io_protocol_guid, |
| 259 | &block_io); |
| 260 | if (r != EFI_SUCCESS) { |
Heinrich Schuchardt | a3a4a9c | 2020-01-09 23:26:43 +0100 | [diff] [blame] | 261 | efi_st_error( |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 262 | "Failed to uninstall block I/O protocol\n"); |
Heinrich Schuchardt | a3a4a9c | 2020-01-09 23:26:43 +0100 | [diff] [blame] | 263 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | if (image) { |
Heinrich Schuchardt | 77ff4d8 | 2019-02-12 21:50:45 +0100 | [diff] [blame] | 268 | r = boottime->free_pool(image); |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 269 | if (r != EFI_SUCCESS) { |
| 270 | efi_st_error("Failed to free image\n"); |
| 271 | return EFI_ST_FAILURE; |
| 272 | } |
| 273 | } |
| 274 | return r; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Get length of device path without end tag. |
| 279 | * |
| 280 | * @dp device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 281 | * Return: length of device path in bytes |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 282 | */ |
| 283 | static efi_uintn_t dp_size(struct efi_device_path *dp) |
| 284 | { |
| 285 | struct efi_device_path *pos = dp; |
| 286 | |
| 287 | while (pos->type != DEVICE_PATH_TYPE_END) |
| 288 | pos = (struct efi_device_path *)((char *)pos + pos->length); |
| 289 | return (char *)pos - (char *)dp; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Execute unit test. |
| 294 | * |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 295 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 296 | */ |
| 297 | static int execute(void) |
| 298 | { |
| 299 | efi_status_t ret; |
| 300 | efi_uintn_t no_handles, i, len; |
| 301 | efi_handle_t *handles; |
| 302 | efi_handle_t handle_partition = NULL; |
| 303 | struct efi_device_path *dp_partition; |
Heinrich Schuchardt | a59b569 | 2021-01-23 19:08:04 +0100 | [diff] [blame] | 304 | struct efi_block_io *block_io_protocol; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 305 | struct efi_simple_file_system_protocol *file_system; |
| 306 | struct efi_file_handle *root, *file; |
Heinrich Schuchardt | b6f15e9 | 2018-04-04 15:42:13 +0200 | [diff] [blame] | 307 | struct { |
| 308 | struct efi_file_system_info info; |
| 309 | u16 label[12]; |
| 310 | } system_info; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 311 | efi_uintn_t buf_size; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 312 | char buf[16] __aligned(ARCH_DMA_MINALIGN); |
Heinrich Schuchardt | a59b569 | 2021-01-23 19:08:04 +0100 | [diff] [blame] | 313 | u32 part1_size; |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 314 | u64 pos; |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 315 | |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 316 | /* Connect controller to virtual disk */ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 317 | ret = boottime->connect_controller(disk_handle, NULL, NULL, 1); |
| 318 | if (ret != EFI_SUCCESS) { |
| 319 | efi_st_error("Failed to connect controller\n"); |
| 320 | return EFI_ST_FAILURE; |
| 321 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 322 | |
| 323 | /* Get the handle for the partition */ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 324 | ret = boottime->locate_handle_buffer( |
| 325 | BY_PROTOCOL, &guid_device_path, NULL, |
| 326 | &no_handles, &handles); |
| 327 | if (ret != EFI_SUCCESS) { |
| 328 | efi_st_error("Failed to locate handles\n"); |
| 329 | return EFI_ST_FAILURE; |
| 330 | } |
| 331 | len = dp_size(dp); |
| 332 | for (i = 0; i < no_handles; ++i) { |
| 333 | ret = boottime->open_protocol(handles[i], &guid_device_path, |
| 334 | (void **)&dp_partition, |
| 335 | NULL, NULL, |
| 336 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 337 | if (ret != EFI_SUCCESS) { |
| 338 | efi_st_error("Failed to open device path protocol\n"); |
| 339 | return EFI_ST_FAILURE; |
| 340 | } |
| 341 | if (len >= dp_size(dp_partition)) |
| 342 | continue; |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 343 | if (memcmp(dp, dp_partition, len)) |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 344 | continue; |
| 345 | handle_partition = handles[i]; |
| 346 | break; |
| 347 | } |
| 348 | ret = boottime->free_pool(handles); |
| 349 | if (ret != EFI_SUCCESS) { |
| 350 | efi_st_error("Failed to free pool memory\n"); |
| 351 | return EFI_ST_FAILURE; |
| 352 | } |
| 353 | if (!handle_partition) { |
| 354 | efi_st_error("Partition handle not found\n"); |
| 355 | return EFI_ST_FAILURE; |
| 356 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 357 | |
Heinrich Schuchardt | a59b569 | 2021-01-23 19:08:04 +0100 | [diff] [blame] | 358 | /* Open the block_io_protocol */ |
| 359 | ret = boottime->open_protocol(handle_partition, |
| 360 | &block_io_protocol_guid, |
| 361 | (void **)&block_io_protocol, NULL, NULL, |
| 362 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 363 | if (ret != EFI_SUCCESS) { |
| 364 | efi_st_error("Failed to open block IO protocol\n"); |
| 365 | return EFI_ST_FAILURE; |
| 366 | } |
| 367 | /* Get size of first MBR partition */ |
| 368 | memcpy(&part1_size, image + 0x1ca, sizeof(u32)); |
| 369 | if (block_io_protocol->media->last_block != part1_size - 1) { |
| 370 | efi_st_error("Last LBA of partition %x, expected %x\n", |
| 371 | (unsigned int)block_io_protocol->media->last_block, |
| 372 | part1_size - 1); |
| 373 | return EFI_ST_FAILURE; |
| 374 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 375 | /* Open the simple file system protocol */ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 376 | ret = boottime->open_protocol(handle_partition, |
| 377 | &guid_simple_file_system_protocol, |
| 378 | (void **)&file_system, NULL, NULL, |
| 379 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 380 | if (ret != EFI_SUCCESS) { |
| 381 | efi_st_error("Failed to open simple file system protocol\n"); |
| 382 | return EFI_ST_FAILURE; |
| 383 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 384 | |
| 385 | /* Open volume */ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 386 | ret = file_system->open_volume(file_system, &root); |
| 387 | if (ret != EFI_SUCCESS) { |
| 388 | efi_st_error("Failed to open volume\n"); |
| 389 | return EFI_ST_FAILURE; |
| 390 | } |
Heinrich Schuchardt | b6f15e9 | 2018-04-04 15:42:13 +0200 | [diff] [blame] | 391 | buf_size = sizeof(system_info); |
| 392 | ret = root->getinfo(root, &guid_file_system_info, &buf_size, |
| 393 | &system_info); |
| 394 | if (ret != EFI_SUCCESS) { |
| 395 | efi_st_error("Failed to get file system info\n"); |
| 396 | return EFI_ST_FAILURE; |
| 397 | } |
| 398 | if (system_info.info.block_size != 512) { |
| 399 | efi_st_error("Wrong block size %u, expected 512\n", |
| 400 | system_info.info.block_size); |
| 401 | return EFI_ST_FAILURE; |
| 402 | } |
| 403 | if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) { |
| 404 | efi_st_todo( |
| 405 | "Wrong volume label '%ps', expected 'U-BOOT TEST'\n", |
| 406 | system_info.info.volume_label); |
| 407 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 408 | |
| 409 | /* Read file */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 410 | ret = root->open(root, &file, u"hello.txt", EFI_FILE_MODE_READ, |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 411 | 0); |
| 412 | if (ret != EFI_SUCCESS) { |
| 413 | efi_st_error("Failed to open file\n"); |
| 414 | return EFI_ST_FAILURE; |
| 415 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 416 | ret = file->setpos(file, 1); |
| 417 | if (ret != EFI_SUCCESS) { |
| 418 | efi_st_error("SetPosition failed\n"); |
| 419 | return EFI_ST_FAILURE; |
| 420 | } |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 421 | buf_size = sizeof(buf) - 1; |
| 422 | ret = file->read(file, &buf_size, buf); |
| 423 | if (ret != EFI_SUCCESS) { |
| 424 | efi_st_error("Failed to read file\n"); |
| 425 | return EFI_ST_FAILURE; |
| 426 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 427 | if (buf_size != 12) { |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 428 | efi_st_error("Wrong number of bytes read: %u\n", |
| 429 | (unsigned int)buf_size); |
| 430 | return EFI_ST_FAILURE; |
| 431 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 432 | if (memcmp(buf, "ello world!", 11)) { |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 433 | efi_st_error("Unexpected file content\n"); |
| 434 | return EFI_ST_FAILURE; |
| 435 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 436 | ret = file->getpos(file, &pos); |
| 437 | if (ret != EFI_SUCCESS) { |
| 438 | efi_st_error("GetPosition failed\n"); |
| 439 | return EFI_ST_FAILURE; |
| 440 | } |
| 441 | if (pos != 13) { |
| 442 | efi_st_error("GetPosition returned %u, expected 13\n", |
| 443 | (unsigned int)pos); |
| 444 | return EFI_ST_FAILURE; |
| 445 | } |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 446 | ret = file->close(file); |
| 447 | if (ret != EFI_SUCCESS) { |
| 448 | efi_st_error("Failed to close file\n"); |
| 449 | return EFI_ST_FAILURE; |
| 450 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 451 | |
| 452 | #ifdef CONFIG_FAT_WRITE |
| 453 | /* Write file */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 454 | ret = root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ | |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 455 | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0); |
| 456 | if (ret != EFI_SUCCESS) { |
| 457 | efi_st_error("Failed to open file\n"); |
| 458 | return EFI_ST_FAILURE; |
| 459 | } |
| 460 | buf_size = 7; |
| 461 | boottime->set_mem(buf, sizeof(buf), 0); |
| 462 | boottime->copy_mem(buf, "U-Boot", buf_size); |
| 463 | ret = file->write(file, &buf_size, buf); |
| 464 | if (ret != EFI_SUCCESS || buf_size != 7) { |
| 465 | efi_st_error("Failed to write file\n"); |
| 466 | return EFI_ST_FAILURE; |
| 467 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 468 | ret = file->getpos(file, &pos); |
| 469 | if (ret != EFI_SUCCESS) { |
| 470 | efi_st_error("GetPosition failed\n"); |
| 471 | return EFI_ST_FAILURE; |
| 472 | } |
| 473 | if (pos != 7) { |
| 474 | efi_st_error("GetPosition returned %u, expected 7\n", |
| 475 | (unsigned int)pos); |
| 476 | return EFI_ST_FAILURE; |
| 477 | } |
Heinrich Schuchardt | d788445 | 2018-12-22 00:37:41 +0100 | [diff] [blame] | 478 | ret = file->close(file); |
| 479 | if (ret != EFI_SUCCESS) { |
| 480 | efi_st_error("Failed to close file\n"); |
| 481 | return EFI_ST_FAILURE; |
| 482 | } |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 483 | |
| 484 | /* Verify file */ |
| 485 | boottime->set_mem(buf, sizeof(buf), 0); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 486 | ret = root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ, |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 487 | 0); |
| 488 | if (ret != EFI_SUCCESS) { |
| 489 | efi_st_error("Failed to open file\n"); |
| 490 | return EFI_ST_FAILURE; |
| 491 | } |
| 492 | buf_size = sizeof(buf) - 1; |
| 493 | ret = file->read(file, &buf_size, buf); |
| 494 | if (ret != EFI_SUCCESS) { |
| 495 | efi_st_error("Failed to read file\n"); |
| 496 | return EFI_ST_FAILURE; |
| 497 | } |
| 498 | if (buf_size != 7) { |
| 499 | efi_st_error("Wrong number of bytes read: %u\n", |
| 500 | (unsigned int)buf_size); |
| 501 | return EFI_ST_FAILURE; |
| 502 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 503 | if (memcmp(buf, "U-Boot", 7)) { |
Heinrich Schuchardt | 76bc73c | 2018-07-02 02:41:24 +0200 | [diff] [blame] | 504 | efi_st_error("Unexpected file content %s\n", buf); |
| 505 | return EFI_ST_FAILURE; |
| 506 | } |
| 507 | ret = file->close(file); |
| 508 | if (ret != EFI_SUCCESS) { |
| 509 | efi_st_error("Failed to close file\n"); |
| 510 | return EFI_ST_FAILURE; |
| 511 | } |
| 512 | #else |
| 513 | efi_st_todo("CONFIG_FAT_WRITE is not set\n"); |
| 514 | #endif /* CONFIG_FAT_WRITE */ |
| 515 | |
| 516 | /* Close volume */ |
Heinrich Schuchardt | d322f92 | 2018-01-21 19:29:31 +0100 | [diff] [blame] | 517 | ret = root->close(root); |
| 518 | if (ret != EFI_SUCCESS) { |
| 519 | efi_st_error("Failed to close volume\n"); |
| 520 | return EFI_ST_FAILURE; |
| 521 | } |
| 522 | |
| 523 | return EFI_ST_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | EFI_UNIT_TEST(blkdev) = { |
| 527 | .name = "block device", |
| 528 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 529 | .setup = setup, |
| 530 | .execute = execute, |
| 531 | .teardown = teardown, |
| 532 | }; |