1 /*
2 * Copyright (C) 2016 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 <time.h>
20
21 #include "ufdt_overlay.h"
22 #include "libufdt_sysdeps.h"
23
24 #include "util.h"
25
apply_overlay_files(const char * out_filename,const char * base_filename,const char * overlay_filename)26 int apply_overlay_files(const char *out_filename, const char *base_filename,
27 const char *overlay_filename) {
28 int ret = 1;
29 char *base_buf = NULL;
30 char *overlay_buf = NULL;
31 struct fdt_header *new_blob = NULL;
32
33 size_t blob_len;
34 base_buf = load_file(base_filename, &blob_len);
35 if (!base_buf || fdt_check_full(base_buf, blob_len)) {
36 fprintf(stderr, "Can not load base file: %s\n", base_filename);
37 goto end;
38 }
39
40 size_t overlay_len;
41 overlay_buf = load_file(overlay_filename, &overlay_len);
42 if (!overlay_buf || fdt_check_full(overlay_buf, overlay_len)) {
43 fprintf(stderr, "Can not load overlay file: %s\n", overlay_filename);
44 goto end;
45 }
46
47 struct fdt_header *blob = ufdt_install_blob(base_buf, blob_len);
48 if (!blob) {
49 fprintf(stderr, "ufdt_install_blob() returns null\n");
50 goto end;
51 }
52
53 clock_t start = clock();
54 new_blob = ufdt_apply_overlay(blob, blob_len, overlay_buf, overlay_len);
55 clock_t end = clock();
56
57 if (!new_blob) {
58 fprintf(stderr, "ufdt_apply_overlay() returned null: bad input?\n");
59 goto end;
60 }
61
62 if (write_fdt_to_file(out_filename, new_blob) != 0) {
63 fprintf(stderr, "Write file error: %s\n", out_filename);
64 goto end;
65 }
66
67 // Outputs the used time.
68 double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
69 printf("ufdt_apply_overlay: took %.9f secs\n", cpu_time_used);
70 ret = 0;
71
72 end:
73 // Do not dto_free(blob) - it's the same as base_buf.
74
75 if (new_blob) dto_free(new_blob);
76 if (overlay_buf) dto_free(overlay_buf);
77 if (base_buf) dto_free(base_buf);
78
79 return ret;
80 }
81
main(int argc,char ** argv)82 int main(int argc, char **argv) {
83 if (argc < 4) {
84 fprintf(stderr, "Usage: %s <base_file> <overlay_file> <out_file>\n", argv[0]);
85 return 1;
86 }
87
88 const char *base_file = argv[1];
89 const char *overlay_file = argv[2];
90 const char *out_file = argv[3];
91 int ret = apply_overlay_files(out_file, base_file, overlay_file);
92
93 return ret;
94 }
95