mkbuffaloimg.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  3. * Copyright (C) 2016 FUKAUMI Naoki <naobsd@gmail.com>
  4. *
  5. * Based on mkdniimg.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <unistd.h> /* for unlink() */
  17. #include <libgen.h>
  18. #include <getopt.h> /* for getopt() */
  19. #include <stdarg.h>
  20. #include <errno.h>
  21. #include <sys/stat.h>
  22. #define DNI_HDR_LEN 128
  23. /*
  24. * Globals
  25. */
  26. static char *ifname;
  27. static char *progname;
  28. static char *ofname;
  29. static char *version = "0.00_0.00";
  30. static char *region = "JP";
  31. static char *rootfs_size;
  32. static char *kernel_size;
  33. static char *board_id;
  34. /*
  35. * Message macros
  36. */
  37. #define ERR(fmt, ...) do { \
  38. fflush(0); \
  39. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  40. progname, ## __VA_ARGS__ ); \
  41. } while (0)
  42. #define ERRS(fmt, ...) do { \
  43. int save = errno; \
  44. fflush(0); \
  45. fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
  46. progname, ## __VA_ARGS__, strerror(save)); \
  47. } while (0)
  48. void usage(int status)
  49. {
  50. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  51. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  52. fprintf(stream,
  53. "\n"
  54. "Options:\n"
  55. " -B <board> create image for the board specified with <board>\n"
  56. " -i <file> read input from the file <file>\n"
  57. " -o <file> write output to the file <file>\n"
  58. " -v <version> set image version to <version>\n"
  59. " -r <region> set image region to <region>\n"
  60. " -R <rootfs_size> set RootfsSize to <rootfs_size>\n"
  61. " -K <kernel_size> set KernelSize to <kernel_size>\n"
  62. " -h show this screen\n"
  63. );
  64. exit(status);
  65. }
  66. int main(int argc, char *argv[])
  67. {
  68. int res = EXIT_FAILURE;
  69. int buflen;
  70. int err;
  71. struct stat st;
  72. char *buf;
  73. int i;
  74. uint8_t csum;
  75. FILE *outfile, *infile;
  76. progname = basename(argv[0]);
  77. while ( 1 ) {
  78. int c;
  79. c = getopt(argc, argv, "B:i:o:v:r:R:K:h");
  80. if (c == -1)
  81. break;
  82. switch (c) {
  83. case 'B':
  84. board_id = optarg;
  85. break;
  86. case 'i':
  87. ifname = optarg;
  88. break;
  89. case 'o':
  90. ofname = optarg;
  91. break;
  92. case 'v':
  93. version = optarg;
  94. break;
  95. case 'r':
  96. region = optarg;
  97. break;
  98. case 'R':
  99. rootfs_size = optarg;
  100. break;
  101. case 'K':
  102. kernel_size = optarg;
  103. break;
  104. case 'h':
  105. usage(EXIT_SUCCESS);
  106. break;
  107. default:
  108. usage(EXIT_FAILURE);
  109. break;
  110. }
  111. }
  112. if (board_id == NULL) {
  113. ERR("no board specified");
  114. goto err;
  115. }
  116. if (rootfs_size == NULL) {
  117. ERR("no rootfs_size specified");
  118. goto err;
  119. }
  120. if (kernel_size == NULL) {
  121. ERR("no kernel_size specified");
  122. goto err;
  123. }
  124. if (ifname == NULL) {
  125. ERR("no input file specified");
  126. goto err;
  127. }
  128. if (ofname == NULL) {
  129. ERR("no output file specified");
  130. goto err;
  131. }
  132. err = stat(ifname, &st);
  133. if (err){
  134. ERRS("stat failed on %s", ifname);
  135. goto err;
  136. }
  137. buflen = st.st_size + DNI_HDR_LEN + 1;
  138. buf = malloc(buflen);
  139. if (!buf) {
  140. ERR("no memory for buffer\n");
  141. goto err;
  142. }
  143. memset(buf, 0, DNI_HDR_LEN);
  144. snprintf(buf, DNI_HDR_LEN, "device:%s\nversion:%s\nregion:%s\n"
  145. "RootfsSize:%s\nKernelSize:%s\nInfoHeadSize:128\n",
  146. board_id, version, region, rootfs_size, kernel_size);
  147. buf[DNI_HDR_LEN - 2] = 0x12;
  148. buf[DNI_HDR_LEN - 1] = 0x32;
  149. infile = fopen(ifname, "r");
  150. if (infile == NULL) {
  151. ERRS("could not open \"%s\" for reading", ifname);
  152. goto err_free;
  153. }
  154. errno = 0;
  155. fread(buf + DNI_HDR_LEN, st.st_size, 1, infile);
  156. if (errno != 0) {
  157. ERRS("unable to read from file %s", ifname);
  158. goto err_close_in;
  159. }
  160. csum = 0;
  161. for (i = 0; i < (st.st_size + DNI_HDR_LEN); i++)
  162. csum += buf[i];
  163. csum = 0xff - csum;
  164. buf[st.st_size + DNI_HDR_LEN] = csum;
  165. outfile = fopen(ofname, "w");
  166. if (outfile == NULL) {
  167. ERRS("could not open \"%s\" for writing", ofname);
  168. goto err_close_in;
  169. }
  170. errno = 0;
  171. fwrite(buf, buflen, 1, outfile);
  172. if (errno) {
  173. ERRS("unable to write to file %s", ofname);
  174. goto err_close_out;
  175. }
  176. res = EXIT_SUCCESS;
  177. fflush(outfile);
  178. err_close_out:
  179. fclose(outfile);
  180. if (res != EXIT_SUCCESS) {
  181. unlink(ofname);
  182. }
  183. err_close_in:
  184. fclose(infile);
  185. err_free:
  186. free(buf);
  187. err:
  188. return res;
  189. }