2014-02-15 16:22:03 +04:00
|
|
|
/***************************************************************************
|
|
|
|
copyright : (C) by 2003-2004 Stefano Barbato
|
|
|
|
email : stefano@codesink.org
|
|
|
|
|
|
|
|
$Id: eeprog.c,v 1.28 2004/02/29 11:06:41 tat Exp $
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "24cXX.h"
|
|
|
|
|
|
|
|
#define VERSION "0.7.6"
|
|
|
|
|
|
|
|
#define ENV_DEV "EEPROG_DEV"
|
|
|
|
#define ENV_I2C_ADDR "EEPROG_I2C_ADDR"
|
|
|
|
|
|
|
|
int g_quiet;
|
|
|
|
|
|
|
|
#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);
|
|
|
|
void do_usage_if(int b, int line)
|
|
|
|
{
|
2020-12-10 17:49:18 +03:00
|
|
|
const static char *eeprog_usage =
|
2014-02-15 16:22:03 +04:00
|
|
|
"eeprog " VERSION ", a 24Cxx EEPROM reader/writer\n"
|
|
|
|
"Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n"
|
2020-12-10 17:49:18 +03:00
|
|
|
"Usage: eeprog [-fqxdh] [-16|-8] [ -r addr[:count] | -w addr ] /dev/i2c-N i2c-address\n"
|
2014-02-15 16:22:03 +04:00
|
|
|
"\n"
|
|
|
|
" Address modes: \n"
|
|
|
|
" -8 Use 8bit address mode for 24c0x...24C16 [default]\n"
|
|
|
|
" -16 Use 16bit address mode for 24c32...24C256\n"
|
|
|
|
" Actions: \n"
|
2020-12-10 17:49:18 +03:00
|
|
|
" -r addr[:count] Read [count] (1 if omitted) bytes from [addr]\n"
|
|
|
|
" and print them to the standard output\n"
|
2014-02-15 16:22:03 +04:00
|
|
|
" -w addr Write input (stdin) at address [addr] of the EEPROM\n"
|
2020-12-10 17:49:18 +03:00
|
|
|
" -t n write timeout in ms (default 10)\n"
|
2014-02-15 16:22:03 +04:00
|
|
|
" -h Print this help\n"
|
|
|
|
" Options: \n"
|
2020-12-10 17:49:18 +03:00
|
|
|
" -x Set hex output mode\n"
|
|
|
|
" -d Dummy mode, display what *would* have been done\n"
|
2014-02-15 16:22:03 +04:00
|
|
|
" -f Disable warnings and don't ask confirmation\n"
|
|
|
|
" -q Quiet mode\n"
|
|
|
|
"\n"
|
|
|
|
"The following environment variables could be set instead of the command\n"
|
|
|
|
"line arguments:\n"
|
|
|
|
" EEPROG_DEV device name(/dev/i2c-N)\n"
|
|
|
|
" EEPROG_I2C_ADDR i2c-address\n"
|
|
|
|
"\n"
|
|
|
|
" Examples\n"
|
|
|
|
" 1- read 64 bytes from the EEPROM at address 0x54 on bus 0 starting\n"
|
|
|
|
" at address 123 (decimal)\n"
|
|
|
|
" eeprog /dev/i2c-0 0x54 -r 123:64\n"
|
|
|
|
" 2- prints the hex codes of the first 32 bytes read from bus 1 \n"
|
|
|
|
" at address 0x22\n"
|
|
|
|
" eeprog /dev/i2c-1 0x51 -x -r 0x22:0x20\n"
|
|
|
|
" 3- write the current timestamp at address 0x200 of the EEPROM on \n"
|
|
|
|
" bus 0 at address 0x33 \n"
|
|
|
|
" date | eeprog /dev/i2c-0 0x33 -w 0x200\n";
|
|
|
|
|
|
|
|
if(!b)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);
|
|
|
|
void do_die_if(int b, char* msg, int line)
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "Error at line %d: %s\n", line, msg);
|
|
|
|
//fprintf(stderr, " sysmsg: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define print_info(args...) do { if(!g_quiet) fprintf(stderr, args); } while(0);
|
|
|
|
|
|
|
|
void parse_arg(char *arg, int* paddr, int *psize)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
*paddr = strtoul(arg, &end, 0);
|
|
|
|
if(*end == ':')
|
|
|
|
*psize = strtoul(++end, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int confirm_action()
|
|
|
|
{
|
2020-12-10 17:49:18 +03:00
|
|
|
fprintf(stderr,
|
2014-02-15 16:22:03 +04:00
|
|
|
"\n"
|
|
|
|
"____________________________WARNING____________________________\n"
|
|
|
|
"Erroneously writing to a system EEPROM (like DIMM SPD modules)\n"
|
|
|
|
"can break your system. It will NOT boot anymore so you'll not\n"
|
|
|
|
"be able to fix it.\n"
|
|
|
|
"\n"
|
|
|
|
"Reading from 8bit EEPROMs (like that in your DIMM) without using\n"
|
|
|
|
"the -8 switch can also UNEXPECTEDLY write to them, so be sure to\n"
|
|
|
|
"use the -8 command param when required.\n"
|
|
|
|
"\n"
|
|
|
|
"Use -f to disable this warning message\n"
|
|
|
|
"\n"
|
|
|
|
"Press ENTER to continue or hit CTRL-C to exit\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
getchar();
|
2020-12-10 17:49:18 +03:00
|
|
|
return 1;
|
2014-02-15 16:22:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
|
|
|
|
{
|
|
|
|
int ch, i;
|
|
|
|
for(i = 0; i < size; ++i, ++addr)
|
|
|
|
{
|
|
|
|
die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error");
|
|
|
|
if(hex)
|
|
|
|
{
|
2020-12-10 17:49:18 +03:00
|
|
|
if( (i % 16) == 0 )
|
2014-02-15 16:22:03 +04:00
|
|
|
printf("\n %.4x| ", addr);
|
2020-12-10 17:49:18 +03:00
|
|
|
else if( (i % 8) == 0 )
|
2014-02-15 16:22:03 +04:00
|
|
|
printf(" ");
|
|
|
|
printf("%.2x ", ch);
|
2020-12-10 17:49:18 +03:00
|
|
|
} else
|
2014-02-15 16:22:03 +04:00
|
|
|
putchar(ch);
|
|
|
|
}
|
|
|
|
if(hex)
|
|
|
|
printf("\n\n");
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:49:18 +03:00
|
|
|
int write_to_eeprom(struct eeprom *e, int addr, int timeout)
|
2014-02-15 16:22:03 +04:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
while((c = getchar()) != EOF)
|
|
|
|
{
|
|
|
|
print_info(".");
|
|
|
|
fflush(stdout);
|
|
|
|
die_if(eeprom_write_byte(e, addr++, c), "write error");
|
2020-12-10 17:49:18 +03:00
|
|
|
die_if(eeprom_wait_ready(e, timeout), "write timeout");
|
2014-02-15 16:22:03 +04:00
|
|
|
}
|
|
|
|
print_info("\n\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
struct eeprom e;
|
|
|
|
int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen;
|
2020-12-10 17:49:18 +03:00
|
|
|
int timeout = 10;
|
2014-02-15 16:22:03 +04:00
|
|
|
char *device, *arg = 0, *i2c_addr_s;
|
|
|
|
struct stat st;
|
|
|
|
int eeprom_type = 0;
|
|
|
|
|
|
|
|
op = want_hex = dummy = force = sixteen = 0;
|
|
|
|
g_quiet = 0;
|
|
|
|
|
2020-12-10 18:17:25 +03:00
|
|
|
while((ret = getopt(argc, argv, "1:8fr:qhw:xdt:")) != -1)
|
2014-02-15 16:22:03 +04:00
|
|
|
{
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case '1':
|
|
|
|
usage_if(*optarg != '6' || strlen(optarg) != 1);
|
|
|
|
die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice");
|
2020-12-10 17:49:18 +03:00
|
|
|
eeprom_type = EEPROM_TYPE_16BIT_ADDR;
|
2014-02-15 16:22:03 +04:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
want_hex++;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dummy++;
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice");
|
|
|
|
eeprom_type = EEPROM_TYPE_8BIT_ADDR;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
g_quiet++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage_if(1);
|
|
|
|
break;
|
2020-12-10 17:49:18 +03:00
|
|
|
case 't':
|
|
|
|
timeout = atoi(optarg);
|
|
|
|
break;
|
2014-02-15 16:22:03 +04:00
|
|
|
default:
|
2020-12-10 17:49:18 +03:00
|
|
|
die_if(op != 0, "Both read and write requested");
|
2014-02-15 16:22:03 +04:00
|
|
|
arg = optarg;
|
|
|
|
op = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!eeprom_type)
|
|
|
|
eeprom_type = EEPROM_TYPE_8BIT_ADDR; // default
|
|
|
|
|
2020-12-10 17:49:18 +03:00
|
|
|
usage_if(op == 0); // no switches
|
2014-02-15 16:22:03 +04:00
|
|
|
// set device and i2c_addr reading from cmdline or env
|
|
|
|
device = i2c_addr_s = 0;
|
|
|
|
switch(argc - optind)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
device = getenv(ENV_DEV);
|
|
|
|
i2c_addr_s = getenv(ENV_I2C_ADDR);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(stat(argv[optind], &st) != -1)
|
|
|
|
{
|
|
|
|
device = argv[optind];
|
|
|
|
i2c_addr_s = getenv(ENV_I2C_ADDR);
|
|
|
|
} else {
|
|
|
|
device = getenv(ENV_DEV);
|
|
|
|
i2c_addr_s = argv[optind];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
device = argv[optind++];
|
|
|
|
i2c_addr_s = argv[optind];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_if(1);
|
|
|
|
}
|
|
|
|
usage_if(!device || !i2c_addr_s);
|
|
|
|
i2c_addr = strtoul(i2c_addr_s, 0, 0);
|
|
|
|
|
|
|
|
print_info("eeprog %s, a 24Cxx EEPROM reader/writer\n", VERSION);
|
|
|
|
print_info("Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n");
|
2020-12-10 17:49:18 +03:00
|
|
|
print_info(" Bus: %s, Address: 0x%x, Mode: %dbit\n",
|
|
|
|
device, i2c_addr,
|
2014-02-15 16:22:03 +04:00
|
|
|
(eeprom_type == EEPROM_TYPE_8BIT_ADDR ? 8 : 16) );
|
|
|
|
if(dummy)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Dummy mode selected, nothing done.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-10 17:49:18 +03:00
|
|
|
die_if(eeprom_open(device, i2c_addr, eeprom_type, &e) < 0,
|
2014-02-15 16:22:03 +04:00
|
|
|
"unable to open eeprom device file "
|
|
|
|
"(check that the file exists and that it's readable)");
|
|
|
|
switch(op)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
if(force == 0)
|
|
|
|
confirm_action();
|
|
|
|
size = 1; // default
|
|
|
|
parse_arg(arg, &memaddr, &size);
|
|
|
|
print_info(" Reading %d bytes from 0x%x\n", size, memaddr);
|
|
|
|
read_from_eeprom(&e, memaddr, size, want_hex);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if(force == 0)
|
|
|
|
confirm_action();
|
|
|
|
parse_arg(arg, &memaddr, &size);
|
2020-12-10 17:49:18 +03:00
|
|
|
print_info(" Writing stdin starting at address 0x%x with timeout %dms\n",
|
|
|
|
memaddr, timeout);
|
|
|
|
write_to_eeprom(&e, memaddr, timeout);
|
2014-02-15 16:22:03 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_if(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
eeprom_close(&e);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|