Skip to contentSkip to conversation

power_supply: add eeprom dump file to olpc_battery's sysfs

PATCH Applied Andres Salomon 2 participants 3 messages Apr 30, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 power_supply: add eeprom dump file to olpc_battery's sysfs

This allows you to dump 0x60 bytes from the battery's EEPROM (starting at address 0x20). Note that it does an EC command for each byte, so it's pretty slow. OTOH, if you want to grab just a single byte from somewhere in the EEPROM, you can do something like:

dd bs=1 count=1 skip=16 if=/sys/class/power_supply/olpc-battery/eeprom | od -x

Userspace battery collection/logging information needs this.

---
 drivers/power/olpc_battery.c |   49 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)
drivers/power/olpc_battery.c +49 -0
index 9d9dd09..e5ecf27 100644
@@ -283,6 +283,48 @@ static enum power_supply_property olpc_bat_props[] = {
 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 };
 
+/* EEPROM reading goes completely around the power_supply API, sadly */
+
+#define EEPROM_START	0x20
+#define EEPROM_END	0x80
+#define EEPROM_SIZE	(EEPROM_END - EEPROM_START)
+
+static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
+		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
+{
+	uint8_t ec_byte;
+	int ret, end;
+
+	if (off >= EEPROM_SIZE)
+		return 0;
+	if (off + count > EEPROM_SIZE)
+		count = EEPROM_SIZE - off;
+
+	end = EEPROM_START + off + count;
+	for (ec_byte = EEPROM_START + off; ec_byte < end; ec_byte++) {
+		ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1,
+				&buf[ec_byte - EEPROM_START], 1);
+		if (ret) {
+			printk(KERN_ERR "olpc-battery:  EC command "
+					"EC_BAT_EEPROM @ 0x%x failed -"
+					" %d!\n", ec_byte, ret);
+			return -EIO;
+		}
+	}
+
+	return count;
+}
+
+static struct bin_attribute olpc_bat_eeprom = {
+	.attr = {
+		.name = "eeprom",
+		.mode = S_IRUGO,
+		.owner = THIS_MODULE,
+	},
+	.size = 0,
+	.read = olpc_bat_eeprom_read,
+};
+
 /*********************************************************************
  *		Initialisation
  *********************************************************************/
@@ -336,8 +378,14 @@ static int __init olpc_bat_init(void)
 	if (ret)
 		goto battery_failed;
 
+	ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
+	if (ret)
+		goto eeprom_failed;
+
 	goto success;
 
+eeprom_failed:
+	power_supply_unregister(&olpc_bat);
 battery_failed:
 	power_supply_unregister(&olpc_ac);
 ac_failed:
@@ -348,6 +396,7 @@ success:
 
 static void __exit olpc_bat_exit(void)
 {
+	device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
 	power_supply_unregister(&olpc_bat);
 	power_supply_unregister(&olpc_ac);
 	platform_device_unregister(bat_pdev);
Signature · 1 line
1.5.5
#1 lore raw
Quoted 27 lines · On Wed, 30 Apr 2008 16:30:08 -0400 Andres Salomon <[email protected]> wrote:

This allows you to dump 0x60 bytes from the battery's EEPROM (starting at address 0x20). Note that it does an EC command for each byte, so it's pretty slow. OTOH, if you want to grab just a single byte from somewhere in the EEPROM, you can do something like:

dd bs=1 count=1 skip=16 if=/sys/class/power_supply/olpc-battery/eeprom | od -x

Userspace battery collection/logging information needs this.

---
 drivers/power/olpc_battery.c |   49 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)
drivers/power/olpc_battery.c +4 -0
index 9d9dd09..e5ecf27 100644
@@ -283,6 +283,48 @@ static enum power_supply_property olpc_bat_props[] = {
 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 };
 
+/* EEPROM reading goes completely around the power_supply API, sadly */
+
+#define EEPROM_START	0x20
+#define EEPROM_END	0x80

drivers/power/olpc_battery.c static enum power_supply_property olpc_bat_props[] = { · line 299

Quoted 10 lines
drivers/power/olpc_battery.c +10 -0
+#define EEPROM_SIZE	(EEPROM_END - EEPROM_START)
+
+static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
+		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
+{
+	uint8_t ec_byte;
+	int ret, end;
+
+	if (off >= EEPROM_SIZE)
+		return 0;

loff_t is signed.

Happily, the VFS prevents negative loff_t's from being passed into read() handlers.

drivers/power/olpc_battery.c static enum power_supply_property olpc_bat_props[] = { · line 301

Quoted 2 lines
drivers/power/olpc_battery.c +2 -0
+	if (off + count > EEPROM_SIZE)
+		count = EEPROM_SIZE - off;

But the vfs doesn't check for (offset+len) going negative (I think?)

However you got lucky (or smart ;)) - size_t is unsigned so the comparison will dtrt.

Plus the first if' will prevent us getting here with huge-nearly-negative off'.

This stuff is harder than it should be :(

#2 lore raw
Quoted 51 lines · On Wed, 30 Apr 2008 14:53:11 -0700 Andrew Morton <[email protected]>…
Quoted 27 lines · On Wed, 30 Apr 2008 16:30:08 -0400 Andres Salomon <[email protected]> wrote:

This allows you to dump 0x60 bytes from the battery's EEPROM (starting at address 0x20). Note that it does an EC command for each byte, so it's pretty slow. OTOH, if you want to grab just a single byte from somewhere in the EEPROM, you can do something like:

dd bs=1 count=1 skip=16 if=/sys/class/power_supply/olpc-battery/eeprom | od -x

Userspace battery collection/logging information needs this.

---
 drivers/power/olpc_battery.c |   49 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)
drivers/power/olpc_battery.c +4 -0
index 9d9dd09..e5ecf27 100644
@@ -283,6 +283,48 @@ static enum power_supply_property olpc_bat_props[] = {
 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 };
 
+/* EEPROM reading goes completely around the power_supply API, sadly */
+
+#define EEPROM_START	0x20
+#define EEPROM_END	0x80

drivers/power/olpc_battery.c static enum power_supply_property olpc_bat_props[] = { · line 299

Quoted 10 lines
drivers/power/olpc_battery.c +10 -0
+#define EEPROM_SIZE	(EEPROM_END - EEPROM_START)
+
+static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
+		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
+{
+	uint8_t ec_byte;
+	int ret, end;
+
+	if (off >= EEPROM_SIZE)
+		return 0;

loff_t is signed.

Happily, the VFS prevents negative loff_t's from being passed into read() handlers.

drivers/power/olpc_battery.c static enum power_supply_property olpc_bat_props[] = { · line 301

Quoted 2 lines
drivers/power/olpc_battery.c +2 -0
+	if (off + count > EEPROM_SIZE)
+		count = EEPROM_SIZE - off;

But the vfs doesn't check for (offset+len) going negative (I think?)

Quoted 8 lines

However you got lucky (or smart ;)) - size_t is unsigned so the comparison will dtrt.

Plus the first if' will prevent us getting here with huge-nearly-negative off'.

This stuff is harder than it should be :(

Agreed. I actually tested it a bunch with of different i/o back in Feb in an attempt to break it. I think it looks horrid; if there's a better interface for this, I'd be more than happy to switch.