Igor Opaniuk | 29cb886 | 2024-02-09 20:20:40 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2018, Linaro Limited |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _AVB_VERIFY_H |
| 7 | #define _AVB_VERIFY_H |
| 8 | |
| 9 | #include <../lib/libavb/libavb.h> |
Jens Wiklander | 179c1d8 | 2018-09-25 16:40:22 +0200 | [diff] [blame] | 10 | #include <mapmem.h> |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 11 | #include <mmc.h> |
| 12 | |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 13 | #define AVB_MAX_ARGS 1024 |
| 14 | #define VERITY_TABLE_OPT_RESTART "restart_on_corruption" |
| 15 | #define VERITY_TABLE_OPT_LOGGING "ignore_corruption" |
| 16 | #define ALLOWED_BUF_ALIGN 8 |
| 17 | |
| 18 | enum avb_boot_state { |
| 19 | AVB_GREEN, |
| 20 | AVB_YELLOW, |
| 21 | AVB_ORANGE, |
| 22 | AVB_RED, |
| 23 | }; |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 24 | |
| 25 | struct AvbOpsData { |
| 26 | struct AvbOps ops; |
| 27 | int mmc_dev; |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 28 | enum avb_boot_state boot_state; |
Jens Wiklander | 838c70c | 2018-09-25 16:40:20 +0200 | [diff] [blame] | 29 | #ifdef CONFIG_OPTEE_TA_AVB |
| 30 | struct udevice *tee; |
| 31 | u32 session; |
| 32 | #endif |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct mmc_part { |
| 36 | int dev_num; |
| 37 | struct mmc *mmc; |
| 38 | struct blk_desc *mmc_blk; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 39 | struct disk_partition info; |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | enum mmc_io_type { |
| 43 | IO_READ, |
| 44 | IO_WRITE |
| 45 | }; |
| 46 | |
| 47 | AvbOps *avb_ops_alloc(int boot_device); |
| 48 | void avb_ops_free(AvbOps *ops); |
| 49 | |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 50 | char *avb_set_state(AvbOps *ops, enum avb_boot_state boot_state); |
| 51 | char *avb_set_enforce_verity(const char *cmdline); |
| 52 | char *avb_set_ignore_corruption(const char *cmdline); |
| 53 | |
| 54 | char *append_cmd_line(char *cmdline_orig, char *cmdline_new); |
Igor Opaniuk | 66fde11 | 2024-02-09 20:20:43 +0100 | [diff] [blame] | 55 | const char *str_avb_io_error(AvbIOResult res); |
| 56 | const char *str_avb_slot_error(AvbSlotVerifyResult res); |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 57 | /** |
| 58 | * ============================================================================ |
| 59 | * I/O helper inline functions |
| 60 | * ============================================================================ |
| 61 | */ |
| 62 | static inline uint64_t calc_offset(struct mmc_part *part, int64_t offset) |
| 63 | { |
| 64 | u64 part_size = part->info.size * part->info.blksz; |
| 65 | |
| 66 | if (offset < 0) |
| 67 | return part_size + offset; |
| 68 | |
| 69 | return offset; |
| 70 | } |
| 71 | |
| 72 | static inline size_t get_sector_buf_size(void) |
| 73 | { |
Usama Arif | df348b0 | 2020-08-11 15:46:03 +0100 | [diff] [blame] | 74 | return (size_t)CONFIG_AVB_BUF_SIZE; |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static inline void *get_sector_buf(void) |
| 78 | { |
Usama Arif | df348b0 | 2020-08-11 15:46:03 +0100 | [diff] [blame] | 79 | return map_sysmem(CONFIG_AVB_BUF_ADDR, CONFIG_AVB_BUF_SIZE); |
Igor Opaniuk | bb4aac4 | 2018-06-03 21:56:38 +0300 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static inline bool is_buf_unaligned(void *buffer) |
| 83 | { |
| 84 | return (bool)((uintptr_t)buffer % ALLOWED_BUF_ALIGN); |
| 85 | } |
| 86 | |
| 87 | static inline int get_boot_device(AvbOps *ops) |
| 88 | { |
| 89 | struct AvbOpsData *data; |
| 90 | |
| 91 | if (ops) { |
| 92 | data = ops->user_data; |
| 93 | if (data) |
| 94 | return data->mmc_dev; |
| 95 | } |
| 96 | |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | #endif /* _AVB_VERIFY_H */ |