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 | |
| 23 | typedef enum {HLOS_TYPE, UBOOT_TYPE, SBL_TYPE, TZ_TYPE, RPM_TYPE}type; |
| 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; |
| 35 | int (*pre_op)(struct image_section *); |
| 36 | int (*get_sw_id)(struct image_section *); |
| 37 | int (*split_components)(struct image_section *, char **, char**, char**); |
| 38 | }; |
| 39 | |
| 40 | typedef struct mbn_header { |
| 41 | uint16_t image_id; |
| 42 | uint32_t ver_num; |
| 43 | uint32_t image_src; |
| 44 | uint8_t *image_dest_ptr; |
| 45 | uint32_t image_size; |
| 46 | uint32_t code_size; |
| 47 | uint8_t *sig_ptr; |
| 48 | uint32_t sig_sz; |
| 49 | uint8_t *cert_ptr; |
| 50 | uint32_t cert_sz; |
| 51 | }Mbn_Hdr; |
| 52 | |
| 53 | struct ubi_ec_hdr { |
| 54 | __be32 magic; |
| 55 | __u8 version; |
| 56 | __u8 padding1[3]; |
| 57 | __be64 ec; /* Warning: the current limit is 31-bit anyway! */ |
| 58 | __be32 vid_hdr_offset; |
| 59 | __be32 data_offset; |
| 60 | __be32 image_seq; |
| 61 | __u8 padding2[32]; |
| 62 | __be32 hdr_crc; |
| 63 | }; |
| 64 | |
| 65 | struct ubi_vid_hdr { |
| 66 | __be32 magic; |
| 67 | __u8 version; |
| 68 | __u8 vol_type; |
| 69 | __u8 copy_flag; |
| 70 | __u8 compat; |
| 71 | __be32 vol_id; |
| 72 | __be32 lnum; |
| 73 | __u8 padding1[4]; |
| 74 | __be32 data_size; |
| 75 | __be32 used_ebs; |
| 76 | __be32 data_pad; |
| 77 | __be32 data_crc; |
| 78 | __u8 padding2[4]; |
| 79 | __be64 sqnum; |
| 80 | __u8 padding3[12]; |
| 81 | __be32 hdr_crc; |
| 82 | }; |
| 83 | |
| 84 | int get_sections(void); |
| 85 | int is_authentication_check_enabled(void); |
| 86 | int get_local_image_version(struct image_section *); |
| 87 | int set_local_image_version(struct image_section *); |
| 88 | int is_version_check_enabled(void); |
| 89 | int get_sw_id_from_component_bin(struct image_section *); |
| 90 | int get_sw_id_from_component_bin_elf(struct image_section *); |
| 91 | int extract_kernel_binary(struct image_section *); |
| 92 | int is_image_version_higher(void); |
| 93 | int update_version(void); |
| 94 | int check_image_version(void); |
| 95 | int split_code_signature_cert_from_component_bin(struct image_section *, char **, char **, char **); |
| 96 | int split_code_signature_cert_from_component_bin_elf(struct image_section *, char **, char **, char **); |
| 97 | void generate_swid_ipad(char *, unsigned long long *); |
| 98 | void generate_hwid_opad(char *, char *, char *, unsigned long long *); |
| 99 | int generate_hash(char *, char *, char *); |
| 100 | int is_component_authenticated(char *, char *, char *); |
| 101 | int is_image_authenticated(void); |
| 102 | int do_board_upgrade_check(char *); |