Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <dumpimage.h> |
| 15 | #include <elf.h> |
| 16 | #include <dirent.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <mtd/mtd-user.h> |
| 19 | #include <limits.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/types.h> |
| 22 | |
Pavithra Palanisamy | a392418 | 2017-10-26 14:43:10 +0530 | [diff] [blame] | 23 | typedef enum {HLOS_TYPE, UBOOT_TYPE, SBL_TYPE, TZ_TYPE, RPM_TYPE, DEVCFG_TYPE}type; |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 24 | |
| 25 | struct image_section { |
| 26 | type section_type; |
| 27 | int max_version; |
| 28 | char file[256]; |
| 29 | char *type; |
| 30 | char tmp_file[256]; |
| 31 | int img_version; |
| 32 | int local_version; |
| 33 | char *version_file; |
| 34 | int is_present; |
Avinash Pandey | bcfcba7 | 2018-01-19 13:07:54 +0530 | [diff] [blame] | 35 | char *img_code; |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 36 | int (*pre_op)(struct image_section *); |
| 37 | int (*get_sw_id)(struct image_section *); |
| 38 | int (*split_components)(struct image_section *, char **, char**, char**); |
| 39 | }; |
| 40 | |
| 41 | typedef struct mbn_header { |
Pavithra Palanisamy | 0e3043f | 2017-09-14 19:45:05 +0530 | [diff] [blame] | 42 | uint32_t image_id; |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 43 | uint32_t ver_num; |
| 44 | uint32_t image_src; |
| 45 | uint8_t *image_dest_ptr; |
| 46 | uint32_t image_size; |
| 47 | uint32_t code_size; |
| 48 | uint8_t *sig_ptr; |
| 49 | uint32_t sig_sz; |
| 50 | uint8_t *cert_ptr; |
| 51 | uint32_t cert_sz; |
| 52 | }Mbn_Hdr; |
| 53 | |
| 54 | struct ubi_ec_hdr { |
| 55 | __be32 magic; |
| 56 | __u8 version; |
| 57 | __u8 padding1[3]; |
| 58 | __be64 ec; /* Warning: the current limit is 31-bit anyway! */ |
| 59 | __be32 vid_hdr_offset; |
| 60 | __be32 data_offset; |
| 61 | __be32 image_seq; |
| 62 | __u8 padding2[32]; |
| 63 | __be32 hdr_crc; |
| 64 | }; |
| 65 | |
| 66 | struct ubi_vid_hdr { |
| 67 | __be32 magic; |
| 68 | __u8 version; |
| 69 | __u8 vol_type; |
| 70 | __u8 copy_flag; |
| 71 | __u8 compat; |
| 72 | __be32 vol_id; |
| 73 | __be32 lnum; |
| 74 | __u8 padding1[4]; |
| 75 | __be32 data_size; |
| 76 | __be32 used_ebs; |
| 77 | __be32 data_pad; |
| 78 | __be32 data_crc; |
| 79 | __u8 padding2[4]; |
| 80 | __be64 sqnum; |
| 81 | __u8 padding3[12]; |
| 82 | __be32 hdr_crc; |
| 83 | }; |
| 84 | |
Ram Chandra Jangir | f40cda9 | 2017-10-16 10:08:42 +0530 | [diff] [blame] | 85 | typedef struct { |
| 86 | uint32_t codeword; |
| 87 | uint32_t magic; |
| 88 | uint32_t RESERVED_0; |
| 89 | uint32_t RESERVED_1; |
| 90 | uint32_t RESERVED_2; |
| 91 | uint32_t image_src; |
| 92 | uint8_t *image_dest_ptr; |
| 93 | uint32_t image_size; |
| 94 | uint32_t code_size; |
| 95 | uint8_t *sig_ptr; |
| 96 | uint32_t sig_size; |
| 97 | uint8_t *cert_ptr; |
| 98 | uint32_t cert_size; |
| 99 | uint32_t root_cert_sel; |
| 100 | uint32_t num_root_certs; |
| 101 | uint32_t RESERVED_5; |
| 102 | uint32_t RESERVED_6; |
| 103 | uint32_t RESERVED_7; |
| 104 | uint32_t RESERVED_8; |
| 105 | uint32_t RESERVED_9; |
| 106 | } Sbl_Hdr; |
| 107 | |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 108 | int get_sections(void); |
| 109 | int is_authentication_check_enabled(void); |
| 110 | int get_local_image_version(struct image_section *); |
| 111 | int set_local_image_version(struct image_section *); |
| 112 | int is_version_check_enabled(void); |
| 113 | int get_sw_id_from_component_bin(struct image_section *); |
| 114 | int get_sw_id_from_component_bin_elf(struct image_section *); |
pavip | fb8e307 | 2017-10-12 14:47:47 +0530 | [diff] [blame] | 115 | int get_sw_id_from_component_bin_elf64(struct image_section *); |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 116 | int extract_kernel_binary(struct image_section *); |
| 117 | int is_image_version_higher(void); |
| 118 | int update_version(void); |
| 119 | int check_image_version(void); |
| 120 | int split_code_signature_cert_from_component_bin(struct image_section *, char **, char **, char **); |
| 121 | int split_code_signature_cert_from_component_bin_elf(struct image_section *, char **, char **, char **); |
pavip | fb8e307 | 2017-10-12 14:47:47 +0530 | [diff] [blame] | 122 | int split_code_signature_cert_from_component_bin_elf64(struct image_section *, char **, char **, char **); |
Sachin Sundar | 1ed5f5b | 2017-03-28 17:34:42 +0530 | [diff] [blame] | 123 | void generate_swid_ipad(char *, unsigned long long *); |
| 124 | void generate_hwid_opad(char *, char *, char *, unsigned long long *); |
| 125 | int generate_hash(char *, char *, char *); |
| 126 | int is_component_authenticated(char *, char *, char *); |
| 127 | int is_image_authenticated(void); |
| 128 | int do_board_upgrade_check(char *); |
Ram Chandra Jangir | 62928dc | 2018-02-01 16:23:46 +0530 | [diff] [blame] | 129 | int check_nand_preamble(uint8_t *); |