Patch Detail
get:
Show a patch.
patch:
Update a patch.
put:
Update a patch.
GET /api/patches/709475/?format=api
{ "id": 709475, "url": "http://patchwork.ozlabs.org/api/patches/709475/?format=api", "web_url": "http://patchwork.ozlabs.org/project/lede/patch/1482996419-10245-2-git-send-email-yszhou4tech@gmail.com/", "project": { "id": 54, "url": "http://patchwork.ozlabs.org/api/projects/54/?format=api", "name": "LEDE development", "link_name": "lede", "list_id": "lede-dev.lists.infradead.org", "list_email": "lede-dev@lists.infradead.org", "web_url": "http://lede-project.org/", "scm_url": "", "webscm_url": "http://git.lede-project.org/", "list_archive_url": "", "list_archive_url_format": "", "commit_url_format": "" }, "msgid": "<1482996419-10245-2-git-send-email-yszhou4tech@gmail.com>", "list_archive_url": null, "date": "2016-12-29T07:26:58", "name": "[LEDE-DEV,v3,1/2] tools: wrt400n: fix making factory images with kernel bigger than 1MB", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "ed53aa6a9e4674e98500980d6afa6d8e178932cd", "submitter": { "id": 65295, "url": "http://patchwork.ozlabs.org/api/people/65295/?format=api", "name": "Yousong Zhou", "email": "yszhou4tech@gmail.com" }, "delegate": null, "mbox": "http://patchwork.ozlabs.org/project/lede/patch/1482996419-10245-2-git-send-email-yszhou4tech@gmail.com/mbox/", "series": [], "comments": "http://patchwork.ozlabs.org/api/patches/709475/comments/", "check": "pending", "checks": "http://patchwork.ozlabs.org/api/patches/709475/checks/", "tags": {}, "related": [], "headers": { "Return-Path": "<lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org>", "X-Original-To": "incoming@patchwork.ozlabs.org", "Delivered-To": "patchwork-incoming@bilbo.ozlabs.org", "Received": [ "from bombadil.infradead.org (bombadil.infradead.org\n\t[IPv6:2001:1868:205::9])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3tq1Sx2Wfwz9s3v\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu, 29 Dec 2016 18:29:37 +1100 (AEDT)", "from localhost ([127.0.0.1] helo=bombadil.infradead.org)\n\tby bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux))\n\tid 1cMV7m-0004qo-IX; Thu, 29 Dec 2016 07:27:42 +0000", "from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242])\n\tby bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat\n\tLinux)) id 1cMV7f-0004jr-0A\n\tfor lede-dev@lists.infradead.org; Thu, 29 Dec 2016 07:27:38 +0000", "by mail-pg0-x242.google.com with SMTP id g1so18121659pgn.0\n\tfor <lede-dev@lists.infradead.org>;\n\tWed, 28 Dec 2016 23:27:14 -0800 (PST)", "from titan.office.mos (mx-yf144-138.meituan.com. [36.110.144.138])\n\tby smtp.gmail.com with ESMTPSA id\n\tp5sm89068035pgk.23.2016.12.28.23.27.10\n\t(version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);\n\tWed, 28 Dec 2016 23:27:13 -0800 (PST)" ], "Authentication-Results": "ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"RBL4Mmse\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references;\n\tbh=We8eE+0YeIyUnkW0ogYOjXjxs3wIPQlrmMEJYrsZUAs=;\n\tb=RBL4MmsemnGMNYeOd3/2oMlXYKBhwqwHwo7HzGNEJIIT7f/A4wEdBa+17lLtg6hG9S\n\tmbDrwnsx4BIFw7k+F6oYDwwchnD+ThU+Tf1oYPQxqXHoF/f/wioQlllBff48veOFzWHS\n\ta9DR9iG3hHr+ToRcXSc9ub43cKA2DBXAb96Brx6oO4bHytTRh6fqYPQ73F2Kgbkn83xa\n\t7PerZxdiM6719nbMLpNX2ZPPyXs5v9fT/DpJVYt/QzwH5xy2/Ux9LqWNjQ0JMnogh1ul\n\tTiBB3Ss0ljdKUbiSGBkbuqV3FUvlEOhWgeJTD3EtPcvbUmkm5hIYnIYj0NpmWHeF/9f/\n\tKwGg==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references;\n\tbh=We8eE+0YeIyUnkW0ogYOjXjxs3wIPQlrmMEJYrsZUAs=;\n\tb=jkD1rybzA1wXvJ+6hsqVE/P0aOuN+jkFxOckUPrEcX69cGXTB8DAC9PbtvuJKxWLPX\n\tPpvd+cjJprh/XCyySP4zputUrpFjWim6+4WwtNhirehokKwCDs0cdyjpC7VxqkziJW1e\n\tRAfpSIRl1yBJbonCH+paMNhumRLaDW4AtkHvyhPEeIG+kZl8wkUhdYUc6eaP16PMBlFA\n\tZUAtPuYUJdY/GtDTe3EfWaG48xwlFl1dmmKXrL0xD/aQ0iwsHDyWdxqp0skP4JVRzRq1\n\tXAGZb92ioS+8IIjaDhXYIQRUfCe4LZk6bMlsJTPvaE31xr4QaKknA5jat9HcD1vVO5/g\n\tuxoA==", "X-Gm-Message-State": "AIkVDXJWl7VhijR4ZFU94SFb8htVJqqTOAMN80LobuW+3agb2Jmbihzu0i3JC7sNEeTG6g==", "X-Received": "by 10.98.99.197 with SMTP id x188mr37443313pfb.179.1482996433756;\n\tWed, 28 Dec 2016 23:27:13 -0800 (PST)", "From": "Yousong Zhou <yszhou4tech@gmail.com>", "To": "john@phrozen.org", "Date": "Thu, 29 Dec 2016 15:26:58 +0800", "Message-Id": "<1482996419-10245-2-git-send-email-yszhou4tech@gmail.com>", "X-Mailer": "git-send-email 2.6.4", "In-Reply-To": "<1482996419-10245-1-git-send-email-yszhou4tech@gmail.com>", "References": "<1482996419-10245-1-git-send-email-yszhou4tech@gmail.com>", "X-CRM114-Version": "20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 ", "X-CRM114-CacheID": "sfid-20161228_232735_217243_FA4B4249 ", "X-CRM114-Status": "GOOD ( 19.02 )", "X-Spam-Score": "-2.0 (--)", "X-Spam-Report": "SpamAssassin version 3.4.0 on bombadil.infradead.org summary:\n\tContent analysis details: (-2.0 points)\n\tpts rule name description\n\t---- ----------------------\n\t--------------------------------------------------\n\t-0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/,\n\tno\n\ttrust [2607:f8b0:400e:c05:0:0:0:242 listed in] [list.dnswl.org]\n\t-0.0 SPF_PASS SPF: sender matches SPF record\n\t0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\n\tprovider (yszhou4tech[at]gmail.com)\n\t-1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1%\n\t[score: 0.0000]\n\t-0.1 DKIM_VALID Message has at least one valid DKIM or DK signature\n\t-0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from\n\tauthor's domain\n\t0.1 DKIM_SIGNED Message has a DKIM or DK signature,\n\tnot necessarily valid", "Subject": "[LEDE-DEV] [PATCH v3 1/2] tools: wrt400n: fix making factory images\n\twith kernel bigger than 1MB", "X-BeenThere": "lede-dev@lists.infradead.org", "X-Mailman-Version": "2.1.20", "Precedence": "list", "List-Id": "<lede-dev.lists.infradead.org>", "List-Unsubscribe": "<http://lists.infradead.org/mailman/options/lede-dev>,\n\t<mailto:lede-dev-request@lists.infradead.org?subject=unsubscribe>", "List-Archive": "<http://lists.infradead.org/pipermail/lede-dev/>", "List-Post": "<mailto:lede-dev@lists.infradead.org>", "List-Help": "<mailto:lede-dev-request@lists.infradead.org?subject=help>", "List-Subscribe": "<http://lists.infradead.org/mailman/listinfo/lede-dev>,\n\t<mailto:lede-dev-request@lists.infradead.org?subject=subscribe>", "Cc": "Yousong Zhou <yszhou4tech@gmail.com>, frank.horowitz@cornell.edu,\n\tlede-dev@lists.infradead.org", "MIME-Version": "1.0", "Content-Type": "text/plain; charset=\"us-ascii\"", "Content-Transfer-Encoding": "7bit", "Sender": "\"Lede-dev\" <lede-dev-bounces@lists.infradead.org>", "Errors-To": "lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org" }, "content": "Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>\n---\n tools/firmware-utils/src/wrt400n.c | 463 ++++++++++++++++---------------------\n 1 file changed, 197 insertions(+), 266 deletions(-)", "diff": "diff --git a/tools/firmware-utils/src/wrt400n.c b/tools/firmware-utils/src/wrt400n.c\nindex 1cf1deb..70d4c32 100644\n--- a/tools/firmware-utils/src/wrt400n.c\n+++ b/tools/firmware-utils/src/wrt400n.c\n@@ -6,110 +6,34 @@\n * from a kernel image file and root fs file\n *\n *\tAuthor: Sandeep Mistry\n+ *\tAuthor: Yousong Zhou\n+ *\t\t- remove size limit on kernel and rootfs part\n */\n #include <arpa/inet.h>\n #include <fcntl.h>\n #include <stdio.h>\n #include <stdint.h>\n+#include <stdbool.h>\n #include <string.h>\n+#include <errno.h>\n #include <sys/types.h>\n #include <sys/stat.h>\n #include <unistd.h>\n \n #include \"cyg_crc.h\"\n \n-// https://dev.openwrt.org/browser/trunk/target/linux/rdc-2.6/files/drivers/mtd/maps/rdc3210.c\n-static uint32_t crctab[257] =\n-{\n-\t0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,\n-\t0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,\n-\t0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,\n-\t0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,\n-\t0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,\n-\t0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,\n-\t0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,\n-\t0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,\n-\t0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,\n-\t0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,\n-\t0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,\n-\t0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,\n-\t0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,\n-\t0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,\n-\t0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,\n-\t0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,\n-\t0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,\n-\t0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,\n-\t0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,\n-\t0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,\n-\t0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,\n-\t0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,\n-\t0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,\n-\t0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,\n-\t0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,\n-\t0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,\n-\t0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,\n-\t0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,\n-\t0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,\n-\t0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,\n-\t0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,\n-\t0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,\n-\t0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,\n-\t0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,\n-\t0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,\n-\t0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,\n-\t0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,\n-\t0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,\n-\t0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,\n-\t0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,\n-\t0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,\n-\t0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,\n-\t0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,\n-\t0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,\n-\t0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,\n-\t0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,\n-\t0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,\n-\t0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,\n-\t0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,\n-\t0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,\n-\t0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,\n-\t0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,\n-\t0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,\n-\t0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,\n-\t0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,\n-\t0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,\n-\t0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,\n-\t0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,\n-\t0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,\n-\t0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,\n-\t0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,\n-\t0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,\n-\t0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,\n-\t0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,\n-\t0\n-};\n-\n-static uint32_t crc32(uint8_t* buf, uint32_t len)\n-{\n-\tregister int i;\n-\tuint32_t sum;\n-\tregister uint32_t s0;\n-\ts0 = ~0;\n-\tfor (i = 0; i < len; i++)\n-\t{\n-\t\ts0 = (s0 >> 8) ^ crctab[(uint8_t) (s0 & 0xFF) ^ buf[i]];\n-\t}\n-\tsum = ~s0;\n-\treturn sum;\n-}\n-\n-#define HEADERSIZE\t60\n-#define MAGIC\t\t\"GMTKRT400N\"\n-\n-// global variables\n-uint8_t kernelbuf[0x100000];\t// kernel - lzma - uImage\n-uint8_t rootfsbuf[0x2FFFC4];\t// root - squashfs\n-\n-uint8_t buf[0x400000];\t\t// buffer for image\n+// Prelimiary test shows that the OEM upgrade program only checks checksum of\n+// the whole firmware image, ignoring specific checksums of kernel and rootfs\n+// part. But we are computing crc32 sums of fixed length of data here in lieu\n+// of read_fw utility found in OEM SDK\n+#define CRC_SZ_KERNEL 0x100000\n+#define CRC_SZ_ROOTFS 0x2fffc4\n+// OEM firmware has size limit on \"linux\" and \"rootfs\" partition. That's also\n+// the limit for factory firmware images as fwupgrade utility in the OEM system\n+// needs to write kernel and rootfs to those two partitions (/dev/mtd2 and\n+// /dev/mtd3 to be exact)\n+#define SZ_MAX_KERNEL (0x180000 - 0x40000)\n+#define SZ_MAX_ROOTFS (0x7b0000 - 0x18000)\n \n \n // Header format:\n@@ -117,27 +41,6 @@ uint8_t buf[0x400000];\t\t// buffer for image\n //\t\tGPL Tarball: http://downloads.linksysbycisco.com/downloads/WRT400N_1.0.01.19_US.tar,0.gz\n //\t\tFile: WRT400N_1.0.01.19_US/FW_WRT400N_1.0.01.19_US_20081229/GTK/user/include/fw_upgrade.h\n //\n-//\n-//\t\tStruct:\n-//\t\t\ttypedef struct\n-//\t\t\t{\n-//\t\t\t\tUINT32\t\tchecksum;\t\t/* CRC32 */\n-//\t\t\t\tUINT8\t\t\tmagic[11];\t\t/* The value of GTIMG_MAGIC */\n-//\t\t\t\tUINT32\t\tkernel_length;\t\t/* The length of the kernel image */\n-//\t\t\t\t//UINT32\t\tkernel_entry_point;\t/* Kernel's entry point for RedBoot's information */\n-//\t\t\t\tUINT32\t\tkernel_upgrade_flag;\t/* Set to 1 if we need to upgrade the kernel parition of the Flash */\n-//\t\t\t\tUINT32\t\trootfs_length;\t\t/* The length of the rootfs image */\n-//\t\t\t\t//UINT32\t\trootfs_entry_point;\t/* Not in use */\n-//\t\t\t\tUINT32\t\trootfs_upgrade_flag;\t/* Set to 1 if we need to upgrade the rootfs parition of the Flash */\n-//\n-//\t\t\t\t// Add 3 items by Vic Yu, 2006-05/10\n-//\t\t\t\tUINT32\t\tkernel_checksum;\n-//\t\t\t\tUINT32\t\trootfs_checksum;\n-//\t\t\t\tUINT32\t\tfw_totalsize;\n-//\t\t\t\tUINT32\t\treserved[4];\n-//\t\t\t}imghdr_t , *pLinuxFWHeader_T;\n-//\n-//\n //\t\tDescription\n //\t\t\t- checksum: \t\t\tCRC32 of kernel and root fs, back to back\n //\t\t\t- magic:\t\t\t\tGMTKRT400N\n@@ -151,186 +54,214 @@ uint8_t buf[0x400000];\t\t// buffer for image\n //\t\t\t\t\t\t\t\t\tset to 0xFF upto 0x2FFFC4 bytes\t(total length)\n //\t\t\t- fw_totalsize:\t\t\ttotal firmware image file length (header length + kernel length + root fs length)\n //\t\t\t- reserved[4]:\t\t\treserved ??? - set to all 0xFF\n+struct imghdr_t\n+{\n+\tuint32_t\t\tchecksum;\t\t/* CRC32 */\n+\tuint8_t\t\t\tmagic[12];\t\t/* The value of GTIMG_MAGIC */\n+\tuint32_t\t\tkernel_length;\t\t/* The length of the kernel image */\n+\t//uint32_t\t\tkernel_entry_point;\t/* Kernel's entry point for RedBoot's information */\n+\tuint32_t\t\tkernel_upgrade_flag;\t/* Set to 1 if we need to upgrade the kernel parition of the Flash */\n+\tuint32_t\t\trootfs_length;\t\t/* The length of the rootfs image */\n+\t//uint32_t\t\trootfs_entry_point;\t/* Not in use */\n+\tuint32_t\t\trootfs_upgrade_flag;\t/* Set to 1 if we need to upgrade the rootfs parition of the Flash */\n+\n+\t// Add 3 items by Vic Yu, 2006-05/10\n+\tuint32_t\t\tkernel_checksum;\n+\tuint32_t\t\trootfs_checksum;\n+\tuint32_t\t\tfw_totalsize;\n+\tuint32_t\t\treserved[4];\n+};\n \n+struct file_info {\n+\tchar\t\t*filename;\n+\tint\t\t\tfilesize;\n+\tint\t\t\tfd;\n+\tuint32_t\tcrc32sum;\n+\tint\t\t\tcrc_sz;\n+};\n \n-\n-int main(int argc, char *argv[])\n+int get_file_info(struct file_info *fi, uint32_t *crc32_acc)\n {\n-\t// file descriptors ...\n-\tint \t\tkernelfd\t\t= -1;\n-\tint \t\trootfsfd\t\t\t= -1;\n-\tint\t\t\toutfd\t\t\t= -1;\n+\tint fd;\n+\tint filesize = 0;\n+\tuint32_t crc32sum = 0;\n+\tuint32_t _crc32_acc;\n+\tint crc_rem = fi->crc_sz;\n+\tint sz;\n+\tchar buf[1024];\n+\n+\tif (crc32_acc)\n+\t\t_crc32_acc = *crc32_acc;\n+\telse\n+\t\t_crc32_acc = 0;\n+\n+\tfd = open(fi->filename, O_RDONLY);\n+\tif(fd < 0) {\n+\t\tfprintf(stderr, \"error: opening '%s'\\n\", fi->filename);\n+\t\treturn -1;\n+\t}\n+\twhile (true) {\n+\t\tsz = read(fd, buf, sizeof(buf));\n+\t\tif (sz > 0) {\n+\t\t\tif (crc32_acc)\n+\t\t\t\t_crc32_acc = cyg_crc32_accumulate(_crc32_acc, buf, sz);\n+\t\t\tif (crc_rem > 0) {\n+\t\t\t\tif (crc_rem < sz) {\n+\t\t\t\t\tcrc32sum = cyg_crc32_accumulate(crc32sum, buf, crc_rem);\n+\t\t\t\t\tcrc_rem = 0;\n+\t\t\t\t} else {\n+\t\t\t\t\tcrc32sum = cyg_crc32_accumulate(crc32sum, buf, sz);\n+\t\t\t\t\tcrc_rem -= sz;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tfilesize += sz;\n+\t\t} else if (sz == 0) {\n+\t\t\tbreak;\n+\t\t} else {\n+\t\t\tfprintf(stderr, \"error read '%s'\", strerror(errno));\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n \n-\tchar*\t\tkernelfilename\t= NULL;\n-\tchar*\t\trootfsfilename\t= NULL;\n-\tchar*\t\toutputfilename\t= NULL;\n+\tif (crc_rem) {\n+\t\tmemset(buf, 0xff, sizeof(buf));\n+\t\twhile (crc_rem > 0) {\n+\t\t\tif (crc_rem > sizeof(buf)) {\n+\t\t\t\tcrc32sum = cyg_crc32_accumulate(crc32sum, buf, sizeof(buf));\n+\t\t\t\tcrc_rem -= sizeof(buf);\n+\t\t\t} else {\n+\t\t\t\tcrc32sum = cyg_crc32_accumulate(crc32sum, buf, crc_rem);\n+\t\t\t\tcrc_rem = 0;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n \n-\t// file sizes\n-\tuint32_t \tkernelsize \t\t= 0;\n-\tuint32_t \trootfssize \t\t= 0;\n-\tuint32_t \ttotalsize \t\t= 0;\n+\tfi->fd = fd;\n+\tfi->filesize = filesize;\n+\tfi->crc32sum = crc32sum;\n+\tif (crc32_acc)\n+\t\t*crc32_acc = _crc32_acc;\n+\treturn 0;\n+}\n \n-\t// header flags\n-\tuint32_t \tkernelflag \t\t= 0;\n-\tuint32_t \trootfsflag \t\t= 0;\n+int copy_file(int fromfd, int tofd)\n+{\n+\tint szr, szw;\n+\tint buf[4096];\n \n-\t// checksums\n-\tuint32_t \tkernelchecksum \t= 0;\n-\tuint32_t \trootfschecksum \t= 0;\n-\tuint32_t\tcrc\t\t\t\t= 0;\n+\tif (lseek(fromfd, SEEK_SET, 0) < 0) {\n+\t\tfprintf(stderr, \"lseek: %s\\n\", strerror(errno));\n+\t\treturn -1;\n+\t}\n+\twhile (true) {\n+\t\tszr = read(fromfd, buf, sizeof(buf));\n+\t\tif (szr > 0) {\n+\t\t\tszw = write(tofd, buf, szr);\n+\t\t\tif (szw != szr) {\n+\t\t\t\tfprintf(stderr, \"copy_file: error writing %d bytes: %s\\n\", szr, strerror(errno));\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\t\t} else if (szr == 0) {\n+\t\t\tbreak;\n+\t\t} else {\n+\t\t\tfprintf(stderr, \"copy_file: error reading: %s\\n\", strerror(errno));\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n \n-\tif(argc != 4)\n-\t{\n+int main(int argc, char *argv[])\n+{\n+\tstruct file_info kernel_fi;\n+\tstruct file_info rootfs_fi;\n+\tstruct file_info output_fi;\n+\tstruct imghdr_t ih;\n+\tint fd, sz;\n+\tint ret = -1;\n+\n+\tif(argc != 4) {\n \t\tprintf(\"Usage:\\n\\t%s <kernel file> <rootfs file> <output file>\\n\", argv[0]);\n \t\treturn 1;\n \t}\n \n-\tkernelfilename = argv[1];\n-\trootfsfilename = argv[2];\n-\toutputfilename = argv[3];\n+\tkernel_fi.crc_sz = CRC_SZ_KERNEL;\n+\trootfs_fi.crc_sz = CRC_SZ_ROOTFS;\n \n-\t// Fill the kernel, rootfs, main buffer\n-\tmemset(kernelbuf, 0xFF, sizeof(kernelbuf));\n-\tmemset(rootfsbuf, 0xFF, sizeof(rootfsbuf));\n-\tmemset(buf, 0xFF, sizeof(buf));\n+\tkernel_fi.fd = -1;\n+\trootfs_fi.fd = -1;\n+\toutput_fi.fd = -1;\n \n-\t// open the kernel ..\n-\tkernelfd = open(kernelfilename, O_RDONLY);\n+\tkernel_fi.filename = argv[1];\n+\trootfs_fi.filename = argv[2];\n+\toutput_fi.filename = argv[3];\n \n-\tif(kernelfd == -1)\n-\t{\n-\t\tprintf(\"Error: opening '%s'\\n\", kernelfilename);\n-\t\tgoto done;\n-\t}\n-\n-\t// read in the kernel ...\n-\tkernelsize = read(kernelfd, kernelbuf, sizeof(kernelbuf));\n+\tkernel_fi.crc32sum = 0;\n+\trootfs_fi.crc32sum = 0;\n+\toutput_fi.crc32sum = 0xffffffff;\n \n-\tif(kernelsize == -1)\n-\t{\n-\t\tprintf(\"Error: reading '%s'\\n\", kernelfilename);\n+\tif (get_file_info(&kernel_fi, &output_fi.crc32sum) < 0)\n \t\tgoto done;\n-\t}\n-\n-\t// calculate the kernel checksum ...\n-\tkernelchecksum = cyg_crc32_accumulate(0, kernelbuf, sizeof(kernelbuf));\n+\tif (get_file_info(&rootfs_fi, &output_fi.crc32sum) < 0)\n+\t\tgoto done;\n+\toutput_fi.crc32sum = ~output_fi.crc32sum;\n \n \t// print out stats\n-\tprintf(\"%s: size %d (0x%x), crc32 = 0x%x\\n\", kernelfilename, kernelsize, kernelsize, kernelchecksum);\n-\n-\n-\t// open the root fs ..\n-\trootfsfd = open(rootfsfilename, O_RDONLY);\n-\n-\tif(rootfsfd == -1)\n-\t{\n-\t\tprintf(\"Error: opening '%s'\\n\", rootfsfilename);\n+\tfprintf(stderr, \"%s: size %d (0x%x), crc32 = 0x%x\\n\",\n+\t\tkernel_fi.filename, kernel_fi.filesize, kernel_fi.filesize, kernel_fi.crc32sum);\n+\tfprintf(stderr, \"%s: size %d (0x%x), crc32 = 0x%x\\n\",\n+\t\trootfs_fi.filename, rootfs_fi.filesize, rootfs_fi.filesize, rootfs_fi.crc32sum);\n+\tif (kernel_fi.filesize > SZ_MAX_KERNEL) {\n+\t\tfprintf(stderr, \"%s: filesize exceeds 0x%x limit\\n\", SZ_MAX_KERNEL);\n \t\tgoto done;\n \t}\n-\n-\t// read in the root fs ..\n-\trootfssize = read(rootfsfd, rootfsbuf, sizeof(rootfsbuf));\n-\n-\tif(rootfssize == -1)\n-\t{\n-\t\tprintf(\"Error: reading '%s'\\n\", rootfsfilename);\n+\tif (rootfs_fi.filesize > SZ_MAX_ROOTFS) {\n+\t\tfprintf(stderr, \"%s: filesize exceeds 0x%x limit\\n\", SZ_MAX_ROOTFS);\n \t\tgoto done;\n \t}\n \n-\t// calculate the root fs checksum ...\n-\trootfschecksum = cyg_crc32_accumulate(0, rootfsbuf, sizeof(rootfsbuf));\n-\n-\t// print out stats\n-\tprintf(\"%s: size %d (0x%x), crc32 = 0x%x\\n\", rootfsfilename, rootfssize, rootfssize, rootfschecksum);\n-\n-\n \t// now for the header ...\n-\n-\ttotalsize = HEADERSIZE;\n-\n-\t// copy over kernel\n-\tmemcpy(buf + totalsize, kernelbuf, kernelsize);\n-\ttotalsize += kernelsize;\n-\n-\t// copy over root fs\n-\tmemcpy(buf + totalsize, rootfsbuf, rootfssize);\n-\ttotalsize += rootfssize;\n-\n-\t// calculate crc\n-\tcrc = crc32(buf + HEADERSIZE, totalsize - HEADERSIZE);\n-\n-\t// print some stats out\n-\tprintf(\"crc = 0x%x, total size = %d (0x%x)\\n\", crc, totalsize, totalsize);\n-\n-\t// copy crc into header\n-\tcrc = htonl(crc);\n-\tmemcpy(buf, &crc, sizeof(crc));\n-\n-\t// copy over magic\n-\tstrcpy((char *)buf + 4, MAGIC);\n-\n-\t// copy over kernel size\n-\tkernelsize = htonl(kernelsize);\n-\tmemcpy(buf + 16, &kernelsize, sizeof(kernelsize));\n-\n-\t// copy over kernal flag\n-\tkernelflag = htonl(0x1);\n-\tmemcpy(buf + 20, &kernelflag, sizeof(kernelflag));\n-\n-\t// copy over root fs size\n-\trootfssize = htonl(rootfssize);\n-\tmemcpy(buf + 24, &rootfssize, sizeof(rootfssize));\n-\n-\t// copy over root fs flag\n-\trootfsflag = htonl(0x1);\n-\tmemcpy(buf + 28, &rootfsflag, sizeof(rootfsflag));\n-\n-\t// copy over kernel check sum\n-\tkernelchecksum = htonl(kernelchecksum);\n-\tmemcpy(buf + 32, &kernelchecksum, sizeof(kernelchecksum));\n-\n-\t// copy over root fs checksum\n-\trootfschecksum = htonl(rootfschecksum);\n-\tmemcpy(buf + 36, &rootfschecksum, sizeof(rootfschecksum));\n-\n-\t// copy over total size\n-\ttotalsize = htonl(totalsize);\n-\tmemcpy(buf + 40, &totalsize, sizeof(totalsize));\n-\n-\t// undo the htonl (for write)\n-\ttotalsize = htonl(totalsize);\n-\n-\n-\t// write out the file from the buffer\n-\toutfd = open(outputfilename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);\n-\n-\tif(outfd == -1)\n-\t{\n-\t\tprintf(\"ERROR: opening '%s' for write\\n\", outputfilename);\n-\t}\n-\n-\twrite(outfd, buf, totalsize);\n-\n-done:\n-\t// close open fd's\n-\n-\tif(kernelfd != -1)\n-\t{\n-\t\tclose(kernelfd);\n-\t\tkernelfd = -1;\n+\tmemset(&ih, 0xff, sizeof(ih));\n+\tstrcpy(ih.magic, \"GMTKRT400N\");\n+\tih.checksum = htonl(output_fi.crc32sum);\n+\tih.kernel_length = htonl(kernel_fi.filesize);\n+\tih.rootfs_length = htonl(rootfs_fi.filesize);\n+\tih.kernel_upgrade_flag = htonl(0x1);\n+\tih.rootfs_upgrade_flag = htonl(0x1);\n+\tih.kernel_checksum = htonl(kernel_fi.crc32sum);\n+\tih.rootfs_checksum = htonl(rootfs_fi.crc32sum);\n+\tih.fw_totalsize = htonl(kernel_fi.filesize + rootfs_fi.filesize + sizeof(ih));\n+\n+\toutput_fi.fd = open(output_fi.filename, O_WRONLY | O_CREAT | O_TRUNC,\n+\t\t\tS_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);\n+\tsz = write(output_fi.fd, &ih, sizeof(ih));\n+\tif (sz != sizeof(ih)) {\n+\t\tfprintf(stderr, \"error writing imghdr to output: %s\\n\", strerror(errno));\n+\t\tgoto done;\n \t}\n-\n-\tif(rootfsfd != -1)\n-\t{\n-\t\tclose(rootfsfd);\n-\t\trootfsfd = -1;\n+\tif (copy_file(kernel_fi.fd, output_fi.fd)) {\n+\t\tfprintf(stderr, \"error copying %s to %s\\n\", kernel_fi.filename, output_fi.filename);\n+\t\tgoto done;\n \t}\n-\n-\tif(outfd != -1)\n-\t{\n-\t\tclose(outfd);\n-\t\toutfd = -1;\n+\tif (copy_file(rootfs_fi.fd, output_fi.fd)) {\n+\t\tfprintf(stderr, \"error copying %s to %s\\n\", rootfs_fi.filename, output_fi.filename);\n+\t\tgoto done;\n \t}\n+\t// print some stats out\n+\tfprintf(stderr, \"crc = 0x%x, total size = %d (0x%x)\\n\",\n+\t\t\toutput_fi.crc32sum, ntohl(ih.fw_totalsize), ntohl(ih.fw_totalsize));\n+\tret = 0;\n \n-\treturn 0;\n+done:\n+#define CLOSE_FI_FD(fi)\tdo {\t\\\n+\tif ((fi).fd >= 0) {\t\t\t\\\n+\t\tclose((fi).fd);\t\t\t\\\n+\t}\t\t\t\t\t\t\t\\\n+} while (0)\n+\n+\tCLOSE_FI_FD(kernel_fi);\n+\tCLOSE_FI_FD(rootfs_fi);\n+\tCLOSE_FI_FD(output_fi);\n+\treturn ret;\n }\n", "prefixes": [ "LEDE-DEV", "v3", "1/2" ] }