Ticket #4943: imagetag-kamikaze-from-stock-firmware.patch

File imagetag-kamikaze-from-stock-firmware.patch, 9.8 KB (added by cshore@…, 8 years ago)
  • target/linux/brcm63xx/files/include/asm-mips/mach-bcm63xx/bcm_tag.h

    diff --git a/target/linux/brcm63xx/files/include/asm-mips/mach-bcm63xx/bcm_tag.h b/target/linux/brcm63xx/files/include/asm-mips/mach-bcm63xx/bcm_tag.h
    index efc4f02..0de957c 100644
    a b struct bcm_tag { 
    1414        unsigned char totalLength[IMAGE_LEN];           //Total length of image 
    1515        unsigned char cfeAddress[ADDRESS_LEN];  // Address in memory of CFE 
    1616        unsigned char cfeLength[IMAGE_LEN];             // Size of CFE 
    17         unsigned char rootAddress[ADDRESS_LEN];         // Address in memory of rootfs 
    18         unsigned char rootLength[IMAGE_LEN];            // Size of rootfs 
     17        unsigned char cfeRootAddress[ADDRESS_LEN];         // Address in memory of rootfs (for broadcom; for wrt this is only used for flashing the image and is offset of the image) 
     18        unsigned char cfeRootLength[IMAGE_LEN];            // Size of rootfs 
    1919        unsigned char kernelAddress[ADDRESS_LEN];       // Address in memory of kernel 
    2020        unsigned char kernelLength[IMAGE_LEN];  // Size of kernel 
    2121        unsigned char dualImage[2];                             // Unused at present 
    2222        unsigned char inactiveFlag[2];                  // Unused at present 
    23         unsigned char reserved1[74];                            // Reserved area not in use 
     23        unsigned char reserved1[52];                            // Reserved area not in use 
     24        unsigned char rootAddress[ADDRESS_LEN];         // Address in memory of rootfs 
     25        unsigned char rootLength[IMAGE_LEN];            // Size of rootfs 
     26 
    2427        unsigned char imageCRC[4];                              // CRC32 of images 
    2528        unsigned char reserved2[16];                            // Unused at present 
    2629        unsigned char headerCRC[4];                             // CRC32 of header excluding tagVersion 
  • target/linux/brcm63xx/image/Makefile

    diff --git a/target/linux/brcm63xx/image/Makefile b/target/linux/brcm63xx/image/Makefile
    index 8714096..eb7edca 100644
    a b define trxalign/squashfs 
    2828endef 
    2929 
    3030define Image/Build/CFE 
    31         # Generate the tagged image 
     31        # Generate the tagged image (CFE) 
    3232        $(STAGING_DIR_HOST)/bin/imagetag -i $(KDIR)/vmlinux.lzma.cfe -f $(KDIR)/root.$(1) \ 
    3333                -o $(BIN_DIR)/openwrt-$(2)-$(1)-cfe.bin \ 
    3434                -b $(2) -c $(3) -e $(LOADADDR) -l $(LOADADDR) 
    3535#               -b $(2) -c $(3) -e $(KERNEL_ENTRY) -l $(LOADADDR) 
    3636 
    37         $(call prepare_generic_squashfs,$(BIN_DIR)/openwrt-$(2)-$(1)-cfe.bin) 
    3837endef 
    3938 
    4039define Image/Build/CFEAGPF 
    41         # Generate the tagged image 
     40        # Generate the tagged image (CFEAGPF) 
    4241        $(STAGING_DIR_HOST)/bin/imagetag -i $(KDIR)/vmlinux.lzma.cfe -f $(KDIR)/root.$(1) \ 
    4342                -o $(BIN_DIR)/openwrt-$(2)-$(1)-cfe.bin \ 
    4443                -b $(2) -c $(3) -e $(LOADADDR) -l $(LOADADDR) \ 
    define Image/Build/RedBoot 
    5554endef 
    5655 
    5756define Image/Build/CFEOLD 
     57        # Generate the tagged image (CFEOLD) 
    5858        $(TOPDIR)/scripts/brcmImage.pl -t -p    \ 
    5959                -b $(2) -c $(3)                 \ 
    6060                -k $(KDIR)/vmlinux.lzma.cfe     \ 
  • tools/firmware-utils/src/imagetag.c

    diff --git a/tools/firmware-utils/src/imagetag.c b/tools/firmware-utils/src/imagetag.c
    index 00dad29..16ad3d7 100644
    a b  
    2323#define DEFAULT_FW_OFFSET               0x10000 
    2424#define DEFAULT_FLASH_START             0xBFC00000 
    2525#define DEFAULT_FLASH_BS                (64 * 1024) 
     26#define DEADCODE                        0xDEADC0DE 
     27#define DEADCODE_LEN                    4 
    2628 
    2729/* Kernel header */ 
    2830struct kernelhdr { 
    struct imagetag { 
    4749        uint8_t                 bigendian[2];   /*  60 -  61: "1" for big endian, "0" for little endian */ 
    4850        uint8_t                 imagelen[10];   /*  62 -  71: The length of all data that follows */ 
    4951        struct imagecomp        cfe;            /*  72 -  93: The offset and length of CFE */ 
    50         struct imagecomp        rootfs;         /*  94 - 115: The offset and length of the root file system */ 
     52        struct imagecomp        rootfs;         /*  94 - 115: The offset of the start of the image and length of the root filesystem; for Broadcom firmware images this is the rootfs but causes flashing problems for WRT hence the addition of wrtrootfs */ 
    5153        struct imagecomp        kernel;         /* 116 - 137: The offset and length of the kernel */ 
    5254        uint8_t                 dualimage[2];   /* 138 - 139: use "0" here */ 
    5355        uint8_t                 inactive[2];    /* 140 - 141: use "0" here */ 
    54         uint8_t                 reserved1[74];  /* 142 - 215: reserved */ 
     56        uint8_t                 reserved1[52];  /* 142 - 193: reserved */ 
     57        struct imagecomp        wrtrootfs;      /* 193 - 215: WRT: real offset and length of the root file system */ 
    5558        uint32_t                imagecrc;       /* 216 - 219: crc of the images (net byte order) */ 
    5659        uint8_t                 reserved2[16];  /* 220 - 235: reserved */ 
    5760        uint32_t                headercrc;      /* 236 - 239: crc starting from sig1 until headercrc (net byte order) */ 
    size_t getlen(FILE *fp) 
    116119        return retval; 
    117120} 
    118121 
     122uint32_t writepadding(FILE *padfile, size_t size, uint32_t crc) { 
     123  size_t paddingsize, padcount; 
     124  uint8_t padbuf[1024]; 
     125   
     126  for (padcount = 0; padcount < 1024; padcount++) { 
     127    padbuf[padcount] = 0; 
     128  }     
     129 
     130  paddingsize = 0; 
     131 
     132  while (paddingsize < size) { 
     133    if ((size - paddingsize) > 1024) { 
     134      padcount = 1024; 
     135    } else { 
     136      padcount = size - paddingsize;  
     137    } 
     138    fwrite(padbuf, sizeof(uint8_t), padcount, padfile); 
     139     
     140    paddingsize += padcount; 
     141    crc = crc32(crc, padbuf, padcount); 
     142  } 
     143  return crc; 
     144} 
     145 
    119146int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    120147            const char *boardid, const char *chipid, const uint32_t fwaddr, 
    121148            const uint32_t loadaddr, const uint32_t entry, 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    124151        struct imagetag tag; 
    125152        struct kernelhdr khdr; 
    126153        FILE *kernelfile = NULL, *rootfsfile = NULL, *binfile; 
    127         size_t kerneloff, kernellen, rootfsoff, rootfslen, read; 
     154        size_t kerneloff, kernellen, rootfsoff, rootfslen, read, cferootfsoff, cferootfslen, imagelen, imageoff, rootfspaddingsize, origrootfslen; 
    128155        uint8_t readbuf[1024]; 
    129156        uint32_t crc; 
     157        const uint32_t deadcode = htonl(DEADCODE); 
    130158 
    131159        memset(&tag, 0, sizeof(struct imagetag)); 
    132160 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    154182                fprintf(stderr, "Unable to open output file \"%s\"\n", bin); 
    155183                return 1; 
    156184        } 
     185        /* Initialize CRC */ 
     186        crc = IMAGETAG_CRC_START; 
     187 
     188        /* Initialize image length calculations */ 
     189        imagelen = 0; 
     190        cferootfslen = 0; 
    157191 
    158192        /* Build the kernel address and length (doesn't need to be aligned, read only) */ 
    159193        kerneloff = fwaddr + sizeof(tag); 
     194        imageoff = kerneloff; 
     195        cferootfsoff = imageoff; 
    160196        kernellen = getlen(kernelfile); 
    161197 
    162198        /* Build the kernel header */ 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    167203        /* Increase the kernel size by the header size */ 
    168204        kernellen += sizeof(khdr); 
    169205 
     206        imagelen += kernellen; 
     207 
    170208        /* Build the rootfs address and length (start and end do need to be aligned on flash erase block boundaries */ 
    171209        rootfsoff = kerneloff + kernellen; 
    172210        rootfsoff = (rootfsoff % flash_bs) > 0 ? (((rootfsoff / flash_bs) + 1) * flash_bs) : rootfsoff; 
    173         rootfslen = getlen(rootfsfile); 
     211        origrootfslen = getlen(rootfsfile); 
     212        rootfslen = origrootfslen; 
    174213        rootfslen = (rootfslen % flash_bs) > 0 ? (((rootfslen / flash_bs) + 1) * flash_bs) : rootfslen; 
     214        cferootfslen = rootfslen; 
    175215 
    176216        /* Seek to the start of the kernel */ 
    177217        fseek(binfile, kerneloff - fwaddr, SEEK_SET); 
    178218 
    179219        /* Write the kernel header */ 
    180         crc = crc32(IMAGETAG_CRC_START, (uint8_t*)&khdr, sizeof(khdr)); 
     220        crc = crc32(crc, (uint8_t*)&khdr, sizeof(khdr)); 
    181221        fwrite(&khdr, sizeof(khdr), 1, binfile); 
    182222 
    183223        /* Write the kernel */ 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    187227                fwrite(readbuf, sizeof(uint8_t), read, binfile); 
    188228        } 
    189229 
     230        /* Write padding from end of kernel to start of RootFS needed so that 
     231         * RootFS starts on and erase boundary, and calculate its CRC  
     232         */ 
     233        rootfspaddingsize = rootfsoff - (kerneloff + kernellen ); 
     234        imagelen += rootfspaddingsize; 
     235        /* FIXME: Don't use this CRC for Alice Gate */ 
     236        crc = writepadding(binfile, rootfspaddingsize, crc); 
     237  
    190238        /* Write the RootFS */ 
    191         fseek(binfile, rootfsoff - fwaddr, SEEK_SET); 
    192239        while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) { 
    193240                read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile); 
    194                 //crc = crc32(crc, readbuf, read); 
     241                /* FIXME: Don't do this for Alice Gate */ 
     242                crc = crc32(crc, readbuf, read); 
    195243                fwrite(readbuf, sizeof(uint8_t), read, binfile); 
    196244        } 
     245        imagelen += origrootfslen; 
     246  
     247        /* Write the ending padding and end-of-filesystem marker for RootFS so 
     248         * that RootFS ends on an erase boundary, and calculate its CRC  
     249         */ 
     250  
     251        /* FIXME: Don't use the CRC for Alice Gate */ 
     252        crc = writepadding(binfile, rootfslen - origrootfslen, crc); 
     253        imagelen += rootfslen - origrootfslen; 
     254        //      cferootfslen += rootfslen - origrootfslen; 
     255  
     256        /* Write end-of-filesystem marker */ 
     257        /* FIXME: Don't use this CRC for Alice Gate */ 
     258        crc = crc32(crc, (uint8_t*)&deadcode, DEADCODE_LEN); 
     259        fwrite((uint8_t*)&deadcode, sizeof(uint8_t), DEADCODE_LEN, binfile); 
     260        imagelen += DEADCODE_LEN; 
     261        cferootfslen += DEADCODE_LEN; 
    197262 
    198263        /* Close the files */ 
    199264        fclose(kernelfile); 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    206271        strcpy(tag.chipid, chipid); 
    207272        strcpy(tag.boardid, boardid); 
    208273        strcpy(tag.bigendian, "1"); 
    209         sprintf(tag.imagelen, "%lu", kernellen + rootfslen); 
     274        sprintf(tag.imagelen, "%lu", imagelen); 
    210275 
    211276        /* We don't include CFE */ 
    212277        strcpy(tag.cfe.address, "0"); 
    int tagfile(const char *kernel, const char *rootfs, const char *bin, 
    218283        } 
    219284 
    220285        if (rootfsfile) { 
    221                 sprintf(tag.rootfs.address, "%lu", rootfsoff); 
    222                 sprintf(tag.rootfs.len, "%lu", rootfslen); 
     286                sprintf(tag.rootfs.address, "%lu", cferootfsoff); 
     287                sprintf(tag.rootfs.len, "%lu", cferootfslen); 
     288                sprintf(tag.wrtrootfs.address, "%lu", rootfsoff); 
     289                sprintf(tag.wrtrootfs.len, "%lu", rootfslen); 
    223290        } 
    224291 
    225292        tag.imagecrc = htonl(crc);