rb532: drop target
[openwrt/staging/jow.git] / tools / patch-image / src / patch-cmdline.c
1 /*
2 * patch-cmdline.c - patch the kernel command line
3 *
4 * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <string.h>
30
31 #define SEARCH_SPACE (16 * 1024)
32 #define CMDLINE_MAX 512
33
34 int main(int argc, char **argv)
35 {
36 int fd, found = 0, len, ret = -1;
37 char *ptr, *p;
38 unsigned int search_space;
39
40 if (argc <= 2 || argc > 4) {
41 fprintf(stderr, "Usage: %s <file> <cmdline> [size]\n", argv[0]);
42 goto err1;
43 } else if (argc == 3) {
44 fprintf(stdout, "search space used is default of 16KB\n");
45 search_space = SEARCH_SPACE;
46 } else {
47 search_space = atoi(argv[3]);
48 }
49 len = strlen(argv[2]);
50 if (len + 9 > CMDLINE_MAX) {
51 fprintf(stderr, "Command line string too long\n");
52 goto err1;
53 }
54
55 if (((fd = open(argv[1], O_RDWR)) < 0) ||
56 (ptr = (char *) mmap(0, search_space + CMDLINE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1)) {
57 fprintf(stderr, "Could not open kernel image");
58 goto err2;
59 }
60
61 for (p = ptr; p < (ptr + search_space); p += 4) {
62 if (memcmp(p, "CMDLINE:", 8) == 0) {
63 found = 1;
64 p += 8;
65 break;
66 }
67 }
68 if (!found) {
69 fprintf(stderr, "Command line marker not found!\n");
70 goto err3;
71 }
72
73 memset(p, 0, CMDLINE_MAX - 8);
74 strcpy(p, argv[2]);
75 msync(p, CMDLINE_MAX, MS_SYNC|MS_INVALIDATE);
76 ret = 0;
77
78 err3:
79 munmap((void *) ptr, len);
80 err2:
81 if (fd > 0)
82 close(fd);
83 err1:
84 return ret;
85 }