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 <stdlib.h>
18 #include <time.h>
19
20 #include "libfdt.h"
21 #include "libufdt_sysdeps.h"
22
23 #include "util.h"
24
apply_overlay_files(const char * out_filename,const char * base_filename,const char * overlay_filename)25 int apply_overlay_files(const char *out_filename, const char *base_filename,
26 const char *overlay_filename) {
27 int ret = 1;
28 char *base_buf = NULL;
29 char *overlay_buf = NULL;
30 char *merged_buf = NULL;
31
32 size_t base_len;
33 base_buf = load_file(base_filename, &base_len);
34 if (!base_buf || fdt_check_full(base_buf, base_len)) {
35 fprintf(stderr, "Can not load base file: %s\n", base_filename);
36 goto end;
37 }
38
39 size_t overlay_len;
40 overlay_buf = load_file(overlay_filename, &overlay_len);
41 if (!overlay_buf || fdt_check_full(overlay_buf, overlay_len)) {
42 fprintf(stderr, "Can not load overlay file: %s\n", overlay_filename);
43 goto end;
44 }
45
46 size_t merged_buf_len = base_len + overlay_len;
47 merged_buf = dto_malloc(merged_buf_len);
48 if (!merged_buf) {
49 fprintf(stderr, "Malloc failed: %zu bytes needed\n", merged_buf_len);
50 goto end;
51 }
52
53 fdt_open_into(base_buf, merged_buf, merged_buf_len);
54
55 clock_t start = clock();
56 fdt_overlay_apply(merged_buf, overlay_buf);
57 clock_t end = clock();
58
59 if (write_fdt_to_file(out_filename, merged_buf) != 0) {
60 fprintf(stderr, "Write file error: %s\n", out_filename);
61 goto end;
62 }
63
64 // Outputs the used time.
65 double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
66 printf(" fdt_apply_overlay: took %.9f secs\n", cpu_time_used);
67 ret = 0;
68
69 end:
70 if (merged_buf) dto_free(merged_buf);
71 if (overlay_buf) dto_free(overlay_buf);
72 if (base_buf) dto_free(base_buf);
73
74 return ret;
75 }
76
main(int argc,char ** argv)77 int main(int argc, char **argv) {
78 if (argc < 4) {
79 fprintf(stderr, "Usage: %s <base_file> <overlay_file> <out_file>\n", argv[0]);
80 return 1;
81 }
82
83 const char *base_file = argv[1];
84 const char *overlay_file = argv[2];
85 const char *out_file = argv[3];
86 int ret = apply_overlay_files(out_file, base_file, overlay_file);
87
88 return ret;
89 }
90