15 #include "eepromread.h"
17 static int __eeprom_read128(int fd
, i2c_addr_t addr
, uint16_t memaddr
,
18 void *buf
, size_t buflen
, int flags
, enum device_types device_type
);
19 static int eeprom_dump(int fd
, i2c_addr_t addr
, int flags
,
20 enum device_types device_type
);
22 #define DEFAULT_I2C_DEVICE "/dev/i2c-1"
23 #define DEFAULT_I2C_ADDRESS 0x50
26 * The /dev interface only supports 128 byte reads/writes and the EEPROM is
27 * larger, so to read the whole EEPROM, the task is broken down into 128 byte
28 * chunks in eeprom_read(). __eeprom_read128() does the actual ioctl() to do
33 __eeprom_read128(int fd
, i2c_addr_t addr
, uint16_t memaddr
, void *buf
,
34 size_t buflen
, int flags
, enum device_types device_type
)
37 minix_i2c_ioctl_exec_t ioctl_exec
;
39 if (buflen
> I2C_EXEC_MAX_BUFLEN
|| buf
== NULL
40 || ((memaddr
+ buflen
) < memaddr
)) {
45 /* if /dev/eeprom, then use read() */
46 if (device_type
== EEPROM_DEVICE
) {
50 offset
= lseek(fd
, memaddr
, SEEK_SET
);
51 if (offset
!= memaddr
) {
55 return read(fd
, buf
, buflen
);
58 /* else /dev/i2c, use i2c_ioctl_exec_t interface */
59 memset(&ioctl_exec
, '\0', sizeof(minix_i2c_ioctl_exec_t
));
61 ioctl_exec
.iie_op
= I2C_OP_READ_WITH_STOP
;
62 ioctl_exec
.iie_addr
= addr
;
64 /* set the address to read from */
65 if ((BDEV_NOPAGE
& flags
) == BDEV_NOPAGE
) {
66 /* reading within the current page */
67 ioctl_exec
.iie_cmd
[0] = (memaddr
& 0xff);
68 ioctl_exec
.iie_cmdlen
= 1;
70 /* reading from device with multiple pages */
71 ioctl_exec
.iie_cmd
[0] = ((memaddr
>> 8) & 0xff);
72 ioctl_exec
.iie_cmd
[1] = (memaddr
& 0xff);
73 ioctl_exec
.iie_cmdlen
= 2;
75 ioctl_exec
.iie_buflen
= buflen
;
77 r
= ioctl(fd
, MINIX_I2C_IOCTL_EXEC
, &ioctl_exec
);
82 /* call was good, copy results to caller's buffer */
83 memcpy(buf
, ioctl_exec
.iie_buf
, buflen
);
89 eeprom_read(int fd
, i2c_addr_t addr
, uint16_t memaddr
, void *buf
,
90 size_t buflen
, int flags
, enum device_types device_type
)
95 if (buf
== NULL
|| ((memaddr
+ buflen
) < memaddr
)) {
100 for (i
= 0; i
< buflen
; i
+= 128) {
102 r
= __eeprom_read128(fd
, addr
, memaddr
+ i
, buf
+ i
,
103 ((buflen
- i
) < 128) ? (buflen
- i
) : 128, flags
,
114 * Read 256 bytes and print it to the screen in HEX and ASCII.
117 eeprom_dump(int fd
, i2c_addr_t addr
, int flags
, enum device_types device_type
)
122 memset(buf
, '\0', 256);
124 r
= eeprom_read(fd
, addr
, 0x0000, buf
, 256, flags
, device_type
);
129 /* print table header */
130 for (i
= 0; i
< 2; i
++) {
132 for (j
= 0x0; j
<= 0xf; j
++) {
141 /* print table data */
142 for (i
= 0x00; i
< 0xff; i
+= 0x10) {
147 /* row data (in hex) */
148 for (j
= 0x0; j
<= 0xf; j
++) {
149 printf(" %02x", buf
[i
+ j
]);
154 /* row data (in ASCII) */
155 for (j
= 0x0; j
<= 0xf; j
++) {
156 if (isprint(buf
[i
+ j
])) {
157 printf("%c", buf
[i
+ j
]);
170 main(int argc
, char *argv
[])
173 int ch
, iflag
= 0, read_flags
= 0;
174 char *device
= DEFAULT_I2C_DEVICE
;
175 i2c_addr_t address
= DEFAULT_I2C_ADDRESS
;
176 enum device_types device_type
= DEFAULT_DEVICE
;
180 while ((ch
= getopt(argc
, argv
, "a:f:in")) != -1) {
183 address
= strtol(optarg
, NULL
, 0x10);
192 read_flags
|= BDEV_NOPAGE
;
199 /* determine whether to use /dev/i2c or /dev/eeprom interface */
201 strstr(device
, "i2c") == NULL
? EEPROM_DEVICE
: I2C_DEVICE
;
203 fd
= open(device
, O_RDWR
);
205 fprintf(stderr
, "open(): %s\n", strerror(errno
));
210 r
= board_info(fd
, address
, read_flags
, device_type
);
212 fprintf(stderr
, "board_info(): %s\n", strerror(errno
));
216 r
= eeprom_dump(fd
, address
, read_flags
, device_type
);
218 fprintf(stderr
, "eeprom_dump(): %s\n",
226 fprintf(stderr
, "close(): %s\n", strerror(errno
));