Simon Shields | 2440fd8 | 2018-01-29 13:17:08 +1100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018, The LineageOS Project |
| 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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #define DEFAULT_PAGE_SIZE 2048 |
| 28 | #define DT_NAME_LEN 64 |
| 29 | |
| 30 | struct dt_entry { |
| 31 | uint32_t chip; |
| 32 | uint32_t platform; |
| 33 | uint32_t subtype; |
| 34 | uint32_t hw_rev; |
| 35 | uint32_t hw_rev_end; |
| 36 | uint32_t offset; |
| 37 | uint32_t size; /* including padding */ |
| 38 | uint32_t space; |
| 39 | }; |
| 40 | |
| 41 | struct dtbh_header { |
| 42 | uint32_t magic; |
| 43 | uint32_t version; |
| 44 | uint32_t entry_count; |
| 45 | |
| 46 | struct dt_entry entries[]; |
| 47 | }; |
| 48 | |
| 49 | static void show_dt_entry(struct dt_entry *ent) { |
| 50 | printf("chip: %u, platform: 0x%x, subtype: 0x%x, hw_rev: %u, hw_rev_end: %u, offset: %u, " |
| 51 | "size: %u, space: %u\n", ent->chip, ent->platform, ent->subtype, ent->hw_rev, |
| 52 | ent->hw_rev_end, ent->offset, ent->size, ent->space); |
| 53 | } |
| 54 | |
| 55 | static void dump_dtb(void *buffer, uint32_t size, char *dest) |
| 56 | { |
| 57 | FILE *output; |
| 58 | ssize_t len; |
| 59 | |
| 60 | output = fopen(dest, "wb"); |
| 61 | if (output == NULL) { |
| 62 | fprintf(stderr, "error: could not open %s\n", dest); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | len = fwrite(buffer, size, 1, output); |
| 67 | fclose(output); |
| 68 | |
| 69 | if (len < 0) { |
| 70 | printf("error: failed to write %s (%s)\n", dest, strerror(errno)); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | printf("Successfully dumped %s (size=%zu)\n", dest, size); |
| 75 | } |
| 76 | |
| 77 | int usage(void) |
| 78 | { |
| 79 | fprintf(stderr,"usage: unpackdtbhimg\n" |
| 80 | " -i|--input <dtbh image path>\n" |
| 81 | " -o|--output <directory>\n" |
| 82 | " -p|--pagesize <pagesize>\n" |
| 83 | ); |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char *argv[]) { |
| 88 | char *dtbhimg = 0; |
| 89 | char *output_dir = 0; |
| 90 | FILE *dtbhimg_fp; |
| 91 | ssize_t len; |
| 92 | int page_size = DEFAULT_PAGE_SIZE; |
| 93 | struct dtbh_header *header; |
| 94 | void *dt_blob; |
| 95 | |
| 96 | argc--; |
| 97 | argv++; |
| 98 | |
| 99 | while (argc > 0){ |
| 100 | char *arg = argv[0]; |
| 101 | char *val = argv[1]; |
| 102 | if (argc < 1) { |
| 103 | return usage(); |
| 104 | } |
| 105 | argc -= 2; |
| 106 | argv += 2; |
| 107 | if (!strcmp(arg, "--input") || !strcmp(arg, "-i")) { |
| 108 | dtbhimg = val; |
| 109 | } else if (!strcmp(arg, "--output") || !strcmp(arg, "-o")) { |
| 110 | output_dir = val; |
| 111 | } else if (!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) { |
| 112 | page_size = atoi(val); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (dtbhimg == 0) { |
| 117 | fprintf(stderr,"error: no input filename specified\n"); |
| 118 | return usage(); |
| 119 | } |
| 120 | |
| 121 | if (output_dir == 0) { |
| 122 | fprintf(stderr,"error: no output path specified\n"); |
| 123 | return usage(); |
| 124 | } |
| 125 | |
| 126 | printf("using page_size: %d\n", page_size); |
| 127 | |
| 128 | dtbhimg_fp = fopen(dtbhimg, "r"); |
| 129 | if (dtbhimg_fp == NULL) { |
| 130 | fprintf(stderr, "error: could not open %s\n", dtbhimg); |
| 131 | goto err_open; |
| 132 | } |
| 133 | |
| 134 | header = calloc(page_size, 1); |
| 135 | if (header == NULL) { |
| 136 | goto err_calloc; |
| 137 | } |
| 138 | |
| 139 | len = fread(header, page_size, 1, dtbhimg_fp); |
| 140 | if (len != 1) { |
| 141 | fprintf(stderr, "Failed to read DTBH header: %s\n", strerror(errno)); |
| 142 | goto err_read; |
| 143 | } |
| 144 | |
| 145 | printf("DTBH_MAGIC = '%.4s'\n", &header->magic); |
| 146 | printf("DTBH_VERSION = %u\n", header->version); |
| 147 | printf("Number of entries: %u\n", header->entry_count); |
| 148 | |
| 149 | for (uint32_t i = 0; i < header->entry_count; i++) { |
| 150 | printf("DTB %d: ", i); |
| 151 | show_dt_entry(&header->entries[i]); |
| 152 | dt_blob = malloc(header->entries[i].size); |
| 153 | if (fseek(dtbhimg_fp, header->entries[i].offset, SEEK_SET) != 0) { |
| 154 | fprintf(stderr, "failed to set offset to 0x%x: %s\n", header->entries[i].offset, |
| 155 | strerror(errno)); |
| 156 | goto err_loop; |
| 157 | } |
| 158 | len = fread(dt_blob, header->entries[i].size, 1, dtbhimg_fp); |
| 159 | if (len != 1) { |
| 160 | fprintf(stderr, "Failed to read DTB: %s\n", strerror(errno)); |
| 161 | } |
| 162 | |
| 163 | size_t dest_file_len = DT_NAME_LEN + strlen(output_dir); |
| 164 | char dest_file[dest_file_len]; |
| 165 | snprintf(dest_file, dest_file_len, "%s/chip%u-0x%x-0x%x_rev%u-%u.dtb", output_dir, |
| 166 | header->entries[i].chip, header->entries[i].platform, header->entries[i].subtype, |
| 167 | header->entries[i].hw_rev, header->entries[i].hw_rev_end); |
| 168 | dump_dtb(dt_blob, header->entries[i].size, dest_file); |
| 169 | free(dt_blob); |
| 170 | } |
| 171 | |
| 172 | free(header); |
| 173 | fclose(dtbhimg_fp); |
| 174 | |
| 175 | return 0; |
| 176 | |
| 177 | err_loop: |
| 178 | free(dt_blob); |
| 179 | err_read: |
| 180 | free(header); |
| 181 | err_calloc: |
| 182 | fclose(dtbhimg_fp); |
| 183 | err_open: |
| 184 | return 1; |
| 185 | } |