2 * patch-dtb.c - patch a dtb into an image
4 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
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.
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.
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.
21 * based on patch-cmdline.c
33 #define SEARCH_SPACE (16 * 1024)
34 #define DTB_MAX (16 * 1024)
36 int main(int argc, char **argv)
38 int fd, fddtb, found = 0, len, ret = -1;
39 char *ptr, *ptrdtb, *p;
43 fprintf(stderr, "Usage: %s <file> <dtb>\n", argv[0]);
46 fddtb = open(argv[1], O_RDONLY);
50 if (stat(argv[2], &s)) {
51 fprintf(stderr, "DTB not found\n");
56 if (len + 8 > DTB_MAX) {
57 fprintf(stderr, "DTB too big\n");
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");
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");
73 for (p = ptr; p < (ptr + SEARCH_SPACE); p += 4) {
74 if (memcmp(p, "OWRTDTB:", 8) == 0) {
81 fprintf(stderr, "DTB marker not found!\n");
85 memset(p, 0, DTB_MAX - 8);
86 memcpy(p, ptrdtb, len);
87 msync(p, len, MS_SYNC|MS_INVALIDATE);
91 munmap((void *) ptr, len);
95 munmap((void *) ptrdtb, len);