wrt350nv2-builder.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. WRT350Nv2-Builder 2.4 (previously called buildimg)
  3. Copyright (C) 2008-2009 Dirk Teurlings <info@upexia.nl>
  4. Copyright (C) 2009-2011 Matthias Buecher (http://www.maddes.net/)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. A lot of thanks to Kaloz and juhosg from OpenWRT and Lennert Buytenhek from
  17. marvell for helping me figure this one out. This code is based on bash
  18. scripts wrote by Peter van Valderen so the real credit should go to him.
  19. This program reads the provided parameter file and creates an image which can
  20. be used to flash a Linksys WRT350N v2 from stock firmware.
  21. The trick is to fill unused space in the bin file with random, so that the
  22. resulting zip file passes the size check of the stock firmware.
  23. The parameter file layout for an original Linksys firmware:
  24. :kernel 0x001A0000 /path/to/uImage
  25. :rootfs 0 /path/to/root.squashfs
  26. :u-boot 0 /path/to/u-boot.bin
  27. #version 0x2020
  28. Additionally since v2.4 an already complete image can be used:
  29. :image 0 /path/to/librecmc-wrt350nv2-[squashfs|jffs2-64k].img
  30. args:
  31. 1 wrt350nv2.par parameter file describing the image layout
  32. 2 wrt350nv2.img output file for linksys style image
  33. A u-boot image inside the bin file is not necessary.
  34. The version is not important.
  35. The name of the bin file is not important, but still "wrt350n.bin" is used to
  36. keep as close as possible to the stock firmware.
  37. Linksys assumes that no mtd will be used to its maximum, so the last 16 bytes
  38. of the mtd are abused to define the length of the next mtd content (4 bytes for
  39. size + 12 pad bytes).
  40. At the end of "rootfs" additional 16 bytes are abused for some data and a
  41. highly important eRcOmM identifier, so the last 32 bytes of "rootfs" are abused.
  42. At the end of "u-boot" 128 bytes are abused for some data, a checksum and a
  43. highly important sErCoMm identifier.
  44. This program uses a special GNU scanf modifier to allocate
  45. sufficient memory for a strings with unknown length.
  46. See http://www.kernel.org/doc/man-pages/online/pages/man3/scanf.3.html#NOTES
  47. To extract everything from a Linksys style firmware image see
  48. https://forum.openwrt.org/viewtopic.php?pid=92928#p92928
  49. Changelog:
  50. v2.4 - added ":image" definition for parameter file, this allows
  51. to use a complete sysupgrade image without any kernel size check
  52. v2.3 - allow jffs by adding its magic number (0x8519)
  53. added parameter option -i to ignore unknown magic numbers
  54. v2.2 - fixed checksum byte calculation for other versions than 0x2019
  55. fixed rare problem with padsize
  56. updated info to stock firmware 2.00.20
  57. fixed typos
  58. v2.1 - used "wrt350n.bin" for the created image (closer to stock)
  59. added option to create the image in two separate steps (-b / -z)
  60. v2.0 - complete re-write
  61. */
  62. // includes
  63. #define _GNU_SOURCE // for GNU's basename()
  64. #include <assert.h>
  65. #include <errno.h> // errno
  66. #include <stdarg.h>
  67. #include <stdio.h> // fopen(), fread(), fclose(), etc.
  68. #include <stdlib.h> // system(), etc.
  69. #include <string.h> // basename(), strerror(), strdup(), etc.
  70. #include <unistd.h> // optopt(), access(), etc.
  71. #include <libgen.h>
  72. #include <sys/wait.h> // WEXITSTATUS, etc.
  73. // custom includes
  74. #include "md5.h" // MD5 routines
  75. #include "upgrade.h" // Linksys definitions from firmware 2.0.19 (unchanged up to 2.0.20)
  76. // version info
  77. #define VERSION "2.4"
  78. char program_info[] = "WRT350Nv2-Builder v%s by Dirk Teurlings <info@upexia.nl> and Matthias Buecher (http://www.maddes.net/)\n";
  79. // verbosity
  80. #define DEBUG 1
  81. #define DEBUG_LVL2 2
  82. int verbosity = 0;
  83. // mtd info
  84. typedef struct {
  85. char *name;
  86. int offset;
  87. int size;
  88. char *filename;
  89. long int filesize;
  90. unsigned char magic[2];
  91. } mtd_info;
  92. mtd_info mtd_kernel = { "kernel", 0, 0, NULL, 0L, { 0, 0 } };
  93. mtd_info mtd_rootfs = { "rootfs", 0, 0, NULL, 0L, { 0, 0 } };
  94. mtd_info mtd_image = { "image", 0, 0, NULL, 0L, { 0, 0 } };
  95. mtd_info mtd_uboot = { "u-boot", 0, 0, NULL, 0L, { 0, 0 } };
  96. #define ROOTFS_END_OFFSET 0x00760000
  97. #define ROOTFS_MIN_OFFSET 0x00640000 // should be filled up to here, to make sure that the zip file is big enough to pass the size check of the stock firmware
  98. // 2.0.17: filled up to 0x00640000
  99. // 2.0.19: filled up to 0x00670000
  100. // 2.0.20: filled up to 0x00670000
  101. // rootfs statics via: hexdump -v -e '1/1 "0x%02X, "' -s 0x0075FFE0 -n 16 "wrt350n.bin" ; echo -en "\n"
  102. unsigned char product_id[] = { 0x00, 0x03 }; // seems to be a fixed value
  103. unsigned char protocol_id[] = { 0x00, 0x00 }; // seems to be a fixed value
  104. unsigned char fw_version[] = { 0x20, 0x20 };
  105. unsigned char rootfs_unknown[] = { 0x90, 0xF7 }; // seems to be a fixed value
  106. unsigned char sign[] = { 0x65, 0x52, 0x63, 0x4F, 0x6D, 0x4D, 0x00, 0x00 }; // eRcOmM
  107. // u-boot statics via: hexdump -v -e '1/1 "0x%02X, "' -s 0x007FFF80 -n 128 "wrt350n.bin" ; echo -en "\n"
  108. //unsigned char sn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // (12) seems to be an unused value
  109. //unsigned char pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // (8) seems to be an unused value
  110. //unsigned char node[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (25) seems to be an unused value
  111. // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  112. //unsigned char checksum[] = { 0xE9 }; // (1) is calculated, does it belong to node?
  113. unsigned char pid[] = { 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x01, 0x00, 0x00, 0x59, 0x42, 0x50, 0x00, 0x01, // (70) seems to be a fixed value, except for fw version
  114. 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, // protocol id?
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, // protocol id?
  117. 0x12, 0x34, // firmware version, same as in rootfs
  118. 0x00, 0x00, 0x00, 0x04,
  119. 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D }; // sErCoMm
  120. // img statics via: hexdump -v -e '1/1 "0x%02X, "' -s 0 -n 512 "WRT350N-EU-ETSI-2.00.19.img" ; echo -en "\n" (unchanged up to 2.0.20)
  121. unsigned char img_hdr[] = { 0x00, 0x01, 0x00, 0x00, 0x59, 0x42, 0x50, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
  124. 0x00, 0x00,
  125. 0x12, 0x34, // firmware version, same as in rootfs
  126. 0x00, 0x00, 0x00, 0x04, 0x61, 0x44, 0x6D, 0x42, 0x6C, 0x4B, 0x3D, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // md5 checksum
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  155. unsigned char img_eof[] = { 0xFF };
  156. void lprintf(int outputlevel, char *fmt, ...) {
  157. va_list argp;
  158. if (outputlevel <= verbosity) {
  159. va_start(argp, fmt);
  160. vprintf(fmt, argp);
  161. va_end(argp);
  162. }
  163. }
  164. int parse_par_file(FILE *f_par) {
  165. int exitcode = 0;
  166. char *buffer;
  167. size_t buffer_size;
  168. char *line;
  169. int lineno;
  170. int count;
  171. char string1[256];
  172. char string2[256];
  173. int value;
  174. mtd_info *mtd;
  175. FILE *f_in;
  176. int f_exitcode = 0;
  177. // read all lines
  178. buffer_size = 1000;
  179. buffer = NULL;
  180. lineno = 0;
  181. while (!feof(f_par)) {
  182. // read next line into memory
  183. do {
  184. // allocate memory for input line
  185. if (!buffer) {
  186. buffer = malloc(buffer_size);
  187. }
  188. if (!buffer) {
  189. exitcode = 1;
  190. printf("parse_par_file: can not allocate %i bytes\n", (int) buffer_size);
  191. break;
  192. }
  193. line = fgets(buffer, buffer_size, f_par);
  194. if (!line) {
  195. exitcode = ferror(f_par);
  196. if (exitcode) {
  197. printf("parse_par_file: %s\n", strerror(exitcode));
  198. }
  199. break;
  200. }
  201. // if buffer was not completely filled, then assume that line is complete
  202. count = strlen(buffer) + 1;
  203. if (count-- < buffer_size) {
  204. break;
  205. }
  206. // otherwise....
  207. // reset file position to line start
  208. value = fseek(f_par, -count, SEEK_CUR);
  209. if (value == -1) {
  210. exitcode = errno;
  211. printf("parse_par_file: %s\n", strerror(exitcode));
  212. break;
  213. }
  214. // double buffer size
  215. free(buffer);
  216. buffer = NULL;
  217. buffer_size *= 2;
  218. lprintf(DEBUG_LVL2, " extending buffer to %i bytes\n", buffer_size);
  219. } while (1);
  220. if ((!line) || (exitcode)) {
  221. break;
  222. }
  223. lineno++; // increase line number
  224. lprintf(DEBUG_LVL2, " line %i (%i) %s", lineno, count, line);
  225. value = 0;
  226. mtd = NULL;
  227. // split line if starting with a colon
  228. switch (line[0]) {
  229. case ':':
  230. count = sscanf(line, ":%255s %i %255s", string1, &value, string2);
  231. if (count != 3) {
  232. printf("line %i does not meet defined format (:<mtdname> <mtdsize> <file>)\n", lineno);
  233. } else {
  234. // populate mtd_info if supported mtd names
  235. if (!strcmp(string1, mtd_kernel.name)) {
  236. mtd = &mtd_kernel;
  237. } else if (!strcmp(string1, mtd_rootfs.name)) {
  238. mtd = &mtd_rootfs;
  239. } else if (!strcmp(string1, mtd_uboot.name)) {
  240. mtd = &mtd_uboot;
  241. } else if (!strcmp(string1, mtd_image.name)) {
  242. mtd = &mtd_image;
  243. }
  244. if (!mtd) {
  245. printf("unknown mtd %s in line %i\n", string1, lineno);
  246. } else if (mtd->filename) {
  247. f_exitcode = 1;
  248. printf("mtd %s in line %i multiple definitions\n", string1, lineno);
  249. } else {
  250. mtd->size = value;
  251. mtd->filename = strdup(string2);
  252. // Get file size
  253. f_in = fopen(mtd->filename, "rb");
  254. if (!f_in) {
  255. f_exitcode = errno;
  256. printf("input file %s: %s\n", mtd->filename, strerror(f_exitcode));
  257. } else {
  258. value = fread(&mtd->magic, 1, 2, f_in);
  259. if (value < 2) {
  260. if (ferror(f_in)) {
  261. f_exitcode = ferror(f_in);
  262. printf("input file %s: %s\n", mtd->filename, strerror(f_exitcode));
  263. } else {
  264. f_exitcode = 1;
  265. printf("input file %s: smaller than two bytes, no magic code\n", mtd->filename);
  266. }
  267. }
  268. value = fseek(f_in, 0, SEEK_END);
  269. if (value == -1) {
  270. f_exitcode = errno;
  271. printf("input file %s: %s\n", mtd->filename, strerror(f_exitcode));
  272. } else {
  273. mtd->filesize = ftell(f_in);
  274. if (mtd->filesize == -1) {
  275. f_exitcode = errno;
  276. printf("input file %s: %s\n", mtd->filename, strerror(f_exitcode));
  277. }
  278. }
  279. fclose(f_in);
  280. }
  281. lprintf(DEBUG, "mtd %s in line %i: size=0x%08X, filesize=0x%08lX, magic=0x%02X%02X, file=%s\n", mtd->name, lineno, mtd->size, mtd->filesize, mtd->magic[0], mtd->magic[1], mtd->filename);
  282. }
  283. }
  284. break;
  285. case '#': // integer values
  286. count = sscanf(line, "#%255s %i", string1, &value);
  287. if (count != 2) {
  288. printf("line %i does not meet defined format (#<variable name> <integer>\n", lineno);
  289. } else {
  290. if (!strcmp(string1, "version")) {
  291. // changing version
  292. fw_version[0] = 0x000000FF & ( value >> 8 );
  293. fw_version[1] = 0x000000FF & value;
  294. } else {
  295. printf("unknown integer variable %s in line %i\n", string1, lineno);
  296. }
  297. lprintf(DEBUG, "integer variable %s in line %i: 0x%08X\n", string1, lineno, value);
  298. }
  299. break;
  300. case '$': // strings
  301. count = sscanf(line, "$%255s %255s", string1, string2);
  302. if (count != 2) {
  303. printf("line %i does not meet defined format (:<mtdname> <mtdsize> <file>)\n", lineno);
  304. } else {
  305. /*
  306. if (!strcmp(string1, "something")) {
  307. something = strdup(string2);
  308. } else {
  309. */
  310. printf("unknown string variable %s in line %i\n", string1, lineno);
  311. // }
  312. lprintf(DEBUG, "string variable %s in line %i: %s\n", string1, lineno, string2);
  313. }
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. free(buffer);
  320. if (!exitcode) {
  321. exitcode = f_exitcode;
  322. }
  323. return exitcode;
  324. }
  325. int create_bin_file(char *bin_filename) {
  326. int exitcode = 0;
  327. unsigned char *buffer;
  328. int i;
  329. mtd_info *mtd;
  330. int addsize;
  331. int padsize;
  332. char *rand_filename = "/dev/urandom";
  333. FILE *f_in;
  334. int size;
  335. unsigned long int csum;
  336. unsigned char checksum;
  337. FILE *f_out;
  338. // allocate memory for bin file
  339. buffer = malloc(KERNEL_CODE_OFFSET + FLASH_SIZE);
  340. if (!buffer) {
  341. exitcode = 1;
  342. printf("create_bin_file: can not allocate %i bytes\n", FLASH_SIZE);
  343. } else {
  344. // initialize with zero
  345. memset(buffer, 0, KERNEL_CODE_OFFSET + FLASH_SIZE);
  346. }
  347. // add files
  348. if (!exitcode) {
  349. for (i = 1; i <= 4; i++) {
  350. addsize = 0;
  351. padsize = 0;
  352. switch (i) {
  353. case 1:
  354. mtd = &mtd_image;
  355. padsize = ROOTFS_MIN_OFFSET - mtd->filesize;
  356. break;
  357. case 2:
  358. mtd = &mtd_kernel;
  359. break;
  360. case 3:
  361. mtd = &mtd_rootfs;
  362. addsize = mtd->filesize;
  363. padsize = ROOTFS_MIN_OFFSET - mtd_kernel.size - mtd->filesize;
  364. break;
  365. case 4:
  366. mtd = &mtd_uboot;
  367. addsize = mtd->filesize;
  368. break;
  369. default:
  370. mtd = NULL;
  371. exitcode = 1;
  372. printf("create_bin_file: unknown mtd %i\n", i);
  373. break;
  374. }
  375. if (!mtd) {
  376. break;
  377. }
  378. if (!mtd->filename) {
  379. continue;
  380. }
  381. lprintf(DEBUG, "adding mtd %s file %s\n", mtd->name, mtd->filename);
  382. // adding file size
  383. if (addsize) {
  384. buffer[KERNEL_CODE_OFFSET + mtd->offset - 16] = 0x000000FFL & ( addsize >> 24 );
  385. buffer[KERNEL_CODE_OFFSET + mtd->offset - 15] = 0x000000FFL & ( addsize >> 16 );
  386. buffer[KERNEL_CODE_OFFSET + mtd->offset - 14] = 0x000000FFL & ( addsize >> 8 );
  387. buffer[KERNEL_CODE_OFFSET + mtd->offset - 13] = 0x000000FFL & addsize;
  388. }
  389. // adding file content
  390. f_in = fopen(mtd->filename, "rb");
  391. if (!f_in) {
  392. exitcode = errno;
  393. printf("input file %s: %s\n", mtd->filename, strerror(exitcode));
  394. } else {
  395. size = fread(&buffer[KERNEL_CODE_OFFSET + mtd->offset], mtd->filesize, 1, f_in);
  396. if (size < 1) {
  397. if (ferror(f_in)) {
  398. exitcode = ferror(f_in);
  399. printf("input file %s: %s\n", mtd->filename, strerror(exitcode));
  400. } else {
  401. exitcode = 1;
  402. printf("input file %s: smaller than before *doh*\n", mtd->filename);
  403. }
  404. }
  405. fclose(f_in);
  406. }
  407. // padding
  408. if (padsize > 0) {
  409. addsize = padsize & 0x0000FFFF; // start on next 64KB border
  410. padsize -= addsize;
  411. }
  412. if (padsize > 0) {
  413. printf("mtd %s input file %s is too small (0x%08lX), adding 0x%08X random bytes\n", mtd->name, mtd->filename, mtd->filesize, padsize);
  414. addsize += KERNEL_CODE_OFFSET + mtd->offset + mtd->filesize; // get offset
  415. lprintf(DEBUG, " padding offset 0x%08X length 0x%08X\n", addsize, padsize);
  416. f_in = fopen(rand_filename, "rb");
  417. if (!f_in) {
  418. exitcode = errno;
  419. printf("input file %s: %s\n", rand_filename, strerror(exitcode));
  420. } else {
  421. size = fread(&buffer[addsize], padsize, 1, f_in);
  422. if (size < 1) {
  423. if (ferror(f_in)) {
  424. exitcode = ferror(f_in);
  425. printf("input file %s: %s\n", rand_filename, strerror(exitcode));
  426. } else {
  427. exitcode = 1;
  428. printf("input file %s: smaller than before *doh*\n", rand_filename);
  429. }
  430. }
  431. }
  432. fclose(f_in);
  433. }
  434. }
  435. }
  436. // add special contents
  437. if (!exitcode) {
  438. lprintf(DEBUG, "adding rootfs special data\n");
  439. memcpy(&buffer[KERNEL_CODE_OFFSET + PRODUCT_ID_OFFSET], product_id, 2);
  440. memcpy(&buffer[KERNEL_CODE_OFFSET + PROTOCOL_ID_OFFSET], protocol_id, 2);
  441. memcpy(&buffer[KERNEL_CODE_OFFSET + FW_VERSION_OFFSET], fw_version, 2);
  442. memcpy(&buffer[KERNEL_CODE_OFFSET + FW_VERSION_OFFSET + 2], rootfs_unknown, 2);
  443. memcpy(&buffer[KERNEL_CODE_OFFSET + SIGN_OFFSET], sign, 8); // eRcOmM
  444. lprintf(DEBUG, "adding u-boot special data\n");
  445. // memcpy(&buffer[KERNEL_CODE_OFFSET + SN_OFF], sn, 12); // ToDo: currently zero, find out what's this for?
  446. // memcpy(&buffer[KERNEL_CODE_OFFSET + PIN_OFF], pin, 8); // ToDo: currently zero, find out what's this for?
  447. // memcpy(&buffer[KERNEL_CODE_OFFSET + NODE_BASE_OFF], node, 25); // ToDo: currently zero, find out what's this for?
  448. memcpy(&buffer[KERNEL_CODE_OFFSET + BOOT_ADDR_BASE_OFF + PID_OFFSET], pid, 70); // sErCoMm
  449. memcpy(&buffer[KERNEL_CODE_OFFSET + BOOT_ADDR_BASE_OFF + PID_OFFSET + 57], fw_version, 2);
  450. lprintf(DEBUG, "adding checksum byte\n");
  451. csum = 0;
  452. for (i = 0; i < KERNEL_CODE_OFFSET + FLASH_SIZE; i++) {
  453. csum += buffer[i];
  454. }
  455. lprintf(DEBUG_LVL2, " checksum 0x%016lX (%li)\n", csum, csum);
  456. buffer[KERNEL_CODE_OFFSET + NODE_BASE_OFF + 25] = ~csum + 1;
  457. lprintf(DEBUG, " byte 0x%02X\n", buffer[KERNEL_CODE_OFFSET + NODE_BASE_OFF + 25]);
  458. }
  459. // write bin file
  460. if (!exitcode) {
  461. lprintf(DEBUG, "writing file %s\n", bin_filename);
  462. f_out = fopen(bin_filename, "wb");
  463. if (!f_out) {
  464. exitcode = errno;
  465. printf("output file %s: %s\n", bin_filename, strerror(exitcode));
  466. } else {
  467. size = fwrite(buffer, KERNEL_CODE_OFFSET + FLASH_SIZE, 1, f_out);
  468. if (size < 1) {
  469. if (ferror(f_out)) {
  470. exitcode = ferror(f_out);
  471. printf("output file %s: %s\n", bin_filename, strerror(exitcode));
  472. } else {
  473. exitcode = 1;
  474. printf("output file %s: unspecified write error\n", bin_filename);
  475. }
  476. }
  477. fclose(f_out);
  478. }
  479. }
  480. return exitcode;
  481. }
  482. int create_zip_file(char *zip_filename, char *bin_filename) {
  483. int exitcode = 0;
  484. char *buffer;
  485. size_t buffer_size;
  486. int count;
  487. buffer_size = 1000;
  488. buffer = NULL;
  489. do {
  490. // allocate memory for command line
  491. if (!buffer) {
  492. buffer = malloc(buffer_size);
  493. }
  494. if (!buffer) {
  495. exitcode = 1;
  496. printf("create_zip_file: can not allocate %i bytes\n", (int) buffer_size);
  497. break;
  498. }
  499. // if buffer was not completely filled, then line fit in completely
  500. count = snprintf(buffer, buffer_size, "zip \"%s\" \"%s\"", zip_filename, bin_filename);
  501. if ((count > -1) && (count < buffer_size)) {
  502. break;
  503. }
  504. // otherwise try again with more space
  505. if (count > -1) { // glibc 2.1
  506. buffer_size = count + 1; // precisely what is needed
  507. } else { // glibc 2.0
  508. buffer_size *= 2; // twice the old size
  509. }
  510. free(buffer);
  511. buffer = NULL;
  512. lprintf(DEBUG_LVL2, " extending buffer to %i bytes\n", buffer_size);
  513. } while (1);
  514. if (!exitcode) {
  515. // zipping binfile
  516. lprintf(DEBUG, "%s\n", buffer);
  517. count = system(buffer);
  518. if ((count < 0) || (WEXITSTATUS(count))) {
  519. exitcode = 1;
  520. printf("create_zip_file: can not execute %s bytes\n", buffer);
  521. }
  522. }
  523. return exitcode;
  524. }
  525. int create_img_file(FILE *f_out, char *out_filename, char *zip_filename) {
  526. int exitcode = 0;
  527. md5_state_t state;
  528. md5_byte_t digest[16];
  529. int i;
  530. int size;
  531. FILE *f_in;
  532. unsigned char buffer[1];
  533. // copy firmware version
  534. memcpy(&img_hdr[50], fw_version, 2);
  535. // clear md5 checksum
  536. memset(&img_hdr[480], 0, 16);
  537. // prepare md5 checksum calculation
  538. md5_init(&state);
  539. // add img header
  540. lprintf(DEBUG_LVL2, " adding img header\n");
  541. for (i = 0; i < 512; i++) {
  542. size = fputc(img_hdr[i], f_out);
  543. if (size == EOF) {
  544. exitcode = ferror(f_out);
  545. printf("output file %s: %s\n", out_filename, strerror(exitcode));
  546. break;
  547. }
  548. md5_append(&state, (const md5_byte_t *)&img_hdr[i], 1);
  549. }
  550. // adding zip file
  551. if (!exitcode) {
  552. lprintf(DEBUG_LVL2, " adding zip file\n");
  553. f_in = fopen(zip_filename, "rb");
  554. if (!f_in) {
  555. exitcode = errno;
  556. printf("input file %s: %s\n", zip_filename, strerror(exitcode));
  557. } else {
  558. while ((size = fgetc(f_in)) != EOF) {
  559. buffer[0] = size;
  560. size = fputc(buffer[0], f_out);
  561. if (size == EOF) {
  562. exitcode = ferror(f_out);
  563. printf("output file %s: %s\n", out_filename, strerror(exitcode));
  564. break;
  565. }
  566. md5_append(&state, (const md5_byte_t *)buffer, 1);
  567. }
  568. if (ferror(f_in)) {
  569. exitcode = ferror(f_in);
  570. printf("input file %s: %s\n", zip_filename, strerror(exitcode));
  571. }
  572. }
  573. }
  574. // add end byte
  575. if (!exitcode) {
  576. lprintf(DEBUG_LVL2, " adding img eof byte\n");
  577. size = fputc(img_eof[0], f_out);
  578. if (size == EOF) {
  579. exitcode = ferror(f_out);
  580. printf("output file %s: %s\n", out_filename, strerror(exitcode));
  581. }
  582. md5_append(&state, (const md5_byte_t *)img_eof, 1);
  583. }
  584. // append salt to md5 checksum
  585. md5_append(&state, (const md5_byte_t *)"A^gU*<>?RFY@#DR&Z", 17);
  586. // finish md5 checksum calculation
  587. md5_finish(&state, digest);
  588. // write md5 checksum into img header
  589. if (!exitcode) {
  590. lprintf(DEBUG_LVL2, " writing md5 checksum into img header of file\n");
  591. size = fseek(f_out, 480, SEEK_SET);
  592. if (size == -1) {
  593. exitcode = errno;
  594. printf("output file %s: %s\n", out_filename, strerror(exitcode));
  595. } else {
  596. size = fwrite(digest, 16, 1, f_out);
  597. if (size < 1) {
  598. if (ferror(f_out)) {
  599. exitcode = ferror(f_out);
  600. printf("output file %s: %s\n", out_filename, strerror(exitcode));
  601. } else {
  602. exitcode = 1;
  603. printf("output file %s: unspecified write error\n", out_filename);
  604. }
  605. }
  606. }
  607. fclose(f_in);
  608. }
  609. return exitcode;
  610. }
  611. int main(int argc, char *argv[]) {
  612. int exitcode = 0;
  613. int help;
  614. int onlybin;
  615. int havezip;
  616. int ignoremagic;
  617. char option;
  618. char *par_filename = NULL;
  619. char *img_filename = NULL;
  620. char *base_filename = NULL;
  621. char *bin_filename = NULL;
  622. char *zip_filename = NULL;
  623. FILE *f_par = NULL;
  624. FILE *f_img = NULL;
  625. int i;
  626. mtd_info *mtd;
  627. int noupdate;
  628. int sizecheck;
  629. int magiccheck;
  630. int magicerror;
  631. // display program header
  632. printf(program_info, VERSION);
  633. // command line processing
  634. // options
  635. help = 0;
  636. onlybin = 0;
  637. havezip = 0;
  638. ignoremagic = 0;
  639. while ((option = getopt(argc, argv, "hbzif:v")) != -1) {
  640. switch(option) {
  641. case 'h':
  642. help = 1;
  643. break;
  644. case 'b':
  645. onlybin = 1;
  646. break;
  647. case 'z':
  648. havezip = 1;
  649. break;
  650. case 'i':
  651. ignoremagic = 1;
  652. break;
  653. case 'f':
  654. sizecheck = sscanf(optarg, "%i", &i);
  655. if (sizecheck != 1) {
  656. printf("Firmware version of -f option not a valid integer\n");
  657. exitcode = 1;
  658. } else {
  659. fw_version[0] = 0x000000FF & ( i >> 8 );
  660. fw_version[1] = 0x000000FF & i;
  661. }
  662. break;
  663. case 'v':
  664. verbosity++;
  665. break;
  666. case ':': // option with missing operand
  667. printf("Option -%c requires an operand\n", optopt);
  668. exitcode = 1;
  669. break;
  670. case '?':
  671. printf("Unrecognized option: -%c\n", optopt);
  672. exitcode = 1;
  673. break;
  674. }
  675. }
  676. // files
  677. for ( ; optind < argc; optind++) {
  678. if (!par_filename) {
  679. par_filename = argv[optind];
  680. if (access(par_filename, R_OK)) {
  681. if (havezip) {
  682. printf("No read access to zip file %s\n", par_filename);
  683. } else {
  684. printf("No read access to parameter or zip file %s\n", par_filename);
  685. }
  686. exitcode = 1;
  687. }
  688. continue;
  689. }
  690. if ((!onlybin) && (!img_filename)) {
  691. img_filename = argv[optind];
  692. if (!access(img_filename, F_OK)) { // if file already exists then check write access
  693. if (access(img_filename, W_OK)) {
  694. printf("No write access to image file %s\n", img_filename);
  695. exitcode = 1;
  696. }
  697. }
  698. continue;
  699. }
  700. printf("Too many files stated\n");
  701. exitcode = 1;
  702. break;
  703. }
  704. // file name checks
  705. if (!par_filename) {
  706. if (havezip) {
  707. printf("Zip file not stated\n");
  708. } else {
  709. printf("Parameter file not stated\n");
  710. }
  711. exitcode = 1;
  712. } else {
  713. base_filename = basename(par_filename);
  714. if (!base_filename) {
  715. if (havezip) {
  716. printf("Zip file is a directory\n");
  717. } else {
  718. printf("Parameter file is a directory\n");
  719. }
  720. exitcode = 1;
  721. }
  722. }
  723. if (!onlybin) {
  724. if (!img_filename) {
  725. printf("Image file not stated\n");
  726. exitcode = 1;
  727. } else {
  728. base_filename = basename(img_filename);
  729. if (!base_filename) {
  730. printf("Image file is a directory\n");
  731. exitcode = 1;
  732. }
  733. }
  734. }
  735. // check for mutually exclusive options
  736. if ((onlybin) && (havezip)) {
  737. printf("Option -b and -z are mutually exclusive\n");
  738. exitcode = 1;
  739. }
  740. // react on option problems or help request, then exit
  741. if ((exitcode) || (help)) {
  742. if (help) {
  743. printf("This program creates Linksys style images for the WRT350Nv2 router.\n");
  744. }
  745. printf(" Usage:\n\
  746. %s [-h] [-b] [-z] [-i] [-f <version>] [-v] <parameter or zip file> [<image file>]\n\n\
  747. Options:\n\
  748. -h - Show this help\n\
  749. -b - Create only bin file, no img or zip file is created\n\
  750. -z - Have zip file, the img file will be directly created from it\n\
  751. -i - Ignore unknown magic numbers\n\
  752. -f <version> - Wanted firmware version to use with -z\n\
  753. Default firmware version is 0x2020 = 2.00.20.\n\
  754. Note: version from parameter file will supersede this\n\
  755. -v - Increase debug verbosity level\n\n\
  756. Example:\n\
  757. %s wrt350nv2.par wrt350nv2.img\n\n", argv[0], argv[0]);
  758. return exitcode;
  759. }
  760. // handle special case when zipfile is stated
  761. if (havezip) {
  762. zip_filename = par_filename;
  763. par_filename = NULL;
  764. }
  765. lprintf(DEBUG_LVL2, " Verbosity: %i\n", verbosity);
  766. lprintf(DEBUG_LVL2, " Program: %s\n", argv[0]);
  767. if (par_filename) {
  768. lprintf(DEBUG, "Parameter file: %s\n", par_filename);
  769. }
  770. if (zip_filename) {
  771. lprintf(DEBUG, "Zip file: %s\n", zip_filename);
  772. }
  773. if (img_filename) {
  774. lprintf(DEBUG, "Image file: %s\n", img_filename);
  775. }
  776. // open files from command line
  777. // parameter/zip file
  778. if (par_filename) {
  779. f_par = fopen(par_filename, "rt");
  780. if (!f_par) {
  781. exitcode = errno;
  782. printf("Input file %s: %s\n", par_filename, strerror(exitcode));
  783. }
  784. }
  785. // image file
  786. if (img_filename) {
  787. f_img = fopen(img_filename, "wb");
  788. if (!f_img) {
  789. exitcode = errno;
  790. printf("Output file %s: %s\n", img_filename, strerror(exitcode));
  791. }
  792. }
  793. if (exitcode) {
  794. return exitcode;
  795. }
  796. // parameter file processing
  797. if ((!exitcode) && (f_par)) {
  798. lprintf(DEBUG, "parsing parameter file...\n");
  799. exitcode = parse_par_file(f_par);
  800. lprintf(DEBUG, "...done parsing file\n");
  801. }
  802. if (f_par) {
  803. fclose(f_par);
  804. }
  805. // check all input data
  806. if ((!exitcode) && (par_filename)) {
  807. lprintf(DEBUG, "checking mtd data...\n");
  808. for (i = 1; i <= 4; i++) {
  809. noupdate = 0;
  810. sizecheck = 0;
  811. magiccheck = 0;
  812. switch (i) {
  813. case 1:
  814. mtd = &mtd_image;
  815. sizecheck = ROOTFS_END_OFFSET;
  816. magiccheck = 1;
  817. break;
  818. case 2:
  819. mtd = &mtd_kernel;
  820. sizecheck = mtd_kernel.size - 16;
  821. magiccheck = 1;
  822. break;
  823. case 3:
  824. mtd = &mtd_rootfs;
  825. mtd->offset = mtd_kernel.size;
  826. mtd->size = ROOTFS_END_OFFSET - mtd_kernel.size;
  827. sizecheck = PRODUCT_ID_OFFSET - mtd_kernel.size;
  828. magiccheck = 1;
  829. break;
  830. case 4:
  831. mtd = &mtd_uboot;
  832. mtd->offset = BOOT_ADDR_BASE_OFF;
  833. noupdate = 1;
  834. sizecheck = SN_OFF - BOOT_ADDR_BASE_OFF;
  835. break;
  836. default:
  837. mtd = NULL;
  838. exitcode = 1;
  839. printf("unknown mtd check %i\n", i);
  840. break;
  841. }
  842. if (!mtd) {
  843. break;
  844. }
  845. lprintf(DEBUG_LVL2, " checking mtd %s\n", mtd->name);
  846. // general checks
  847. // no further checks if no file data present
  848. if (!mtd->filename) {
  849. continue;
  850. }
  851. // not updated by stock firmware
  852. if (noupdate) {
  853. printf("mtd %s is specified, but will not be updated as of Linksys firmware 2.0.19\n", mtd->name);
  854. }
  855. // general magic number check
  856. magicerror = 0;
  857. if (magiccheck) {
  858. switch (i) {
  859. case 1: // image
  860. case 2: // kernel
  861. if (!(
  862. ((mtd->magic[0] == 0x27) && (mtd->magic[1] == 0x05)) // uImage
  863. )) {
  864. magicerror = 1;
  865. }
  866. break;
  867. case 3: // rootfs
  868. if (!(
  869. ((mtd->magic[0] == 0x68) && (mtd->magic[1] == 0x73)) // squashfs
  870. || ((mtd->magic[0] == 0x85) && (mtd->magic[1] == 0x19)) // jffs
  871. )) {
  872. magicerror = 1;
  873. }
  874. break;
  875. default:
  876. magicerror = 1;
  877. break;
  878. }
  879. if (magicerror) {
  880. printf("mtd %s input file %s has unknown magic number (0x%02X%02X)", mtd->name, mtd->filename, mtd->magic[0], mtd->magic[1]);
  881. if (ignoremagic) {
  882. printf("...ignoring");
  883. } else {
  884. exitcode = 1;
  885. }
  886. printf("\n");
  887. }
  888. }
  889. // mtd specific size check
  890. if (mtd == &mtd_image) {
  891. if (mtd->filesize < 0x00200000) {
  892. exitcode = 1;
  893. printf("mtd %s input file %s too unrealistic small (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
  894. }
  895. }
  896. if (mtd == &mtd_kernel) {
  897. if (mtd->filesize < 0x00080000) {
  898. exitcode = 1;
  899. printf("mtd %s input file %s too unrealistic small (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
  900. }
  901. }
  902. // general size check
  903. if (sizecheck) {
  904. if (sizecheck <= 0) {
  905. exitcode = 1;
  906. printf("mtd %s bad file size check (%i) due to input data\n", mtd->name, sizecheck);
  907. } else {
  908. if (mtd->filesize > sizecheck) {
  909. exitcode = 1;
  910. printf("mtd %s input file %s too big (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
  911. }
  912. }
  913. }
  914. }
  915. // Check for mandatory parts
  916. if ((!mtd_image.filename) && (!mtd_kernel.filename || !mtd_rootfs.filename)) {
  917. exitcode = 1;
  918. if (mtd_kernel.filename && !mtd_rootfs.filename) {
  919. printf("Kernel without rootfs, either incorrectly specified or not at all in parameter file\n");
  920. } else if (!mtd_kernel.filename && mtd_rootfs.filename) {
  921. printf("Rootfs without kernel, either incorrectly specified or not at all in parameter file\n");
  922. } else {
  923. printf("Neither an image nor kernel with rootfs was/were correctly specified or at all in parameter file\n");
  924. }
  925. }
  926. // Check for duplicate parts
  927. if ((mtd_image.filename) && (mtd_kernel.filename || mtd_rootfs.filename)) {
  928. exitcode = 1;
  929. printf("Image and kernel/rootfs specified in parameter file\n");
  930. }
  931. lprintf(DEBUG, "...done checking mtd data\n");
  932. }
  933. // bin creation in memory
  934. if ((!exitcode) && (par_filename)) {
  935. bin_filename = "wrt350n.bin";
  936. lprintf(DEBUG, "creating bin file %s...\n", bin_filename);
  937. exitcode = create_bin_file(bin_filename);
  938. lprintf(DEBUG, "...done creating bin file\n");
  939. }
  940. // zip file creation
  941. if ((!exitcode) && (!onlybin) && (!zip_filename)) {
  942. zip_filename = "wrt350n.zip";
  943. lprintf(DEBUG, "creating zip file %s...\n", zip_filename);
  944. exitcode = create_zip_file(zip_filename, bin_filename);
  945. lprintf(DEBUG, "...done creating zip file\n");
  946. }
  947. // img file creation
  948. if ((!exitcode) && (f_img)) {
  949. lprintf(DEBUG, "creating img file...\n");
  950. exitcode = create_img_file(f_img, img_filename, zip_filename);
  951. lprintf(DEBUG, "...done creating img file\n");
  952. }
  953. // clean up
  954. if (f_img) {
  955. fclose(f_img);
  956. }
  957. // end program
  958. return exitcode;
  959. }