treewide: fix replace nbd@openwrt.org with nbd@nbd.name
[openwrt.git] / tools / patch-image / src / patch-dtb.c
1 /*
2  * patch-dtb.c - patch a dtb into an image
3  *
4  * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
5  * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * based on patch-cmdline.c
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <string.h>
32
33 #define SEARCH_SPACE    (16 * 1024)
34 #define DTB_MAX (16 * 1024)
35
36 int main(int argc, char **argv)
37 {
38         int fd, fddtb, found = 0, len, ret = -1;
39         char *ptr, *ptrdtb, *p;
40         struct stat s;
41
42         if (argc != 3) {
43                 fprintf(stderr, "Usage: %s <file> <dtb>\n", argv[0]);
44                 goto err1;
45         }
46         fddtb = open(argv[1], O_RDONLY);
47         if (!fddtb)
48                 goto err1;
49
50         if (stat(argv[2], &s)) {
51                 fprintf(stderr, "DTB not found\n");
52                 goto err1;
53         }
54
55         len = s.st_size;
56         if (len + 8 > DTB_MAX) {
57                 fprintf(stderr, "DTB too big\n");
58                 goto err1;
59         }
60
61         if (((fddtb = open(argv[2], O_RDONLY)) < 0) ||
62                 (ptrdtb = (char *) mmap(0, DTB_MAX, PROT_READ, MAP_SHARED, fddtb, 0)) == (void *) (-1)) {
63                 fprintf(stderr, "Could not open DTB");
64                 goto err2;
65         }
66
67         if (((fd = open(argv[1], O_RDWR)) < 0) ||
68                 (ptr = (char *) mmap(0, SEARCH_SPACE + DTB_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1)) {
69                 fprintf(stderr, "Could not open kernel image");
70                 goto err3;
71         }
72
73         for (p = ptr; p < (ptr + SEARCH_SPACE); p += 4) {
74                 if (memcmp(p, "OWRTDTB:", 8) == 0) {
75                         found = 1;
76                         p += 8;
77                         break;
78                 }
79         }
80         if (!found) {
81                 fprintf(stderr, "DTB marker not found!\n");
82                 goto err4;
83         }
84
85         memset(p, 0, DTB_MAX - 8);
86         memcpy(p, ptrdtb, len);
87         msync(p, len, MS_SYNC|MS_INVALIDATE);
88         ret = 0;
89
90 err4:
91         munmap((void *) ptr, len);
92 err3:
93         if (fd > 0)
94                 close(fd);
95         munmap((void *) ptrdtb, len);
96 err2:
97         if (fddtb > 0)
98                 close(fddtb);
99 err1:
100         return ret;
101 }