[package] libpcap: explicitely disable libnl support to prevent different build resul...
[openwrt.git] / tools / wrt350nv2-builder / src / wrt350nv2-builder.c
index a459329..3ebb40e 100644 (file)
@@ -1,8 +1,8 @@
 /*
 
 /*
 
-       WRT350Nv2-Builder 2.2 (previously called buildimg)
+       WRT350Nv2-Builder 2.4 (previously called buildimg)
        Copyright (C) 2008-2009 Dirk Teurlings <info@upexia.nl>
        Copyright (C) 2008-2009 Dirk Teurlings <info@upexia.nl>
-       Copyright (C) 2009-2010 Matthias Buecher (http://www.maddes.net/)
+       Copyright (C) 2009-2011 Matthias Buecher (http://www.maddes.net/)
 
        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
 
        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
@@ -33,6 +33,9 @@
                :u-boot 0       /path/to/u-boot.bin
                #version        0x2020
 
                :u-boot 0       /path/to/u-boot.bin
                #version        0x2020
 
+       Additionally since v2.4 an already complete image can be used:
+               :image          0       /path/to/openwrt-wrt350nv2-[squashfs|jffs2-64k].img
+
        args:
                1       wrt350nv2.par           parameter file describing the image layout
                2       wrt350nv2.img           output file for linksys style image
        args:
                1       wrt350nv2.par           parameter file describing the image layout
                2       wrt350nv2.img           output file for linksys style image
        https://forum.openwrt.org/viewtopic.php?pid=92928#p92928
 
        Changelog:
        https://forum.openwrt.org/viewtopic.php?pid=92928#p92928
 
        Changelog:
+       v2.4 - added ":image" definition for parameter file, this allows
+              to use a complete sysupgrade image without any kernel size check
+       v2.3 - allow jffs by adding its magic number (0x8519)
+              added parameter option -i to ignore unknown magic numbers
        v2.2 - fixed checksum byte calculation for other versions than 0x2019
               fixed rare problem with padsize
               updated info to stock firmware 2.00.20
        v2.2 - fixed checksum byte calculation for other versions than 0x2019
               fixed rare problem with padsize
               updated info to stock firmware 2.00.20
@@ -90,7 +97,7 @@
 
 
 // version info
 
 
 // version info
-#define VERSION "2.2"
+#define VERSION "2.4"
 char program_info[] = "WRT350Nv2-Builder v%s by Dirk Teurlings <info@upexia.nl> and Matthias Buecher (http://www.maddes.net/)\n";
 
 // verbosity
 char program_info[] = "WRT350Nv2-Builder v%s by Dirk Teurlings <info@upexia.nl> and Matthias Buecher (http://www.maddes.net/)\n";
 
 // verbosity
@@ -110,6 +117,7 @@ typedef struct {
 
 mtd_info mtd_kernel = { "kernel", 0, 0, NULL, 0L, { 0, 0 } };
 mtd_info mtd_rootfs = { "rootfs", 0, 0, NULL, 0L, { 0, 0 } };
 
 mtd_info mtd_kernel = { "kernel", 0, 0, NULL, 0L, { 0, 0 } };
 mtd_info mtd_rootfs = { "rootfs", 0, 0, NULL, 0L, { 0, 0 } };
+mtd_info mtd_image = { "image", 0, 0, NULL, 0L, { 0, 0 } };
 mtd_info mtd_uboot = { "u-boot", 0, 0, NULL, 0L, { 0, 0 } };
 
 #define ROOTFS_END_OFFSET      0x00760000
 mtd_info mtd_uboot = { "u-boot", 0, 0, NULL, 0L, { 0, 0 } };
 
 #define ROOTFS_END_OFFSET      0x00760000
@@ -279,6 +287,8 @@ int parse_par_file(FILE *f_par) {
                                                mtd = &mtd_rootfs;
                                        } else if (!strcmp(string1, mtd_uboot.name)) {
                                                mtd = &mtd_uboot;
                                                mtd = &mtd_rootfs;
                                        } else if (!strcmp(string1, mtd_uboot.name)) {
                                                mtd = &mtd_uboot;
+                                       } else if (!strcmp(string1, mtd_image.name)) {
+                                               mtd = &mtd_image;
                                        }
 
                                        if (!mtd) {
                                        }
 
                                        if (!mtd) {
@@ -402,20 +412,24 @@ int create_bin_file(char *bin_filename) {
 
        // add files
        if (!exitcode) {
 
        // add files
        if (!exitcode) {
-               for (i = 1; i <= 3; i++) {
+               for (i = 1; i <= 4; i++) {
                        addsize = 0;
                        padsize = 0;
 
                        switch (i) {
                                case 1:
                        addsize = 0;
                        padsize = 0;
 
                        switch (i) {
                                case 1:
-                                       mtd = &mtd_kernel;
+                                       mtd = &mtd_image;
+                                       padsize = ROOTFS_MIN_OFFSET - mtd->filesize;
                                        break;
                                case 2:
                                        break;
                                case 2:
+                                       mtd = &mtd_kernel;
+                                       break;
+                               case 3:
                                        mtd = &mtd_rootfs;
                                        addsize = mtd->filesize;
                                        padsize = ROOTFS_MIN_OFFSET - mtd_kernel.size - mtd->filesize;
                                        break;
                                        mtd = &mtd_rootfs;
                                        addsize = mtd->filesize;
                                        padsize = ROOTFS_MIN_OFFSET - mtd_kernel.size - mtd->filesize;
                                        break;
-                               case 3:
+                               case 4:
                                        mtd = &mtd_uboot;
                                        addsize = mtd->filesize;
                                        break;
                                        mtd = &mtd_uboot;
                                        addsize = mtd->filesize;
                                        break;
@@ -708,6 +722,7 @@ int main(int argc, char *argv[]) {
        int help;
        int onlybin;
        int havezip;
        int help;
        int onlybin;
        int havezip;
+       int ignoremagic;
        char option;
        char *par_filename = NULL;
        char *img_filename = NULL;
        char option;
        char *par_filename = NULL;
        char *img_filename = NULL;
@@ -720,10 +735,10 @@ int main(int argc, char *argv[]) {
 
        int i;
        mtd_info *mtd;
 
        int i;
        mtd_info *mtd;
-       int mandatory;
        int noupdate;
        int sizecheck;
        int noupdate;
        int sizecheck;
-       unsigned char magic[2];
+       int magiccheck;
+       int magicerror;
 
 
 // display program header
 
 
 // display program header
@@ -735,7 +750,8 @@ int main(int argc, char *argv[]) {
        help = 0;
        onlybin = 0;
        havezip = 0;
        help = 0;
        onlybin = 0;
        havezip = 0;
-       while ((option = getopt(argc, argv, ":hbzf:v")) != -1) {
+       ignoremagic = 0;
+       while ((option = getopt(argc, argv, "hbzif:v")) != -1) {
                switch(option) {
                        case 'h':
                                help = 1;
                switch(option) {
                        case 'h':
                                help = 1;
@@ -746,6 +762,9 @@ int main(int argc, char *argv[]) {
                        case 'z':
                                havezip = 1;
                                break;
                        case 'z':
                                havezip = 1;
                                break;
+                       case 'i':
+                               ignoremagic = 1;
+                               break;
                        case 'f':
                                sizecheck = sscanf(optarg, "%i", &i);
                                if (sizecheck != 1) {
                        case 'f':
                                sizecheck = sscanf(optarg, "%i", &i);
                                if (sizecheck != 1) {
@@ -850,11 +869,12 @@ int main(int argc, char *argv[]) {
                        printf("This program creates Linksys style images for the WRT350Nv2 router.\n");
                }
                printf("  Usage:\n\
                        printf("This program creates Linksys style images for the WRT350Nv2 router.\n");
                }
                printf("  Usage:\n\
-  %s [-h] [-b] [-z] [-f <version>] [-v] <parameter or zip file> [<image file>]\n\n\
+  %s [-h] [-b] [-z] [-i] [-f <version>] [-v] <parameter or zip file> [<image file>]\n\n\
   Options:\n\
   -h            -  Show this help\n\
   -b            -  Create only bin file, no img or zip file is created\n\
   -z            -  Have zip file, the img file will be directly created from it\n\
   Options:\n\
   -h            -  Show this help\n\
   -b            -  Create only bin file, no img or zip file is created\n\
   -z            -  Have zip file, the img file will be directly created from it\n\
+  -i            -  Ignore unknown magic numbers\n\
   -f <version>  -  Wanted firmware version to use with -z\n\
                    Default firmware version is 0x2020 = 2.00.20.\n\
                    Note: version from parameter file will supersede this\n\
   -f <version>  -  Wanted firmware version to use with -z\n\
                    Default firmware version is 0x2020 = 2.00.20.\n\
                    Note: version from parameter file will supersede this\n\
@@ -925,31 +945,30 @@ int main(int argc, char *argv[]) {
        if ((!exitcode) && (par_filename)) {
                lprintf(DEBUG, "checking mtd data...\n");
 
        if ((!exitcode) && (par_filename)) {
                lprintf(DEBUG, "checking mtd data...\n");
 
-               for (i = 1; i <= 3; i++) {
-                       mandatory = 0;
+               for (i = 1; i <= 4; i++) {
                        noupdate = 0;
                        sizecheck = 0;
                        noupdate = 0;
                        sizecheck = 0;
-                       magic[0] = 0;
-                       magic[1] = 0;
+                       magiccheck = 0;
 
                        switch (i) {
                                case 1:
 
                        switch (i) {
                                case 1:
+                                       mtd = &mtd_image;
+                                       sizecheck = ROOTFS_END_OFFSET;
+                                       magiccheck = 1;
+                                       break;
+                               case 2:
                                        mtd = &mtd_kernel;
                                        mtd = &mtd_kernel;
-                                       mandatory = 1;
                                        sizecheck = mtd_kernel.size - 16;
                                        sizecheck = mtd_kernel.size - 16;
-                                       magic[0] = 0x27;
-                                       magic[1] = 0x05;
+                                       magiccheck = 1;
                                        break;
                                        break;
-                               case 2:
+                               case 3:
                                        mtd = &mtd_rootfs;
                                        mtd->offset = mtd_kernel.size;
                                        mtd->size = ROOTFS_END_OFFSET - mtd_kernel.size;
                                        mtd = &mtd_rootfs;
                                        mtd->offset = mtd_kernel.size;
                                        mtd->size = ROOTFS_END_OFFSET - mtd_kernel.size;
-                                       mandatory = 1;
                                        sizecheck = PRODUCT_ID_OFFSET - mtd_kernel.size;
                                        sizecheck = PRODUCT_ID_OFFSET - mtd_kernel.size;
-                                       magic[0] = 0x68;
-                                       magic[1] = 0x73;
+                                       magiccheck = 1;
                                        break;
                                        break;
-                               case 3:
+                               case 4:
                                        mtd = &mtd_uboot;
                                        mtd->offset = BOOT_ADDR_BASE_OFF;
                                        noupdate = 1;
                                        mtd = &mtd_uboot;
                                        mtd->offset = BOOT_ADDR_BASE_OFF;
                                        noupdate = 1;
@@ -968,12 +987,8 @@ int main(int argc, char *argv[]) {
                        lprintf(DEBUG_LVL2, " checking mtd %s\n", mtd->name);
 
                        // general checks
                        lprintf(DEBUG_LVL2, " checking mtd %s\n", mtd->name);
 
                        // general checks
-                       if ((mandatory) && (!mtd->filename)) {
-                               exitcode = 1;
-                               printf("mtd %s not specified correctly or at all in parameter file\n", mtd->name);
-                       }
 
 
-                       // end checks if no file data present
+                       // no further checks if no file data present
                        if (!mtd->filename) {
                                continue;
                        }
                        if (!mtd->filename) {
                                continue;
                        }
@@ -984,16 +999,50 @@ int main(int argc, char *argv[]) {
                        }
 
                        // general magic number check
                        }
 
                        // general magic number check
-                       if (magic[0]) {
-                               if ((mtd->magic[0] != magic[0]) || (mtd->magic[1] != magic[1])) {
-                                       exitcode = 1;
-                                       printf("mtd %s input file %s has wrong magic number (0x%02X%02X)\n", mtd->name, mtd->filename, mtd->magic[0], mtd->magic[1]);
+                       magicerror = 0;
+                       if (magiccheck) {
+                               switch (i) {
+                                       case 1: // image
+                                       case 2: // kernel
+                                               if (!( 
+                                                      ((mtd->magic[0] == 0x27) && (mtd->magic[1] == 0x05))     // uImage
+                                               )) {
+                                                       magicerror = 1;
+                                               }
+                                               break;
+                                       case 3: // rootfs
+                                               if (!( 
+                                                      ((mtd->magic[0] == 0x68) && (mtd->magic[1] == 0x73))     // squashfs
+                                                   || ((mtd->magic[0] == 0x85) && (mtd->magic[1] == 0x19))     // jffs
+                                               )) {
+                                                       magicerror = 1;
+                                               }
+                                               break;
+                                       default:
+                                               magicerror = 1;
+                                               break;
+                               }
+                               if (magicerror) {
+                                       printf("mtd %s input file %s has unknown magic number (0x%02X%02X)", mtd->name, mtd->filename, mtd->magic[0], mtd->magic[1]);
+                                       if (ignoremagic) {
+                                               printf("...ignoring");
+                                       } else {
+                                               exitcode = 1;
+                                       }
+                                       printf("\n");
                                }
                        }
 
                        // mtd specific size check
                                }
                        }
 
                        // mtd specific size check
+                       if (mtd == &mtd_image) {
+                               if (mtd->filesize < 0x00200000) {
+                                       exitcode = 1;
+                                       printf("mtd %s input file %s too unrealistic small (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
+                               }
+                       }
+
                        if (mtd == &mtd_kernel) {
                        if (mtd == &mtd_kernel) {
-                               if (mtd->filesize < 0x00050000) {
+                               if (mtd->filesize < 0x00080000) {
                                        exitcode = 1;
                                        printf("mtd %s input file %s too unrealistic small (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
                                }
                                        exitcode = 1;
                                        printf("mtd %s input file %s too unrealistic small (0x%08lX)\n", mtd->name, mtd->filename, mtd->filesize);
                                }
@@ -1012,6 +1061,25 @@ int main(int argc, char *argv[]) {
                                }
                        }
                }
                                }
                        }
                }
+
+               // Check for mandatory parts
+               if ((!mtd_image.filename) && (!mtd_kernel.filename || !mtd_rootfs.filename)) {
+                       exitcode = 1;
+                       if (mtd_kernel.filename && !mtd_rootfs.filename) {
+                               printf("Kernel without rootfs, either incorrectly specified or not at all in parameter file\n");
+                       } else if (!mtd_kernel.filename && mtd_rootfs.filename) {
+                               printf("Rootfs without kernel, either incorrectly specified or not at all in parameter file\n");
+                       } else {
+                               printf("Neither an image nor kernel with rootfs was/were correctly specified or at all in parameter file\n");
+                       }
+               }
+
+               // Check for duplicate parts
+               if ((mtd_image.filename) && (mtd_kernel.filename || mtd_rootfs.filename)) {
+                       exitcode = 1;
+                       printf("Image and kernel/rootfs specified in parameter file\n");
+               }
+
                lprintf(DEBUG, "...done checking mtd data\n");
        }
 
                lprintf(DEBUG, "...done checking mtd data\n");
        }
 
This page took 0.044498 seconds and 4 git commands to generate.