1 /*
2  * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "volume.h"
8 
9 #include <errno.h>
10 #include <stddef.h>
11 
volume_init(struct volume * this_volume,const io_dev_funcs_t * io_dev_funcs,uintptr_t concrete_volume)12 void volume_init(struct volume *this_volume, const io_dev_funcs_t *io_dev_funcs,
13 		 uintptr_t concrete_volume)
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 
volume_open(struct volume * this_volume)28 int volume_open(struct volume *this_volume)
29 {
30 	return io_open(this_volume->dev_handle, this_volume->io_spec, &this_volume->io_handle);
31 }
32 
volume_close(struct volume * this_volume)33 int volume_close(struct volume *this_volume)
34 {
35 	return io_close(this_volume->io_handle);
36 }
37 
volume_seek(struct volume * this_volume,io_seek_mode_t mode,signed long long offset)38 int volume_seek(struct volume *this_volume, io_seek_mode_t mode, signed long long offset)
39 {
40 	return io_seek(this_volume->io_handle, mode, offset);
41 }
42 
volume_size(struct volume * this_volume,size_t * size)43 int volume_size(struct volume *this_volume, size_t *size)
44 {
45 	return io_size(this_volume->io_handle, size);
46 }
47 
volume_read(struct volume * this_volume,uintptr_t buffer,size_t length,size_t * length_read)48 int volume_read(struct volume *this_volume, uintptr_t buffer, size_t length, size_t *length_read)
49 {
50 	return io_read(this_volume->io_handle, buffer, length, length_read);
51 }
52 
volume_write(struct volume * this_volume,const uintptr_t buffer,size_t length,size_t * length_written)53 int volume_write(struct volume *this_volume, const uintptr_t buffer, size_t length,
54 		 size_t *length_written)
55 {
56 	return io_write(this_volume->io_handle, buffer, length, length_written);
57 }
58 
volume_erase(struct volume * this_volume)59 int volume_erase(struct volume *this_volume)
60 {
61 	return (this_volume->erase) ? this_volume->erase(this_volume->dev_info.info) : 0;
62 }
63 
volume_get_storage_ids(struct volume * this_volume,struct uuid_octets * partition_guid,struct uuid_octets * parent_guid)64 int volume_get_storage_ids(struct volume *this_volume, struct uuid_octets *partition_guid,
65 			   struct uuid_octets *parent_guid)
66 {
67 	if (this_volume->get_storage_ids)
68 		return this_volume->get_storage_ids(this_volume->dev_info.info, partition_guid,
69 						    parent_guid);
70 
71 	return -EIO;
72 }
73