📄 mkmylofw.c
字号:
{ struct mylo_fw_blockdesc desc; struct fw_block *b; uint32_t dlen; int i; /* * if at least one partition specified, write out block descriptor * for the partition table */ if (fw_num_partitions > 0) { desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED); desc.addr = HOST_TO_LE32(board->part_offset); desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table)); desc.blen = HOST_TO_LE32(board->part_size); if (write_out_desc(outfile, &desc, crc) != 0) return -1; } /* * write out block descriptors for each files */ for (i = 0; i < fw_num_blocks; i++) { b = &fw_blocks[i]; /* detect block size */ dlen = b->size; if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) { dlen += sizeof(struct mylo_partition_header); } /* round up to 4 bytes */ dlen = ALIGN(dlen, 4); /* setup the descriptor */ desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED); desc.addr = HOST_TO_LE32(b->addr); desc.dlen = HOST_TO_LE32(dlen); desc.blen = HOST_TO_LE32(b->blocklen); if (write_out_desc(outfile, &desc, crc) != 0) return -1; } /* * write out the null block descriptor */ memset(&desc, 0, sizeof(desc)); if (write_out_desc(outfile, &desc, crc) != 0) return -1; if (write_out_partitions(outfile, crc) != 0) return -1; /* * write out data for each blocks */ for (i = 0; i < fw_num_blocks; i++) { b = &fw_blocks[i]; if (write_out_file(outfile, b, crc) != 0) return -1; } return 0;}/* * argument parsing */intparse_arg(char *arg, char *buf, char *argv[]){ int res = 0; size_t argl; char *tok; char **ap = &buf; int i; if ((arg == NULL)) { /* invalid argument string */ return -1; } argl = strlen(arg); if (argl == 0) { /* no arguments */ return res; } if (argl >= MAX_ARG_LEN) { /* argument is too long */ argl = MAX_ARG_LEN-1; } memset(argv, 0, MAX_ARG_COUNT * sizeof(void *)); memcpy(buf, arg, argl); buf[argl] = '\0'; for (i = 0; i < MAX_ARG_COUNT; i++) { tok = strsep(ap, ":"); if (tok == NULL) { break; }#if 0 else if (tok[0] == '\0') { break; }#endif argv[i] = tok; res++; } return res;}intrequired_arg(char c, char *arg){ if ((optarg != NULL) && (*arg == '-')){ errmsg(0,"option %c requires an argument\n", c); return -1; } return 0;}intis_empty_arg(char *arg){ int ret = 1; if (arg != NULL) { if (*arg) ret = 0; }; return ret;}intparse_opt_flags(char ch, char *arg){ if (required_arg(ch, arg)) { goto err_out; } if (str2u32(arg, &fw_header.flags) != 0) { errmsg(0,"invalid firmware flags: %s", arg); goto err_out; } dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags); return 0;err_out: return -1;}intparse_opt_size(char ch, char *arg){ if (required_arg(ch, arg)) { goto err_out; } if (str2u32(arg, &flash_size) != 0) { errmsg(0,"invalid flash size: %s", arg); goto err_out; } dbgmsg(1, "flash size set to %d bytes", flash_size); return 0;err_out: return -1;}intparse_opt_id(char ch, char *arg){ char buf[MAX_ARG_LEN]; char *argv[MAX_ARG_COUNT]; int argc; char *p; if (required_arg(ch, arg)) { goto err_out; } argc = parse_arg(arg, buf, argv); /* processing vendor ID*/ p = argv[0]; if (is_empty_arg(p)) { errmsg(0,"vendor id is missing from -%c %s",ch, arg); goto err_out; } else if (str2u16(p, &fw_header.vid) != 0) { errmsg(0,"invalid vendor id: %s", p); goto err_out; } dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid); /* processing device ID*/ p = argv[1]; if (is_empty_arg(p)) { errmsg(0,"device id is missing from -%c %s",ch, arg); goto err_out; } else if (str2u16(p, &fw_header.did) != 0) { errmsg(0,"invalid device id: %s", p); goto err_out; } dbgmsg(1, "device id is set to 0x%04X", fw_header.did); /* processing sub vendor ID*/ p = argv[2]; if (is_empty_arg(p)) { fw_header.svid = fw_header.vid; } else if (str2u16(p, &fw_header.svid) != 0) { errmsg(0,"invalid sub vendor id: %s", p); goto err_out; } dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid); /* processing device ID*/ p = argv[3]; if (is_empty_arg(p)) { fw_header.sdid = fw_header.did; } else if (str2u16(p, &fw_header.sdid) != 0) { errmsg(0,"invalid sub device id: %s", p); goto err_out; } dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid); /* processing revision */ p = argv[4]; if (is_empty_arg(p)) { fw_header.rev = 0; } else if (str2u32(arg, &fw_header.rev) != 0) { errmsg(0,"invalid revision number: %s", p); goto err_out; } dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev); return 0;err_out: return -1;}intparse_opt_block(char ch, char *arg){ char buf[MAX_ARG_LEN]; char *argv[MAX_ARG_COUNT]; int argc; struct fw_block *b; char *p; if (required_arg(ch, arg)) { goto err_out; } if (fw_num_blocks >= MAX_FW_BLOCKS) { errmsg(0,"too many blocks specified"); goto err_out; } argc = parse_arg(arg, buf, argv); dbgmsg(1,"processing block option %s, count %d", arg, argc); b = &fw_blocks[fw_num_blocks++]; /* processing block address */ p = argv[0]; if (is_empty_arg(p)) { errmsg(0,"no block address specified in %s", arg); goto err_out; } else if (str2u32(p, &b->addr) != 0) { errmsg(0,"invalid block address: %s", p); goto err_out; } /* processing block length */ p = argv[1]; if (is_empty_arg(p)) { errmsg(0,"no block length specified in %s", arg); goto err_out; } else if (str2u32(p, &b->blocklen) != 0) { errmsg(0,"invalid block length: %s", p); goto err_out; } if (argc < 3) { dbgmsg(1,"empty block %s", arg); goto success; } /* processing flags */ p = argv[2]; if (is_empty_arg(p) == 0) { for ( ; *p != '\0'; p++) { switch (*p) { case 'h': b->flags |= BLOCK_FLAG_HAVEHDR; break; default: errmsg(0, "invalid block flag \"%c\"", *p); goto err_out; } } } /* processing file name */ p = argv[3]; if (is_empty_arg(p)) { errmsg(0,"file name missing in %s", arg); goto err_out; } b->name = strdup(p); if (b->name == NULL) { errmsg(0,"not enough memory"); goto err_out; }success: return 0;err_out: return -1;}intparse_opt_partition(char ch, char *arg){ char buf[MAX_ARG_LEN]; char *argv[MAX_ARG_COUNT]; int argc; char *p; struct mylo_partition *part; if (required_arg(ch, arg)) { goto err_out; } if (fw_num_partitions >= MYLO_MAX_PARTITIONS) { errmsg(0, "too many partitions specified"); goto err_out; } part = &fw_partitions[fw_num_partitions++]; argc = parse_arg(arg, buf, argv); /* processing partition address */ p = argv[0]; if (is_empty_arg(p)) { errmsg(0,"partition address missing in -%c %s",ch, arg); goto err_out; } else if (str2u32(p, &part->addr) != 0) { errmsg(0,"invalid partition address: %s", p); goto err_out; } /* processing partition size */ p = argv[1]; if (is_empty_arg(p)) { errmsg(0,"partition size missing in -%c %s",ch, arg); goto err_out; } else if (str2u32(p, &part->size) != 0) { errmsg(0,"invalid partition size: %s", p); goto err_out; } /* processing partition flags */ p = argv[2]; if (is_empty_arg(p) == 0) { for ( ; *p != '\0'; p++) { switch (*p) { case 'a': part->flags |= PARTITION_FLAG_ACTIVE; break; case 'p': part->flags |= PARTITION_FLAG_PRELOAD; break; case 'l': part->flags |= PARTITION_FLAG_LZMA; break; case 'h': part->flags |= PARTITION_FLAG_HAVEHDR; break; default: errmsg(0, "invalid partition flag \"%c\"", *p); goto err_out; } } } /* processing partition parameter */ p = argv[3]; if (is_empty_arg(p)) { /* set default partition parameter */ part->param = 0; } else if (str2u32(p, &part->param) != 0) { errmsg(0,"invalid partition parameter: %s", p); goto err_out; }#if 1 if (part->size == 0) { part->size = flash_size - part->addr; } /* processing file parameter */ p = argv[4]; if (is_empty_arg(p) == 0) { struct fw_block *b; if (fw_num_blocks == MAX_FW_BLOCKS) { errmsg(0,"too many blocks specified", p); goto err_out; } b = &fw_blocks[fw_num_blocks++]; b->name = strdup(p); b->addr = part->addr; b->blocklen = part->size; if (part->flags & PARTITION_FLAG_HAVEHDR) { b->flags |= BLOCK_FLAG_HAVEHDR; } }#endif return 0;err_out: return -1;}intparse_opt_board(char ch, char *arg){ if (required_arg(ch, arg)) { goto err_out; } board = find_board(arg); if (board == NULL){ errmsg(0,"invalid/unknown board specified: %s", arg); goto err_out; } fw_header.vid = board->vid; fw_header.did = board->did; fw_header.svid = board->svid; fw_header.sdid = board->sdid; flash_size = board->flash_size; return 0;err_out: return -1;}intparse_opt_rev(char ch, char *arg){ if (required_arg(ch, arg)) { return -1; } if (str2u32(arg, &fw_header.rev) != 0) { errmsg(0,"invalid revision number: %s", arg); return -1; } return 0;}/* * main */intmain(int argc, char *argv[]){ int optinvalid = 0; /* flag for invalid option */ int c; int res = EXIT_FAILURE; FILE *outfile; uint32_t crc; progname=basename(argv[0]); memset(&fw_header, 0, sizeof(fw_header)); /* init header defaults */ fw_header.vid = VENID_COMPEX; fw_header.did = DEVID_COMPEX_WP54G; fw_header.svid = VENID_COMPEX; fw_header.sdid = DEVID_COMPEX_WP54G; fw_header.fwhi = 0x20000; fw_header.fwlo = 0x20000; fw_header.flags = 0; opterr = 0; /* could not print standard getopt error messages */ while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) { optinvalid = 0; switch (c) { case 'b': optinvalid = parse_opt_block(c,optarg); break; case 'B': optinvalid = parse_opt_board(c,optarg); break; case 'f': optinvalid = parse_opt_flags(c,optarg); break; case 'h': usage(EXIT_SUCCESS); break; case 'i': optinvalid = parse_opt_id(c,optarg); break; case 'p': optinvalid = parse_opt_partition(c,optarg); break; case 'r': optinvalid = parse_opt_rev(c,optarg); break; case 's': optinvalid = parse_opt_size(c,optarg); break; case 'v': verblevel++; break; default: optinvalid = 1; break; } if (optinvalid != 0 ){ errmsg(0, "invalid option: -%c", optopt); goto out; } } if (optind == argc) { errmsg(0, "no output file specified"); goto out; } ofname = argv[optind++]; if (optind < argc) { errmsg(0, "invalid option: %s", argv[optind]); goto out; } if (!board) { errmsg(0, "no board specified"); goto out; } if (flash_size == 0) { errmsg(0, "no flash size specified"); goto out; } if (process_files() != 0) { goto out; } if (process_partitions() != 0) { goto out; } outfile = fopen(ofname, "w"); if (outfile == NULL) { errmsg(1, "could not open \"%s\" for writing", ofname); goto out; } crc = 0; init_crc_table(); if (write_out_header(outfile, &crc) != 0) goto out_flush; if (write_out_blocks(outfile, &crc) != 0) goto out_flush; fw_header.crc = crc; if (write_out_header(outfile, NULL) != 0) goto out_flush; dbgmsg(1,"Firmware file %s completed.", ofname); res = EXIT_SUCCESS;out_flush: fflush(outfile); fclose(outfile); if (res != EXIT_SUCCESS) { unlink(ofname); }out: return res;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -