[OpenWrt-Devel] [PATCH 3/4] firmware-utils: kernel image generator for TP-Link RE450

Tal Keren kooolk at gmail.com
Tue Jan 19 15:51:59 EST 2016


On 19/01/2016 11:30, John Crispin wrote:
> can this not be merged into one of the other tplink tools ?
>
>

That is what I initially did, add a flag for this case. But then I had to disable about 3/4 of the original code with if/else for this case. It doesn't use most of the functionally that mktplinkfw provides (It doesn't search for and use the flash layout, it doesn't write the jffs section, it doesn't write most of the fields in the header, it ignores some of the flags because they are irrelevant for it).

So I think that creating a new and (very) stripped down version of it is simpler.

For reference, this is my original patch. What do you think that is better?


--- a/tools/firmware-utils/src/mktplinkfw.c
+++ b/tools/firmware-utils/src/mktplinkfw.c
@@ -154,6 +154,7 @@ static uint32_t rootfs_ofs = 0;
 static uint32_t rootfs_align;
 static struct file_info boot_info;
 static int combined;
+static int kernel_only;
 static int strip_padding;
 static int ignore_size;
 static int add_jffs2_eof;
@@ -518,6 +519,7 @@ static void usage(int status)
 "Options:\n"
 "  -B <board>      create image for the board specified with <board>\n"
 "  -c              use combined kernel image\n"
+"  -K              make image with only kernel\n"
 "  -E <ep>         overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
 "  -L <la>         overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
 "  -H <hwid>       use hardware id specified with <hwid>\n"
@@ -613,54 +615,61 @@ static int check_options(void)
         return -1;
     }
 
