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 <lk/list.h>
20 #include <stdint.h>
21 
22 typedef uint64_t data_block_t;
23 #define DATA_BLOCK_MAX (UINT64_MAX - 1)
24 #define DATA_BLOCK_INVALID (UINT64_MAX)
25 
26 /**
27  * struct block_device - Block device functions and state
28  * @start_read:         Function to start a read operation from block device.
29  * @start_write:        Function to start a write operation to block device.
30  * @wait_for_io:        Function to wait for read or write operations to
31  *                      complete. If @start_read and @start_write always call
32  *                      block_cache_complete_read and block_cache_complete_write
33  *                      this can be %NULL.
34  * @block_count:        Number of blocks in block device.
35  * @block_size:         Number of bytes per block.
36  * @block_num_size:     Number of bytes used to store block numbers.
37  * @mac_size:           Number of bytes used to store mac values.
38  *                      Must be 16 if @tamper_detecting is %false.
39  * @tamper_detecting:   %true if block device detects tampering of read and
40  *                      write opeartions. %false otherwise. If %true, when a
41  *                      write operation is reported as successfully completed
42  *                      it should not be possible for non-secure code to modify
43  *                      the stored data.
44  * @io_ops:             List os active read or write operations. Once
45  *                      initialized, this list is managed by block cache, not by
46  *                      the block device.
47  */
48 struct block_device {
49     void (*start_read)(struct block_device* dev, data_block_t block);
50     void (*start_write)(struct block_device* dev,
51                         data_block_t block,
52                         const void* data,
53                         size_t data_size,
54                         bool sync);
55     void (*wait_for_io)(struct block_device* dev);
56 
57     data_block_t block_count;
58     size_t block_size;
59     size_t block_num_size;
60     size_t mac_size;
61     bool tamper_detecting;
62 
63     struct list_node io_ops;
64 };
65