r5u87x / fw-extract.c

/*
 * fw-extract.c
 * Produces Intel HEX files from Ricoh webcam microcode.
 * 
 * Code by Marcin Cieslak <saper@saper.info>, 2009
 * Released into the public domain.
 * 
 * Modifications:
 * uint_8 -> unsigned int so we can compile without kernel headers.
 * Added usage with no args.
 */

#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

struct fwchunk {
    int len;
    unsigned int addr[2];
    unsigned int bytes[2];
};

int
main(int argc, const char *argv[])
{
#define MAXFWSIZE 32768
    unsigned int fwbuf[MAXFWSIZE];
    unsigned int *ptr, *endptr;
    struct fwchunk *chkptr;
    ssize_t fwsize, chksiz;
    int firmware = 0;
    int i;
    const char *fwname;
    unsigned int cksum;

    if (argc >= 2) {
            fwname = argv[1];
    
            if ((firmware = open(fwname, O_RDONLY)) < 0) {
                perror("open firmware");
                return 3;
            }
    } else {
        printf("usage: fw-extract <firmware>\n");
        return 2;
    }
    fwsize = read(firmware, &fwbuf, MAXFWSIZE);
    if (fwsize == 0) {
        perror("read firmware");
        return 4;
    }
    close(firmware);
    if (fwsize == MAXFWSIZE)    
        return 1;
    ptr = fwbuf;
    endptr = fwbuf + fwsize;
    for (chkptr = (struct fwchunk *)fwbuf;
        ptr < endptr; ) {
        chksiz = chkptr->len;
        printf(":%02X%02X%02X%02X", (unsigned int)chksiz, 
            chkptr->addr[1], 
            chkptr->addr[0], 
            0);
        cksum = chksiz + chkptr->addr[1] + chkptr->addr[2];
        for (i = 0; i < chksiz; i ++)  {
            printf("%02X", chkptr->bytes[i]); 
            cksum += chkptr->bytes[i];
        }
        printf("%02X\n", ((cksum ^ 0xff) + 1) & 0xff);
        chkptr = (struct fwchunk *)(ptr += (chksiz + 3));
    }
    printf(":00000001FF\n");
    return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.