b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 DTB_MAX (16 * 1024) |
| 34 | |
| 35 | int main(int argc, char **argv) |
| 36 | { |
| 37 | int fd, fddtb, found = 0, len, ret = -1; |
| 38 | char *ptr, *ptrdtb, *p; |
| 39 | struct stat s; |
| 40 | unsigned int search_space , dtb_max_size; |
| 41 | |
| 42 | if (argc <= 2 || argc > 4) { |
| 43 | fprintf(stderr, "Usage: %s <file> <dtb> [size]\n", argv[0]); |
| 44 | goto err1; |
| 45 | } else if (argc == 3) { |
| 46 | fprintf(stdout, "DT size used is default of 16KB\n"); |
| 47 | search_space = dtb_max_size = DTB_MAX; |
| 48 | } else { |
| 49 | search_space = dtb_max_size = atoi(argv[3]); |
| 50 | } |
| 51 | |
| 52 | if (stat(argv[2], &s)) { |
| 53 | fprintf(stderr, "DTB not found\n"); |
| 54 | goto err1; |
| 55 | } |
| 56 | |
| 57 | len = s.st_size; |
| 58 | if (len + 8 > dtb_max_size) { |
| 59 | fprintf(stderr, "DTB too big\n"); |
| 60 | goto err1; |
| 61 | } |
| 62 | |
| 63 | if (((fddtb = open(argv[2], O_RDONLY)) < 0) || |
| 64 | (ptrdtb = (char *) mmap(0, dtb_max_size, PROT_READ, MAP_SHARED, fddtb, 0)) == (void *) (-1)) { |
| 65 | fprintf(stderr, "Could not open DTB"); |
| 66 | goto err2; |
| 67 | } |
| 68 | |
| 69 | if (((fd = open(argv[1], O_RDWR)) < 0) || |
| 70 | (ptr = (char *) mmap(0, search_space + dtb_max_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1)) { |
| 71 | fprintf(stderr, "Could not open kernel image"); |
| 72 | goto err3; |
| 73 | } |
| 74 | |
| 75 | for (p = ptr; p < (ptr + search_space); p += 4) { |
| 76 | if (memcmp(p, "OWRTDTB:", 8) == 0) { |
| 77 | found = 1; |
| 78 | p += 8; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | if (!found) { |
| 83 | fprintf(stderr, "DTB marker not found!\n"); |
| 84 | goto err4; |
| 85 | } |
| 86 | |
| 87 | memset(p, 0, dtb_max_size - 8); |
| 88 | memcpy(p, ptrdtb, len); |
| 89 | msync(p, len, MS_SYNC|MS_INVALIDATE); |
| 90 | ret = 0; |
| 91 | |
| 92 | err4: |
| 93 | munmap((void *) ptr, len); |
| 94 | err3: |
| 95 | if (fd > 0) |
| 96 | close(fd); |
| 97 | munmap((void *) ptrdtb, len); |
| 98 | err2: |
| 99 | if (fddtb > 0) |
| 100 | close(fddtb); |
| 101 | err1: |
| 102 | return ret; |
| 103 | } |