├── Android.mk ├── README ├── lib ├── api │ ├── bootimage.c │ ├── bootimage_extract.c │ ├── bootimage_file.c │ ├── bootimage_file_extract.c │ ├── bootimage_file_print.c │ ├── bootimage_print.c │ └── bootimage_utils.c ├── include │ ├── api │ │ ├── bootimage.h │ │ ├── bootimage_extract.h │ │ ├── bootimage_file_extract.h │ │ ├── bootimage_file_print.h │ │ ├── bootimage_print.h │ │ ├── bootimage_utils.h │ │ ├── errors.h │ │ └── utils.h │ ├── biutils.h │ └── private │ │ ├── api.h │ │ ├── archive.h │ │ ├── bootimage.h │ │ ├── bootimage_utils.h │ │ ├── checks.h │ │ ├── errors.h │ │ ├── factory_images.h │ │ ├── kernel.h │ │ ├── print.h │ │ ├── trace.h │ │ └── utils.h └── private │ ├── archive.c │ ├── bootimage.c │ ├── bootimage_utils.c │ ├── checks.c │ ├── kernel.c │ ├── print.c │ ├── trace.c │ └── utils.c ├── src ├── bootimage_utils_options.c ├── bootimage_utils_private.h ├── file.c ├── help.c ├── main.c ├── print_header.c └── strings.h ├── test1.c └── windows ├── memmem.c ├── mman.c ├── mman.vcproj └── sys └── mman.h /Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | libbootimage_src_files := \ 4 | lib/api/bootimage.c \ 5 | lib/api/bootimage_extract.c \ 6 | lib/api/bootimage_file_extract.c \ 7 | lib/api/bootimage_print.c \ 8 | lib/api/bootimage_file_print.c \ 9 | lib/api/bootimage_utils.c \ 10 | lib/private/bootimage.c \ 11 | lib/private/checks.c \ 12 | lib/private/archive.c \ 13 | lib/private/trace.c \ 14 | lib/private/print.c \ 15 | lib/private/kernel.c \ 16 | lib/private/utils.c 17 | 18 | libbootimage_c_includes := $(LOCAL_PATH)/lib/include 19 | 20 | bootimageutils_src_files := \ 21 | src/main.c 22 | 23 | 24 | 25 | include $(CLEAR_VARS) 26 | LOCAL_MODULE := libbootimage 27 | LOCAL_MODULE_TAGS := optional 28 | LOCAL_CFLAGS := -D_GNU_SOURCE -fvisibility=hidden 29 | LOCAL_SRC_FILES := $(libbootimage_src_files) 30 | LOCAL_C_INCLUDES := $(libbootimage_c_includes) 31 | LOCAL_WHOLE_STATIC_LIBRARIES := libarchive 32 | LOCAL_EXPORT_C_INCLUDE_DIRS := $(libbootimage_c_includes) 33 | $(info LOCAL_C_INCLUDES $(LOCAL_C_INCLUDES)) 34 | include $(BUILD_HOST_STATIC_LIBRARY) 35 | 36 | 37 | include $(CLEAR_VARS) 38 | LOCAL_MODULE := libbootimage 39 | LOCAL_MODULE_TAGS := optional 40 | LOCAL_CFLAGS := -D_GNU_SOURCE -fvisibility=hidden 41 | LOCAL_WHOLE_STATIC_LIBRARIES := libbootimage 42 | LOCAL_EXPORT_C_INCLUDE_DIRS := $(libbootimage_c_includes) 43 | $(info LOCAL_C_INCLUDES $(LOCAL_C_INCLUDES)) 44 | include $(BUILD_HOST_SHARED_LIBRARY) 45 | 46 | include $(CLEAR_VARS) 47 | LOCAL_MODULE := bootimage-utils 48 | LOCAL_MODULE_TAGS := optional 49 | LOCAL_SRC_FILES := $(bootimageutils_src_files) 50 | LOCAL_STATIC_LIBRARIES := libbootimage 51 | LOCAL_C_INCLUDES := $(libbootimage_c_includes) 52 | LOCAL_FORCE_STATIC_EXECUTABLE := true 53 | include $(BUILD_HOST_EXECUTABLE) 54 | 55 | # Do not build target binaries if we are not targeting linux 56 | # on the host 57 | ifeq ($(HOST_OS),linux) 58 | 59 | include $(CLEAR_VARS) 60 | LOCAL_MODULE := libbootimage 61 | LOCAL_CFLAGS := -fvisibility=hidden 62 | LOCAL_MODULE_TAGS := optional 63 | LOCAL_SHARED_LIBRARIES := libarchive 64 | LOCAL_SRC_FILES := $(libbootimage_src_files) 65 | LOCAL_C_INCLUDES := $(libbootimage_c_includes) 66 | LOCAL_EXPORT_C_INCLUDE_DIRS := $(libbootimage_c_includes) 67 | include $(BUILD_SHARED_LIBRARY) 68 | 69 | 70 | 71 | include $(CLEAR_VARS) 72 | LOCAL_MODULE := bootimage-utils-test1 73 | LOCAL_MODULE_TAGS := optional 74 | LOCAL_SRC_FILES := test1.c 75 | LOCAL_SHARED_LIBRARIES := libbootimage 76 | #include $(BUILD_EXECUTABLE) 77 | 78 | endif 79 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | libbootimage api 2 | 3 | 4 | The Bootimage Utils Functions 5 | ============================= 6 | 7 | enum bootimage_utils_filetype 8 | { 9 | BOOTIMAGE_UTILS_FILETYPE_UNKNOWN = 0, 10 | BOOTIMAGE_UTILS_FILETYPE_NEXUS_FACTORY_IMAGE, 11 | BOOTIMAGE_UTILS_FILETYPE_UPDATE_ZIP, 12 | BOOTIMAGE_UTILS_FILETYPE_OTA_UPDATE_ZIP, 13 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_BOOT_IMAGE, 14 | BOOTIMAGE_UTILS_FILETYPE_OEM_BOOT_IMAGE, 15 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_RECOVERY_IMAGE, 16 | BOOTIMAGE_UTILS_FILETYPE_OEM_RECOVERY_IMAGE, 17 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_RAMDISK, 18 | BOOTIMAGE_UTILS_FILETYPE_RECOVERY_RAMDISK, 19 | BOOTIMAGE_UTILS_FILETYPE_COMPRESSED_KERNEL, 20 | BOOTIMAGE_UTILS_FILETYPE_MAX = BOOTIMAGE_UTILS_FILETYPE_COMPRESSED_KERNEL 21 | } 22 | 23 | struct bootimage_utils 24 | { 25 | int file_type ; 26 | char* data ; 27 | } 28 | 29 | struct bootimage_utils* bootimage_utils_initialize(); 30 | bootimage_utils_read(struct bootimage_utils* biu,const char* file_name); 31 | 32 | 33 | 34 | 35 | 36 | The Main Bootimage Functions 37 | ============================ 38 | 39 | struct bootimage* bootimage_initialize(); 40 | struct bootimage* bootimage_initialize_from_factory_image(char* data); 41 | struct bootimage* bootimage_initialize_from_ota_zip(char* data); 42 | bootimage_free(struct bootimage** bip); 43 | bootimage_file_read(struct bootimage* bi,const char* file_name) 44 | 45 | 46 | The Main Bootimage File Extraction Functions 47 | ============================================ 48 | 49 | 50 | 51 | bootimage_file_extract_header(const char* file_name,const char* header_name); 52 | bootimage_file_extract_header_block(const char* file_name,const char* header_block_name); 53 | bootimage_file_extract_kernel(const char* file_name,const char* kernel_name); 54 | bootimage_file_extract_kernel_config(const char* file_name,const char* kernel_config_name); 55 | bootimage_file_extract_kernel_config_gz(const char* file_name,const char* kernel_config_gz_name); 56 | bootimage_file_extract_kernel_ramdisk(const char* file_name,const char* kernel_ramdisk_name); 57 | bootimage_file_extract_kernel_ramdisk_archive(const char* file_name,const char* kernel_ramdisk_dir_name); 58 | bootimage_file_extract_kernel_ramdisk_entry(const char* file_name,const char* kernel_ramdisk_entry_name); 59 | bootimage_file_extract_uncompressed_kernel(const char* file_name,const char* uncompressed_kernel_name); 60 | bootimage_file_extract_ramdisk(const char* file_name,const char* ramdisk_name); 61 | bootimage_file_extract_ramdisk_archive(const char* file_name,const char* ramdisk_dir_name); 62 | bootimage_file_extract_ramdisk_entry(const char* file_name,const char* ramdisk_entry_name); 63 | 64 | 65 | 66 | bootimage_file_print_header(const char* file_name); 67 | bootimage_file_print_header_fd(const char* file_name,int fd); 68 | bootimage_file_print_kernel(const char* file_name); 69 | bootimage_file_print_kernel_version(const char* file_name); 70 | bootimage_file_print_ramdisk(const char* file_name); 71 | bootimage_file_print_ramdisk_list(const char* file_name); 72 | bootimage_file_print_ramdisk_file(const char* file_name,const char* ramdisk_file); 73 | bootimage_file_print_ramdisk_files(const char* file_name,const char** ramdisk_files); 74 | bootimage_file_print_all(const char* file_name); 75 | 76 | bootimage_extract_header(struct bootimage* bi,const char* header_name); 77 | bootimage_extract_header_block(struct bootimage* bi,const char* header_block_name); 78 | bootimage_extract_kernel(struct bootimage* bi,const char* kernel_name); 79 | bootimage_extract_kernel_config(struct bootimage* bi,const char* kernel_config_name); 80 | bootimage_extract_kernel_config_gz(struct bootimage* bi,const char* kernel_config_gz_name); 81 | bootimage_extract_kernel_ramdisk(struct bootimage* bi,const char* kernel_ramdisk_name); 82 | bootimage_extract_kernel_ramdisk_archive(struct bootimage* bi,const char* kernel_ramdisk_dir_name); 83 | bootimage_extract_kernel_ramdisk_entry(struct bootimage* bi,const char* kernel_ramdisk_entry_name); 84 | bootimage_extract_uncompressed_kernel(struct bootimage* bi,const char* uncompressed_kernel_name); 85 | bootimage_extract_ramdisk(struct bootimage* bi,const char* ramdisk_name); 86 | bootimage_extract_ramdisk_archive(struct bootimage* bi,const char* ramdisk_dir_name); 87 | bootimage_extract_ramdisk_entry(struct bootimage* bi,const char* ramdisk_entry_name); 88 | 89 | 90 | bootimage-utils features 91 | 92 | Print Bootimage Image Header 93 | Print Bootimage Image File Structure 94 | Print Bootimage Type 95 | Fingerprint BootImage 96 | 97 | Kernel Compression 98 | Kernel Version 99 | Kernel Uncompressed Size 100 | Kernel Config.gz 101 | 102 | List Properties 103 | List Services 104 | 105 | Add/Change Service 106 | Add/Change Property 107 | 108 | 109 | Mount Ramdisk 110 | 111 | usage 112 | 113 | bootimage-utils extract [ options ] 114 | 115 | options: 116 | -i --bootimage= [ default boot.img ] 117 | -d --out-dir - output directory [ default : cwd ] 118 | -k --kernel= [ default : kernel ] 119 | -r --ramdisk= [ default : ramdisk ] 120 | -x --ramdisk-archive= 121 | -f --ramdisk-file= 122 | -o --output-file= 123 | -F --ramdisk-files=; 124 | -O --output-file=; 125 | -h --header 126 | - 127 | 128 | 129 | bootimage-utils features technical implementation 130 | ================================================= 131 | 132 | Known FileTypes 133 | 134 | Nexus Factory Image 135 | file contains a zip file and the zip file contains 136 | at least a boot.img 137 | 138 | Update Zip 139 | file contains a META-INF and has an updater-script 140 | 141 | OTA Update Zip ( incremental ) 142 | file contains a META-INF and has an updater-script 143 | 144 | Standard Boot Image 145 | Has Android Magic at offset 0. 146 | Has Valid Kernel Data 147 | Has Valid Ramdisk Data 148 | Does not have sbin/recovery 149 | default.prop does not contain build.prop info 150 | 151 | OEM Boot Image 152 | Has Android Magic not at offset 0 153 | Has Valid Kernel Data 154 | Has Valid Ramdisk Data 155 | Does not have sbin/recovery 156 | default.prop does not contain build.prop info 157 | 158 | Standard Recovery Image 159 | Has Android Magic at offset 0. 160 | Has sbin/recovery 161 | default.prop contains build.prop info 162 | 163 | OEM Recovery Image 164 | Has Android Magic at offset 0. 165 | Has sbin/recovery 166 | default.prop contains build.prop info 167 | 168 | Standard Ramdisk 169 | Is a cpio or cpio.gz file 170 | Does not have sbin/recovery 171 | default.prop does not contain build.prop info 172 | 173 | Recovery Ramdisk 174 | Is a cpio or cpio.gz file 175 | has sbin/recovery 176 | default.prop contains build.prop info 177 | 178 | Compressed Kernel 179 | Is a valid Kernel Compression Type 180 | 181 | UnCompressed Kernel 182 | Has a valid kernel Version Line 183 | Has ( other ) kernel identifiers 184 | 185 | 186 | Bootimage Utils Options 187 | ======================= 188 | 189 | The options control how the program behaves with regard to 190 | memory usage and allocation strategy, Temporary File Usage 191 | default action behaviour. Search Path Behaviour, Logging Options 192 | Clobber Behaviour. These can be set in an "options" file or passed 193 | as command line arguments 194 | 195 | Top Level Actions 196 | ================= 197 | info - Print Various Bootimage or archive info 198 | print - same as info 199 | extract - Extract various parts of a bootimage 200 | update - update various parts of a bootimage 201 | create - create a bootimage 202 | options - print default options 203 | 204 | 205 | Print Action 206 | ============ 207 | 208 | bootimage-utils [print|p] [ options ] [ program options ] < file > 209 | 210 | -B |--boot-image= ( default : boot.img ) 211 | If the is a nexus factory image or an update zip 212 | and contains a file named then the data is printed 213 | from 214 | 215 | -R |--recovery-image= ( default : recovery.img ) 216 | If the is a nexus factory image or an update zip 217 | and contains a file named then the data is printed 218 | from this, recovery images will be attempted to be fingerprinted 219 | 220 | -H|--header 221 | Prints the image header information 222 | 223 | -k|--kernel 224 | Prints the image kernel information 225 | 226 | -K|--kernel-version 227 | Prints the kernel version line only 228 | 229 | -r|--ramdisk 230 | Prints ramdisk information 231 | 232 | -f|--file= 233 | Print ramdisk file. if the file is a text file 234 | 235 | -P|--property= 236 | Print property value found in default.prop 237 | 238 | -L|--list-ramdisk 239 | 240 | Examples 241 | bootimage-utils print 242 | Prints File Type " Android Nexus Factory Image " 243 | Prints Internal Zip File Name / Zip File Size 244 | Prints Internal Zip Boot Image Name and size 245 | Prints boot.img header structure 246 | 247 | 248 | Second Level Actions 249 | ==================== 250 | These are quick shortcuts to actions arguments 251 | 252 | print-header 253 | print-bootimage-header 254 | print-recovery-header 255 | print 256 | 257 | 258 | print-kernel = print --kernel 259 | Prints all kernel details. 260 | Kernel Compressed Size, Version 261 | 262 | 263 | 264 | print-kernel-version = print --kernel-version 265 | print-header = print --header 266 | print-ramdisk = print --ramdisk 267 | print-ramdisk-ls = print --ramdisk-ls 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | -------------------------------------------------------------------------------- /lib/api/bootimage.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/api/bootimage.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_API_BOOTIMAGE 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | 27 | __LIBBOOTIMAGE_PUBLIC_API__ struct bootimage* bootimage_initialize(){ 28 | 29 | /* Allocate and zero memory to store the bootimage struct 30 | This will contain metadata for a loaded bootimage 31 | */ 32 | trace_init(); 33 | struct bootimage* bi = calloc(1,sizeof(struct bootimage)); 34 | D("bi=%p",bi); 35 | return bi ; 36 | 37 | } 38 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_free(struct bootimage** bip){ 39 | 40 | D("bip=%p",bip); 41 | if ( bip == NULL ){ 42 | errno = EINVAL ; 43 | return -1; 44 | } 45 | D("bip=%p",bip); 46 | if ( check_bootimage_structure(bip[0]) == -1){ 47 | return -1; 48 | } 49 | 50 | if ( ( bip[0]->kernel != bip[0]->uncompressed_kernel ) && ( bip[0]->uncompressed_kernel != NULL ) ){ 51 | D("freeing uncompressed_kernel=%p",bip[0]->uncompressed_kernel); 52 | free(bip[0]->uncompressed_kernel) ; 53 | bip[0]->uncompressed_kernel = NULL ; 54 | } 55 | 56 | if ( ( bip[0]->start != NULL ) && ( bip[0]->stat.st_size > 0 ) ){ 57 | D("unmapping boot image structures=%p",bip[0]->start); 58 | /* munmap(bip[0]->start,bip[0]->stat.st_size); */ 59 | bip[0]->start = NULL ; 60 | bip[0]->stat.st_size = 0 ; 61 | 62 | } 63 | if( bip[0] != NULL ){ 64 | free(bip[0]); 65 | bip[0] = NULL ; 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_read(struct bootimage* bi,const char* file_name) 72 | { 73 | 74 | /* Call */ 75 | if ( bi == NULL ){ 76 | errno = EINVAL ; 77 | return -1; 78 | } 79 | D("bip=%p",bi); 80 | if ( utils_read_all(file_name,&bi->start,&bi->stat) ){ 81 | return -1 ; 82 | 83 | } 84 | if ( bootimage_set_magic_address(bi) == -1){ 85 | return -1; 86 | } 87 | if( bootimage_set_sections(bi) == -1 ){ 88 | return -1; 89 | } 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /lib/api/bootimage_extract.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/api/bootimage_extract.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_API_BOOTIMAGE_EXTRACT 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | 31 | 32 | 33 | 34 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_header(struct bootimage* bi,const char* header_name) 35 | { 36 | if ( check_bootimage_structure(bi) == -1 ){ 37 | return -1; 38 | } 39 | if ( header_name == NULL ) { 40 | header_name = DEFAULT_NAME_HEADER; 41 | } 42 | 43 | if ( check_output_name ( header_name ) == -1 ) { 44 | return -1 ; 45 | } 46 | D("bi %u\n",bi->header_size); 47 | 48 | FILE* fi = fopen(header_name,"w+b"); 49 | if ( fi == NULL ){ 50 | return -1 ; 51 | } 52 | 53 | 54 | fwrite(bi->header,bi->header_size,1,fi); 55 | fclose(fi); 56 | return 0; 57 | 58 | } 59 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_header_block(struct bootimage* bi,const char* header_block_name) 60 | { 61 | D("bi=%p",bi); 62 | if ( check_bootimage_structure(bi) == -1 ){ 63 | D("check_bootimage_structure failed"); 64 | return -1; 65 | } 66 | 67 | if ( header_block_name == NULL ) { 68 | header_block_name = DEFAULT_NAME_HEADER_BLOCK; 69 | } 70 | D("header_block_name:%s bi->header_size:%u",header_block_name,bi->header_size); 71 | if ( check_output_name ( header_block_name ) == -1 ) { 72 | D("bi=%p",bi); 73 | return -1 ; 74 | } 75 | 76 | D("bi %u\n",bi->header_size); 77 | 78 | FILE* fi = fopen("test","w+b"); 79 | if ( fi == NULL ){ 80 | return -1 ; 81 | } 82 | 83 | fwrite(bi->header,bi->header_size,1,fi); 84 | fclose(fi); 85 | return 0; 86 | } 87 | 88 | 89 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel(struct bootimage* bi,const char* kernel_name) 90 | { 91 | if ( check_bootimage_structure(bi) == -1 ){ 92 | return -1; 93 | } 94 | if ( kernel_name == NULL ) { 95 | kernel_name = DEFAULT_NAME_KERNEL; 96 | } 97 | 98 | if ( check_output_name ( kernel_name ) == -1 ) { 99 | return -1 ; 100 | } 101 | D("bi %u\n",bi->header->kernel_size); 102 | 103 | utils_write_all(kernel_name,0660,bi->kernel,bi->header->kernel_size); 104 | return 0; 105 | } 106 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_config(struct bootimage* bi,const char* kernel_config_name) 107 | { 108 | return 0; 109 | } 110 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_config_gz(struct bootimage* bi,const char* kernel_config_gz_name) 111 | { 112 | return 0; 113 | } 114 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk(struct bootimage* bi,const char* kernel_ramdisk_dir_name) 115 | { 116 | return 0; 117 | } 118 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk_archive(struct bootimage* bi,const char* kernel_ramdisk_name) 119 | { 120 | return 0; 121 | } 122 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk_entry(struct bootimage* bi,const char* kernel_ramdisk_entry_name) 123 | { 124 | return 0; 125 | } 126 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_uncompressed_kernel(struct bootimage* bi,const char* uncompressed_kernel_name) 127 | { 128 | if ( check_bootimage_structure(bi) == -1 ){ 129 | return -1; 130 | } 131 | if ( check_bootimage_kernel(bi) == -1 ){ 132 | return -1; 133 | } 134 | if ( bootimage_kernel_decompress(bi) == -1 ){ 135 | return -1; 136 | } 137 | 138 | if ( uncompressed_kernel_name == NULL ) { 139 | uncompressed_kernel_name = DEFAULT_NAME_KERNEL_UNCOMPRESSED; 140 | } 141 | D("uncompressed_kernel_name:%s",uncompressed_kernel_name); 142 | if ( check_output_name ( uncompressed_kernel_name ) == -1 ) { 143 | D("check_output_name failed"); 144 | return -1 ; 145 | } 146 | 147 | FILE* fi = fopen(uncompressed_kernel_name,"w+b"); 148 | if ( fi == NULL ){ 149 | D("fopen failed"); 150 | return -1 ; 151 | } 152 | D("uncompressed_kernel:%p size=%u",bi->uncompressed_kernel ,bi->uncompressed_kernel_size); 153 | fwrite(bi->uncompressed_kernel ,bi->uncompressed_kernel_size,1,fi); 154 | fclose(fi); 155 | 156 | return 0; 157 | } 158 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk(struct bootimage* bi,const char* ramdisk_dir_name) 159 | { 160 | D("bi=%p",bi); 161 | 162 | if ( check_bootimage_structure(bi) == -1 ){ 163 | return -1; 164 | } 165 | if ( check_bootimage_ramdisk(bi) == -1 ){ 166 | return -1; 167 | } 168 | 169 | if ( ramdisk_dir_name == NULL ) { 170 | ramdisk_dir_name = DEFAULT_NAME_RAMDISK_DIRECTORY; 171 | } 172 | D("ramdisk_dir_name=%s",ramdisk_dir_name); 173 | int ramdisk_dir_name_length = check_output_name ( ramdisk_dir_name ); 174 | if ( ramdisk_dir_name_length == -1 ) { 175 | D("ramdisk_dir_name_length=%d",ramdisk_dir_name_length); 176 | return -1 ; 177 | } 178 | D("ramdisk_dir_name_length=%d",ramdisk_dir_name_length); 179 | if ( ( ramdisk_dir_name[ramdisk_dir_name_length-1] == '/' ) || ( ramdisk_dir_name[ramdisk_dir_name_length-1] == '\\' ) ){ 180 | ramdisk_dir_name_length -= 1 ; 181 | } 182 | 183 | if ( utils_mkdir_and_parents_umask ( ramdisk_dir_name, 0755 , 0) == -1 ) { 184 | return -1 ; 185 | } 186 | 187 | 188 | if ( archive_extract_all_memory_directory(bi->ramdisk , bi->header->ramdisk_size,ramdisk_dir_name) == -1 ){ 189 | return -1 ; 190 | 191 | } 192 | return 0; 193 | } 194 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk_archive(struct bootimage* bi,const char* ramdisk_name) 195 | { 196 | if ( check_bootimage_structure(bi) == -1 ){ 197 | return -1; 198 | } 199 | if ( ramdisk_name == NULL ) { 200 | ramdisk_name = DEFAULT_NAME_RAMDISK_ARCHIVE; 201 | } 202 | D("ramdisk_name=%s\n",ramdisk_name); 203 | if ( check_output_name ( ramdisk_name ) == -1 ) { 204 | return -1 ; 205 | } 206 | D("ramdisk_size=%u\n",bi->header->ramdisk_size); 207 | 208 | 209 | FILE* fi = fopen(ramdisk_name,"w+b"); 210 | if ( fi == NULL ){ 211 | return -1 ; 212 | } 213 | 214 | fwrite(bi->ramdisk,bi->header->ramdisk_size,1,fi); 215 | fclose(fi); 216 | return 0; 217 | } 218 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk_entry(struct bootimage* bi,const char* ramdisk_entry_name,const char* output_file) 219 | { 220 | if ( check_bootimage_structure(bi) == -1 ){ 221 | return -1; 222 | } 223 | if ( check_bootimage_ramdisk(bi) == -1 ){ 224 | return -1; 225 | } 226 | 227 | if ( check_output_name ( output_file ) == -1 ) { 228 | return -1 ; 229 | } 230 | 231 | 232 | int ret = archive_extract_memory_file(bi->ramdisk, bi->header->ramdisk_size,ramdisk_entry_name,output_file); 233 | 234 | return ret; 235 | } 236 | -------------------------------------------------------------------------------- /lib/api/bootimage_file.c: -------------------------------------------------------------------------------- 1 | #define TRACE_TAG TRACE_API_BOOTIMAGE_FILE 2 | -------------------------------------------------------------------------------- /lib/api/bootimage_file_extract.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/api/bootimage_file_extract.c 17 | * 18 | */ 19 | 20 | #include 21 | #define TRACE_TAG TRACE_API_BOOTIMAGE_FILE_EXTRACT 22 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel(char* file_name,char* kernel_name) 23 | { 24 | struct bootimage* bi = bootimage_initialize(); 25 | D("file_name=%s",file_name); 26 | if ( bootimage_file_read(bi,file_name) == -1 ){ 27 | E("bootimage_file_read"); 28 | if ( bootimage_free(&bi) == -1 ){ 29 | 30 | return -1 ; 31 | } 32 | return -1 ; 33 | } 34 | if ( bootimage_extract_kernel(bi,kernel_name) == -1 ) { 35 | E("bootimage_extract_kernel"); 36 | if ( bootimage_free(&bi) == -1 ){ 37 | return -1 ; 38 | } 39 | return -1 ; 40 | } 41 | if ( bootimage_free(&bi) == -1 ){ 42 | return -1 ; 43 | } 44 | 45 | return 0; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /lib/api/bootimage_file_print.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License") 5 | { 6 | return 0; 7 | } 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * file : lib/api/bootimage_file_print.c 20 | * 21 | */ 22 | #define TRACE_TAG TRACE_API_BOOTIMAGE_FILE_PRINT 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | 29 | 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_header(const char* file_name) 31 | { 32 | 33 | 34 | struct bootimage* bi = bootimage_initialize(); 35 | D("file_name:%s",file_name); 36 | if( bootimage_file_read(bi,file_name) == -1 ){ 37 | int ie = errno ; 38 | if ( bootimage_free(&bi) == -1 ){ 39 | return -1 ; 40 | } 41 | errno = ie ; 42 | return -1 ; 43 | } 44 | 45 | bootimage_structure_print_header(bi); 46 | 47 | 48 | if ( bootimage_free(&bi) == -1 ){ 49 | return -1 ; 50 | } 51 | return 0; 52 | } 53 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_header_fd(const char* file_name,int fd) 54 | { 55 | return 0; 56 | } 57 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_kernel(const char* file_name) 58 | { 59 | return 0; 60 | } 61 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_kernel_version(const char* file_name) 62 | { 63 | return 0; 64 | } 65 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk(const char* file_name) 66 | { 67 | return 0; 68 | } 69 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_list(const char* file_name) 70 | { 71 | return 0; 72 | } 73 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_file(const char* file_name,const char* ramdisk_file) 74 | { 75 | return 0; 76 | } 77 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_files(const char* file_name,const char** ramdisk_files) 78 | { 79 | return 0; 80 | } 81 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_all(const char* file_name) 82 | { 83 | return 0; 84 | } 85 | 86 | -------------------------------------------------------------------------------- /lib/api/bootimage_print.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License") 5 | { 6 | return 0; 7 | } 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * file : lib/api/bootimage_print.c 20 | * 21 | */ 22 | #define TRACE_TAG TRACE_API_BOOTIMAGE_PRINT 23 | #include 24 | #include 25 | 26 | 27 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_header(struct bootimage* bi) 28 | { 29 | D("bi=%p",bi); 30 | if ( check_bootimage_structure(bi) == -1 ){ 31 | return -1 ; 32 | } 33 | bootimage_structure_print_header(bi); 34 | return 0 ; 35 | 36 | } 37 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_header_fd(struct bootimage* bi,int fd) 38 | { 39 | return 0; 40 | } 41 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_kernel(struct bootimage* bi) 42 | { 43 | D("bi=%p",bi); 44 | if ( check_bootimage_structure(bi) == -1 ){ 45 | return -1; 46 | } 47 | if ( check_bootimage_kernel(bi) == -1 ){ 48 | return -1; 49 | } 50 | if ( bootimage_kernel_decompress(bi) == -1 ){ 51 | return -1; 52 | } 53 | 54 | bootimage_structure_print_kernel(bi); 55 | return 0 ; 56 | } 57 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_kernel_version(struct bootimage* bi) 58 | { 59 | 60 | if ( check_bootimage_structure(bi) == -1 ){ 61 | return -1 ; 62 | } 63 | if ( check_bootimage_kernel(bi) == -1 ){ 64 | return -1; 65 | } 66 | if ( bootimage_kernel_decompress(bi) == -1 ){ 67 | return -1; 68 | } 69 | 70 | fprintf(stdout,"%s\n",bi->kernel_version_string); 71 | 72 | 73 | return 0 ; 74 | 75 | } 76 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk(struct bootimage* bi) 77 | { 78 | if ( check_bootimage_structure(bi) == -1 ){ 79 | return -1 ; 80 | } 81 | if ( check_bootimage_kernel(bi) == -1 ){ 82 | return -1; 83 | } 84 | return 0; 85 | } 86 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_list(struct bootimage* bi) 87 | { 88 | return 0; 89 | } 90 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_file(struct bootimage* bi,const char* ramdisk_file) 91 | { 92 | return 0; 93 | } 94 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_files(struct bootimage* bi,const char** ramdisk_files) 95 | { 96 | return 0; 97 | } 98 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_all(struct bootimage* bi) 99 | { 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /lib/api/bootimage_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/api/bootimage_utils.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_API_BOOTIMAGE_UTILS 20 | #include 21 | #include 22 | 23 | 24 | 25 | __LIBBOOTIMAGE_PUBLIC_API__ struct bootimage_utils* bootimage_utils_initialize() 26 | { 27 | 28 | /* Allocate and zero memory to store the bootimage struct 29 | This will contain metadata for a loaded bootimage 30 | */ 31 | trace_init(); 32 | struct bootimage_utils* bi = calloc(1,sizeof(struct bootimage_utils)); 33 | D("bi=%p",bi); 34 | return bi ; 35 | 36 | } 37 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_utils_free(struct bootimage_utils** biup) 38 | { 39 | D("biup=%p",biup); 40 | if ( biup == NULL ){ 41 | errno = EINVAL ; 42 | return -1; 43 | } 44 | if ( check_bootimage_utils_structure(biup[0]) == -1){ 45 | return -1; 46 | } 47 | 48 | if ( ( biup[0]->data != NULL ) ){ 49 | D("freeing bootimage_utils data=%p",biup[0]->data); 50 | free(biup[0]->data) ; 51 | biup[0]->data = NULL ; 52 | } 53 | 54 | if( biup[0] != NULL ){ 55 | D("freeing bootimage_utils structure=%p",biup[0]); 56 | free(biup[0]); 57 | biup[0] = NULL ; 58 | } 59 | 60 | return 0; 61 | 62 | } 63 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_utils_file_read(struct bootimage_utils* biu,const char* file_name){ 64 | 65 | /* Call */ 66 | if ( biu == NULL ) { 67 | errno = EBIUNULL; 68 | D("biu=%p errno=%d [ EBINULL ]",biu,errno); 69 | return -1; 70 | } 71 | if ( utils_read_all(file_name,&biu->data,&biu->stat) == -1 ){ 72 | return -1; 73 | } 74 | biu->file_name = file_name; 75 | 76 | D("biu->data 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",biu->data[0],biu->data[1],biu->data[2],biu->data[3],biu->data[4],biu->data[5]); 77 | if( check_bootimage_utils_file_type(biu) == -1 ){ 78 | D("file_type check failed"); 79 | return -1; 80 | } 81 | D("Return 0"); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /lib/include/api/bootimage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage.h 17 | * 18 | */ 19 | 20 | #ifndef _40589ef6_7d28_11e4_9dc9_5404a601fa9d 21 | #define _40589ef6_7d28_11e4_9dc9_5404a601fa9d 22 | 23 | /* Opaque bootimage structure declaration. The full definition can be 24 | found in lib/include/private/bootimage.h */ 25 | struct bootimage ; 26 | 27 | __LIBBOOTIMAGE_PUBLIC_API__ struct bootimage* bootimage_initialize(); 28 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_free(struct bootimage** bip); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_read(struct bootimage* bi,const char* file_name); 30 | #endif 31 | -------------------------------------------------------------------------------- /lib/include/api/bootimage_extract.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage_extract.h 17 | * 18 | */ 19 | 20 | #ifndef _2a65bc00_7d28_11e4_8509_5404a601fa9d 21 | #define _2a65bc00_7d28_11e4_8509_5404a601fa9d 22 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_header(struct bootimage* bi,const char* header_name); 23 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_header_block(struct bootimage* bi,const char* header_block_name); 24 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel(struct bootimage* bi,const char* kernel_name); 25 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_config(struct bootimage* bi,const char* kernel_config_name); 26 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_config_gz(struct bootimage* bi,const char* kernel_config_gz_name); 27 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk(struct bootimage* bi,const char* kernel_ramdisk_dir_name); 28 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk_archive(struct bootimage* bi,const char* kernel_ramdisk_name); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_kernel_ramdisk_entry(struct bootimage* bi,const char* kernel_ramdisk_entry_name); 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_uncompressed_kernel(struct bootimage* bi,const char* uncompressed_kernel_name); 31 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk(struct bootimage* bi,const char* ramdisk_dir_name); 32 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk_archive(struct bootimage* bi,const char* ramdisk_name); 33 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_extract_ramdisk_entry(struct bootimage* bi,const char* ramdisk_entry_name,const char* output_file); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/include/api/bootimage_file_extract.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage_file_extract.h 17 | * 18 | */ 19 | 20 | #ifndef _31556c36_7d28_11e4_9d27_5404a601fa9d 21 | #define _31556c36_7d28_11e4_9d27_5404a601fa9d 22 | 23 | 24 | 25 | 26 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_header(char* file_name, char* header_name); 27 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_header_block(char* file_name, char* header_block_name); 28 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel(char* file_name, char* kernel_name); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel_config(char* file_name, char* kernel_config_name); 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel_config_gz(char* file_name, char* kernel_config_gz_name); 31 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel_ramdisk(char* file_name, char* kernel_ramdisk_name); 32 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel_ramdisk_archive(char* file_name, char* kernel_ramdisk_dir_name); 33 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_kernel_ramdisk_entry(char* file_name, char* kernel_ramdisk_entry_name); 34 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_uncompressed_kernel(char* file_name, char* uncompressed_kernel_name); 35 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_ramdisk(char* file_name, char* ramdisk_name); 36 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_ramdisk_archive(char* file_name, char* ramdisk_dir_name); 37 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_extract_ramdisk_entry(char* file_name, char* ramdisk_entry_name); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /lib/include/api/bootimage_file_print.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage_file_print.h 17 | * 18 | */ 19 | 20 | #ifndef _b15dfc64_8196_11e4_9db6_5404a601fa9d 21 | #define _b15dfc64_8196_11e4_9db6_5404a601fa9d 22 | 23 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_header(const char* file_name); 24 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_header_fd(const char* file_name,int fd); 25 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_kernel(const char* file_name); 26 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_kernel_version(const char* file_name); 27 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk(const char* file_name); 28 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_list(const char* file_name); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_file(const char* file_name,const char* ramdisk_file); 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_ramdisk_files(const char* file_name,const char** ramdisk_files); 31 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_file_print_all(const char* file_name); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lib/include/api/bootimage_print.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage_print.h 17 | * 18 | */ 19 | 20 | #ifndef _83a76a1c_8196_11e4_8539_5404a601fa9d 21 | #define _83a76a1c_8196_11e4_8539_5404a601fa9d 22 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_header(struct bootimage* bi); 23 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_header_fd(struct bootimage* bi,int fd); 24 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_kernel(struct bootimage* bi); 25 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_kernel_version(struct bootimage* bi); 26 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk(struct bootimage* bi); 27 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_list(struct bootimage* bi); 28 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_file(struct bootimage* bi,const char* ramdisk_file); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_ramdisk_files(struct bootimage* bi,const char** ramdisk_files); 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_print_all(struct bootimage* bi); 31 | #endif 32 | -------------------------------------------------------------------------------- /lib/include/api/bootimage_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/bootimage_utils.h 17 | * 18 | */ 19 | 20 | #ifndef _ceeda38e_928e_11e4_a689_5404a601fa9d 21 | #define _ceeda38e_928e_11e4_a689_5404a601fa9d 22 | 23 | 24 | /* Opaque bootimage structure declaration. The full definition can be 25 | found in lib/include/private/bootimage_utils.h */ 26 | struct bootimage_utils ; 27 | 28 | __LIBBOOTIMAGE_PUBLIC_API__ struct bootimage_utils* bootimage_utils_initialize(); 29 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_utils_file_read(struct bootimage_utils* biu,const char* file_name); 30 | __LIBBOOTIMAGE_PUBLIC_API__ int bootimage_utils_free(struct bootimage_utils** biup) ; 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lib/include/api/errors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/errors.h 17 | * 18 | */ 19 | 20 | #ifndef _236ac100_7d5c_11e4_b559_5404a601fa9d 21 | #define _236ac100_7d5c_11e4_b559_5404a601fa9d 22 | #include 23 | #define EBIOK 0 24 | #define EBINULL 10000 25 | #define EBIFSIZE 10001 26 | #define EBIFNAME 10002 27 | #define EBIFACCESS 10003 28 | #define EBINOMAGIC 10004 29 | #define EBIBADPADHEAD 10005 30 | #define EBIBADPADKERNEL 10006 31 | #define EBIBADPADRAMDISK 10007 32 | #define EBIBADPADSECOND 10008 33 | #define EBISTAT 10009 34 | #define EBIRDENTRY 10010 35 | #define EBIRDENTRYLENGTH 10011 36 | #define EBIRDMEM 10012 37 | #define EBIHEADMEM 10013 38 | #define EBIRDMEMSIZE 10014 39 | #define EBIOUTNAME 10015 40 | #define EBIOUTNAMELEN 10016 41 | #define EBIARCHIVEREAD 10017 42 | #define EBIARCHIVEREADFILTER 10018 43 | #define EBIARCHIVEREADFORMAT 10019 44 | #define EBIARCHIVEREADFORMATRAW 10020 45 | #define EBIKERNELMEM 10021 46 | #define EBIKERNELMEMSIZE 10022 47 | #define EBIUNULL 10023 48 | #define EBIUFILETYPE 10024 49 | #define EBIUSTAT 10025 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /lib/include/api/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/api/utils.h 17 | * 18 | */ 19 | 20 | #ifndef _d6fb49bc_9786_11e4_8458_5404a601fa9d 21 | #define _d6fb49bc_9786_11e4_8458_5404a601fa9d 22 | __LIBBOOTIMAGE_PUBLIC_API__ ssize_t utils_sanitize_string(char* s,ssize_t maxlen) ; 23 | __LIBBOOTIMAGE_PUBLIC_API__ unsigned char *utils_memmem(unsigned char *haystack, unsigned haystack_len, char* needle, unsigned needle_len); 24 | #endif 25 | -------------------------------------------------------------------------------- /lib/include/biutils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/biutils.h 17 | * 18 | */ 19 | 20 | #ifndef _826f4afc_92a3_11e4_937c_5404a601fa9d 21 | #define _826f4afc_92a3_11e4_937c_5404a601fa9d 22 | 23 | #ifndef __LIBBOOTIMAGE_PUBLIC_API__ 24 | #define __LIBBOOTIMAGE_PUBLIC_API__ __attribute__((visibility("default"))) 25 | #endif 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /lib/include/private/api.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/api.h 17 | * 18 | */ 19 | 20 | 21 | #ifndef _3453ef82_7d16_11e4_ba53_5404a601fa9d 22 | #define _3453ef82_7d16_11e4_ba53_5404a601fa9d 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef __LIBBOOTIMAGE_PRIVATE_API__ 37 | #define __LIBBOOTIMAGE_PRIVATE_API__ __attribute__((visibility("hidden"))) 38 | #endif 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /lib/include/private/archive.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/archive.h 17 | * 18 | */ 19 | 20 | #ifndef _a7808ef4_8179_11e4_991e_5404a601fa9d 21 | #define _a7808ef4_8179_11e4_991e_5404a601fa9d 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_all_memory_directory( char* archive_data , uint64_t archive_size, char* output_dir_name); 28 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_all(struct archive *a,char* output_dir_name); 29 | __LIBBOOTIMAGE_PRIVATE_API__ char* archive_extract_entry(char* data,off_t data_size,char* name,size_t name_length,size_t* entry_size); 30 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_file(char* archive_data,off_t archive_data_size,char* name,size_t name_length); 31 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_memory_file( char* archive_data , uint64_t archive_size, char* entry_name, char* output_file_name); 32 | __LIBBOOTIMAGE_PRIVATE_API__ unsigned int archive_gzip_get_uncompressed_size(char* data,off_t data_size); 33 | #endif 34 | -------------------------------------------------------------------------------- /lib/include/private/bootimage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/bootimage.h 17 | * This file is part of the INTERNAL api for the bootimage utils project 18 | * 19 | */ 20 | 21 | 22 | #ifndef _a373d2ec_79e2_11e4_a4f4_5404a601fa9d 23 | #define _a373d2ec_79e2_11e4_a4f4_5404a601fa9d 24 | #include 25 | #include 26 | #include 27 | 28 | #define DEFAULT_NAME_KERNEL "kernel" 29 | #define DEFAULT_NAME_KERNEL_UNCOMPRESSED "image" 30 | #define DEFAULT_NAME_KERNEL_RAMDISK_DIRECTORY "kernel-ramdisk" 31 | #define DEFAULT_NAME_KERNEL_RAMDISK_ARCHIVE "kernel-ramdisk.img" 32 | #define DEFAULT_NAME_RAMDISK_DIRECTORY "ramdisk" 33 | #define DEFAULT_NAME_RAMDISK_ARCHIVE "ramdisk.img" 34 | #define DEFAULT_NAME_HEADER "header" 35 | #define DEFAULT_NAME_HEADER_BLOCK "header.bin" 36 | 37 | 38 | 39 | /* This is the codeaurora ( caf ) version of the boot_img_hdr structure 40 | * This is modified to "handle" kernel device trees see 41 | * https://www.codeaurora.org/cgit/quic/la/platform/system/core/commit/?h=lp&id=27d21ae19ba72a66a6094aa3ffd995e3979211a8 42 | * for more info . 43 | */ 44 | #define BOOT_MAGIC "ANDROID!" 45 | #define BOOT_MAGIC_SIZE 8 46 | #define BOOT_NAME_SIZE 16 47 | #define BOOT_ARGS_SIZE 512 48 | #define BOOT_EXTRA_ARGS_SIZE 1024 49 | 50 | struct bootimage_header 51 | { 52 | unsigned char magic[BOOT_MAGIC_SIZE]; 53 | 54 | uint32_t kernel_size; /* size in bytes */ 55 | uint32_t kernel_addr; /* physical load addr */ 56 | 57 | uint32_t ramdisk_size; /* size in bytes */ 58 | uint32_t ramdisk_addr; /* physical load addr */ 59 | 60 | uint32_t second_size; /* size in bytes */ 61 | uint32_t second_addr; /* physical load addr */ 62 | 63 | uint32_t tags_addr; /* physical addr for kernel tags */ 64 | uint32_t page_size; /* flash page size we assume */ 65 | uint32_t dt_size; /* device tree in bytes */ 66 | uint32_t unused; /* future expansion: should be 0 */ 67 | 68 | unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */ 69 | 70 | unsigned char cmdline[BOOT_ARGS_SIZE]; 71 | 72 | unsigned id[8]; /* timestamp / checksum / sha1 / etc */ 73 | 74 | /* Supplemental command line data; kept here to maintain 75 | * binary compatibility with older versions of mkbootimg */ 76 | unsigned char extra_cmdline[BOOT_EXTRA_ARGS_SIZE]; 77 | }; 78 | 79 | 80 | /* 81 | ** +-----------------+ 82 | ** | boot header | 1 page 83 | ** +-----------------+ 84 | ** | kernel | n pages 85 | ** +-----------------+ 86 | ** | ramdisk | m pages 87 | ** +-----------------+ 88 | ** | second stage | o pages 89 | ** +-----------------+ 90 | ** | device tree | p pages 91 | ** +-----------------+ 92 | ** 93 | ** n = (kernel_size + page_size - 1) / page_size 94 | ** m = (ramdisk_size + page_size - 1) / page_size 95 | ** o = (second_size + page_size - 1) / page_size 96 | ** p = (dt_size + page_size - 1) / page_size 97 | ** 98 | ** 0. all entities are page_size aligned in flash 99 | ** 1. kernel and ramdisk are required (size != 0) 100 | ** 2. second is optional (second_size == 0 -> no second) 101 | ** 3. load each element (kernel, ramdisk, second) at 102 | ** the specified physical address (kernel_addr, etc) 103 | ** 4. prepare tags at tag_addr. kernel_args[] is 104 | ** appended to the kernel commandline in the tags. 105 | ** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 106 | ** 6. if second_size != 0: jump to second_addr 107 | ** else: jump to kernel_addr 108 | */ 109 | 110 | enum page_size { 111 | PAGE_SIZE_2048= 2048 , 112 | PAGE_SIZE_MIN = PAGE_SIZE_2048, 113 | PAGE_SIZE_4096 = 4096 , 114 | PAGE_SIZE_8192 = 8192 , 115 | PAGE_SIZE_16384 = 16384 , 116 | PAGE_SIZE_32768 = 32768 , 117 | PAGE_SIZE_65536 = 65536 , 118 | PAGE_SIZE_131072 = 131072 , 119 | PAGE_SIZE_262144 = 262144 , 120 | PAGE_SIZE_MAX = PAGE_SIZE_262144 121 | 122 | }; 123 | 124 | struct bootimage 125 | { 126 | 127 | unsigned char* start ; /* pointer to the start of the image file in memory 128 | This is often the same as value as the header. 129 | However some boot images have additional data before 130 | The header magic ( e.g HTC ) */ 131 | 132 | 133 | struct bootimage_header* header ; /* pointer to the start of the boot image header 134 | identified with the ANDROID! magic */ 135 | 136 | char* kernel ; 137 | /* pointer to the start of the kernel data. 138 | This is normally locate one page after the header and 139 | is usually compressed. If the kernel is uncompressed then 140 | the uncompressed_kernel and kernel members will be the same */ 141 | 142 | char* ramdisk ; /* pointer to the start of the ramdisk data. This is 143 | on the next page boundary after the kernel data */ 144 | char* second ; /* pointer to the start of the second bootloader data. */ 145 | 146 | struct stat stat ; /* The file size of the bootimage. other sizes are found in the 147 | bootimage header*/ 148 | 149 | uint32_t header_size; /* Header size stores the sizeof(bootimage_header) structure 150 | for convient easy access */ 151 | 152 | uint32_t header_padding; /* the number of bytes required to align the 153 | header to the next page boundary */ 154 | uint32_t kernel_padding; 155 | uint32_t ramdisk_padding; 156 | uint32_t second_padding; 157 | 158 | char* compressed_kernel_offset ; /* pointer to the start of the compressed kernel data */ 159 | struct kernel_type_t* compressed_kernel_type; /* */ 160 | uint32_t compressed_kernel_size; /* size in bytes */ 161 | 162 | char* uncompressed_kernel ; /* pointer to the uncompressed kernel data */ 163 | uint32_t uncompressed_kernel_size; /* size in bytes */ 164 | char* kernel_version_string ; /* pointer to the start of the kernel version 165 | string in the uncompressed kernel data */ 166 | uint32_t kernel_version_string_length; /* length of the kernel version string */ 167 | 168 | 169 | }; 170 | 171 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_mmap_file(struct bootimage* bi,const char* file_name); 172 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_magic_address(struct bootimage* bi); 173 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_header_section(struct bootimage* bi); 174 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_kernel_section(struct bootimage* bi); 175 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_ramdisk_section(struct bootimage* bi); 176 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_second_section(struct bootimage* bi); 177 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_sections(struct bootimage* bi); 178 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_file_read_magic(struct bootimage* bi,const char* file_name); 179 | #endif 180 | -------------------------------------------------------------------------------- /lib/include/private/bootimage_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/bootimage_utils.h 17 | * 18 | */ 19 | 20 | #ifndef _5203c50e_9290_11e4_981e_5404a601fa9d 21 | #define _5203c50e_9290_11e4_981e_5404a601fa9d 22 | 23 | #define BOOTIMAGE_UTILS_FILENAME_UPDATE_ZIP "update.zip" 24 | #define BOOTIMAGE_UTILS_FILENAME_BOOT_IMG "boot.img" 25 | #define BOOTIMAGE_UTILS_FILENAME_RECOVERY_IMG "recovery.img" 26 | #define BOOTIMAGE_UTILS_FILENAME_KERNEL "kernel" 27 | #define BOOTIMAGE_UTILS_FILENAME_BZIMAGE "bzImage" 28 | #define BOOTIMAGE_UTILS_FILENAME_RAMDISK_CPIO_GZ "ramdisk.cpio.gz" 29 | #define BOOTIMAGE_UTILS_FILENAME_RAMDISK_IMG "ramdisk.img" 30 | #define BOOTIMAGE_UTILS_FILENAME_RECOVERY_RAMDISK_CPIO_GZ "ramdisk-recovery.cpio.gz" 31 | #define BOOTIMAGE_UTILS_FILENAME_RECOVERY_RAMDISK_IMG "ramdisk-recovery.img" 32 | 33 | 34 | #define BOOTIMAGE_UTILS_FILE_EXTENSION_ZIP ".zip" 35 | #define BOOTIMAGE_UTILS_FILE_EXTENSION_TGZ ".tgz" 36 | #define BOOTIMAGE_UTILS_FILE_EXTENSION_IMG ".img" 37 | 38 | enum bootimage_utils_filetype 39 | { 40 | BOOTIMAGE_UTILS_FILETYPE_UNKNOWN = 0, 41 | BOOTIMAGE_UTILS_FILETYPE_NEXUS_FACTORY_IMAGE, 42 | BOOTIMAGE_UTILS_FILETYPE_UPDATE_ZIP, 43 | BOOTIMAGE_UTILS_FILETYPE_OTA_UPDATE_ZIP, 44 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_BOOT_IMAGE, 45 | BOOTIMAGE_UTILS_FILETYPE_OEM_BOOT_IMAGE, 46 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_RECOVERY_IMAGE, 47 | BOOTIMAGE_UTILS_FILETYPE_OEM_RECOVERY_IMAGE, 48 | BOOTIMAGE_UTILS_FILETYPE_STANDARD_RAMDISK, 49 | BOOTIMAGE_UTILS_FILETYPE_RECOVERY_RAMDISK, 50 | BOOTIMAGE_UTILS_FILETYPE_COMPRESSED_KERNEL, 51 | BOOTIMAGE_UTILS_FILETYPE_MAX = BOOTIMAGE_UTILS_FILETYPE_COMPRESSED_KERNEL 52 | }; 53 | 54 | struct bootimage_utils { 55 | int filetype ; 56 | struct stat stat ; 57 | char* file_name ; 58 | char* data ; 59 | struct bootimage bootimage ; 60 | 61 | }; 62 | 63 | 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lib/include/private/checks.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/checks.h 17 | * 18 | */ 19 | 20 | #ifndef _a4d5d442_7d15_11e4_9366_5404a601fa9d 21 | #define _a4d5d442_7d15_11e4_9366_5404a601fa9d 22 | #include 23 | #include 24 | #include 25 | __LIBBOOTIMAGE_PRIVATE_API__ int check_output_name(char* name); 26 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_structure(struct bootimage* bi); 27 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_header(struct bootimage* bi); 28 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_file_stat_size(struct bootimage* bi ,char* file_name); 29 | __LIBBOOTIMAGE_PRIVATE_API__ int check_file_name(char* file_name); 30 | __LIBBOOTIMAGE_PRIVATE_API__ int check_ramdisk_entryname( char* entry_name); 31 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_ramdisk(struct bootimage* bi); 32 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_kernel(struct bootimage* bi); 33 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_file_read_magic(struct bootimage* bi,char* file_name); 34 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_structure(struct bootimage_utils* biu); 35 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_file_read(struct bootimage_utils* biu,char* file_name); 36 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_file_type(struct bootimage_utils* biu); 37 | #define CPIO_FILE_NAME_MAX 1024 38 | #endif 39 | -------------------------------------------------------------------------------- /lib/include/private/errors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/errors.h 17 | * 18 | */ 19 | 20 | #ifndef _1606d2f4_8182_11e4_8a14_5404a601fa9d 21 | #define _1606d2f4_8182_11e4_8a14_5404a601fa9d 22 | #endif 23 | -------------------------------------------------------------------------------- /lib/include/private/factory_images.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/factory_images.h 17 | * 18 | */ 19 | 20 | #ifndef _91b5499e_92c6_11e4_80bf_5404a601fa9d 21 | #define _91b5499e_92c6_11e4_80bf_5404a601fa9d 22 | 23 | #define FACTORY_IMAGE_COUNT 101 24 | #define FACTORY_IMAGE_STRNCMP_MATCH_FOUND 0 25 | #define FACTORY_IMAGE_MAGIC_GZIP "\x1F\x8B\x08" 26 | #define FACTORY_IMAGE_MAGIC_GZIP_SIZE 3 27 | 28 | 29 | 30 | static struct factory_images { 31 | char* name ; 32 | int name_length ; 33 | char* zip_name ; 34 | int zip_name_length ; 35 | } factory_image_info[FACTORY_IMAGE_COUNT] = { 36 | {"fugu-lrx21m-factory-aeed3bef.tgz",32,"fugu-lrx21m/image-fugu-lrx21m.zip",33}, 37 | {"fugu-lrx21v-factory-64050f47.tgz",32,"fugu-lrx21v/image-fugu-lrx21v.zip",33}, 38 | {"hammerhead-kot49h-factory-02006b99.tgz",38,"hammerhead-kot49h/image-hammerhead-kot49h.zip",45}, 39 | {"hammerhead-krt16m-factory-bd9c39de.tgz",38,"hammerhead-krt16m/image-hammerhead-krt16m.zip",45}, 40 | {"hammerhead-ktu84m-factory-53ff95bf.tgz",38,"hammerhead-ktu84m/image-hammerhead-ktu84m.zip",45}, 41 | {"hammerhead-ktu84p-factory-35ea0277.tgz",38,"hammerhead-ktu84p/image-hammerhead-ktu84p.zip",45}, 42 | {"hammerhead-ktu84q-factory-ae475293.tgz",38,"hammerhead-ktu84q/image-hammerhead-ktu84q.zip",45}, 43 | {"hammerhead-lpv79-preview-ac1d8a8e.tgz",37,"hammerhead-lpv79/image-hammerhead-lpv79.zip",43}, 44 | {"hammerhead-lrx21o-factory-01315e08.tgz",38,"hammerhead-lrx21o/image-hammerhead-lrx21o.zip",45}, 45 | {"hammerhead-lrx22c-factory-0f9eda1b.tgz",38,"hammerhead-lrx22c/image-hammerhead-lrx22c.zip",45}, 46 | {"mantaray-jdq39-factory-d79f489e.tgz",35,"mantaray-jdq39/image-mantaray-jdq39.zip",39}, 47 | {"mantaray-jwr66y-factory-3d8252dd.tgz",36,"mantaray-jwr66y/image-mantaray-jwr66y.zip",41}, 48 | {"mantaray-kot49h-factory-174ba74f.tgz",36,"mantaray-kot49h/image-mantaray-kot49h.zip",41}, 49 | {"mantaray-krt16o-factory-85349288.tgz",36,"mantaray-krt16o/image-mantaray-krt16o.zip",41}, 50 | {"mantaray-krt16s-factory-94413961.tgz",36,"mantaray-krt16s/image-mantaray-krt16s.zip",41}, 51 | {"mantaray-ktu84l-factory-8fefbb27.tgz",36,"mantaray-ktu84l/image-mantaray-ktu84l.zip",41}, 52 | {"mantaray-ktu84p-factory-74e52998.tgz",36,"mantaray-ktu84p/image-mantaray-ktu84p.zip",41}, 53 | {"mantaray-lrx21p-factory-ad2499ea.tgz",36,"mantaray-lrx21p/image-mantaray-lrx21p.zip",41}, 54 | {"mantaray-lrx22c-factory-1a7ae5d1.tgz",36,"mantaray-lrx22c/image-mantaray-lrx22c.zip",41}, 55 | {"mysid-imm76k-factory-98d21321.tgz",33,"mysid-imm76k/image-mysid-imm76k.zip",35}, 56 | {"mysid-jdq39-factory-e365033f.tgz",32,"mysid-jdq39/image-mysid-jdq39.zip",33}, 57 | {"mysid-jro03o-factory-f17426e6.tgz",33,"mysid-jro03o/image-mysid-jro03o.zip",35}, 58 | {"mysidspr-fh05-factory-8cb5208b.tgz",34,"mysidspr-fh05/image-mysidspr-fh05.zip",37}, 59 | {"mysidspr-ga02-factory.tgz",25,"mysidspr-ga02/signed-toroplus-img-ga02.zip",42}, 60 | {"nakasig-jdq39-factory-0798439d.tgz",34,"nakasig-jdq39/image-nakasig-jdq39.zip",37}, 61 | {"nakasig-jop40c-factory-a0431f67.tgz",35,"nakasig-jop40c/image-nakasig-jop40c.zip",39}, 62 | {"nakasig-jwr66y-factory-bdbb7bd7.tgz",35,"nakasig-jwr66y/image-nakasig-jwr66y.zip",39}, 63 | {"nakasig-kot49h-factory-83d93b5f.tgz",35,"nakasig-kot49h/image-nakasig-kot49h.zip",39}, 64 | {"nakasig-krt16o-factory-9c987833.tgz",35,"nakasig-krt16o/image-nakasig-krt16o.zip",39}, 65 | {"nakasig-krt16s-factory-1e882585.tgz",35,"nakasig-krt16s/image-nakasig-krt16s.zip",39}, 66 | {"nakasig-ktu84l-factory-8ce3d5ea.tgz",35,"nakasig-ktu84l/image-nakasig-ktu84l.zip",39}, 67 | {"nakasig-ktu84p-factory-0cc2750b.tgz",35,"nakasig-ktu84p/image-nakasig-ktu84p.zip",39}, 68 | {"nakasi-jdq39-factory-c317339e.tgz",33,"nakasi-jdq39/image-nakasi-jdq39.zip",35}, 69 | {"nakasi-jop40c-factory-6aabb391.tgz",34,"nakasi-jop40c/image-nakasi-jop40c.zip",37}, 70 | {"nakasi-jro03d-factory-e102ba72.tgz",34,"nakasi-jro03d/image-nakasi-jro03d.zip",37}, 71 | {"nakasi-jwr66v-factory-d341d356.tgz",34,"nakasi-jwr66v/image-nakasi-jwr66v.zip",37}, 72 | {"nakasi-jwr66y-factory-1e503532.tgz",34,"nakasi-jwr66y/image-nakasi-jwr66y.zip",37}, 73 | {"nakasi-jzo54k-factory-973f190e.tgz",34,"nakasi-jzo54k/image-nakasi-jzo54k.zip",37}, 74 | {"nakasi-kot49h-factory-5e9db5e1.tgz",34,"nakasi-kot49h/image-nakasi-kot49h.zip",37}, 75 | {"nakasi-krt16o-factory-db4a1a8a.tgz",34,"nakasi-krt16o/image-nakasi-krt16o.zip",37}, 76 | {"nakasi-krt16s-factory-da7dee49.tgz",34,"nakasi-krt16s/image-nakasi-krt16s.zip",37}, 77 | {"nakasi-ktu84l-factory-0e21238f.tgz",34,"nakasi-ktu84l/image-nakasi-ktu84l.zip",37}, 78 | {"nakasi-ktu84p-factory-76acdbe9.tgz",34,"nakasi-ktu84p/image-nakasi-ktu84p.zip",37}, 79 | {"nakasi-lrx21p-factory-93daa4d3.tgz",34,"nakasi-lrx21p/image-nakasi-lrx21p.zip",37}, 80 | {"nakasi-lrx22g-factory-2291c36b.tgz",34,"nakasi-lrx22g/image-nakasi-lrx22g.zip",37}, 81 | {"occam-jdq39-factory-345dc199.tgz",32,"occam-jdq39/image-occam-jdq39.zip",33}, 82 | {"occam-jwr66y-factory-74b1deab.tgz",33,"occam-jwr66y/image-occam-jwr66y.zip",35}, 83 | {"occam-kot49h-factory-02e344de.tgz",33,"occam-kot49h/image-occam-kot49h.zip",35}, 84 | {"occam-krt16o-factory-75ccae7a.tgz",33,"occam-krt16o/image-occam-krt16o.zip",35}, 85 | {"occam-krt16s-factory-2006f418.tgz",33,"occam-krt16s/image-occam-krt16s.zip",35}, 86 | {"occam-ktu84l-factory-0d3fd624.tgz",33,"occam-ktu84l/image-occam-ktu84l.zip",35}, 87 | {"occam-ktu84p-factory-b6ac3ad6.tgz",33,"occam-ktu84p/image-occam-ktu84p.zip",35}, 88 | {"occam-lrx21t-factory-51cee750.tgz",33,"occam-lrx21t/image-occam-lrx21t.zip",35}, 89 | {"occam-lrx22c-factory-86c04af6.tgz",33,"occam-lrx22c/image-occam-lrx22c.zip",35}, 90 | {"razorg-JLS36C-factory-fb03a89f.tgz",34,"razorg-JLS36C/image-razorg-JLS36C.zip",37}, 91 | {"razorg-jls36i-factory-ecb320cd.tgz",34,"razorg-jls36i/image-razorg-jls36i.zip",37}, 92 | {"razorg-kot49h-factory-49789b24.tgz",34,"razorg-kot49h/image-razorg-kot49h.zip",37}, 93 | {"razorg-krt16o-factory-2b749c29.tgz",34,"razorg-krt16o/image-razorg-krt16o.zip",37}, 94 | {"razorg-krt16s-factory-bd6c9241.tgz",34,"razorg-krt16s/image-razorg-krt16s.zip",37}, 95 | {"razorg-ktu84l-factory-9f9b9ef2.tgz",34,"razorg-ktu84l/image-razorg-ktu84l.zip",37}, 96 | {"razorg-ktu84p-factory-f21762aa.tgz",34,"razorg-ktu84p/image-razorg-ktu84p.zip",37}, 97 | {"razorg-kvt49l-factory-65bdbe0a.tgz",34,"razorg-kvt49l/image-razorg-kvt49l.zip",37}, 98 | {"razor-jss15q-factory-4f77b811.tgz",33,"razor-jss15q/image-razor-jss15q.zip",35}, 99 | {"razor-jss15r-factory-ec2d4f76.tgz",33,"razor-jss15r/image-razor-jss15r.zip",35}, 100 | {"razor-kot49h-factory-ebb4918e.tgz",33,"razor-kot49h/image-razor-kot49h.zip",35}, 101 | {"razor-krt16o-factory-d9e7d441.tgz",33,"razor-krt16o/image-razor-krt16o.zip",35}, 102 | {"razor-krt16s-factory-7235eb0d.tgz",33,"razor-krt16s/image-razor-krt16s.zip",35}, 103 | {"razor-ktu84l-factory-afe3afc8.tgz",33,"razor-ktu84l/image-razor-ktu84l.zip",35}, 104 | {"razor-ktu84p-factory-b1b2c0da.tgz",33,"razor-ktu84p/image-razor-ktu84p.zip",35}, 105 | {"razor-lpv79-preview-d0ddf8ce.tgz",32,"razor-lpv79/image-razor-lpv79.zip",33}, 106 | {"razor-lrx21p-factory-ba55c6ab.tgz",33,"razor-lrx21p/image-razor-lrx21p.zip",35}, 107 | {"razor-lrx22c-factory-a9c6e55f.tgz",33,"razor-lrx22c/image-razor-lrx22c.zip",35}, 108 | {"shamu-lrx21o-factory-e028f5ea.tgz",33,"shamu-lrx21o/image-shamu-lrx21o.zip",35}, 109 | {"shamu-lrx22c-factory-ff173fc6.tgz",33,"shamu-lrx22c/image-shamu-lrx22c.zip",35}, 110 | {"sojua-grk39f-factory-5d73a09d.tgz",33,"sojua-grk39f/image-sojua-grk39f.zip",35}, 111 | {"sojua-imm76d-factory-76ad4959.tgz",33,"sojua-imm76d/image-sojua-imm76d.zip",35}, 112 | {"sojua-jzo54k-factory-1121b619.tgz",33,"sojua-jzo54k/image-sojua-jzo54k.zip",35}, 113 | {"soju-grk39f-factory-5ab09c98.tgz",32,"soju-grk39f/image-soju-grk39f.zip",33}, 114 | {"soju-imm76d-factory-ca4ae9ee.tgz",32,"soju-imm76d/image-soju-imm76d.zip",33}, 115 | {"soju-jzo54k-factory-36602333.tgz",32,"soju-jzo54k/image-soju-jzo54k.zip",33}, 116 | {"sojuk-grk39f-factory-4d5663c0.tgz",33,"sojuk-grk39f/image-sojuk-grk39f.zip",35}, 117 | {"sojuk-imm76d-factory-422adc36.tgz",33,"sojuk-imm76d/image-sojuk-imm76d.zip",35}, 118 | {"sojuk-jro03e-factory-93a21b70.tgz",33,"sojuk-jro03e/image-sojuk-jro03e.zip",35}, 119 | {"sojus-gwk74-factory-4a34b67a.tgz",32,"sojus-gwk74/image-sojus-gwk74.zip",33}, 120 | {"sojus-imm76d-factory-10660f4c.tgz",33,"sojus-imm76d/image-sojus-imm76d.zip",35}, 121 | {"sojus-jro03r-factory-59a247f5.tgz",33,"sojus-jro03r/image-sojus-jro03r.zip",35}, 122 | {"takju-imm76i-factory-e8c33767.tgz",33,"takju-imm76i/image-takju-imm76i.zip",35}, 123 | {"takju-jdq39-factory-5e273f02.tgz",32,"takju-jdq39/image-takju-jdq39.zip",33}, 124 | {"takju-jwr66y-factory-5104ab1d.tgz",33,"takju-jwr66y/image-takju-jwr66y.zip",35}, 125 | {"takju-jzo54k-factory-92830c0b.tgz",33,"takju-jzo54k/image-takju-jzo54k.zip",35}, 126 | {"tungsten-ian67k-factory-468d9865.tgz",36,"tungsten-ian67k/image-tungsten-ian67k.zip",41}, 127 | {"volantisg-lrx22c-factory-a9668749.tgz",37,"volantisg-lrx22c/image-volantisg-lrx22c.zip",43}, 128 | {"volantis-lrx21l-factory-dc7e3fc7.tgz",36,"volantis-lrx21l/image-volantis-lrx21l.zip",41}, 129 | {"volantis-lrx21q-factory-10521789.tgz",36,"volantis-lrx21q/image-volantis-lrx21q.zip",41}, 130 | {"volantis-lrx21r-factory-ac87eba2.tgz",36,"volantis-lrx21r/image-volantis-lrx21r.zip",41}, 131 | {"volantis-lrx22c-factory-8d83cd9b.tgz",36,"volantis-lrx22c/image-volantis-lrx22c.zip",41}, 132 | {"yakju-imm76i-factory-8001e72f.tgz",33,"yakju-imm76i/image-yakju-imm76i.zip",35}, 133 | {"yakju-jdq39-factory-b2ebb5f3.tgz",32,"yakju-jdq39/image-yakju-jdq39.zip",33}, 134 | {"yakju-jwr66y-factory-09207065.tgz",33,"yakju-jwr66y/image-yakju-jwr66y.zip",35}, 135 | {"yakju-jzo54k-factory-92ff9457.tgz",33,"yakju-jzo54k/image-yakju-jzo54k.zip",35}, 136 | { NULL,-1, NULL, -1 }, 137 | } ; 138 | #endif 139 | -------------------------------------------------------------------------------- /lib/include/private/kernel.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/kernel.h 17 | * 18 | */ 19 | 20 | #ifndef _b24c0928_81d6_11e4_8336_5404a601fa9d 21 | #define _b24c0928_81d6_11e4_8336_5404a601fa9d 22 | #include 23 | #include 24 | 25 | 26 | #define UNCOMPRESSED_KERNEL_SIZE_32MB ( (1024 * 1024 ) *32 ) 27 | 28 | #define KERNEL_VERSION_STRING "Linux version" 29 | #define KERNEL_VERSION_STRING_SIZE 13 30 | 31 | #define KERNEL_COMPRESSION_MAGIC_LZ4 "\x02\x21" 32 | #define KERNEL_COMPRESSION_MAGIC_LZ4_SIZE 2 33 | #define KERNEL_COMPRESSION_MAGIC_GZIP "\x1F\x8B\x08" 34 | #define KERNEL_COMPRESSION_MAGIC_GZIP_SIZE 3 35 | #define KERNEL_COMPRESSION_MAGIC_LZOP "\x89\x4C\x5A" 36 | #define KERNEL_COMPRESSION_MAGIC_LZOP_SIZE 3 37 | #define KERNEL_COMPRESSION_MAGIC_BZIP2 "\x42\x5A\x68" /* B Z h */ 38 | #define KERNEL_COMPRESSION_MAGIC_BZIP2_SIZE 3 39 | #define KERNEL_COMPRESSION_MAGIC_XZ "\xFD\x37\x7A\x58\x5A\x00" /* \xFD 7z X Z \x00 */ 40 | #define KERNEL_COMPRESSION_MAGIC_XZ_SIZE 6 41 | #define KERNEL_COMPRESSION_MAGIC_LZMA "\x5D\x00\x00\x00" 42 | #define KERNEL_COMPRESSION_MAGIC_LZMA_SIZE 4 43 | 44 | enum kernel_compression_type { 45 | KERNEL_COMPRESSION_TYPE_UNKNOWN = 0 , 46 | KERNEL_COMPRESSION_TYPE_LZ4, 47 | KERNEL_COMPRESSION_TYPE_GZIP, 48 | KERNEL_COMPRESSION_TYPE_LZOP, 49 | KERNEL_COMPRESSION_TYPE_BZIP2, 50 | KERNEL_COMPRESSION_TYPE_XZ, 51 | KERNEL_COMPRESSION_TYPE_LZMA, 52 | KERNEL_COMPRESSION_TYPE_MAX = KERNEL_COMPRESSION_TYPE_LZMA 53 | }; 54 | 55 | #define KERNEL_COMPRESSION_TYPE_STRING_UNKNOWN "unknown" 56 | #define KERNEL_COMPRESSION_TYPE_STRING_LZ4 "lz4" 57 | #define KERNEL_COMPRESSION_TYPE_STRING_GZIP "gzip" 58 | #define KERNEL_COMPRESSION_TYPE_STRING_LZOP "lzop" 59 | #define KERNEL_COMPRESSION_TYPE_STRING_BZIP2 "bzip2" 60 | #define KERNEL_COMPRESSION_TYPE_STRING_XZ "xz" 61 | #define KERNEL_COMPRESSION_TYPE_STRING_LZMA "lzma" 62 | 63 | 64 | static struct kernel_type_t { 65 | int compression_type ; 66 | char* compression_type_string ; 67 | char* magic ; 68 | uint32_t magic_size ; 69 | } kernel_type[] = { 70 | { KERNEL_COMPRESSION_TYPE_LZ4 , KERNEL_COMPRESSION_TYPE_STRING_LZ4 , KERNEL_COMPRESSION_MAGIC_LZ4 , KERNEL_COMPRESSION_MAGIC_LZ4_SIZE }, 71 | { KERNEL_COMPRESSION_TYPE_GZIP , KERNEL_COMPRESSION_TYPE_STRING_GZIP , KERNEL_COMPRESSION_MAGIC_GZIP , KERNEL_COMPRESSION_MAGIC_GZIP_SIZE }, 72 | { KERNEL_COMPRESSION_TYPE_LZOP , KERNEL_COMPRESSION_TYPE_STRING_LZOP , KERNEL_COMPRESSION_MAGIC_LZOP , KERNEL_COMPRESSION_MAGIC_LZOP_SIZE }, 73 | { KERNEL_COMPRESSION_TYPE_BZIP2 , KERNEL_COMPRESSION_TYPE_STRING_BZIP2 , KERNEL_COMPRESSION_MAGIC_BZIP2 , KERNEL_COMPRESSION_MAGIC_BZIP2_SIZE }, 74 | { KERNEL_COMPRESSION_TYPE_XZ , KERNEL_COMPRESSION_TYPE_STRING_XZ , KERNEL_COMPRESSION_MAGIC_XZ , KERNEL_COMPRESSION_MAGIC_XZ_SIZE }, 75 | { KERNEL_COMPRESSION_TYPE_LZMA , KERNEL_COMPRESSION_TYPE_STRING_LZMA , KERNEL_COMPRESSION_MAGIC_LZMA , KERNEL_COMPRESSION_MAGIC_LZMA_SIZE }, 76 | { KERNEL_COMPRESSION_TYPE_UNKNOWN , KERNEL_COMPRESSION_TYPE_STRING_UNKNOWN , NULL , 0 } 77 | }; 78 | 79 | 80 | struct bootimage ; 81 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_kernel_compression_type(struct bootimage* bi); 82 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_kernel_decompress(struct bootimage* bi); 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /lib/include/private/print.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/print.h 17 | * This file contains the strings used by the bootimage*_print functions 18 | */ 19 | 20 | #ifndef _ed8c3286_8197_11e4_a167_5404a601fa9d 21 | #define _ed8c3286_8197_11e4_a167_5404a601fa9d 22 | 23 | /* Layout : Use spaces the colon (:) is at column 27 */ 24 | 25 | #define BOOTIMAGE_PRINT_HEADER "\ 26 | Header: \n\ 27 | kernel size : %u\n\ 28 | kernel addr : 0x%8x\n\ 29 | ramdisk size : %u\n\ 30 | ramdisk addr : 0x%8x\n\ 31 | second size : %u\n\ 32 | second addr : 0x%8x\n\ 33 | tags addr : 0x%8x\n\ 34 | page size : %u\n\ 35 | dt size : %u\n\ 36 | name : %s\n\ 37 | cmd line : %s\n\ 38 | extra cmd line : %s\n\ 39 | " 40 | 41 | #define BOOTIMAGE_PRINT_KERNEL_HEADER "\ 42 | Kernel:\n\ 43 | " 44 | 45 | #define BOOTIMAGE_PRINT_KERNEL "\ 46 | compression type : %s\n\ 47 | uncompressed size : %u\n\ 48 | version : %s\n\ 49 | " 50 | #define BOOTIMAGE_PRINT_KERNEL_CONFIG "\ 51 | config.gz size : %u\n\ 52 | config size : %u\n\ 53 | config entry count : %d\n\ 54 | " 55 | #define BOOTIMAGE_PRINT_KERNEL_INITRD "\ 56 | initrd compression type : %s\n\ 57 | initrd size : %u\n\ 58 | initrd uncompressed size : %u\n\ 59 | " 60 | 61 | #define BOOTIMAGE_PRINT_EXTRACT_HEADER "\ 62 | kernel_size=%u\n\ 63 | kernel_addr=0x%8x\n\ 64 | ramdisk_size=%u\n\ 65 | ramdisk_addr=0x%8x\n\ 66 | second_size=%u\n\ 67 | second_addr=0x%8x\n\ 68 | tags_addr=0x%8x\n\ 69 | page_size=%u\n\ 70 | dt_size=%u\n\ 71 | name=%s\n\ 72 | cmd_line=%s\n\ 73 | extra_cmd_line=%s\n\ 74 | " 75 | 76 | #define BOOTIMAGE_PRINT_COMPRESSION_TYPE "\ 77 | compression type : %s\n\ 78 | " 79 | 80 | /* #define BOOTIMAGE_PRINT_KERNEL_HEADER " */ 81 | 82 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_structure_print_header(struct bootimage* bi); 83 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_structure_print_kernel(struct bootimage* bi); 84 | #endif 85 | -------------------------------------------------------------------------------- /lib/include/private/trace.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/trace.h 17 | * 18 | */ 19 | 20 | #ifndef _95a5126a_81b8_11e4_bea0_5404a601fa9d 21 | #define _95a5126a_81b8_11e4_bea0_5404a601fa9d 22 | 23 | /* IMPORTANT: if you change the following list, don't 24 | * forget to update the corresponding 'tags' table in 25 | * the adb_trace_init() function implemented in adb.c 26 | */ 27 | #include 28 | #include 29 | #include 30 | 31 | typedef enum { 32 | TRACE_ALL = 0, 33 | TRACE_API_BOOTIMAGE_UTILS, 34 | TRACE_API_BOOTIMAGE, 35 | TRACE_API_BOOTIMAGE_EXTRACT, 36 | TRACE_API_BOOTIMAGE_FILE, 37 | TRACE_API_BOOTIMAGE_FILE_EXTRACT, 38 | TRACE_API_BOOTIMAGE_FILE_PRINT, 39 | TRACE_API_BOOTIMAGE_PRINT, 40 | TRACE_PRIVATE_ARCHIVE, 41 | TRACE_PRIVATE_BOOTIMAGE, 42 | TRACE_PRIVATE_CHECKS, 43 | TRACE_PRIVATE_KERNEL, 44 | TRACE_PRIVATE_TRACE, 45 | TRACE_PRIVATE_UTILS, 46 | TRACE_PRIVATE_PRINT, 47 | } bitrace; 48 | 49 | extern __LIBBOOTIMAGE_PRIVATE_API__ int trace_mask; 50 | 51 | __LIBBOOTIMAGE_PRIVATE_API__ void trace_init(void); 52 | 53 | #define TRACING ((trace_mask & (1 << TRACE_TAG)) != 0) 54 | 55 | # define D(...) \ 56 | if (TRACING) { \ 57 | int save_errno = errno; \ 58 | fprintf(stderr, "%s:%d ", \ 59 | __FUNCTION__,__LINE__); \ 60 | errno = save_errno; \ 61 | fprintf(stderr, __VA_ARGS__ ); \ 62 | fprintf(stderr,"\n"); \ 63 | fflush(stderr); \ 64 | errno = save_errno; \ 65 | } 66 | 67 | 68 | # define E(...) \ 69 | if (TRACING) { \ 70 | int save_errno = errno; \ 71 | fprintf(stderr, "ERROR:%s:%d ", \ 72 | __FUNCTION__,__LINE__); \ 73 | errno = save_errno; \ 74 | fprintf(stderr, __VA_ARGS__ ); \ 75 | fprintf(stderr,"\n"); \ 76 | fflush(stderr); \ 77 | errno = save_errno; \ 78 | } 79 | 80 | # define ENULLARG(...) \ 81 | if (TRACING) { \ 82 | int save_errno = errno; \ 83 | fprintf(stderr, "ERROR:%s:%d ", \ 84 | __FUNCTION__,__LINE__); \ 85 | errno = save_errno; \ 86 | fprintf(stderr, __VA_ARGS__ ); \ 87 | fprintf(stderr," is null\n"); \ 88 | fflush(stderr); \ 89 | errno = save_errno; \ 90 | } 91 | #endif 92 | -------------------------------------------------------------------------------- /lib/include/private/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/include/private/utils.h 17 | * 18 | */ 19 | 20 | #ifndef _b4cab7f2_80b0_11e4_a8e8_5404a601fa9d 21 | #define _b4cab7f2_80b0_11e4_a8e8_5404a601fa9d 22 | #include 23 | #include 24 | #include 25 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_mkdir_and_parents_umask(char *path,unsigned mode, mode_t umask); 26 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_mkdir_and_parents(char *path,unsigned mode); 27 | __LIBBOOTIMAGE_PRIVATE_API__ char* utils_dirname( char* s); 28 | __LIBBOOTIMAGE_PRIVATE_API__ char* utils_basename( char* s); 29 | 30 | __LIBBOOTIMAGE_PRIVATE_API__ ssize_t utils_write_all_fd (int fd, const void* buffer, ssize_t count); 31 | __LIBBOOTIMAGE_PRIVATE_API__ ssize_t utils_write_all (char* file_name,mode_t mode, const void* buffer, ssize_t count); 32 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_read_all(char* file_name,char** buffer ,struct stat* st); 33 | 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/private/archive.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/archive.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_PRIVATE_ARCHIVE 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | #include 34 | #include 35 | 36 | #define ARCHIVE_ENTRY_DEFAULT_SIZE_32MB ( (1024 * 1024 ) *32 ) 37 | 38 | struct bootimage_utils_archive { 39 | struct archive* archive; 40 | struct archive_entry* archive_entry; 41 | }; 42 | 43 | struct bootimage_utils_archive_entry { 44 | char* name ; 45 | int should_extract ; 46 | char* data ; 47 | 48 | }; 49 | struct bootimage_utils_archive_entries { 50 | 51 | struct archive* archive; 52 | struct bootimage_utils_archive_entry** archive_entries; 53 | }; 54 | 55 | 56 | __LIBBOOTIMAGE_PRIVATE_API__ static struct archive* __archive_read_init() 57 | { 58 | struct archive* a = archive_read_new(); 59 | if( a == NULL ){ 60 | /* Failed to initialize libarchive reading */ 61 | errno = EBIARCHIVEREAD; 62 | return NULL ; 63 | } 64 | /* */ 65 | if ( archive_read_support_filter_all(a) == ARCHIVE_WARN ){ 66 | errno = EBIARCHIVEREADFILTER ; 67 | archive_read_free(a); 68 | return NULL ; 69 | } 70 | 71 | if ( archive_read_support_format_all(a) == ARCHIVE_FATAL ){ 72 | errno = EBIARCHIVEREADFORMAT ; 73 | archive_read_free(a); 74 | return NULL ; 75 | } 76 | if ( archive_read_support_format_raw(a) == ARCHIVE_FATAL ){ 77 | archive_read_free(a); 78 | errno = EBIARCHIVEREADFORMATRAW ; 79 | return NULL; 80 | } 81 | errno = EBIOK; 82 | 83 | return a; 84 | 85 | 86 | } 87 | 88 | __LIBBOOTIMAGE_PRIVATE_API__ static struct archive* __archive_read_memory(char* data , ssize_t data_size) 89 | { 90 | struct archive* a = __archive_read_init(); 91 | if ( a == NULL ){ 92 | return NULL; 93 | } 94 | int r = archive_read_open_memory(a, data,data_size); 95 | if (r != ARCHIVE_OK){ 96 | archive_error_string(a); 97 | D("r=%d %s",r,archive_error_string(a)); 98 | archive_read_free(a); 99 | return NULL; 100 | } 101 | return a; 102 | } 103 | 104 | __LIBBOOTIMAGE_PRIVATE_API__ unsigned int archive_gzip_get_uncompressed_size(char* data,off_t data_size) 105 | { 106 | 107 | if (data == NULL ){ 108 | ENULLARG("data"); 109 | return -1; 110 | } 111 | if (data_size == 0 ){ 112 | ENULLARG("data"); 113 | return -1; 114 | } 115 | int* last_four_bytes = data+(data_size-4); 116 | int last_four_bytes_int = *last_four_bytes; 117 | D("last_four_bytes_int=%d",last_four_bytes_int); 118 | 119 | return last_four_bytes_int; 120 | 121 | } 122 | __LIBBOOTIMAGE_PRIVATE_API__ unsigned int archive_list_entries(char* archive_data,off_t archive_size,char** entries) 123 | { 124 | 125 | if (archive_data == NULL ){ 126 | ENULLARG("data"); 127 | return -1; 128 | } 129 | if (archive_size == 0 ){ 130 | ENULLARG("data"); 131 | return -1; 132 | } 133 | 134 | 135 | struct archive *a = __archive_read_memory(archive_data,archive_size); 136 | if ( a == NULL ){ 137 | return -1 ; 138 | } 139 | 140 | 141 | return 0; 142 | 143 | } 144 | 145 | 146 | 147 | __LIBBOOTIMAGE_PRIVATE_API__ static char* __archive_extract_entry( struct archive *a, struct archive_entry *entry,size_t* entry_size) 148 | { 149 | if(entry == NULL){ 150 | D("ERROR:entry argument is null"); 151 | return NULL ; 152 | } 153 | 154 | 155 | *entry_size = archive_entry_size(entry); 156 | if(*entry_size == 0 ){ 157 | /* we could not get the entry size from archive entry 158 | so we will use the default size of 32MB instead */ 159 | *entry_size = ARCHIVE_ENTRY_DEFAULT_SIZE_32MB; 160 | D("WARNING:Using Default Entry Size"); 161 | } 162 | 163 | D("archive_entry_size=%lu",*entry_size); 164 | 165 | 166 | /* Allocate the memory for the uncompressed entry data */ 167 | char* entry_data = calloc(*entry_size,sizeof(unsigned char)); 168 | if ( entry_data == NULL ){ 169 | D("ERROR:Failed to allocate memory for entry data"); 170 | return NULL ; 171 | } 172 | 173 | 174 | *entry_size = archive_read_data(a,entry_data,*entry_size); 175 | if ( *entry_size == ARCHIVE_FATAL ){ 176 | /* there was a fatal error; the archive should be closed immediately */ 177 | D("archive_read_data=ARCHIVE_FATAL"); 178 | free(entry_data); 179 | return NULL; 180 | } 181 | return entry_data ; 182 | 183 | 184 | 185 | } 186 | 187 | __LIBBOOTIMAGE_PRIVATE_API__ static int __archive_extract_entry_file( struct archive *a, struct archive_entry *entry,char* output_file_name) 188 | { 189 | if(output_file_name == NULL ) { 190 | D("INFO:using entry name"); 191 | output_file_name = archive_entry_pathname(entry); 192 | if( output_file_name == NULL ){ 193 | D("ERROR:archive_entry_pathname failed"); 194 | return -1; 195 | } 196 | } 197 | /* Get the output directory name */ 198 | char* output_dir_name = utils_dirname(output_file_name) ; 199 | D("output_dir_name=%s",output_dir_name); 200 | 201 | if(output_dir_name != NULL ) { 202 | /* Create the output directory if needed */ 203 | if ( utils_mkdir_and_parents_umask(output_dir_name,0755,0) == -1 ){ 204 | D("ERROR:Failed to create output directory %s",output_dir_name); 205 | free(output_dir_name); 206 | return -1 ; 207 | } 208 | free(output_dir_name); 209 | } 210 | ssize_t entry_size =0 ; 211 | char* entry_data = __archive_extract_entry(a,entry,&entry_size); 212 | 213 | 214 | mode_t mode = archive_entry_mode(entry) ; 215 | if ( utils_write_all(output_file_name,mode,entry_data,entry_size) == -1 ){ 216 | D("ERROR:Failed to write entry data real_entry_size:%lu",entry_size); 217 | free(entry_data); 218 | return -1 ; 219 | } 220 | free(entry_data); 221 | return 0; 222 | } 223 | __LIBBOOTIMAGE_PRIVATE_API__ static struct bootimage_utils_archive* __archive_find_entry_in_archive_data(struct bootimage_utils_archive* biua ,char* name,size_t name_length) 224 | { 225 | int break_error = 0 ; 226 | int break_found = 0 ; 227 | while (archive_read_next_header( biua->archive, &biua->archive_entry) == ARCHIVE_OK) { 228 | /* Get the entry information we need */ 229 | char* entry_name = archive_entry_pathname(biua->archive_entry) ; 230 | 231 | D("archive_entry_pathname=%s fi.zip_name=%s",entry_name,name,name_length); 232 | if ( !strncmp( entry_name , name ,name_length) ){ 233 | break_found = 1 ; 234 | break; 235 | } 236 | } 237 | if(break_found == 0 ){ 238 | archive_read_free(biua->archive); 239 | free(biua); 240 | } 241 | return biua; 242 | } 243 | __LIBBOOTIMAGE_PRIVATE_API__ static struct bootimage_utils_archive* __archive_find_entry(char* data,off_t data_size,char* name,size_t name_length) 244 | { 245 | struct bootimage_utils_archive* biua = calloc(1,sizeof(struct bootimage_utils_archive)); 246 | biua->archive = __archive_read_memory(data,data_size) ; 247 | if ( biua->archive == NULL ){ 248 | return NULL ; 249 | } 250 | if ( name_length == 0 ){ 251 | name_length = utils_sanitize_string(name,PATH_MAX); 252 | } 253 | return __archive_find_entry_in_archive_data(biua,name,name_length); 254 | 255 | 256 | } 257 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_file(char* archive_data,off_t archive_data_size,char* name,size_t name_length) 258 | { 259 | 260 | 261 | struct bootimage_utils_archive* biua = __archive_find_entry(archive_data,archive_data_size,name,name_length); 262 | if( biua == NULL ){ 263 | return 0; 264 | } 265 | char* entry_data = NULL ; 266 | switch ( archive_entry_filetype(biua->archive_entry) ){ 267 | case AE_IFREG:{ // Entry File Type is a Regular File 268 | __archive_extract_entry_file(biua->archive,biua->archive_entry,NULL); 269 | 270 | 271 | } 272 | default: 273 | break ; 274 | } 275 | 276 | archive_read_free(biua->archive); 277 | free(biua); 278 | 279 | return 0; 280 | } 281 | 282 | 283 | __LIBBOOTIMAGE_PRIVATE_API__ char* archive_extract_entry(char* data,off_t data_size,char* name,size_t name_length,size_t* entry_size){ 284 | 285 | 286 | struct bootimage_utils_archive* biua = __archive_find_entry(data,data_size,name,name_length); 287 | if( biua == NULL ){ 288 | return NULL; 289 | } 290 | char* entry_data = NULL ; 291 | switch ( archive_entry_filetype(biua->archive_entry) ){ 292 | case AE_IFREG:{ // Entry File Type is a Regular File 293 | entry_data = __archive_extract_entry(biua->archive,biua->archive_entry,entry_size); 294 | D("entry_data=%p",entry_data); 295 | 296 | } 297 | default: 298 | break ; 299 | } 300 | 301 | 302 | archive_read_free(biua->archive); 303 | free(biua); 304 | 305 | return entry_data; 306 | 307 | 308 | } 309 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_memory_file( char* archive_data , uint64_t archive_size, char* entry_name, char* output_file_name) 310 | { 311 | 312 | 313 | 314 | /* if ( check_output_name(output_file_name) == -1 ) { 315 | return -1 ; 316 | } 317 | int entry_name_len = check_output_name(entry_name) ; 318 | if ( entry_name_len == -1 ) { 319 | return -1 ; 320 | } 321 | 322 | struct archive *a = archive_read_memory(archive_data,archive_size); 323 | if ( a == NULL ){ 324 | return -1 ; 325 | } 326 | D("a=%p\n",a); 327 | 328 | struct archive_entry *entry = NULL; 329 | int break_error = 0 ; 330 | int break_found = 0 ; 331 | while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {*/ 332 | /* Get the entry information we need */ 333 | /*char* name = archive_entry_pathname(entry) ; 334 | D("archive_entry_pathname=%s",name); 335 | if ( !strncmp( name , entry_name,entry_name_len) ){ 336 | switch ( archive_entry_filetype(entry) ){ 337 | case AE_IFREG:{ /* Entry File Type is a Regular File */ 338 | /* break_found = 1 ; 339 | if ( __archive_extract_entry_file(a,entry,output_file_name) == -1 ) { 340 | break_error = 1; 341 | } 342 | 343 | } 344 | default: 345 | break ; 346 | } 347 | } 348 | if ( (break_found == 1)|| (break_error == 1) ){ 349 | break ; 350 | } 351 | } 352 | archive_read_free(a); 353 | if ( break_error == 1 ) { 354 | return -1 ; 355 | }*/ 356 | return 0; 357 | 358 | 359 | } 360 | 361 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_all_memory_directory( char* archive_data , uint64_t archive_size, char* output_dir_name) 362 | { 363 | 364 | if ( check_output_name(output_dir_name) == -1 ) { 365 | return -1 ; 366 | } 367 | 368 | struct archive *a = __archive_read_memory(archive_data,archive_size); 369 | if ( a == NULL ){ 370 | return -1 ; 371 | } 372 | D("archive_compression_name=%s",archive_compression_name(a)); 373 | if ( archive_extract_all(a,output_dir_name) == -1 ){ 374 | int en = errno ; 375 | archive_read_free(a); 376 | errno = en ; 377 | return -1 ; 378 | } 379 | archive_read_free(a); 380 | return 0; 381 | } 382 | 383 | __LIBBOOTIMAGE_PRIVATE_API__ int archive_extract_all(struct archive *a,char* output_dir_name) 384 | { 385 | if ( check_output_name(output_dir_name) == -1 ) { 386 | return -1 ; 387 | } 388 | if ( utils_mkdir_and_parents_umask(output_dir_name,0755,0) == -1 ){ 389 | D("ERROR:Failed to create output directory %s",output_dir_name); 390 | return -1 ; 391 | } 392 | char* cwd = calloc(PATH_MAX,sizeof(char)); 393 | if( cwd == NULL ) { 394 | D("ERROR:Failed to allocate memory for cwd"); 395 | return -1 ; 396 | } 397 | if ( getcwd(cwd, PATH_MAX) == NULL ){ 398 | D("ERROR:getcwd failed"); 399 | free(cwd); 400 | return -1; 401 | } 402 | if ( chdir(output_dir_name) == -1 ){ 403 | D("ERROR:chdir failed errno=%d",errno); 404 | free(cwd); 405 | return -1; 406 | } 407 | 408 | struct archive_entry *entry = NULL; 409 | 410 | while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { 411 | switch ( archive_entry_filetype(entry) ){ 412 | case AE_IFDIR:{ /* Entry File Type is a Directory */ 413 | mode_t mode = archive_entry_mode(entry) ; 414 | char* name = archive_entry_pathname(entry) ; 415 | utils_mkdir_and_parents(name,mode); 416 | archive_read_data_skip(a); 417 | break; 418 | } 419 | case AE_IFLNK:{ /* Entry File Type is a Symbolic Link */ 420 | char* name = archive_entry_pathname(entry) ; 421 | #ifndef _WIN32 422 | symlink(archive_entry_symlink(entry), name); 423 | #endif 424 | archive_read_data_skip(a); 425 | break; 426 | } 427 | case AE_IFREG:{ /* Entry File Type is a Regular File */ 428 | __archive_extract_entry_file(a,entry,NULL); 429 | break; 430 | 431 | } 432 | default:{ /* Entry File Type is a something else and unsupported */ 433 | 434 | archive_read_data_skip(a); 435 | break; 436 | } 437 | } 438 | } 439 | if ( chdir(cwd) == -1 ){ 440 | D("ERROR:chdir cwd failed errno=%d",errno); 441 | free(cwd); 442 | return -1; 443 | } 444 | free(cwd); 445 | return 0 ; 446 | } 447 | -------------------------------------------------------------------------------- /lib/private/bootimage.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/bootimage.c 17 | * This file is part of the INTERNAL api for the bootimage utils project 18 | * 19 | */ 20 | #define TRACE_TAG TRACE_PRIVATE_BOOTIMAGE 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | 34 | __LIBBOOTIMAGE_PRIVATE_API__ static uint32_t calculate_padding(size_t section_size, uint32_t page_size); 35 | 36 | 37 | __LIBBOOTIMAGE_PRIVATE_API__ static uint32_t calculate_padding(size_t section_size, uint32_t page_size) 38 | { 39 | 40 | errno = 0 ; // clear the errno 41 | if(page_size == 0 || section_size == 0){ 42 | //D("Calculate Padding Error %d\n",errno); 43 | errno = EINVAL ; 44 | return 0 ; 45 | } 46 | uint32_t pagemask = page_size - 1; 47 | uint32_t padding_size = page_size - (section_size & pagemask); 48 | if(padding_size == page_size) padding_size = 0 ; 49 | //D("Calculate Padding Returns %d\n",padding_size); 50 | return padding_size ; 51 | } 52 | 53 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_mmap_file(struct bootimage* bi,const char* file_name) 54 | { 55 | 56 | D("bi->stat.st_size=%lu\n",bi->stat.st_size ); 57 | /* Open the file as read only, read for mmapping */ 58 | int bifd = open(file_name,O_RDONLY); 59 | D("bifd=%d\n",bifd); 60 | if(bifd < 0 ){ 61 | /* Could not open file. errno should be set already so return -1 */ 62 | return -1; 63 | } 64 | /* mmap the full file into memory */ 65 | 66 | bi->start = calloc(bi->stat.st_size,sizeof(char)); 67 | D("bi->start=%p\n",bi->start ); 68 | read(bifd,bi->start,bi->stat.st_size); 69 | close(bifd); 70 | D("Returning 0\n" ); 71 | return 0 ; 72 | 73 | 74 | 75 | } 76 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_magic_address(struct bootimage* bi) 77 | { 78 | /* Look for the ANDROID! boot magic in the mapped file area */ 79 | bi->header = utils_memmem(bi->start,bi->stat.st_size,BOOT_MAGIC,BOOT_MAGIC_SIZE); 80 | if( bi->header == NULL ){ 81 | /* set the error to no magic and return */ 82 | errno = EBINOMAGIC ; 83 | return -1 ; 84 | } 85 | return 0; 86 | } 87 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_header_section(struct bootimage* bi) 88 | { 89 | /* Store the size of the header struct for convience */ 90 | bi->header_size = sizeof(struct bootimage_header) ; 91 | /* Work out the padding for header section of the bootimage file */ 92 | bi->header_padding = calculate_padding(bi->header_size,bi->header->page_size); 93 | if ( bi->header_padding == -1 ) { 94 | /* set the error and return */ 95 | errno = EBIBADPADHEAD ; 96 | return -1 ; 97 | } 98 | errno = EBIOK; 99 | return 0; 100 | 101 | } 102 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_kernel_section(struct bootimage* bi) 103 | { 104 | /* Set the kernel address offset within the mmapped area. 105 | We could just use header offset + page_size as the header size should never 106 | span multiple pages ... but that is just not a safe assumption to make. 107 | 108 | We must also use the address of the first entry of the header structure 109 | we are pointing to ( i.e magic ) instead of the outer bi->header address 110 | Although these pointers have the same value doing pointer arithmetic on 111 | bi->header results in traversing the bootimage stricture rather than the 112 | mmapped area of which bi->header->magic[0] points at 113 | */ 114 | bi->kernel = bi->header->magic + bi->header_size + bi->header_padding ; 115 | 116 | /* Work out the padding for kernel section of the bootimage file */ 117 | bi->kernel_padding = calculate_padding(bi->header->kernel_size ,bi->header->page_size); 118 | if ( bi->kernel_padding == -1 ) { 119 | /* set the error and return */ 120 | errno = EBIBADPADKERNEL ; 121 | return -1 ; 122 | } 123 | 124 | errno = EBIOK; 125 | return 0; 126 | 127 | } 128 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_ramdisk_section(struct bootimage* bi) 129 | { 130 | /* Set the ramdisk address offset within the mmapped area */ 131 | bi->ramdisk = bi->kernel + bi->header->kernel_size + bi->kernel_padding ; 132 | 133 | /* Work out the padding for ramdisk section of the bootimage file */ 134 | bi->ramdisk_padding = calculate_padding(bi->header->ramdisk_size ,bi->header->page_size); 135 | if ( bi->ramdisk_padding == -1 ) { 136 | /* set the error and return */ 137 | errno = EBIBADPADRAMDISK ; 138 | return -1 ; 139 | } 140 | errno = EBIOK; 141 | return 0; 142 | 143 | } 144 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_second_section(struct bootimage* bi) 145 | { 146 | 147 | /* Set the second bootloader address offset within the mmapped area 148 | We must first check the second size in the header. If it is zero 149 | then we are not using the second bootloader section and the checks 150 | can be skipped 151 | */ 152 | if ( bi->header->second_size > 0 ){ 153 | bi->second = bi->ramdisk + bi->header->ramdisk_size + bi->ramdisk_padding ; 154 | 155 | /* Work out the padding for ramdisk section of the bootimage file */ 156 | bi->second_padding = calculate_padding(bi->header->second_size,bi->header->page_size); 157 | if ( bi->second_padding == -1 ) { 158 | 159 | /* set the error and return */ 160 | errno = EBIBADPADSECOND ; 161 | return -1 ; 162 | } 163 | 164 | } 165 | errno = EBIOK; 166 | return 0; 167 | 168 | } 169 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_set_sections(struct bootimage* bi) 170 | { 171 | 172 | if ( bootimage_set_header_section(bi) == -1 ){ 173 | return -1 ; 174 | } 175 | if ( bootimage_set_kernel_section(bi) == -1 ){ 176 | return -1 ; 177 | } 178 | if ( bootimage_set_ramdisk_section(bi) == -1 ){ 179 | return -1 ; 180 | } 181 | if ( bootimage_set_second_section(bi) == -1 ){ 182 | return -1 ; 183 | } 184 | return 0; 185 | } 186 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_file_read_magic(struct bootimage* bi,const char* file_name) 187 | { 188 | 189 | if( bootimage_mmap_file(bi,file_name) == -1 ){ 190 | return -1; 191 | } 192 | D("file_name=%s",file_name); 193 | if( bootimage_set_magic_address(bi) == -1 ){ 194 | return -1; 195 | } 196 | D("file_name=%s",file_name); 197 | return 0; 198 | } 199 | -------------------------------------------------------------------------------- /lib/private/bootimage_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/bootimage_utils.c 17 | * 18 | */ 19 | #include 20 | 21 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_nexus_factory_image(struct bootimage_utils* biu) 22 | { 23 | 24 | struct factory_images* fi = &factory_image_info[0] ; 25 | char* bname = utils_basename(biu->file_name); 26 | D("bname=%s",bname); 27 | while ( fi->name != NULL ){ 28 | /* D("fi=%p fi->name=%s fi->name_length=%d",fi,fi->name,fi->name_length); */ 29 | if ( strncmp(fi->name,bname,fi->name_length) == FACTORY_IMAGE_STRNCMP_MATCH_FOUND ){ 30 | D("Potential Factory Image File Found file_name=%s",biu->file_name); 31 | break ; 32 | } 33 | 34 | fi++; 35 | } 36 | if ( fi == NULL ) { 37 | return -1 ; 38 | } 39 | /* For this to be a potentially bona-fida factory image the Gzip identifaction must be at the start of the data */ 40 | char* magic = utils_memmem(biu->data,biu->stat.st_size,FACTORY_IMAGE_MAGIC_GZIP,FACTORY_IMAGE_MAGIC_GZIP_SIZE); 41 | D("Factory Image Gzip Magic %p : biu-data[0] %p",magic ,biu->data ) ; 42 | if ( magic != biu->data ){ 43 | return -1 ; 44 | } 45 | 46 | size_t zip_size ,boot_image_size; 47 | char* zip_data = archive_extract_entry(biu->data,biu->stat.st_size, fi->zip_name,fi->zip_name_length,&zip_size); 48 | 49 | 50 | 51 | // unsigned int uncompressed_size = archive_gzip_get_uncompressed_size(biu->compressed_data,biu->stat.st_size); 52 | 53 | D("Factory Image zip data %p : zip_size %zu",zip_data,zip_size) ; 54 | 55 | 56 | archive_extract_file(zip_data, zip_size,"boot.img",0); 57 | //D("Factory Image boot_image_data %p : boot_image_size %zu",boot_image_data,boot_image_size) ; 58 | //D("Factory Image Gzip Magic %p : biu-data[0] %p",magic ,biu->compressed_data ) ; 59 | //} 60 | 61 | } 62 | 63 | } 64 | 65 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_factory_image(struct bootimage_utils* biu) 66 | { 67 | 68 | 69 | } 70 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_update_zip_image(struct bootimage_utils* biu) 71 | { 72 | 73 | 74 | } 75 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_ota_update_zip_image(struct bootimage_utils* biu) 76 | { 77 | 78 | 79 | } 80 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_standard_boot_image(struct bootimage_utils* biu) 81 | { 82 | 83 | char* magic = utils_memmem(biu->data,biu->stat.st_size,BOOT_MAGIC,BOOT_MAGIC_SIZE); 84 | 85 | if ( ( magic == NULL ) || ( magic != biu->data ) ){ 86 | /* Boot Image Magic Not Found or Not at the start of the data */ 87 | return -1 ; 88 | } 89 | 90 | 91 | return 0 ; 92 | } 93 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_oem_boot_image(struct bootimage_utils* biu) 94 | { 95 | 96 | 97 | char* magic = utils_memmem(biu->data,biu->stat.st_size,BOOT_MAGIC,BOOT_MAGIC_SIZE); 98 | 99 | if ( ( magic == NULL ) ){ 100 | /* Boot Image Magic Not Found */ 101 | return -1 ; 102 | } 103 | 104 | } 105 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_recovery_boot_image(struct bootimage_utils* biu) 106 | { 107 | 108 | 109 | } 110 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_oem_recovery_image(struct bootimage_utils* biu) 111 | { 112 | 113 | 114 | } 115 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_standard_ramdisk(struct bootimage_utils* biu) 116 | { 117 | 118 | 119 | } 120 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_recovery_ramdisk(struct bootimage_utils* biu) 121 | { 122 | 123 | 124 | } 125 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_factory(struct bootimage_utils* biu) 126 | { 127 | 128 | 129 | } 130 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_is_compressed_kernel(struct bootimage_utils* biu) 131 | { 132 | 133 | 134 | } 135 | 136 | 137 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_utils_get_filetype(struct bootimage_utils* biu) 138 | { 139 | 140 | 141 | } 142 | 143 | 144 | -------------------------------------------------------------------------------- /lib/private/checks.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/checks.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_PRIVATE_CHECKS 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | /* libarchive archive.h */ 31 | #include 32 | 33 | #include 34 | 35 | 36 | 37 | 38 | /* check_output_name - checks the validity of the name argument */ 39 | __LIBBOOTIMAGE_PRIVATE_API__ int check_output_name(char* name) 40 | { 41 | D("name=%s",name); 42 | if ( name == NULL ){ 43 | errno = EBIOUTNAME ; 44 | return -1; 45 | } 46 | int size = utils_sanitize_string(name,PATH_MAX); 47 | if ( size > PATH_MAX-1){ 48 | errno = EBIOUTNAMELEN ; 49 | return -1; 50 | } 51 | if ( size <= 0){ 52 | errno = EBIOUTNAMELEN ; 53 | return -1; 54 | } 55 | errno = EBIOK; 56 | return size ; 57 | 58 | 59 | } 60 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_structure(struct bootimage* bi) 61 | { 62 | /* Validate inputs */ 63 | D("bi=%p",bi); 64 | if ( bi == NULL ) { 65 | errno = EBINULL; 66 | D("bi=%p errno=%d [ EBINULL ]",bi,errno); 67 | return -1; 68 | } 69 | errno = EBIOK; 70 | return 0; 71 | } 72 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_file_stat_size(struct bootimage* bi ,char* file_name) 73 | { 74 | /* Stat the boot image file an store it in the structure */ 75 | if (stat(file_name, &bi->stat) == -1){ 76 | D("bi->stat.st_size [ %jd ]", bi->stat.st_size); 77 | errno = EBISTAT; 78 | return -1 ; 79 | } 80 | /* File size is zero or could not be determined or 81 | File size is less than a minimum know page size 82 | This is probably not good. */ 83 | 84 | if ( ( bi->stat.st_size <= 0 ) || ( bi->stat.st_size < PAGE_SIZE_MIN ) ) { 85 | errno = EBIFSIZE; 86 | return -1 ; 87 | } 88 | errno = EBIOK; 89 | return 0; 90 | } 91 | __LIBBOOTIMAGE_PRIVATE_API__ int check_file_name_and_access(char* file_name) 92 | { 93 | /* Check the file_name is valid */ 94 | if ( file_name == NULL ) { 95 | errno = EBIFNAME; 96 | return -1 ; 97 | } 98 | 99 | /* Does the file exist? , can we read it? */ 100 | if ( access(file_name , R_OK ) == -1 ) { 101 | errno = EBIFACCESS ; 102 | return -1 ; 103 | } 104 | errno = EBIOK; 105 | return 0; 106 | } 107 | 108 | 109 | 110 | __LIBBOOTIMAGE_PRIVATE_API__ int check_ramdisk_entryname(char* entry_name) 111 | { 112 | if ( entry_name == NULL ){ 113 | errno = EBIRDENTRY ; 114 | return -1; 115 | } 116 | int entry_length = strnlen(entry_name,CPIO_FILE_NAME_MAX+1) ; 117 | if ( entry_length >= CPIO_FILE_NAME_MAX ){ 118 | errno = EBIRDENTRYLENGTH ; 119 | return -1; 120 | 121 | } 122 | errno = EBIOK; 123 | return entry_length; 124 | } 125 | 126 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_ramdisk(struct bootimage* bi) 127 | { 128 | if ( bi->ramdisk == NULL ){ 129 | errno = EBIRDMEM ; 130 | return -1; 131 | } 132 | if ( bi->header == NULL || bi->header->magic[0] == 0 ){ 133 | errno = EBIHEADMEM ; 134 | return -1; 135 | } 136 | D("bi->header->ramdisk_size=%d",bi->header->ramdisk_size) ; 137 | if ( bi->header->ramdisk_size <= 0 ){ 138 | errno = EBIRDMEMSIZE ; 139 | return -1; 140 | 141 | } 142 | errno = EBIOK; 143 | return 0; 144 | } 145 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_kernel(struct bootimage* bi) 146 | { 147 | 148 | /* check we have a valid header which is needed for the kernel size */ 149 | if ( bi->header == NULL || bi->header->magic[0] == 0 ){ 150 | errno = EBIHEADMEM ; 151 | D("bi->header=%p bi->header->magic[0]=%c errno=%d [ EBIKERNELMEMSIZE ]",bi->header , bi->header->magic[0], EBIHEADMEM) ; 152 | return -1; 153 | } 154 | /* check we have a "valid" kernel size in the header */ 155 | if ( bi->header->kernel_size <= 0 ){ 156 | errno = EBIKERNELMEMSIZE ; 157 | D("bi->header->kernel_size=%d errno=%d [ EBIKERNELMEMSIZE ]",bi->header->kernel_size,EBIKERNELMEMSIZE) ; 158 | return -1; 159 | 160 | } 161 | /* check we at least have a valid pointer to the compressed kernel memory */ 162 | if ( bi->kernel == NULL ){ 163 | errno = EBIKERNELMEM ; 164 | D("bi->kernel=NULL errno=%d [ EBIKERNELMEM ]",EBIKERNELMEM) ; 165 | return -1; 166 | } 167 | 168 | errno = EBIOK; 169 | return 0; 170 | } 171 | 172 | /* check_bootimage_file_read_magic - acts as a check wrapper for ultimately calling bootimage_file_read_magic */ 173 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_file_read_magic(struct bootimage* bi,char* file_name) 174 | { 175 | if ( check_bootimage_structure(bi) == -1){ 176 | D("bootimage_file_read check_bootimage_structure failed [ %p ]", bi); 177 | return -1; 178 | } 179 | 180 | if( check_file_name_and_access(file_name) == -1 ){ 181 | D("bootimage_file_read check_bootimage_file_name failed [ %p ]", bi); 182 | return -1; 183 | } 184 | 185 | if( check_bootimage_file_stat_size(bi,file_name) == -1 ){ 186 | return -1; 187 | } 188 | bootimage_file_read_magic(bi,file_name); 189 | errno = EBIOK; 190 | return 0; 191 | } 192 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_structure(struct bootimage_utils* biu) 193 | { 194 | /* Validate inputs */ 195 | D("bi=%p",biu); 196 | if ( biu == NULL ) { 197 | errno = EBIUNULL; 198 | D("biu=%p errno=%d [ EBINULL ]",biu,errno); 199 | return -1; 200 | } 201 | if ( ( biu->filetype < 0 ) || ( biu->filetype > BOOTIMAGE_UTILS_FILETYPE_MAX ) ) { 202 | errno = EBIUFILETYPE ; 203 | D("biu=%p errno=%d [ EBIUFILETYPE ]",biu,errno); 204 | return -1; 205 | 206 | } 207 | 208 | errno = EBIOK; 209 | return 0; 210 | } 211 | __LIBBOOTIMAGE_PRIVATE_API__ int validate_file_stat_size(struct stat* st,char* file_name) 212 | { 213 | /* Stat the boot image file an store it in the structure */ 214 | 215 | if (stat(file_name, st) == -1){ 216 | D("biu->stat.st_size [ %jd ]", st->st_size); 217 | errno = EBIUSTAT; 218 | return -1 ; 219 | } 220 | /* File size is zero or could not be determined or 221 | File size is less than a minimum know page size 222 | This is probably not good. */ 223 | 224 | if ( ( st->st_size <= 0 ) ) { 225 | errno = EBIFSIZE; 226 | return -1 ; 227 | } 228 | D("biu->stat.st_size [ %jd ]", st->st_size); 229 | errno = EBIOK; 230 | return 0; 231 | } 232 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_file_type(struct bootimage_utils* biu) 233 | { 234 | /* File Type Identification strategy 235 | 236 | Factory Image Identification 237 | Check for a known factory image file name. 238 | Check if the file is a valid gzip magic. 239 | */ 240 | 241 | struct factory_images* fi = &factory_image_info[0] ; 242 | char* bname = utils_basename(biu->file_name); 243 | D("bname=%s",bname); 244 | while ( fi->name != NULL ){ 245 | /* D("fi=%p fi->name=%s fi->name_length=%d",fi,fi->name,fi->name_length); */ 246 | if ( strncmp(fi->name,bname,fi->name_length) == FACTORY_IMAGE_STRNCMP_MATCH_FOUND ){ 247 | D("Potential Factory Image File Found file_name=%s",biu->file_name); 248 | break ; 249 | } 250 | 251 | fi++; 252 | } 253 | 254 | if ( fi != NULL ) { 255 | /* For this to be a potentially bona-fida factory image the Gzip identifaction must be at the 256 | start of the data */ 257 | 258 | //char* magic = utils_memmem(biu->compressed_data,biu->stat.st_size,FACTORY_IMAGE_MAGIC_GZIP,FACTORY_IMAGE_MAGIC_GZIP_SIZE); 259 | //D("Factory Image Gzip Magic %p : biu-data[0] %p",magic ,biu->compressed_data ) ; 260 | //if ( magic == biu->compressed_data ){ 261 | // unsigned int uncompressed_size = archive_gzip_get_uncompressed_size(biu->compressed_data,biu->stat.st_size); 262 | size_t zip_size ,boot_image_size; 263 | char* zip_data = archive_extract_entry(biu->data,biu->stat.st_size, fi->zip_name,fi->zip_name_length,&zip_size); 264 | D("Factory Image zip data %p : zip_size %zu",zip_data,zip_size) ; 265 | 266 | archive_extract_file(zip_data, zip_size,"boot.img",0); 267 | //D("Factory Image boot_image_data %p : boot_image_size %zu",boot_image_data,boot_image_size) ; 268 | //D("Factory Image Gzip Magic %p : biu-data[0] %p",magic ,biu->compressed_data ) ; 269 | //} 270 | 271 | } 272 | return 0; 273 | } 274 | __LIBBOOTIMAGE_PRIVATE_API__ int check_bootimage_utils_file_read(struct bootimage_utils* biu,char* file_name) 275 | { 276 | if ( check_bootimage_utils_structure(biu) == -1){ 277 | D("check_bootimage_utils_structure failed [ %p ]", biu); 278 | return -1; 279 | } 280 | 281 | biu->file_name = file_name; 282 | D("biu->compressed_data=%p",biu->data); 283 | 284 | 285 | utils_read_all(file_name,&biu->data,&biu->stat); 286 | D("biu->data=%p",biu->data); 287 | D("biu->stat.st_size=%u",biu->stat.st_size); 288 | D("biu->data 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",biu->data[0],biu->data[1],biu->data[2],biu->data[3],biu->data[4],biu->data[5]); 289 | errno = EBIOK; 290 | return 0; 291 | } 292 | -------------------------------------------------------------------------------- /lib/private/kernel.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/kernel.c 17 | * 18 | */ 19 | 20 | #define TRACE_TAG TRACE_PRIVATE_KERNEL 21 | #include 22 | #include 23 | 24 | /* */ 25 | 26 | __LIBBOOTIMAGE_PRIVATE_API__ static int bootimage_kernel_set_compressed_data_offset(struct bootimage* bi) 27 | { 28 | int i = 0 ; 29 | 30 | 31 | for(i = 1 ; i <= KERNEL_COMPRESSION_TYPE_MAX ; i++){ 32 | 33 | bi->compressed_kernel_offset = utils_memmem(bi->kernel,bi->header->kernel_size,kernel_type[i].magic,kernel_type[i].magic_size); 34 | if ( bi->compressed_kernel_offset != NULL ){ 35 | bi->compressed_kernel_type = &kernel_type[i] ; 36 | bi->compressed_kernel_size = bi->header->kernel_size - ( bi->compressed_kernel_offset -bi->kernel); 37 | D("bi->compressed_kernel_offset=%p bi->compressed_kernel_type.compression_type=%d",bi->compressed_kernel_offset,bi->compressed_kernel_type->compression_type); 38 | break ; 39 | } 40 | } 41 | if( bi->compressed_kernel_offset == NULL ) { 42 | return -1 ; 43 | } 44 | return 0; 45 | } 46 | 47 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_kernel_decompress(struct bootimage* bi) 48 | { 49 | /* */ 50 | if ( check_bootimage_kernel(bi) == -1 ){ 51 | return -1; 52 | } 53 | if ( bootimage_kernel_set_compressed_data_offset(bi) == -1 ){ 54 | return -1 ; 55 | } 56 | 57 | 58 | struct archive *a = NULL ; 59 | //if ( check_archive_read_memory(bi->compressed_kernel_offset,bi->compressed_kernel_size ) == -1 ){ 60 | // return -1; 61 | //} 62 | 63 | D("archive_compression_name=%s",archive_compression_name(a)) ; 64 | 65 | struct archive_entry *entry = NULL; 66 | 67 | if ( archive_read_next_header(a, &entry) == ARCHIVE_OK) { 68 | uint64_t size = archive_entry_size(entry); 69 | if ( size <= 0 ){ 70 | size = UNCOMPRESSED_KERNEL_SIZE_32MB ; 71 | } 72 | bi->uncompressed_kernel = calloc(size,sizeof(char)); 73 | if ( bi->uncompressed_kernel == NULL ){ 74 | archive_read_free(a); 75 | return -1; 76 | } 77 | bi->uncompressed_kernel_size = archive_read_data(a,bi->uncompressed_kernel,size); 78 | if ( bi->uncompressed_kernel_size <= 0 ){ 79 | free(bi->uncompressed_kernel) ; 80 | archive_read_free(a); 81 | return -1; 82 | 83 | } 84 | D("bi->uncompressed_kernel_size=%llu",bi->uncompressed_kernel_size) ; 85 | } 86 | archive_read_free(a); 87 | 88 | bi->kernel_version_string = utils_memmem(bi->uncompressed_kernel,bi->uncompressed_kernel_size, KERNEL_VERSION_STRING,KERNEL_VERSION_STRING_SIZE); 89 | if ( bi->kernel_version_string == NULL ){ 90 | D("kstring is null"); 91 | return -1; 92 | } 93 | bi->kernel_version_string_length = utils_sanitize_string(bi->kernel_version_string,256); 94 | if ( bi->kernel_version_string_length <= 0 ) { 95 | return -1 ; 96 | } 97 | D("bi->kernel_version_string_length len %d",bi->kernel_version_string_length); 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /lib/private/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/print.c 17 | * 18 | */ 19 | 20 | #define TRACE_TAG TRACE_PRIVATE_PRINT 21 | #include 22 | #include 23 | #include 24 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_structure_print_header(struct bootimage* bi) 25 | { 26 | 27 | fprintf(stdout,"\n"BOOTIMAGE_PRINT_HEADER"\n", bi->header->kernel_size, 28 | bi->header->kernel_addr, 29 | bi->header->ramdisk_size, 30 | bi->header->ramdisk_addr, 31 | bi->header->second_size, 32 | bi->header->second_addr, 33 | bi->header->tags_addr, 34 | bi->header->page_size, 35 | bi->header->dt_size, 36 | bi->header->name, 37 | bi->header->cmdline, 38 | bi->header->extra_cmdline); 39 | return 0; 40 | } 41 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_structure_print_kernel(struct bootimage* bi) 42 | { 43 | 44 | fprintf(stdout,"\n"BOOTIMAGE_PRINT_KERNEL_HEADER""BOOTIMAGE_PRINT_KERNEL"\n", bi->compressed_kernel_type->compression_type_string, 45 | bi->uncompressed_kernel_size, 46 | bi->kernel_version_string); 47 | return 0; 48 | } 49 | __LIBBOOTIMAGE_PRIVATE_API__ int bootimage_structure_print_ramdisk(struct bootimage* bi) 50 | { 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /lib/private/trace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/trace.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_PRIVATE_TRACE 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | __LIBBOOTIMAGE_PRIVATE_API__ int trace_mask; 26 | 27 | /* 28 | * Shamelessly borrowed and adapted from Android adb 29 | * read a comma/space/colum/semi-column separated list of tags 30 | * from the TRACE environment variable and build the trace 31 | * mask from it. note that '1' and 'all' are special cases to 32 | * enable all tracing 33 | */ 34 | __LIBBOOTIMAGE_PRIVATE_API__ void trace_init(void) 35 | { 36 | if (TRACING) { 37 | D("TRACING ALREADY INITIALIZED"); 38 | return ; 39 | } 40 | const char* p = getenv("BITRACE"); 41 | const char* q; 42 | 43 | static const struct { 44 | const char* tag; 45 | int flag; 46 | } tags[] = { 47 | { "all", TRACE_ALL }, 48 | { "1", TRACE_ALL }, 49 | { "utils", TRACE_API_BOOTIMAGE_UTILS }, 50 | { "api", TRACE_API_BOOTIMAGE }, 51 | { "extract", TRACE_API_BOOTIMAGE_EXTRACT }, 52 | { "file", TRACE_API_BOOTIMAGE_FILE }, 53 | { "fextract", TRACE_API_BOOTIMAGE_FILE_EXTRACT }, 54 | { "fprint", TRACE_API_BOOTIMAGE_FILE_PRINT }, 55 | { "print", TRACE_API_BOOTIMAGE_PRINT }, 56 | { "archive", TRACE_PRIVATE_ARCHIVE }, 57 | { "bi", TRACE_PRIVATE_BOOTIMAGE }, 58 | { "checks", TRACE_PRIVATE_CHECKS }, 59 | { "kernel", TRACE_PRIVATE_KERNEL }, 60 | { "trace", TRACE_PRIVATE_TRACE }, 61 | { "putils", TRACE_PRIVATE_UTILS }, 62 | { "pprint", TRACE_PRIVATE_PRINT }, 63 | { NULL, 0 } 64 | }; 65 | 66 | if (p == NULL) 67 | return; 68 | 69 | /* use a comma/column/semi-colum/space separated list */ 70 | while (*p) { 71 | int len, tagn; 72 | 73 | q = strpbrk(p, " ,:;"); 74 | if (q == NULL) { 75 | q = p + strlen(p); 76 | } 77 | len = q - p; 78 | 79 | for (tagn = 0; tags[tagn].tag != NULL; tagn++) 80 | { 81 | int taglen = strlen(tags[tagn].tag); 82 | 83 | if (len == taglen && !memcmp(tags[tagn].tag, p, len) ) 84 | { 85 | int flag = tags[tagn].flag; 86 | if (flag == 0) { 87 | 88 | trace_mask = ~0; 89 | 90 | return; 91 | } 92 | trace_mask |= (1 << flag); 93 | break; 94 | } 95 | } 96 | p = q; 97 | if (*p) 98 | p++; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /lib/private/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : lib/private/utils.c 17 | * 18 | */ 19 | #define TRACE_TAG TRACE_PRIVATE_UTILS 20 | 21 | #include 22 | 23 | #ifdef _WIN32 24 | #define mkdir_os(path,mode) mkdir(path) 25 | #define symlink(source,path) ((void)0) 26 | #else 27 | #define mkdir_os(path,mode) mkdir(path,mode) 28 | #endif 29 | 30 | 31 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_mkdirat_umask( char *path,unsigned mode, mode_t mask) 32 | { 33 | mode_t oldumask = umask(mask); 34 | int ret = utils_mkdir_and_parents(path,mode); 35 | umask(oldumask); 36 | return ret; 37 | } 38 | 39 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_mkdir_and_parents_umask( char *path,unsigned mode, mode_t mask) 40 | { 41 | mode_t oldumask = umask(mask); 42 | int ret = utils_mkdir_and_parents(path,mode); 43 | umask(oldumask); 44 | return ret; 45 | } 46 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_mkdir_and_parents( char *path,unsigned mode) 47 | { 48 | errno = 0; 49 | char opath[PATH_MAX]; 50 | char *p; 51 | size_t len; 52 | 53 | if( utils_sanitize_string(path,PATH_MAX) == SSIZE_MAX ){ 54 | return -1; 55 | } 56 | 57 | /* stat the full path, see if we have an existing directory */ 58 | struct stat statbuf ; 59 | if ( stat(path, &statbuf) == 0 ){ 60 | if ( S_ISDIR(statbuf.st_mode) ){ 61 | return 0; 62 | }else 63 | /* path exists but not a directory */ 64 | /* D("path %s found but not a directory\n",path); */ 65 | errno = ENOTDIR ; 66 | return -1; 67 | } 68 | 69 | strncpy(opath,(char*) path, sizeof(opath)); 70 | len = strnlen(opath,PATH_MAX); 71 | 72 | if(opath[len - 1] == '/'){ 73 | /* Replace a trailing slash with a null 74 | we make the assumption that the user did not want to 75 | create a directory that ended with a slash */ 76 | opath[len - 1] = '\0'; 77 | } 78 | for(p = opath; *p; p++){ 79 | if(*p == '/') { 80 | *p = '\0'; 81 | if((strnlen(opath,sizeof(opath)) > 0) && (access(opath, F_OK))){ 82 | /* D("in loop opath=%s\n",opath); */ 83 | mkdir_os(opath, mode); 84 | } 85 | *p = '/'; 86 | } 87 | } 88 | 89 | /* D("opath=%s errno=%u %s\n",opath,errno,strerror(errno)); */ 90 | if(access(opath, F_OK)){ 91 | /* if path is not terminated with / */ 92 | errno = 0 ; 93 | mkdir_os(opath, mode); 94 | } 95 | /* D("opath=%s errno=%u %s\n",opath,errno,strerror(errno)); */ 96 | 97 | return 0; 98 | 99 | } 100 | /* utils_sanitize_string - sanitizes the s argunement 101 | * returns the string length and also zero terminates the input string */ 102 | __LIBBOOTIMAGE_PUBLIC_API__ ssize_t utils_sanitize_string(char* s,ssize_t maxlen) 103 | { 104 | D("s=%s",s); 105 | if ( s == NULL ){ 106 | errno = EBIOUTNAME ; 107 | return -1; 108 | } 109 | /* The Paranoid strnlen function runs strnlen then checks the returned 110 | string again for non printable values and breaks at the first one it find 111 | length */ 112 | 113 | const char* cs = s; 114 | D("s=%s maxlen=%zu",s,maxlen); 115 | ssize_t len = strnlen(cs,maxlen); 116 | if ( len == maxlen ) { 117 | D("s=%s maxlen=%zu len=%zu",s,maxlen,len); 118 | s[len-1] = '\0'; 119 | return len; 120 | } 121 | D("len=%zu",len); 122 | char *p = s; 123 | int i = 0; 124 | for(i = 0 ; i <= len ; i++){ 125 | if ( ( s[i] < '\x20' ) || ( s[i] > '\x7E' ) ) { 126 | if ( len > i ){ 127 | D("non ascii char found %d %c",i,s[i]); 128 | len = i ; 129 | s[i] = '\0'; 130 | } 131 | break ; 132 | } 133 | 134 | } 135 | D("returning len=%zu",len); 136 | return len ; 137 | 138 | } 139 | __LIBBOOTIMAGE_PRIVATE_API__ char* utils_dirname(char* s) 140 | { 141 | D("s=%s",s); 142 | size_t len = utils_sanitize_string(s,PATH_MAX); 143 | if(len == SIZE_MAX ){ 144 | return NULL ; 145 | } 146 | 147 | char* d = strrchr(s,'/'); 148 | 149 | D("d=%s",d); 150 | if(d == NULL ){ 151 | return NULL; 152 | } 153 | int dir_len = d-s ; 154 | D("dir_len=%d",dir_len); 155 | char* r = calloc(dir_len , sizeof(char)+1); 156 | strncpy(r,s,dir_len); 157 | D("r=%s",r); 158 | return r; 159 | 160 | } 161 | __LIBBOOTIMAGE_PRIVATE_API__ char* utils_basename(char* s) 162 | { 163 | D("s=%s",s); 164 | size_t len = utils_sanitize_string(s,PATH_MAX); 165 | if(len == SIZE_MAX ){ 166 | return NULL ; 167 | } 168 | char* r = strrchr(s,'/'); 169 | if( r == NULL ){ 170 | return s; 171 | } 172 | return r+1; 173 | 174 | } 175 | __LIBBOOTIMAGE_PRIVATE_API__ ssize_t utils_write_all_fd (int fd, const void* buffer, ssize_t count) 176 | { 177 | 178 | if ( fd <= 0 ){ 179 | E("fd %d",fd); 180 | return -1 ; 181 | } 182 | if ( buffer == NULL ){ 183 | ENULLARG("buffer"); 184 | return -1; 185 | } 186 | if ( count <= 0 ){ 187 | E("count %zu",count); 188 | return -1 ; 189 | } 190 | 191 | ssize_t left_to_write = count; 192 | while (left_to_write > 0) { 193 | ssize_t written = write (fd, buffer, count); 194 | if (written == -1){ 195 | /* An error occurred; bail. */ 196 | return -1; 197 | } 198 | /* Keep count of how much more we need to write. */ 199 | left_to_write -= written; 200 | } 201 | /* We should have written no more than COUNT bytes! */ 202 | assert (left_to_write == 0); 203 | /* The number of bytes written is exactly COUNT. */ 204 | return count; 205 | } 206 | __LIBBOOTIMAGE_PRIVATE_API__ ssize_t utils_write_all (char* file_name,mode_t mode, const void* buffer, ssize_t count) 207 | { 208 | /* Open the file as read only, read for mmapping */ 209 | ssize_t len = utils_sanitize_string(file_name,PATH_MAX); 210 | if( len == -1 ){ 211 | return -1 ; 212 | } 213 | char* dirname = utils_dirname(file_name) ; 214 | if ( dirname != NULL ) { 215 | utils_mkdir_and_parents_umask(dirname,0755,0); 216 | } 217 | 218 | int fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, mode); 219 | D("fd=%d",fd); 220 | if(fd < 0 ){ 221 | D("could not open file_name %s",file_name); 222 | /* Could not open file. errno should be set already so return -1 */ 223 | return -1; 224 | } 225 | ssize_t written = utils_write_all_fd(fd,buffer,count); 226 | close(fd); 227 | return written; 228 | } 229 | /* utils_read_all - Populate buffer with data and st with stat info for filename */ 230 | __LIBBOOTIMAGE_PRIVATE_API__ int utils_read_all(char* file_name,char** buffer, struct stat* st) 231 | { 232 | 233 | if ( utils_sanitize_string(file_name,PATH_MAX) == -1 ){ 234 | errno = EBIFNAME ; 235 | return -1 ; 236 | } 237 | /* Does the file exist? , can we read it? */ 238 | if ( access(file_name , R_OK ) == -1 ) { 239 | errno = EBIFACCESS ; 240 | return -1 ; 241 | } 242 | if (stat(file_name, st) == -1){ 243 | E("stat"); 244 | errno = EBISTAT; 245 | return -1 ; 246 | } 247 | 248 | if ( st->st_size <= 0 ) { 249 | D("wrong file_size %jd",st->st_size); 250 | return -1; 251 | } 252 | /* Open the file as read only, read for mmapping */ 253 | int fd = open(file_name,O_RDONLY); 254 | D("fd=%d",fd); 255 | if(fd < 0 ){ 256 | D("could not open file_name %s",file_name); 257 | /* Could not open file. errno should be set already so return -1 */ 258 | return -1; 259 | } 260 | 261 | 262 | 263 | buffer[0] = calloc(st->st_size,sizeof(char)); 264 | D("dest=%p",buffer[0] ); 265 | ssize_t left_to_read = st->st_size; 266 | while (left_to_read > 0 ) { 267 | ssize_t tmp_bytes_read = read(fd, buffer[0], left_to_read); 268 | if ( tmp_bytes_read <= 0 ){ 269 | D("bytes_read=%zu errno=%d %s",tmp_bytes_read , errno, strerror(errno) ); 270 | close(fd); 271 | return -1; 272 | } 273 | left_to_read -= tmp_bytes_read; 274 | buffer += tmp_bytes_read; 275 | 276 | 277 | } 278 | close(fd); 279 | //D("buffer 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",buffer[0][0],buffer[0][1],buffer[0][2],buffer[0][3],buffer[0][4],buffer[0][5]); 280 | D("left_to_read=%zu",left_to_read ); 281 | return 0; 282 | } 283 | __LIBBOOTIMAGE_PUBLIC_API__ unsigned char *utils_memmem(unsigned char *haystack, unsigned haystack_len, char* needle, unsigned needle_len) 284 | { 285 | 286 | errno = 0; 287 | if(!haystack) 288 | errno = ENOMEM ; 289 | else if(!needle) 290 | errno = ENOMEM ; 291 | else if(haystack_len < needle_len || !haystack_len || !needle_len) 292 | errno = EINVAL ; 293 | 294 | if(errno) return NULL ; 295 | 296 | 297 | 298 | size_t begin=0; 299 | unsigned char* uneedle = (unsigned char *)needle ; 300 | //D("find_in_memory haystack=%p haystack_len=%u needle=%p needle_len=%u\n",haystack,haystack_len,needle,needle_len); 301 | //fprintf(stderr,"Memory HS:%p HL:%u\n",haystack, haystack_len); 302 | //D("haystack[0]='%x' needle[0]='%x'\n",haystack[0],uneedle[0]); 303 | 304 | for(begin=0 ; begin < haystack_len; begin++){ 305 | // make sure we are comparing apples with apples 306 | if(haystack[begin]==uneedle[0]){ 307 | 308 | if(!memcmp(uneedle,haystack+begin,needle_len)){ 309 | //D("haystack[%d]='%x'\n",begin,haystack[begin]); 310 | return haystack+begin; 311 | } 312 | } 313 | } 314 | 315 | //D("INFO: needle Not Found In Memory\n"); 316 | return NULL; 317 | } 318 | -------------------------------------------------------------------------------- /src/bootimage_utils_options.c: -------------------------------------------------------------------------------- 1 | int bootimage 2 | -------------------------------------------------------------------------------- /src/bootimage_utils_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : src/bootimage_utils_private.h 17 | * 18 | */ 19 | 20 | #ifndef _467a2a5e_9400_11e4_80f4_5404a601fa9d 21 | #define _467a2a5e_9400_11e4_80f4_5404a601fa9d 22 | 23 | 24 | enum bootimage_options { 25 | 26 | char* name ; 27 | int has_arg ; 28 | int short_name ; 29 | char* val ; 30 | 31 | }; 32 | struct bootimage_options { 33 | 34 | }; 35 | struct bootimage_utils_program { 36 | 37 | 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : src/file.c 17 | * 18 | */ 19 | 20 | int bootimage_utils_get_file_type(const char* file_name) 21 | { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/help.c: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | enum program_actions_enum { 10 | ACTION_EXTRACT, 11 | ACTION_EXTRACT_KERNEL, 12 | ACTION_EXTRACT_RAMDISK, 13 | ACTION_EXTRACT_BOOTIMAGE, 14 | ACTION_EXTRACT_BOOTIMAGE_KERNEL, 15 | ACTION_EXTRACT_BOOTIMAGE_FILE, 16 | ACTION_EXTRACT_BOOTIMAGE_FILES, 17 | ACTION_PRINT, 18 | ACTION_PRINT_HEADER, 19 | ACTION_PRINT_KERNEL, 20 | ACTION_PRINT_RAMDISK, 21 | ACTION_PRINT_RAMDISK_LIST, 22 | ACTION_PRINT_BOOTIMAGE, 23 | ACTION_PRINT_BOOTIMAGE_HEADER, 24 | ACTION_PRINT_BOOTIMAGE_KERNEL, 25 | ACTION_PRINT_BOOTIMAGE_RAMDISK, 26 | ACTION_INFO 27 | }; 28 | #define ACTION_COUNT 17 29 | static struct program_actions_t { 30 | int index ; 31 | char* name ; 32 | int name_length ; 33 | } program_actions[ACTION_COUNT] = { 34 | {ACTION_EXTRACT_BOOTIMAGE_KERNEL,"extract-bootimage-kernel",24 }, 35 | {ACTION_EXTRACT_BOOTIMAGE_FILE,"extract-bootimage-file",22 }, 36 | {ACTION_EXTRACT_BOOTIMAGE_FILES,"extract-bootimage-files",23 }, 37 | {ACTION_EXTRACT_BOOTIMAGE,"extract-bootimage",17 }, 38 | {ACTION_EXTRACT_RAMDISK,"extract-ramdisk",15 }, 39 | {ACTION_EXTRACT_KERNEL,"extract-kernel",14 }, 40 | {ACTION_EXTRACT,"extract",7 }, 41 | {ACTION_PRINT_HEADER,"print-header",12 }, 42 | {ACTION_PRINT_KERNEL,"print-kernel",12 }, 43 | {ACTION_PRINT_RAMDISK,"print-ramdisk",13 }, 44 | {ACTION_PRINT_RAMDISK_LIST,"print-ramdisk-list",18 }, 45 | {ACTION_PRINT_BOOTIMAGE,"print-bootimage",15 }, 46 | {ACTION_PRINT_BOOTIMAGE_HEADER,"print-bootimage-header",22 }, 47 | {ACTION_PRINT_BOOTIMAGE_KERNEL,"print-bootimage-kernel",22 }, 48 | {ACTION_PRINT_BOOTIMAGE_RAMDISK,"print-bootimage-ramdisk",23 }, 49 | {ACTION_PRINT,"print",5 }, 50 | {ACTION_INFO,"info",4 }, 51 | 52 | }; 53 | int get_action(char* argv){ 54 | 55 | int i = 0 ; 56 | int found = 0 ; 57 | for ( i = 0 ; i < ACTION_COUNT ; i++) { 58 | //int j = 0 ; 59 | //fprintf(stdout,"program_actions[i].name=%s\n",program_actions[i].name); 60 | //for ( j = 0 ; j < argc ; j++ ) { 61 | size_t len = utils_sanitize_string(argv,PATH_MAX); 62 | //fprintf(stdout,"len=%d\n",len); 63 | if (len == SIZE_MAX ){ 64 | //fprintf(stdout,"returning =%d\n",len); 65 | return -1; 66 | } 67 | //fprintf(stdout,"Found %d argv[j]=%s program_actions[i].name=%s\n",i,argv[j],program_actions[i].name); 68 | unsigned char* action = utils_memmem(argv,len,program_actions[i].name,program_actions[i].name_length); 69 | if ( action != NULL ){ 70 | fprintf(stdout,"Found %d\n",i); 71 | found = 1 ; 72 | break ; 73 | } 74 | 75 | 76 | } 77 | if ( found == 0 ){ 78 | return -1; 79 | } 80 | //fprintf(stdout,"Found %d\n",i); 81 | return program_actions[i].index ; 82 | 83 | } 84 | 85 | 86 | int main(int argc , char** argv){ 87 | 88 | /* Sanity Check argc first. Never trust user input not even in main */ 89 | if ( argc <= 0 ){ 90 | return 0 ; 91 | } 92 | if ( argc == 1 ){ 93 | fprintf(stdout,"Help\n"); 94 | } 95 | int action = get_action(argv[0]); 96 | fprintf(stdout,"argv[0]=%s action=%d action=%s\n",argv[0],action ,program_actions[action].name); 97 | if ( action == -1 ){ 98 | argv++; 99 | argc--; 100 | action = get_action(argv[0]); 101 | fprintf(stdout,"Second argv[0]=%s action=%d action=%s\n",argv[0],action ,program_actions[action].name); 102 | } 103 | fprintf(stdout,"action=%d %d\n",action,ACTION_EXTRACT_BOOTIMAGE_KERNEL); 104 | if ( action == ACTION_EXTRACT_BOOTIMAGE_KERNEL ) { 105 | fprintf(stdout,"argv[1]=%s\n",argv[1]); 106 | if ( argc == 2 ){ 107 | 108 | bootimage_file_extract_kernel(argv[1],NULL); 109 | } else if ( argc == 3 ){ 110 | bootimage_file_extract_kernel(argv[1],argv[2]); 111 | } 112 | } 113 | /*struct bootimage_utils* biu = bootimage_utils_initialize(); 114 | bootimage_utils_file_read(biu,argv[1]); 115 | bootimage_utils_free(&biu); 116 | 117 | fprintf(stdout,"biu=%p\n",biu); 118 | */ 119 | return 0 ; 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/print_header.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : src/print_header.c 17 | * 18 | */ 19 | #include 20 | int print_bootimage_header(struct bootimage_utils* biu) 21 | { 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/strings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Trevor Drake 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * file : src/strings.h 17 | * 18 | */ 19 | 20 | #ifndef _39c4b55e_909a_11e4_bb61_5404a601fa9d 21 | #define _39c4b55e_909a_11e4_bb61_5404a601fa9d 22 | 23 | #define STR_PROG_TITLE "Android Boot Image Utilities" 24 | #define STR_PROG_VERSION "x.xx Alpha Release" 25 | #define STR_PROG_COMPILE "Compiled on "__DATE__" at "__TIME__ 26 | 27 | #define STR_FORMAT_FILE_TYPE "File:%s Type is %s" 28 | #define STR_FILE_TYPE_NEXUS_FACTORY_IMAGE "Nexus Factory Image" 29 | #define STR_FILE_TYPE_NEXUS_FACTORY_IMAGE "Nexus Factory Image" 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /test1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | void extract_ramdisk(struct bootimage* bi) 10 | { 11 | int ret = bootimage_extract_ramdisk_archive(bi,NULL); 12 | if ( ret == -1 ){ 13 | printf("bootimage_extract_ramdisk failed err=%d\n",errno); 14 | } 15 | 16 | } 17 | 18 | void extract_ramdisk_directory(struct bootimage* bi) 19 | { 20 | int ret = bootimage_extract_ramdisk(bi,NULL); 21 | if ( ret == -1 ){ 22 | printf("bootimage_extract_ramdisk failed err=%d\n",errno); 23 | } 24 | 25 | } 26 | 27 | void extract_kernel(struct bootimage* bi) 28 | { 29 | int ret = bootimage_extract_kernel(bi,NULL); 30 | if ( ret == -1 ){ 31 | printf("bootimage_extract_kernel failed err=%d\n",errno); 32 | } 33 | 34 | } 35 | void extract_header_block(struct bootimage* bi) 36 | { 37 | 38 | int ret = bootimage_extract_header_block(bi,NULL); 39 | if ( ret == -1 ){ 40 | printf("bootimage_extract_header_block failed err=%d\n",errno); 41 | } 42 | } 43 | 44 | void list_archive(const char *name) 45 | { 46 | 47 | struct bootimage* bi = NULL; 48 | printf("bi %p\n",bi); 49 | bi = bootimage_initialize(); 50 | int ret = bootimage_file_read(bi,name); 51 | if ( ret == -1 ){ 52 | printf("bootimage_file_read failed err=%d\n",errno); 53 | }else{ 54 | 55 | extract_ramdisk(bi); 56 | extract_kernel(bi); 57 | extract_header_block(bi); 58 | extract_ramdisk_directory(bi); 59 | } 60 | bootimage_free(&bi); 61 | printf("bi %p\n",bi); 62 | //int ret = bootimage_read_filename_(bi,name); 63 | //int ret = bootimage_extract_ramdisk_entry_from_file(bi,name,"init.rc"); 64 | //ret = bootimage_read_kernel(bi); 65 | /*printf("bi %p ret=%d errno=%d\n",bi,ret,errno); 66 | bootimage_read_free(&bi); 67 | 68 | printf("bi %p\n",bi); 69 | 70 | */ 71 | 72 | return ; 73 | 74 | } 75 | int print_tests(struct bootimage* bi ) 76 | { 77 | printf("Running bootimage_print_header\n"); 78 | bootimage_print_header(bi); 79 | printf("Running bootimage_print_kernel\n"); 80 | bootimage_print_kernel(bi); 81 | printf("Running bootimage_print_kernel_version\n"); 82 | bootimage_print_kernel_version(bi); 83 | return 0 ; 84 | } 85 | int extract_tests(struct bootimage* bi ) 86 | { 87 | printf("Running bootimage_extract_uncompressed_kernel\n"); 88 | bootimage_extract_uncompressed_kernel(bi,NULL); 89 | bootimage_extract_ramdisk(bi,NULL); 90 | return 0 ; 91 | } 92 | 93 | 94 | int main(int argc ,char** argv) 95 | { 96 | //int ret = bootimage_file_print_header(argv[1]) ; 97 | //if ( ret == -1 ){ 98 | // printf("bootimage_file_read failed err=%d\n",errno); 99 | //} 100 | char cwd[1024] = "out/out.rc"; 101 | //if (getcwd(cwd, sizeof(cwd)) != NULL) 102 | // fprintf(stdout, "Current working dir: %s\n", cwd); 103 | //else 104 | // perror("getcwd() error"); 105 | 106 | char* dname = dirname(cwd); 107 | printf("dirname=%s\n", dname); 108 | 109 | 110 | struct bootimage* bi = NULL; 111 | //printf("bi %p\n",bi); 112 | bi = bootimage_initialize(); 113 | int ret = bootimage_file_read(bi,argv[1]); 114 | //bootimage_extract_ramdisk_entry(bi,"init.rc","out.rc"); 115 | //bootimage_extract_ramdisk_entry(bi,"init.rc","out/out.rc"); 116 | printf("dirname=%s\n", dname); 117 | 118 | bootimage_extract_ramdisk(bi,NULL); 119 | //bootimage_extract_uncompressed_kernel(bi,NULL); 120 | //bootimage_extract_kernel(bi,NULL); 121 | //extract_tests(bi); 122 | //print_tests(bi) ; 123 | 124 | 125 | //printf("bi %p\n",bi); 126 | //bootimage_extract_ramdisk(bi,NULL); 127 | bootimage_free(&bi); 128 | /*list_archive(argv[1]);*/ 129 | 130 | return 0 ; 131 | } 132 | -------------------------------------------------------------------------------- /windows/memmem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | * Find the first occurrence of the byte string s in byte string l. 5 | */ 6 | 7 | void * 8 | memmem(const void *l, size_t l_len, const void *s, size_t s_len) 9 | { 10 | register char *cur, *last; 11 | const char *cl = (const char *)l; 12 | const char *cs = (const char *)s; 13 | 14 | /* we need something to compare */ 15 | if (l_len == 0 || s_len == 0) 16 | return NULL; 17 | 18 | /* "s" must be smaller or equal to "l" */ 19 | if (l_len < s_len) 20 | return NULL; 21 | 22 | /* special case where s_len == 1 */ 23 | if (s_len == 1) 24 | return memchr(l, (int)*cs, l_len); 25 | 26 | /* the last position where its possible to find "s" in "l" */ 27 | last = (char *)cl + l_len - s_len; 28 | 29 | for (cur = (char *)cl; cur <= last; cur++) 30 | if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) 31 | return cur; 32 | 33 | return NULL; 34 | } 35 | -------------------------------------------------------------------------------- /windows/mman.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #ifndef FILE_MAP_EXECUTE 9 | #define FILE_MAP_EXECUTE 0x0020 10 | #endif /* FILE_MAP_EXECUTE */ 11 | 12 | static int __map_mman_error(const DWORD err, const int deferr) 13 | { 14 | if (err == 0) 15 | return 0; 16 | //TODO: implement 17 | return err; 18 | } 19 | 20 | static DWORD __map_mmap_prot_page(const int prot) 21 | { 22 | DWORD protect = 0; 23 | 24 | if (prot == PROT_NONE) 25 | return protect; 26 | 27 | if ((prot & PROT_EXEC) != 0) 28 | { 29 | protect = ((prot & PROT_WRITE) != 0) ? 30 | PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ; 31 | } 32 | else 33 | { 34 | protect = ((prot & PROT_WRITE) != 0) ? 35 | PAGE_READWRITE : PAGE_READONLY; 36 | } 37 | 38 | return protect; 39 | } 40 | 41 | static DWORD __map_mmap_prot_file(const int prot) 42 | { 43 | DWORD desiredAccess = 0; 44 | 45 | if (prot == PROT_NONE) 46 | return desiredAccess; 47 | 48 | if ((prot & PROT_READ) != 0) 49 | desiredAccess |= FILE_MAP_READ; 50 | if ((prot & PROT_WRITE) != 0) 51 | desiredAccess |= FILE_MAP_WRITE; 52 | if ((prot & PROT_EXEC) != 0) 53 | desiredAccess |= FILE_MAP_EXECUTE; 54 | 55 | return desiredAccess; 56 | } 57 | 58 | void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) 59 | { 60 | HANDLE fm, h; 61 | 62 | void * map = MAP_FAILED; 63 | 64 | #ifdef _MSC_VER 65 | #pragma warning(push) 66 | #pragma warning(disable: 4293) 67 | #endif 68 | 69 | const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD)) ? 70 | (DWORD)off : (DWORD)(off & 0xFFFFFFFFL); 71 | const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD)) ? 72 | (DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL); 73 | const DWORD protect = __map_mmap_prot_page(prot); 74 | const DWORD desiredAccess = __map_mmap_prot_file(prot); 75 | 76 | const off_t maxSize = off + (off_t)len; 77 | 78 | const DWORD dwMaxSizeLow = (sizeof(off_t) <= sizeof(DWORD)) ? 79 | (DWORD)maxSize : (DWORD)(maxSize & 0xFFFFFFFFL); 80 | const DWORD dwMaxSizeHigh = (sizeof(off_t) <= sizeof(DWORD)) ? 81 | (DWORD)0 : (DWORD)((maxSize >> 32) & 0xFFFFFFFFL); 82 | 83 | #ifdef _MSC_VER 84 | #pragma warning(pop) 85 | #endif 86 | 87 | errno = 0; 88 | 89 | if (len == 0 90 | /* Unsupported flag combinations */ 91 | || (flags & MAP_FIXED) != 0 92 | /* Usupported protection combinations */ 93 | || prot == PROT_EXEC) 94 | { 95 | errno = EINVAL; 96 | return MAP_FAILED; 97 | } 98 | 99 | h = ((flags & MAP_ANONYMOUS) == 0) ? 100 | (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE; 101 | 102 | if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) 103 | { 104 | errno = EBADF; 105 | return MAP_FAILED; 106 | } 107 | 108 | fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL); 109 | 110 | if (fm == NULL) 111 | { 112 | errno = __map_mman_error(GetLastError(), EPERM); 113 | return MAP_FAILED; 114 | } 115 | 116 | map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len); 117 | 118 | CloseHandle(fm); 119 | 120 | if (map == NULL) 121 | { 122 | errno = __map_mman_error(GetLastError(), EPERM); 123 | return MAP_FAILED; 124 | } 125 | 126 | return map; 127 | } 128 | 129 | int munmap(void *addr, size_t len) 130 | { 131 | if (UnmapViewOfFile(addr)) 132 | return 0; 133 | 134 | errno = __map_mman_error(GetLastError(), EPERM); 135 | 136 | return -1; 137 | } 138 | 139 | int mprotect(void *addr, size_t len, int prot) 140 | { 141 | DWORD newProtect = __map_mmap_prot_page(prot); 142 | DWORD oldProtect = 0; 143 | 144 | if (VirtualProtect(addr, len, newProtect, &oldProtect)) 145 | return 0; 146 | 147 | errno = __map_mman_error(GetLastError(), EPERM); 148 | 149 | return -1; 150 | } 151 | 152 | int msync(void *addr, size_t len, int flags) 153 | { 154 | if (FlushViewOfFile(addr, len)) 155 | return 0; 156 | 157 | errno = __map_mman_error(GetLastError(), EPERM); 158 | 159 | return -1; 160 | } 161 | 162 | int mlock(const void *addr, size_t len) 163 | { 164 | if (VirtualLock((LPVOID)addr, len)) 165 | return 0; 166 | 167 | errno = __map_mman_error(GetLastError(), EPERM); 168 | 169 | return -1; 170 | } 171 | 172 | int munlock(const void *addr, size_t len) 173 | { 174 | if (VirtualUnlock((LPVOID)addr, len)) 175 | return 0; 176 | 177 | errno = __map_mman_error(GetLastError(), EPERM); 178 | 179 | return -1; 180 | } 181 | -------------------------------------------------------------------------------- /windows/mman.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 25 | 28 | 31 | 34 | 37 | 40 | 51 | 54 | 57 | 60 | 63 | 66 | 69 | 72 | 75 | 78 | 79 | 87 | 90 | 93 | 96 | 99 | 102 | 111 | 114 | 117 | 120 | 123 | 126 | 129 | 132 | 135 | 138 | 139 | 140 | 141 | 142 | 143 | 148 | 151 | 152 | 153 | 158 | 161 | 162 | 163 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /windows/sys/mman.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sys/mman.h 3 | * mman-win32 4 | */ 5 | 6 | #ifndef _SYS_MMAN_H_ 7 | #define _SYS_MMAN_H_ 8 | 9 | #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. 10 | #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. 11 | #endif 12 | 13 | /* All the headers include this file. */ 14 | #ifndef _MSC_VER 15 | #include <_mingw.h> 16 | #endif 17 | 18 | #include 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #define PROT_NONE 0 25 | #define PROT_READ 1 26 | #define PROT_WRITE 2 27 | #define PROT_EXEC 4 28 | 29 | #define MAP_FILE 0 30 | #define MAP_SHARED 1 31 | #define MAP_PRIVATE 2 32 | #define MAP_TYPE 0xf 33 | #define MAP_FIXED 0x10 34 | #define MAP_ANONYMOUS 0x20 35 | #define MAP_ANON MAP_ANONYMOUS 36 | 37 | #define MAP_FAILED ((void *)-1) 38 | 39 | /* Flags for msync. */ 40 | #define MS_ASYNC 1 41 | #define MS_SYNC 2 42 | #define MS_INVALIDATE 4 43 | 44 | void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off); 45 | int munmap(void *addr, size_t len); 46 | int mprotect(void *addr, size_t len, int prot); 47 | int msync(void *addr, size_t len, int flags); 48 | int mlock(const void *addr, size_t len); 49 | int munlock(const void *addr, size_t len); 50 | 51 | #ifdef __cplusplus 52 | }; 53 | #endif 54 | 55 | #endif /* _SYS_MMAN_H_ */ 56 | --------------------------------------------------------------------------------