-    if (board_id == NULL && opt_hw_id == NULL) {
-        ERR("either board or hardware id must be specified");
-        return -1;
-    }
-
-    if (board_id) {
-        board = find_board(board_id);
-        if (board == NULL) {
-            ERR("unknown/unsupported board id \"%s\"", board_id);
+    if (kernel_only) {
+        if (!kernel_la || !kernel_ep) {
+            ERR("kernel loading address and entry point must be specified");
             return -1;
         }
-        if (layout_id == NULL)
-            layout_id = board->layout_id;
-
-        hw_id = board->hw_id;
-        hw_rev = board->hw_rev;
     } else {
-        if (layout_id == NULL) {
-            ERR("flash layout is not specified");
+        if (board_id == NULL && opt_hw_id == NULL) {
+            ERR("either board or hardware id must be specified");
             return -1;
         }
-        hw_id = strtoul(opt_hw_id, NULL, 0);
 
-        if (opt_hw_rev)
-            hw_rev = strtoul(opt_hw_rev, NULL, 0);
-        else
-            hw_rev = 1;
-    }
+        if (board_id) {
+            board = find_board(board_id);
+            if (board == NULL) {
+                ERR("unknown/unsupported board id \"%s\"", board_id);
+                return -1;
+            }
+            if (layout_id == NULL)
+                layout_id = board->layout_id;
 
-    layout = find_layout(layout_id);
-    if (layout == NULL) {
-        ERR("unknown flash layout \"%s\"", layout_id);
-        return -1;
-    }
+            hw_id = board->hw_id;
+            hw_rev = board->hw_rev;
+        } else {
+            if (layout_id == NULL) {
+                ERR("flash layout is not specified");
+                return -1;
+            }
+            hw_id = strtoul(opt_hw_id, NULL, 0);
 
-    if (!kernel_la)
-        kernel_la = layout->kernel_la;
-    if (!kernel_ep)
-        kernel_ep = layout->kernel_ep;
-    if (!rootfs_ofs)
-        rootfs_ofs = layout->rootfs_ofs;
+            if (opt_hw_rev)
+                hw_rev = strtoul(opt_hw_rev, NULL, 0);
+            else
+                hw_rev = 1;
+        }
 
-    if (reserved_space > layout->fw_max_len) {
-        ERR("reserved space is not valid");
-        return -1;
-    }
+        layout = find_layout(layout_id);
+        if (layout == NULL) {
+            ERR("unknown flash layout \"%s\"", layout_id);
+            return -1;
+        }
 
-    fw_max_len = layout->fw_max_len - reserved_space;
+        if (!kernel_la)
+            kernel_la = layout->kernel_la;
+        if (!kernel_ep)
+            kernel_ep = layout->kernel_ep;
+        if (!rootfs_ofs)
+            rootfs_ofs = layout->rootfs_ofs;
+
+        if (reserved_space > layout->fw_max_len) {
+            ERR("reserved space is not valid");
+            return -1;
+        }
+
+        fw_max_len = layout->fw_max_len - reserved_space;
+    }
 
     if (kernel_info.file_name == NULL) {
         ERR("no kernel image specified");
@@ -673,50 +682,52 @@ static int check_options(void)
 
     kernel_len = kernel_info.file_size;
 
-    if (combined) {
-        exceed_bytes = kernel_info.file_size - (fw_max_len - sizeof(struct fw_header));
-        if (exceed_bytes > 0) {
-            if (!ignore_size) {
-                ERR("kernel image is too big by %i bytes", exceed_bytes);
-                return -1;
-            }
-            layout->fw_max_len = sizeof(struct fw_header) +
-                         kernel_info.file_size +
-                         reserved_space;
-        }
-    } else {
-        if (rootfs_info.file_name == NULL) {
-            ERR("no rootfs image specified");
-            return -1;
-        }
-
-        ret = get_file_stat(&rootfs_info);
-        if (ret)
-            return ret;
-
-        if (rootfs_align) {
-            kernel_len += sizeof(struct fw_header);
-            kernel_len = ALIGN(kernel_len, rootfs_align);
-            kernel_len -= sizeof(struct fw_header);
-
-            DBG("kernel length aligned to %u", kernel_len);
-
-            exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header));
+    if (!kernel_only) {
+        if (combined) {
+            exceed_bytes = kernel_info.file_size - (fw_max_len - sizeof(struct fw_header));
             if (exceed_bytes > 0) {
-                ERR("images are too big by %i bytes", exceed_bytes);
-                return -1;
+                if (!ignore_size) {
+                    ERR("kernel image is too big by %i bytes", exceed_bytes);
+                    return -1;
+                }
+                layout->fw_max_len = sizeof(struct fw_header) +
+                             kernel_info.file_size +
+                             reserved_space;
             }
         } else {
-            exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
-            if (exceed_bytes > 0) {
-                ERR("kernel image is too big by %i bytes", exceed_bytes);
+            if (rootfs_info.file_name == NULL) {
+                ERR("no rootfs image specified");
                 return -1;
             }
 
-            exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
-            if (exceed_bytes > 0) {
-                ERR("rootfs image is too big by %i bytes", exceed_bytes);
-                return -1;
+            ret = get_file_stat(&rootfs_info);
+            if (ret)
+                return ret;
+
+            if (rootfs_align) {
+                kernel_len += sizeof(struct fw_header);
+                kernel_len = ALIGN(kernel_len, rootfs_align);
+                kernel_len -= sizeof(struct fw_header);
+
+                DBG("kernel length aligned to %u", kernel_len);
+
+                exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header));
+                if (exceed_bytes > 0) {
+                    ERR("images are too big by %i bytes", exceed_bytes);
+                    return -1;
+                }
+            } else {
+                exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
+                if (exceed_bytes > 0) {
+                    ERR("kernel image is too big by %i bytes", exceed_bytes);
+                    return -1;
+                }
+
+                exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
+                if (exceed_bytes > 0) {
+                    ERR("rootfs image is too big by %i bytes", exceed_bytes);
+                    return -1;
+                }
             }
         }
     }
@@ -753,29 +764,35 @@ static void fill_header(char *buf, int len)
     hdr->version = htonl(hdr_ver);
     strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
     strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
-    hdr->hw_id = htonl(hw_id);
-    hdr->hw_rev = htonl(hw_rev);
+    if (!kernel_only) {
+        hdr->hw_id = htonl(hw_id);
+        hdr->hw_rev = htonl(hw_rev);
 
-    if (boot_info.file_size == 0)
-        memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
-    else
-        memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
+        if (boot_info.file_size == 0)
+            memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
+        else
+            memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
+    }
 
     hdr->kernel_la = htonl(kernel_la);
     hdr->kernel_ep = htonl(kernel_ep);
-    hdr->fw_length = htonl(layout->fw_max_len);
+    if (!kernel_only) {
+        hdr->fw_length = htonl(layout->fw_max_len);
+    }
     hdr->kernel_ofs = htonl(sizeof(struct fw_header));
     hdr->kernel_len = htonl(kernel_len);
-    if (!combined) {
+    if (!combined && !kernel_only) {
         hdr->rootfs_ofs = htonl(rootfs_ofs);
         hdr->rootfs_len = htonl(rootfs_info.file_size);
     }
 
-    hdr->ver_hi = htons(fw_ver_hi);
-    hdr->ver_mid = htons(fw_ver_mid);
-    hdr->ver_lo = htons(fw_ver_lo);
+    if (!kernel_only) {
+        hdr->ver_hi = htons(fw_ver_hi);
+        hdr->ver_mid = htons(fw_ver_mid);
+        hdr->ver_lo = htons(fw_ver_lo);
 
-    get_md5(buf, len, hdr->md5sum1);
+        get_md5(buf, len, hdr->md5sum1);
+    }
 }
 
 static int pad_jffs2(char *buf, int currlen)
@@ -852,7 +869,12 @@ static int build_fw(void)
     int ret = EXIT_FAILURE;
     int writelen = 0;
 
-    buflen = layout->fw_max_len;
+    if (kernel_only) {
+        buflen = sizeof(struct fw_header) + kernel_len;
+        buflen = ALIGN(buflen, 0x4);
+    } else {
+        buflen = layout->fw_max_len;
+    }
 
     buf = malloc(buflen);
     if (!buf) {
@@ -868,7 +890,7 @@ static int build_fw(void)
 
     writelen = sizeof(struct fw_header) + kernel_len;
 
-    if (!combined) {
+    if (!combined && !kernel_only) {
         if (rootfs_align)
             p = buf + writelen;
         else
@@ -1127,7 +1149,7 @@ int main(int argc, char *argv[])
     while ( 1 ) {
         int c;
 
-        c = getopt(argc, argv, "a:B:H:E:F:L:m:V:N:W:ci:k:r:R:o:xX:hsSjv:");
+        c = getopt(argc, argv, "a:B:H:E:F:L:m:V:N:W:cKi:k:r:R:o:xX:hsSjv:");
         if (c == -1)
             break;
 
@@ -1168,6 +1190,9 @@ int main(int argc, char *argv[])
         case 'c':
             combined++;
             break;
+        case 'K':
+            kernel_only++;
+            break;
         case 'k':
             kernel_info.file_name = optarg;
             break;
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list