xref: /rk3399_rockchip-uboot/include/android_avb/avb_ops.h (revision 37a7bc39c0a3beca986e8c1c85d1dd6b9921e313)
15b69db07SJason Zhu /*
25b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
35b69db07SJason Zhu  *
45b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
55b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
65b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
75b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
85b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
95b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
105b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
115b69db07SJason Zhu  *
125b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
135b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
145b69db07SJason Zhu  *
155b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
165b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
175b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
185b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
195b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
205b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
215b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
225b69db07SJason Zhu  * SOFTWARE.
235b69db07SJason Zhu  */
245b69db07SJason Zhu 
255b69db07SJason Zhu /*
265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
285b69db07SJason Zhu #endif
295b69db07SJason Zhu */
305b69db07SJason Zhu 
315b69db07SJason Zhu #ifndef AVB_OPS_H_
325b69db07SJason Zhu #define AVB_OPS_H_
335b69db07SJason Zhu 
345b69db07SJason Zhu #include <android_avb/avb_sysdeps.h>
355b69db07SJason Zhu 
365b69db07SJason Zhu #ifdef __cplusplus
375b69db07SJason Zhu extern "C" {
385b69db07SJason Zhu #endif
395b69db07SJason Zhu 
405b69db07SJason Zhu /* Return codes used for I/O operations.
415b69db07SJason Zhu  *
425b69db07SJason Zhu  * AVB_IO_RESULT_OK is returned if the requested operation was
435b69db07SJason Zhu  * successful.
445b69db07SJason Zhu  *
455b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk
465b69db07SJason Zhu  * or other subsystem) encountered an I/O error.
475b69db07SJason Zhu  *
485b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory.
495b69db07SJason Zhu  *
505b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested
515b69db07SJason Zhu  * partition does not exist.
525b69db07SJason Zhu  *
535b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the
545b69db07SJason Zhu  * range of bytes requested to be read or written is outside the range
555b69db07SJason Zhu  * of the partition.
565b69db07SJason Zhu  */
575b69db07SJason Zhu typedef enum {
585b69db07SJason Zhu   AVB_IO_RESULT_OK,
595b69db07SJason Zhu   AVB_IO_RESULT_ERROR_OOM,
605b69db07SJason Zhu   AVB_IO_RESULT_ERROR_IO,
615b69db07SJason Zhu   AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
625b69db07SJason Zhu   AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION
635b69db07SJason Zhu } AvbIOResult;
645b69db07SJason Zhu 
655b69db07SJason Zhu struct AvbOps;
665b69db07SJason Zhu typedef struct AvbOps AvbOps;
675b69db07SJason Zhu 
685b69db07SJason Zhu /* Forward-declaration of operations in libavb_ab. */
695b69db07SJason Zhu struct AvbABOps;
705b69db07SJason Zhu 
715b69db07SJason Zhu /* Forward-declaration of operations in libavb_atx. */
725b69db07SJason Zhu struct AvbAtxOps;
735b69db07SJason Zhu 
745b69db07SJason Zhu /* High-level operations/functions/methods that are platform
755b69db07SJason Zhu  * dependent.
765b69db07SJason Zhu  *
775b69db07SJason Zhu  * Operations may be added in the future so when implementing it
785b69db07SJason Zhu  * always make sure to zero out sizeof(AvbOps) bytes of the struct to
795b69db07SJason Zhu  * ensure that unimplemented operations are set to NULL.
805b69db07SJason Zhu  */
815b69db07SJason Zhu struct AvbOps {
825b69db07SJason Zhu   /* This pointer can be used by the application/bootloader using
835b69db07SJason Zhu    * libavb and is typically used in each operation to get a pointer
845b69db07SJason Zhu    * to platform-specific resources. It cannot be used by libraries.
855b69db07SJason Zhu    */
865b69db07SJason Zhu   void* user_data;
875b69db07SJason Zhu 
885b69db07SJason Zhu   /* If libavb_ab is used, this should point to the
895b69db07SJason Zhu    * AvbABOps. Otherwise it must be set to NULL.
905b69db07SJason Zhu    */
915b69db07SJason Zhu   struct AvbABOps* ab_ops;
925b69db07SJason Zhu 
935b69db07SJason Zhu   /* If libavb_atx is used, this should point to the
945b69db07SJason Zhu    * AvbAtxOps. Otherwise it must be set to NULL.
955b69db07SJason Zhu    */
965b69db07SJason Zhu   struct AvbAtxOps* atx_ops;
975b69db07SJason Zhu 
985b69db07SJason Zhu   /* Reads |num_bytes| from offset |offset| from partition with name
995b69db07SJason Zhu    * |partition| (NUL-terminated UTF-8 string). If |offset| is
1005b69db07SJason Zhu    * negative, its absolute value should be interpreted as the number
1015b69db07SJason Zhu    * of bytes from the end of the partition.
1025b69db07SJason Zhu    *
1035b69db07SJason Zhu    * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
1045b69db07SJason Zhu    * there is no partition with the given name,
1055b69db07SJason Zhu    * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
1065b69db07SJason Zhu    * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
1075b69db07SJason Zhu    * there was an I/O error from the underlying I/O subsystem.  If the
1085b69db07SJason Zhu    * operation succeeds as requested AVB_IO_RESULT_OK is returned and
1095b69db07SJason Zhu    * the data is available in |buffer|.
1105b69db07SJason Zhu    *
1115b69db07SJason Zhu    * The only time partial I/O may occur is if reading beyond the end
1125b69db07SJason Zhu    * of the partition. In this case the value returned in
1135b69db07SJason Zhu    * |out_num_read| may be smaller than |num_bytes|.
1145b69db07SJason Zhu    */
1155b69db07SJason Zhu   AvbIOResult (*read_from_partition)(AvbOps* ops,
1165b69db07SJason Zhu                                      const char* partition,
1175b69db07SJason Zhu                                      int64_t offset,
1185b69db07SJason Zhu                                      size_t num_bytes,
1195b69db07SJason Zhu                                      void* buffer,
1205b69db07SJason Zhu                                      size_t* out_num_read);
1215b69db07SJason Zhu 
122*37a7bc39SJason Zhu   /* Gets the starting pointer of a partition that is pre-loaded in memory, and
123*37a7bc39SJason Zhu    * save it to |out_pointer|. The preloaded partition is expected to be
124*37a7bc39SJason Zhu    * |num_bytes|, where the actual preloaded byte count is returned in
125*37a7bc39SJason Zhu    * |out_num_bytes_preloaded|. |out_num_bytes_preloaded| must be no larger than
126*37a7bc39SJason Zhu    * |num_bytes|.
127*37a7bc39SJason Zhu    *
128*37a7bc39SJason Zhu    * This provides an alternative way to access a partition that is preloaded
129*37a7bc39SJason Zhu    * into memory without a full memory copy. When this function pointer is not
130*37a7bc39SJason Zhu    * set (has value NULL), or when the |out_pointer| is set to NULL as a result,
131*37a7bc39SJason Zhu    * |read_from_partition| will be used as the fallback. This function is mainly
132*37a7bc39SJason Zhu    * used for accessing the entire partition content to calculate its hash.
133*37a7bc39SJason Zhu    *
134*37a7bc39SJason Zhu    * Preloaded partition data must outlive the lifespan of the
135*37a7bc39SJason Zhu    * |AvbSlotVerifyData| structure that |avb_slot_verify| outputs.
136*37a7bc39SJason Zhu    */
137*37a7bc39SJason Zhu   AvbIOResult (*get_preloaded_partition)(AvbOps* ops,
138*37a7bc39SJason Zhu                                          const char* partition,
139*37a7bc39SJason Zhu                                          size_t num_bytes,
140*37a7bc39SJason Zhu                                          uint8_t** out_pointer,
141*37a7bc39SJason Zhu                                          size_t* out_num_bytes_preloaded);
142*37a7bc39SJason Zhu 
1435b69db07SJason Zhu   /* Writes |num_bytes| from |bffer| at offset |offset| to partition
1445b69db07SJason Zhu    * with name |partition| (NUL-terminated UTF-8 string). If |offset|
1455b69db07SJason Zhu    * is negative, its absolute value should be interpreted as the
1465b69db07SJason Zhu    * number of bytes from the end of the partition.
1475b69db07SJason Zhu    *
1485b69db07SJason Zhu    * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
1495b69db07SJason Zhu    * there is no partition with the given name,
1505b69db07SJason Zhu    * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
1515b69db07SJason Zhu    * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
1525b69db07SJason Zhu    * if there was an I/O error from the underlying I/O subsystem.  If
1535b69db07SJason Zhu    * the operation succeeds as requested AVB_IO_RESULT_OK is
1545b69db07SJason Zhu    * returned.
1555b69db07SJason Zhu    *
1565b69db07SJason Zhu    * This function never does any partial I/O, it either transfers all
1575b69db07SJason Zhu    * of the requested bytes or returns an error.
1585b69db07SJason Zhu    */
1595b69db07SJason Zhu   AvbIOResult (*write_to_partition)(AvbOps* ops,
1605b69db07SJason Zhu                                     const char* partition,
1615b69db07SJason Zhu                                     int64_t offset,
1625b69db07SJason Zhu                                     size_t num_bytes,
1635b69db07SJason Zhu                                     const void* buffer);
1645b69db07SJason Zhu 
1655b69db07SJason Zhu   /* Checks if the given public key used to sign the 'vbmeta'
1665b69db07SJason Zhu    * partition is trusted. Boot loaders typically compare this with
1675b69db07SJason Zhu    * embedded key material generated with 'avbtool
1685b69db07SJason Zhu    * extract_public_key'.
1695b69db07SJason Zhu    *
1705b69db07SJason Zhu    * The public key is in the array pointed to by |public_key_data|
1715b69db07SJason Zhu    * and is of |public_key_length| bytes.
1725b69db07SJason Zhu    *
1735b69db07SJason Zhu    * If there is no public key metadata (set with the avbtool option
1745b69db07SJason Zhu    * --public_key_metadata) then |public_key_metadata| will be set to
1755b69db07SJason Zhu    * NULL. Otherwise this field points to the data which is
1765b69db07SJason Zhu    * |public_key_metadata_length| bytes long.
1775b69db07SJason Zhu    *
1785b69db07SJason Zhu    * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
1795b69db07SJason Zhu    * true if trusted or false if untrusted.
1805b69db07SJason Zhu    */
1815b69db07SJason Zhu   AvbIOResult (*validate_vbmeta_public_key)(AvbOps* ops,
1825b69db07SJason Zhu                                             const uint8_t* public_key_data,
1835b69db07SJason Zhu                                             size_t public_key_length,
1845b69db07SJason Zhu                                             const uint8_t* public_key_metadata,
1855b69db07SJason Zhu                                             size_t public_key_metadata_length,
1865b69db07SJason Zhu                                             bool* out_is_trusted);
1875b69db07SJason Zhu 
1885b69db07SJason Zhu   /* Gets the rollback index corresponding to the location given by
1895b69db07SJason Zhu    * |rollback_index_location|. The value is returned in
1905b69db07SJason Zhu    * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
1915b69db07SJason Zhu    * index was retrieved, otherwise an error code.
1925b69db07SJason Zhu    *
1935b69db07SJason Zhu    * A device may have a limited amount of rollback index locations (say,
1945b69db07SJason Zhu    * one or four) so may error out if |rollback_index_location| exceeds
1955b69db07SJason Zhu    * this number.
1965b69db07SJason Zhu    */
1975b69db07SJason Zhu   AvbIOResult (*read_rollback_index)(AvbOps* ops,
1985b69db07SJason Zhu                                      size_t rollback_index_location,
1995b69db07SJason Zhu                                      uint64_t* out_rollback_index);
2005b69db07SJason Zhu 
2015b69db07SJason Zhu   /* Sets the rollback index corresponding to the location given by
2025b69db07SJason Zhu    * |rollback_index_location| to |rollback_index|. Returns
2035b69db07SJason Zhu    * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
2045b69db07SJason Zhu    * error code.
2055b69db07SJason Zhu    *
2065b69db07SJason Zhu    * A device may have a limited amount of rollback index locations (say,
2075b69db07SJason Zhu    * one or four) so may error out if |rollback_index_location| exceeds
2085b69db07SJason Zhu    * this number.
2095b69db07SJason Zhu    */
2105b69db07SJason Zhu   AvbIOResult (*write_rollback_index)(AvbOps* ops,
2115b69db07SJason Zhu                                       size_t rollback_index_location,
2125b69db07SJason Zhu                                       uint64_t rollback_index);
2135b69db07SJason Zhu 
2145b69db07SJason Zhu   /* Gets whether the device is unlocked. The value is returned in
2155b69db07SJason Zhu    * |out_is_unlocked| (true if unlocked, false otherwise). Returns
2165b69db07SJason Zhu    * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
2175b69db07SJason Zhu    * code.
2185b69db07SJason Zhu    */
2195b69db07SJason Zhu   AvbIOResult (*read_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
2205b69db07SJason Zhu 
221*37a7bc39SJason Zhu   /* write the device lock flag. Returns
222*37a7bc39SJason Zhu    * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
223*37a7bc39SJason Zhu    * code.
224*37a7bc39SJason Zhu    */
225*37a7bc39SJason Zhu   AvbIOResult (*write_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
2265b69db07SJason Zhu   /* Gets the unique partition GUID for a partition with name in
2275b69db07SJason Zhu    * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
2285b69db07SJason Zhu    * a string into |guid_buf| of size |guid_buf_size| and will be NUL
2295b69db07SJason Zhu    * terminated. The string must be lower-case and properly
2305b69db07SJason Zhu    * hyphenated. For example:
2315b69db07SJason Zhu    *
2325b69db07SJason Zhu    *  527c1c6d-6361-4593-8842-3c78fcd39219
2335b69db07SJason Zhu    *
2345b69db07SJason Zhu    * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
2355b69db07SJason Zhu    */
2365b69db07SJason Zhu   AvbIOResult (*get_unique_guid_for_partition)(AvbOps* ops,
2375b69db07SJason Zhu                                                const char* partition,
2385b69db07SJason Zhu                                                char* guid_buf,
2395b69db07SJason Zhu                                                size_t guid_buf_size);
2405b69db07SJason Zhu 
2415b69db07SJason Zhu   /* Gets the size of a partition with the name in |partition|
2425b69db07SJason Zhu    * (NUL-terminated UTF-8 string). Returns the value in
2435b69db07SJason Zhu    * |out_size_num_bytes|.
2445b69db07SJason Zhu    *
2455b69db07SJason Zhu    * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
2465b69db07SJason Zhu    */
2475b69db07SJason Zhu   AvbIOResult (*get_size_of_partition)(AvbOps* ops,
2485b69db07SJason Zhu                                        const char* partition,
2495b69db07SJason Zhu                                        uint64_t* out_size_num_bytes);
2505b69db07SJason Zhu };
2515b69db07SJason Zhu 
2525b69db07SJason Zhu #ifdef __cplusplus
2535b69db07SJason Zhu }
2545b69db07SJason Zhu #endif
2555b69db07SJason Zhu 
2565b69db07SJason Zhu #endif /* AVB_OPS_H_ */
257