X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=fwcutter%2Ffwcutter.c;h=38d4c45c98da1f792d972939c3631490652586bd;hb=9340af4c79501e72d6e191f4343b33f46929cc77;hp=fef90990453814672422af3e63d39bc45de8711f;hpb=b9e19faa25ad9e1b24f08dc77267760df1b98504;p=b43-tools.git diff --git a/fwcutter/fwcutter.c b/fwcutter/fwcutter.c index fef9099..38d4c45 100644 --- a/fwcutter/fwcutter.c +++ b/fwcutter/fwcutter.c @@ -2,7 +2,7 @@ * firmware cutter for broadcom 43xx wireless driver files * * Copyright (c) 2005 Martin Langer , - * 2005-2007 Michael Buesch + * 2005-2007 Michael Buesch * 2005 Alex Beregszaszi * 2007 Johannes Berg * @@ -38,7 +38,7 @@ #include #include -#ifdef __DragonFly__ +#if defined(__DragonFly__) || defined(__FreeBSD__) #include #else #include @@ -48,7 +48,7 @@ #include "fwcutter.h" #include "fwcutter_list.h" -#ifdef __DragonFly__ +#if defined(__DragonFly__) || defined(__FreeBSD__) #define V3_FW_DIRNAME "v3" #define V4_FW_DIRNAME "v4" #else @@ -59,18 +59,12 @@ static struct cmdline_args cmdargs; -/* Convert a CPU-endian 16bit integer to Big-Endian */ -static be16_t to_be16(uint16_t v) +/* check whether file will be listed/extracted from */ +static int file_ok(const struct file *f) { - uint8_t ret[2]; - - ret[0] = (v & 0xFF00) >> 8; - ret[1] = (v & 0x00FF); - - return *((be16_t *)ret); + return !(f->flags & FW_FLAG_UNSUPPORTED) || cmdargs.unsupported; } -#if 0 /* Convert a Big-Endian 16bit integer to CPU-endian */ static uint16_t from_be16(be16_t v) { @@ -81,19 +75,11 @@ static uint16_t from_be16(be16_t v) return ret; } -#endif -/* Convert a CPU-endian 32bit integer to Big-Endian */ -static be32_t to_be32(uint32_t v) +/* Convert a CPU-endian 16bit integer to Big-Endian */ +static be16_t to_be16(uint16_t v) { - uint8_t ret[4]; - - ret[0] = (v & 0xFF000000) >> 24; - ret[1] = (v & 0x00FF0000) >> 16; - ret[2] = (v & 0x0000FF00) >> 8; - ret[3] = (v & 0x000000FF); - - return *((be32_t *)ret); + return (be16_t)from_be16((be16_t)v); } /* Convert a Big-Endian 32bit integer to CPU-endian */ @@ -109,6 +95,12 @@ static uint32_t from_be32(be32_t v) return ret; } +/* Convert a CPU-endian 32bit integer to Big-Endian */ +static be32_t to_be32(uint32_t v) +{ + return (be32_t)from_be32((be32_t)v); +} + /* tiny disassembler */ static void print_ucode_version(struct insn *insn) { @@ -398,7 +390,7 @@ static void extract_or_identify(FILE *f, const struct extract *extract, exit(255); } - if (!cmdargs.identify_only) + if (cmdargs.mode == FWCM_EXTRACT) write_file(extract->name, buf, data_length, &hdr, flags); free(buf); @@ -433,6 +425,8 @@ static void print_file(const struct file *file) printf("%s\t", file->ucode_version); if (strlen(file->ucode_version) < 8) printf("\t"); + printf("%s\t", file->id); + printf("%s\n", file->md5); } @@ -442,17 +436,23 @@ static void print_supported_files(void) print_banner(); printf("\nExtracting firmware is possible " - "from these binary driver files:\n\n"); + "from these binary driver files.\n" + "The column shows the unique identifier string " + "for your firmware.\nYou must select the firmware with the " + "same ID as printed by the kernel driver on modprobe.\n" + "Note that only recent drivers print such a message on modprobe.\n" + "Please read http://linuxwireless.org/en/users/Drivers/b43#devicefirmware\n\n"); printf("\t" "\t\t" "\t" + "\t" "\n\n"); /* print for legacy driver first */ - for (i = 0; i < FILES; i++) - if (!(files[i].flags & FW_FLAG_V4)) + for (i = 0; i < ARRAY_SIZE(files); i++) + if (file_ok(&files[i]) && !(files[i].flags & FW_FLAG_V4)) print_file(&files[i]); - for (i = 0; i < FILES; i++) - if (files[i].flags & FW_FLAG_V4) + for (i = 0; i < ARRAY_SIZE(files); i++) + if (file_ok(&files[i]) && files[i].flags & FW_FLAG_V4) print_file(&files[i]); printf("\n"); } @@ -477,9 +477,11 @@ static const struct file *find_file(FILE *fd) signature[8], signature[9], signature[10], signature[11], signature[12], signature[13], signature[14], signature[15]); - for (i = 0; i < FILES; ++i) { - if (strcasecmp(md5sig, files[i].md5) == 0) { + for (i = 0; i < ARRAY_SIZE(files); i++) { + if (file_ok(&files[i]) && + strcasecmp(md5sig, files[i].md5) == 0) { printf("This file is recognised as:\n"); + printf(" ID : %s\n", files[i].id); printf(" filename : %s\n", files[i].name); printf(" version : %s\n", files[i].ucode_version); printf(" MD5 : %s\n", files[i].md5); @@ -496,7 +498,11 @@ static const struct file *find_file(FILE *fd) static void print_usage(int argc, char *argv[]) { print_banner(); - printf("\nUsage: %s [OPTION] [driver.sys]\n", argv[0]); + printf("\nA tool to extract firmware for a Broadcom 43xx device\n"); + printf("from a proprietary Broadcom 43xx device driver file.\n"); + printf("\nUsage: %s [OPTION] [proprietary-driver-file]\n", argv[0]); + printf(" --unsupported " + "Allow working on extractable but unsupported drivers\n"); printf(" -l|--list " "List supported driver versions\n"); printf(" -i|--identify " @@ -507,8 +513,9 @@ static void print_usage(int argc, char *argv[]) "Print b43-fwcutter version\n"); printf(" -h|--help " "Print this help\n"); - printf("\nExample: %s bcmwl5.sys\n" - " to extract the firmware blobs from bcmwl5.sys\n", + printf("\nExample: %s -w /lib/firmware wl_apsta.o\n" + " to extract the firmware blobs from wl_apsta.o and store\n" + " the resulting firmware in /lib/firmware\n", argv[0]); } @@ -544,7 +551,7 @@ static int do_cmp_arg(char **argv, int *pos, if (param) { /* Skip the parameter on the next iteration. */ (*pos)++; - if (*param == 0) { + if (!*param) { printf("%s needs a parameter\n", arg); return ARG_ERROR; } @@ -582,29 +589,36 @@ static int parse_args(int argc, char *argv[]) if (argc < 2) goto out_usage; for (i = 1; i < argc; i++) { - res = cmp_arg(argv, &i, "--list", "-l", 0); + res = cmp_arg(argv, &i, "--list", "-l", NULL); if (res == ARG_MATCH) { - print_supported_files(); - return 1; + cmdargs.mode = FWCM_LIST; + continue; } else if (res == ARG_ERROR) goto out; - res = cmp_arg(argv, &i, "--version", "-v", 0); + res = cmp_arg(argv, &i, "--version", "-v", NULL); if (res == ARG_MATCH) { print_banner(); return 1; } else if (res == ARG_ERROR) goto out; - res = cmp_arg(argv, &i, "--help", "-h", 0); + res = cmp_arg(argv, &i, "--help", "-h", NULL); if (res == ARG_MATCH) goto out_usage; else if (res == ARG_ERROR) goto out; - res = cmp_arg(argv, &i, "--identify", "-i", 0); + res = cmp_arg(argv, &i, "--identify", "-i", NULL); if (res == ARG_MATCH) { - cmdargs.identify_only = 1; + cmdargs.mode = FWCM_IDENTIFY; + continue; + } else if (res == ARG_ERROR) + goto out; + + res = cmp_arg(argv, &i, "--unsupported", NULL, NULL); + if (res == ARG_MATCH) { + cmdargs.unsupported = 1; continue; } else if (res == ARG_ERROR) goto out; @@ -620,7 +634,7 @@ static int parse_args(int argc, char *argv[]) break; } - if (!cmdargs.infile) + if (!cmdargs.infile && cmdargs.mode != FWCM_LIST) goto out_usage; return 0; @@ -645,6 +659,11 @@ int main(int argc, char *argv[]) else if (err != 0) return err; + if (cmdargs.mode == FWCM_LIST) { + print_supported_files(); + return 0; + } + fd = fopen(cmdargs.infile, "rb"); if (!fd) { fprintf(stderr, "Cannot open input file %s\n", cmdargs.infile); @@ -664,7 +683,7 @@ int main(int argc, char *argv[]) extract = file->extract; while (extract->name) { printf("%s %s/%s.fw\n", - cmdargs.identify_only ? "Contains" : "Extracting", + cmdargs.mode == FWCM_IDENTIFY ? "Contains" : "Extracting", dir, extract->name); extract_or_identify(fd, extract, file->flags); extract++;