1 /* 2 * Copyright (C) 2018 The Android Open Source 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 <stdio.h> 18 #include <stdlib.h> 19 #include <fstream> 20 21 #include "ufdt_test_overlay.h" 22 23 extern "C" { 24 25 #include "ufdt_overlay.h" 26 #include "libufdt_sysdeps.h" 27 28 } 29 30 size_t read_file_to_buf(const char *filename, char** buf) { 31 size_t size = 0; 32 std::ifstream file(filename, std::ios::binary | std::ios::in); 33 34 if (!file) { 35 return size; 36 } 37 38 file.seekg(0, file.end); 39 size = file.tellg(); 40 file.seekg(0, std::ios::beg); 41 42 *buf = new char[size]; 43 file.read(*buf, size); 44 return size; 45 } 46 47 int verify_overlay_files(const char *final_filename, 48 char** overlay_filenames, size_t overlay_count) { 49 char* final_buf = nullptr; 50 void** overlay_buf_array = new void*[overlay_count]; 51 struct fdt_header *blob = nullptr; 52 int result = 1; 53 size_t final_size = 0, overlay_size = 0; 54 55 final_size = read_file_to_buf(final_filename, &final_buf); 56 if (final_size == 0) { 57 fprintf(stderr, "Cannot load final DTB: %s \n", final_filename); 58 goto end; 59 } 60 61 for (size_t i = 0; i < overlay_count; i++) { 62 overlay_size = read_file_to_buf(overlay_filenames[i], 63 reinterpret_cast<char**>(&overlay_buf_array[i])); 64 if (overlay_size == 0) { 65 fprintf(stderr, "Cannot load DTB Overlay: %s\n", overlay_filenames[i]); 66 goto end; 67 } 68 } 69 70 blob = ufdt_install_blob(final_buf, final_size); 71 if (!blob) { 72 fprintf(stderr, "ufdt_install_blob() returns null\n"); 73 goto end; 74 } 75 76 result = ufdt_verify_dtbo(blob, final_size, overlay_buf_array, overlay_count); 77 78 if (result != 0) { 79 fprintf(stderr, "bad overlay error"); 80 } 81 82 end: 83 // Do not dto_free(blob) - it's the same as final_buf. 84 for (size_t i = 0; i < overlay_count; i++) { 85 if (overlay_buf_array[i]) dto_free(overlay_buf_array[i]); 86 } 87 delete[] overlay_buf_array; 88 89 if (final_buf) dto_free(final_buf); 90 91 return result; 92 } 93 94 int main(int argc, char **argv) { 95 if (argc < 3) { 96 fprintf(stderr, "Usage: %s <final_file> <overlay_file1> <overlay_file2> ..\n", argv[0]); 97 return 1; 98 } 99 100 const char *final_file = argv[1]; 101 char** overlay_file_names = new char*[argc - 2]; 102 103 for(int argv_idx = 2; argv_idx < argc; argv_idx++) { 104 overlay_file_names[argv_idx - 2] = argv[argv_idx]; 105 } 106 int ret = verify_overlay_files(final_file, overlay_file_names, argc - 2); 107 108 return ret == 0 ? ret : 1; 109 } 110