Blame echoaudio/fw_writer.c

Packit e67170
/*
Packit e67170
 *  ALSA driver for Echoaudio soundcards.
Packit e67170
 *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
Packit e67170
 *
Packit e67170
 *  This program is free software; you can redistribute it and/or modify
Packit e67170
 *  it under the terms of the GNU General Public License as published by
Packit e67170
 *  the Free Software Foundation; version 2 of the License.
Packit e67170
 *
Packit e67170
 *  This program is distributed in the hope that it will be useful,
Packit e67170
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit e67170
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit e67170
 *  GNU General Public License for more details.
Packit e67170
 *
Packit e67170
 *  You should have received a copy of the GNU General Public License
Packit e67170
 *  along with this program; if not, write to the Free Software
Packit e67170
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Packit e67170
 */
Packit e67170
Packit e67170
#include <stdio.h>
Packit e67170
#include <sys/stat.h>
Packit e67170
#include <stdint.h>
Packit e67170
#include <stdlib.h>
Packit e67170
#include <unistd.h>
Packit e67170
#include <fcntl.h>
Packit e67170
#include <string.h>
Packit e67170
#include <errno.h>
Packit e67170
Packit e67170
Packit e67170
char *next_number(char *c)
Packit e67170
{
Packit e67170
	while (1) {
Packit e67170
		c++;
Packit e67170
		if (*c==0 || *c == '}')
Packit e67170
			return NULL;
Packit e67170
		if (c[0]=='0' && c[1]=='x')
Packit e67170
			return c;
Packit e67170
	}
Packit e67170
}
Packit e67170
Packit e67170
Packit e67170
Packit e67170
int write_fw(const char *dest, const char *src)
Packit e67170
{
Packit e67170
	uint16_t d16;
Packit e67170
	uint8_t d8;
Packit e67170
	int fd;
Packit e67170
	struct stat stbuf;
Packit e67170
	char *buf, *c;
Packit e67170
Packit e67170
	if ((fd = open(src, O_RDONLY)) < 0) {
Packit e67170
		printf("%s: %s\n", src, strerror(errno));
Packit e67170
		exit(errno);
Packit e67170
	}
Packit e67170
	if (fstat(fd, &stbuf) < 0) {
Packit e67170
		printf("%s: %s\n", src, strerror(errno));
Packit e67170
		exit(errno);
Packit e67170
	}
Packit e67170
	if (!(buf = malloc(stbuf.st_size + 1))) {
Packit e67170
		puts("Out of memory.");
Packit e67170
		exit(ENOMEM);
Packit e67170
	}
Packit e67170
	if (read(fd, buf, stbuf.st_size) < stbuf.st_size) {
Packit e67170
		puts("Read error.");
Packit e67170
		exit(EIO);
Packit e67170
	}
Packit e67170
	close(fd);
Packit e67170
	buf[stbuf.st_size] = 0;
Packit e67170
Packit e67170
	if ((fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
Packit e67170
		printf("%s: %s\n", dest, strerror(errno));
Packit e67170
		exit(errno);
Packit e67170
	}
Packit e67170
	if ((c = strstr(buf, "u8 ")) || (c = strstr(buf, "BYTE ")) || (c = strstr(buf, "char "))) {
Packit e67170
		while (c = next_number(c)) {
Packit e67170
			d8 = strtol(c, NULL, 16);
Packit e67170
			if (write(fd, &d8, 1) < 1) {
Packit e67170
				printf("Error writing %s\n", dest);
Packit e67170
				exit(EIO);
Packit e67170
			}
Packit e67170
		}
Packit e67170
	} else if ((c = strstr(buf, "u16 ")) || (c = strstr(buf, "WORD "))) {
Packit e67170
		while (c = next_number(c)) {
Packit e67170
			d16 = strtol(c, NULL, 16);
Packit e67170
			if (write(fd, &d16, 2) < 2) {
Packit e67170
				printf("Error writing %s\n", dest);
Packit e67170
				exit(EIO);
Packit e67170
			}
Packit e67170
		}
Packit e67170
	} else {
Packit e67170
		printf("%s currupted ?\n", src);
Packit e67170
		exit(EINVAL);
Packit e67170
	}
Packit e67170
	close(fd);
Packit e67170
	free(buf);
Packit e67170
Packit e67170
	return 0;
Packit e67170
}
Packit e67170
Packit e67170
Packit e67170
Packit e67170
int main(int argc, char *argv[])
Packit e67170
{
Packit e67170
	if (argc != 3) {
Packit e67170
		printf("Syntax: %s <source> <destination>\n", argv[0]);
Packit e67170
		exit(0);
Packit e67170
	}
Packit e67170
	write_fw(argv[2], argv[1]);
Packit e67170
	return 0;
Packit e67170
}