Root/tools/firmware-utils/src/mkbrncmdline.c

1/*
2 * mkbrncmdline.c - partially based on OpenWrt's wndr3700.c
3 *
4 * Copyright (C) 2011 Tobias Diedrich <ranma+openwrt@tdiedrich.de>
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 * version 2 as published by the Free Software Foundation.
9 *
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.
14 *
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.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <unistd.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <sys/mman.h>
27#include <string.h>
28#include <netinet/in.h>
29#include <inttypes.h>
30
31static void usage(const char *) __attribute__ (( __noreturn__ ));
32
33static void usage(const char *mess)
34{
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");
38    exit(1);
39}
40
41static char *input_file = NULL;
42static char *output_file = NULL;
43static unsigned loadaddr = 0x80002000;
44
45static void parseopts(int *argc, char ***argv)
46{
47    char *endptr;
48    int res;
49
50    while ((res = getopt(*argc, *argv, "a:i:o:")) != -1) {
51        switch (res) {
52        default:
53            usage("Unknown option");
54            break;
55        case 'a':
56            loadaddr = strtoul(optarg, &endptr, 0);
57            if (endptr == optarg || *endptr != 0)
58                usage("loadaddress must be a decimal or hexadecimal 32-bit value");
59            break;
60        case 'i':
61            input_file = optarg;
62            break;
63        case 'o':
64            output_file = optarg;
65            break;
66        }
67    }
68    *argc -= optind;
69    *argv += optind;
70}
71
72static void emitload(int outfd, int reg, unsigned value)
73{
74    char buf[8] = {
75        0x3c, 0x04 + reg,
76        value >> 24, value >> 16,
77        0x34, 0x84 + reg + (reg << 5),
78        value >> 8, value,
79    };
80    if (write(outfd, buf, sizeof(buf)) != sizeof(buf)) {
81        fprintf(stderr, "write: %s\n", strerror(errno));
82        exit(1);
83    }
84}
85
86int main(int argc, char **argv)
87{
88    int outfd;
89    int i;
90    int fd;
91    size_t len, skip, buf_len;
92    unsigned cmdline_addr;
93    unsigned s_ofs;
94    char *buf;
95
96    parseopts(&argc, &argv);
97
98    if (argc < 1)
99        usage("must specify at least one kernel cmdline argument");
100
101    if (input_file == NULL || output_file == NULL)
102        usage("must specify input and output file");
103
104    if ((outfd = open(output_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
105    {
106        fprintf(stderr, "Error opening '%s' for writing: %s\n", output_file, strerror(errno));
107        exit(1);
108    }
109
110    // mmap input_file
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)
114    || close(fd) < 0)
115    {
116        fprintf(stderr, "Error mapping file '%s': %s\n", input_file, strerror(errno));
117        exit(1);
118    }
119
120    cmdline_addr = loadaddr + len;
121
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);
128
129    // write the kernel
130    if (write(outfd, input_file + skip, len - skip) != len -skip) {
131        fprintf(stderr, "write: %s\n", strerror(errno));
132        exit(1);
133    }
134
135    // write cmdline structure
136    buf_len = (argc + 1) * 4;
137    for (i=0; i<argc; i++) {
138        buf_len += strlen(argv[i]) + 1;
139    }
140    buf = malloc(buf_len + 16);
141    if (!buf) {
142        fprintf(stderr, "Could not allocate memory for cmdline buffer\n");
143        exit(1);
144    }
145    memset(buf, 0, buf_len);
146
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;
156    }
157    if (write(outfd, buf, buf_len) != buf_len) {
158        fprintf(stderr, "write: %s\n", strerror(errno));
159        exit(1);
160    }
161
162
163    munmap(input_file, len);
164    close(outfd);
165    free(buf);
166
167    return 0;
168}
169

Archive Download this file



interactive