Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stddef.h> |
| 9 | #include "volume.h" |
| 10 | |
| 11 | void volume_init( |
| 12 | struct volume *this_volume, |
| 13 | const io_dev_funcs_t *io_dev_funcs, |
| 14 | uintptr_t concrete_volume) |
| 15 | { |
| 16 | this_volume->dev_info.funcs = io_dev_funcs; |
| 17 | this_volume->dev_info.info = concrete_volume; |
| 18 | |
| 19 | this_volume->dev_handle = (uintptr_t)&this_volume->dev_info; |
| 20 | this_volume->io_spec = concrete_volume; |
| 21 | |
| 22 | this_volume->io_handle = 0; |
| 23 | |
| 24 | /* Optional functions that a concrete volume may provide */ |
| 25 | this_volume->erase = NULL; |
| 26 | this_volume->get_storage_ids = NULL; |
| 27 | } |
| 28 | |
| 29 | int volume_open( |
| 30 | struct volume *this_volume) |
| 31 | { |
| 32 | return io_open( |
| 33 | this_volume->dev_handle, |
| 34 | this_volume->io_spec, |
| 35 | &this_volume->io_handle); |
| 36 | } |
| 37 | |
| 38 | int volume_close( |
| 39 | struct volume *this_volume) |
| 40 | { |
| 41 | return io_close(this_volume->io_handle); |
| 42 | } |
| 43 | |
| 44 | int volume_seek( |
| 45 | struct volume *this_volume, |
| 46 | io_seek_mode_t mode, |
| 47 | signed long long offset) |
| 48 | { |
| 49 | return io_seek(this_volume->io_handle, mode, offset); |
| 50 | } |
| 51 | |
| 52 | int volume_size( |
| 53 | struct volume *this_volume, |
| 54 | size_t *size) |
| 55 | { |
| 56 | return io_size(this_volume->io_handle, size); |
| 57 | } |
| 58 | |
| 59 | int volume_read( |
| 60 | struct volume *this_volume, |
| 61 | uintptr_t buffer, |
| 62 | size_t length, |
| 63 | size_t *length_read) |
| 64 | { |
| 65 | return io_read(this_volume->io_handle, buffer, length, length_read); |
| 66 | } |
| 67 | |
| 68 | int volume_write( |
| 69 | struct volume *this_volume, |
| 70 | const uintptr_t buffer, |
| 71 | size_t length, |
| 72 | size_t *length_written) |
| 73 | { |
| 74 | return io_write(this_volume->io_handle, buffer, length, length_written); |
| 75 | } |
| 76 | |
| 77 | int volume_erase( |
| 78 | struct volume *this_volume) |
| 79 | { |
| 80 | return (this_volume->erase) ? |
| 81 | this_volume->erase(this_volume->dev_info.info) : 0; |
| 82 | } |
| 83 | |
| 84 | int volume_get_storage_ids( |
| 85 | struct volume *this_volume, |
| 86 | struct uuid_octets *partition_guid, |
| 87 | struct uuid_octets *parent_guid) |
| 88 | { |
| 89 | if (this_volume->get_storage_ids) |
| 90 | return this_volume->get_storage_ids( |
| 91 | this_volume->dev_info.info, |
| 92 | partition_guid, parent_guid); |
| 93 | |
| 94 | return -EIO; |
| 95 | } |