mkdniimg.c 3.8 KB

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