summaryrefslogtreecommitdiff
path: root/tests/src/ufdt_verify_overlay_app.cpp
blob: 0373cee26bb3efe54191f5c12ad5e2e30e4a36b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fstream>

#include "ufdt_test_overlay.h"

extern "C" {

#include "ufdt_overlay.h"
#include "libufdt_sysdeps.h"

}

size_t read_file_to_buf(const char *filename, char** buf) {
    size_t size = 0;
    std::ifstream file(filename, std::ios::binary | std::ios::in);

    if (!file) {
        return size;
    }

    file.seekg(0, file.end);
    size = file.tellg();
    file.seekg(0, std::ios::beg);

    *buf = new char[size];
    file.read(*buf, size);
    return size;
}

int verify_overlay_files(const char *final_filename,
                         const char *overlay_filename) {
    char *final_buf = nullptr;
    char *overlay_buf = nullptr;
    struct fdt_header *blob = nullptr;
    int result = 1;
    size_t final_size = 0, overlay_size = 0;

    final_size = read_file_to_buf(final_filename, &final_buf);
    if (final_size == 0) {
        fprintf(stderr, "Cannot load final DTB: %s \n", final_filename);
        goto end;
    }

    overlay_size = read_file_to_buf(overlay_filename, &overlay_buf);
    if (overlay_size == 0) {
        fprintf(stderr, "Cannot load DTB Overlay: %s\n", overlay_filename);
        goto end;
    }

    blob = ufdt_install_blob(final_buf, final_size);
    if (!blob) {
        fprintf(stderr, "ufdt_install_blob() returns null\n");
        goto end;
    }

    result = ufdt_verify_dtbo(blob, final_size, overlay_buf, overlay_size);

    if (result != 0) {
        fprintf(stderr, "bad overlay error: %s\n", overlay_filename);
    }

end:
    // Do not dto_free(blob) - it's the same as final_buf.
    if (overlay_buf) dto_free(overlay_buf);
    if (final_buf) dto_free(final_buf);

    return result;
}

int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <final_file> <overlay_file>\n", argv[0]);
    return 1;
  }

  const char *final_file = argv[1];
  const char *overlay_file = argv[2];
  int ret = verify_overlay_files(final_file, overlay_file);

  return ret == 0 ? ret : 1;
}