blob: d5fda12db382cd7371e0fa69d5f447e00d6e54cd [file] [log] [blame]
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +01001/* tools/mkbootimg/mkdtbimg.c
2**
3** Copyright 2016-2017, The LineageOS Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16**
17** December 2016, Christopher N. Hesse
18** Separate dt creation from mkbootimg program.
19**
20** January 2017, Christopher N. Hesse
21** Adjust to dtbToolCM call syntax.
22*/
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <errno.h>
30#include <limits.h>
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <arpa/inet.h>
35#include <assert.h>
36#include <dirent.h>
37#include <err.h>
38#include <stdint.h>
39
40#include <dtbimg.h>
41
42int usage(void)
43{
44 fprintf(stderr,"usage: mkdtimg\n"
45 " --dt_dir <dtb path>\n"
46 " -o|--output <filename>\n"
47 );
48 return 1;
49}
50
51
52
53int main(int argc, char **argv)
54{
55 char *dtimg = 0;
56 char *dt_dir = 0;
57 void *dt_data = 0;
58 unsigned pagesize = 0;
59 unsigned default_pagesize = 2048;
60 uint32_t dt_size;
61 int fd;
62 struct stat sbuf;
63
64 argc--;
65 argv++;
66
67 while(argc > 0){
68 char *arg = argv[0];
69 char *val = argv[1];
70 if(argc < 1) {
71 return usage();
72 }
73 argc -= 2;
74 argv += 2;
75 if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
76 dtimg = val;
77 } else if (!strcmp(arg, "--dt_dir")) {
78 dt_dir = val;
79 } else if (!strcmp(arg, "-p")) {
80 // Ignore this parameter
81 } else if (!strcmp(arg, "-s")) {
82 pagesize = strtol(val, NULL, 10);
83 if (pagesize == 0)
84 pagesize = default_pagesize;
85 } else {
86 // Check if this is the dtb path
87 int err = stat(arg, &sbuf);
88 if (err != 0 || !S_ISDIR(sbuf.st_mode))
89 return usage();
90 dt_dir = arg;
91 }
92 }
93
94 if(dtimg == 0) {
95 fprintf(stderr,"error: no output filename specified\n");
96 return usage();
97 }
98
99 if(dt_dir == 0) {
100 fprintf(stderr,"error: no dtb path specified\n");
101 return usage();
102 }
103
104 dt_data = load_dtbh_block(dt_dir, pagesize, &dt_size);
105 if (dt_data == 0) {
106 fprintf(stderr, "error: could not load device tree blobs '%s'\n", dt_dir);
107 return 1;
108 }
109
110 fd = open(dtimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
111 if(fd < 0) {
112 fprintf(stderr,"error: could not create '%s'\n", dtimg);
113 return 1;
114 }
115
116 if(write(fd, dt_data, dt_size) != dt_size) goto fail;
117
118 return 0;
119
120fail:
121 unlink(dtimg);
122 close(fd);
123 fprintf(stderr,"error: failed writing '%s': %s\n", dtimg,
124 strerror(errno));
125 return 1;
126}