void jffs_swap_inode(struct jffs_raw_inode *i) { i->magic = LONG_SWAP(i->magic); i->ino = LONG_SWAP(i->ino); i->pino = LONG_SWAP(i->pino); i->version = LONG_SWAP(i->version); i->mode = LONG_SWAP(i->mode); i->uid = WORD_SWAP(i->uid); i->gid = WORD_SWAP(i->gid); i->atime = LONG_SWAP(i->atime); i->mtime = LONG_SWAP(i->mtime); i->ctime = LONG_SWAP(i->ctime); i->offset = LONG_SWAP(i->offset); i->dsize = LONG_SWAP(i->dsize); i->rsize = LONG_SWAP(i->rsize); i->nsize = BYTE_SWAP(i->nsize); i->nlink = BYTE_SWAP(i->nlink); #if 0 /* damn bit fields, lucky its a byte */ i->spare = BYTE_SWAP(i->spare : 6); i->rename = BYTE_SWAP(i->rename : 1); i->deleted = BYTE_SWAP(i->deleted : 1); #endif i->accurate = BYTE_SWAP(i->accurate); i->dchksum = LONG_SWAP(i->dchksum); i->nchksum = WORD_SWAP(i->nchksum); i->chksum = WORD_SWAP(i->chksum); }
/* This function writes at least one inode. */ void write_file(struct jffs_file *f, FILE *fs, struct stat st) { int npad = JFFS_GET_PAD_BYTES(f->inode.nsize); int dpad = JFFS_GET_PAD_BYTES(f->inode.dsize); int size = sizeof(struct jffs_raw_inode) + f->inode.nsize + npad + f->inode.dsize + dpad; unsigned char ff_data[] = { 0xff, 0xff, 0xff, 0xff }; if (verbose >= 2) { fprintf(stderr, "***write_file()\n"); } jffs_swap_inode(&f->inode); fwrite((void *)&f->inode, sizeof(struct jffs_raw_inode), 1, fs); jffs_swap_inode(&f->inode); if (f->inode.nsize) { fwrite(f->name, 1, f->inode.nsize, fs); if (npad) { fwrite(ff_data, 1, npad, fs); } } if (f->inode.dsize) { if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { if (sizeof(st.st_rdev) == 2) st.st_rdev = WORD_SWAP(st.st_rdev); else if (sizeof(st.st_rdev) == 4) st.st_rdev = LONG_SWAP(st.st_rdev); fwrite((char *)&st.st_rdev, sizeof(st.st_rdev)/4, 1, fs); if (sizeof(st.st_rdev) == 2) st.st_rdev = WORD_SWAP(st.st_rdev); else if (sizeof(st.st_rdev) == 4) st.st_rdev = LONG_SWAP(st.st_rdev); } else { fwrite(f->data, 1, f->inode.dsize, fs); } if (dpad) { fwrite(ff_data, 1, dpad, fs); } } fs_pos += size; /* If the space left on the block is smaller than the size of an inode, then skip it. */ }
static unsigned short calculateChecksum(char *buf, int len, unsigned short oldv) { int i, j; unsigned short sum=oldv, tmp; j = (len/2)*2; for (i=0; i<j; i+=2) { tmp = *((unsigned short *)(buf + i)); sum += WORD_SWAP(tmp); } if ( len % 2 ) { tmp = buf[len-1]; sum += WORD_SWAP(tmp); } return sum; }
/* Fortran character scan */ int ar_scan (ar_data *result, const AR_TYPE *resulttype, const char *str1, long len1, const char *str2, long len2, long backward) { long index; long back = backward; int status; extern long _F90_SCAN(const char *st1, const char *st2, long *back, int len1, int len2); index = _F90_SCAN(str1, str2, &back, len1, len2); result->ar_i64.part1 = 0; result->ar_i64.part2 = 0; result->ar_i64.part3 = index>>16; result->ar_i64.part4 = index & 0xffff; status = AR_STAT_OK; switch (*resulttype) { case AR_Int_8_S: if (( INT8_SIGN(result) && !IS_INT8_UPPER_ONES(result)) || (!INT8_SIGN(result) && !IS_INT8_UPPER_ZERO(result))) { return AR_STAT_OVERFLOW; } break; case AR_Int_16_S: if (( INT16_SIGN(result) && !IS_INT16_UPPER_ONES(result)) || (!INT16_SIGN(result) && !IS_INT16_UPPER_ZERO(result))) { return AR_STAT_OVERFLOW; } break; case AR_Int_32_S: if (( INT32_SIGN(result) && !IS_INT32_UPPER_ONES(result)) || (!INT32_SIGN(result) && !IS_INT32_UPPER_ZERO(result))) { return AR_STAT_OVERFLOW; } break; case AR_Int_64_S: break; default: return AR_STAT_INVALID_TYPE; } WORD_SWAP(result->ar_i64); return AR_status((AR_DATA*)result, resulttype); }
int main(int argc, char** argv) { int argNum=1; #ifdef COMPACT_FILENAME_BUFFER const char *outFile = NULL; #else char outFile[80]= {0}; #endif TYPE_T type; int mask=0, fh_out, fh_in, len, i, total=0; unsigned char *buf, *buf1; struct stat sbuf; int byteswap=0, cascade=0, last_idx=-1, is_all=0; int offset=0, with_header; IMG_HEADER_Tp pHeader; unsigned long burnAddr=0; IMG_HEADER_T allHeader; unsigned short checksum=0; memset(§or, 0, sizeof(sector)); while (argNum < argc) { if ( !strcmp(argv[argNum], "-o") ) { if (++argNum >= argc) break; #ifdef COMPACT_FILENAME_BUFFER outFile = argv[argNum]; #else sscanf(argv[argNum], "%s", outFile); #endif } else if ( !strcmp(argv[argNum], "-s") ) { byteswap = 1; } else if ( !strcmp(argv[argNum], "-c") ) { cascade = 1; } else if ( !strcmp(argv[argNum], "-a") ) { is_all = 1; memset(&allHeader, '\0', sizeof(IMG_HEADER_T)); memcpy(allHeader.signature, ALL_HEADER, SIGNATURE_LEN); } #if 0 else if ( !strcmp(argv[argNum], "-a2") ) { is_all = 1; memset(&allHeader, '\0', sizeof(IMG_HEADER_T)); memcpy(allHeader.signature, ALL_HEADER2, SIGNATURE_LEN); } #endif else { if (is_all) { if ((argNum+1) < argc) { printf("argument error, only input file could be specified!\n"); return -1; } #ifdef COMPACT_FILENAME_BUFFER sector[0].filename = argv[argNum]; #else strcpy(sector[0].filename,argv[argNum]); #endif sector[0].type=ALL; sector[0].with_header=0; mask |= (1 << ALL); } else { type=checkInputFile(argv[argNum], &with_header); DEBUG_PRINT("filename=%s, type=%d\n", argv[argNum], type); if (type == INVALID_FILE) { printf("\nInvalid input file %s!!\n", argv[argNum]); showHelp(); exit(1); } #ifdef COMPACT_FILENAME_BUFFER sector[type].filename = argv[argNum]; #else strcpy(sector[type].filename, argv[argNum]); #endif sector[type].with_header = with_header; sector[type].type = type; mask |= (1 << type); } } argNum++; } #ifdef COMPACT_FILENAME_BUFFER if( outFile == NULL ) #else if (!outFile[0]) #endif { printf("No output file specified!\n"); showHelp(); exit(1); } if (mask == 0) { printf("No valid input image found!\n"); exit(1); } // Create output file #ifdef WIN32 _chmod(outFile, S_IREAD|S_IWRITE); fh_out = open(outFile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY); #else chmod(outFile, S_IREAD|S_IWRITE); fh_out = open(outFile, O_RDWR|O_CREAT|O_TRUNC, 0644); #endif if (fh_out == -1) { printf("Create output file %s error!\n", outFile); exit(1); } if (is_all) { write(fh_out, &allHeader, sizeof(allHeader)); total += sizeof(allHeader); printf("\nGenerate "); } else printf("\nMerge "); for (i=BOOT_CODE; i<=ROOT ; i++) { #ifdef COMPACT_FILENAME_BUFFER if (sector[i].filename) #else if (sector[i].filename[0]) #endif { if ( stat(sector[i].filename, &sbuf) != 0 ) { printf("Stat file %s error!\n", sector[i].filename); exit(1); } buf = malloc(sbuf.st_size+1); if (buf == NULL) { printf("allocate buffer failed %d!\n", (int)sbuf.st_size); exit(1); } #ifdef WIN32 fh_in = open(sector[i].filename, O_RDONLY|O_BINARY); #else fh_in = open(sector[i].filename, O_RDONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); #endif if (fh_in < 0) { printf("Open file %s error!\n", sector[i].filename); close(fh_out); exit(1); } switch(sector[i].type) { case BOOT_CODE: printf("BOOT-CODE "); break; case CONFIG: printf("CONFIG-DATA(%s) ", (no_hw_config ? "no-hw" : "all")); break; case WEB_PAGES: printf("WEB-PAGES "); break; case SYS: printf("LINUX "); break; case ROOT: printf("ROOT "); break; case ALL: printf("ALL "); break; case INVALID_FILE: break; } if (read(fh_in, buf, sbuf.st_size) != sbuf.st_size) { printf("Read file %s error!\n", sector[i].filename); close(fh_in); close(fh_out); free(buf); exit(1); } if (is_all) checksum = calculateChecksum((char *)buf, sbuf.st_size, checksum); else { if (sector[i].with_header) { if (sector[i].type == CONFIG) { if (no_hw_config == 0) burnAddr = HW_SETTING_OFFSET; else burnAddr = DEFAULT_SETTING_OFFSET; } else { pHeader = (IMG_HEADER_Tp)buf; burnAddr = DWORD_SWAP(pHeader->burnAddr); } } else burnAddr = 0; if (byteswap) { if (sbuf.st_size % 2) { buf[sbuf.st_size] = '\0'; sbuf.st_size++; } do_byteswap(buf, sbuf.st_size); } // try to append 0 if necessary if (!cascade && last_idx!=-1 && sector[i].with_header) { if ((sector[last_idx].offset+sector[last_idx].size) < burnAddr) { len = burnAddr - (sector[last_idx].offset+sector[last_idx].size); buf1 = calloc(len, 1); if (buf1 == NULL) { printf("allocate buffer failed %d!\n", len); exit(1); } write(fh_out, buf1, len); // pad 0 free(buf1); total += len; // printf("pad size=%d, last_idx=%d, burnAddr=%d\n", len, last_idx, burnAddr); } } // skip header if necessary if (!cascade && sector[i].with_header && ((sector[i].type == ROOT) || (sector[i].type == BOOT_CODE))) { offset = sizeof(IMG_HEADER_T); // sbuf.st_size -= sizeof(IMG_HEADER_T); } else offset = 0; } // printf("write offset=%d, size=%d\n", offset, sbuf.st_size); if ( write(fh_out, buf + offset , sbuf.st_size-offset) != sbuf.st_size-offset) { printf("Write output file %s error!\n", outFile); close(fh_in); close(fh_out); free(buf); exit(1); } close(fh_in); if (burnAddr > 0) sector[i].offset = burnAddr; else sector[i].offset = total; sector[i].size = sbuf.st_size-offset; // printf("section[%d], offset=%d, size=%d\n", i, sector[i].offset, sector[i].size); total += sbuf.st_size; free(buf); last_idx = i; if (is_all) break; } } if (is_all) { allHeader.len = DWORD_SWAP((total+2)); checksum = calculateChecksum((char *)&allHeader, sizeof(allHeader), checksum); checksum = WORD_SWAP((~checksum+1)); write(fh_out, &checksum, 2); lseek(fh_out, 0, SEEK_SET); allHeader.len = DWORD_SWAP((total+2)); // allHeader.startAddr = DWORD_SWAP(image_num); write(fh_out, &allHeader, sizeof(allHeader)); total += (2 + sizeof(allHeader)); } close(fh_out); #ifdef WIN32 _chmod(outFile, S_IREAD); #else chmod(outFile, DEFFILEMODE); #endif printf("=> %s ok, size=%d.\n", outFile, total); exit(0); }
int main(int argc, char** argv) { #ifdef COMPACT_FILENAME_BUFFER const char *inFile = NULL, *outFile = NULL; #else char inFile[80]={0}, outFile[80]={0}; #endif int fh, size; struct stat status; char *buf; IMG_HEADER_Tp pHeader; unsigned int startAddr; unsigned int burnAddr; unsigned short checksum=0; int is_vmlinux = 0; int is_vmlinuxhdr = 0; int is_signature = 0; unsigned int lchecksum, padding_len=0; unsigned int start_addr=0; if (argc == 4 && !strcmp(argv[1], "size_chk")) { unsigned int total_size; #ifdef COMPACT_FILENAME_BUFFER inFile = argv[2]; #else sscanf(argv[2], "%s", inFile); #endif sscanf(argv[3], "%x", &startAddr); if ( stat(inFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inFile ); exit(1); } printf("==============================================\n"); printf("Summary ==>\n"); printf("Image loading addr :0x%x\n", (int)startAddr); printf("Image decompress end addr :0x%x\n", ((unsigned int)DEFAULT_BASE_ADDR)+(unsigned int)status.st_size); total_size = startAddr - ((unsigned int)DEFAULT_BASE_ADDR); if (status.st_size > (int)total_size) { printf("Error!!!! : Kernel image decompress will overwirte load image\n"); exit(1); } else printf("Available size :0x%08x\n", (unsigned int)(total_size - status.st_size)); exit(0); } #ifdef CONFIG_RTL_FLASH_MAPPING_ENABLE if (argc == 3 && !strcmp(argv[1], "flash_size_chk")) { unsigned int total_size; #ifdef COMPACT_FILENAME_BUFFER inFile = argv[2]; #else sscanf(argv[2], "%s", inFile); #endif if ( stat(inFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inFile ); exit(1); } printf("==============================================\n"); printf("Summary ==>\n"); printf("Image flash start addr :0x%x\n", (unsigned int)CODE_IMAGE_OFFSET); printf("Image flash end addr :0x%x\n", (unsigned int)CODE_IMAGE_OFFSET+(unsigned int)status.st_size); #ifdef CONFIG_RTL_802_1X_CLIENT_SUPPORT total_size = ROOT_IMAGE_OFFSET - CODE_IMAGE_OFFSET - CERT_SIZE; // To reserve CERT_SIZE for 802.1x wlan client mode to store 802.1x certs #else total_size = ROOT_IMAGE_OFFSET - CODE_IMAGE_OFFSET; #endif if (status.st_size > (int)total_size) { printf("Error!!!! : Kernel image too big will overwirte rootfs image, cur size(%d), available size(%d).\n",(int)status.st_size, total_size); exit(1); } else printf("Available size :0x%08x\n", (unsigned int)(total_size - status.st_size)); exit(0); } #endif #ifdef RTK_4K_CHKSUM_FIX if (argc == 4 && !strcmp(argv[1], "fix_chksum")) { rtk_fix_4k_chk(argv[2],argv[3]); return 0; } #endif if (argc == 4 && !strcmp(argv[1], "vmlinux")) is_vmlinux = 1; if (argc == 5 && !strcmp(argv[1], "vmlinuxhdr")) { is_vmlinuxhdr = 1; start_addr = extractStartAddr(argv[4]); } if (!strcmp(argv[1], "signature")) { is_signature = 1; if (argc != 7) { printf_usage(); exit(1); } } // parse input arguments if ( argc != 6 && !is_vmlinux && !is_vmlinuxhdr && !is_signature) { printf_usage(); exit(1); } #ifdef COMPACT_FILENAME_BUFFER inFile = argv[2]; outFile = argv[3]; #else sscanf(argv[2], "%s", inFile); sscanf(argv[3], "%s", outFile); #endif if ((!is_vmlinux) && (!is_vmlinuxhdr)) { sscanf(argv[4], "%x", &startAddr); sscanf(argv[5], "%x", &burnAddr); } // check input file and allocate buffer if ( stat(inFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inFile ); exit(1); } if (is_vmlinuxhdr) { size = status.st_size + sizeof(padding_len) + sizeof(lchecksum) + sizeof(start_addr); padding_len = 4 - (size%4); size += padding_len; } else if (!is_vmlinux) { size = status.st_size + sizeof(IMG_HEADER_T) + sizeof(checksum); if (size%2) size++; // pad } else { size = status.st_size + sizeof(padding_len) + sizeof(lchecksum); padding_len = 4 - (size%4); size += padding_len; } buf = malloc(size); if (buf == NULL) { printf("Malloc buffer failed!\n"); exit(1); } memset(buf, '\0', size); pHeader = (IMG_HEADER_Tp)buf; if (is_vmlinuxhdr) buf += 8; // skip padding-length field and start-address field else if (!is_vmlinux) buf += sizeof(IMG_HEADER_T); else buf += 4; // skip padding-length field // Read data and generate header fh = open(inFile, O_RDONLY); if ( fh == -1 ) { printf("Open input file error!\n"); free( pHeader ); exit(1); } lseek(fh, 0L, SEEK_SET); if ( read(fh, buf, status.st_size) != status.st_size) { printf("Read file error!\n"); close(fh); free(pHeader); exit(1); } close(fh); if (is_vmlinuxhdr) { *((unsigned int *)pHeader) = DWORD_SWAP(padding_len); *((unsigned int *)((char *)pHeader+4)) = start_addr; lchecksum = DWORD_SWAP(calculate_long_checksum((unsigned int *)buf, size-12)); memcpy(&buf[size-12], &lchecksum, 4); } else if (!is_vmlinux) { if( !strcmp("root", argv[1])) memcpy(pHeader->signature, ROOT_HEADER, SIGNATURE_LEN); else if ( !strcmp("boot", argv[1])) memcpy(pHeader->signature, BOOT_HEADER, SIGNATURE_LEN); else if ( !strcmp("linux", argv[1])) memcpy(pHeader->signature, FW_HEADER, SIGNATURE_LEN); else if ( !strcmp("linux-ro", argv[1])) memcpy(pHeader->signature, FW_HEADER_WITH_ROOT, SIGNATURE_LEN); else if ( !strcmp("signature", argv[1])) memcpy(pHeader->signature, argv[6], SIGNATURE_LEN); else{ printf("not supported signature\n"); exit(1); } pHeader->len = DWORD_SWAP((size-sizeof(IMG_HEADER_T))); pHeader->startAddr = DWORD_SWAP(startAddr); pHeader->burnAddr = DWORD_SWAP(burnAddr); if( !strcmp("root", argv[1])) { #define SIZE_OF_SQFS_SUPER_BLOCK 640 unsigned int fs_len; fs_len = DWORD_SWAP((size-sizeof(IMG_HEADER_T) - sizeof(checksum)- SIZE_OF_SQFS_SUPER_BLOCK)); memcpy(buf + 8, &fs_len, 4); } checksum = WORD_SWAP(calculateChecksum(buf, status.st_size)); *((unsigned short *)&buf[size-sizeof(IMG_HEADER_T)-sizeof(checksum)]) = checksum; } else { // is_vmlinux=1 *((unsigned int *)pHeader) = DWORD_SWAP(padding_len); lchecksum = DWORD_SWAP(calculate_long_checksum((unsigned int *)buf, size-8)); memcpy(&buf[size-8], &lchecksum, 4); } // Write image to output file fh = open(outFile, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU); //mark_88 if ( fh == -1 ) { printf("Create output file error! [%s]\n", outFile); free(pHeader); exit(1); } write(fh, pHeader, size); close(fh); chmod(outFile, DEFFILEMODE); if (is_vmlinuxhdr) printf("Generate image successfully, length=%d, checksum=0x%x, padding=%d, start address=0x%08x\n", size-12-padding_len, lchecksum, padding_len, DWORD_SWAP(start_addr)); else if (!is_vmlinux) printf("Generate image successfully, length=%d, checksum=0x%x\n", (int)DWORD_SWAP(pHeader->len), checksum); else printf("Generate image successfully, length=%d, checksum=0x%x, padding=%d\n", size-8-padding_len, lchecksum, padding_len); free(pHeader); return 0; }
static int rtk_fix_4k_chk(char *inputFile,char *outFile) { struct stat status; char *buf; int fh, size; IMG_HEADER_Tp pHeader; unsigned short checksum=0; int old_linx_len=0; if ( stat(inputFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inputFile ); exit(1); } //decide output size , current , stat+(checksum) size= status.st_size ; buf = malloc(size); if (buf == NULL) { printf("Malloc buffer failed!\n"); exit(1); } memset(buf, '\0', size); pHeader = (IMG_HEADER_Tp)buf; fh = open(inputFile, O_RDONLY); if ( fh == -1 ) { printf("Open input file error!\n"); free( pHeader ); exit(1); } lseek(fh, 0L, SEEK_SET); if ( read(fh, buf, status.st_size) != status.st_size) { printf("Read file error!\n"); close(fh); free(pHeader); exit(1); } close(fh); //update len(not include jffs2 endmark) old_linx_len = DWORD_SWAP(pHeader->len); //printf("old_linx_len = %8x\n !!",old_linx_len); pHeader->len = DWORD_SWAP((size-sizeof(IMG_HEADER_T)-JFFS2_ENDMARK_LEN)); //erase old chksum //printf("old_cksum = %8x\n !!",*((unsigned short *)&buf[sizeof(IMG_HEADER_T)+old_linx_len-sizeof(checksum)])); *((unsigned short *)&buf[sizeof(IMG_HEADER_T)+old_linx_len-sizeof(checksum)]) = 0; //add new chksum checksum = WORD_SWAP(calculateChecksum(buf+sizeof(IMG_HEADER_T), (size-sizeof(IMG_HEADER_T)-JFFS2_ENDMARK_LEN))); //*((unsigned short *)&buf[status.st_size]) = checksum; *((unsigned short *)&buf[sizeof(IMG_HEADER_T)+old_linx_len-sizeof(checksum)]) = checksum; //write to new file fh = open(outFile, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU); //mark_88 if ( fh == -1 ) { printf("Create output file error! [%s]\n", outFile); free(pHeader); exit(1); } write(fh, pHeader, size); close(fh); chmod(outFile, DEFFILEMODE); printf("Generate 4k alignment image(with jffs2 end mark)successfully, length=%d, checksum=0x%x\n", (int)DWORD_SWAP(pHeader->len), checksum); free(pHeader); return 0; }
int main(int argc, char** argv) { char inFile[80]={0}, outFile[80]={0}; int fh, size; struct stat status; char *buf; IMG_HEADER_Tp pHeader; unsigned long startAddr; unsigned long burnAddr; unsigned short checksum; if (argc == 4 && !strcmp(argv[1], "size_chk")) { unsigned long total_size; sscanf(argv[2], "%s", inFile); sscanf(argv[3], "%x", &startAddr); if ( stat(inFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inFile ); exit(1); } printf("==============================================\n"); printf("Summary ==>\n"); printf("Image loading addr :0x%x\n", startAddr); printf("Image decompress end addr :0x%x\n", ((unsigned long)DEFAULT_BASE_ADDR)+(unsigned long)status.st_size); total_size = startAddr - ((unsigned long)DEFAULT_BASE_ADDR); if (status.st_size > (int)total_size) printf("Error!!!! : Kernel image decompress will overwirte load image\n"); else printf("Available size :0x%08x\n", total_size - (unsigned long)status.st_size); exit(0); } // parse input arguments if ( argc != 6) { printf("Usage: cvimg [root|linux|boot|all] input-filename output-filename start-addr burn-addr\n"); exit(1); } sscanf(argv[2], "%s", inFile); sscanf(argv[3], "%s", outFile); sscanf(argv[4], "%x", &startAddr); sscanf(argv[5], "%x", &burnAddr); // check input file and allocate buffer if ( stat(inFile, &status) < 0 ) { printf("Can't stat file! [%s]\n", inFile ); exit(1); } size = status.st_size + sizeof(IMG_HEADER_T) + sizeof(checksum); if (size%2) size++; // pad buf = malloc(size); if (buf == NULL) { printf("Malloc buffer failed!\n"); exit(1); } memset(buf, '\0', size); pHeader = (IMG_HEADER_Tp)buf; buf += sizeof(IMG_HEADER_T); // Read data and generate header fh = open(inFile, O_RDONLY); if ( fh == -1 ) { printf("Open input file error!\n"); free( pHeader ); exit(1); } lseek(fh, 0L, SEEK_SET); if ( read(fh, buf, status.st_size) != status.st_size) { printf("Read file error!\n"); close(fh); free(pHeader); exit(1); } close(fh); if( !strcmp("root", argv[1])) memcpy(pHeader->signature, ROOT_HEADER, SIGNATURE_LEN); else if ( !strcmp("boot", argv[1])) memcpy(pHeader->signature, BOOT_HEADER, SIGNATURE_LEN); else if ( !strcmp("linux", argv[1])) memcpy(pHeader->signature, FW_HEADER, SIGNATURE_LEN); else if ( !strcmp("all", argv[1])) memcpy(pHeader->signature, ALL_HEADER, SIGNATURE_LEN); else{ printf("not supported signature\n"); exit(1); } pHeader->len = DWORD_SWAP(size-sizeof(IMG_HEADER_T)); pHeader->startAddr = DWORD_SWAP(startAddr); pHeader->burnAddr = DWORD_SWAP(burnAddr); checksum = WORD_SWAP(calculateChecksum(buf, status.st_size)); *((unsigned short *)&buf[size-sizeof(IMG_HEADER_T)-sizeof(checksum)]) = checksum; // Write image to output file fh = open(outFile, O_RDWR|O_CREAT|O_TRUNC); if ( fh == -1 ) { printf("Create output file error! [%s]\n", outFile); free(pHeader); exit(1); } write(fh, pHeader, size); close(fh); chmod(outFile, DEFFILEMODE); printf("Generate image successfully, length=%d, checksum=0x%x\n", DWORD_SWAP(pHeader->len), checksum); free(pHeader); }