mkbrncmdline.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <sys/mman.h>
  26. #include <string.h>
  27. #include <netinet/in.h>
  28. #include <inttypes.h>
  29. static void usage(const char *) __attribute__ (( __noreturn__ ));
  30. static void usage(const char *mess)
  31. {
  32. fprintf(stderr, "Error: %s\n", mess);
  33. fprintf(stderr, "Usage: mkbrncmdline -i input_file -o output_file [-a loadaddress] arg1 [argx ...]\n");
  34. fprintf(stderr, "\n");
  35. exit(1);
  36. }
  37. static char *input_file = NULL;
  38. static char *output_file = NULL;
  39. static unsigned loadaddr = 0x80002000;
  40. static void parseopts(int *argc, char ***argv)
  41. {
  42. char *endptr;
  43. int res;
  44. while ((res = getopt(*argc, *argv, "a:i:o:")) != -1) {
  45. switch (res) {
  46. default:
  47. usage("Unknown option");
  48. break;
  49. case 'a':
  50. loadaddr = strtoul(optarg, &endptr, 0);
  51. if (endptr == optarg || *endptr != 0)
  52. usage("loadaddress must be a decimal or hexadecimal 32-bit value");
  53. break;
  54. case 'i':
  55. input_file = optarg;
  56. break;
  57. case 'o':
  58. output_file = optarg;
  59. break;
  60. }
  61. }
  62. *argc -= optind;
  63. *argv += optind;
  64. }
  65. static void emitload(int outfd, int reg, unsigned value)
  66. {
  67. char buf[8] = {
  68. 0x3c, 0x04 + reg,
  69. value >> 24, value >> 16,
  70. 0x34, 0x84 + reg + (reg << 5),
  71. value >> 8, value,
  72. };
  73. if (write(outfd, buf, sizeof(buf)) != sizeof(buf)) {
  74. fprintf(stderr, "write: %s\n", strerror(errno));
  75. exit(1);
  76. }
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. int outfd;
  81. int i;
  82. int fd;
  83. size_t len, skip, buf_len;
  84. unsigned cmdline_addr;
  85. unsigned s_ofs;
  86. char *buf;
  87. parseopts(&argc, &argv);
  88. if (argc < 1)
  89. usage("must specify at least one kernel cmdline argument");
  90. if (input_file == NULL || output_file == NULL)
  91. usage("must specify input and output file");
  92. if ((outfd = open(output_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
  93. {
  94. fprintf(stderr, "Error opening '%s' for writing: %s\n", output_file, strerror(errno));
  95. exit(1);
  96. }
  97. // mmap input_file
  98. if ((fd = open(input_file, O_RDONLY)) < 0
  99. || (len = lseek(fd, 0, SEEK_END)) < 0
  100. || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
  101. || close(fd) < 0)
  102. {
  103. fprintf(stderr, "Error mapping file '%s': %s\n", input_file, strerror(errno));
  104. exit(1);
  105. }
  106. cmdline_addr = loadaddr + len;
  107. // Kernel args are passed in registers a0,a1,a2 and a3
  108. emitload(outfd, 0, 0); /* a0 = 0 */
  109. emitload(outfd, 1, 0); /* a1 = 0 */
  110. emitload(outfd, 2, cmdline_addr); /* a2 = &cmdline */
  111. emitload(outfd, 3, 0); /* a3 = 0 */
  112. skip = lseek(outfd, 0, SEEK_END);
  113. // write the kernel
  114. if (write(outfd, input_file + skip, len - skip) != len -skip) {
  115. fprintf(stderr, "write: %s\n", strerror(errno));
  116. exit(1);
  117. }
  118. // write cmdline structure
  119. buf_len = (argc + 1) * 4;
  120. for (i=0; i<argc; i++) {
  121. buf_len += strlen(argv[i]) + 1;
  122. }
  123. buf = malloc(buf_len + 16);
  124. if (!buf) {
  125. fprintf(stderr, "Could not allocate memory for cmdline buffer\n");
  126. exit(1);
  127. }
  128. memset(buf, 0, buf_len);
  129. s_ofs = 4 * (argc + 1);
  130. for (i=0; i<argc; i++) {
  131. unsigned s_ptr = cmdline_addr + s_ofs;
  132. buf[i * 4 + 0] = s_ptr >> 24;
  133. buf[i * 4 + 1] = s_ptr >> 16;
  134. buf[i * 4 + 2] = s_ptr >> 8;
  135. buf[i * 4 + 3] = s_ptr >> 0;
  136. memcpy(&buf[s_ofs], argv[i], strlen(argv[i]));
  137. s_ofs += strlen(argv[i]) + 1;
  138. }
  139. if (write(outfd, buf, buf_len) != buf_len) {
  140. fprintf(stderr, "write: %s\n", strerror(errno));
  141. exit(1);
  142. }
  143. munmap(input_file, len);
  144. close(outfd);
  145. free(buf);
  146. return 0;
  147. }