Encode it

From OpenFSG
Jump to: navigation, search


The last step in building the firmware is encoding it. That's done by encode_it. This is an (unofficial) reimplemenatation:

/*
 * encode_it.c: reimplementation of encode_it as shipped in
 * http://www.openfsg.com/download/source-fcsnap-3.1.15.tar.bz2
 *
 * copyright (C) 2006 Paul Bolle <pebolle@tiscali.nl>
 **/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 **/

#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>

char *err_a = "ERROR: Argument missing!";
char *err_r = "ERROR: File not found!";
char *err_w = "ERROR: File not writeable!";

FILE *fp;
unsigned int i, csum, tmpsum;
unsigned char *buf, *ptr;
size_t size, sread;

int
calc_csum (unsigned int csum, unsigned char *ptr) {
    unsigned int tmp;
    int i;

    tmp = ((unsigned int) *ptr & 0xff);
    tmp = tmp << 8;
    csum = csum ^ tmp;

    for (i=0; i<8; i++) {
        csum = csum << 1;
        if (csum & 0x10000)
            csum = csum ^ 0x1021;
    }

    return csum;
}

unsigned char *
read_file (size_t *size, char * filename) {
    struct stat stat_buf;
    unsigned char *tmp, *end;

    if (stat (filename, &stat_buf) != 0)
        return NULL;
    *size = (size_t) stat_buf.st_size;

    fp = fopen(filename, "r");
    if (fp == NULL) {
       return NULL;
    }

    tmp = (unsigned char *) malloc(*size+1);
    if (tmp == NULL) {
        return tmp;
    }

    sread = fread (tmp, sizeof(char), *size, fp);
    if (sread != *size) {
        free (tmp);
        return NULL;
    }

    end = tmp + *size + 1;
    *end = 0;

    fclose(fp);

    return tmp;
}

int
main (int argc, char *argv[]) {

   if (argc != 2) {
        printf ("%s\n", err_a);
        return 1;
    }

    buf = read_file(&size, argv[1]);
    if (buf == NULL) {
        printf ("%s\n", err_r);
        return 1;
    }

    ptr = buf;
    csum = 0xffff;

    for (i=0; i<size; i++) {
        csum = calc_csum(csum, ptr);
        ptr++;
    }

    tmpsum = (csum & 0xff00) >> 8;
    tmpsum |= (csum & 0xff) << 8;

    ptr = buf;
    for (i=0; i<size; i++) {
        csum = csum ^ *ptr;
        *ptr = csum & 0xff;
        ptr++;
    }

    fp = fopen (argv[1], "w");
    if (fp == NULL) {
        printf ("%s\n", err_w);
        return 1;
    }

    fwrite (&tmpsum, 1, 2, fp);
    fwrite (buf, size, 1, fp);

    fclose (fp);

    return 0;
}

See also decode_it.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox