2 * mkbrncmdline.c - partially based on OpenWrt's wndr3700.c
4 * Copyright (C) 2011 Tobias Diedrich <ranma+openwrt@tdiedrich.de>
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 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <netinet/in.h>
31 static void usage(const char *) __attribute__ (( __noreturn__
));
33 static void usage(const char *mess
)
35 fprintf(stderr
, "Error: %s\n", mess
);
36 fprintf(stderr
, "Usage: mkbrncmdline -i input_file -o output_file [-a loadaddress] arg1 [argx ...]\n");
37 fprintf(stderr
, "\n");
41 static char *input_file
= NULL
;
42 static char *output_file
= NULL
;
43 static unsigned loadaddr
= 0x80002000;
45 static void parseopts(int *argc
, char ***argv
)
50 while ((res
= getopt(*argc
, *argv
, "a:i:o:")) != -1) {
53 usage("Unknown option");
56 loadaddr
= strtoul(optarg
, &endptr
, 0);
57 if (endptr
== optarg
|| *endptr
!= 0)
58 usage("loadaddress must be a decimal or hexadecimal 32-bit value");
72 static void emitload(int outfd
, int reg
, unsigned value
)
76 value
>> 24, value
>> 16,
77 0x34, 0x84 + reg
+ (reg
<< 5),
80 if (write(outfd
, buf
, sizeof(buf
)) != sizeof(buf
)) {
81 fprintf(stderr
, "write: %s\n", strerror(errno
));
86 int main(int argc
, char **argv
)
91 size_t len
, skip
, buf_len
;
92 unsigned cmdline_addr
;
96 parseopts(&argc
, &argv
);
99 usage("must specify at least one kernel cmdline argument");
101 if (input_file
== NULL
|| output_file
== NULL
)
102 usage("must specify input and output file");
104 if ((outfd
= open(output_file
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) == -1)
106 fprintf(stderr
, "Error opening '%s' for writing: %s\n", output_file
, strerror(errno
));
111 if ((fd
= open(input_file
, O_RDONLY
)) < 0
112 || (len
= lseek(fd
, 0, SEEK_END
)) < 0
113 || (input_file
= mmap(0, len
, PROT_READ
, MAP_SHARED
, fd
, 0)) == (void *) (-1)
116 fprintf(stderr
, "Error mapping file '%s': %s\n", input_file
, strerror(errno
));
120 cmdline_addr
= loadaddr
+ len
;
122 // Kernel args are passed in registers a0,a1,a2 and a3
123 emitload(outfd
, 0, 0); /* a0 = 0 */
124 emitload(outfd
, 1, 0); /* a1 = 0 */
125 emitload(outfd
, 2, cmdline_addr
); /* a2 = &cmdline */
126 emitload(outfd
, 3, 0); /* a3 = 0 */
127 skip
= lseek(outfd
, 0, SEEK_END
);
130 if (write(outfd
, input_file
+ skip
, len
- skip
) != len
-skip
) {
131 fprintf(stderr
, "write: %s\n", strerror(errno
));
135 // write cmdline structure
136 buf_len
= (argc
+ 1) * 4;
137 for (i
=0; i
<argc
; i
++) {
138 buf_len
+= strlen(argv
[i
]) + 1;
140 buf
= malloc(buf_len
+ 16);
142 fprintf(stderr
, "Could not allocate memory for cmdline buffer\n");
145 memset(buf
, 0, buf_len
);
147 s_ofs
= 4 * (argc
+ 1);
148 for (i
=0; i
<argc
; i
++) {
149 unsigned s_ptr
= cmdline_addr
+ s_ofs
;
150 buf
[i
* 4 + 0] = s_ptr
>> 24;
151 buf
[i
* 4 + 1] = s_ptr
>> 16;
152 buf
[i
* 4 + 2] = s_ptr
>> 8;
153 buf
[i
* 4 + 3] = s_ptr
>> 0;
154 memcpy(&buf
[s_ofs
], argv
[i
], strlen(argv
[i
]));
155 s_ofs
+= strlen(argv
[i
]) + 1;
157 if (write(outfd
, buf
, buf_len
) != buf_len
) {
158 fprintf(stderr
, "write: %s\n", strerror(errno
));
163 munmap(input_file
, len
);
This page took 0.061126 seconds and 5 git commands to generate.