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 | |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 7 | #include "volume.h" |
| 8 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <stddef.h> |
| 11 | |
| 12 | void volume_init(struct volume *this_volume, const io_dev_funcs_t *io_dev_funcs, |
| 13 | uintptr_t concrete_volume) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 14 | { |
| 15 | this_volume->dev_info.funcs = io_dev_funcs; |
| 16 | this_volume->dev_info.info = concrete_volume; |
| 17 | |
| 18 | this_volume->dev_handle = (uintptr_t)&this_volume->dev_info; |
| 19 | this_volume->io_spec = concrete_volume; |
| 20 | |
| 21 | this_volume->io_handle = 0; |
| 22 | |
| 23 | /* Optional functions that a concrete volume may provide */ |
| 24 | this_volume->erase = NULL; |
| 25 | this_volume->get_storage_ids = NULL; |
| 26 | } |
| 27 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 28 | int volume_open(struct volume *this_volume) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 29 | { |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 30 | return io_open(this_volume->dev_handle, this_volume->io_spec, &this_volume->io_handle); |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 33 | int volume_close(struct volume *this_volume) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 34 | { |
| 35 | return io_close(this_volume->io_handle); |
| 36 | } |
| 37 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 38 | int volume_seek(struct volume *this_volume, io_seek_mode_t mode, signed long long offset) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 39 | { |
| 40 | return io_seek(this_volume->io_handle, mode, offset); |
| 41 | } |
| 42 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 43 | int volume_size(struct volume *this_volume, size_t *size) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 44 | { |
| 45 | return io_size(this_volume->io_handle, size); |
| 46 | } |
| 47 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 48 | int volume_read(struct volume *this_volume, uintptr_t buffer, size_t length, size_t *length_read) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 49 | { |
| 50 | return io_read(this_volume->io_handle, buffer, length, length_read); |
| 51 | } |
| 52 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 53 | int volume_write(struct volume *this_volume, const uintptr_t buffer, size_t length, |
| 54 | size_t *length_written) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 55 | { |
| 56 | return io_write(this_volume->io_handle, buffer, length, length_written); |
| 57 | } |
| 58 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 59 | int volume_erase(struct volume *this_volume) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 60 | { |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 61 | return (this_volume->erase) ? this_volume->erase(this_volume->dev_info.info) : 0; |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 64 | int volume_get_storage_ids(struct volume *this_volume, struct uuid_octets *partition_guid, |
| 65 | struct uuid_octets *parent_guid) |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 66 | { |
| 67 | if (this_volume->get_storage_ids) |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 68 | return this_volume->get_storage_ids(this_volume->dev_info.info, partition_guid, |
| 69 | parent_guid); |
Julian Hall | 4a658ac | 2022-10-20 10:44:49 +0100 | [diff] [blame] | 70 | |
| 71 | return -EIO; |
| 72 | } |