ufdt_verify_overlay_app.cpp revision 5168cab4cb068dc964e6354167bc857a0e948ff7
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
23extern "C" {
24
25#include "ufdt_overlay.h"
26#include "libufdt_sysdeps.h"
27
28}
29
30size_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
47int verify_overlay_files(const char *final_filename,
48                         const char *overlay_filename) {
49    char *final_buf = nullptr;
50    char *overlay_buf = nullptr;
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    overlay_size = read_file_to_buf(overlay_filename, &overlay_buf);
62    if (overlay_size == 0) {
63        fprintf(stderr, "Cannot load DTB Overlay: %s\n", overlay_filename);
64        goto end;
65    }
66
67    blob = ufdt_install_blob(final_buf, final_size);
68    if (!blob) {
69        fprintf(stderr, "ufdt_install_blob() returns null\n");
70        goto end;
71    }
72
73    result = ufdt_verify_dtbo(blob, final_size, overlay_buf, overlay_size);
74
75    if (result != 0) {
76        fprintf(stderr, "bad overlay error: %s\n", overlay_filename);
77    }
78
79end:
80    // Do not dto_free(blob) - it's the same as final_buf.
81    if (overlay_buf) dto_free(overlay_buf);
82    if (final_buf) dto_free(final_buf);
83
84    return result;
85}
86
87int main(int argc, char **argv) {
88  if (argc < 3) {
89    fprintf(stderr, "Usage: %s <final_file> <overlay_file>\n", argv[0]);
90    return 1;
91  }
92
93  const char *final_file = argv[1];
94  const char *overlay_file = argv[2];
95  int ret = verify_overlay_files(final_file, overlay_file);
96
97  return ret == 0 ? ret : 1;
98}
99