1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdint.h>
20#include <trusty/interface/storage.h>
21
22#define STORAGE_MAX_NAME_LENGTH_BYTES 159
23
24__BEGIN_DECLS
25
26typedef uint32_t storage_session_t;
27typedef uint64_t file_handle_t;
28typedef uint64_t storage_off_t;
29
30#define STORAGE_INVALID_SESSION ((storage_session_t)-1)
31
32/**
33 * storage_ops_flags - storage related operation flags
34 * @STORAGE_OP_COMPLETE: forces to commit current transaction
35 */
36enum storage_ops_flags {
37    STORAGE_OP_COMPLETE = 0x1,
38};
39
40/**
41 * storage_open_session() - Opens a storage session.
42 * @device:    device node for talking with Trusty
43 * @session_p: pointer to location in which to store session handle
44 *             in case of success.
45 *
46 * Return: 0 on success, or an error code < 0 on failure.
47 */
48int storage_open_session(const char *device, storage_session_t *session_p, const char *port);
49
50/**
51 * storage_close_session() - Closes the session.
52 * @session: the session to close
53 */
54void storage_close_session(storage_session_t session);
55
56/**
57 * storage_open_file() - Opens a file
58 * @session:  the storage_session_t returned from a call to storage_open_session
59 * @handle_p: pointer to location in which to store file handle in case of success
60 * @name:     a null-terminated string identifier of the file to open.
61 *            Cannot be more than STORAGE_MAX_NAME_LENGTH_BYTES in length.
62 * @flags:    A bitmask consisting any storage_file_flag value or'ed together:
63 * - STORAGE_FILE_OPEN_CREATE:           if this file does not exist, create it.
64 * - STORAGE_FILE_OPEN_CREATE_EXCLUSIVE: when specified, opening file with
65 *                                       STORAGE_OPEN_FILE_CREATE flag will
66 *                                       fail if the file already exists.
67 *                                       Only meaningful if used in combination
68 *                                       with STORAGE_FILE_OPEN_CREATE flag.
69 * - STORAGE_FILE_OPEN_TRUNCATE: if this file already exists, discard existing
70 *                               content and open it as a new file. No change
71 *                               in semantics if the  file does not exist.
72 * @opflags: a combination of @storage_op_flags
73 *
74 * Return: 0 on success, or an error code < 0 on failure.
75 */
76int storage_open_file(storage_session_t session, file_handle_t *handle_p,
77                      const char *name, uint32_t flags, uint32_t opflags);
78
79/**
80 * storage_close_file() - Closes a file.
81 * @handle: the file_handle_t retrieved from storage_open_file
82 */
83void storage_close_file(file_handle_t handle);
84
85/**
86 * storage_delete_file - Deletes a file.
87 * @session: the storage_session_t returned from a call to storage_open_session
88 * @name: the name of the file to delete
89 * @opflags: a combination of @storage_op_flags
90 *
91 * Return: 0 on success, or an error code < 0 on failure.
92 */
93int storage_delete_file(storage_session_t session, const char *name,
94                        uint32_t opflags);
95
96/**
97 * storage_read() - Reads a file at a given offset.
98 * @handle: the file_handle_t retrieved from storage_open_file
99 * @off: the start offset from whence to read in the file
100 * @buf: the buffer in which to write the data read
101 * @size: the size of buf and number of bytes to read
102 *
103 * Return: the number of bytes read on success, negative error code on failure
104 */
105ssize_t storage_read(file_handle_t handle,
106                     storage_off_t off, void *buf, size_t size);
107
108/**
109 * storage_write() - Writes to a file at a given offset. Grows the file if necessary.
110 * @handle: the file_handle_t retrieved from storage_open_file
111 * @off: the start offset from whence to write in the file
112 * @buf: the buffer containing the data to write
113 * @size: the size of buf and number of bytes to write
114 * @opflags: a combination of @storage_op_flags
115 *
116 * Return: the number of bytes written on success, negative error code on failure
117 */
118ssize_t storage_write(file_handle_t handle,
119                      storage_off_t off, const void *buf, size_t size,
120                      uint32_t opflags);
121
122/**
123 * storage_set_file_size() - Sets the size of the file.
124 * @handle: the file_handle_t retrieved from storage_open_file
125 * @off: the number of bytes to set as the new size of the file
126 * @opflags: a combination of @storage_op_flags
127 *
128 * Return: 0 on success, negative error code on failure.
129 */
130int storage_set_file_size(file_handle_t handle, storage_off_t file_size,
131                          uint32_t opflags);
132
133/**
134 * storage_get_file_size() - Gets the size of the file.
135 * @session: the storage_session_t returned from a call to storage_open_session
136 * @handle: the file_handle_t retrieved from storage_open_file
137 * @size: pointer to storage_off_t in which to store the file size
138 *
139 * Return: 0 on success, negative error code on failure.
140 */
141int storage_get_file_size(file_handle_t handle, storage_off_t *size);
142
143
144/**
145 * storage_end_transaction: End current transaction
146 * @session: the storage_session_t returned from a call to storage_open_session
147 * @complete: if true, commit current transaction, discard it otherwise
148 *
149 * Return: 0 on success, negative error code on failure.
150 */
151int storage_end_transaction(storage_session_t session, bool complete);
152
153
154__END_DECLS
155