ramips: fix USW-Flex reversed switch-port order
[openwrt/staging/dedeckeh.git] / tools / firmware-utils / src / mkfwimage2.c
index ee09abba1aec34124eaf0cf76a25d12402f33cbb..a0db930df26202ff8e8f5a3ee1025f83cba9f0e9 100644 (file)
@@ -1,21 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2007 Ubiquiti Networks, Inc.
  * Copyright (C) 2008 Lukas Kuna <ValXdater@seznam.cz>
  * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 #include <sys/types.h>
@@ -65,7 +52,6 @@ typedef struct image_info {
 } image_info_t;
 
 static image_info_t im;
-static int debug = 0;
 static int zero_part_baseaddr = 0;
 
 static void write_header(void* mem, const char* version)
@@ -197,6 +183,10 @@ int str2u32(char *arg, u_int32_t *val)
        return 0;
 }
 
+#ifndef STRINGIFY
+#define STRINGIFY2(X) #X
+#define STRINGIFY(X) STRINGIFY2(X)
+#endif
 static int image_layout_add_partition(const char *part_desc)
 {
        part_data_t *d;
@@ -212,7 +202,7 @@ static int image_layout_add_partition(const char *part_desc)
        }
 
        d = &im.parts[im.part_count];
-       t = sscanf(part_desc, "%15[a-zA-Z]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%256s",
+       t = sscanf(part_desc, "%15[-0-9a-zA-Z]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%15[0-9a-fA-Fx]:%"STRINGIFY(PATH_MAX)"s",
                        d->partition_name,
                        offset,
                        length,
@@ -359,12 +349,15 @@ static int build_image(void)
        /* write in-memory buffer into file */
        if ((f = fopen(im.outputfile, "w")) == NULL) {
                ERROR("Can not create output file: '%s'\n", im.outputfile);
+               free(mem);
                return -10;
        }
 
        if (fwrite(mem, mem_size, 1, f) != 1) {
                ERROR("Could not write %d bytes into file: '%s'\n",
                                mem_size, im.outputfile);
+               free(mem);
+               fclose(f);
                return -11;
        }