rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <errno.h> |
| 7 | #include <limits.h> |
| 8 | #include <libgen.h> |
| 9 | |
| 10 | #include "mincrypt/sha.h" |
| 11 | #include "bootimg.h" |
| 12 | |
| 13 | typedef unsigned char byte; |
| 14 | |
| 15 | int read_padding(FILE* f, unsigned itemsize, int pagesize) |
| 16 | { |
| 17 | byte* buf = (byte*)malloc(sizeof(byte) * pagesize); |
| 18 | unsigned pagemask = pagesize - 1; |
| 19 | unsigned count; |
| 20 | |
| 21 | if((itemsize & pagemask) == 0) { |
| 22 | free(buf); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | count = pagesize - (itemsize & pagemask); |
| 27 | |
| 28 | if(fread(buf, count, 1, f)){}; |
| 29 | free(buf); |
| 30 | return count; |
| 31 | } |
| 32 | |
| 33 | void write_string_to_file(const char* file, const char* string) |
| 34 | { |
| 35 | FILE* f = fopen(file, "w"); |
| 36 | fwrite(string, strlen(string), 1, f); |
| 37 | fwrite("\n", 1, 1, f); |
| 38 | fclose(f); |
| 39 | } |
| 40 | |
| 41 | const char *detect_hash_type(const struct boot_img_hdr *hdr) |
| 42 | { |
| 43 | /* |
| 44 | * This isn't a sophisticated or 100% reliable method to detect the hash |
| 45 | * type but it's probably good enough. |
| 46 | * |
| 47 | * sha256 is expected to have no zeroes in the id array |
| 48 | * sha1 is expected to have zeroes in id[5], id[6] and id[7] |
| 49 | * Zeroes anywhere else probably indicates neither. |
| 50 | */ |
| 51 | const uint32_t *id = hdr->id; |
| 52 | if (id[0] != 0 && id[1] != 0 && id[2] != 0 && id[3] != 0 && |
| 53 | id[4] != 0 && id[5] != 0 && id[6] != 0 && id[7] != 0) |
| 54 | return "sha256"; |
| 55 | else if (id[0] != 0 && id[1] != 0 && id[2] != 0 && id[3] != 0 && |
| 56 | id[4] != 0 && id[5] == 0 && id[6] == 0 && id[7] == 0) |
| 57 | return "sha1"; |
| 58 | else |
| 59 | return "unknown"; |
| 60 | } |
| 61 | |
| 62 | int usage() { |
| 63 | printf("usage: unpackbootimg\n"); |
| 64 | printf("\t-i|--input boot.img\n"); |
| 65 | printf("\t[ -o|--output output_directory]\n"); |
| 66 | printf("\t[ -p|--pagesize <size-in-hexadecimal> ]\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int main(int argc, char** argv) |
| 71 | { |
| 72 | char tmp[PATH_MAX]; |
| 73 | char* directory = "./"; |
| 74 | char* filename = NULL; |
| 75 | int pagesize = 0; |
| 76 | // int base = 0; |
| 77 | |
| 78 | argc--; |
| 79 | argv++; |
| 80 | while(argc > 0){ |
| 81 | char *arg = argv[0]; |
| 82 | char *val = argv[1]; |
| 83 | argc -= 2; |
| 84 | argv += 2; |
| 85 | if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) { |
| 86 | filename = val; |
| 87 | } else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) { |
| 88 | directory = val; |
| 89 | } else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) { |
| 90 | pagesize = strtoul(val, 0, 16); |
| 91 | } else { |
| 92 | return usage(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (filename == NULL) { |
| 97 | return usage(); |
| 98 | } |
| 99 | |
| 100 | int total_read = 0; |
| 101 | FILE* f = fopen(filename, "rb"); |
| 102 | boot_img_hdr header; |
| 103 | |
| 104 | //printf("Reading header...\n"); |
| 105 | int i; |
| 106 | int seeklimit = 65536; |
| 107 | for (i = 0; i <= seeklimit; i++) { |
| 108 | fseek(f, i, SEEK_SET); |
| 109 | if(fread(tmp, BOOT_MAGIC_SIZE, 1, f)){}; |
| 110 | if (memcmp(tmp, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0) |
| 111 | break; |
| 112 | } |
| 113 | total_read = i; |
| 114 | if (i > seeklimit) { |
| 115 | printf("Android boot magic not found.\n"); |
| 116 | return 1; |
| 117 | } |
| 118 | fseek(f, i, SEEK_SET); |
| 119 | if (i > 0) { |
| 120 | printf("Android magic found at: %d\n", i); |
| 121 | } |
| 122 | |
| 123 | if(fread(&header, sizeof(header), 1, f)){}; |
| 124 | //base = header.kernel_addr - 0x00008000; |
| 125 | printf("BOARD_KERNEL_CMDLINE %.*s%.*s\n", BOOT_ARGS_SIZE, header.cmdline, BOOT_EXTRA_ARGS_SIZE, header.extra_cmdline); |
| 126 | //printf("BOARD_KERNEL_BASE %08x\n", base); |
| 127 | printf("BOARD_NAME %s\n", header.name); |
| 128 | printf("BOARD_PAGE_SIZE %d\n", header.page_size); |
| 129 | printf("BOARD_HASH_TYPE %s\n", detect_hash_type(&header)); |
| 130 | printf("BOARD_KERNEL_ADDR 0x%08x\n", header.kernel_addr); |
| 131 | printf("BOARD_KERNEL_SIZE 0x%08x\n", header.kernel_size); |
| 132 | printf("BOARD_RAMDISK_ADDR 0x%08x\n", header.ramdisk_addr); |
| 133 | printf("BOARD_RAMDISK_SIZE 0x%08x\n", header.ramdisk_size); |
| 134 | printf("BOARD_SECOND_ADDR 0x%08x\n", header.second_addr); |
| 135 | printf("BOARD_SECOND_SIZE 0x%08x\n", header.second_size); |
| 136 | printf("BOARD_TAGS_ADDR 0x%08x\n", header.tags_addr); |
| 137 | printf("BOARD_DTB_SIZE 0x%08x\n", header.dt_size); |
| 138 | int a=0, b=0, c=0, y=0, m=0; |
| 139 | if (header.os_version != 0) { |
| 140 | int os_version,os_patch_level; |
| 141 | os_version = header.os_version >> 11; |
| 142 | os_patch_level = header.os_version&0x7ff; |
| 143 | |
| 144 | a = (os_version >> 14)&0x7f; |
| 145 | b = (os_version >> 7)&0x7f; |
| 146 | c = os_version&0x7f; |
| 147 | |
| 148 | y = (os_patch_level >> 4) + 2000; |
| 149 | m = os_patch_level&0xf; |
| 150 | |
| 151 | if((a < 128) && (b < 128) && (c < 128) && (y >= 2000) && (y < 2128) && (m > 0) && (m <= 12)) { |
| 152 | printf("BOARD_OS_VERSION %d.%d.%d\n", a, b, c); |
| 153 | printf("BOARD_OS_PATCH_LEVEL %d-%02d\n", y, m); |
| 154 | } else { |
| 155 | header.os_version = 0; |
| 156 | } |
| 157 | } |
| 158 | if (header.dt_size != 0) { |
| 159 | printf("BOARD_DT_SIZE %d\n", header.dt_size); |
| 160 | } |
| 161 | |
| 162 | if (pagesize == 0) { |
| 163 | pagesize = header.page_size; |
| 164 | } |
| 165 | |
| 166 | //printf("cmdline...\n"); |
| 167 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 168 | strcat(tmp, "-cmdline"); |
| 169 | char cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE+1]; |
| 170 | sprintf(cmdlinetmp, "%.*s%.*s", BOOT_ARGS_SIZE, header.cmdline, BOOT_EXTRA_ARGS_SIZE, header.extra_cmdline); |
| 171 | cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE]='\0'; |
| 172 | write_string_to_file(tmp, cmdlinetmp); |
| 173 | |
| 174 | //printf("board...\n"); |
| 175 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 176 | strcat(tmp, "-board"); |
| 177 | write_string_to_file(tmp, (char *)header.name); |
| 178 | |
| 179 | //printf("base...\n"); |
| 180 | #if 0 |
| 181 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 182 | strcat(tmp, "-base"); |
| 183 | char basetmp[200]; |
| 184 | sprintf(basetmp, "%08x", base); |
| 185 | write_string_to_file(tmp, basetmp); |
| 186 | #endif |
| 187 | |
| 188 | //printf("pagesize...\n"); |
| 189 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 190 | strcat(tmp, "-pagesize"); |
| 191 | char pagesizetmp[200]; |
| 192 | sprintf(pagesizetmp, "%d", header.page_size); |
| 193 | write_string_to_file(tmp, pagesizetmp); |
| 194 | |
| 195 | //printf("kerneloff...\n"); |
| 196 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 197 | strcat(tmp, "-kerneladdr"); |
| 198 | char kernelofftmp[200]; |
| 199 | sprintf(kernelofftmp, "%08x", header.kernel_addr); |
| 200 | write_string_to_file(tmp, kernelofftmp); |
| 201 | |
| 202 | //printf("ramdiskoff...\n"); |
| 203 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 204 | strcat(tmp, "-ramdiskaddr"); |
| 205 | char ramdiskofftmp[200]; |
| 206 | sprintf(ramdiskofftmp, "%08x", header.ramdisk_addr); |
| 207 | write_string_to_file(tmp, ramdiskofftmp); |
| 208 | |
| 209 | //printf("secondoff...\n"); |
| 210 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 211 | strcat(tmp, "-secondaddr"); |
| 212 | char secondofftmp[200]; |
| 213 | sprintf(secondofftmp, "%08x", header.second_addr); |
| 214 | write_string_to_file(tmp, secondofftmp); |
| 215 | |
| 216 | //printf("tagsoff...\n"); |
| 217 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 218 | strcat(tmp, "-tagsaddr"); |
| 219 | char tagsofftmp[200]; |
| 220 | sprintf(tagsofftmp, "%08x", header.tags_addr); |
| 221 | write_string_to_file(tmp, tagsofftmp); |
| 222 | |
| 223 | if (header.os_version != 0) { |
| 224 | //printf("os_version...\n"); |
| 225 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 226 | strcat(tmp, "-osversion"); |
| 227 | char osvertmp[200]; |
| 228 | sprintf(osvertmp, "%d.%d.%d", a, b, c); |
| 229 | write_string_to_file(tmp, osvertmp); |
| 230 | |
| 231 | //printf("os_patch_level...\n"); |
| 232 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 233 | strcat(tmp, "-oslevel"); |
| 234 | char oslvltmp[200]; |
| 235 | sprintf(oslvltmp, "%d-%02d", y, m); |
| 236 | write_string_to_file(tmp, oslvltmp); |
| 237 | } |
| 238 | |
| 239 | //printf("hash...\n"); |
| 240 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 241 | strcat(tmp, "-hash"); |
| 242 | const char *hashtype = detect_hash_type(&header); |
| 243 | write_string_to_file(tmp, hashtype); |
| 244 | |
| 245 | total_read += sizeof(header); |
| 246 | //printf("total read: %d\n", total_read); |
| 247 | total_read += read_padding(f, sizeof(header), pagesize); |
| 248 | |
| 249 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 250 | strcat(tmp, "-zImage"); |
| 251 | FILE *k = fopen(tmp, "wb"); |
| 252 | byte* kernel = (byte*)malloc(header.kernel_size); |
| 253 | //printf("Reading kernel...\n"); |
| 254 | if(fread(kernel, header.kernel_size, 1, f)){}; |
| 255 | total_read += header.kernel_size; |
| 256 | fwrite(kernel, header.kernel_size, 1, k); |
| 257 | fclose(k); |
| 258 | |
| 259 | //printf("total read: %d\n", header.kernel_size); |
| 260 | total_read += read_padding(f, header.kernel_size, pagesize); |
| 261 | |
| 262 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 263 | strcat(tmp, "-ramdisk.gz"); |
| 264 | FILE *r = fopen(tmp, "wb"); |
| 265 | byte* ramdisk = (byte*)malloc(header.ramdisk_size); |
| 266 | //printf("Reading ramdisk...\n"); |
| 267 | if(fread(ramdisk, header.ramdisk_size, 1, f)){}; |
| 268 | total_read += header.ramdisk_size; |
| 269 | fwrite(ramdisk, header.ramdisk_size, 1, r); |
| 270 | fclose(r); |
| 271 | |
| 272 | //printf("total read: %d\n", header.ramdisk_size); |
| 273 | total_read += read_padding(f, header.ramdisk_size, pagesize); |
| 274 | |
| 275 | if (header.second_size != 0) { |
| 276 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 277 | strcat(tmp, "-second"); |
| 278 | FILE *s = fopen(tmp, "wb"); |
| 279 | byte* second = (byte*)malloc(header.second_size); |
| 280 | //printf("Reading second...\n"); |
| 281 | if(fread(second, header.second_size, 1, f)){}; |
| 282 | total_read += header.second_size; |
| 283 | fwrite(second, header.second_size, 1, s); |
| 284 | fclose(s); |
| 285 | } |
| 286 | |
| 287 | //printf("total read: %d\n", header.second_size); |
| 288 | total_read += read_padding(f, header.second_size, pagesize); |
| 289 | |
| 290 | if (header.dt_size != 0) { |
| 291 | sprintf(tmp, "%s/%s", directory, basename(filename)); |
| 292 | strcat(tmp, "-dtb"); |
| 293 | FILE *d = fopen(tmp, "wb"); |
| 294 | byte* dtb = (byte*)malloc(header.dt_size); |
| 295 | //printf("Reading dtb...\n"); |
| 296 | if(fread(dtb, header.dt_size, 1, f)){}; |
| 297 | total_read += header.dt_size; |
| 298 | fwrite(dtb, header.dt_size, 1, d); |
| 299 | fclose(d); |
| 300 | } |
| 301 | |
| 302 | fclose(f); |
| 303 | |
| 304 | //printf("Total Read: %d\n", total_read); |
| 305 | return 0; |
| 306 | } |