Original 0.2.0pre6_plus_fixes_7 tarball
[acx-mac80211.git] / firmware / extract.c
blobd7dfa246ca56de784ad464a0d6b4ff923d429d5b
1 /* firmware/extract.c - tool for extracting firmwares out of linux
2 * binary drivers
4 * --------------------------------------------------------------------
6 * Copyright (C) 2003 ACX100 Open Source Project
8 * The contents of this file are subject to the Mozilla Public
9 * License Version 1.1 (the "License"); you may not use this file
10 * except in compliance with the License. You may obtain a copy of
11 * the License at http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS
14 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 * implied. See the License for the specific language governing
16 * rights and limitations under the License.
18 * Alternatively, the contents of this file may be used under the
19 * terms of the GNU Public License version 2 (the "GPL"), in which
20 * case the provisions of the GPL are applicable instead of the
21 * above. If you wish to allow the use of your version of this file
22 * only under the terms of the GPL and not to allow others to use
23 * your version of this file under the MPL, indicate your decision
24 * by deleting the provisions above and replace them with the notice
25 * and other provisions required by the GPL. If you do not delete
26 * the provisions above, a recipient may use your version of this
27 * file under either the MPL or the GPL.
29 * --------------------------------------------------------------------
31 * Inquiries regarding the ACX100 Open Source Project can be
32 * made directly to:
34 * acx100-users@lists.sf.net
35 * http://acx100.sf.net
37 * --------------------------------------------------------------------
40 /* TODO:
41 * - get the firmware size from the debug symbols in the binary
42 * - check for version string, not only 4 first bytes
45 #define _GNU_SOURCE
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
53 #define FRMWR_SIZE 29612
54 #define NR_COLS 10
56 int main(int argc, char *argv[])
58 FILE *driver;
59 unsigned char c;
60 int size, pos, file_size;
61 unsigned char *mem, *begin;
63 /* this is the signature for firmware version 1.5.0 which is present in all the linux binary drivers. */
64 unsigned char signature[] = {0x58, 0xb2, 0x24, 0x00};
66 /* check the arguments */
67 if (argc != 2)
69 fprintf(stderr, "Wrong call arguments.\n");
70 fprintf(stderr, "USAGE: extract binary_driver.o\n");
71 fprintf(stderr, " The firmware will be extracted, and will be written to stdout.\n");
72 exit(1);
75 /* try to open the file */
76 driver = fopen(argv[1], "r");
77 if(!driver)
79 fprintf(stderr, "ERROR: couldn't open or find given binary file %s to extract firmware image from it, aborting!\n", argv[1]);
80 exit(1);
83 /* get file stats */
84 fseek(driver, 0, SEEK_END);
85 file_size = ftell(driver);
86 fseek(driver, 0, SEEK_SET);
88 /* alloc buffer to hold complete file */
89 mem = malloc(file_size);
90 if(!mem)
92 fprintf(stderr, "Could not allocate enough memory\n");
93 exit(1);
96 /* read the complete file */
97 size = fread(mem, 1, file_size, driver);
98 if ( size != file_size)
100 fprintf(stderr, "Was unable to read complete file (%i, %i)\n", size, file_size);
101 exit(1);
105 /* find the firmware */
106 begin = memmem(mem, file_size, signature, 4);
107 if(!begin)
109 fprintf(stderr, "Firmware was not found\n");
110 exit(1);
113 /* start processing the firmware */
114 pos = 0;
115 do {
116 c = begin[pos];
117 printf("%c", c);
118 pos++;
119 } while(pos != FRMWR_SIZE);
121 free(driver);
123 exit(0);