David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person |
| 5 | * obtaining a copy of this software and associated documentation |
| 6 | * files (the "Software"), to deal in the Software without |
| 7 | * restriction, including without limitation the rights to use, copy, |
| 8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) |
| 26 | #error "Never include this file directly, include libavb.h instead." |
| 27 | #endif |
| 28 | |
| 29 | #ifndef AVB_OPS_H_ |
| 30 | #define AVB_OPS_H_ |
| 31 | |
| 32 | #include "avb_sysdeps.h" |
| 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /* Return codes used for I/O operations. |
| 39 | * |
| 40 | * AVB_IO_RESULT_OK is returned if the requested operation was |
| 41 | * successful. |
| 42 | * |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 43 | * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk |
| 44 | * or other subsystem) encountered an I/O error. |
| 45 | * |
| 46 | * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory. |
| 47 | * |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 48 | * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested |
| 49 | * partition does not exist. |
| 50 | * |
| 51 | * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the |
| 52 | * range of bytes requested to be read or written is outside the range |
| 53 | * of the partition. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 54 | */ |
| 55 | typedef enum { |
| 56 | AVB_IO_RESULT_OK, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 57 | AVB_IO_RESULT_ERROR_OOM, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 58 | AVB_IO_RESULT_ERROR_IO, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 59 | AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, |
| 60 | AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 61 | } AvbIOResult; |
| 62 | |
| 63 | struct AvbOps; |
| 64 | typedef struct AvbOps AvbOps; |
| 65 | |
| 66 | /* High-level operations/functions/methods that are platform |
| 67 | * dependent. |
| 68 | */ |
| 69 | struct AvbOps { |
| 70 | /* Reads |num_bytes| from offset |offset| from partition with name |
| 71 | * |partition| (NUL-terminated UTF-8 string). If |offset| is |
| 72 | * negative, its absolute value should be interpreted as the number |
| 73 | * of bytes from the end of the partition. |
| 74 | * |
| 75 | * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if |
| 76 | * there is no partition with the given name, |
| 77 | * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested |
| 78 | * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if |
| 79 | * there was an I/O error from the underlying I/O subsystem. If the |
| 80 | * operation succeeds as requested AVB_IO_RESULT_OK is returned and |
| 81 | * the data is available in |buffer|. |
| 82 | * |
| 83 | * The only time partial I/O may occur is if reading beyond the end |
| 84 | * of the partition. In this case the value returned in |
| 85 | * |out_num_read| may be smaller than |num_bytes|. |
| 86 | */ |
| 87 | AvbIOResult (*read_from_partition)(AvbOps* ops, const char* partition, |
| 88 | int64_t offset, size_t num_bytes, |
| 89 | void* buffer, size_t* out_num_read); |
| 90 | |
| 91 | /* Writes |num_bytes| from |bffer| at offset |offset| to partition |
| 92 | * with name |partition| (NUL-terminated UTF-8 string). If |offset| |
| 93 | * is negative, its absolute value should be interpreted as the |
| 94 | * number of bytes from the end of the partition. |
| 95 | * |
| 96 | * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if |
| 97 | * there is no partition with the given name, |
| 98 | * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested |
| 99 | * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO |
| 100 | * if there was an I/O error from the underlying I/O subsystem. If |
| 101 | * the operation succeeds as requested AVB_IO_RESULT_OK is |
| 102 | * returned. |
| 103 | * |
| 104 | * This function never does any partial I/O, it either transfers all |
| 105 | * of the requested bytes or returns an error. |
| 106 | */ |
| 107 | AvbIOResult (*write_to_partition)(AvbOps* ops, const char* partition, |
| 108 | int64_t offset, size_t num_bytes, |
| 109 | const void* buffer); |
| 110 | |
| 111 | /* Checks if the given public key used to sign the 'vbmeta' |
| 112 | * partition is trusted. Boot loaders typically compare this with |
| 113 | * embedded key material generated with 'avbtool |
| 114 | * extract_public_key'. |
| 115 | * |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 116 | * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set - |
| 117 | * true if trusted or false if untrusted. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 118 | */ |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 119 | AvbIOResult (*validate_vbmeta_public_key)(AvbOps* ops, |
| 120 | const uint8_t* public_key_data, |
| 121 | size_t public_key_length, |
| 122 | bool* out_is_trusted); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 123 | |
| 124 | /* Gets the rollback index corresponding to the slot given by |
| 125 | * |rollback_index_slot|. The value is returned in |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 126 | * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback |
| 127 | * index was retrieved, otherwise an error code. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 128 | * |
| 129 | * A device may have a limited amount of rollback index slots (say, |
| 130 | * one or four) so may error out if |rollback_index_slot| exceeds |
| 131 | * this number. |
| 132 | */ |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 133 | AvbIOResult (*read_rollback_index)(AvbOps* ops, size_t rollback_index_slot, |
| 134 | uint64_t* out_rollback_index); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 135 | |
| 136 | /* Sets the rollback index corresponding to the slot given by |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 137 | * |rollback_index_slot| to |rollback_index|. Returns |
| 138 | * AVB_IO_RESULT_OK if the rollback index was set, otherwise an |
| 139 | * error code. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 140 | * |
| 141 | * A device may have a limited amount of rollback index slots (say, |
| 142 | * one or four) so may error out if |rollback_index_slot| exceeds |
| 143 | * this number. |
| 144 | */ |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 145 | AvbIOResult (*write_rollback_index)(AvbOps* ops, size_t rollback_index_slot, |
| 146 | uint64_t rollback_index); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 147 | |
| 148 | /* Gets whether the device is unlocked. The value is returned in |
| 149 | * |out_is_unlocked| (true if unlocked, false otherwise). Returns |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 150 | * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error |
| 151 | * code. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 152 | */ |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 153 | AvbIOResult (*read_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 154 | |
| 155 | /* Gets the unique partition GUID for a partition with name in |
| 156 | * |partition| (NUL-terminated UTF-8 string). The GUID is copied as |
| 157 | * a string into |guid_buf| of size |guid_buf_size| and will be NUL |
| 158 | * terminated. The string must be lower-case and properly |
| 159 | * hyphenated. For example: |
| 160 | * |
| 161 | * 527c1c6d-6361-4593-8842-3c78fcd39219 |
| 162 | * |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 163 | * Returns AVB_IO_RESULT_OK on success, otherwise an error code. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 164 | */ |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 165 | AvbIOResult (*get_unique_guid_for_partition)(AvbOps* ops, |
| 166 | const char* partition, |
| 167 | char* guid_buf, |
| 168 | size_t guid_buf_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #endif /* AVB_OPS_H_ */ |